
    +h=                        S SK Jr  S SKJr  SSKJr  SSKJ	r	J
r
Jr   " S S\R                  5      r " S S\R                  5      r " S	 S
\R                  5      r " S S\R                  5      r " S S\R                  5      rg)    N   )FlaxTransformer2DModel)FlaxDownsample2DFlaxResnetBlock2DFlaxUpsample2Dc                       \ rS rSr% Sr\\S'   \\S'   Sr\\S'   Sr	\\S'   Sr
\\S	'   S
r\\S'   Sr\\S'   Sr\\S'   Sr\\S'   Sr\\S'   \R$                  r\R&                  \S'   Sr\\S'   S rSS jrSrg)FlaxCrossAttnDownBlock2D   a  
Cross Attention 2D Downsizing block - original architecture from Unet transformers:
https://huggingface.co/papers/2103.06104

Parameters:
    in_channels (:obj:`int`):
        Input channels
    out_channels (:obj:`int`):
        Output channels
    dropout (:obj:`float`, *optional*, defaults to 0.0):
        Dropout rate
    num_layers (:obj:`int`, *optional*, defaults to 1):
        Number of attention blocks layers
    num_attention_heads (:obj:`int`, *optional*, defaults to 1):
        Number of attention heads of each spatial transformer block
    add_downsample (:obj:`bool`, *optional*, defaults to `True`):
        Whether to add downsampling layer before each final output
    use_memory_efficient_attention (`bool`, *optional*, defaults to `False`):
        enable memory efficient attention https://huggingface.co/papers/2112.05682
    split_head_dim (`bool`, *optional*, defaults to `False`):
        Whether to split the head dimension into a new axis for the self-attention computation. In most cases,
        enabling this flag should speed up the computation for Stable Diffusion 2.x and Stable Diffusion XL.
    dtype (:obj:`jnp.dtype`, *optional*, defaults to jnp.float32):
        Parameters `dtype`
