
    +h                     @   S SK Jr  S SKJrJr  S SKrS SKrS SKJ	r	  SSK
Jr  SSKJr  SSKJr  SS	KJr  SS
KJrJrJrJr  \ " S S\5      5       r\ " S S\5      5       r " S S\	R2                  5      r " S S\	R2                  5      r " S S\	R2                  5      r " S S\	R2                  5      r " S S\	R2                  5      r " S S\	R2                  5      r " S S\ 5      r! " S S\ 5      r" " S S \	R2                  5      r# " S! S"\	R2                  5      r$g)#    )	dataclass)OptionalTupleN   )
BaseOutput)randn_tensor   )get_activation)SpatialNorm)AutoencoderTinyBlockUNetMidBlock2Dget_down_blockget_up_blockc                   8    \ rS rSr% Sr\R                  \S'   Srg)EncoderOutput!   z
Output of encoding method.

Args:
    latent (`torch.Tensor` of shape `(batch_size, num_channels, latent_height, latent_width)`):
        The encoded latent.
latent N)	__name__
__module____qualname____firstlineno____doc__torchTensor__annotations____static_attributes__r       [/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/models/autoencoders/vae.pyr   r   !   s     LLr   r   c                   `    \ rS rSr% Sr\R                  \S'   Sr\	\R                     \S'   Srg)DecoderOutput.   z
Output of decoding method.

Args:
    sample (`torch.Tensor` of shape `(batch_size, num_channels, height, width)`):
        The decoded output sample from the last layer of the model.
sampleNcommit_lossr   )r   r   r   r   r   r   r   r   r$   r   FloatTensorr   r   r   r   r!   r!   .   s(     LL/3K%++,3r   r!   c                      ^  \ rS rSrSr         SS\S\S\\S4   S\\S4   S\S	\S
\S\4U 4S jjjr	S\
R                  S\
R                  4S jrSrU =r$ )Encoder<   aw  
The `Encoder` layer of a variational autoencoder that encodes its input into a latent representation.

Args:
    in_channels (`int`, *optional*, defaults to 3):
        The number of input channels.
    out_channels (`int`, *optional*, defaults to 3):
        The number of output channels.
    down_block_types (`Tuple[str, ...]`, *optional*, defaults to `("DownEncoderBlock2D",)`):
        The types of down blocks to use. See `~diffusers.models.unet_2d_blocks.get_down_block` for available
        options.
    block_out_channels (`Tuple[int, ...]`, *optional*, defaults to `(64,)`):
        The number of output channels for each block.
    layers_per_block (`int`, *optional*, defaults to 2):
        The number of layers per block.
    norm_num_groups (`int`, *optional*, defaults to 32):
        The number of groups for normalization.
    act_fn (`str`, *optional*, defaults to `"silu"`):
        The activation function to use. See `~diffusers.models.activations.get_activation` for available options.
    double_z (`bool`, *optional*, defaults to `True`):
        Whether to double the number of output channels for the last block.
in_channelsout_channelsdown_block_types.block_out_channelslayers_per_blocknorm_num_groupsact_fndouble_zc
                   > [         TU ]  5         XPl        [        R                  " UUS   SSSS9U l        [        R                  " / 5      U l        US   n
[        U5       HY  u  pU
nXK   n
U[        U5      S-
  :H  n[        UU R                  UU
U(       + SSUUU
S S9nU R                  R                  U5        M[     [        US   SUSSUS   US U	S	9	U l        [        R                  " US   USS
9U l        [        R                   " 5       U l        U(       a  SU-  OUn[        R                  " US   USSS9U l        SU l        g )Nr   r      kernel_sizestridepaddingư>)

num_layersr)   r*   add_downsample
resnet_epsdownsample_paddingresnet_act_fnresnet_groupsattention_head_dimtemb_channelsdefault	r)   r:   r<   output_scale_factorresnet_time_scale_shiftr>   r=   r?   add_attentionnum_channels
num_groupsepsr	   r6   F)super__init__r-   nnConv2dconv_in
ModuleListdown_blocks	enumeratelenr   appendr   	mid_block	GroupNormconv_norm_outSiLUconv_actconv_outgradient_checkpointing)selfr)   r*   r+   r,   r-   r.   r/   r0   mid_block_add_attentionoutput_channelidown_block_typeinput_channelis_final_block
down_blockconv_out_channels	__class__s                    r   rL   Encoder.__init__T   sl    	 0yyq!
 ==, ,A."+,<"=A*M/2N#&8"9A"==N'00)+#11#$$-#1"J ##J/% #>* (*2.  !$-1"5)1

  \\7I"7MZiost	08A,l		"4R"8:KQXYZ&+#r   r#   returnc                    U R                  U5      n[        R                  " 5       (       aR  U R                  (       aA  U R                   H  nU R                  X!5      nM     U R                  U R                  U5      nO,U R                   H  nU" U5      nM     U R                  U5      nU R                  U5      nU R                  U5      nU R                  U5      nU$ )z*The forward method of the `Encoder` class.)
rO   r   is_grad_enabledr[   rQ   _gradient_checkpointing_funcrU   rW   rY   rZ   )r\   r#   rc   s      r   forwardEncoder.forward   s     f%  ""t'B'B"..
:::N / 66t~~vNF #..
#F+ / ^^F+F ##F+v&v&r   )rY   rO   rW   rZ   rQ   r[   r-   rU   )	r   r   )DownEncoderBlock2D@   r	       siluTT)r   r   r   r   r   intr   strboolrL   r   r   rk   r   __classcell__re   s   @r   r'   r'   <   s    2 ,C.3 !! $C,C, C,  S/	C,
 "#s(OC, C, C, C, C, C,Jell u||  r   r'   c                      ^  \ rS rSrSr         SS\S\S\\S4   S\\S4   S\S	\S
\S\4U 4S jjjr SS\	R                  S\\	R                     S\	R                  4S jjrSrU =r$ )Decoder   av  
The `Decoder` layer of a variational autoencoder that decodes its latent representation into an output sample.

Args:
    in_channels (`int`, *optional*, defaults to 3):
        The number of input channels.
    out_channels (`int`, *optional*, defaults to 3):
        The number of output channels.
    up_block_types (`Tuple[str, ...]`, *optional*, defaults to `("UpDecoderBlock2D",)`):
        The types of up blocks to use. See `~diffusers.models.unet_2d_blocks.get_up_block` for available options.
    block_out_channels (`Tuple[int, ...]`, *optional*, defaults to `(64,)`):
        The number of output channels for each block.
    layers_per_block (`int`, *optional*, defaults to 2):
        The number of layers per block.
    norm_num_groups (`int`, *optional*, defaults to 32):
        The number of groups for normalization.
    act_fn (`str`, *optional*, defaults to `"silu"`):
        The activation function to use. See `~diffusers.models.activations.get_activation` for available options.
    norm_type (`str`, *optional*, defaults to `"group"`):
        The normalization type to use. Can be either `"group"` or `"spatial"`.
r)   r*   up_block_types.r,   r-   r.   r/   	norm_typec
                    > [         TU ]  5         XPl        [        R                  " UUS   SSSS9U l        [        R                  " / 5      U l        US:X  a  UOS n
[        US   SUSUS:X  a  SOUUS   UU
U	S	9	U l	        [        [        U5      5      nUS
   n[        U5       H_  u  pUnX   nU[        U5      S-
  :H  n[        UU R                  S-   UUUU(       + SUUUU
US9nU R                  R                  U5        UnMa     US:X  a  [!        US
   U
5      U l        O[        R$                  " US
   USS9U l        [        R&                  " 5       U l        [        R                  " US
   USSS9U l        SU l        g )Nr@   r   r2   r3   spatialr7   grouprA   rB   r   r8   r)   r*   prev_output_channeladd_upsampler:   r<   r=   r>   r?   rD   rF   rJ   F)rK   rL   r-   rM   rN   rO   rP   	up_blocksr   rU   listreversedrR   rS   r   rT   r   rW   rV   rX   rY   rZ   r[   )r\   r)   r*   rz   r,   r-   r.   r/   r{   r]   r?   reversed_block_out_channelsr^   r_   up_block_typer   rb   up_blockre   s                     r   rL   Decoder.__init__   s    	 0yyr"
 r*'0I'=4 (*2.  !1:g1EI91"5)'1

 '+84F+G&H#4Q7 ). 9A"08;N#&8"9A"==N#0014/+$7!//$-#1+(1H NN!!(+"0+ !:0 	!!,-?-BM!RD!#;Ma;P]lrv!wD			"4Q"7qRST&+#r   r#   latent_embedsrg   c                    U R                  U5      n[        [        U R                  R	                  5       5      5      R
                  n[        R                  " 5       (       ad  U R                  (       aS  U R                  U R                  X5      nUR                  U5      nU R                   H  nU R                  XAU5      nM     O=U R                  X5      nUR                  U5      nU R                   H  nU" X5      nM     Uc  U R                  U5      nOU R                  X5      nU R                  U5      nU R                  U5      nU$ )z*The forward method of the `Decoder` class.)rO   nextiterr   
parametersdtyper   ri   r[   rj   rU   torW   rY   rZ   )r\   r#   r   upscale_dtyper   s        r   rk   Decoder.forward  s    f%T$..";";"=>?EE  ""t'B'B66t~~v]FYY}-F !NN::8][ + ^^F:FYY}-F !NN!&8 +  ''/F''>Fv&v&r   )rY   rO   rW   rZ   r[   r-   rU   r   )	r   r   UpDecoderBlock2Drn   r	   rp   rq   r~   TNr   r   r   r   r   rr   r   rs   rL   r   r   r   rk   r   ru   rv   s   @r   rx   rx      s    0 *?.3 !!  $J,J, J, c3h	J,
 "#s(OJ, J, J, J, J, J,^ 15##  -# 
	# #r   rx   c                   v   ^  \ rS rSrSrS\S\SS4U 4S jjrS\R                  S\R                  4S	 jr	S
r
U =r$ )UpSamplei>  a
  
The `UpSample` layer of a variational autoencoder that upsamples its input.

Args:
    in_channels (`int`, *optional*, defaults to 3):
        The number of input channels.
    out_channels (`int`, *optional*, defaults to 3):
        The number of output channels.
r)   r*   rg   Nc                 r   > [         TU ]  5         Xl        X l        [        R
                  " XSSSS9U l        g )N   r	   r2   r3   )rK   rL   r)   r*   rM   ConvTranspose2ddeconv)r\   r)   r*   re   s      r   rL   UpSample.__init__I  s7    
 	&(((PQZ[efgr   xc                 T    [         R                  " U5      nU R                  U5      nU$ )z+The forward method of the `UpSample` class.)r   relur   r\   r   s     r   rk   UpSample.forwardS  s!    JJqMKKNr   )r   r)   r*   r   r   r   r   r   rr   rL   r   r   rk   r   ru   rv   s   @r   r   r   >  sM    hh h 
	h %,,  r   r   c                      ^  \ rS rSrSr   SS\S\S\S\SS4
U 4S	 jjjrSS
\R                  S\R                  4S jjr	Sr
U =r$ )MaskConditionEncoderiZ  z!
used in AsymmetricAutoencoderKL
in_chout_chres_chr5   rg   Nc                 8  > [         TU ]  5         / nUS:  a8  US-  nUS-  nX#:  a  UnUS:X  a  UnUR                  Xb45        US-  nUS:  a  M8  / nU H  u  pUR                  U	5        M     UR                  US   S   5        / n
Un[        [	        U5      5       Hb  nX{   nUS:X  d  US:X  a'  U
R                  [
        R                  " XlSSSS95        O&U
R                  [
        R                  " XlSSSS95        UnMd     [
        R                  " U
6 U l        g )Nr2   r	   r@   r   r   r3   r   )	rK   rL   rT   rangerS   rM   rN   
Sequentiallayers)r\   r   r   r   r5   channelsin_ch_r*   _in_ch_out_chr   lout_ch_re   s                r   rL   MaskConditionEncoder.__init___  s    	qjq[FaZF{OOV,-aKF qj 'OF(  (HRLO,s<()A"oGAvabiiQqZ[\]biiQqZ[\]F * mmV,r   r   c                     0 n[        [        U R                  5      5       HP  nU R                  U   nU" U5      nX[        [	        UR
                  5      5      '   [        R                  " U5      nMR     U$ )z7The forward method of the `MaskConditionEncoder` class.)r   rS   r   rs   tupleshaper   r   )r\   r   maskoutr   layers         r   rk   MaskConditionEncoder.forward  s^    s4;;'(AKKNEaA'(E!''N#$

1A	 )
 
r   )r   )   i      r   r   rv   s   @r   r   r   Z  sn     #-#- #- 	#-
 #- 
#- #-J U\\  r   r   c                     ^  \ rS rSrSr        SS\S\S\\S4   S\\S4   S\S	\S
\S\4U 4S jjjr   SS\	R                  S\\	R                     S\\	R                     S\\	R                     S\	R                  4
S jjrSrU =r$ )MaskConditionDecoderi  a  The `MaskConditionDecoder` should be used in combination with [`AsymmetricAutoencoderKL`] to enhance the model's
decoder with a conditioner on the mask and masked image.

Args:
    in_channels (`int`, *optional*, defaults to 3):
        The number of input channels.
    out_channels (`int`, *optional*, defaults to 3):
        The number of output channels.
    up_block_types (`Tuple[str, ...]`, *optional*, defaults to `("UpDecoderBlock2D",)`):
        The types of up blocks to use. See `~diffusers.models.unet_2d_blocks.get_up_block` for available options.
    block_out_channels (`Tuple[int, ...]`, *optional*, defaults to `(64,)`):
        The number of output channels for each block.
    layers_per_block (`int`, *optional*, defaults to 2):
        The number of layers per block.
    norm_num_groups (`int`, *optional*, defaults to 32):
        The number of groups for normalization.
    act_fn (`str`, *optional*, defaults to `"silu"`):
        The activation function to use. See `~diffusers.models.activations.get_activation` for available options.
    norm_type (`str`, *optional*, defaults to `"group"`):
        The normalization type to use. Can be either `"group"` or `"spatial"`.
r)   r*   rz   .r,   r-   r.   r/   r{   c	                 *  > [         TU ]  5         XPl        [        R                  " UUS   SSSS9U l        [        R                  " / 5      U l        US:X  a  UOS n	[        US   SUSUS:X  a  SOUUS   UU	S	9U l	        [        [        U5      5      n
U
S
   n[        U5       H_  u  pUnX   nU[        U5      S-
  :H  n[        UU R                  S-   UUS U(       + SUUUU	US9nU R                  R                  U5        UnMa     [!        UUS
   US   S9U l        US:X  a  [%        US
   U	5      U l        O[        R(                  " US
   USS9U l        [        R*                  " 5       U l        [        R                  " US
   USSS9U l        SU l        g )Nr@   r   r2   r3   r}   r7   r~   rA   )r)   r:   r<   rC   rD   r>   r=   r?   r   r   )r   r   r   rF   rJ   F)rK   rL   r-   rM   rN   rO   rP   r   r   rU   r   r   rR   rS   r   rT   r   condition_encoderr   rW   rV   rX   rY   rZ   r[   )r\   r)   r*   rz   r,   r-   r.   r/   r{   r?   r   r^   r_   r   r   rb   r   re   s                    r   rL   MaskConditionDecoder.__init__  s    	 0yyr"
 r*'0I'=4 (*2.  !1:g1EI91"5)'	
 '+84F+G&H#4Q7 ). 9A"08;N#&8"9A"==N#0014/+$(!//$-#1+(1H NN!!(+"0+ !:0 "6%a(%b)"
 	!!,-?-BM!RD!#;Ma;P]lrv!wD			"4Q"7qRST&+#r   zimager   r   rg   c                 z   UnU R                  U5      n[        [        U R                  R	                  5       5      5      R
                  n[        R                  " 5       (       Ga!  U R                  (       Ga  U R                  U R                  XT5      nUR                  U5      nUb(  Ub%  SU-
  U-  nU R                  U R                  UU5      nU R                   Ht  n	Ub\  UbY  W[        [        UR                  5      5         n
[         R"                  R%                  X5R                  SS SS9nX[-  U
SU-
  -  -   nU R                  XU5      nMv     Ub/  Ub,  XS-  W[        [        UR                  5      5         SU-
  -  -   nOU R                  XT5      nUR                  U5      nUb  Ub  SU-
  U-  nU R                  Xs5      nU R                   Hj  n	Ub\  UbY  W[        [        UR                  5      5         n
[         R"                  R%                  X5R                  SS SS9nX[-  U
SU-
  -  -   nU	" XT5      nMl     Ub/  Ub,  XS-  W[        [        UR                  5      5         SU-
  -  -   nUc  U R'                  U5      nOU R'                  XT5      nU R)                  U5      nU R+                  U5      nU$ )z7The forward method of the `MaskConditionDecoder` class.Nr2   nearest)sizemode)rO   r   r   r   r   r   r   ri   r[   rj   rU   r   r   rs   r   r   rM   
functionalinterpolaterW   rY   rZ   )r\   r   r   r   r   r#   r   masked_imageim_xr   sample_mask_s               r   rk   MaskConditionDecoder.forward  s    f%T$..";";"=>?EE  ""t'B'B'B66t~~v]FYY}-F  T%5 !DE188**  !NN$)9"3uV\\':#;<GMM55dbcARYb5cE#^gU.CCF::8][ +  T%5c%2E.F)G1t8)TT ^^F:FYY}-F  T%5 !DE1--lA !NN$)9"3uV\\':#;<GMM55dbcARYb5cE#^gU.CCF!&8 +  T%5c%2E.F)G1t8)TT  ''/F''>Fv&v&r   )	r   rY   rO   rW   rZ   r[   r-   rU   r   )r   r   r   rn   r	   rp   rq   r~   )NNNr   rv   s   @r   r   r     s    0 *?.3 !! O,O, O, c3h	O,
 "#s(OO, O, O, O, O, O,h )-'+04?<<? %? u||$	?
  -? 
? ?r   r   c                   j  ^  \ rS rSrSr    SS\S\S\S\S\S\4U 4S	 jjjr	S
\
R                  S\
R                  4S jrS
\
R                  S\
R                  4S jrS\
R                  S\\
R                  \
R                  \4   4S jrS\
R                  S\\S4   S\
R                  4S jrSrU =r$ )VectorQuantizeri9  z
Improved version over VectorQuantizer, can be used as a drop-in replacement. Mostly avoids costly matrix
multiplications and allows for post-hoc remapping of indices.
n_evq_embed_dimbetaunknown_indexsane_index_shapelegacyc           	      *  > [         TU ]  5         Xl        X l        X0l        Xpl        [        R                  " U R                  U R                  5      U l        U R                  R                  R                  R                  SU R                  -  SU R                  -  5        X@l        U R                  b  U R                  S[        R                  " [         R"                  " U R                  5      5      5        U   U R$                  R&                  S   U l        XPl        U R*                  S:X  a%  U R(                  U l        U R(                  S-   U l        [-        SU R                   SU R(                   S	U R*                   S
35        OXl        X`l        g )Ng            ?usedr   extrar2   z
Remapping z indices to z indices. Using z for unknown indices.)rK   rL   r   r   r   r   rM   	Embedding	embeddingweightdatauniform_remapregister_bufferr   tensornploadr   r   re_embedr   printr   )	r\   r   r   r   r   r   r   r   re   s	           r   rL   VectorQuantizer.__init__B  s2    	(	dhh0A0AB""++D488OS488^L
::!  bggdjj6I)JK IIOOA.DM!.!!W,%)]]" $ 1TXXJl4==/ B++,,AC
  M 0r   indsrg   c                    UR                   n[        U5      S:  d   eUR                  US   S5      nU R                  R	                  U5      nUS S 2S S 2S 4   US   :H  R                  5       nUR                  S5      nUR                  S5      S:  nU R                  S:X  aF  [        R                  " SU R                  XV   R                   S9R	                  UR                  S9XV'   OU R                  XV'   UR                  U5      $ )	Nr2   r   r@   )NN.r	   random)r   )device)r   rS   reshaper   r   longargmaxsumr   r   randintr   r   )r\   r   ishaper   matchnewunknowns          r   remap_to_usedVectorQuantizer.remap_to_usedg  s    6{Q||F1Ir*yy||D!aDj!T/%::@@Bll2))A,") ==DMM@R@RSVV^a^h^hViCL--CL{{6""r   c                    UR                   n[        U5      S:  d   eUR                  US   S5      nU R                  R	                  U5      nU R
                  U R                  R                   S   :  a  SXU R                  R                   S   :  '   [        R                  " US S S 24   UR                   S   S/-  S S 24   SU5      nUR                  U5      $ )Nr2   r   r@   )r   rS   r   r   r   r   r   gather)r\   r   r   r   backs        r   unmap_to_allVectorQuantizer.unmap_to_allu  s    6{Q||F1Ir*yy||D!==499??1--/0D++,||DqM$**Q-1#*=q*@A1dK||F##r   r   c                    UR                  SSSS5      R                  5       nUR                  SU R                  5      n[        R
                  " [        R                  " X R                  R                  5      SS9nU R                  U5      R                  UR                  5      nS nS nU R                  (       db  U R                  [        R                  " UR                  5       U-
  S-  5      -  [        R                  " XAR                  5       -
  S-  5      -   nOa[        R                  " UR                  5       U-
  S-  5      U R                  [        R                  " XAR                  5       -
  S-  5      -  -   nXU-
  R                  5       -   nUR                  SSSS5      R                  5       nU R                  bB  UR                  UR                  S   S5      nU R!                  U5      nUR                  SS5      nU R"                  (       a:  UR                  UR                  S   UR                  S   UR                  S   5      nXGXVU44$ )Nr   r	   r   r2   r@   dim)permute
contiguousviewr   r   argmincdistr   r   r   r   r   meandetachr   r   r   r   )r\   r   z_flattenedmin_encoding_indicesz_q
perplexitymin_encodingslosss           r   rk   VectorQuantizer.forward  s   IIaAq!,,.ffR!2!23  %||EKK^^EZEZ,[abcnn1277@
 {{99uzz3::<!+;*ABBUZZQTW_W_WaQafgPgEhhD::szz|a/A56UZZQTW_W_WaQafgPgEh9hhD q 0 0 22 kk!Q1%002::!#7#?#?
B#O #'#5#56J#K #7#?#?A#F   #7#?#?		!ciiXYl\_\e\efg\h#i :6JKKKr   indicesr   .c                    U R                   b7  UR                  US   S5      nU R                  U5      nUR                  S5      nU R                  U5      nUb3  UR	                  U5      nUR                  SSSS5      R                  5       nU$ )Nr   r@   r   r2   r	   )r   r   r   r   r  r   r   )r\   r  r   r  s       r   get_codebook_entry"VectorQuantizer.get_codebook_entry  s    ::!ooeAh3G''0Goob)G !NN73((5/C++aAq)446C
r   )	r   r   r   r   r   r   r   r   r   )Nr   FT)r   r   r   r   r   rr   floatrs   rt   rL   r   
LongTensorr   r   r   r   rk   r  r   ru   rv   s   @r   r   r   9  s     %!&#1#1 #1 	#1 #1 #1 #1 #1J#%"2"2 #u7G7G #$!1!1 $e6F6F $ L  L%ellE0Q*R  LD%*:*: 5c? W\WcWc  r   r   c                       \ rS rSrSS\R
                  S\4S jjrSS\\R                     S\R
                  4S jjr
SS	S S\R
                  4S
 jjr/ SQ4S\R
                  S\\S4   S\R
                  4S jjrS\R
                  4S jrSrg)DiagonalGaussianDistributioni  r   deterministicc                    Xl         [        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                  (       aU  [        R                  " U R                  U R                   R                  U R                   R                  S9=U l	        U l        g g )Nr	   r2   r   g      >g      4@      ?)r   r   )r   r   chunkr  logvarclampr  expstdvar
zeros_liker   r   )r\   r   r  s      r   rL   %DiagonalGaussianDistribution.__init__  s    $!&Z!B	4;kk$++ud;*99S4;;./99T[[)"'"2"2		$//"8"8@U@U# DHtx r   N	generatorrg   c                     [        U R                  R                  UU R                  R                  U R                  R
                  S9nU R                  U R                  U-  -   nU$ )N)r   r   r   )r   r  r   r   r   r   r  )r\   r   r#   r   s       r   r#   #DiagonalGaussianDistribution.sample  sR    IIOO??))//''	
 II6))r   otherc                    U R                   (       a  [        R                  " S/5      $ UcV  S[        R                  " [        R                  " U R
                  S5      U R                  -   S-
  U R                  -
  / SQS9-  $ S[        R                  " [        R                  " U R
                  UR
                  -
  S5      UR                  -  U R                  UR                  -  -   S-
  U R                  -
  UR                  -   / SQS9-  $ )N        r  r	   r   r2   r	   r   r   )r  r   r   r   powr  r  r  )r\   r#  s     r   klDiagonalGaussianDistribution.kl  s    <<&&}UYYIIdii+dhh6<t{{J!  
 UYYIIdii%**4a8599Dhh*+ kk" ll	#
 "  r   r&  r#   dims.c                 J   U R                   (       a  [        R                  " S/5      $ [        R                  " S[        R
                  -  5      nS[        R                  " X0R                  -   [        R                  " XR                  -
  S5      U R                  -  -   US9-  $ )Nr%  g       @r  r	   r   )r  r   r   r   logpir   r  r'  r  r  )r\   r#   r*  logtwopis       r   nll DiagonalGaussianDistribution.nll  sw    <<&&66#+&UYY{{"UYYv		/A1%E%PP
 
 	
r   c                     U R                   $ r   )r  r\   s    r   r   !DiagonalGaussianDistribution.mode  s    yyr   )r  r  r  r   r  r  )Fr   )r   r   r   r   r   r   rt   rL   r   	Generatorr#   r(  r   rr   r/  r   r   r   r   r   r  r    s    
5<< 
 
	 9 	U\\ 	6 %,, & AJ 
%,, 
eCHo 
ell 
ell r   r  c                       \ rS rSrS\R
                  4S jrS
S\\R                     S\R
                  4S jjr	S\R
                  4S jr
S	rg)IdentityDistributioni  r   c                     Xl         g r   r   )r\   r   s     r   rL   IdentityDistribution.__init__  s    $r   Nr   rg   c                     U R                   $ r   r8  )r\   r   s     r   r#   IdentityDistribution.sample      r   c                     U R                   $ r   r8  r2  s    r   r   IdentityDistribution.mode  r<  r   r8  r   )r   r   r   r   r   r   rL   r   r4  r#   r   r   r   r   r   r6  r6    sB    %5<< % 9 U\\ ell r   r6  c            
          ^  \ rS rSrSrS\S\S\\S4   S\\S4   S\4
