
    
3j                     D   S SK JrJr  S SKrS SKrS SKJr  S SKJ	r	  SSK
Jr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJrJr  SSKJr  SSKJrJr  SSKJrJrJrJ r J!r!J"r"  SSK#J$r$  \" 5       (       a  S SK%J&s  J'r(  Sr)OSr)\RT                  " \+5      r,Sr- " S S\\5      r.g)    )AnyCallableN)AutoTokenizer)SmolLM3ForCausalLM   )VaeImageProcessor)FluxLoraLoaderMixin)AutoencoderKLWan)BriaFiboTransformer2DModel)BriaFiboPipelineOutput)calculate_shiftretrieve_timesteps)DiffusionPipeline)FlowMatchEulerDiscreteSchedulerKarrasDiffusionSchedulers)USE_PEFT_BACKENDis_torch_xla_availableloggingreplace_example_docstringscale_lora_layersunscale_lora_layers)randn_tensorTFa  
    Example:
    ```python
    import torch
    from diffusers import BriaFiboPipeline
    from diffusers.modular_pipelines import ModularPipeline

    torch.set_grad_enabled(False)
    vlm_pipe = ModularPipeline.from_pretrained("briaai/FIBO-VLM-prompt-to-JSON", trust_remote_code=True)

    pipe = BriaFiboPipeline.from_pretrained(
        "briaai/FIBO",
        trust_remote_code=True,
        torch_dtype=torch.bfloat16,
    )
    pipe.enable_model_cpu_offload()

    with torch.inference_mode():
        # 1. Create a prompt to generate an initial image
        output = vlm_pipe(prompt="a beautiful dog")
        json_prompt_generate = output.values["json_prompt"]

        # Generate the image from the structured json prompt
        results_generate = pipe(prompt=json_prompt_generate, num_inference_steps=50, guidance_scale=5)
        results_generate.images[0].save("image_generate.png")
    ```
c            '          \ rS rSrSrSrSS/rS\S\\	-  S\
S	\S
\4
S jr    S8S\\\   -  S\S\S\R$                  S-  S\R&                  S-  4
S jjr\S9S j5       r        S:S\\\   -  S\R$                  S-  S\S\S\\\   -  S-  S\R0                  S-  S\R0                  S-  S\S\S-  4S jjr\S 5       r\S 5       r\S 5       r\S 5       r\S  5       r\S! 5       r \S" 5       r!\S# 5       r"\S$ 5       r#  S;S& jr$\S' 5       r%\RL                  " 5       \'" \(5      SSSS(SSSSSSSSS)S*SSS/SS%4S\\\   -  S+\S-  S,\S-  S-\S.\\   S\S\\\   -  S-  S\S-  S/\RR                  \\RR                     -  S-  S\R0                  S-  S\R0                  S-  S\R0                  S-  S0\S-  S1\*S2\+\\,4   S-  S3\-\\/S4   S-  S4\\   S\4$S5 jj5       5       r.     S<S6 jr/S7r0g)=BriaFiboPipelineL   af  
Args:
    transformer (`BriaFiboTransformer2DModel`):
        The transformer model for 2D diffusion modeling.
    scheduler (`FlowMatchEulerDiscreteScheduler` or `KarrasDiffusionSchedulers`):
        Scheduler to be used with `transformer` to denoise the encoded latents.
    vae (`AutoencoderKLWan`):
        Variational Auto-Encoder for encoding and decoding images to and from latent representations.
    text_encoder (`SmolLM3ForCausalLM`):
        Text encoder for processing input prompts.
    tokenizer (`AutoTokenizer`):
        Tokenizer used for processing the input text prompts for the text_encoder.
