
    +hY                     H   S SK J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  SSKJrJr  SSK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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&  \RN                  " \(5      r)\ " S S\5      5       r* " S S\#\\5      r+ " S S\#5      r,g)    )	dataclass)AnyDictListOptionalTupleUnionN   )ConfigMixinregister_to_config)PeftAdapterMixin)USE_PEFT_BACKEND
BaseOutputloggingscale_lora_layersunscale_lora_layers   )AttentionProcessor)ControlNetConditioningEmbeddingzero_module)*CombinedTimestepGuidanceTextProjEmbeddings"CombinedTimestepTextProjEmbeddingsFluxPosEmbed)Transformer2DModelOutput)
ModelMixin)FluxSingleTransformerBlockFluxTransformerBlockc                   ^    \ rS rSr% \\R                     \S'   \\R                     \S'   Srg)FluxControlNetOutput#   controlnet_block_samplescontrolnet_single_block_samples N)	__name__
__module____qualname____firstlineno__r   torchTensor__annotations____static_attributes__r#       f/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/models/controlnets/controlnet_flux.pyr   r   #   s     #ELL11%*5<<%88r,   r   c                     ^  \ rS rSrSr\SSSSSSS	S
S/ SQSS4S\S\S\S\S\S\S\S\S\S\\   S\S\4U 4S jjj5       r	\
S 5       rS r\     S-S\S\S\S\4S jj5       r          S.S\R                   S\R                   S \R                   S!\S"\R                   S#\R                   S$\R$                  S%\R                   S&\R                   S'\R                   S(\\\\4      S)\S*\\R0                  \4   4S+ jjrS,rU =r$ )/FluxControlNetModel)   T   @      &         i   i   F)   8   r8   N
patch_sizein_channels
num_layersnum_single_layersattention_head_dimnum_attention_headsjoint_attention_dimpooled_projection_dimguidance_embedsaxes_dims_ropenum_modeconditioning_embedding_channelsc                 H  > [         TU ]  5         X l        Xe-  U l        [	        SU
S9U l        U	(       a  [        O[        nU" U R                  US9U l        [        R                  " XpR                  5      U l        [        R                  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        [        R                  " / 5      U l        [        [+        U R"                  5      5       HP  nU R(                  R-                  [/        [        R                  " U R                  U R                  5      5      5        MR     [        R                  " / 5      U l        [        [+        U R&                  5      5       HP  nU R0                  R-                  [/        [        R                  " U R                  U R                  5      5      5        MR     US LU l        U R2                  (       a%  [        R4                  " XR                  5      U l        Ub>  [9        USS9U l        [        R                  R                  X R                  5      U l        O>S U l        [/        [        R                  R                  X R                  5      5      U l        SU l        g s  snf s  snf )Ni'  )thetaaxes_dim)embedding_dimr@   )dimr>   r=   )r7   r7   r7   r7   )rD   block_out_channelsF) super__init__out_channels	inner_dimr   	pos_embedr   r   time_text_embednnLinearcontext_embedderr(   
x_embedder
ModuleListranger   transformer_blocksr   single_transformer_blockscontrolnet_blockslenappendr   controlnet_single_blocksunion	Embeddingcontrolnet_mode_embedderr   input_hint_blockcontrolnet_x_embeddergradient_checkpointing)selfr9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   text_time_guidance_clsi_	__class__s                   r-   rL   FluxControlNetModel.__init__,   sk     	',A%ENK:I6Oq 	  6..@U 
 !#		*=~~ N((//+~~F"$-- z* +A %(;'9
 +	#
 *, 01 2A +(;'9
 2	*
& "$r!2s42234A""))+biiPTP^P^6_*`a 5 )+b(9%s499:;A))00RYYt~~W[WeWe=f1gh < T)
::,.LL>>,RD)*6$C0Odt%D! */nn)UD&$(D!)4UXX__[R`R`5a)bD&&+#Ws   LLc                    ^ 0 nS[         S[        R                  R                  S[        [         [
        4   4U4S jjmU R                  5        H  u  p#T" X#U5        M     U$ )z
Returns:
    `dict` of attention processors: A dictionary containing all attention processors used in the model with
    indexed by its weight name.
namemodule
processorsc                    > [        US5      (       a  UR                  5       X  S3'   UR                  5        H  u  p4T" U  SU 3XB5        M     U$ )Nget_processor
.processor.)hasattrrn   named_children)rj   rk   rl   sub_namechildfn_recursive_add_processorss        r-   ru   HFluxControlNetModel.attn_processors.<locals>.fn_recursive_add_processors   sZ    v//282F2F2H
V:./#)#8#8#:+tfAhZ,@%T $; r,   )strr(   rQ   Moduler   r   rr   )rc   rl   rj   rk   ru   s       @r-   attn_processors#FluxControlNetModel.attn_processorsy   sb     
	c 	588?? 	X\]`bt]tXu 	 !//1LD'jA 2 r,   c           	      d  ^ [        U R                  R                  5       5      n[        U[        5      (       a-  [        U5      U:w  a  [        S[        U5       SU SU S35      eS[        S[        R                  R                  4U4S jjmU R                  5        H  u  p4T" X4U5        M     g)	a  
Sets the attention processor to use to compute attention.

Parameters:
    processor (`dict` of `AttentionProcessor` or only `AttentionProcessor`):
        The instantiated processor class or a dictionary of processor classes that will be set as the processor
        for **all** `Attention` layers.

        If `processor` is a dict, the key needs to define the path to the corresponding cross attention
        processor. This is strongly recommended when setting trainable attention processors.

z>A dict of processors was passed, but the number of processors z0 does not match the number of attention layers: z. Please make sure to pass z processor classes.rj   rk   c                 
  > [        US5      (       aJ  [        U[        5      (       d  UR                  U5        O#UR                  UR	                  U  S35      5        UR                  5        H  u  p4T" U  SU 3XB5        M     g )Nset_processorro   rp   )rq   
isinstancedictr}   poprr   )rj   rk   	processorrs   rt   fn_recursive_attn_processors        r-   r   KFluxControlNetModel.set_attn_processor.<locals>.fn_recursive_attn_processor   ss    v//!)T22((3(($z7J)KL#)#8#8#:+tfAhZ,@%S $;r,   N)rZ   ry   keysr~   r   
ValueErrorrw   r(   rQ   rx   rr   )rc   r   countrj   rk   r   s        @r-   set_attn_processor&FluxControlNetModel.set_attn_processor   s     D((--/0i&&3y>U+BPQTU^Q_P` a005w6QRWQXXkm 
	Tc 	T588?? 	T !//1LD'i@ 2r,   c                    [        UR                  5      nX'S'   X7S'   XGS'   XWS'   U R                  U5      nU(       GaJ  UR                  R	                  UR                  R                  5       5        UR                  R	                  UR                  R                  5       5        UR                  R	                  UR                  R                  5       5        UR                  R	                  UR                  R                  5       5        UR                  R	                  UR                  R                  5       SS9  UR                  R	                  UR                  R                  5       SS9  [        UR                  5      Ul        U$ )Nr;   r<   r=   r>   F)strict)r   configfrom_configrO   load_state_dict
state_dictrP   rS   rT   rW   rX   r   ra   )	clstransformerr;   r<   r=   r>   load_weights_from_transformerr   
controlnets	            r-   from_transformer$FluxControlNetModel.from_transformer   sF    k(())|&7"#'9#$(;$%__V,
(  001F1F1Q1Q1ST&&66{7R7R7]7]7_`''778T8T8_8_8ab!!11+2H2H2S2S2UV))99+:X:X:c:c:ens9t00@@55@@B5 A  0;:;[;[/\J,r,   hidden_statescontrolnet_condcontrolnet_modeconditioning_scaleencoder_hidden_statespooled_projectionstimestepimg_idstxt_idsguidancejoint_attention_kwargsreturn_dictreturnc                    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                  b  U R                  U5      nUR                  u  pnnUU R                  R                  -  nUU R                  R                  -  nUR                  XUU R                  R                  UU R                  R                  5      nUR                  SSSSS	S
5      nUR                  UUU-  S5      nXR                  U5      -   nUR                  UR                   5      S-  nU
b  U
R                  UR                   5      S-  n
OSn
U
c  U R#                  Xv5      OU R#                  XzU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U R(                  (       aP  Uc  [+        S5      eU R-                  U5      n[.        R0                  " UU/SS9n[.        R0                  " U	SS U	/SS9n	[.        R0                  " X4SS9nU R3                  U5      nSn[5        U R6                  5       HZ  u  nn[.        R8                  " 5       (       a)  U R:                  (       a  U R=                  UUUUU5      u  pQOU" UUUUS9u  pQUU4-   nM\     Sn[5        U R>                  5       HZ  u  nn[.        R8                  " 5       (       a)  U R:                  (       a  U R=                  UUUUU5      u  pQOU" UUUUS9u  pQUU4-   nM\     Sn[A        UU RB                  5       H  u  nnU" U5      nUU4-   nM     Sn[A        UU RD                  5       H  u  n nU" U 5      n UU 4-   nM     U V!s/ s H  n!U!U-  PM
     nn!U V!s/ s H  n!U!U-  PM
     nn![G        U5      S:X  a  SOUn[G        U5      S:X  a  SOUn[        (       a  [I        X5        U(       d  UU4$ [K        UUS9$ s  sn!f s  sn!f )a  
The [`FluxTransformer2DModel`] forward method.

Args:
    hidden_states (`torch.FloatTensor` of shape `(batch size, channel, height, width)`):
        Input `hidden_states`.
    controlnet_cond (`torch.Tensor`):
        The conditional input tensor of shape `(batch_size, sequence_length, hidden_size)`.
    controlnet_mode (`torch.Tensor`):
        The mode tensor of shape `(batch_size, 1)`.
    conditioning_scale (`float`, defaults to `1.0`):
        The scale factor for ControlNet outputs.
    encoder_hidden_states (`torch.FloatTensor` of shape `(batch size, sequence_len, embed_dims)`):
        Conditional embeddings (embeddings computed from the input conditions such as prompts) to use.
    pooled_projections (`torch.FloatTensor` 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.
Nscale      ?z\Passing `scale` via `joint_attention_kwargs` when not using the PEFT backend is ineffective.r   r      r1   r
      i  zrPassing `txt_ids` 3d torch.Tensor is deprecated.Please remove the batch dimension and pass it as a 2d torch TensorzrPassing `img_ids` 3d torch.Tensor is deprecated.Please remove the batch dimension and pass it as a 2d torch TensorzA`controlnet_mode` cannot be `None` when applying ControlNet-Union)rI   r#   )r   r   tembimage_rotary_emb)r!   r"   )&copyr   r   r   getloggerwarningrT   r`   shaper   r9   reshapepermutera   todtyperP   rS   ndimr]   r   r_   r(   catrO   	enumeraterW   is_grad_enabledrb   _gradient_checkpointing_funcrX   ziprY   r\   rZ   r   r   )"rc   r   r   r   r   r   r   r   r   r   r   r   r   
lora_scale
batch_sizechannels	height_pwwidth_pwheightwidthr   controlnet_mode_embidsr   block_samplesindex_blockblocksingle_block_samplesr!   block_samplecontrolnet_blockr"   single_block_samplesamples"                                     r-   forwardFluxControlNetModel.forward   s   ^ "-%;%@%@%B"/33GSAJJd/%16L6P6PQXZ^6_6kr 6  ,"33ODO8G8M8M5J)X$++"8"88F 6 66E-55fdkk.D.DeT[[McMcO .55aAq!QGO-55j&5.RTUO%(B(B?(SS;;}223d:{{=#6#67$>HH    >%%h:LM 	
 !% 5 56K L<<1NNU ajG<<1NNU ajG::& !dee"&"?"?"P$)II/BDY.Z`a$b!ii!g 6A>Gii*2>>#."+D,C,C"DK$$&&4+F+F7;7X7X!)$84%} 8="/*?%5	84% *],<<M# #E&  ""+D,J,J"KK$$&&4+F+F7;7X7X!)$84%} 8="/*?%5	84% $8=:J#J # #L( $& .1-AWAW.X*L*+L9L'?</'Q$ /Y +-'589MtOlOl5m1!1"23F"G.MQdPf.f+ 6n
 Og#gNfFF-?$?Nf #gUt*uUt664F+FUt'*u+./G+HA+M4Sk 78A=DCb 	( 1,.MNN#%=,K
 	
 $h*us   ;Q2Q7)rS   rY   r_   r\   ra   rb   rN   r`   rM   rO   rX   rP   rW   r]   rT   )r   
   r5   r6   T)
Nr   NNNNNNNT)r$   r%   r&   r'    _supports_gradient_checkpointingr   intboolr   rL   propertyry   r   classmethodr   r(   r)   float
LongTensorr   r   rw   r   r	   FloatTensorr   r   r+   __classcell__rg   s   @r-   r/   r/   )   s0   '+$ !#"%#%#'%( %$0/3J,J, J, 	J,
 J,  J, !J, !J,  #J, J, S	J, J, *-J, J,X  0 AD  !#"%#%&*  	
   ! F )-$'.2+/%) $ $!%;? w
||w
 w
 	w

 "w
  %||w
 "LLw
 ""w
 w
 w
 ,,w
 !)c3h 8w
 w
 
u  "::	;w
 w
r,   r/   c                   ^  ^  \ rS rSrSrU 4S jr        SS\R                  S\\R                     S\\R                     S\\
   S\R                  S	\R                  S
\R                  S\R                  S\R                  S\R                  S\\\\4      S\S\\\4   4S jjrSrU =r$ )FluxMultiControlNetModeli  a  
`FluxMultiControlNetModel` wrapper class for Multi-FluxControlNetModel

This module is a wrapper for multiple instances of the `FluxControlNetModel`. The `forward()` API is designed to be
compatible with `FluxControlNetModel`.

Args:
    controlnets (`List[FluxControlNetModel]`):
        Provides additional conditioning to the unet during the denoising process. You must set multiple
        `FluxControlNetModel` as a list.
c                 X   > [         TU ]  5         [        R                  " U5      U l        g )N)rK   rL   rQ   rU   nets)rc   controlnetsrg   s     r-   rL   !FluxMultiControlNetModel.__init__  s    MM+.	r,   r   r   r   r   r   r   r   r   r   r   r   r   r   c                 0   [        U R                  5      S:X  a  U R                  S   n[        [        X#U5      5       H  u  nu  nnnU" UUUS S 2S 4   UUU
UUU	UUUS9u  nnUS:X  a  UnUnM1  Ub'  Wb$  [        UU5       VVs/ s H  u  nnUU-   PM     nnnUc  M`  Wc  Me  [        UU5       VVs/ s H  u  nnUU-   PM     nnnM     WW4$ [        [        X#X@R                  5      5       H  u  nu  nnnnU" UUUS S 2S 4   UUU
UUU	UUUS9u  nnUS:X  a  UnUnM2  Ub'  Wb$  [        UU5       VVs/ s H  u  nnUU-   PM     nnnUc  Ma  Wc  Mf  [        UU5       VVs/ s H  u  nnUU-   PM     nnnM     WW4$ s  snnf s  snnf s  snnf s  snnf )Nr1   r   )r   r   r   r   r   r   r   r   r   r   r   r   )rZ   r   r   r   )rc   r   r   r   r   r   r   r   r   r   r   r   r   r   re   imagemoder   r   r   control_block_samplescontrol_single_block_samplescontrol_block_sampler   control_single_block_samples                            r-   r    FluxMultiControlNetModel.forward  sz   " tyy>Q1J+4S[m5n+o''E46@"/$)$(DM',%%'9*?##+A +733  6,9)3G0$05J5V GJJ_anFo1Fo B 4l 1<?Fo . 1 ,7<X<d NQ <>RN8N I ;\ 8,FN 5 847 ,pR %&BBBI 8AO6H))T833E4
 7A"/$)$(DM',%%'9*?##+A +733  6,9)3G0$05J5V GJJ_anFo1Fo B 4l 1<?Fo . 1 ,7<X<d NQ <>RN8N I ;\ 8,FN 5 84;8H %&BBBg1
8D1
8s   F 2F5F#F)r   )NNNNNNNT)r$   r%   r&   r'   __doc__rL   r(   r   r   tensorr   r)   r   r   r   rw   r   r   r	   r   r   r   r+   r   r   s   @r-   r   r     s   
/ /3+/%) $ $!%;? ]C((]C ell+]C ell+	]C
 !K]C  %||]C "LL]C ""]C ]C ]C ,,]C !)c3h 8]C ]C 
#U*	+]C ]Cr,   r   )-dataclassesr   typingr   r   r   r   r   r	   r(   torch.nnrQ   configuration_utilsr   r   loadersr   utilsr   r   r   r   r   attention_processorr   controlnets.controlnetr   r   
embeddingsr   r   r   modeling_outputsr   modeling_utilsr   transformers.transformer_fluxr   r   
get_loggerr$   r   r   r/   r   r#   r,   r-   <module>r      s    " : :   B ' b b 4 Q u u 7 ' \ 
		H	% 9: 9 9
c
*k3C c
LnCz nCr,   