U 4S	 jjrS
\	R                  S\	R                  4S jrSrU =r$ )EncoderTinyi  aZ  
The `EncoderTiny` layer is a simpler version of the `Encoder` layer.

Args:
    in_channels (`int`):
        The number of input channels.
    out_channels (`int`):
        The number of output channels.
    num_blocks (`Tuple[int, ...]`):
        Each value of the tuple represents a Conv2d layer followed by `value` number of `AutoencoderTinyBlock`'s to
        use.
    block_out_channels (`Tuple[int, ...]`):
        The number of output channels for each block.
    act_fn (`str`):
        The activation function to use. See `~diffusers.models.activations.get_activation` for available options.
r)   r*   
num_blocks.r,   r/   c                   > [         TU ]  5         / n[        U5       H  u  pxXG   n	US:X  a&  UR                  [        R
                  " XSSS95        O(UR                  [        R
                  " U	U	SSSSS95        [        U5       H  n
UR                  [        XU5      5        M      M     UR                  [        R
                  " US   USSS95        [        R                  " U6 U l	        SU l
        g )	Nr   r   r2   r4   r6   r	   F)r4   r6   r5   biasr@   )rK   rL   rR   rT   rM   rN   r   r   r   r   r[   )r\   r)   r*   rA  r,   r/   r   r_   	num_blockrG   _re   s              r   rL   EncoderTiny.__init__  s     	%j1LA-0LAvbiiqZ[\]II$$$% ! "	 9%2<vVW &# 2( 	bii 22 6RS]^_`mmV,&+#r   r   rg   c                     [         R                  " 5       (       a/  U R                  (       a  U R                  U R                  U5      nU$ U R	                  UR                  S5      R                  S5      5      nU$ )z.The forward method of the `EncoderTiny` class.r2   r	   )r   ri   r[   rj   r   adddivr   s     r   rk   EncoderTiny.forward,  s[      ""t'B'B11$++qAA  AEE!HLLO,Ar   r[   r   r   r   r   r   r   rr   r   rs   rL   r   r   rk   r   ru   rv   s   @r   r@  r@    sn    "",", ", #s(O	",
 "#s(O", ",H	 	%,, 	 	r   r@  c                      ^  \ rS rSrSrS\S\S\\S4   S\\S4   S\S	\S
\4U 4S jjrS\	R                  S\	R                  4S jrSrU =r$ )DecoderTinyi8  a  
The `DecoderTiny` layer is a simpler version of the `Decoder` layer.

Args:
    in_channels (`int`):
        The number of input channels.
    out_channels (`int`):
        The number of output channels.
    num_blocks (`Tuple[int, ...]`):
        Each value of the tuple represents a Conv2d layer followed by `value` number of `AutoencoderTinyBlock`'s to
        use.
    block_out_channels (`Tuple[int, ...]`):
        The number of output channels for each block.
    upsampling_scaling_factor (`int`):
        The scaling factor to use for upsampling.
    act_fn (`str`):
        The activation function to use. See `~diffusers.models.activations.get_activation` for available options.
r)   r*   rA  .r,   upsampling_scaling_factorr/   upsample_fnc                   > [         TU ]  5         [        R                  " XS   SSS9[	        U5      /n[        U5       H  u  pU	[        U5      S-
  :H  nXI   n[        U
5       H  nUR                  [        XU5      5        M      U(       d#  UR                  [        R                  " XWS95        U(       d  UOUnUR                  [        R                  " UUSSUS95        M     [        R                  " U6 U l        SU l        g )Nr   r   r2   rC  )scale_factorr   )r4   r6   rD  F)rK   rL   rM   rN   r
   rR   rS   r   rT   r   Upsampler   r   r[   )r\   r)   r*   rA  r,   rP  r/   rQ  r   r_   rE  rb   rG   rF  conv_out_channelre   s                  r   rL   DecoderTiny.__init__L  s     	 IIka#8aQRS6"

 &j1LA3z?Q#67N-0L9%2<vVW & "bkk7Pcd3A||MM		 $ !' 2* mmV,&+#r   r   rg   c                 ,   [         R                  " US-  5      S-  n[         R                  " 5       (       a.  U R                  (       a  U R	                  U R
                  U5      nOU R                  U5      nUR                  S5      R                  S5      $ )z.The forward method of the `DecoderTiny` class.r   r	   r2   )r   tanhri   r[   rj   r   mulsubr   s     r   rk   DecoderTiny.forwardu  sm     JJq1u!  ""t'B'B11$++qAAAA uuQx||Ar   rL  rM  rv   s   @r   rO  rO  8  s    &',', ', #s(O	',
 "#s(O', $'', ', ',R %,,  r   rO  )%dataclassesr   typingr   r   numpyr   r   torch.nnrM   utilsr   utils.torch_utilsr   activationsr
   attention_processorr   unets.unet_2d_blocksr   r   r   r   r   r!   Moduler'   rx   r   r   r   r   objectr  r6  r@  rO  r   r   r   <module>rg     s   " "     - ( -  	J 	 	 
4J 
4 
4vbii vrFbii FRryy 82299 2jg299 gTwbii wt56 5p6 ?")) ?DH")) Hr   