
    +hy                        S SK r S SKrS SKJrJrJrJrJrJr  S SK	r	S SK
JrJrJrJrJrJr  SSKJr  SSKJr  SSKJrJr  SSKJrJr  SS	KJrJrJrJr  SS
K J!r!  SSK"J#r#  SSK$J%r%  \" 5       (       a  S SK&J's  J(r)  Sr*OSr*\RV                  " \,5      r-Sr.    SS\/S\/S\0S\04S jjr1    SS\\/   S\\\2\	Rf                  4      S\\\/      S\\\0      4S jjr4 " S S\#\5      r5g)     N)AnyCallableDictListOptionalUnion)CLIPTextModelWithProjectionCLIPTokenizerLlamaForCausalLMPreTrainedTokenizerFastT5EncoderModelT5Tokenizer   )VaeImageProcessor)HiDreamImageLoraLoaderMixin)AutoencoderKLHiDreamImageTransformer2DModel)FlowMatchEulerDiscreteSchedulerUniPCMultistepScheduler)	deprecateis_torch_xla_availableloggingreplace_example_docstring)randn_tensor   )DiffusionPipeline   )HiDreamImagePipelineOutputTFa  
    Examples:
        ```py
        >>> import torch
        >>> from transformers import AutoTokenizer, LlamaForCausalLM
        >>> from diffusers import HiDreamImagePipeline


        >>> tokenizer_4 = AutoTokenizer.from_pretrained("meta-llama/Meta-Llama-3.1-8B-Instruct")
        >>> text_encoder_4 = LlamaForCausalLM.from_pretrained(
        ...     "meta-llama/Meta-Llama-3.1-8B-Instruct",
        ...     output_hidden_states=True,
        ...     output_attentions=True,
        ...     torch_dtype=torch.bfloat16,
        ... )

        >>> pipe = HiDreamImagePipeline.from_pretrained(
        ...     "HiDream-ai/HiDream-I1-Full",
        ...     tokenizer_4=tokenizer_4,
        ...     text_encoder_4=text_encoder_4,
        ...     torch_dtype=torch.bfloat16,
        ... )
        >>> pipe.enable_model_cpu_offload()

        >>> image = pipe(
        ...     'A cat holding a sign that says "Hi-Dreams.ai".',
        ...     height=1024,
        ...     width=1024,
        ...     guidance_scale=5.0,
        ...     num_inference_steps=50,
        ...     generator=torch.Generator("cuda").manual_seed(0),
        ... ).images[0]
        >>> image.save("output.png")
        ```
