
    
3j                        S SK r S SKJr  S SKrS SKrS SKJr  S SKJs  J	r
  SSKJrJr  SSKJrJr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  SSKJr  SSK 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,J-r-J.r.  \R^                  " \05      r1S"S#S jjr2S"S#S jjr3S"S#S jjr4 " S S5      r5 " S S\R                  Rl                  5      r7 " S S\R                  Rl                  \5      r8\ " S S\Rl                  5      5       r9\ " S S\Rl                  5      5       r: " S S\Rl                  5      r; " S  S!\*\\\\\!\5	      r<g)$    N)Any   )ConfigMixinregister_to_config)FluxTransformer2DLoadersMixinFromOriginalModelMixinPeftAdapterMixin)apply_lora_scalelogging)maybe_allow_in_graph   )ContextParallelInputContextParallelOutput)AttentionMixinAttentionModuleMixinFeedForward)dispatch_attention_fn)
CacheMixin)*CombinedTimestepGuidanceTextProjEmbeddings"CombinedTimestepTextProjEmbeddingsapply_rotary_embget_1d_rotary_pos_embed)Transformer2DModelOutput)
ModelMixin)AdaLayerNormContinuousAdaLayerNormZeroAdaLayerNormZeroSingleFluxAttentionc                    U R                  U5      nU R                  U5      nU R                  U5      nS =n=pxUb@  U R                  b3  U R	                  U5      nU R                  U5      nU R                  U5      nX4XVXx4$ N)to_qto_kto_vadded_kv_proj_dim
add_q_proj
add_k_proj
add_v_proj	attnhidden_statesencoder_hidden_statesquerykeyvalueencoder_queryencoder_keyencoder_values	            h/home/wildlama/miniconda3/lib/python3.13/site-packages/diffusers/models/transformers/transformer_flux.py_get_projectionsr3   -   s    IIm$E
))M
"CIIm$E266M6K(T-C-C-O(=>oo&;<(=>u[GG    c                     U R                  U5      R                  SSS9u  p4nS=n=pxUb3  [        U S5      (       a"  U R                  U5      R                  SSS9u  pgnX4XVXx4$ )Nr   dimr    to_added_qkv)to_qkvchunkhasattrr9   r(   s	            r2   _get_fused_projectionsr=   ;   sy    M2888CE299M9K(WT>-J-J484E4EF[4\4b4bcdjl4b4m1Mu[GGr4   c                 T    U R                   (       a  [        XU5      $ [        XU5      $ r    )fused_projectionsr=   r3   )r)   r*   r+   s      r2   _get_qkv_projectionsr@   E   s'    %d;PQQD1FGGr4   c                       \ rS rSrSrSrS r   SSSS\R                  S\R                  S\R                  S-  S	\R                  S-  S
\R                  4S jjr	Sr
g)FluxAttnProcessorK   Nc                 r    [        [        S5      (       d"  [        U R                  R                   S35      eg )Nscaled_dot_product_attentionz; requires PyTorch 2.0. Please upgrade your pytorch version.)r<   FImportError	__class____name__)selfs    r2   __init__FluxAttnProcessor.__init__O   s3    q899!8!8 99tuvv :r4   r)   r   r*   r+   attention_maskimage_rotary_embreturnc           	         [        XU5      u  pgppUR                  SUR                  S45      nUR                  SUR                  S45      nUR                  SUR                  S45      nUR                  U5      nUR	                  U5      nUR
                  b  U	R                  SUR                  S45      n	U
R                  SUR                  S45      n
UR                  SUR                  S45      nUR                  U	5      n	UR                  U
5      n
[        R                  " X/SS9n[        R                  " X/SS9n[        R                  " X/SS9nUb  [        XeSS9n[        XuSS9n[        UUUUU R                  U R                  S9nUR                  SS5      nUR                  UR                   5      nUb  UR#                  UR$                  S   UR$                  S   UR$                  S   -
  /SS9u  p2UR&                  S   " UR)                  5       5      nUR&                  S   " U5      nUR+                  UR)                  5       5      nX#4$ U$ )	Nr6      r7   sequence_dim)	attn_maskbackendparallel_configr   r   r   )r@   	unflattenheadsnorm_qnorm_kr$   norm_added_qnorm_added_ktorchcatr   r   _attention_backend_parallel_configflattentodtypesplit_with_sizesshapeto_out
contiguous
to_add_out)rJ   r)   r*   r+   rM   rN   r,   r-   r.   r/   r0   r1   s               r2   __call__FluxAttnProcessor.__call__S   sU    H\!6H
DE+ TZZ$45mmBR 01TZZ$45E"kk#!!-)33BR8HIM%//TZZ4DEK)33BR8HIM --m<M++K8KII}4!<E))[.A6CII}4!<E'$U1ME"3qIC-$++ 11
 &--a3%((5 ,3@3Q3Q&,,Q/1D1DQ1GJ_JeJefgJh1hiop 4R 40! !KKN=+C+C+EFM KKN=9M$(OO4I4T4T4V$W! 77  r4    NNN)rI   
__module____qualname____firstlineno__r_   r`   rK   r]   Tensorri   __static_attributes__rk   r4   r2   rB   rB   K   s    w /3.2048!8! ||8!  %||	8!
 t+8!  ,,-8! 
8! 8!r4   rB   c                   (  ^  \ rS rSrSrSrSr SS\S\4U 4S jjjr     SSSS	\	R                  S
\	R                  S\	R                  S-  S\	R                  S-  S\\	R                     S-  S\	R                  S-  S\	R                  4S jjrSrU =r$ )FluxIPAdapterAttnProcessor   z(Flux Attention processor for IP-Adapter.Nhidden_sizecross_attention_dimc                   > [         TU ]  5         [        [        S5      (       d"  [	        U R
                  R                   S35      eXl        X l        [        U[        [        45      (       d  U/n[        U[        5      (       d  U/[        U5      -  n[        U5      [        U5      :w  a  [        S5      eX@l        [        R                   " [#        [        U5      5       Vs/ s H  n[        R$                  " X!SXVS9PM     sn5      U l        [        R                   " [#        [        U5      5       Vs/ s H  n[        R$                  " X!SXVS9PM     sn5      U l        g s  snf s  snf )NrE   z@ requires PyTorch 2.0, to use it, please upgrade PyTorch to 2.0.zJ`scale` should be a list of integers with the same length as `num_tokens`.T)biasdevicerc   )superrK   r<   rF   rG   rH   rI   ru   rv   
isinstancetuplelistlen
ValueErrorscalenn
ModuleListrangeLinearto_k_ipto_v_ip)	rJ   ru   rv   
num_tokensr   ry   rc   _rH   s	           r2   rK   #FluxIPAdapterAttnProcessor.__init__   s9    	q899>>**++kl  '#6 *udm44$J%&&Gc*o-Eu:Z(ijj
}} s://A 		-fb/
 }} s://A 		-fb/
s   $ E#6 E(r)   r   r*   r+   rM   rN   ip_hidden_statesip_adapter_masksrO   c                    UR                   S   n[        XU5      u  pppU	R                  SUR                  S45      n	U
R                  SUR                  S45      n
UR                  SUR                  S45      nUR	                  U	5      n	UR                  U
5      n
U	nUb  UR                  SUR                  S45      nUR                  SUR                  S45      nUR                  SUR                  S45      nUR                  U5      nUR                  U5      n[        R                  " X/SS9n	[        R                  " X/SS9n
[        R                  " X/SS9nUb  [        XSS9n	[        XSS9n
[        U	U
UUSSU R                  U R                  S9nUR                  S	S
5      nUR                  U	R                   5      nUGb  UR#                  UR                   S   UR                   S   UR                   S   -
  /SS9u  p2UR$                  S   " U5      nUR$                  S   " U5      nUR'                  U5      n[        R(                  " U5      n[+        X`R,                  U R.                  U R0                  5       H  u  nnnnU" U5      nU" U5      nUR3                  USUR                  UR4                  5      nUR3                  USUR                  UR4                  5      n[        UUUS SSU R                  U R                  S9nUR7                  USUR                  UR4                  -  5      nUR                  UR                   5      nUUU-  -  nM     X#U4$ U$ )Nr   r6   rQ   r7   rR           F)rT   	dropout_p	is_causalrU   rV   r   r   )re   r@   rW   rX   rY   rZ   r[   r\   r]   r^   r   r   r_   r`   ra   rb   rc   rd   rf   rh   
zeros_likezipr   r   r   viewhead_dimreshape)rJ   r)   r*   r+   rM   rN   r   r   
batch_sizer,   r-   r.   r/   r0   r1   ip_queryip_attn_outputcurrent_ip_hidden_statesr   r   r   ip_keyip_values                          r2   ri   #FluxIPAdapterAttnProcessor.__call__   sj    #((+
G[!6H
DE+ TZZ$45mmBR 01TZZ$45E"kk# ,)33BR8HIM%//TZZ4DEK)33BR8HIM --m<M++K8KII}4!<E))[.A6CII}4!<E'$U1ME"3qIC-$++ 11	
 &--a3%((5 ,3@3Q3Q&,,Q/1D1DQ1GJ_JeJefgJh1hiop 4R 40! !KKN=9M KKN=9M$(OO4I$J! #--m<NEH **dllDLLFA(%' !!9:"#;<ZTZZO#==RT]]S+@"!# 33$($9$9	,( ,D+K+KJXZ\`\f\fimiviv\v+w(+C+F+Fx~~+V(%*B"BB+F. !GG  r4   )rv   ru   r   r   r   ))   g      ?NN)NNNNN)rI   rm   rn   ro   __doc__r_   r`   intrK   r]   rp   r}   ri   rq   __classcell__rH   s   @r2   rs   rs      s    2 jn!
!
58!
 !
N /3.2046:04Y!Y! ||Y!  %||	Y!
 t+Y!  ,,-Y! u||,t3Y!  ,,-Y! 
Y! Y!r4   rs   c                   :  ^  \ rS rSr\r\\/r             SS\S\S\S\	S\
S\S-  S	\
S-  S
\
S\	S\S\
S-  S\
S\
4U 4S jjjr   SS\R                  S\R                  S-  S\R                  S-  S\R                  S-  S\R                  4
S jjrSrU =r$ )r   i  N	query_dimrX   dim_headdropoutrx   r$   added_proj_biasout_biasepsout_dimcontext_pre_onlypre_onlyelementwise_affinec                   > [         TU ]  5         X0l        U
b  U
OX2-  U l        Xl        XPl        X@l        U
b  U
OUU l        Xl        Xl	        U
b  X-  OUU l
        X`l        Xpl        [        R                  R                  X9US9U l        [        R                  R                  X9US9U l        [        R                  R%                  XR                  US9U l        [        R                  R%                  XR                  US9U l        [        R                  R%                  XR                  US9U l        U R                  (       d  [        R                  R-                  / 5      U l        U R.                  R1                  [        R                  R%                  U R                  U R                  US95        U R.                  R1                  [        R                  R3                  U5      5        Ub  [        R                  R                  X9S9U l        [        R                  R                  X9S9U l        [        R                  R%                  X`R                  US9U l        [        R                  R%                  X`R                  US9U l        [        R                  R%                  X`R                  US9U l        [        R                  R%                  U R                  XS9U l        Uc  U RA                  5       nU RC                  U5        g )N)r   r   rx   )r   )"rz   rK   r   	inner_dimr   use_biasr   r   r   r   rX   r$   r   r]   r   RMSNormrY   rZ   r   r!   r"   r#   r   rf   appendDropoutr[   r\   r%   r&   r'   rh   _default_processor_clsset_processor)rJ   r   rX   r   r   rx   r$   r   r   r   r   r   r   r   	processorrH   s                  r2   rK   FluxAttention.__init__  s5   " 	 $+$7X=M"")"5w9 0 ,3,?W(U
!2.hh&&xM_&`hh&&xM_&`HHOOI~~DOI	HHOOI~~DOI	HHOOI~~DOI	}}((--b1DKKKuxxt~~t||RZ[\KKuxx//89( % 0 0 0 CD % 0 0 0 CD#hhoo.?VeofDO#hhoo.?VeofDO#hhoo.?VeofDO#hhoodnnioWDO335I9%r4   r*   r+   rM   rN   rO   c                 .   [        [        R                  " U R                  R                  5      R
                  R                  5       5      nSS1nUR                  5        VV	s/ s H  u  pX;  d  M  X;  d  M  UPM     n
nn	[        U
5      S:  a:  [        R                  SU
 SU R                  R                  R                   S35        UR                  5        VVs0 s H  u  pX;   d  M  X_M     nnnU R                  " XX#U40 UD6$ s  sn	nf s  snnf )Nr   r   r   zjoint_attention_kwargs z are not expected by z and will be ignored.)setinspect	signaturer   ri   
parameterskeysitemsr~   loggerwarningrH   rI   )rJ   r*   r+   rM   rN   kwargsattn_parametersquiet_attn_parameterskr   unused_kwargsws               r2   forwardFluxAttention.forwardP  s     g//0G0GHSSXXZ[!35G H'-||~u~tq9QVWVt~u}!NN)-8MdnnNfNfNoNoMp  qF  G $*<<>J>41Q5I$!$>J~~d3HZjuntuu v
 Ks   #D2D9DD-D)r&   r%   r'   r$   r   r   r   r   rX   r   r\   r[   rZ   rY   r   r   r   rh   r"   rf   r!   r#   r   )   @   r   FNTTgh㈵>NNFTNrl   )rI   rm   rn   ro   rB   r   rs   _available_processorsr   floatboolrK   r]   rp   r   rq   r   r   s   @r2   r   r     sF   ." (,'+(,#'4&4& 4& 	4&
 4& 4& :4& 4& 4& 4& 4& +4& 4& !4& 4&r 6:.204v||v  %||d2v t+	v
  ,,-v 
v vr4   c                      ^  \ rS rSrSS\S\S\S\4U 4S jjjr  SS\R                  S	\R                  S
\R                  S\	\R                  \R                  4   S-  S\
\\4   S-  S\	\R                  \R                  4   4S jjrSrU =r$ )FluxSingleTransformerBlockic  r8   num_attention_headsattention_head_dim	mlp_ratioc                 l  > [         TU ]  5         [        X-  5      U l        [	        U5      U l        [        R                  " XR                  5      U l        [        R                  " SS9U l
        [        R                  " XR                  -   U5      U l        [        UUUUS[        5       SSS9U l        g )Ntanh)approximateTư>)r   r   rX   r   rx   r   r   r   )rz   rK   r   mlp_hidden_dimr   normr   r   proj_mlpGELUact_mlpproj_outr   rB   r)   )rJ   r8   r   r   r   rH   s        r2   rK   #FluxSingleTransformerBlock.__init__e  s    !#/2*3/			#':':;ww62		#(;(;";SA!'%')	
	r4   Nr*   r+   tembrN   joint_attention_kwargsrO   c                    UR                   S   n[        R                  " X!/SS9nUnU R                  XS9u  pU R	                  U R                  U5      5      n
U=(       d    0 nU R                  " SUUS.UD6n[        R                  " X/SS9nU	R                  S5      n	XR                  U5      -  nXq-   nUR                  [        R                  :X  a  UR                  SS5      nUS S 2S U24   US S 2US 24   pX!4$ )	NrQ   r7   emb)r*   rN   r       rk   )re   r]   r^   r   r   r   r)   	unsqueezer   rc   float16clip)rJ   r*   r+   r   rN   r   text_seq_lenresidualnorm_hidden_statesgatemlp_hidden_statesattn_outputs               r2   r   "FluxSingleTransformerBlock.forwardy  s    -2215		#8"HaP #'99]9#E  LL7I)JK!7!=2ii 
,-
 %
 		;"BJ~~a }}];; 0%--/)..vu=M/<Q=M/NP]^_aman^nPo}$33r4   )r   r)   r   r   r   r   )g      @NN)rI   rm   rn   ro   r   r   rK   r]   rp   r|   dictstrr   r   rq   r   r   s   @r2   r   r   c  s    
C 
c 
s 
_d 
 
2 FJ8<4||4  %||4 ll	4
  ell :;dB4 !%S#X 54 
u||U\\)	*4 4r4   r   c                   &  ^  \ rS rSr SS\S\S\S\S\4
U 4S jjjr  SS	\R                  S
\R                  S\R                  S\
\R                  \R                  4   S-  S\\\4   S-  S\
\R                  \R                  4   4S jjrSrU =r$ )FluxTransformerBlocki  r8   r   r   qk_normr   c                 F  > [         TU ]  5         [        U5      U l        [        U5      U l        [        UUUUUSS[        5       US9	U l        [        R                  " USSS9U l
        [        XSS9U l        [        R                  " USSS9U l        [        XSS9U l        g )NFT)	r   r$   r   rX   r   r   rx   r   r   r   r   r   zgelu-approximate)r8   dim_outactivation_fn)rz   rK   r   norm1norm1_contextr   rB   r)   r   	LayerNormnorm2r   ffnorm2_context
ff_context)rJ   r8   r   r   r   r   rH   s         r2   rK   FluxTransformerBlock.__init__  s     	%c*
-c2!!'%"')

	 \\#%TJ
#BTU\\#%TR%#J\]r4   Nr*   r+   r   rN   r   rO   c                     U R                  XS9u  pgpn
U R                  X#S9u  ppnU=(       d    0 nU R                  " SUUUS.UD6n[        U5      S:X  a  Uu  nnO[        U5      S:X  a  Uu  nnnUR	                  S5      W-  nUU-   nU R                  U5      nUSU	S S 2S 4   -   -  US S 2S 4   -   nU R                  U5      nU
R	                  S5      U-  nUU-   n[        U5      S:X  a  UW-   nUR	                  S5      W-  nUU-   nU R                  U5      nUSUS S 2S 4   -   -  US S 2S 4   -   nU R                  U5      nX/R	                  S5      U-  -   nUR                  [        R                  :X  a  UR                  SS5      nX!4$ )	Nr   )r*   r+   rN   r   r   rQ   r   r   rk   )r   r   r)   r~   r   r   r   r   r   rc   r]   r   r   )rJ   r*   r+   r   rN   r   r   gate_msa	shift_mlp	scale_mlpgate_mlpnorm_encoder_hidden_states
c_gate_msac_shift_mlpc_scale_mlp
c_gate_mlpattention_outputsr   context_attn_outputr   	ff_outputcontext_ff_outputs                         r2   r   FluxTransformerBlock.forward  s    HLzzR_zGjDiHW[WiWi! Xj X
T"* "8!=2 !II 
,"<-
 %	
  !Q&/@,K,"#q(?P<K,n ((+k9%3!ZZ6/1yD7I3IJYWXZ^W^M__GG./	&&q)I5	%	1 !Q&)N:M )22158KK 58K K%)%7%78M%N"%?1{STVZSZG[C[%\_jklnrkr_s%s" OO,FG 58L8LQ8ORc8c c &&%--7$9$>$>vu$M!$33r4   )r)   r   r   r   r   r   r   )rms_normr   r   )rI   rm   rn   ro   r   r   r   rK   r]   rp   r|   r   r   r   rq   r   r   s   @r2   r   r     s     tx^^-0^FI^TW^kp^ ^> FJ8<64||64  %||64 ll	64
  ell :;dB64 !%S#X 564 
u||U\\)	*64 64r4   r   c                   t   ^  \ rS rSrS\S\\   4U 4S jjrS\R                  S\R                  4S jr	Sr
U =r$ )	FluxPosEmbedi  thetaaxes_dimc                 :   > [         TU ]  5         Xl        X l        g r    )rz   rK   r  r  )rJ   r  r  rH   s      r2   rK   FluxPosEmbed.__init__  s    
 r4   idsrO   c           
         UR                   S   n/ n/ nUR                  5       nUR                  R                  S:H  nUR                  R                  S:H  nU(       d  U(       a  [        R
                  O[        R                  n[        U5       HS  n	[        U R                  U	   US S 2U	4   U R                  SSUS9u  pUR                  U
5        UR                  U5        MU     [        R                  " USS9R                  UR                  5      n[        R                  " USS9R                  UR                  5      nX4$ )Nr6   mpsnpuT)r  repeat_interleave_realuse_realfreqs_dtyper7   )re   r   ry   typer]   float32float64r   r   r  r  r   r^   rb   )rJ   r  n_axescos_outsin_outposis_mpsis_npur  icossin	freqs_cos	freqs_sins                 r2   r   FluxPosEmbed.forward  s    2iikE)E)(.&emmu}}vA.a AqD	jj'+'HC NN3NN3  IIg2.11#**=	IIg2.11#**=	##r4   )r  r  )rI   rm   rn   ro   r   r}   rK   r]   rp   r   rq   r   r   s   @r2   r  r    s:    !c !T#Y !
$5<< $ELL $ $r4   r  c                     ^  \ rS rSrSrSrSS/rSS/rSS/r\	" SS	S
S9\	" SS	S
S9\	" SSS
S9\	" SSS
S9S.\
" SS	S9S.r\           S+S\S\S\S-  S\S\S\S\S\S\S\S\\\\4   4U 4S jjj5       r\" S5                 S,S\R&                  S \R&                  S!\R&                  S"\R(                  S#\R&                  S$\R&                  S%\R&                  S\\\4   S-  S&\S'\S(\R&                  \-  4S) jj5       rS*rU =r$ )-FluxTransformer2DModeli  a  
The Transformer model introduced in Flux.

Reference: https://blackforestlabs.ai/announcing-black-forest-labs/

Args:
    patch_size (`int`, defaults to `1`):
        Patch size to turn the input data into small patches.
    in_channels (`int`, defaults to `64`):
        The number of channels in the input.
    out_channels (`int`, *optional*, defaults to `None`):
        The number of channels in the output. If not specified, it defaults to `in_channels`.
    num_layers (`int`, defaults to `19`):
        The number of layers of dual stream DiT blocks to use.
    num_single_layers (`int`, defaults to `38`):
        The number of layers of single stream DiT blocks to use.
    attention_head_dim (`int`, defaults to `128`):
        The number of dimensions to use for each attention head.
    num_attention_heads (`int`, defaults to `24`):
        The number of attention heads to use.
    joint_attention_dim (`int`, defaults to `4096`):
        The number of dimensions to use for the joint attention (embedding/channel dimension of
        `encoder_hidden_states`).
    pooled_projection_dim (`int`, defaults to `768`):
        The number of dimensions to use for the pooled projection.
    guidance_embeds (`bool`, defaults to `False`):
        Whether to use guidance embeddings for guidance-distilled variant of the model.
    axes_dims_rope (`tuple[int]`, defaults to `(16, 56, 56)`):
        The dimensions to use for the rotary positional embeddings.
Tr   r   	pos_embedr   rQ   r   F)	split_dimexpected_dimssplit_outputr   r   )r*   r+   img_idstxt_ids)
gather_dimr0  ) r   N
patch_sizein_channelsout_channels
num_layersnum_single_layersr   r   joint_attention_dimpooled_projection_dimguidance_embedsaxes_dims_ropec                 L  > [         TU ]  5         U=(       d    UU l        Xv-  U l        [	        SUS9U l        U
(       a  [        O[        nU" U R                  U	S9U l        [        R                  " XR                  5      U l        [        R                  " X R                  5      U l        [        R                  " [        U5       Vs/ s H  n[        U R                  UUS9PM     sn5      U l        [        R                  " [        U5       Vs/ s H  n[#        U R                  UUS9PM     sn5      U l        ['        U R                  U R                  SSS9U l        [        R                  " U R                  X-  U R                  -  SS	9U l        SU l        g s  snf s  snf )
Ni'  )r  r  )embedding_dimr<  )r8   r   r   Fr   r   Tr   )rz   rK   r8  r   r  r.  r   r   time_text_embedr   r   context_embedder
x_embedderr   r   r   transformer_blocksr   single_transformer_blocksr   norm_outr   gradient_checkpointing)rJ   r6  r7  r8  r9  r:  r   r   r;  r<  r=  r>  text_time_guidance_clsr   rH   s                 r2   rK   FluxTransformer2DModel.__init__C  sw    	(7K,A%ENK ;J6Oq 	  6..@U 
 !#		*=~~ N))K@"$-- z* +A %(;'9
 +	#
 *, 01 2A +(;'9
 2	*
& /t~~t~~bgmqr		$..*2IDL]L]2]dhi&+#1s   FF!r   r*   r+   pooled_projectionstimestepr2  r3  guidancereturn_dictcontrolnet_blocks_repeatrO   c           	         U R                  U5      nUR                  UR                  5      S-  nUb  UR                  UR                  5      S-  nUc  U R                  XC5      OU R                  XGU5      nU R	                  U5      nUR
                  S:X  a  [        R                  S5        US   nUR
                  S:X  a  [        R                  S5        US   n[        R                  " Xe4SS9nU R                  U5      nUb;  SU;   a5  UR                  S5      nU R                  U5      nUR                  SU05        [        U R                  5       H  u  nn[        R                   " 5       (       a*  U R"                  (       a  U R%                  UUUUUU5      u  p!OU" UUUUUS	9u  p!U	c  M[  ['        U R                  5      ['        U	5      -  n[)        [*        R,                  " U5      5      nU(       a  XU['        U	5      -     -   nM  XUU-     -   nM     [        U R.                  5       H  u  nn[        R                   " 5       (       a*  U R"                  (       a  U R%                  UUUUUU5      u  p!OU" UUUUUS	9u  p!U
c  M[  ['        U R.                  5      ['        U
5      -  n[)        [*        R,                  " U5      5      nXUU-     -   nM     U R1                  X5      nU R3                  U5      nU(       d  U4$ [5        US
9$ )a  
The [`FluxTransformer2DModel`] forward method.

Args:
    hidden_states (`torch.Tensor` of shape `(batch_size, image_sequence_length, in_channels)`):
        Input `hidden_states`.
    encoder_hidden_states (`torch.Tensor` of shape `(batch_size, text_sequence_length, joint_attention_dim)`):
        Conditional embeddings (embeddings computed from the input conditions such as prompts) to use.
    pooled_projections (`torch.Tensor` of shape `(batch_size, projection_dim)`): Embeddings projected
        from the embeddings of input conditions.
    timestep ( `torch.LongTensor`):
        Used to indicate denoising step.
    block_controlnet_hidden_states: (`list` of `torch.Tensor`):
        A list of tensors that if specified are added to the residuals of transformer blocks.
    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).
    return_dict (`bool`, *optional*, defaults to `True`):
        Whether or not to return a [`~models.transformer_2d.Transformer2DModelOutput`] instead of a plain
        tuple.

Returns:
    If `return_dict` is True, an [`~models.transformer_2d.Transformer2DModelOutput`] is returned, otherwise a
    `tuple` where the first element is the sample tensor.
i  r   zrPassing `txt_ids` 3d torch.Tensor is deprecated.Please remove the batch dimension and pass it as a 2d torch Tensorr   zrPassing `img_ids` 3d torch.Tensor is deprecated.Please remove the batch dimension and pass it as a 2d torch Tensorr7   ip_adapter_image_embedsr   )r*   r+   r   rN   r   )sample)rC  rb   rc   rA  rB  ndimr   r   r]   r^   r.  popencoder_hid_projupdate	enumeraterD  is_grad_enabledrG  _gradient_checkpointing_funcr~   r   npceilrE  rF  r   r   )rJ   r*   r+   rJ  rK  r2  r3  rL  r   controlnet_block_samplescontrolnet_single_block_samplesrM  rN  r   r  rN   rP  r   index_blockblockinterval_controloutputs                         r2   r   FluxTransformer2DModel.forward}  s+   V 6;;}223d:{{=#6#67$>H    >%%h:LM 	
 !% 5 56K L<<1NNU ajG<<1NNU ajGii*2>>#.!-2KOe2e&<&@&@AZ&[##445LM"))+=?O*PQ"+D,C,C"DK$$&&4+F+F7;7X7X!)$*84%} 8="/*?%5+A84% (3#&t'>'>#?#F^B_#_ #&rww/?'@#A +%sSkOlAl(mm " %2[\lMl4m$mM= #E@ #,D,J,J"KK$$&&4+F+F7;7X7X!)$*84%} 8="/*?%5+A84% /:#&t'E'E#FMlIm#m #&rww/?'@#A  -P[_oPo0p p1 #L4 m:}-9'v66r4   )rB  rG  r   rF  r8  r.  r   rE  rA  rD  rC  )rQ   r   N   &         i   i   F)   8   rg  )NNNNNNNNNTF)rI   rm   rn   ro   r    _supports_gradient_checkpointing_no_split_modules _skip_layerwise_casting_patterns_repeated_blocksr   r   _cp_planr   r   r   r|   rK   r
   r]   rp   
LongTensorr   r   r   r   r   rq   r   r   s   @r2   r-  r-    s    > (,$/1MN(3V'<$.0LM 2AQ]bc%9AUVej%k+aqW\]+aqW\]	
 *QaHH  #'!#"%#%#'%( %/;7,7, 7, Dj	7,
 7, 7,  7, !7, !7,  #7, 7, c3m,7, 7,r ./ /3+/%) $ $!%8<!%(, ).L7||L7  %||L7 "LL	L7
 ""L7 L7 L7 ,,L7 !%S#X 5L7 L7 #'L7 
0	0L7 0L7r4   r-  r    )r)   r   )=r   typingr   numpyrY  r]   torch.nnr   torch.nn.functional
functionalrF   configuration_utilsr   r   loadersr   r   r	   utilsr
   r   utils.torch_utilsr   _modeling_parallelr   r   	attentionr   r   r   attention_dispatchr   cache_utilsr   
embeddingsr   r   r   r   modeling_outputsr   modeling_utilsr   normalizationr   r   r   
get_loggerrI   r   r3   r=   r@   rB   Modulers   r   r   r   r  r-  rk   r4   r2   <module>r     s*          B ^ ^ . 5 L I I 6 $  8 ' \ \ 
		H	%HHH@! @!FB! B!JMvEHHOO%9 Mv` 24 24 24j Q4299 Q4 Q4h$299 $>}7!}7r4   