
    +h0                     2   S SK Jr  S SK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  S	SKJr  S	SKJrJ r   S	SK!J"r"  SSK#J$r$  \RJ                  " \&5      r'\ " S S\5      5       r( " S S\\\5      r)g)    )	dataclass)AnyDictOptionalTupleUnionN)nn   )ConfigMixinregister_to_config)PeftAdapterMixin)USE_PEFT_BACKEND
BaseOutputloggingscale_lora_layersunscale_lora_layers   )AttentionProcessor)
PatchEmbedPixArtAlphaTextProjection)Transformer2DModelOutput)
ModelMixin)AdaLayerNormSingleRMSNorm)SanaTransformerBlock   )zero_modulec                   :    \ rS rSr% \\R                     \S'   Srg)SanaControlNetOutput$   controlnet_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_sana.pyr   r   $   s    #ELL11r+   r   c            &         ^  \ rS rSrSrSS/rSS/r\                 S*S\S	\	\   S
\S\S\S\	\   S\	\   S\	\   S\S\
S\
S\S\S\S\S\
S\	\   SS4$U 4S jjj5       r\S\\\4   4S j5       rS\\\\\4   4   4S jr     S+S\R*                  S\R*                  S \R,                  S!\R*                  S"\
S#\	\R*                     S$\	\R*                     S%\	\\\4      S&\S\\\R*                  S'4   \4   4S( jjrS)rU =r$ ),SanaControlNetModel)   Tr   r   patch_embednormNin_channelsout_channelsnum_attention_headsattention_head_dim
num_layersnum_cross_attention_headscross_attention_head_dimcross_attention_dimcaption_channels	mlp_ratiodropoutattention_biassample_size
patch_sizenorm_elementwise_affinenorm_epsinterpolation_scalereturnc                   > [         TU ]  5         U=(       d    UnX4-  n[        UUUUUUUb  SOS S9U l        [	        U5      U l        [        U	US9U l        [        USSS9U l	        [        R                  " [        U5       Vs/ s H  n[        UUUUUUUUUUU
S9PM     sn5      U l        [        R                  " / 5      U l        [!        [        R"                  " UU5      5      U l        [        ['        U R                  5      5       H@  n[        R"                  " UU5      n[!        U5      nU R                  R)                  U5        MB     SU l        g s  snf )	Nsincos)heightwidthr?   r2   	embed_dimrB   pos_embed_type)in_featureshidden_sizegh㈵>T)epselementwise_affine)r<   r7   r8   r9   r=   r@   rA   r;   F)super__init__r   r0   r   
time_embedr   caption_projectionr   caption_normr	   
ModuleListranger   transformer_blockscontrolnet_blocksr   Linearinput_blocklenappendgradient_checkpointing)selfr2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   	inner_dim_controlnet_block	__class__s                        r,   rO   SanaControlNetModel.__init__.   sb   * 	#2{'<	 &!# 3':'F8D
 -Y7";HXfo"p#I4DQ #%-- z* +A %'&#.G-E(;#1,C%' +#
( "$r!2&ryyI'FGs42234A!yyI>*+;<""))*:; 5
 ',#7s   E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.)hasattrrg   named_children)rc   rd   re   sub_namechildfn_recursive_add_processorss        r,   rn   HSanaControlNetModel.attn_processors.<locals>.fn_recursive_add_processors   sZ    v//282F2F2H
V:./#)#8#8#:+tfAhZ,@%T $; r+   )strr'   r	   Moduler   r   rk   )r\   re   rc   rd   rn   s       @r,   attn_processors#SanaControlNetModel.attn_processorsx   sb     
	c 	588?? 	X\]`bt]tXu 	 !//1LD'jA 2 r+   	processorc           	      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.rc   rd   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_processorrh   ri   )rj   
isinstancedictrw   poprk   )rc   rd   rt   rl   rm   fn_recursive_attn_processors        r,   r{   KSanaControlNetModel.set_attn_processor.<locals>.fn_recursive_attn_processor   ss    v//!)T22((3(($z7J)KL#)#8#8#:+tfAhZ,@%S $;r+   N)rY   rr   keysrx   ry   
ValueErrorrp   r'   r	   rq   rk   )r\   rt   countrc   rd   r{   s        @r,   set_attn_processor&SanaControlNetModel.set_attn_processor   s     D((--/0i&&3y>U+BPQTU^Q_P` a005w6QRWQXXkm 
	Tc 	T588?? 	T !//1LD'i@ 2r+   hidden_statesencoder_hidden_statestimestepcontrolnet_condconditioning_scaleencoder_attention_maskattention_maskattention_kwargsreturn_dict.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bB  UR                  S:X  a2  SUR                  UR                  5      -
  S-  nUR                  S5      nUbB  UR                  S:X  a2  SUR                  UR                  5      -
  S-  nUR                  S5      nUR                  u  ppU R                  R                  nX-  X-  nnU R                  U5      nXR                  U R                  UR                  UR                  5      5      5      -   nU R!                  X;UR                  S9u  nnU R#                  U5      nUR%                  USUR                  S   5      nU R'                  U5      nS	n[(        R*                  " 5       (       aC  U R,                  (       a2  U R.                   H!  nU R1                  UUUUUUUU5      nUU4-   nM#     O'U R.                   H  nU" UUUUUUU5      nUU4-   nM     S	n[3        UU R4                  5       H  u  nnU" U5      nUU4-   nM     [        (       a  [7        X
5        U Vs/ s H  nUU-  PM
     nnU	(       d  U4$ [9        US
9$ s  snf )Nscale      ?zVPassing `scale` via `attention_kwargs` when not using the PEFT backend is ineffective.r   r   g     )
batch_sizehidden_dtyper"   )r!   )copyrz   r   r   getloggerwarningndimtodtype	unsqueezeshapeconfigr?   r0   rX   rP   rQ   viewrR   r'   is_grad_enabledr[   rU   _gradient_checkpointing_funcziprV   r   r   )r\   r   r   r   r   r   r   r   r   r   
lora_scaler   num_channelsrF   rG   ppost_patch_heightpost_patch_widthembedded_timestepblock_res_samplesblockcontrolnet_block_res_samplesblock_res_sampler_   samples                            r,   forwardSanaControlNetModel.forward   s
    '/446)--gs;JJd/+0@0D0DWd0S0_l %.*=*=*B
  ."3"3M4G4G"HHHTN+55a8N "-2H2M2MQR2R&'*@*C*CMDWDW*X&X\d%d"%;%E%Ea%H" 3@2E2E/
&KK"".4k5:+((7%(8(89I9I/J\J\]j]p]pJq9r(ss&*oo-:M:M '6 '
## !% 7 78M N 5 : ::r=K^K^_aKb c $ 1 12G H   ""t'B'B00 $ A A!")*%$	! %68H$H! 1 00 %!")*%$! %68H$H! 1 (*$256GI_I_2`../0@A+GK[J]+]( 3a 1Rn'oRn1C(CRn$'o022#=YZZ (ps   K)rR   rQ   rV   r[   rX   r0   rP   rU   )    r   F   r         p   i  i 	  g      @g        Fr   r   Fgư>N)r   NNNT)r#   r$   r%   r&    _supports_gradient_checkpointing_no_split_modules _skip_layerwise_casting_patternsr   intr   floatboolrO   propertyr   rp   r   rr   r   r   r'   r(   
LongTensorr   r   r   r   r*   __classcell__)r`   s   @r,   r.   r.   )   sk   '+$/>(5v'>$ &(#%"$3525-1 $$(--1%G,G, smG, !	G,
  G, G, $,C=G, #+3-G, &c]G, G, G, G, G, G, G,  "&!G," #G,$ &c]%G,& 
'G, G,R c+=&=!>  0 AE2Dd3PbKbFc2c,d  AP %(9=1559 n[||n[  %||n[ ""	n[
 n[ "n[ !) 6n[ !.n[ #4S>2n[ n[ 
uU\\3&')AA	Bn[ n[r+   r.   )*dataclassesr   typingr   r   r   r   r   r'   r	   configuration_utilsr   r   loadersr   utilsr   r   r   r   r   attention_processorr   
embeddingsr   r   modeling_outputsr   modeling_utilsr   normalizationr   r   transformers.sana_transformerr   
controlnetr   
get_loggerr#   r   r   r.   r"   r+   r,   <module>r      sy    " 4 4   B ' b b 4 > 7 ' 7 @ # 
		H	% 2: 2 2y[*k3C y[r+   