base_seq_lenmax_seq_len
base_shift	max_shiftc                 4    XC-
  X!-
  -  nX5U-  -
  nX-  U-   nU$ N )image_seq_lenr   r    r!   r"   mbmus           r/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/pipelines/hidream_image/pipeline_hidream_image.pycalculate_shiftr+   W   s3     
	K$>?A%%A		Q	BI    num_inference_stepsdevice	timestepssigmasc                    Ub  Ub  [        S5      eUb  S[        [        R                  " U R                  5      R
                  R                  5       5      ;   nU(       d  [        SU R                   S35      eU R                  " S
X2S.UD6  U R                  n[        U5      nX14$ Ub  S[        [        R                  " U R                  5      R
                  R                  5       5      ;   nU(       d  [        SU R                   S35      eU R                  " S
XBS.UD6  U R                  n[        U5      nX14$ U R                  " U4S	U0UD6  U R                  nX14$ )a  
Calls the scheduler's `set_timesteps` method and retrieves timesteps from the scheduler after the call. Handles
custom timesteps. Any kwargs will be supplied to `scheduler.set_timesteps`.

Args:
    scheduler (`SchedulerMixin`):
        The scheduler to get timesteps from.
    num_inference_steps (`int`):
        The number of diffusion steps used when generating samples with a pre-trained model. If used, `timesteps`
        must be `None`.
    device (`str` or `torch.device`, *optional*):
        The device to which the timesteps should be moved to. If `None`, the timesteps are not moved.
    timesteps (`List[int]`, *optional*):
        Custom timesteps used to override the timestep spacing strategy of the scheduler. If `timesteps` is passed,
        `num_inference_steps` and `sigmas` must be `None`.
    sigmas (`List[float]`, *optional*):
        Custom sigmas used to override the timestep spacing strategy of the scheduler. If `sigmas` is passed,
        `num_inference_steps` and `timesteps` must be `None`.

Returns:
    `Tuple[torch.Tensor, int]`: A tuple where the first element is the timestep schedule from the scheduler and the
    second element is the number of inference steps.
zYOnly one of `timesteps` or `sigmas` can be passed. Please choose one to set custom valuesr/   zThe current scheduler class zx's `set_timesteps` does not support custom timestep schedules. Please check whether you are using the correct scheduler.)r/   r.   r0   zv's `set_timesteps` does not support custom sigmas schedules. Please check whether you are using the correct scheduler.)r0   r.   r.   r%   )

ValueErrorsetinspect	signatureset_timesteps
parameterskeys	__class__r/   len)	schedulerr-   r.   r/   r0   kwargsaccepts_timestepsaccept_sigmass           r*   retrieve_timestepsr?   e   s}   > !3tuu'3w/@/@AXAX/Y/d/d/i/i/k+ll .y/B/B.C Da b  	M)MfM''	!)n )) 
	 C(9(9):Q:Q(R(](](b(b(d$ee.y/B/B.C D_ `  	GvGG''	!)n )) 	 3MFMfM''	))r,   c            ;       <  ^  \ rS rSrSr/ SQrS\S\S\S\	S\S	\	S
\
S\S\S\S\4U 4S jjr    SHS\\\\   4   S\S\\R,                     S\\R.                     4S jjr   SIS\\\\   4   S\S\\R,                     S\\R.                     4S jjr    SHS\\\\   4   S\S\\R,                     S\\R.                     4S jjr                    SJS\\\\\   4      S\\\\\   4      S\\\\\   4      S\\\\\   4      S\\R,                     S\\R.                     S\S\S \\\\\   4      S!\\\\\   4      S"\\\\\   4      S#\\\\\   4      S$\\\R8                        S%\\\R8                        S&\\\R8                        S'\\\R8                        S(\\R8                     S)\\R8                     S\S*\\   4(S+ jjrS, rS- r S. r!S/ r"           SKS0 jr# SLS1 jr$\%S2 5       r&\%S3 5       r'\%S4 5       r(\%S5 5       r)\%S6 5       r*\RV                  " 5       \," \-5      SSSSSSS7SS8SSSSSSSSSSSSSS9SSSS:/S4S\\\\   4   S\\\\\   4      S\\\\\   4      S\\\\\   4      S;\\   S<\\   S=\S>\\\      S?\S \\\\\   4      S!\\\\\   4      S"\\\\\   4      S#\\\\\   4      S\\   S@\\\R\                  \\R\                     4      S:\\R8                     S$\\R8                     S%\\R8                     S&\\R8                     S'\\R8                     S(\\R8                     S)\\R8                     SA\\   SB\SC\\/\\04      SD\\1\\\//S4      SE\\   S\48SF jj5       5       r2SGr3U =r4$ )MHiDreamImagePipeline   zNtext_encoder->text_encoder_2->text_encoder_3->text_encoder_4->transformer->vae)latentsprompt_embeds_t5prompt_embeds_llama3pooled_prompt_embedsr;   vaetext_encoder	tokenizertext_encoder_2tokenizer_2text_encoder_3tokenizer_3text_encoder_4tokenizer_4transformerc                   > [         TU ]  5         U R                  UUUUU	UUUU
UUS9  [        U S5      (       a<  U R                  b/  S[        U R                  R                  R                  5      S-
  -  OSU l        [        U R                  S-  S9U l
        SU l        [        U SS 5      b&  U R                  R                  U R                  l        g g )	N)rG   rH   rJ   rL   rN   rI   rK   rM   rO   r;   rP   rG   r   r      )vae_scale_factor   rO   )super__init__register_moduleshasattrrG   r:   configblock_out_channelsrS   r   image_processordefault_sample_sizegetattrrO   	eos_token	pad_token)selfr;   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   r9   s               r*   rV   HiDreamImagePipeline.__init__   s     	%)))#### 	 	
 CJ$PUBVBV[_[c[c[oA#dhhoo889A=>uv 	
  1$BWBWZ[B[\#& 4-9)-)9)9)C)CD& :r,   NrT   promptmax_sequence_lengthr.   dtypec           	      X   U=(       d    U R                   nU=(       d    U R                  R                  n[        U[        5      (       a  U/OUnU R                  US[        X R
                  R                  5      SSSS9nUR                  nUR                  nU R                  USSS9R                  nUR                  S   UR                  S   :  a  [        R                  " Xh5      (       d|  U R
                  R                  US S 2[        X R
                  R                  5      S-
  S24   5      n	[        R                  S	[        X R
                  R                  5       S
U	 35        U R                  UR!                  U5      UR!                  U5      S9S   n
U
R!                  XCS9n
U
$ )N
max_lengthTptpaddingrf   
truncationadd_special_tokensreturn_tensorslongestri   rl   r   XThe following part of your input was truncated because `max_sequence_length` is set to  	 tokens: )attention_maskr   rd   r.   )_execution_devicerL   rd   
isinstancestrrM   minmodel_max_length	input_idsrr   shapetorchequalbatch_decodeloggerwarningto)r`   rb   rc   r.   rd   text_inputstext_input_idsrr   untruncated_idsremoved_textprompt_embedss              r*   _get_t5_prompt_embeds*HiDreamImagePipeline._get_t5_prompt_embeds   s    14112,,22'44&&&& .0@0@0Q0QR# ' 
 %..$33**69UY*Zdd  $(<(<R(@@UcIuIu++883':<L<L<]<]#^ab#beg#g ghL NN+-=-=-N-NOPPYZfYgi
 ++N,=,=f,EVdVgVghnVo+pqrs%((u(Dr,   c                 &   U=(       d    U R                   nU=(       d    UR                  n[        U[        5      (       a  U/OUnU" US[	        US5      SSS9nUR
                  nU" USSS9R
                  n	U	R                  S   UR                  S   :  aP  [        R                  " X5      (       d5  UR                  U	S S 2S	S24   5      n
[        R                  S
S SU
 35        U" UR                  U5      SS9nUS   nUR                  XeS9nU$ )Nrf      Trg   )ri   rf   rj   rl   rm   rn   ro      z\The following part of your input was truncated because CLIP can only handle sequences up to rq   )output_hidden_statesr   rs   )rt   rd   ru   rv   rw   ry   rz   r{   r|   r}   r~   r   r   )r`   rI   rH   rb   rc   r.   rd   r   r   r   r   r   s               r*   _get_clip_prompt_embeds,HiDreamImagePipeline._get_clip_prompt_embeds   s%    1411+++'44&& .4
 %..#FIdS]]  $(<(<R(@@UcIuIu$11/!Wr\/2RSLNN5	,1 %^%6%6v%>UYZ &a(%((u(Dr,   c           	         U=(       d    U R                   nU=(       d    U R                  R                  n[        U[        5      (       a  U/OUnU R                  US[        X R
                  R                  5      SSSS9nUR                  nUR                  nU R                  USSS9R                  nUR                  S   UR                  S   :  a  [        R                  " Xh5      (       d|  U R
                  R                  US S 2[        X R
                  R                  5      S-
  S24   5      n	[        R                  S	[        X R
                  R                  5       S
U	 35        U R                  UR!                  U5      UR!                  U5      SSS9n
U
R"                  SS  n[        R$                  " USS9nU$ )Nrf   Trg   rh   rm   rn   ro   r   rp   rq   )rr   r   output_attentionsr   dim)rt   rN   rd   ru   rv   rO   rw   rx   ry   rr   rz   r{   r|   r}   r~   r   r   hidden_statesstack)r`   rb   rc   r.   rd   r   r   rr   r   r   outputsr   s               r*   _get_llama3_prompt_embeds.HiDreamImagePipeline._get_llama3_prompt_embeds  s    14112,,22'44&&&& .0@0@0Q0QR# ' 
 %..$33**69UY*Zdd  $(<(<R(@@UcIuIu++883':<L<L<]<]#^ab#beg#g ghL NN+-=-=-N-NOPPYZfYgi
 %%f%),,V4!%"	 & 
  --ab1Mq9r,   r   Tprompt_2prompt_3prompt_4num_images_per_promptdo_classifier_free_guidancenegative_promptnegative_prompt_2negative_prompt_3negative_prompt_4rD   rE   negative_prompt_embeds_t5negative_prompt_embeds_llama3rF   negative_pooled_prompt_embeds
lora_scalec                 "   [        U[        5      (       a  U/OUnUb  [        U5      nOUR                  S   nU=(       d    U R                  nUc)  U R                  U R                  U R                  UUXV5      nU(       a  Uc  U	=(       d    Sn	[        U	[        5      (       a  U	/OU	n	[        U	5      S:  a  [        U	5      U:w  a  [        SU 35      eU R                  U R                  U R                  U	UXV5      nUR                  S   S:X  a  US:  a  UR                  US5      nUc  U=(       d    Un[        U[        5      (       a  U/OUn[        U5      S:  a  [        U5      U:w  a  [        SU 35      eU R                  U R                  U R                  UUXV5      nUR                  S   S:X  a  US:  a  UR                  US5      nU(       a  Uc  U
=(       d    U	n
[        U
[        5      (       a  U
/OU
n
[        U
5      S:  a  [        U
5      U:w  a  [        SU 35      eU R                  U R                  U R                  U
UXV5      nUR                  S   S:X  a  US:  a  UR                  US5      nUc  [        R                  " WW/SS9nU(       a  Uc  [        R                  " WW/SS9nUc  U=(       d    Un[        U[        5      (       a  U/OUn[        U5      S:  a  [        U5      U:w  a  [        S	U 35      eU R                  UUXV5      nUR                  S   S:X  a  US:  a  UR                  USS5      nU(       a  Uc  U=(       d    U	n[        U[        5      (       a  U/OUn[        U5      S:  a  [        U5      U:w  a  [        S
U 35      eU R                  UUXV5      nUR                  S   S:X  a  US:  a  UR                  USS5      nUc  U=(       d    Un[        U[        5      (       a  U/OUn[        U5      S:  a  [        U5      U:w  a  [        SU 35      eU R                  UUXV5      nUR                  S   S:X  a  US:  a  UR                  SUSS5      nU(       a  Uc  U=(       d    U	n[        U[        5      (       a  U/OUn[        U5      S:  a  [        U5      U:w  a  [        SU 35      eU R                  UUXV5      nUR                  S   S:X  a  US:  a  UR                  SUSS5      nUR                  SU5      nUR!                  UU-  S5      nUR                  u  nnnUS:X  a  US:  a  UR                  USS5      nOUS:  a  UU:w  a  [        SU 35      eUR                  SUS5      nUR!                  UU-  US5      nUR                  u  nnnnUS:X  a  US:  a  UR                  SUSS5      nOUS:  a  UU:w  a  [        SU 35      eUR                  SSUS5      nUR!                  SUU-  UU5      nU(       GaY  UR                  u  nnUS:X  a  US:  a  UR                  US5      nOUS:  a  UU:w  a  [        SU 35      eUR                  SU5      nUR!                  UU-  S5      nUR                  u  nnnUS:X  a  US:  a  UR                  USS5      nOUS:  a  UU:w  a  [        SU 35      eUR                  SUS5      nUR!                  UU-  US5      nUR                  u  nnnnUS:X  a  US:  a  UR                  SUSS5      nOUS:  a  UU:w  a  [        SU 35      eUR                  SSUS5      nUR!                  SUU-  UU5      nUUUUUU4$ )Nr    r   z'negative_prompt must be of length 1 or z prompt_2 must be of length 1 or z)negative_prompt_2 must be of length 1 or ro   r   z prompt_3 must be of length 1 or z)negative_prompt_3 must be of length 1 or z prompt_4 must be of length 1 or z)negative_prompt_4 must be of length 1 or z0cannot duplicate prompt_embeds_t5 of batch size z4cannot duplicate prompt_embeds_llama3 of batch size z=cannot duplicate negative_pooled_prompt_embeds of batch size z9cannot duplicate negative_prompt_embeds_t5 of batch size z=cannot duplicate negative_prompt_embeds_llama3 of batch size )ru   rv   r:   rz   rt   r   rI   rH   r2   repeatrK   rJ   r{   catr   r   view)r`   rb   r   r   r   r.   rd   r   r   r   r   r   r   rD   rE   r   r   rF   r   rc   r   
batch_sizepooled_prompt_embeds_1negative_pooled_prompt_embeds_1pooled_prompt_embeds_2negative_pooled_prompt_embeds_2bs_embedseq_len_r   s                                 r*   encode_prompt"HiDreamImagePipeline.encode_promptA  sm   . (44&&VJ-33A6J1411'%)%A%A 1 16;NPV&" '+H+P-3O3=os3S3S/YhO?#a'C,@J,N #J:,!WXX.2.J.J 1 1?DWY_/+ /44Q71<a2Q2X2XYcef2g/')6H%/#%>%>zHH8}q S]j%@ #CJ<!PQQ%)%A%A  $"5"5xATV\&" &++A.!3
Q)?)F)FzST)U&&+H+P 1 D_7ABSUX7Y7Y!2 3_p$%)c2C.D
.R #LZL!YZZ.2.J.J  $"5"57HJ]_e/+ /44Q71<a2Q2X2XYcef2g/'#(99.DF\-]ce#f &+H+P,1II02QRXZ-) #)6H%/#%>%>zHH8}q S]j%@ #CJ<!PQQ#99(DWY_g%%a(A-*q.#3#:#::q!#L &+D+L 1 D_7ABSUX7Y7Y!2 3_p$%)c2C.D
.R #LZL!YZZ(,(B(B!#6)% )..q1Q6:>,E,L,LZYZ\],^)')6H%/#%>%>zHH8}q S]j%@ #CJ<!PQQ#'#A#A(L_ag#o #))!,1j1n';'B'B1jRSUV'W$&+H+P 1 D_7ABSUX7Y7Y!2 3_p$%)c2C.D
.R #LZL!YZZ,0,J,J!#6-) -2215:zA~0M0T0TUVXbdegh0i-  4::1>ST388F[9[]_`  055'1q=Z!^/66z1aH\h*4OPXzZ[[+2216KQO+00>S1SU\^`a %9$>$>!8Wcq=Z!^#7#>#>q*aQR#S \h*4ST\S]^__3::1aAVXYZ388ZJ_=_ahjmn& = C CHg1}a0M0T0TU_ab0c-A(j"8 #`ai`j!kll,I,P,PQRTi,j),I,N,Nz\qOqsu,v) $=#B#B Hgq1}a,E,L,LZYZ\],^)A(j"8 #\]e\f!ghh(A(H(HLacd(e%(A(F(FzTiGikrtv(w% )F(K(K%Ax#1}a0M0T0TUVXbdegh0i-A(j"8 #`ai`j!kll,I,P,PQRTUWlno,p),I,N,NJ!66-)
 % ) )
 	
r,   c                 8    U R                   R                  5         g)z
Enable sliced VAE decoding. When this option is enabled, the VAE will split the input tensor in slices to
compute decoding in several steps. This is useful to save some memory and allow larger batch sizes.
N)rG   enable_slicingr`   s    r*   enable_vae_slicing'HiDreamImagePipeline.enable_vae_slicing      
 	!r,   c                 8    U R                   R                  5         g)z
Disable sliced VAE decoding. If `enable_vae_slicing` was previously enabled, this method will go back to
computing decoding in one step.
N)rG   disable_slicingr   s    r*   disable_vae_slicing(HiDreamImagePipeline.disable_vae_slicing  s    
 	  "r,   c                 8    U R                   R                  5         g)z
Enable tiled VAE decoding. When this option is enabled, the VAE will split the input tensor into tiles to
compute decoding and encoding in several steps. This is useful for saving a large amount of memory and to allow
processing larger images.
N)rG   enable_tilingr   s    r*   enable_vae_tiling&HiDreamImagePipeline.enable_vae_tiling  s     	 r,   c                 8    U R                   R                  5         g)z
Disable tiled VAE decoding. If `enable_vae_tiling` was previously enabled, this method will go back to
computing decoding in one step.
N)rG   disable_tilingr   s    r*   disable_vae_tiling'HiDreamImagePipeline.disable_vae_tiling  r   r,   c           
      t  ^  UbX  [        U 4S jU 5       5      (       d>  [        ST R                   SU Vs/ s H  nUT R                  ;  d  M  UPM     sn 35      eUb  Ub  [        SU SU S35      eUb  Ub  [        SU SU S35      eUb  U	b  [        SU S	U	 S35      eUb  U
b  [        S
U SU
 S35      eUc  Uc  [        S5      eUc  U	c  [        S5      eUc  U
c  [        S5      eUbA  [        U[        5      (       d,  [        U[
        5      (       d  [        S[        U5       35      eUbA  [        U[        5      (       d,  [        U[
        5      (       d  [        S[        U5       35      eUbA  [        U[        5      (       d,  [        U[
        5      (       d  [        S[        U5       3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b  Ub  [        SU SU S35      eUb  Ub  [        SU SU S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	bC  Ub@  U	R                  UR                  :w  a&  [        SU	R                   SUR                   S35      eU
bE  UbA  U
R                  UR                  :w  a&  [        SU
R                   S UR                   S35      eg g g s  snf )!Nc              3   @   >#    U  H  oTR                   ;   v   M     g 7fr$   )_callback_tensor_inputs).0kr`   s     r*   	<genexpr>4HiDreamImagePipeline.check_inputs.<locals>.<genexpr>7  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 `pooled_prompt_embeds`: z2. Please make sure to only forward one of the two.z Cannot forward both `prompt_2`: z Cannot forward both `prompt_3`: z and `prompt_embeds_t5`: z Cannot forward both `prompt_4`: z and `prompt_embeds_llama3`: zsProvide either `prompt` or `pooled_prompt_embeds`. Cannot leave both `prompt` and `pooled_prompt_embeds` undefined.zkProvide either `prompt` or `prompt_embeds_t5`. Cannot leave both `prompt` and `prompt_embeds_t5` undefined.zsProvide either `prompt` or `prompt_embeds_llama3`. Cannot leave both `prompt` and `prompt_embeds_llama3` undefined.z2`prompt` has to be of type `str` or `list` but is z4`prompt_2` has to be of type `str` or `list` but is z4`prompt_3` has to be of type `str` or `list` but is z4`prompt_4` has to be of type `str` or `list` but is z'Cannot forward both `negative_prompt`: z& and `negative_pooled_prompt_embeds`: z)Cannot forward both `negative_prompt_2`: z)Cannot forward both `negative_prompt_3`: z" and `negative_prompt_embeds_t5`: z)Cannot forward both `negative_prompt_4`: z& and `negative_prompt_embeds_llama3`: z`pooled_prompt_embeds` and `negative_pooled_prompt_embeds` must have the same shape when passed directly, but got: `pooled_prompt_embeds` z$ != `negative_pooled_prompt_embeds` .z~`prompt_embeds_t5` and `negative_prompt_embeds_t5` must have the same shape when passed directly, but got: `prompt_embeds_t5` z  != `negative_prompt_embeds_t5` z`prompt_embeds_llama3` and `negative_prompt_embeds_llama3` must have the same shape when passed directly, but got: `prompt_embeds_llama3` z$ != `negative_prompt_embeds_llama3` )allr2   r   ru   rv   listtyperz   )r`   rb   r   r   r   r   r   r   r   rD   rE   r   r   rF   r   "callback_on_step_end_tensor_inputsr   s   `                r*   check_inputs!HiDreamImagePipeline.check_inputs%  s   $ .9# F
7YF
 C
 C
 DTEaEaDbbn  |^  pH  |^vw  bc  ko  kG  kG  bGpq  |^  pH  oI  J  "6"B08UVjUk l0 0  !&:&F28*<YZnYo p0 0  !&6&B28*<UVfUg h0 0  !&:&F28*<YZnYo p0 0  ^ 4 < F  ^ 0 8}  ^ 4 < F  FC)@)@TZ\`IaIaQRVW]R^Q_`aa!:h+D+DZX`bfMgMgSTXYaTbScdee!:h+D+DZX`bfMgMgSTXYaTbScdee!:h+D+DZX`bfMgMgSTXYaTbScdee&+H+T9/9J K122df  */L/X;<M;N O122df  */H/T;<M;N O-..`b  */L/X;<M;N O122df 
  +0M0Y#))-J-P-PP 44H4N4N3O P5;;<A? 
 ',E,Q%%)B)H)HH 00@0F0F/G H1778; 
  +0M0Y#))-J-P-PP 44H4N4N3O P5;;<A?  Q 1Z+_ pHs   L5L5c	                    S[        U5      U R                  S-  -  -  nS[        U5      U R                  S-  -  -  nXX44n	Uc  [        XXeS9nU$ UR                  U	:w  a  [	        SUR                   SU	 35      eUR                  U5      nU$ )Nr   )	generatorr.   rd   zUnexpected latents shape, got z, expected )intrS   r   rz   r2   r   )
r`   r   num_channels_latentsheightwidthrd   r.   r   rC   rz   s
             r*   prepare_latents$HiDreamImagePipeline.prepare_latents  s     c&kd&;&;a&?@ASZD$9$9A$=>?6A?"5fZG
  }}% #A'--P[\a[b!cddjj(Gr,   c                     U R                   $ r$   _guidance_scaler   s    r*   guidance_scale#HiDreamImagePipeline.guidance_scale  s    ###r,   c                      U R                   S:  $ )Nr   r   r   s    r*   r   0HiDreamImagePipeline.do_classifier_free_guidance  s    ##a''r,   c                     U R                   $ r$   )_attention_kwargsr   s    r*   attention_kwargs%HiDreamImagePipeline.attention_kwargs  s    %%%r,   c                     U R                   $ r$   )_num_timestepsr   s    r*   num_timesteps"HiDreamImagePipeline.num_timesteps  s    """r,   c                     U R                   $ r$   )
_interruptr   s    r*   	interruptHiDreamImagePipeline.interrupt  s    r,   2   g      @pilrC   r   r   r-   r0   r   r   output_typereturn_dictr   callback_on_step_endr   c                    UR                  SS5      nUR                  SS5      nUb  Sn [        SSU 5        US   nUS   nUb  Sn [        SSU 5        US   nUS   nU=(       d    U R                  U R                  -  nU=(       d    U R                  U R                  -  nU R                  S	-  n!U R                  U R                  -  S	-  n"U"Xe-  -  n#[        R
                  " U#5      n#[        UU#-  U!-  U!-  5      [        UU#-  U!-  U!-  5      pVU R                  UUUUU
UUUUUUUUUUS
9  Xl        UU l	        SU l
        Ub  [        U[        5      (       a  Sn$O6Ub!  [        U[        5      (       a  [        U5      n$OUb  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 R&                  UUUUUUU%UUU&S9u  nnnnnnU R&                  (       aE  [(        R*                  " UU/SS9n[(        R*                  " UU/SS9n[(        R*                  " UU/SS9nU R,                  R.                  R0                  n'U R3                  W$U-  U'UUUR4                  U%UU5      n[7        U R,                  R8                  5      n(SU(0n)[        U R:                  [<        5      (       a1  U R:                  R?                  UU%S9  U R:                  R@                  n*O[C        U R:                  UU%4SU0U)D6u  n*n[E        [        U*5      XpR:                  RF                  -  -
  S5      n+[        U*5      U l$        U RK                  US9 n,[M        U*5       GH  u  n-n.U RN                  (       a  M  U R&                  (       a  [(        R*                  " U/S	-  5      OUn/U.RQ                  U/R                  S   5      n0U R-                  U/U0UUUSS9S   n1U1* n1U R&                  (       a)  U1RS                  S	5      u  n2n3U2U RT                  U3U2-
  -  -   n1UR4                  n4U R:                  RW                  U1U.USS9S   nUR4                  U4:w  a>  [(        RX                  RZ                  R]                  5       (       a  UR_                  U45      nUbn  0 n5U H  n6[a        5       U6   U5U6'   M     U" U U-U.U55      n7U7Rc                  SU5      nU7Rc                  SU5      nU7Rc                  SU5      nU7Rc                  SU5      nU-[        U*5      S-
  :X  d)  U-S-   U+:  a0  U-S-   U R:                  RF                  -  S:X  a  U,Re                  5         [f        (       d  GM  [h        Rj                  " 5         GM     SSS5        US:X  a  Un8O{UU Rl                  R.                  Rn                  -  U Rl                  R.                  Rp                  -   nU Rl                  Rs                  USS9S   n8U Rt                  Rw                  U8US9n8U Ry                  5         U(       d  U84$ [{        U8S9$ ! , (       d  f       N= f)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.
    prompt_2 (`str` or `List[str]`, *optional*):
        The prompt or prompts to be sent to `tokenizer_2` and `text_encoder_2`. If not defined, `prompt` is
        will be used instead.
    prompt_3 (`str` or `List[str]`, *optional*):
        The prompt or prompts to be sent to `tokenizer_3` and `text_encoder_3`. If not defined, `prompt` is
        will be used instead.
    prompt_4 (`str` or `List[str]`, *optional*):
        The prompt or prompts to be sent to `tokenizer_4` and `text_encoder_4`. If not defined, `prompt` is
        will be used 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.
    sigmas (`List[float]`, *optional*):
        Custom sigmas to use for the denoising process with schedulers which support a `sigmas` argument in
        their `set_timesteps` method. If not defined, the default behavior when `num_inference_steps` is passed
        will be used.
    guidance_scale (`float`, *optional*, defaults to 3.5):
        Embedded guiddance scale is enabled by setting `guidance_scale` > 1. Higher `guidance_scale` encourages
        a model to generate images more aligned with `prompt` at the expense of lower image quality.

        Guidance-distilled models approximates true classifer-free guidance for `guidance_scale` > 1. Refer to
        the [paper](https://huggingface.co/papers/2210.03142) to learn more.
    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 `true_cfg_scale` is
        not greater than `1`).
    negative_prompt_2 (`str` or `List[str]`, *optional*):
        The prompt or prompts not to guide the image generation to be sent to `tokenizer_2` and
        `text_encoder_2`. If not defined, `negative_prompt` is used in all the text-encoders.
    negative_prompt_3 (`str` or `List[str]`, *optional*):
        The prompt or prompts not to guide the image generation to be sent to `tokenizer_3` and
        `text_encoder_3`. If not defined, `negative_prompt` is used in all the text-encoders.
    negative_prompt_4 (`str` or `List[str]`, *optional*):
        The prompt or prompts not to guide the image generation to be sent to `tokenizer_4` and
        `text_encoder_4`. If not defined, `negative_prompt` is used in all the text-encoders.
    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.
    pooled_prompt_embeds (`torch.FloatTensor`, *optional*):
        Pre-generated pooled text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting.
        If not provided, pooled text embeddings will be generated from `prompt` input argument.
    negative_pooled_prompt_embeds (`torch.FloatTensor`, *optional*):
        Pre-generated negative pooled text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt
        weighting. If not provided, pooled 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.flux.FluxPipelineOutput`] instead of a plain tuple.
    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 128): Maximum sequence length to use with the `prompt`.

Examples:

Returns:
    [`~pipelines.hidream_image.HiDreamImagePipelineOutput`] or `tuple`:
    [`~pipelines.hidream_image.HiDreamImagePipelineOutput`] if `return_dict` is True, otherwise a `tuple`. When
    returning a tuple, the first element is a list with the generated. images.
r   Nnegative_prompt_embedszmThe `prompt_embeds` argument is deprecated. Please use `prompt_embeds_t5` and `prompt_embeds_llama3` instead.z0.35.0r   r   zThe `negative_prompt_embeds` argument is deprecated. Please use `negative_prompt_embeds_t5` and `negative_prompt_embeds_llama3` instead.r   )r   r   r   r   rD   rE   r   r   rF   r   r   Fscale)rb   r   r   r   r   r   r   r   r   rD   rE   r   r   rF   r   r.   r   rc   r   r   r)   )r.   r0   )total)r   r/   encoder_hidden_states_t5encoder_hidden_states_llama3pooled_embedsr   )r   rC   rD   rE   rF   latent)r   )images)>getr   r\   rS   mathsqrtr   r   r   r   r   ru   rv   r   r:   rz   rt   r   r   r   r{   r   rP   rY   in_channelsr   rd   r+   max_seqr;   r   r6   r/   r?   maxorderr   progress_bar	enumerater   expandchunkr   stepbackendsmpsis_availabler   localspopupdateXLA_AVAILABLExm	mark_steprG   scaling_factorshift_factordecoder[   postprocessmaybe_free_model_hooksr   )9r`   rb   r   r   r   r   r   r-   r0   r   r   r   r   r   r   r   rC   rD   rE   r   r   rF   r   r   r   r   r   r   rc   r<   r   r   deprecation_messagedivisionS_maxr   r   r.   r   r   r)   scheduler_kwargsr/   num_warmup_stepsr  itlatent_model_inputtimestep
noise_prednoise_pred_uncondnoise_pred_textlatents_dtypecallback_kwargsr   callback_outputsimages9                                                            r*   __call__HiDreamImagePipeline.__call__  s   F 

?D9!',Dd!K$ #Rox1DE,Q/#0#3 !- #m.:MN(>q(A%,B1,E)K433d6K6KKI11D4I4II((1,))D,A,AAaG(		% EEMX5@A3vPU~YaGadlGlCmv 	+///-!5&?*G!5*G/Q 	 	
$  .!1 *VS"9"9JJvt$<$<VJ!--33A6J'' BFAVAVAbT**..w=hl
 +///(,(H(H-!5&?*G!5*G"7 3!'  
	
% ) ). ++$yy*CEU)V\]^#(99.KMa-bhi#j #(99.KMa-bhi#j   $//66BB&&..  &&	
 T--556 ":dnn&=>>NN(()<V(L00I-?#. 	.
 #.*I* s9~0CnnFZFZ0ZZ\]^!)n %89\!),1>> BFAaAaUYYy1}%=gn"88$6$<$<Q$?@!--"4&-=1E"6 % .  
 )[
 339C9I9I!9L6%!2T5H5HO^oLo5p!pJ !(..--j!WRW-XYZ[==M1~~))6688")**]";'3&(O?-3Xa[* @';D!Q'X$.229gFG'7';';<NP`'a$+;+?+?@VXl+m(+;+?+?@VXl+m( I**A9I/IqSTuX\XfXfXlXlNlpqNq '') =LLNc - :h ("E !?!??488??C_C__GHHOOGO?BE((44U4TE 	##%8O)77G :9s   3HY;Y
Y)r   r   r   r   r\   r[   rS   )NrT   NN)rT   NN)NNNNNNr   TNNNNNNNNNNrT   N)NNNNNNNNNNNr$   )5__name__
__module____qualname____firstlineno__model_cpu_offload_seqr   r   r   r	   r
   r   r   r   r   r   rV   r   rv   r   r   r   r{   r.   rd   r   r   r   boolFloatTensorfloatr   r   r   r   r   r   r   propertyr   r   r   r   r   no_gradr   EXAMPLE_DOC_STRING	Generatorr   r   r   r&  __static_attributes____classcell__)r9   s   @r*   rA   rA      sF   lm%D2%D %D 2	%D
 !%D 4%D #%D '%D !%D )%D -%D 4%DR )-#&)-'+#c49n%# !# &	#
 $#T $')-'+# c49n%	#
 !# &# $#N )-#&)-'+*c49n%* !* &	*
 $*\ 37484848)-'+%&,0;?=A=A=A>BBFGKKO<@EI#&&*+E
