
    +h'                        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
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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$  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K0J1r1  S	SK2J3r3J4r4J5r5  \Rl                  " \75      r8S"S#S jjr9S"S#S jjr:S"S#S jjr; " S S5      r< " S S\
R                  Rz                  5      r> " S S\
R                  Rz                  \#5      r?\  " S S\Rz                  5      5       r@\  " S S\Rz                  5      5       rA " S S\Rz                  5      rB " S  S!\1\\\\\(\"5	      rCg)$    N)AnyDictListOptionalTupleUnion   )ConfigMixinregister_to_config)FluxTransformer2DLoadersMixinFromOriginalModelMixinPeftAdapterMixin)USE_PEFT_BACKEND	deprecateloggingscale_lora_layersunscale_lora_layers)is_torch_npu_available)maybe_allow_in_graph   )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/james-whalen/.local/lib/python3.13/site-packages/diffusers/models/transformers/transformer_flux.py_get_projectionsr:   -   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hasattrr@   r/   s	            r9   _get_fused_projectionsrD   ;   sy    M2888CE299M9K(WT>-J-J484E4EF[4\4b4bcdjl4b4m1Mu[GGr;   c                 T    U R                   (       a  [        XU5      $ [        XU5      $ r'   )fused_projectionsrD   r:   )r0   r1   r2   s      r9   _get_qkv_projectionsrG   E   s'    %d;PQQD1FGGr;   c                       \ rS rSrSrS r   SSSS\R                  S\R                  S\\R                     S	\\R                     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.)rC   FImportError	__class____name__)selfs    r9   __init__FluxAttnProcessor.__init__N   s3    q899!8!8 99tuvv :r;   r0   r%   r1   r2   attention_maskimage_rotary_embreturnc                 d   [        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[        XgXU 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5      nUR$                  S   " U5      nUR'                  U5      nX#4$ U$ )	Nr=      r>   sequence_dim)	attn_maskbackendr   r	   r   )rG   	unflattenheadsnorm_qnorm_kr+   norm_added_qnorm_added_ktorchcatr   r   _attention_backendflattentodtypesplit_with_sizesshapeto_out
to_add_out)rQ   r0   r1   r2   rT   rU   r3   r4   r5   r6   r7   r8   s               r9   __call__FluxAttnProcessor.__call__R   s2    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-AXAX
 &--a3%((5 ,3@3Q3Q&,,Q/1D1DQ1GJ_JeJefgJh1hiop 4R 40! !KKN=9M KKN=9M$(OO4I$J! 77  r;    NNN)rP   
__module____qualname____firstlineno__re   rR   rc   Tensorr   rm   __static_attributes__ro   r;   r9   rI   rI   K   s{    w /315373!3! ||3!  %||	3!
 !.3! #5<<03! 
3! 3!r;   rI   c                   $  ^  \ 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\
\R                     S\
\\R                        S\
\R                     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 )NrL   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devicerh   )superrR   rC   rM   rN   rO   rP   ry   rz   
isinstancetuplelistlen
ValueErrorscalenn
ModuleListrangeLinearto_k_ipto_v_ip)	rQ   ry   rz   
num_tokensr   r}   rh   _rO   s	           r9   rR   #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(r0   r%   r1   r2   rT   rU   ip_hidden_statesip_adapter_masksrV   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                  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 R.                  5       H  u  nnnnU" U5      nU" U5      nUR1                  USUR                  UR2                  5      nUR1                  USUR                  UR2                  5      n[        UUUS SSU R                  S9nUR5                  USUR                  UR2                  -  5      nUR                  UR                  5      nUUU-  -  nM     X#U4$ U$ )Nr   r=   rX   r>   rY           F)r[   	dropout_p	is_causalr\   r   r	   )rj   rG   r]   r^   r_   r`   ra   rb   rc   rd   r   r   re   rf   rg   rh   ri   rk   rl   
zeros_likezipr   r   r   viewhead_dimreshape)rQ   r0   r1   r2   rT   rU   r   r   
batch_sizer3   r4   r5   r6   r7   r8   ip_queryip_attn_outputcurrent_ip_hidden_statesr   r   r   ip_keyip_values                          r9   rm   #FluxIPAdapterAttnProcessor.__call__   sX    #((+
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-$++
 &--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,( ,D+K+KJXZ\`\f\fimiviv\v+w(+C+F+Fx~~+V(%*B"BB)F, !GG  r;   )rz   ry   r   r   r   ))         ?NN)NNNNN)rP   rq   rr   rs   __doc__re   intrR   rc   rt   r   r   rm   ru   __classcell__rO   s   @r9   rw   rw      s    2 jn!
!
58!
 !
N /315379=37W!W! ||W!  %||	W!
 !.W! #5<<0W! #4#56W! #5<<0W! 
W! W!r;   rw   c                   :  ^  \ rS rSr\r\\/r             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\\R                     S\\R                     S\R                  4
S jjrSrU =r$ )r%   i
  	query_dimr^   dim_headdropoutr|   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   r|   )r   )"r~   rR   r   	inner_dimr   use_biasr   r   r   r   r^   r+   r   rc   r   RMSNormr_   r`   r   r(   r)   r*   r   rk   appendDropoutra   rb   r,   r-   r.   rl   _default_processor_clsset_processor)rQ   r   r^   r   r   r|   r+   r   r   r   r   r   r   r   	processorrO   s                  r9   rR   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%r;   r1   r2   rT   rU   rV   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   rm   