z=text_encoder->text_encoder_2->image_encoder->transformer->vaelatentsprompt_embedstransformer	schedulervaetext_encoder	tokenizerc                 |    U R                  UUUUUS9  SU l        [        U R                  S-  S9U l        SU l        g )N)r    r!   r"   r   r         )vae_scale_factor@   )register_modulesr&   r   image_processordefault_sample_size)selfr   r   r    r!   r"   s         j/home/wildlama/miniconda3/lib/python3.13/site-packages/diffusers/pipelines/bria_fibo/pipeline_bria_fibo.py__init__BriaFiboPipeline.__init__^   sQ     	%# 	 	
 !#0$BWBWZ[B[\#%        Npromptnum_images_per_promptmax_sequence_lengthdevicedtypec           	        ^^ T=(       d    U R                   mU=(       d    U R                  R                  n[        U[        5      (       a  U/OUnU(       d  [        S5      e[        U5      nSnTb  TO[        R                  " S5      n[        U[        R                  5      (       d  [        R                  " U5      n[        S U 5       5      (       a>  [        R                  " US4U[        R                  US9n	[        R                  " U	5      n
OU R                  USUSSS	S
9nUR                  R                  U5      n	UR                   R                  U5      n
[#        S U 5       5      (       a?  [        R$                  " U Vs/ s H  oS:H  PM	     sn[        R&                  US9nXyU'   SX'   U R                  U	U
SS9nUR(                  n[        R*                  " US   US   /SS9nUR                  TUS9nUR-                  TSS9n[/        UU4S jU 5       5      nU
R-                  TSS9R                  TS9n
UX4$ s  snf )Nz7`prompt` must be a non-empty string or list of strings.i  cpuc              3   *   #    U  H	  oS :H  v   M     g7f N .0ps     r,   	<genexpr>5BriaFiboPipeline.get_prompt_embeds.<locals>.<genexpr>   s     '1Bw   r0   r5   r4   longestTpt)padding
max_length
truncationadd_special_tokensreturn_tensorsc              3   *   #    U  H	  oS :H  v   M     g7fr9   r;   r<   s     r,   r?   r@      s     +Fq7FrA   r:   )attention_maskoutput_hidden_statesdimr4   r5   r   c              3   \   >#    U  H!  oR                  TS S9R                  TS9v   M#     g7f)r   rO   r4   N)repeat_interleaveto)r=   layerr4   r2   s     r,   r?   r@      s3      
anX]##$9q#ADDFDSans   ),rS   )_execution_devicer!   r5   
isinstancestr
ValueErrorlentorchr4   allfulllong	ones_liker"   	input_idsrU   rK   anytensorboolhidden_statescatrT   tuple)r+   r1   r2   r3   r4   r5   
batch_sizebot_token_idtext_encoder_devicera   rK   	tokenizedr>   
empty_rowsencoder_outputsre   r   s     ` `            r,   get_prompt_embeds"BriaFiboPipeline.get_prompt_embedsr   s5    14110**00'44&&VWW[
(.(:fU@S-u||<<"',,/B"C''''

J?L

[noI"__Y7N!.#'# ' I "++../BCI&55889LMN+F+++"\\F*CFq7F*C5::^qr
(4*%-.*++)!% , 

 (55		=#4mB6G"HbQ%((e(D%778MST7U 
an
 
 (99:OUV9WZZbhZim;;) +Ds   
Ic                    U R                   u  p4nUc,  [        R                  " X44U R                  U R                  S9nO$UR                  U R                  U R                  S9nX:  a  [        S5      eX:  a  X-
  n[        R                  " X6U4U R                  U R                  S9n[        R                  " X/SS9n [        R                  " X64U R                  U R                  S9n[        R                  " X(/SS9nX4$ )NrB   rQ   zE`max_tokens` must be greater or equal to the current sequence length.r0   rO   )	shaper\   onesr5   r4   rU   rZ   zerosrf   )	r   
max_tokensrK   rh   seq_lenrP   
pad_lengthrE   mask_paddings	            r,   pad_embeddingBriaFiboPipeline.pad_embedding   s     $1#6#6 
S!"ZZ(=]EXEXanauauvN+..m6J6JR_ReRe.fNdee#-Jkk-]5H5HQ^QeQeG "II}&>AFM ;;(0C0CML`L`L #YY'E1MN,,r/        guidance_scalenegative_promptnegative_prompt_embeds
lora_scalec
                    U=(       d    U R                   nU	bI  [        U [        5      (       a4  Xl        U R                  b!  [
        (       a  [        U R                  U	5        [        U[        5      (       a  U/OUnUb  [        U5      n
OUR                  S   n
SnSnUcj  U R                  UUUUS9u  pmnUR                  U R                  R                  S9nU Vs/ s H%  oR                  U R                  R                  S9PM'     nnUS:  Ga  [        U[        5      (       a  US   c  SnU=(       d    Sn[        U[        5      (       a  X/-  OUnUb;  [        U5      [        U5      La$  [!        S[        U5       S[        U5       S	35      eU
[        U5      :w  a!  [#        S
U S[        U5       SU SU
 S3	5      eU R                  UUUUS9u  pnUR                  U R                  R                  S9nU Vs/ s H%  oR                  U R                  R                  S9PM'     nnU R                  b6  [        U [        5      (       a!  [
        (       a  [%        U R                  U	5        Ub$  UR                  UR&                  UR                  S9nUb  Ub$  UR                  UR&                  UR                  S9n[)        UR                  S   UR                  S   5      nU R+                  UUUS9u  pkW Vs/ s H  nU R+                  UU5      S   PM     nnU R+                  UUUS9u  p|W Vs/ s H  nU R+                  UU5      S   PM     nnO$UR                  S   nU R+                  UUUS9u  pkSnU R                  R                  n[,        R.                  " UR                  S   US5      R                  UUS9nUUUUUWU4$ s  snf s  snf s  snf s  snf )ar  
Args:
    prompt (`str` or `list[str]`, *optional*):
        prompt to be encoded
    device: (`torch.device`):
        torch device
    num_images_per_prompt (`int`):
        number of images that should be generated per prompt
    guidance_scale (`float`):
        Guidance scale for classifier free guidance.
    negative_prompt (`str` or `list[str]`, *optional*):
        The prompt or prompts not to guide the image generation. If not defined, one has to pass
        `negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `guidance_scale` is
        less than `1`).
    prompt_embeds (`torch.FloatTensor`, *optional*):
        Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not
        provided, text embeddings will be generated from `prompt` input argument.
    negative_prompt_embeds (`torch.FloatTensor`, *optional*):
        Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt
        weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input
        argument.
Nr   )r1   r2   r3   r4   r5   r0   r:   z?`negative_prompt` should be the same type to `prompt`, but got z != .z`negative_prompt`: z has batch size z, but `prompt`: zT. Please make sure that passed `negative_prompt` matches the batch size of `prompt`.rQ   )rK   r   )rW   rX   r	   _lora_scaler!   r   r   rY   r[   rq   rn   rU   r   r5   listtype	TypeErrorrZ   r   r4   maxrx   r\   rs   )r+   r1   r4   r2   r|   r}   r   r~   r3   r   rh   prompt_attention_masknegative_prompt_attention_maskprompt_layersrc   negative_prompt_layersrt   rV   r5   text_idss                       r,   encode_promptBriaFiboPipeline.encode_prompt   sw   D 1411 !j7J&K&K)   ,1A1A!$"3"3Z@'44&&VJ&,,Q/J $)-& BFBXBX&;$7	 CY C?M*? *,,43C3C3I3I,JMS`aS`YYT-=-=-C-CYDS`MaA/400_Q5G5O"$-3O@J?\_@`@`j+<<fuO!d6l$:O&OUVZ[jVkUl mV~Q(  s?33 )/)::J3K_J` ax/
| <33  ^b]s]s&&;$7	 ^t ^Z"<Z &<%>%>TEUEUE[E[%>%\"\r%s\rRXiid6F6F6L6Li&M\r"%s($ 3449I9I#D$5$5zB !,$9$<$<MDXDX`m`s`s$<$t!!--91O1R1R188@V@\@\ 2S 2. 399!<m>Q>QRS>TUJ373E3Ez:O 4F 40M TaaS`%T//zB1ES`MaEIEWEW&
Ca FX FB" ]s%s\rSXd&8&8
&KA&N\r"%s"&,,Q/J373E3Ez:O 4F 40M &*"!!'';;}2215z1EHHPV^cHd "!*"
 	
 b4 &t* b
 &ts   ,O ,O?O7Oc                     U R                   $ N)_guidance_scaler+   s    r,   r|   BriaFiboPipeline.guidance_scaleO  s    ###r/   c                     U R                   $ r   )_joint_attention_kwargsr   s    r,   joint_attention_kwargs'BriaFiboPipeline.joint_attention_kwargsW  s    +++r/   c                     U R                   $ r   )_num_timestepsr   s    r,   num_timestepsBriaFiboPipeline.num_timesteps[  s    """r/   c                     U R                   $ r   )
_interruptr   s    r,   	interruptBriaFiboPipeline.interrupt_  s    r/   c                     U R                   u  pEnX-  nX#-  nU R                  XAS-  US-  US-  SS5      n U R                  SSSSSS5      n U R                  XFS-  X5      n U $ )Nr%      r   r   r0   rz   )rq   viewpermutereshaper   heightwidthr&   rh   num_patcheschannelss          r,   _unpack_latents BriaFiboPipeline._unpack_latentsc  sy     -4MM)
+),,zQ;
HPQMSTVWX//!Q1a3//*5.A6Qr/   c                 ,   [         R                  " XS5      nUS   [         R                  " U5      S S 2S 4   -   US'   US   [         R                  " U5      S S S 24   -   US'   UR                  u  pgnUR	                  Xg-  U5      nUR                  X4S9$ )Nr   ).r0   ).r%   rQ   )r\   rs   arangerq   r   rU   )	rh   r   r   r4   r5   latent_image_idslatent_image_id_heightlatent_image_id_widthlatent_image_id_channelss	            r,   _prepare_latent_image_ids*BriaFiboPipeline._prepare_latent_image_idsq  s     !;;va8#3F#;ell6>RSTVZSZ>[#[ #3F#;ell5>QRVXYRY>Z#Z RbRhRhO7O+33":<T
  ""&">>r/   c                     U R                   u  pEnX-  nX#-  nU R                  XAX&5      n U R                  SSSS5      n U $ )Nr   r   r0   r%   )rq   r   r   r   s          r,   _unpack_latents_no_patch)BriaFiboPipeline._unpack_latents_no_patch  sI    ,3MM)
+),,z5C//!Q1-r/   c                 X    U R                  SSSS5      n U R                  XU-  U5      n U $ )Nr   r%   r   r0   )r   r   r   rh   num_channels_latentsr   r   s        r,   _pack_latents_no_patch'BriaFiboPipeline._pack_latents_no_patch  s0    //!Q1-//*un>RSr/   c                     U R                  XUS-  SUS-  S5      n U R                  SSSSSS5      n U R                  XS-  US-  -  US-  5      n U $ )Nr%   r   r   r0   r   rz   )r   r   r   r   s        r,   _pack_latentsBriaFiboPipeline._pack_latents  sg     ,,z1aQVZ[Q[]^_//!Q1a3//*{uz.JL`cdLder/   Fc
                    [        U5      U R                  -  n[        U5      U R                  -  nXX44n
Ub$  U R                  XXFU5      nUR                  XeS9U4$ [	        U[
        5      (       a*  [        U5      U:w  a  [        S[        U5       SU S35      e[        XXeS9nU	(       a/  U R                  XX#U5      nU R                  XS-  US-  Xe5      nX4$ U R                  XX#U5      nU R                  XXFU5      nX4$ )NrQ   z/You have passed a list of generators of length z+, but requested an effective batch size of z@. Make sure the batch size matches the length of the generators.)	generatorr4   r5   r%   )intr&   r   rU   rX   r   r[   rZ   r   r   r   )r+   rh   r   r   r   r5   r4   r   r   do_patchingrq   r   s               r,   prepare_latents BriaFiboPipeline.prepare_latents  s;    V 5 55E
d3336A#==jRWafg::V:9;KKKi&&3y>Z+GA#i.AQ R&<'gi 
 u&V((>R\abG#==jTU+W\`aWaciq
 (( 11'G[ejkG#==jRWafg((r/   c                     [         R                  " SX 5      n[         R                  " US:H  S[         R                  * 5      nU$ )Nz
bi,bj->bijr0   g        )r\   einsumwhereinf)rK   attention_matrixs     r,   _prepare_attention_mask(BriaFiboPipeline._prepare_attention_mask  s>     <<nU !;;!3

  r/      pilTr   r   num_inference_steps	timestepsr   output_typereturn_dictr   callback_on_step_end"callback_on_step_end_tensor_inputsc                    U=(       d    U R                   U R                  -  nU=(       d    U R                   U R                  -  nU R                  UUUUUUS9  X`l        Xl        SU l        Ub  [        U[        5      (       a  SnO3Ub!  [        U[        5      (       a  [        U5      nOUR                  S   nU R                  nU R                  b  U R                  R                  SS5      OSnU R                  UUUUUUUUUS9	u  nnnnnnnUR                  S   nUS:  al  [        R                   " X/SS9n[#        [        U5      5       Vs/ s H   n[        R                   " UU   UU   /SS9PM"     nn[        R                   " UU/SS9n[        U R$                  R&                  5      [        U R$                  R(                  5      -   n[        U5      U:  a  U[        U5      U-
  S nOUUS	   /U[        U5      -
  -  -   nU R$                  R*                  R,                  nU(       a  [/        US
-  5      nU R1                  UUUUUR2                  UU	U
U5	      u  n
n [        R4                  " U
R                  S   U
R                  S   /U
R2                  U
R6                  S9n!US:  a  U!R9                  SS5      n![        R                   " UU!/SS9n"U R;                  U"5      n"U"R=                  SS9R?                  U R$                  R2                  S9n"U R                  c  0 U l        U"U R                  S'   U(       a$  X R                  S-  -  X0R                  S-  -  -  n#OX R                  -  X0R                  -  -  n#[@        RB                  " SSU-  U5      n$[E        U#U RF                  R*                  RH                  U RF                  R*                  RJ                  U RF                  R*                  RL                  U RF                  R*                  RN                  5      n%[Q        U RF                  UUSU$U%S9u  pT[S        [        U5      X@RF                  RT                  -  -
  S5      n&[        U5      U l+        [        U R                  5      S:X  a  U S   n [        UR                  5      S:X  a  US   nU RY                  US9 n'[[        U5       GH  u  nn(U R\                  (       a  M  US:  a  [        R                   " U
/S-  5      OU
n)U(R_                  U)R                  S   5      R?                  U)R6                  U)R2                  S9n*U R%                  U)U*UUU R                  SUU S9S   n+US:  a)  U+Ra                  S5      u  n,n-U,U Rb                  U-U,-
  -  -   n+U
R2                  n.U RF                  Re                  U+U(U
SS9S   n
U
R2                  U.:w  a>  [        Rf                  Rh                  Rk                  5       (       a  U
R?                  U.5      n
Ub\  0 n/U H  n0[m        5       U0   U/U0'   M     U" U UU(U/5      n1U1Ro                  SU
5      n
U1Ro                  SU5      nU1Ro                  SU5      nU[        U5      S-
  :X  d)  US-   U&:  a0  US-   U RF                  RT                  -  S:X  a  U'Rq                  5         [r        (       d  GM  [t        Rv                  " 5         GM     SSS5        US:X  a  U
n2GO U(       a  U Ry                  XX0R                  5      n
OU R{                  XX0R                  5      n
U
R=                  SS9n
U
S   R6                  n3U
S   R2                  n.[        R|                  " U R~                  R*                  R                  5      R                  SU R~                  R*                  R                  SSS5      R?                  U3U.5      n4S[        R|                  " U R~                  R*                  R                  5      R                  SU R~                  R*                  R                  SSS5      R?                  U3U.5      -  n5U
 V6s/ s H  n6U6U5-  U4-   PM     n7n6[        R                   " U7SS9n7/ n2U7 Hg  n8U R~                  R                  U8R=                  S5      SS9S   n9U R                  R                  U9R                  SS9US9n9U2R                  U95        Mi     [        U25      S:X  a  U2S   n2O[@        R                  " U2SS9n2U R                  5         U(       d  U24$ [        U2S9$ s  snf ! , (       d  f       GNa= fs  sn6f )a   
Function invoked when calling the pipeline for generation.

Args:
    prompt (`str` or `list[str]`, *optional*):
        The prompt or prompts to guide the image generation. If not defined, one has to pass `prompt_embeds`.
        instead.
    height (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor):
        The height in pixels of the generated image. This is set to 1024 by default for the best results.
    width (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor):
        The width in pixels of the generated image. This is set to 1024 by default for the best results.
    num_inference_steps (`int`, *optional*, defaults to 50):
        The number of denoising steps. More denoising steps usually lead to a higher quality image at the
        expense of slower inference.
    timesteps (`list[int]`, *optional*):
        Custom timesteps to use for the denoising process with schedulers which support a `timesteps` argument
        in their `set_timesteps` method. If not defined, the default behavior when `num_inference_steps` is
        passed will be used. Must be in descending order.
    guidance_scale (`float`, *optional*, defaults to 5.0):
        Guidance scale as defined in [Classifier-Free Diffusion
        Guidance](https://huggingface.co/papers/2207.12598). `guidance_scale` is defined as `w` of equation 2.
        of [Imagen Paper](https://huggingface.co/papers/2205.11487). Guidance scale is enabled by setting
        `guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to
        the text `prompt`, usually at the expense of lower image quality.
    negative_prompt (`str` or `list[str]`, *optional*):
        The prompt or prompts not to guide the image generation. If not defined, one has to pass
        `negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `guidance_scale` is
        less than `1`).
    num_images_per_prompt (`int`, *optional*, defaults to 1):
        The number of images to generate per prompt.
    generator (`torch.Generator` or `list[torch.Generator]`, *optional*):
        One or a list of [torch generator(s)](https://pytorch.org/docs/stable/generated/torch.Generator.html)
        to make generation deterministic.
    latents (`torch.FloatTensor`, *optional*):
        Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image
        generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
        tensor will ge generated by sampling using the supplied random `generator`.
    prompt_embeds (`torch.FloatTensor`, *optional*):
        Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not
        provided, text embeddings will be generated from `prompt` input argument.
    negative_prompt_embeds (`torch.FloatTensor`, *optional*):
        Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt
        weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input
        argument.
    output_type (`str`, *optional*, defaults to `"pil"`):
        The output format of the generate image. Choose between
        [PIL](https://pillow.readthedocs.io/en/stable/): `PIL.Image.Image` or `np.array`.
    return_dict (`bool`, *optional*, defaults to `True`):
        Whether or not to return a [`~pipelines.stable_diffusion_xl.StableDiffusionXLPipelineOutput`] instead
        of a plain tuple.
    joint_attention_kwargs (`dict`, *optional*):
        A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under
        `self.processor` in
        [diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py).
    callback_on_step_end (`Callable`, *optional*):
        A function that calls at the end of each denoising steps during the inference. The function is called
        with the following arguments: `callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int,
        callback_kwargs: Dict)`. `callback_kwargs` will include a list of all tensors as specified by
        `callback_on_step_end_tensor_inputs`.
    callback_on_step_end_tensor_inputs (`List`, *optional*):
        The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list
        will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the
        `._callback_tensor_inputs` attribute of your pipeline class.
    max_sequence_length (`int` defaults to 3000): Maximum sequence length to use with the `prompt`.
    do_patching (`bool`, *optional*, defaults to `False`): Whether to use patching.
Examples:
  Returns:
    [`~pipelines.flux.BriaFiboPipelineOutput`] or `tuple`: [`~pipelines.flux.BriaFiboPipelineOutput`] if
    `return_dict` is True, otherwise a `tuple`. When returning a tuple, the first element is a list with the
    generated images.
)r1   r   r   r   r   r3   FNr0   r   scale)	r1   r}   r|   r   r~   r4   r3   r2   r   rO   rM   r   rB   r%   r   rK   g      ?)r   r4   r   sigmasmur   )totalrQ   )re   timestepencoder_hidden_statestext_encoder_layersr   r   txt_idsimg_ids)r   r   r   r~   latent)r   )axis)images)Lr*   r&   check_inputsr   r   r   rX   rY   r   r[   rq   rW   r   getr   r\   rf   ranger   transformer_blockssingle_transformer_blocksconfigin_channelsr   r   r5   rr   r4   repeatr   	unsqueezerU   nplinspacer   r   base_image_seq_lenmax_image_seq_len
base_shift	max_shiftr   r   orderr   progress_bar	enumerater   expandchunkr|   stepbackendsmpsis_availablelocalspopupdateXLA_AVAILABLExm	mark_stepr   r   rc   r    latents_meanr   z_dimlatents_stddecoder)   postprocesssqueezeappendstackmaybe_free_model_hooksr   ):r+   r1   r   r   r   r   r|   r}   r2   r   r   r   r~   r   r   r   r   r   r3   r   rh   r4   r   r   r   r   r   r   prompt_batch_sizeitotal_num_layers_transformerr   r   latent_attention_maskrK   ru   r   r   num_warmup_stepsr   tlatent_model_inputr   
noise_prednoise_pred_uncondnoise_pred_textlatents_dtypecallback_kwargskcallback_outputsimagelatents_devicer   r  r   latents_scaledscaled_latent
curr_images:                                                             r,   __call__BriaFiboPipeline.__call__  s.	   @ K433d6K6KKI11D4I4II 	'/Q 3 	 	
  .'=$ *VS"9"9JJvt$<$<VJ&,,Q/J'' ?C>Y>Y>eD''++GT:ko 	 +)'#9 3"7!  

	
"!*" *//2A!II'=&MSTUMY^_bcp_qYrYrTU		1!4mA6FGQOYr   %*II/MOd.ekl$m!'*4+;+;+N+N'ORU66S
 (
$ }!==)#m*<?[*[*]^M *]2->,?C_befsbtCt,uuM  $//66BB#&';a'?#@ $($8$8 
%
!! !&

]]1w}}Q/0gnn!
 A$9$@$@A$F!$9;P#QWXY55nE'11a18;;$BRBRBXBX;Y''/+-D(9G$$%56 "7"7!";<K`K`cdKdAefG!6!665DYDY;YZGS!&9"9;NONN!!44NN!!33NN!!,,NN!!++
 *<NN 3*
&	 s9~0CnnFZFZ0ZZ\]^!)n %%&!+/2x~~!#{H %89\!),1>> BPRSASUYYy1}%=Y`" 88$6$<$<Q$?@CC-44<N<T<T D 
 "--"4%*7(5+/+F+F %$, . 	 	
 "A%9C9I9I!9L6%!2T5H5HO^oLo5p!pJ !(..--j!WRW-XYZ[==M1~~))6688")**]";'3&(O?-3Xa[* @';D!Q'X$.229gFG$4$8$8-$XM-=-A-ABZ\r-s* I**A9I/IqSTuX\XfXfXlXlNlpqNq '') =LLNk - :p ("E ..wG\G\]77PePef''A'.G$QZ..N#AJ,,MTXX__99:a..1a8NM2 
 TXX__-H-H I N NqRVRZRZRaRaRgRgijlmop q t t! K QXXPWff{2\APWNX"YY~1=NE!/!XX__]-D-DQ-GUZ_[\]^
!11==j>P>PUV>P>Wep=q
Z( "0 5zQaQ/ 	##%8O%U33il :9X Ys   'e
He	e	e	
ec	           
        ^  US-  S:w  d	  US-  S:w  a  [        SU SU S35      eUbW  [        U 4S jU 5       5      (       d=  [        ST R                   SU V	s/ s H  oT R                  ;  d  M  U	PM     sn	 35      eUb  Ub  [        S	U S
U S35      eUc  Uc  [        S5      eUbA  [        U[        5      (       d,  [        U[
        5      (       d  [        S[        U5       35      eUb  Ub  [        SU SU S35      eUbC  Ub@  UR                  UR                  :w  a&  [        SUR                   SUR                   S35      eUb  US:  a  [        SU 35      eg g s  sn	f )Nr$   r   z8`height` and `width` have to be divisible by 16 but are z and r   c              3   @   >#    U  H  oTR                   ;   v   M     g 7fr   )_callback_tensor_inputs)r=   r  r+   s     r,   r?   0BriaFiboPipeline.check_inputs.<locals>.<genexpr>$  s      F
7Y!---7Ys   z2`callback_on_step_end_tensor_inputs` has to be in z, but found zCannot forward both `prompt`: z and `prompt_embeds`: z2. Please make sure to only forward one of the two.zeProvide either `prompt` or `prompt_embeds`. Cannot leave both `prompt` and `prompt_embeds` undefined.z2`prompt` has to be of type `str` or `list` but is z'Cannot forward both `negative_prompt`: z and `negative_prompt_embeds`: zu`prompt_embeds` and `negative_prompt_embeds` must have the same shape when passed directly, but got: `prompt_embeds` z != `negative_prompt_embeds` r{   z9`max_sequence_length` cannot be greater than 3000 but is )rZ   r]   r  rX   rY   r   r   rq   )
r+   r1   r   r   r}   r   r~   r   r3   r  s
   `         r,   r   BriaFiboPipeline.check_inputs  s#    B;!urzQWX^W__dejdkklmnn-9# F
7YF
 C
 C
 DTEaEaDbbn  |^  pH  |^vw  ko  kG  kG  bGpq  |^  pH  oI  J  -";08N}o ^0 0  ^ 5w  FC)@)@TZ\`IaIaQRVW]R^Q_`aa&+A+M9/9J K*++]_ 
 $)?)K""&<&B&BB --:-@-@,A B.445Q8  */BT/IXYlXmnoo 0J*; pHs   E$1E$)r   r   r   r   r   r*   r)   r&   )r0   i   NNr   )Nr0   rz   NNNr{   N)NF)NNNNN)1__name__
__module____qualname____firstlineno____doc__model_cpu_offload_seqr  r   r   r   r
   r   r   r-   rY   r   r   r\   r4   r5   rn   staticmethodrx   floatFloatTensorr   propertyr|   r   r   r   r   r   r   r   r   r   r   no_gradr   EXAMPLE_DOC_STRING	Generatorrd   dictr   r   r  r   __static_attributes__r;   r/   r,   r   r   L   s     \(/:&/& 35NN& 	&
 )& !&. &'#'&*$(:<d3i:<  #:< !	:<
 t#:< {{T!:<x - -: '+%& !2626;?#'#'D
d3iD
 t#D
  #	D

 D
 tCy4/D
 ((4/D
 !& 1 1D 8D
 !D
 DLD
L $ $ , , # #   
 
 ? ? 	 	  
    #)J     ]]_12 #'! #%# !26,-DH,026;?"' 8<BF9B#')H4d3iH4 d
H4 Tz	H4
 !H4 9H4 H4 tCy4/H4  #TzH4 ??T%//%::TAH4 ""T)H4 ((4/H4 !& 1 1D 8H4 4ZH4 H4  !%S#X 5!H4" 'Sz4'784?#H4$ -1I%H4& !'H4 3 H4^
 #+/ 0pr/   r   )/typingr   r   numpyr   r\   transformersr   ,transformers.models.smollm3.modeling_smollm3r   r)   r   loadersr	   &models.autoencoders.autoencoder_kl_wanr
   )models.transformers.transformer_bria_fibor   #pipelines.bria_fibo.pipeline_outputr   pipelines.flux.pipeline_fluxr   r   pipelines.pipeline_utilsr   
schedulersr   r   utilsr   r   r   r   r   r   utils.torch_utilsr   torch_xla.core.xla_modelcore	xla_modelr   r   
get_loggerr"  loggerr-  r   r;   r/   r,   <module>rC     s    !   & K 0 * F S I O 9 T  . ))MM 
		H	% :zp(*= zpr/   