
    +h|                        S SK r S SKJr  S SKJr  S SKrS SKJr  S SK	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  \R,                  R.                   " S
 S\5      5       r\R,                  R.                   " S S\5      5       r " S S\R4                  5      r " S S\R4                  5      r " S S\R4                  5      r " S S\R4                  5      r " S S\R4                  5      r " S S\R4                  5      r  " S S\R4                  5      r! " S S\R4                  5      r" " S S\R4                  5      r# " S  S!\$5      r%\ " S" S#\R4                  \\5      5       r&g)$    N)partial)Tuple)
FrozenDict   )ConfigMixinflax_register_to_config)
BaseOutput   )FlaxModelMixinc                   8    \ rS rSr% Sr\R                  \S'   Srg)FlaxDecoderOutput    a  
Output of decoding method.

Args:
    sample (`jnp.ndarray` of shape `(batch_size, num_channels, height, width)`):
        The decoded output sample from the last layer of the model.
    dtype (`jnp.dtype`, *optional*, defaults to `jnp.float32`):
        The `dtype` of the parameters.
sample N)	__name__
__module____qualname____firstlineno____doc__jnpndarray__annotations____static_attributes__r       S/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/models/vae_flax.pyr   r       s     KKr   r   c                   $    \ rS rSr% SrS\S'   Srg)FlaxAutoencoderKLOutput/   a4  
Output of AutoencoderKL encoding method.

Args:
    latent_dist (`FlaxDiagonalGaussianDistribution`):
        Encoded outputs of `Encoder` represented as the mean and logvar of `FlaxDiagonalGaussianDistribution`.
        `FlaxDiagonalGaussianDistribution` allows for sampling latents from the distribution.
 FlaxDiagonalGaussianDistributionlatent_distr   N)r   r   r   r   r   r   r   r   r   r   r   r   /   s     43r   r   c                   f    \ rS rSr% Sr\\S'   \R                  r	\R                  \S'   S r
S rSrg)	FlaxUpsample2D=   z
Flax implementation of 2D Upsample layer

Args:
    in_channels (`int`):
        Input channels
    dtype (:obj:`jnp.dtype`, *optional*, defaults to jnp.float32):
        Parameters `dtype`
in_channelsdtypec                 f    [         R                  " U R                  SSSU R                  S9U l        g )N   r(   r
   r
   r)   r)   kernel_sizestridespaddingr%   nnConvr$   r%   convselfs    r   setupFlaxUpsample2D.setupK   s+    GG$**
	r   c                     UR                   u  p#pE[        R                  R                  UX#S-  US-  U4SS9nU R	                  U5      nU$ )Nr   nearest)shapemethod)r9   jaximageresizer2   )r4   hidden_statesbatchheightwidthchannelss         r   __call__FlaxUpsample2D.__call__T   sY    )6)<)<&u		((1*eai: ) 

 		-0r   r2   Nr   r   r   r   r   intr   r   float32r%   r5   rC   r   r   r   r   r"   r"   =   s+     {{E399"
r   r"   c                   f    \ rS rSr% Sr\\S'   \R                  r	\R                  \S'   S r
S rSrg)	FlaxDownsample2D_   z
Flax implementation of 2D Downsample layer

Args:
    in_channels (`int`):
        Input channels
    dtype (:obj:`jnp.dtype`, *optional*, defaults to jnp.float32):
        Parameters `dtype`
r$   r%   c                 f    [         R                  " U R                  SSSU R                  S9U l        g )Nr'   )r   r   VALIDr+   r/   r3   s    r   r5   FlaxDownsample2D.setupm   s+    GG**
	r   c                 T    Sn[         R                  " XS9nU R                  U5      nU$ )N)r   r   r   r
   rQ   rP   )	pad_width)r   padr2   )r4   r>   rS   s      r   rC   FlaxDownsample2D.__call__v   s)    .=		-0r   rE   NrF   r   r   r   rJ   rJ   _   s+     {{E399"
r   rJ   c                       \ rS rSr% Sr\\S'   Sr\\S'   Sr\	\S'   Sr
\\S	'   Sr\\S
'   \R                  r\R                  \S'   S rSS jrSrg)FlaxResnetBlock2D}   aC  
Flax implementation of 2D Resnet Block.