in_channelsout_channels        dropout   
num_layersnum_attention_headsTadd_downsampleFuse_linear_projectiononly_cross_attentionuse_memory_efficient_attentionsplit_head_dimdtypetransformer_layers_per_blockc                    / n/ n[        U R                  5       H  nUS:X  a  U R                  OU R                  n[	        UU R                  U R
                  U R                  S9nUR                  U5        [        U R                  U R                  U R                  U R                  -  U R                  U R                  U R                  U R                  U R                  U R                  S9	nUR                  U5        M     Xl        X l        U R"                  (       a$  [%        U R                  U R                  S9U l        g g )Nr   r   r   dropout_probr   	r   n_headsd_headdepthr   r   r   r   r   r   )ranger   r   r   r   r   r   appendr   r   r   r   r   r   r   resnets
attentionsr   r   downsamplers_0)selfr#   r$   ir   	res_block
attn_blocks          d/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/models/unets/unet_2d_blocks_flax.pysetupFlaxCrossAttnDownBlock2D.setup>   s   
t'A./1f$**$:K:KK)'!..!\\jj	I NN9%/ --00((D,D,DD77&*&@&@%)%>%>/3/R/R#22jj
J j)- (0 $"243D3DDJJ"WD     c                     Sn[        U R                  U R                  5       H  u  pgU" XUS9nU" XUS9nXQ4-  nM     U R                  (       a  U R	                  U5      nXQ4-  nX4$ N deterministic)zipr#   r$   r   r%   )r&   hidden_statestembencoder_hidden_statesr2   output_statesresnetattns           r*   __call__!FlaxCrossAttnDownBlock2D.__call__`   su    doo>LF"=mTM UbcM--M ?
  //>M--M++r-   )r$   r%   r#   NT)__name__
__module____qualname____firstlineno____doc__int__annotations__r   floatr   r   r   boolr   r   r   r   jnpfloat32r   r   r+   r:   __static_attributes__r0   r-   r*   r	   r	      s    4 GUJ  ND"'4'!&$&+0"D0 ND {{E399"() #) XD,r-   r	   c                       \ rS rSr% Sr\\S'   \\S'   Sr\\S'   Sr	\\S'   S	r
\\S
'   \R                  r\R                  \S'   S rSS jrSrg)FlaxDownBlock2Do   a&  
Flax 2D downsizing block

Parameters:
    in_channels (:obj:`int`):
        Input channels
    out_channels (:obj:`int`):
        Output channels
    dropout (:obj:`float`, *optional*, defaults to 0.0):
        Dropout rate
    num_layers (:obj:`int`, *optional*, defaults to 1):
        Number of attention blocks layers
    add_downsample (:obj:`bool`, *optional*, defaults to `True`):
        Whether to add downsampling layer before each final output
    dtype (:obj:`jnp.dtype`, *optional*, defaults to jnp.float32):
        Parameters `dtype`
r   r   r   r   r   r   Tr   r   c                 h   / n[        U R                  5       H\  nUS:X  a  U R                  OU R                  n[	        UU R                  U R
                  U R                  S9nUR                  U5        M^     Xl        U R                  (       a$  [        U R                  U R                  S9U l        g g )Nr   r   r    )r!   r   r   r   r   r   r   r"   r#   r   r   r%   )r&   r#   r'   r   r(   s        r*   r+   FlaxDownBlock2D.setup   s    t'A./1f$**$:K:KK)'!..!\\jj	I NN9% ( "243D3DDJJ"WD r-   c                     SnU R                    H  nU" XUS9nXA4-  nM     U R                  (       a  U R                  U5      nXA4-  nX4$ r/   )r#   r   r%   )r&   r4   r5   r2   r7   r8   s         r*   r:   FlaxDownBlock2D.__call__   s[    llF"=mTM--M #  //>M--M++r-   )r%   r#   Nr<   )r=   r>   r?   r@   rA   rB   rC   r   rD   r   r   rE   rF   rG   r   r+   r:   rH   r0   r-   r*   rJ   rJ   o   sP    $ GUJND{{E399"X$,r-   rJ   c                       \ rS rSr% Sr\\S'   \\S'   \\S'   Sr\\S'   Sr	\\S	'   Sr
\\S
'   Sr\\S'   Sr\\S'   Sr\\S'   Sr\\S'   Sr\\S'   \R$                  r\R&                  \S'   Sr\\S'   S rSS jrSrg)FlaxCrossAttnUpBlock2D   a  
Cross Attention 2D Upsampling block - original architecture from Unet transformers:
https://huggingface.co/papers/2103.06104

Parameters:
    in_channels (:obj:`int`):
        Input channels
    out_channels (:obj:`int`):
        Output channels
    dropout (:obj:`float`, *optional*, defaults to 0.0):
        Dropout rate
    num_layers (:obj:`int`, *optional*, defaults to 1):
        Number of attention blocks layers
    num_attention_heads (:obj:`int`, *optional*, defaults to 1):
        Number of attention heads of each spatial transformer block
    add_upsample (:obj:`bool`, *optional*, defaults to `True`):
        Whether to add upsampling layer before each final output
    use_memory_efficient_attention (`bool`, *optional*, defaults to `False`):
        enable memory efficient attention https://huggingface.co/papers/2112.05682
    split_head_dim (`bool`, *optional*, defaults to `False`):
        Whether to split the head dimension into a new axis for the self-attention computation. In most cases,
        enabling this flag should speed up the computation for Stable Diffusion 2.x and Stable Diffusion XL.
    dtype (:obj:`jnp.dtype`, *optional*, defaults to jnp.float32):
        Parameters `dtype`
r   r   prev_output_channelr   r   r   r   r   Tadd_upsampleFr   r   r   r   r   r   c                    / n/ n[        U R                  5       GH  nX0R                  S-
  :X  a  U R                  OU R                  nUS:X  a  U R                  OU R                  n[        XT-   U R                  U R                  U R                  S9nUR                  U5        [        U R                  U R                  U R                  U R                  -  U R                  U R                  U R                  U R                  U R                  U R                  S9	nUR                  U5        GM     Xl        X l        U R$                  (       a$  ['        U R                  U R                  S9U l        g g )Nr   r   r   r   r    )r!   r   r   r   rS   r   r   r   r"   r   r   r   r   r   r   r   r#   r$   rT   r   upsamplers_0)r&   r#   r$   r'   res_skip_channelsresnet_in_channelsr(   r)   s           r*   r+   FlaxCrossAttnUpBlock2D.setup   s8   
t'A56//A:M5M 0 0TXTeTe=>!V!9!9IZIZ).B!..!\\jj	I NN9%/ --00((D,D,DD77&*&@&@%)%>%>/3/R/R#22jj
J j)/ (2 $ .t/@/@

 SD r-   c                     [        U R                  U R                  5       H3  u  pgUS   nUS S n[        R                  " X4SS9nU" XUS9nU" XUS9nM5     U R
                  (       a  U R                  U5      nU$ N)axisr1   )r3   r#   r$   rF   concatenaterT   rV   )	r&   r4   res_hidden_states_tupler5   r6   r2   r8   r9   res_hidden_statess	            r*   r:   FlaxCrossAttnUpBlock2D.__call__   s    doo>LF 7 ;&=cr&B#OO],NUWXM"=mTM UbcM ?  --m<Mr-   )r$   r#   rV   Nr<   )r=   r>   r?   r@   rA   rB   rC   r   rD   r   r   rT   rE   r   r   r   r   rF   rG   r   r   r+   r:   rH   r0   r-   r*   rQ   rQ      s    4 GUJ  L$"'4'!&$&+0"D0 ND {{E399"() #)!TFr-   rQ   c                       \ rS rSr% Sr\\S'   \\S'   \\S'   Sr\\S'   Sr	\\S	'   S
r
\\S'   \R                  r\R                  \S'   S rSS jrSrg)FlaxUpBlock2Di  a|  
Flax 2D upsampling block

Parameters:
    in_channels (:obj:`int`):
        Input channels
    out_channels (:obj:`int`):
        Output channels
    prev_output_channel (:obj:`int`):
        Output channels from the previous block
    dropout (:obj:`float`, *optional*, defaults to 0.0):
        Dropout rate
    num_layers (:obj:`int`, *optional*, defaults to 1):
        Number of attention blocks layers
    add_downsample (:obj:`bool`, *optional*, defaults to `True`):
        Whether to add downsampling layer before each final output
    dtype (:obj:`jnp.dtype`, *optional*, defaults to jnp.float32):
        Parameters `dtype`
r   r   rS   r   r   r   r   TrT   r   c                    / n[        U R                  5       H  nX R                  S-
  :X  a  U R                  OU R                  nUS:X  a  U R                  OU R                  n[        XC-   U R                  U R                  U R                  S9nUR                  U5        M     Xl	        U R                  (       a$  [        U R                  U R                  S9U l        g g )Nr   r   r   r    )r!   r   r   r   rS   r   r   r   r"   r#   rT   r   rV   )r&   r#   r'   rW   rX   r(   s         r*   r+   FlaxUpBlock2D.setup"  s    t'A56//A:M5M 0 0TXTeTe=>!V!9!9IZIZ).B!..!\\jj	I NN9% (  .t/@/@

 SD r-   c                     U R                    H*  nUS   nUS S n[        R                  " X4SS9nU" XUS9nM,     U R                  (       a  U R	                  U5      nU$ r[   )r#   rF   r^   rT   rV   )r&   r4   r_   r5   r2   r8   r`   s          r*   r:   FlaxUpBlock2D.__call__6  si    llF 7 ;&=cr&B#OO],NUWXM"=mTM #  --m<Mr-   )r#   rV   Nr<   )r=   r>   r?   r@   rA   rB   rC   r   rD   r   rT   rE   rF   rG   r   r+   r:   rH   r0   r-   r*   rc   rc     sV    ( GUJL${{E399"T(r-   rc   c                       \ rS rSr% Sr\\S'   Sr\\S'   Sr	\\S'   Sr
\\S'   S	r\\S
'   S	r\\S'   S	r\\S'   \R                   r\R"                  \S'   Sr\\S'   S rSS jrSrg)FlaxUNetMidBlock2DCrossAttniE  a  
Cross Attention 2D Mid-level block - original architecture from Unet transformers:
https://huggingface.co/papers/2103.06104

Parameters:
    in_channels (:obj:`int`):
        Input channels
    dropout (:obj:`float`, *optional*, defaults to 0.0):
        Dropout rate
    num_layers (:obj:`int`, *optional*, defaults to 1):
        Number of attention blocks layers
    num_attention_heads (:obj:`int`, *optional*, defaults to 1):
        Number of attention heads of each spatial transformer block
    use_memory_efficient_attention (`bool`, *optional*, defaults to `False`):
        enable memory efficient attention https://huggingface.co/papers/2112.05682
    split_head_dim (`bool`, *optional*, defaults to `False`):
        Whether to split the head dimension into a new axis for the self-attention computation. In most cases,
        enabling this flag should speed up the computation for Stable Diffusion 2.x and Stable Diffusion XL.
    dtype (:obj:`jnp.dtype`, *optional*, defaults to jnp.float32):
        Parameters `dtype`
r   r   r   r   r   r   Fr   r   r   r   r   c                 H   [        U R                  U R                  U R                  U R                  S9/n/ n[	        U R
                  5       H  n[        U R                  U R                  U R                  U R                  -  U R                  U R                  U R                  U R                  U R                  S9nUR                  U5        [        U R                  U R                  U R                  U R                  S9nUR                  U5        M     Xl        X l        g )Nr   )r   r   r   r   r   r   r   r   )r   r   r   r   r!   r   r   r   r   r   r   r   r"   r#   r$   )r&   r#   r$   _r)   r(   s         r*   r+   !FlaxUNetMidBlock2DCrossAttn.setupf  s      ,,!--!\\jj	
 
t'A/ ,,00''4+C+CC77&*&@&@/3/R/R#22jj	J j)) ,,!--!\\jj	I NN9%' (* $r-   c                     U R                   S   " X5      n[        U R                  U R                   SS  5       H  u  pVU" XUS9nU" XUS9nM     U$ )Nr   r   r1   )r#   r3   r$   )r&   r4   r5   r6   r2   r9   r8   s          r*   r:   $FlaxUNetMidBlock2DCrossAttn.__call__  sX    Q<ab1ABLD UbcM"=mTM C r-   )r$   r#   Nr<   )r=   r>   r?   r@   rA   rB   rC   r   rD   r   r   r   rE   r   r   rF   rG   r   r   r+   r:   rH   r0   r-   r*   ri   ri   E  sv    , GUJ  "'4'+0"D0 ND {{E399"() #)#%Jr-   ri   )
flax.linenlinennn	jax.numpynumpyrF   attention_flaxr   resnet_flaxr   r   r   Moduler	   rJ   rQ   rc   ri   r0   r-   r*   <module>rw      sp      3 M MV,ryy V,r7,bii 7,tYRYY Yx=BII =@L")) Lr-   