sDI~./E
 5d3i01E
 5d3i01	E

 5d3i01E
 &E
 $E
  #E
 &*E
 "%T#Y"78E
 $E#tCy.$9:E
 $E#tCy.$9:E
 $E#tCy.$9:E
 #4(9(9#:;E
 'tE,=,='>?E
  $,D1B1B,C#D!E
" (0U5F5F0G'H#E
$ 'u'8'89%E
& (00A0A'B'E
( !)E
* UO+E
N"#!" !"&&*!&*+/!kl 4 $ $ ( ( & & # #   ]]_12 )-484848 $##%(, #;?=A=A=A/0MQ/38<<@AEEI<@EI%* 59KO9B#&;@8c49n%@8 5d3i01@8 5d3i01	@8
 5d3i01@8 @8 }@8 !@8 e%@8 @8 "%T#Y"78@8 $E#tCy.$9:@8 $E#tCy.$9:@8 $E#tCy.$9:@8  (}@8  E%//43H"HIJ!@8" %++,#@8$ #5#4#45%@8& 'u'8'89'@8( $,E,=,=#>)@8* (00A0A'B+@8, 'u'8'89-@8. (00A0A'B/@80 c]1@82 3@84 #4S>25@86 'xc40@$0F'GH7@88 -1I9@8: !;@8 3 @8r,   rA   )   i   g      ?gffffff?)NNNN)6r4   r   typingr   r   r   r   r   r   r{   transformersr	   r
   r   r   r   r   r[   r   loadersr   modelsr   r   
schedulersr   r   utilsr   r   r   r   utils.torch_utilsr   pipeline_utilsr   pipeline_outputr   torch_xla.core.xla_modelcore	xla_modelr  r  
get_loggerr(  r~   r2  r   r/  r+   rv   r.   r?   rA   r%   r,   r*   <module>rD     s!     = =   1 2 C R Z Z - . 7 ))MM 
		H	%" P 

 
 	

 
  *.15%)$(8*!#8* U3,-.8* S	"	8*
 T%[!8*vb8,.I b8r,   