Args:
    in_channels (`int`):
        Input channels
    out_channels (`int`):
        Output channels
    dropout (:obj:`float`, *optional*, defaults to 0.0):
        Dropout rate
    groups (:obj:`int`, *optional*, defaults to `32`):
        The number of groups to use for group norm.
    use_nin_shortcut (:obj:`bool`, *optional*, defaults to `None`):
        Whether to use `nin_shortcut`. This activates a new layer inside ResNet block
    dtype (:obj:`jnp.dtype`, *optional*, defaults to jnp.float32):
        Parameters `dtype`
r$   Nout_channels        dropoutr   groupsuse_nin_shortcutr%   c                    U R                   c  U R                  OU R                   n[        R                  " U R                  SS9U l        [        R                  " USSSU R                  S9U l        [        R                  " U R                  SS9U l	        [        R                  " U R                  5      U l        [        R                  " USSSU R                  S9U l        U R                  c  U R                  U:g  OU R                  nS U l        U(       a(  [        R                  " USSSU R                  S9U l        g g )Nư>
num_groupsepsilonr'   r)   r*   r+   rM   )rX   r$   r0   	GroupNormr[   norm1r1   r%   conv1norm2DropoutrZ   dropout_layerconv2r\   conv_shortcut)r4   rX   r\   s      r   r5   FlaxResnetBlock2D.setup   s   +/+<+<+Dt''$J[J[\\T[[$G
WW$**

 \\T[[$G
ZZ5WW$**

 @D?T?T?\4++|;bfbwbw!!#"jj"D r   c                 L   UnU R                  U5      n[        R                  " U5      nU R                  U5      nU R	                  U5      n[        R                  " U5      nU R                  X5      nU R                  U5      nU R                  b  U R                  U5      nX-   $ N)rc   r0   swishrd   re   rg   rh   ri   )r4   r>   deterministicresiduals       r   rC   FlaxResnetBlock2D.__call__   s     

=1/

=1

=1/**=H

=1)))(3H''r   )rd   rh   ri   rg   rc   re   T)r   r   r   r   r   rG   r   rX   rZ   floatr[   r\   boolr   rH   r%   r5   rC   r   r   r   r   rV   rV   }   sU    $ L#GUFC!d!{{E399" D(r   rV   c                       \ rS rSr% Sr\\S'   Sr\\S'   Sr\\S'   \	R                  r\	R                  \S'   S	 rS
 rS rSrg)FlaxAttentionBlock   a  
Flax Convolutional based multi-head attention block for diffusion-based VAE.

Parameters:
    channels (:obj:`int`):
        Input channels
    num_head_channels (:obj:`int`, *optional*, defaults to `None`):
        Number of attention heads
    num_groups (:obj:`int`, *optional*, defaults to `32`):
        The number of groups to use for group norm
    dtype (:obj:`jnp.dtype`, *optional*, defaults to jnp.float32):
        Parameters `dtype`

rB   Nnum_head_channelsr   r`   r%   c                 b   U R                   b  U R                  U R                   -  OSU l        [        [        R
                  U R                  U R                  S9n[        R                  " U R                  SS9U l	        U" 5       U" 5       U" 5       sU l
        U l        U l        U" 5       U l        g )Nr
   r%   r^   r_   )rw   rB   	num_headsr   r0   Denser%   rb   r`   
group_normquerykeyvalue	proj_attn)r4   denses     r   r5   FlaxAttentionBlock.setup   s{    DHDZDZDf$*@*@@lm$--tzzB,,$//4P+07EGUW(
DHdjr   c                     UR                   S S U R                  S4-   nUR                  U5      n[        R                  " US5      nU$ )Nr   r   r
   r(   )r9   rz   reshaper   	transpose)r4   
projectionnew_projection_shapenew_projections       r   transpose_for_scores'FlaxAttentionBlock.transpose_for_scores   sJ    )//47KK#++,@A~|Dr   c                 4   UnUR                   u  p4pVU R                  U5      nUR                  X4U-  U45      nU R                  U5      nU R	                  U5      nU R                  U5      n	U R                  U5      nU R                  U5      nU R                  U	5      n	S[        R                  " [        R                  " U R                  U R                  -  5      5      -  n
[        R                  " SXz-  X-  5      n[        R                  " USS9n[        R                  " SX5      n[        R                  " US5      nUR                   S S U R                  4-   nUR                  U5      nU R!                  U5      nUR                  X4XV45      nX-   nU$ )Nr
   z...qc,...kc->...qkr   axisz...kc,...qk->...qcr   )r9   r|   r   r}   r~   r   r   mathsqrtrB   rz   r   einsumr0   softmaxr   r   )r4   r>   ro   r?   r@   rA   rB   r}   r~   r   scaleattn_weightsnew_hidden_states_shapes                r   rC   FlaxAttentionBlock.__call__   sp    )6)<)<&u6%--uunh.OP

=)hh}%

=) ))%0'',))%0 DIIdii(FGHHzz"6s{Szz,R8 

#7Mm\B"/"5"5cr":dmm=M"M%--.EF}5%--ue.NO%0r   )r|   r~   rz   r   r}   r   )r   r   r   r   r   rG   r   rw   r`   r   rH   r%   r5   r   rC   r   r   r   r   ru   ru      sD     M!s!J{{E399"! r   ru   c                       \ rS rSr% Sr\\S'   \\S'   Sr\\S'   Sr	\\S'   S	r
\\S
'   Sr\\S'   \R                  r\R                  \S'   S rSS jrSrg)FlaxDownEncoderBlock2Di  a  
Flax Resnet blocks-based Encoder block for diffusion-based VAE.

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 Resnet layer block
    resnet_groups (:obj:`int`, *optional*, defaults to `32`):
        The number of groups to use for the Resnet block group norm
    add_downsample (:obj:`bool`, *optional*, defaults to `True`):
        Whether to add downsample layer
    dtype (:obj:`jnp.dtype`, *optional*, defaults to jnp.float32):
        Parameters `dtype`
r$   rX   rY   rZ   r
   
num_layersr   resnet_groupsTadd_downsampler%   c           	      ~   / n[        U R                  5       Hg  nUS:X  a  U R                  OU R                  n[	        UU R                  U R
                  U R                  U R                  S9nUR                  U5        Mi     Xl	        U R                  (       a$  [        U R                  U R                  S9U l        g g Nr   r$   rX   rZ   r[   r%   ry   )ranger   r$   rX   rV   rZ   r   r%   appendresnetsr   rJ   downsamplers_0r4   r   ir$   	res_blocks        r   r5   FlaxDownEncoderBlock2D.setup0  s    t'A./1f$**$:K:KK)'!..))jjI NN9% ( "243D3DDJJ"WD r   c                 |    U R                    H	  nU" XS9nM     U R                  (       a  U R                  U5      nU$ Nrn   )r   r   r   r4   r>   rn   resnets       r   rC   FlaxDownEncoderBlock2D.__call__B  s;    llF"=NM #  //>Mr   )r   r   Nrq   )r   r   r   r   r   rG   r   rZ   rr   r   r   r   rs   r   rH   r%   r5   rC   r   r   r   r   r   r     sZ    ( GUJM3ND{{E399"X$r   r   c                       \ rS rSr% Sr\\S'   \\S'   Sr\\S'   Sr	\\S'   S	r
\\S
'   Sr\\S'   \R                  r\R                  \S'   S rSS jrSrg)FlaxUpDecoderBlock2DiL  a  
Flax Resnet blocks-based Decoder block for diffusion-based VAE.

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 Resnet layer block
    resnet_groups (:obj:`int`, *optional*, defaults to `32`):
        The number of groups to use for the Resnet block group norm
    add_upsample (:obj:`bool`, *optional*, defaults to `True`):
        Whether to add upsample layer
    dtype (:obj:`jnp.dtype`, *optional*, defaults to jnp.float32):
        Parameters `dtype`
r$   rX   rY   rZ   r
   r   r   r   Tadd_upsampler%   c           	      ~   / n[        U R                  5       Hg  nUS:X  a  U R                  OU R                  n[	        UU R                  U R
                  U R                  U R                  S9nUR                  U5        Mi     Xl	        U R                  (       a$  [        U R                  U R                  S9U l        g g r   )r   r   r$   rX   rV   rZ   r   r%   r   r   r   r"   upsamplers_0r   s        r   r5   FlaxUpDecoderBlock2D.setupi  s    t'A./1f$**$:K:KK)'!..))jjI NN9% (  .t/@/@

 SD r   c                 |    U R                    H	  nU" XS9nM     U R                  (       a  U R                  U5      nU$ r   )r   r   r   r   s       r   rC   FlaxUpDecoderBlock2D.__call__{  s;    llF"=NM #  --m<Mr   )r   r   Nrq   )r   r   r   r   r   rG   r   rZ   rr   r   r   r   rs   r   rH   r%   r5   rC   r   r   r   r   r   r   L  sZ    ( GUJM3L${{E399"T$r   r   c                       \ rS rSr% Sr\\S'   Sr\\S'   Sr	\\S'   Sr
\\S	'   Sr\\S
'   \R                  r\R                  \S'   S rSS jrSrg)FlaxUNetMidBlock2Di  av  
Flax Unet Mid-Block module.

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 Resnet layer block
    resnet_groups (:obj:`int`, *optional*, defaults to `32`):
        The number of groups to use for the Resnet and Attention block group norm
    num_attention_heads (:obj:`int`, *optional*, defaults to `1`):
        Number of attention heads for each attention block
    dtype (:obj:`jnp.dtype`, *optional*, defaults to jnp.float32):
        Parameters `dtype`
r$   rY   rZ   r
   r   r   r   num_attention_headsr%   c           	      *   U R                   b  U R                   O[        U R                  S-  S5      n[        U R                  U R                  U R                  UU R
                  S9/n/ n[        U R                  5       H  n[        U R                  U R                  UU R
                  S9nUR                  U5        [        U R                  U R                  U R                  UU R
                  S9nUR                  U5        M     X l        X0l        g )N   r   r   )rB   rw   r`   r%   )r   minr$   rV   rZ   r%   r   r   ru   r   r   r   
attentions)r4   r   r   r   _
attn_blockr   s          r   r5   FlaxUNetMidBlock2D.setup  s    .2.@.@.L**RUVZVfVfjkVkmoRp  ,,!--$jj
 
t'A+))"&":":(jj	J j)) ,,!--$jjI NN9%! ($ $r   c                     U R                   S   " XS9n[        U R                  U R                   SS  5       H  u  p4U" U5      nU" XS9nM     U$ )Nr   r   r
   )r   zipr   )r4   r>   rn   attnr   s        r   rC   FlaxUNetMidBlock2D.__call__  sS    QSab1ABLD /M"=NM C r   )r   r   Nrq   )r   r   r   r   r   rG   r   rZ   rr   r   r   r   r   rH   r%   r5   rC   r   r   r   r   r   r     sU    $ GUJM3  {{E399"#%Jr   r   c                       \ rS 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'   Sr\\S'   \R&                  r\R(                  \S'   S rSS\4S jjrSrg)FlaxEncoderi  a  
Flax Implementation of VAE Encoder.

This model is a Flax Linen [flax.linen.Module](https://flax.readthedocs.io/en/latest/flax.linen.html#module)
subclass. Use it as a regular Flax linen Module and refer to the Flax documentation for all matter related to
general usage and behavior.

Finally, this model supports inherent JAX features such as:
- [Just-In-Time (JIT) compilation](https://jax.readthedocs.io/en/latest/jax.html#just-in-time-compilation-jit)
- [Automatic Differentiation](https://jax.readthedocs.io/en/latest/jax.html#automatic-differentiation)
- [Vectorization](https://jax.readthedocs.io/en/latest/jax.html#vectorization-vmap)
- [Parallelization](https://jax.readthedocs.io/en/latest/jax.html#parallelization-pmap)

Parameters:
    in_channels (:obj:`int`, *optional*, defaults to 3):
        Input channels
    out_channels (:obj:`int`, *optional*, defaults to 3):
        Output channels
    down_block_types (:obj:`Tuple[str]`, *optional*, defaults to `(DownEncoderBlock2D)`):
        DownEncoder block type
    block_out_channels (:obj:`Tuple[str]`, *optional*, defaults to `(64,)`):
        Tuple containing the number of output channels for each block
    layers_per_block (:obj:`int`, *optional*, defaults to `2`):
        Number of Resnet layer for each block
    norm_num_groups (:obj:`int`, *optional*, defaults to `32`):
        norm num group
    act_fn (:obj:`str`, *optional*, defaults to `silu`):
        Activation function
    double_z (:obj:`bool`, *optional*, defaults to `False`):
        Whether to double the last output channels
    dtype (:obj:`jnp.dtype`, *optional*, defaults to jnp.float32):
        Parameters `dtype`
r(   r$   rX   DownEncoderBlock2Ddown_block_types@   block_out_channelsr   layers_per_blockr   norm_num_groupssiluact_fnFdouble_zr%   c           
         U R                   n[        R                  " US   SSSU R                  S9U l        / nUS   n[        U R                  5       H^  u  pEUnX   nU[        U5      S-
  :H  n[        UUU R                  U R                  U(       + U R                  S9nUR                  U5        M`     X l        [        US   U R                  S U R                  S	9U l        U R                  (       a  S
U R                   -  OU R                   n	[        R"                  " U R                  SS9U l        [        R                  " U	SSSU R                  S9U l        g )Nr   r'   r)   r*   r+   r
   )r$   rX   r   r   r   r%   r   r$   r   r   r%   r   r^   r_   )r   r0   r1   r%   conv_in	enumerater   lenr   r   r   r   down_blocksr   	mid_blockr   rX   rb   conv_norm_outconv_out)
r4   r   r   output_channelr   r   input_channelis_final_block
down_blockconv_out_channelss
             r   r5   FlaxEncoder.setup  sN   !44wwq!$**
 +A.d334DA*M/2N#&8"9A"==N/)+00"22#11jjJ z* 5 ' ,*2... $**	
 6:]]A 1 11HYHY\\T5I5ISWX$**
r   rn   c                     U R                  U5      nU R                   H	  nU" XS9nM     U R                  XS9nU R                  U5      n[        R
                  " U5      nU R                  U5      nU$ r   )r   r   r   r   r0   rm   r   r4   r   rn   blocks       r   rC   FlaxEncoder.__call__+  sq    f% %%E6?F & D ##F+&!v&r   )r   r   r   r   r   Nrq   )r   r   r   r   r   r$   rG   r   rX   r   r   strr   r   r   r   r   rs   r   rH   r%   r5   rC   r   r   r   r   r   r     s     D KL##:eCj:%*c
*cOSFCHd{{E399"/
bd  r   r   c                       \ rS 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\4S jjrSrg)FlaxDecoderi>  ay  
Flax Implementation of VAE Decoder.

This model is a Flax Linen [flax.linen.Module](https://flax.readthedocs.io/en/latest/flax.linen.html#module)
subclass. Use it as a regular Flax linen Module and refer to the Flax documentation for all matter related to
general usage and behavior.

Finally, this model supports inherent JAX features such as:
- [Just-In-Time (JIT) compilation](https://jax.readthedocs.io/en/latest/jax.html#just-in-time-compilation-jit)
- [Automatic Differentiation](https://jax.readthedocs.io/en/latest/jax.html#automatic-differentiation)
- [Vectorization](https://jax.readthedocs.io/en/latest/jax.html#vectorization-vmap)
- [Parallelization](https://jax.readthedocs.io/en/latest/jax.html#parallelization-pmap)

Parameters:
    in_channels (:obj:`int`, *optional*, defaults to 3):
        Input channels
    out_channels (:obj:`int`, *optional*, defaults to 3):
        Output channels
    up_block_types (:obj:`Tuple[str]`, *optional*, defaults to `(UpDecoderBlock2D)`):
        UpDecoder block type
    block_out_channels (:obj:`Tuple[str]`, *optional*, defaults to `(64,)`):
        Tuple containing the number of output channels for each block
    layers_per_block (:obj:`int`, *optional*, defaults to `2`):
        Number of Resnet layer for each block
    norm_num_groups (:obj:`int`, *optional*, defaults to `32`):
        norm num group
    act_fn (:obj:`str`, *optional*, defaults to `silu`):
        Activation function
    double_z (:obj:`bool`, *optional*, defaults to `False`):
        Whether to double the last output channels
    dtype (:obj:`jnp.dtype`, *optional*, defaults to jnp.float32):
        parameters `dtype`
r(   r$   rX   UpDecoderBlock2Dup_block_typesr   r   r   r   r   r   r   r   r%   c           
         U R                   n[        R                  " US   SSSU R                  S9U l        [        US   U R                  S U R                  S9U l        [        [        U5      5      nUS   n/ n[        U R                  5       Hc  u  pVUnX%   nU[        U5      S-
  :H  n[        UUU R                  S-   U R                  U(       + U R                  S	9n	UR                  U	5        UnMe     X@l        [        R"                  " U R                  S
S9U l        [        R                  " U R&                  SSSU R                  S9U l        g )Nr   r'   r)   r*   r+   r   r   r
   )r$   rX   r   r   r   r%   r^   r_   )r   r0   r1   r%   r   r   r   r   listreversedr   r   r   r   r   r   	up_blocksrb   r   rX   r   )
r4   r   reversed_block_out_channelsr   r   r   r   prev_output_channelr   up_blocks
             r   r5   FlaxDecoder.setupj  sS   !44 wwr"$**
 ,*2... $**	
 '+84F+G&H#4Q7	d112DA"08;N#&8"9A"==N+/+0014"22!//jjH X&"0 3" #  \\T5I5ISWX$**
r   rn   c                     U R                  U5      nU R                  XS9nU R                   H	  nU" XS9nM     U R                  U5      n[        R
                  " U5      nU R                  U5      nU$ r   )r   r   r   r   r0   rm   r   r   s       r   rC   FlaxDecoder.__call__  so    f% D ^^E6?F $ ##F+&!v&r   )r   r   r   r   r   Nrq   )r   r   r   r   r   r$   rG   r   rX   r   r   r   r   r   r   r   r   rH   r%   r5   rs   rC   r   r   r   r   r   r   >  s     D KL#!6NE#J6##cOSFC{{E399"3
jd  r   r   c                   D    \ rS rSrS
S jrS rSS jr/ SQ4S jrS rS	r	g)r   i  c                    [         R                  " USSS9u  U l        U l        [         R                  " U R                  SS5      U l        X l        [         R                  " SU R                  -  5      U l        [         R                  " U R                  5      U l        U R
                  (       a-  [         R                  " U R                  5      =U l        U l        g g )Nr   r   r   g      >g      4@      ?)
r   splitmeanlogvarcliprn   expstdvar
zeros_like)r4   
parametersrn   s      r   __init__)FlaxDiagonalGaussianDistribution.__init__  s    !$:qr!B	4;hht{{E48*773,-774;;'"%..";;DHtx r   c                     U R                   U R                  [        R                  R	                  XR                   R
                  5      -  -   $ rl   )r   r   r;   randomnormalr9   )r4   r~   s     r   r   'FlaxDiagonalGaussianDistribution.sample  s1    yy488cjj&7&7YY__&MMMMr   Nc                    U R                   (       a  [        R                  " S/5      $ UcD  S[        R                  " U R                  S-  U R
                  -   S-
  U R                  -
  / SQS9-  $ S[        R                  " [        R                  " U R                  UR                  -
  5      UR
                  -  U R
                  UR
                  -  -   S-
  U R                  -
  UR                  -   / SQS9-  $ )NrY   r   r   g      ?r
   r   r(   r   )rn   r   arraysumr   r   r   square)r4   others     r   kl#FlaxDiagonalGaussianDistribution.kl  s    99cU##=A!83!>!LS\]]]SWWJJtyy5::-.:TXX		=QQTWWZ^ZeZeehmhthtt
 
 	
r   r  c                 H   U R                   (       a  [        R                  " S/5      $ [        R                  " S[        R                  -  5      nS[        R
                  " X0R                  -   [        R                  " XR                  -
  5      U R                  -  -   US9-  $ )NrY   g       @r   r   )
rn   r   r	  logpir
  r   r  r   r   )r4   r   r   logtwopis       r   nll$FlaxDiagonalGaussianDistribution.nll  sq    99cU##773<(SWWX3cjj))AS6TW[W_W_6__fjkkkr   c                     U R                   $ rl   )r   r3   s    r   mode%FlaxDiagonalGaussianDistribution.mode  s    yyr   )rn   r   r   r   r   )Frl   )
r   r   r   r   r  r   r  r  r  r   r   r   r   r   r     s"    <N

  ) lr   r   c                   j   \ rS 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'   Sr\\S'   Sr\\S'   Sr\\S'   Sr\\S'   \R,                  r\R.                  \S'   S rS\R4                  S\4S jrS#S\S\4S jjrS#S\S\4S jjrS$S\S\4S  jjr S!r!g")%FlaxAutoencoderKLi  a
  
Flax implementation of a VAE model with KL loss for decoding latent representations.

This model inherits from [`FlaxModelMixin`]. Check the superclass documentation for it's generic methods
implemented for all models (such as downloading or saving).

This model is a Flax Linen [flax.linen.Module](https://flax.readthedocs.io/en/latest/flax.linen.html#module)
subclass. Use it as a regular Flax Linen module and refer to the Flax documentation for all matter related to its
general usage and behavior.

Inherent JAX features such as the following are supported:

- [Just-In-Time (JIT) compilation](https://jax.readthedocs.io/en/latest/jax.html#just-in-time-compilation-jit)
- [Automatic Differentiation](https://jax.readthedocs.io/en/latest/jax.html#automatic-differentiation)
- [Vectorization](https://jax.readthedocs.io/en/latest/jax.html#vectorization-vmap)
- [Parallelization](https://jax.readthedocs.io/en/latest/jax.html#parallelization-pmap)

Parameters:
    in_channels (`int`, *optional*, defaults to 3):
        Number of channels in the input image.
    out_channels (`int`, *optional*, defaults to 3):
        Number of channels in the output.
    down_block_types (`Tuple[str]`, *optional*, defaults to `(DownEncoderBlock2D)`):
        Tuple of downsample block types.
    up_block_types (`Tuple[str]`, *optional*, defaults to `(UpDecoderBlock2D)`):
        Tuple of upsample block types.
    block_out_channels (`Tuple[str]`, *optional*, defaults to `(64,)`):
        Tuple of block output channels.
    layers_per_block (`int`, *optional*, defaults to `2`):
        Number of ResNet layer for each block.
    act_fn (`str`, *optional*, defaults to `silu`):
        The activation function to use.
    latent_channels (`int`, *optional*, defaults to `4`):
        Number of channels in the latent space.
    norm_num_groups (`int`, *optional*, defaults to `32`):
        The number of groups for normalization.
    sample_size (`int`, *optional*, defaults to 32):
        Sample input size.
    scaling_factor (`float`, *optional*, defaults to 0.18215):
        The component-wise standard deviation of the trained latent space computed using the first batch of the
        training set. This is used to scale the latent space to have unit variance when training the diffusion
        model. The latents are scaled with the formula `z = z * scaling_factor` before being passed to the
        diffusion model. When decoding, the latents are scaled back to the original scale with the formula: `z = 1
        / scaling_factor * z`. For more details, refer to sections 4.3.2 and D.1 of the [High-Resolution Image
        Synthesis with Latent Diffusion Models](https://huggingface.co/papers/2112.10752) paper.
    dtype (`jnp.dtype`, *optional*, defaults to `jnp.float32`):
        The `dtype` of the parameters.
r(   r$   rX   r   r   r   r   r   r   r
   r   r   r   r   latent_channelsr   r   sample_sizeg{P?scaling_factorr%   c                    [        U R                  R                  U R                  R                  U R                  R                  U R                  R
                  U R                  R                  U R                  R                  U R                  R                  SU R                  S9	U l
        [        U R                  R                  U R                  R                  U R                  R                  U R                  R
                  U R                  R                  U R                  R                  U R                  R                  U R                  S9U l        [        R                   " SU R                  R                  -  SSSU R                  S9U l        [        R                   " U R                  R                  SSSU R                  S9U l        g )NT)	r$   rX   r   r   r   r   r   r   r%   )r$   rX   r   r   r   r   r   r%   r   r)   rM   r+   )r   configr$   r  r   r   r   r   r   r%   encoderr   rX   r   decoderr0   r1   
quant_convpost_quant_convr3   s    r   r5   FlaxAutoencoderKL.setup  sG   "//44![[99#{{==![[99;;%% KK77**

 #3311;;55#{{==![[99 KK77;;%%**	
 ''+++**
  "wwKK''** 
r   rngreturnc                    SU R                   U R                  U R                  4n[        R                  " U[        R                  S9n[
        R                  R                  US5      u  pEnXEUS.nU R                  Xs5      S   $ )Nr
   ry   r(   )paramsrZ   gaussianr'  )	r$   r  r   zerosrH   r;   r  r   init)r4   r$  sample_shaper   
params_rngdropout_rnggaussian_rngrngss           r   init_weightsFlaxAutoencoderKL.init_weights;  so    4++T-=-=t?O?OP<s{{;03

0@0@a0H-
$,Wyy&x00r   rn   return_dictc                     [         R                  " US5      nU R                  XS9nU R                  U5      n[	        U5      nU(       d  U4$ [        US9$ )Nr   r   r(   r
   r   )r    )r   r   r  r!  r   r   )r4   r   rn   r2  r>   moments	posteriors          r   encodeFlaxAutoencoderKL.encodeE  sP    v|4VI//-04W=	<&9==r   c                    UR                   S   U R                  R                  :w  a  [        R                  " US5      nU R                  U5      nU R                  XBS9n[        R                  " US5      nU(       d  U4$ [        US9$ )Nr   r4  r   )r   r(   r
   r   r   )r9   r  r  r   r   r"  r   r   )r4   latentsrn   r2  r>   s        r   decodeFlaxAutoencoderKL.decodeQ  st    == ; ;;mmG\:G,,W5]Pm\B!## 66r   c                    U R                  XUS9nU(       a-  U R                  S5      nUR                  R                  U5      nOUR                  R	                  5       nU R                  XtS9R                  nU(       d  U4$ [        US9$ )N)rn   r2  r(  )r2  r:  )r7  make_rngr    r   r  r<  r   )r4   r   sample_posteriorrn   r2  r6  r$  r>   s           r   rC   FlaxAutoencoderKL.__call___  s|    KKQ\K]	--
+C%1188=M%11668M]DKK9 //r   )r   r  r"  r!  N)TT)FTT)"r   r   r   r   r   r$   rG   r   rX   r   r   r   r   r   r   r   r  r   r  r  rr   r   rH   r%   r5   r;   Arrayr   r0  rs   r7  r<  rC   r   r   r   r   r  r    s    /b KL##:eCj:!6NE#J6%*c
*cFCOSOSK#NE#{{E399"#
J1		 1j 1
>D 
>d 
>7T 7t 70d 0`d 0 0r   r  )'r   	functoolsr   typingr   flax
flax.linenlinenr0   r;   	jax.numpynumpyr   flax.core.frozen_dictr   configuration_utilsr   r   utilsr	   modeling_flax_utilsr   struct	dataclassr   r   Moduler"   rJ   rV   ru   r   r   r   r   r   objectr   r  r   r   r   <module>rR     sG  "      
  , F  / 
   
4j 
4 
4RYY Dryy <J(		 J(ZF FR6RYY 6r6299 6rE EPn")) nbp")) pf"v "J U0		>; U0 U0r   