
    +h                         S SK r S SK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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JrJr   " S
 S\\\\5      rg)    N)DictUnion   )ConfigMixinregister_to_config)PeftAdapterMixinUNet2DConditionLoadersMixin)ADDED_KV_ATTENTION_PROCESSORSCROSS_ATTENTION_PROCESSORSAttentionProcessorAttnAddedKVProcessorAttnProcessor)
ModelMixin   )	AttnBlockResBlockTimestepBlockWuerstchenLayerNormc                      ^  \ rS rSrSrSr\SU 4S jj5       r\S\	\
\4   4S j5       rS\\\	\
\4   4   4S jrS	 rSS
 jrS rSrU =r$ )WuerstchenPrior#   priorTc                   > [         T	U ]  5         X@l        [        R                  " XSS9U l        [        R                  " [        R                  " X25      [        R                  " S5      [        R                  " X"5      5      U l	        [        R                  " 5       U l        [        U5       Hn  nU R                  R                  [        X'S95        U R                  R                  [        X$5      5        U R                  R                  [!        X"USUS95        Mp     [        R                  " [#        USSS	9[        R                  " X!S
-  SS95      U l        SU l        U R)                  5         g )Nr   )kernel_sizeg?)dropoutT)	self_attnr   Fgư>)elementwise_affineeps   )super__init__c_rnnConv2d
projection
SequentialLinear	LeakyReLUcond_mapper
ModuleListblocksrangeappendr   r   r   r   outgradient_checkpointingset_default_attn_processor)
selfc_incc_condr"   depthnheadr   _	__class__s
            r/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/pipelines/wuerstchen/modeling_wuerstchen_prior.pyr!   WuerstchenPrior.__init__'   s   ))D;==IIf LLIIaO
 mmouAKKx;<KK}Q45KKyugVW  ==eFIIaq1

 ',#'')    returnc                    ^ 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.)hasattrrB   named_children)r>   r?   r@   sub_namechildfn_recursive_add_processorss        r9   rI   DWuerstchenPrior.attn_processors.<locals>.fn_recursive_add_processorsK   sZ    v//282F2F2H
V:./#)#8#8#:+tfAhZ,@%T $; r;   )strtorchr#   Moduler   r   rF   )r1   r@   r>   r?   rI   s       @r9   attn_processorsWuerstchenPrior.attn_processors@   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.r>   r?   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_processorrC   rD   )rE   
isinstancedictrS   poprF   )r>   r?   rP   rG   rH   fn_recursive_attn_processors        r9   rW   GWuerstchenPrior.set_attn_processor.<locals>.fn_recursive_attn_processoro   ss    v//!)T22((3(($z7J)KL#)#8#8#:+tfAhZ,@%S $;r;   N)lenrN   keysrT   rU   
ValueErrorrK   rL   r#   rM   rF   )r1   rP   countr>   r?   rW   s        @r9   set_attn_processor"WuerstchenPrior.set_attn_processorZ   s     D((--/0i&&3y>U+BPQTU^Q_P` a005w6QRWQXXkm 
	Tc 	T588?? 	T !//1LD'i@ 2r;   c           	      ~   [        S U R                  R                  5        5       5      (       a  [        5       nOr[        S U R                  R                  5        5       5      (       a  [	        5       nO8[        S[        [        U R                  R                  5       5      5       35      eU R                  U5        g)zU
Disables custom attention processors and sets the default attention implementation.
c              3   F   #    U  H  oR                   [        ;   v   M     g 7fN)r8   r
   .0procs     r9   	<genexpr>=WuerstchenPrior.set_default_attn_processor.<locals>.<genexpr>   s     iKh4~~!>>Kh   !c              3   F   #    U  H  oR                   [        ;   v   M     g 7fra   )r8   r   rb   s     r9   re   rf      s     hJg$#==Jgrg   zOCannot call `set_default_attn_processor` when attention processors are of type N)	allrN   valuesr   r   r[   nextiterr]   )r1   rP   s     r9   r0   *WuerstchenPrior.set_default_attn_processor}   s     i4K_K_KfKfKhiii,.Ih$J^J^JeJeJghhh%Iabfgklp  mA  mA  mH  mH  mJ  hK  cL  bM  N  		*r;   c                    X-  nU R                   S-  n[        R                  " U5      US-
  -  n[        R                  " X1R
                  S9R                  5       R                  U* 5      R                  5       nUS S 2S 4   US S S 24   -  n[        R                  " UR                  5       UR                  5       /SS9nU R                   S-  S:X  a  [        R                  R                  USSS9nUR                  UR                   S9$ )	Nr   r   )devicedim)r   r   constant)mode)dtype)r"   mathlogrL   arangero   floatmulexpcatsincosr#   
functionalpadtort   )r1   rmax_positionshalf_dimembs        r9   gen_r_embeddingWuerstchenPrior.gen_r_embedding   s    88q=hh}%A6ll8HH5;;=AA3$GKKM4j3tQw<'iiCGGI.A688a<1--##Cj#ACvvAGGv$$r;   c                    UnU R                  U5      nU R                  U5      nU R                  U5      n[        R                  " 5       (       a  U R
                  (       aw  U R                   Hf  n[        U[        5      (       a  U R                  XqU5      nM,  [        U[        5      (       a  U R                  XqU5      nMU  U R                  Xq5      nMh     OYU R                   HI  n[        U[        5      (       a
  U" X5      nM"  [        U[        5      (       a
  U" X5      nMA  U" U5      nMK     U R                  U5      R                  SSS9u  pXH-
  SU	-
  R                  5       S-   -  $ )Nr   r   rp   gh㈵>)r%   r)   r   rL   is_grad_enabledr/   r+   rT   r   _gradient_checkpointing_funcr   r.   chunkabs)
r1   xr   r3   x_inc_embedr_embedblockabs
             r9   forwardWuerstchenPrior.forward   s,   OOA""1%&&q)  ""t'B'BeY//99%GLA}5599%GLA99%CA % eY//a)A}55a)AaA % xx{   *a!e[[]T122r;   )r+   r"   r)   r/   r.   r%   )   i   i   @   r   r   g?)i'  )__name__
__module____qualname____firstlineno__	unet_name _supports_gradient_checkpointingr   r!   propertyr   rK   r   rN   r   r]   r0   r   r   __static_attributes____classcell__)r8   s   @r9   r   r   #   s    I'+$* *0 c+=&=!>  0 AE2Dd3PbKbFc2c,d  AF+	%3 3r;   r   )ru   typingr   r   rL   torch.nnr#   configuration_utilsr   r   loadersr   r	   models.attention_processorr
   r   r   r   r   models.modeling_utilsr   modeling_wuerstchen_commonr   r   r   r   r    r;   r9   <module>r      sD         B D  0 _ _K3j+/JL\ K3r;   