parameterskeysitemsr   loggerwarningrO   rP   )rQ   r1   r2   rT   rU   kwargsattn_parametersquiet_attn_parameterskr   unused_kwargsws               r9   forwardFluxAttention.forwardG  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   r^   r   rb   ra   r`   r_   r   r   r   rl   r)   rk   r(   r*   r   )   @   r   FNTTgh㈵>NNFTNrp   )rP   rq   rr   rs   rI   r   rw   _available_processorsr   floatboolr   rR   rc   rt   r   ru   r   r   s   @r9   r%   r%   
  sF   ." +/*.+/#'4&4& 4& 	4&
 4& 4& $C=4& "$4& 4& 4& 4& #4.4& 4& !4& 4&r 9=1537v||v  (5v !.	v
 #5<<0v 
v vr;   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\	\\\4      S\
\R                  \R                  4   4S jjrSrU =r$ )FluxSingleTransformerBlockiZ  r?   num_attention_headsattention_head_dim	mlp_ratioc                   > [         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        [        5       (       a  SSKJn  Sn[        SSU5        U" 5       nO
[!        5       n[#        UUUUSUS	SS
9U l        g )Ntanh)approximater   )FluxAttnProcessor2_0_NPUzDefaulting to FluxAttnProcessor2_0_NPU for NPU devices will be removed. Attention processors should be set explicitly using the `set_attn_processor` method.npu_processorz0.34.0Tư>)r   r   r^   r   r|   r   r   r   )r~   rR   r   mlp_hidden_dimr$   normr   r   proj_mlpGELUact_mlpproj_outr   attention_processorr   r   rI   r%   r0   )	rQ   r?   r   r   r   r   deprecation_messager   rO   s	           r9   rR   #FluxSingleTransformerBlock.__init__\  s    !#/2*3/			#':':;ww62		#(;(;";SA!##FR   ox1DE02I)+I!'%	
	r;   r1   r2   tembrU   joint_attention_kwargsrV   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$ )	NrX   r>   emb)r1   rU   r       ro   )rj   rc   rd   r   r   r   r0   	unsqueezer   rh   float16clip)rQ   r1   r2   r   rU   r   text_seq_lenresidualnorm_hidden_statesgatemlp_hidden_statesattn_outputs               r9   r   "FluxSingleTransformerBlock.forward|  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3r;   )r   r0   r   r   r   r   )g      @NN)rP   rq   rr   rs   r   r   rR   rc   rt   r   r   r   strr   r   ru   r   r   s   @r9   r   r   Z  s    
C 
c 
s 
_d 
 
J IM;?4||4  %||4 ll	4
 #5u||)C#DE4 !)c3h 84 
u||U\\)	*4 4r;   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\
\\\4      S\\R                  \R                  4   4S jjrSrU =r$ )FluxTransformerBlocki  r?   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   r^   r   r   r|   r   r   r   r   r   zgelu-approximate)r?   dim_outactivation_fn)r~   rR   r#   norm1norm1_contextr%   rI   r0   r   	LayerNormnorm2r   ffnorm2_context
ff_context)rQ   r?   r   r   r   r   rO   s         r9   rR   FluxTransformerBlock.__init__  s     	%c*
-c2!!'%"')

	 \\#%TJ
#BTU\\#%TR%#J\]r;   r1   r2   r   rU   r   rV   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   )r1   r2   rU   r   r	   rX   r   r   ro   )r   r  r0   r   r   r  r  r  r  rh   rc   r   r   )rQ   r1   r2   r   rU   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                         r9   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3r;   )r0   r  r  r   r  r  r  )rms_normr   r   )rP   rq   rr   rs   r   r   r   rR   rc   rt   r   r   r   r   r   ru   r   r   s   @r9   r   r     s     tx^^-0^FI^TW^kp^ ^> IM;?64||64  %||64 ll	64
 #5u||)C#DE64 !)c3h 864 
u||U\\)	*64 64r;   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'   )r~   rR   r  r  )rQ   r  r  rO   s      r9   rR   FluxPosEmbed.__init__  s    
 r;   idsrV   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$ )Nr=   mpsnpuT)r  repeat_interleave_realuse_realfreqs_dtyper>   )rj   r   r}   typerc   float32float64r   r   r  r  r   rd   rg   )rQ   r  n_axescos_outsin_outposis_mpsis_npur$  icossin	freqs_cos	freqs_sins                 r9   r   FluxPosEmbed.forward  s    2iikE)E)(.&emmu}}vA.a AqD	jj'+'HC NN3NN3  IIg2.11#**=	IIg2.11#**=	##r;   )r  r  )rP   rq   rr   rs   r   r   rR   rc   rt   r   ru   r   r   s   @r9   r  r    s:    !c !T#Y !
$5<< $ELL $ $r;   r  c                     ^  \ rS rSrSrSrSS/rSS/rSS/r\	           S!S\
S	\
S
\\
   S\
S\
S\
S\
S\
S\
S\S\\
\
\
4   4U 4S jjj5       r           S"S\R                   S\R                   S\R                   S\R"                  S\R                   S\R                   S\R                   S\\\\4      S\S\S\\R                   \4   4S jj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   
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=  )r?   r   r   Fr   r   Tr   )r~   rR   r9  r   r  r6  r   r   time_text_embedr   r   context_embedder
x_embedderr   r   r   transformer_blocksr   single_transformer_blocksr"   norm_outr   gradient_checkpointing)rQ   r7  r8  r9  r:  r;  r   r   r<  r=  r>  r?  text_time_guidance_clsr   rO   s                 r9   rR   FluxTransformer2DModel.__init__=  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!r1   r2   pooled_projectionstimestepimg_idstxt_idsguidancer   return_dictcontrolnet_blocks_repeatrV   c           	         Ub#  UR                  5       nUR                  SS5      nOSn[        (       a  [        X5        O+Ub(  UR	                  SS5      b  [
        R                  S5        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[1        [2        R4                  " U5      5      nU(       a  XU[/        U	5      -     -   nM  XUU-     -   nM     [%        U R6                  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 R6                  5      [/        U
5      -  n[1        [2        R4                  " U5      5      nXUU-     -   nM     U R9                  X5      nU R;                  U5      n[        (       a  [=        X5        U(       d  U4$ [?        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.
Nr   r   z\Passing `scale` via `joint_attention_kwargs` when not using the PEFT backend is ineffective.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 Tensorr>   ip_adapter_image_embedsr   )r1   r2   r   rU   r   )sample) copypopr   r   getr   r   rD  rg   rh   rB  rC  ndimrc   rd   r6  encoder_hid_projupdate	enumeraterE  is_grad_enabledrH  _gradient_checkpointing_funcr   r   npceilrF  rG  r   r   r    )rQ   r1   r2   rK  rL  rM  rN  rO  r   controlnet_block_samplescontrolnet_single_block_samplesrP  rQ  
lora_scaler   r  rU   rS  r   index_blockblockinterval_controloutputs                          r9   r   FluxTransformer2DModel.forwardw  s   R "-%;%@%@%B"/33GSAJJd/%16L6P6PQXZ^6_6kr 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:}-19'v66r;   )rC  rH  r   rG  r9  r6  r   rF  rB  rE  rD  )rX   r   N   &         i   i   F)   8   rm  )NNNNNNNNNTF)rP   rq   rr   rs   r    _supports_gradient_checkpointing_no_split_modules _skip_layerwise_casting_patterns_repeated_blocksr   r   r   r   r   rR   rc   rt   
LongTensorr   r   r   r   r    r   ru   r   r   s   @r9   r5  r5    s   > (,$/1MN(3V'<$.0LM &*!#"%#%#'%( %/;7,7, 7, sm	7,
 7, 7,  7, !7, !7,  #7, 7, c3m,7, 7,x /3+/%) $ $!%;?!%(, ).^7||^7  %||^7 "LL	^7
 ""^7 ^7 ^7 ,,^7 !)c3h 8^7 ^7 #'^7 
u||55	6^7 ^7r;   r5  r'   )r0   r%   )Dr   typingr   r   r   r   r   r   numpyr^  rc   torch.nnr   torch.nn.functional
functionalrM   configuration_utilsr
   r   loadersr   r   r   utilsr   r   r   r   r   utils.import_utilsr   utils.torch_utilsr   	attentionr   r   r   attention_dispatchr   cache_utilsr   
embeddingsr   r   r   r   modeling_outputsr    modeling_utilsr!   normalizationr"   r#   r$   
get_loggerrP   r   r:   rD   rG   rI   Modulerw   r%   r   r   r  r5  ro   r;   r9   <module>r     s.    : :      B ^ ^ a a 8 5 I I 6 $  8 ' \ \ 
		H	%HHH:! :!z! !DMvEHHOO%9 Mv` >4 >4 >4B Q4299 Q4 Q4h$299 $>E7!E7r;   