
    +h                     ^   S SK JrJrJr  S SKrS SKJr  SSKJrJ	r	  SSK
Jr  SSKJr  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   " S S\R6                  5      r " S S\R6                  5      r " S S\R6                  5      r " S S\R6                  5      r " S S\R6                  5      r  " S S\R6                  5      r! " S S\R6                  5      r" " S S\R6                  5      r# " S S \R6                  5      r$ " S! S"\R6                  5      r% " S# S$\\\5      r&g)%    )OptionalTupleUnionN   )ConfigMixinregister_to_config)FromOriginalModelMixin)apply_forward_hook   )get_activation))PixArtAlphaCombinedTimestepSizeEmbeddings)AutoencoderKLOutput)
ModelMixin)RMSNorm   )DecoderOutputDiagonalGaussianDistributionc                      ^  \ rS rSr      SS\S\S\\\\\\4   4   S\\\\\\4   4   S\\\\\\4   4   S\S\S	\4U 4S
 jjjr	S\
R                  S\
R                  4S jrSrU =r$ )LTXVideoCausalConv3d    in_channelsout_channelskernel_sizestridedilationgroupspadding_mode	is_causalc	                   > [         TU ]  5         Xl        X l        Xl        [        U[        5      (       a  UOX3U4U l        [        U[        5      (       a  UOUSS4n[        U[        5      (       a  UOXDU4nU R                  S   S-  n	U R                  S   S-  n
SX4n[        R                  " UUU R                  UUUUUS9U l
        g )Nr   r   r   )r   r   r   paddingr   )super__init__r   r   r   
isinstancetupler   nnConv3dconv)selfr   r   r   r   r   r   r   r   
height_pad	width_padr    	__class__s               j/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/models/autoencoders/autoencoder_kl_ltx.pyr"   LTXVideoCausalConv3d.__init__!   s     	&("*4[%*H*H;{itNu)(E::81a@P%fe4466:R%%a(A-
$$Q'1,	j,II%	
	    hidden_statesreturnc                    U R                   S   nU R                  (       aB  US S 2S S 2S S2S S 2S S 24   R                  SSUS-
  SS45      n[        R                  " X1/SS9nOsUS S 2S S 2S S2S S 2S S 24   R                  SSUS-
  S-  SS45      nUS S 2S S 2SS 2S S 2S S 24   R                  SSUS-
  S-  SS45      n[        R                  " X1U/SS9nU R                  U5      nU$ )Nr   r   r   dim)r   r   repeattorchconcatenater'   )r(   r/   time_kernel_sizepad_left	pad_rights        r,   forwardLTXVideoCausalConv3d.forwardD   s
   ++A.>>$Q2A2q!^4;;QCSVWCWYZ\]<^_H!--x.GQOM$Q2A2q!^4;;QDTWXDX]^C^`acd<efH%aBCAo6==q!FVYZFZ_`E`bcef>ghI!--x	.RXYZM		-0r.   )r'   r   r   r   r   )r   r   r   r   zerosT)__name__
__module____qualname____firstlineno__intr   r   strboolr"   r6   Tensorr;   __static_attributes____classcell__r+   s   @r,   r   r       s    
 9:3456#!
!
 !
 3c3m 445	!

 c5c3//0!
 U3S=112!
 !
 !
 !
 !
FU\\ ell  r.   r   c                      ^  \ rS rSrSr        SS\S\\   S\S\S\S	\	S
\S\S\SS4U 4S jjjr
 SS\R                  S\\R                     S\\R                     S\R                  4S jjrSrU =r$ )LTXVideoResnetBlock3dS   a  
A 3D ResNet block used in the LTXVideo model.

Args:
    in_channels (`int`):
        Number of input channels.
    out_channels (`int`, *optional*):
        Number of output channels. If None, defaults to `in_channels`.
    dropout (`float`, defaults to `0.0`):
        Dropout rate.
    eps (`float`, defaults to `1e-6`):
        Epsilon value for normalization layers.
    elementwise_affine (`bool`, defaults to `False`):
        Whether to enable elementwise affinity in the normalization layers.
    non_linearity (`str`, defaults to `"swish"`):
        Activation function to use.
    conv_shortcut (bool, defaults to `False`):
        Whether or not to use a convolution shortcut.
Nr   r   dropoutepselementwise_affinenon_linearityr   inject_noisetimestep_conditioningr0   c
                   > [         T
U ]  5         U=(       d    Un[        U5      U l        [	        USUS9U l        [        XSUS9U l        [	        USUS9U l        [        R                  " U5      U l        [        X"SUS9U l        S U l        S U l        X:w  a,  [        R                  " XSSS9U l        [        XSSUS9U l        S U l        S U l        U(       ab  [        R$                  " [&        R(                  " USS5      5      U l        [        R$                  " [&        R(                  " USS5      5      U l        S U l        U	(       a7  [        R$                  " [&        R,                  " S	U5      US
-  -  5      U l        g g )N:0yE>rM   rN   r   )r   r   r   r   T)rM   rN   biasr   r   r   r   r   r            ?)r!   r"   r   nonlinearityr   norm1r   conv1norm2r%   DropoutrL   conv2norm3conv_shortcut	LayerNormper_channel_scale1per_channel_scale2	Parameterr6   r=   scale_shift_tablerandn)r(   r   r   rL   rM   rN   rO   r   rP   rQ   r+   s             r,   r"   LTXVideoResnetBlock3d.__init__h   sQ    	#2{*=9[dGYZ
)#AYb

 \tHZ[
zz'*)$QZc

 
!&ktZ^_DJ!5'PQZ[gp"D #'"&&(ll5;;{Aq3Q&RD#&(ll5;;{Aq3Q&RD#!% %'\\%++a2MP[]`P`2`%aD" !r.   inputstemb	generatorc                    UnU R                  UR                  SS5      5      R                  SS5      nU R                  b?  UR                  SS5      U R                  S   -   nUR	                  SS9u  pVpxUSU-   -  U-   nU R                  U5      nU R                  U5      nU R                  bV  UR                  SS  n	[        R                  " XUR                  UR                  S9S    n
XJU R                  -  S S S 2S S4   -   nU R                  UR                  SS5      5      R                  SS5      nU R                  b  USW-   -  W-   nU R                  U5      nU R                  U5      nU R                  U5      nU R                   bV  UR                  SS  n	[        R                  " XUR                  UR                  S9S    n
XJU R                   -  S S S 2S S4   -   nU R"                  b1  U R#                  UR                  SS5      5      R                  SS5      nU R$                  b  U R%                  U5      nXA-   nU$ )	Nr   r4   )rW   r4   N.NNNr2   )rj   devicedtype.)rZ   movedimre   	unflattenunbindrY   r[   rb   shaper6   rf   rn   ro   r\   rL   r^   rc   r_   r`   )r(   rh   ri   rj   r/   shift_1scale_1shift_2scale_2spatial_shapespatial_noises              r,   r;   LTXVideoResnetBlock3d.forward   sO    

=#8#8B#?@HHQO!!->>!W-0F0FGb0ccD151C.Gg)Q[9GCM))-8

=1"".)//4M!KK=;O;OWdWjWjM *T=T=T-TVZ\]_cehVh,iiM

=#8#8B#?@HHQO!!-)Q[9GCM))-8]3

=1"".)//4M!KK=;O;OWdWjWjM *T=T=T-TVZ\]_cehVh,iiM::!ZZq" 56>>r1EF)''/F%.r.   )r[   r^   r`   rL   rY   rZ   r\   r_   rb   rc   re   )N        ư>FswishTFFNN)r>   r?   r@   rA   __doc__rB   r   floatrD   rC   r"   r6   rE   	Generatorr;   rF   rG   rH   s   @r,   rJ   rJ   S   s    . '+#($"&+-b-b sm-b 	-b
 -b !-b -b -b -b  $-b 
-b -b` qu-ll-*25<<*@-T\]b]l]lTm-	- -r.   rJ   c                      ^  \ rS rSr   SS\S\S\\\\\\4   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$ )LTXVideoDownsampler3d   r   r   r   r   r   r0   Nc           	         > [         TU ]  5         [        U[        5      (       a  UOX3U4U l        XS   -  US   -  US   -  U-  U l        X R                  S   U R                  S   -  U R                  S   -  -  n[        UUSSUUS9U l        g Nr   r   r   r   )r   r   r   r   r   r   )r!   r"   r#   r$   r   
group_sizer   r'   )r(   r   r   r   r   r   r+   s         r,   r"   LTXVideoDownsampler3d.__init__   s     	 *65 9 9fPV?W&2VAY>J|[#AQ(G$++VW.(XY(#%%
	r.   r/   c           
         [         R                  " US S 2S S 2S U R                  S   S-
  24   U/SS9nUR                  SSU R                  S   45      R                  SSU R                  S   45      R                  SSU R                  S   45      nUR	                  SSSSS	SSS
5      R                  SS5      nUR                  SSU R                  45      nUR                  SS9nU R                  U5      nUR                  SSU R                  S   45      R                  SSU R                  S   45      R                  SSU R                  S   45      nUR	                  SSSSS	SSS
5      R                  SS5      nX-   nU$ )Nr   r   r   r2   rW   r4   r            )	r6   catr   rq   permuteflattenr   meanr'   )r(   r/   residuals      r,   r;   LTXVideoDownsampler3d.forward   s   		=A7KQ!9K7K1K#Lm"\bcd ##ADKKN';<Yq2t{{1~./Yq2t{{1~./ 	
 ##Aq!Q1a;CCAqI%%a"doo)>?==Q='		-0##ADKKN';<Yq2t{{1~./Yq2t{{1~./ 	
 &--aAq!Q1EMMaQRS%0r.   )r'   r   r   )r   Tr=   r>   r?   r@   rA   rB   r   r   rD   rC   r"   r6   rE   r;   rF   rG   rH   s   @r,   r   r      s    
 45#

 
 c5c3//0	

 
 
 

 
0U\\ ell  r.   r   c                      ^  \ rS rSr     SS\S\\\\\\4   4   S\S\S\S\SS	4U 4S
 jjjr	S\
R                  S\
R                  4S jrSrU =r$ )LTXVideoUpsampler3d   r   r   r   r   upscale_factorr   r0   Nc           	         > [         TU ]  5         [        U[        5      (       a  UOX"U4U l        X@l        XPl        XS   -  US   -  US   -  U-  n[        UUSSUUS9U l        g r   )	r!   r"   r#   r$   r   r   r   r   r'   )	r(   r   r   r   r   r   r   r   r+   s	           r,   r"   LTXVideoUpsampler3d.__init__   sw     	 *65 9 9fPV?W ,#Qi/&);fQiGNZ(#%%
	r.   r/   c           
         UR                   u  p#pEnU R                  (       a  UR                  USU R                  S   U R                  S   U R                  S   XEU5      nUR	                  SSSSSSSS	5      R                  SS5      R                  S	S5      R                  SS5      nU R                  S   U R                  S   -  U R                  S   -  U R                  -  nUR                  SUSSS5      nUS S 2S S 2U R                  S   S-
  S 24   nU R                  U5      nUR                  USU R                  S   U R                  S   U R                  S   XEU5      nUR	                  SSSSSSSS	5      R                  SS5      R                  S	S5      R                  SS5      nUS S 2S S 2U R                  S   S-
  S 24   nU R                  (       a  UW-   nU$ )
Nr4   r   r   r   r   r   r   r   rW   )	rs   r   reshaper   r   r   r   r5   r'   )	r(   r/   
batch_sizenum_channels
num_framesheightwidthr   repeatss	            r,   r;   LTXVideoUpsampler3d.forward  s   >K>Q>Q;
*e==$,,BAAAPZdiH  ''1aAq!Q?GG1MUUVWYZ[ccdeghiH{{1~A6QGDL_L__Gq'1a;H1dkk!nq&8&: :;H		-0%--DKKNDKKNDKKNJ`e
 &--aAq!Q1EMMaQRS[[\]_`aiijkmno%aDKKNQ,>,@&@A==)H4Mr.   )r'   r   r   r   )r   TFr   r=   r   rH   s   @r,   r   r      s     45#

 c5c3//0
 	

 
 
 
 

 
4U\\ ell  r.   r   c                      ^  \ rS rSrSrSr       SS\S\\   S\S\S\S	\	S
\
S\
4U 4S jjjr  SS\R                  S\\R                     S\\R                     S\R                  4S jjrSrU =r$ )LTXVideoDownBlock3Di,    
Down block used in the LTXVideo model.

Args:
    in_channels (`int`):
        Number of input channels.
    out_channels (`int`, *optional*):
        Number of output channels. If None, defaults to `in_channels`.
    num_layers (`int`, defaults to `1`):
        Number of resnet layers.
    dropout (`float`, defaults to `0.0`):
        Dropout rate.
    resnet_eps (`float`, defaults to `1e-6`):
        Epsilon value for normalization layers.
    resnet_act_fn (`str`, defaults to `"swish"`):
        Activation function to use.
    spatio_temporal_scale (`bool`, defaults to `True`):
        Whether or not to use a downsampling layer. If not used, output dimension would be same as input dimension.
        Whether or not to downsample across temporal dimension.
    is_causal (`bool`, defaults to `True`):
        Whether this layer behaves causally (future frames depend only on past frames) or not.
Tr   r   
num_layersrL   
resnet_epsresnet_act_fnspatio_temporal_scaler   c	                   > [         TU ]  5         U=(       d    Un/ n	[        U5       H   n
U	R                  [	        UUUUUUS95        M"     [
        R                  " U	5      U l        S U l        U(       a'  [
        R                  " [        UUSSUS9/5      U l        S U l
        X:w  a  [	        UUUUUUS9U l
        SU l        g )Nr   r   rL   rM   rO   r   r   r   r   r   rV   F)r!   r"   rangeappendrJ   r%   
ModuleListresnetsdownsamplersr   conv_outgradient_checkpointing)r(   r   r   r   rL   r   r   r   r   r   _r+   s              r,   r"   LTXVideoDownBlock3D.__init__F  s     	#2{z"ANN% +!,#""/'	 # }}W-   "($/%0$%("+
!D &1')+#DM ',#r.   r/   ri   rj   r0   c                 `   [        U R                  5       HM  u  pE[        R                  " 5       (       a%  U R                  (       a  U R                  XQX#5      nMD  U" XU5      nMO     U R                  b  U R                   H  nU" U5      nM     U R                  b  U R                  XU5      nU$ z-Forward method of the `LTXDownBlock3D` class.)	enumerater   r6   is_grad_enabledr   _gradient_checkpointing_funcr   r   r(   r/   ri   rj   iresnetdownsamplers          r,   r;   LTXVideoDownBlock3D.forward~  s     #4<<0IA$$&&4+F+F $ A A&Y] i &}I F	 1 (#00 +M :  1 ==$ MM-yIMr.   )r   r   r   r   )Nr   r{   r|   r}   TTr~   r>   r?   r@   rA   r    _supports_gradient_checkpointingrB   r   r   rC   rD   r"   r6   rE   r   r;   rF   rG   rH   s   @r,   r   r   ,  s    . (,$
 '+ $&*6,6, sm6, 	6,
 6, 6, 6,  $6, 6, 6,v (,/3	|| u||$ EOO,	
 
 r.   r   c                      ^  \ rS rSrSrSr        SS\S\\   S\S\S\S	\	S
\
S\
S\	4U 4S jjjr  SS\R                  S\\R                     S\\R                     S\R                  4S jjrSrU =r$ )LTXVideo095DownBlock3Di  r   Tr   r   r   rL   r   r   r   r   downsample_typec
                   > [         TU ]  5         U=(       d    Un/ n
[        U5       H   nU
R                  [	        UUUUUUS95        M"     [
        R                  " U
5      U l        S U l        U(       a  [
        R                  " 5       U l        U	S:X  a'  U R                  R                  [        UUSSUS95        OU	S:X  a%  U R                  R                  [        XSUS95        OUU	S	:X  a%  U R                  R                  [        XS
US95        O*U	S:X  a$  U R                  R                  [        XSUS95        SU l        g )Nr   r'   r   r   rV   spatial)r   r   r   )r   r   r   r   temporal)r   r   r   spatiotemporalF)r!   r"   r   r   rJ   r%   r   r   r   r   r   r   )r(   r   r   r   rL   r   r   r   r   r   r   r   r+   s               r,   r"   LTXVideo095DownBlock3D.__init__  sJ    	#2{z"ANN% +!,#""/'	 # }}W-   "D&(!!((($/%0$%("+ !I-!!(()$/S\hq
 !J.!!(()$/S\hq
 !$44!!(()$/S\hq ',#r.   r/   ri   rj   r0   c                 "   [        U R                  5       HM  u  pE[        R                  " 5       (       a%  U R                  (       a  U R                  XQX#5      nMD  U" XU5      nMO     U R                  b  U R                   H  nU" U5      nM     U$ r   )r   r   r6   r   r   r   r   r   s          r,   r;   LTXVideo095DownBlock3D.forward  s     #4<<0IA$$&&4+F+F $ A A&Y] i &}I F	 1 (#00 +M :  1 r.   )r   r   r   )Nr   r{   r|   r}   TTr'   r~   r   rH   s   @r,   r   r     s    . (,$
 '+ $&*%?,?, sm?, 	?,
 ?, ?, ?,  $?, ?, ?, ?,H (,/3	|| u||$ EOO,	
 
 r.   r   c                      ^  \ rS rSrSrSr       SS\S\S\S\S\S	\	S
\	S\	SS4U 4S jjjr
  SS\R                  S\\R                     S\\R                     S\R                  4S jjrSrU =r$ )LTXVideoMidBlock3di  a7  
A middle block used in the LTXVideo model.

Args:
    in_channels (`int`):
        Number of input channels.
    num_layers (`int`, defaults to `1`):
        Number of resnet layers.
    dropout (`float`, defaults to `0.0`):
        Dropout rate.
    resnet_eps (`float`, defaults to `1e-6`):
        Epsilon value for normalization layers.
    resnet_act_fn (`str`, defaults to `"swish"`):
        Activation function to use.
    is_causal (`bool`, defaults to `True`):
        Whether this layer behaves causally (future frames depend only on past frames) or not.
Tr   r   rL   r   r   r   rP   rQ   r0   Nc	                   > [         TU ]  5         S U l        U(       a  [        US-  S5      U l        / n	[	        U5       H"  n
U	R                  [        UUUUUUUUS95        M$     [        R                  " U	5      U l	        SU l
        g )NrW   r   r   r   rL   rM   rO   r   rP   rQ   F)r!   r"   time_embedderr   r   r   rJ   r%   r   r   r   )r(   r   r   rL   r   r   r   rP   rQ   r   r   r+   s              r,   r"   LTXVideoMidBlock3d.__init__  s     	! !J;YZ?\]!^Dz"ANN% +!,#""/'!-*?	 # }}W-&+#r.   r/   ri   rj   c                    U R                   b^  U R                  UR                  5       SSUR                  S5      UR                  S9nUR	                  UR                  S5      SSSS5      n[        U R                  5       HM  u  pE[        R                  " 5       (       a%  U R                  (       a  U R                  XQX#5      nMD  U" XU5      nMO     U$ )z,Forward method of the `LTXMidBlock3D` class.Nr   timestep
resolutionaspect_ratior   hidden_dtyper4   r   )r   r   sizero   viewr   r   r6   r   r   r   )r(   r/   ri   rj   r   r   s         r,   r;   LTXVideoMidBlock3d.forward?  s     )%%!(--a0*00 & D 99]//2B1a@D"4<<0IA$$&&4+F+F $ A A&Y] i &}I F	 1 r.   )r   r   r   )r   r{   r|   r}   TFFr~   )r>   r?   r@   rA   r   r   rB   r   rC   rD   r"   r6   rE   r   r   r;   rF   rG   rH   s   @r,   r   r     s    $ (,$
  $"&+!,!, !, 	!,
 !, !, !, !,  $!, 
!, !,L (,/3	|| u||$ EOO,	
 
 r.   r   c                     ^  \ rS rSrSrSr           SS\S\\   S\S\S\S	\	S
\
S\
S\
S\
S\
S\4U 4S jjjr  SS\R                  S\\R                     S\\R                     S\R                  4S jjrSrU =r$ )LTXVideoUpBlock3diZ  a  
Up block used in the LTXVideo model.

Args:
    in_channels (`int`):
        Number of input channels.
    out_channels (`int`, *optional*):
        Number of output channels. If None, defaults to `in_channels`.
    num_layers (`int`, defaults to `1`):
        Number of resnet layers.
    dropout (`float`, defaults to `0.0`):
        Dropout rate.
    resnet_eps (`float`, defaults to `1e-6`):
        Epsilon value for normalization layers.
    resnet_act_fn (`str`, defaults to `"swish"`):
        Activation function to use.
    spatio_temporal_scale (`bool`, defaults to `True`):
        Whether or not to use a downsampling layer. If not used, output dimension would be same as input dimension.
        Whether or not to downsample across temporal dimension.
    is_causal (`bool`, defaults to `True`):
        Whether this layer behaves causally (future frames depend only on past frames) or not.
Tr   r   r   rL   r   r   r   r   rP   rQ   upsample_residualr   c                   > [         TU ]  5         U=(       d    UnS U l        U
(       a  [        US-  S5      U l        S U l        X:w  a  [        UUUUUUU	U
S9U l        S U l        U(       a)  [        R                  " [        X,-  SUUUS9/5      U l        / n[        U5       H"  nUR                  [        UUUUUUU	U
S95        M$     [        R                  " U5      U l        SU l        g )NrW   r   r   r   )r   r   r   r   F)r!   r"   r   r   conv_inrJ   
upsamplersr%   r   r   r   r   r   r   )r(   r   r   r   rL   r   r   r   r   rP   rQ   r   r   r   r   r+   s                  r,   r"   LTXVideoUpBlock3d.__init__t  s    	#2{! !J;YZ?\]!^D&0')+#)&;	DL   mm'$5("+!2'5
DO z"ANN% ,!-#""/'!-*?	 # }}W-&+#r.   r/   ri   rj   r0   c                 6   U R                   b  U R                  XU5      nU R                  b^  U R                  UR                  5       S S UR                  S5      UR                  S9nUR                  UR                  S5      SSSS5      nU R                  b  U R                   H  nU" U5      nM     [        U R                  5       HM  u  pV[        R                  " 5       (       a%  U R                  (       a  U R                  XaX#5      nMD  U" XU5      nMO     U$ )Nr   r   r4   r   )r   r   r   r   ro   r   r   r   r   r6   r   r   r   )r(   r/   ri   rj   	upsamplerr   r   s          r,   r;   LTXVideoUpBlock3d.forward  s     <<# LLiHM)%%!(--a0*00 & D 99]//2B1a@D??&!__	 )- 8 - #4<<0IA$$&&4+F+F $ A A&Y] i &}I F	 1 r.   )r   r   r   r   r   )Nr   r{   r|   r}   TTFFFr   r~   r   rH   s   @r,   r   r   Z  s   . (,$
 '+ $&*"&+"'B,B, smB, 	B,
 B, B, B,  $B, B, B,  $B,  B, B, B,N (,/3	|| u||$ EOO,	
 
 r.   r   c                      ^  \ rS rSrSr           SS\S\S\\S4   S\\S4   S\\S4   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$ )LTXVideoEncoder3di  aP  
The `LTXVideoEncoder3d` layer of a variational autoencoder that encodes input video samples to its latent
representation.

Args:
    in_channels (`int`, defaults to 3):
        Number of input channels.
    out_channels (`int`, defaults to 128):
        Number of latent channels.
    block_out_channels (`Tuple[int, ...]`, defaults to `(128, 256, 512, 512)`):
        The number of output channels for each block.
    spatio_temporal_scaling (`Tuple[bool, ...], defaults to `(True, True, True, False)`:
        Whether a block should contain spatio-temporal downscaling layers or not.
    layers_per_block (`Tuple[int, ...]`, defaults to `(4, 3, 3, 3, 4)`):
        The number of layers per block.
    patch_size (`int`, defaults to `4`):
        The size of spatial patches.
    patch_size_t (`int`, defaults to `1`):
        The size of temporal patches.
    resnet_norm_eps (`float`, defaults to `1e-6`):
        Epsilon value for ResNet normalization layers.
    is_causal (`bool`, defaults to `True`):
        Whether this layer behaves causally (future frames depend only on past frames) or not.
r   r   block_out_channels.down_block_typesspatio_temporal_scalinglayers_per_blockr   
patch_sizepatch_size_tresnet_norm_epsr   c                   > [         TU ]  5         Xl        Xl        XS-  -  U l        US   n[        U R                  USSUS9U l        US   S:H  n[        U5      U(       a  SOS-
  n[        R                  " / 5      U l
        [        U5       H  nUnU(       d  US-   U:  a  X?S-      OX?   nOX?S-      nXO   S:X  a  [        UUXo   U
X_   US	9nO.XO   S:X  a  [        UUXo   U
X_   UX   S
9nO[        SXO    35      eU R                  R                  U5        M     [!        UUS   U
US9U l        [%        USSS9U l        [        R(                  " 5       U l        [        XS-   SSUS9U l        SU l        g )Nr   r   r   r   rV   r4   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   r   zUnknown down block type: )r   r   r   r   rS   FrT   )r!   r"   r   r   r   r   r   lenr%   r   down_blocksr   r   r   
ValueErrorr   r   	mid_blockr   norm_outSiLUconv_actr   r   )r(   r   r   r   r   r   r   r   r   r   r   r   output_channel
is_ltx_095num_block_out_channelsr   input_channel
down_blockr+   s                     r,   r"   LTXVideoEncoder3d.__init__  s   & 	$(&Q6+A.+(('
 &b)-EE
!$%7!8AQR!S==,-.A*M>?!eF\>\!3E!:btbw!3E!:"&;;0 -!//2.*A*D'
 "$(@@3 -!//2.*A*D'$3$6
 !#<=M=P<Q!RSS##J/; /@ ,&'+&	
  $5Q	,&A5EST]^js
 ',#r.   r/   r0   c           
      n   U R                   nU R                  nUR                  u  pEpgnXc-  n	Xr-  n
X-  nUR                  XEXXX5      nUR	                  SSSSSSSS5      R                  SS5      nU R                  U5      n[        R                  " 5       (       aR  U R                  (       aA  U R                   H  nU R                  X5      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R                  SS	5      5      R                  S	S5      nU R                  U5      nU R!                  U5      nUS
S
2S	S
24   nUR#                  SUR%                  S5      S-
  SSS5      n[        R&                  " X/SS9nU$ )z4The forward method of the `LTXVideoEncoder3d` class.r   r   r   r   r   r   rW   r   r4   Nr2   )r   r   rs   r   r   r   r   r6   r   r   r   r   r   r   rp   r   r   r5   r   r   )r(   r/   pp_tr   r   r   r   r   post_patch_num_framespost_patch_heightpost_patch_widthr   last_channels                 r,   r;   LTXVideoEncoder3d.forwardI  s    OO>K>Q>Q;
*e * 1"K :%--&;BSXh
 &--aAq!Q1EMMaQRS]3  ""t'B'B"..
 $ A A* \ / !==dnnm\M"..
 *= 9 / !NN=9Mm&;&;Ar&BCKKBPQRm4m4$QV,#**1m.@.@.Ca.GAqQ		="?QGr.   )
r   r   r   r   r   r   r   r   r   r   )r      r        r  r   r   r   r   TTTFrW   r   r   r   rW   r'   r'   r'   r'   rW   r   r|   T)r>   r?   r@   rA   r   rB   r   rC   rD   r   r"   r6   rE   r;   rF   rG   rH   s   @r,   r   r     s    6 .B-
 5N,;+K!%#U,U, U, "#s(O	U,
  S/U, "'tSy!1U,  S/U, sCxU, U, U,  !U," #U, U,n%U\\ %ell % %r.   r   c                     ^  \ rS rSrSr             SS\S\S\\S4   S\\S4   S\\S4   S	\S
\S\S\S\\S4   S\S\\S4   S\\S4   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$ )LTXVideoDecoder3diq  a  
The `LTXVideoDecoder3d` layer of a variational autoencoder that decodes its latent representation into an output
sample.

Args:
    in_channels (`int`, defaults to 128):
        Number of latent channels.
    out_channels (`int`, defaults to 3):
        Number of output channels.
    block_out_channels (`Tuple[int, ...]`, defaults to `(128, 256, 512, 512)`):
        The number of output channels for each block.
    spatio_temporal_scaling (`Tuple[bool, ...], defaults to `(True, True, True, False)`:
        Whether a block should contain spatio-temporal upscaling layers or not.
    layers_per_block (`Tuple[int, ...]`, defaults to `(4, 3, 3, 3, 4)`):
        The number of layers per block.
    patch_size (`int`, defaults to `4`):
        The size of spatial patches.
    patch_size_t (`int`, defaults to `1`):
        The size of temporal patches.
    resnet_norm_eps (`float`, defaults to `1e-6`):
        Epsilon value for ResNet normalization layers.
    is_causal (`bool`, defaults to `False`):
        Whether this layer behaves causally (future frames depend only on past frames) or not.
    timestep_conditioning (`bool`, defaults to `False`):
        Whether to condition the model on timesteps.
r   r   r   .r   r   r   r   r   r   rP   rQ   r   upsample_factorr0   Nc                   > [         TU ]  5         X`l        Xpl        X&S-  -  U l        [        [        U5      5      n[        [        U5      5      n[        [        U5      5      n[        [        U
5      5      n
[        [        U5      5      n[        [        U5      5      nUS   n[        XSSU	S9U l        [        UUS   UU	U
S   US9U l
        [        U5      n[        R                  " / 5      U l        [        U5       HW  nXU   -  nUU   UU   -  n[!        UUUUS-      UUU   U	U
US-      UUU   UU   S9
nU R                  R#                  U5        MY     [%        USS	S
9U l        [        R(                  " 5       U l        [        XR                  SSU	S9U l        S U l        S U l        S U l        U(       a  [        R4                  " [6        R8                  " S[6        R:                  S95      U l        [=        US-  S5      U l        [        R4                  " [6        R>                  " SU5      US-  -  5      U l        S	U l         g )Nr   r   r   r   rV   )r   r   r   r   rP   rQ   )
r   r   r   r   r   r   rP   rQ   r   r   rS   FrT   g     @@)ro   rX   )!r!   r"   r   r   r   r$   reversedr   r   r   r   r   r%   r   	up_blocksr   r   r   r   r   r   r   r   r   re   timestep_scale_multiplierrd   r6   tensorfloat32r   rf   r   )r(   r   r   r   r   r   r   r   r   r   rP   rQ   r   r  r   r   r   r   up_blockr+   s                      r,   r"   LTXVideoDecoder3d.__init__  sE     	$((q=8"8,>#?@"'1H(I"J *:!;<Xl34!(+<"=> 9:+A.+#aXYen
 ,&'*&%a"7
 "%%7!8r*-.A*a.@@M/2oa6HHN()++AE2*&=a&@#)!a%0&;"3A"6.q1H NN!!(+# /(  $5Q	,&5F5FTU^_kt

 "!%)-& -/\\%,,vUZUbUb:c-dD*!J>\]K]_`!aD%'\\%++a2PSacfSf2f%gD"&+#r.   r/   ri   c           
         U R                  U5      nU R                  b  X R                  -  n[        R                  " 5       (       aS  U R                  (       aB  U R                  U R                  X5      nU R                   H  nU R                  X1U5      nM     O,U R                  X5      nU R                   H  nU" X5      nM     U R                  UR                  SS5      5      R                  SS5      nU R                  b  U R                  UR                  5       S S UR                  S5      UR                  S9nUR                  UR                  S5      SSSS5      R                  SS5      nX R                   S   -   nUR#                  SS9u  pEUSU-   -  U-   nU R%                  U5      nU R'                  U5      nU R(                  nU R*                  nUR,                  u  ppnUR/                  USXvXjX5      nUR1                  SSSS	S
SSS5      R                  S
S5      R                  SS5      R                  S	S5      nU$ )Nr   r4   r   r   )r   r4   rl   r2   r   r   r   rW   r   r   )r   r  r6   r   r   r   r   r  r   rp   r   r   r   ro   r   rq   re   rr   r   r   r   r   rs   r   r   )r(   r/   ri   r  shiftscaler   r   r   r   r   r   r   s                r,   r;   LTXVideoDecoder3d.forward  s&   ]3))5888D  ""t'B'B ==dnnmbM NN $ A A([_ ` + !NN=?M NN ( = + m&;&;Ar&BCKKBPQR)%%!(--a0*00 & D 99]//2B1a@JJ1gVD001LMMD;;1;-LE)QY7%?Mm4m4OO>K>Q>Q;
*e%--j"caU[c%--aAq!Q1EMMaQRS[[\]_`aiijkmnor.   )r   r   r   r   r   r   r   r   r   re   r   r  r  )r  r   r  r	  r
  rW   r   r|   FFFFFFr  r   r   r   r   N)r>   r?   r@   rA   r   rB   r   rD   r   r"   r6   rE   r   r;   rF   rG   rH   s   @r,   r  r  q  s=   : .B4M,;!%)E&+.J,8Q,Q, Q, "#s(O	Q,
 "'tSy!1Q,  S/Q, Q, Q, Q, Q, D#I&Q,  $Q, !s+Q, tSy)Q, 
Q, Q,f*U\\ *%,,9O *[`[g[g * *r.   r  c            2         ^  \ rS rSrSrSr\                       S@S\S\S\S\\S	4   S
\\	S	4   S\\S	4   S\\S	4   S\\S	4   S\\
S	4   S\\
S	4   S\\
S	4   S\\	S	4   S\\
S	4   S\\S	4   S\
S\S\S\S\S\
S\
S\S\SS40U 4S jjj5       r      SAS\\   S \\   S!\\   S"\\   S#\\   S$\\   SS4S% jjrSBS& jrSBS' jrSBS( jrS)\R&                  S\R&                  4S* jr\ SCS)\R&                  S+\
S\\\\   4   4S, jj5       r SDS-\R&                  S.\\R&                     S+\
S\\\R&                  4   4S/ jjr\ SDS-\R&                  S.\\R&                     S+\
S\\\R&                  4   4S0 jj5       rS1\R&                  S2\R&                  S3\S\R&                  4S4 jrS1\R&                  S2\R&                  S3\S\R&                  4S5 jrS1\R&                  S2\R&                  S3\S\R&                  4S6 jrS)\R&                  S\R&                  4S7 jr  SCS-\R&                  S.\\R&                     S+\
S\\\R&                  4   4S8 jjr!S)\R&                  S\4S9 jr" SCS-\R&                  S.\\R&                     S+\
S\\\R&                  4   4S: jjr#    SES;\R&                  S.\\R&                     S<\
S+\
S=\\RH                     S\\R&                  \R&                  4   4S> jjr%S?r&U =r'$ )FAutoencoderKLLTXVideoi  a  
A VAE model with KL loss for encoding images into latents and decoding latent representations into images. Used in
[LTX](https://huggingface.co/Lightricks/LTX-Video).

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

Args:
    in_channels (`int`, defaults to `3`):
        Number of input channels.
    out_channels (`int`, defaults to `3`):
        Number of output channels.
    latent_channels (`int`, defaults to `128`):
        Number of latent channels.
    block_out_channels (`Tuple[int, ...]`, defaults to `(128, 256, 512, 512)`):
        The number of output channels for each block.
    spatio_temporal_scaling (`Tuple[bool, ...], defaults to `(True, True, True, False)`:
        Whether a block should contain spatio-temporal downscaling or not.
    layers_per_block (`Tuple[int, ...]`, defaults to `(4, 3, 3, 3, 4)`):
        The number of layers per block.
    patch_size (`int`, defaults to `4`):
        The size of spatial patches.
    patch_size_t (`int`, defaults to `1`):
        The size of temporal patches.
    resnet_norm_eps (`float`, defaults to `1e-6`):
        Epsilon value for ResNet normalization layers.
    scaling_factor (`float`, *optional*, defaults to `1.0`):
        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.
    encoder_causal (`bool`, defaults to `True`):
        Whether the encoder should behave causally (future frames depend only on past frames) or not.
    decoder_causal (`bool`, defaults to `False`):
        Whether the decoder should behave causally (future frames depend only on past frames) or not.
TNr   r   latent_channelsr   .r   decoder_block_out_channelsr   decoder_layers_per_blockr   decoder_spatio_temporal_scalingdecoder_inject_noiser   r   r  rQ   r   r   r   scaling_factorencoder_causaldecoder_causalspatial_compression_ratiotemporal_compression_ratior0   c                 6  > [         TU ]  5         [        UUUUU	UUUUUUS9U l        [	        UUUU
UUUUUUUUUS9U l        [        R                  " U4SS9n[        R                  " U4SS9nU R                  SUSS9  U R                  SUSS9  Uc  US	[        U	5      -  -  OUU l        Uc  US	[        U	5      -  -  OUU l        SU l        SU l        SU l        SU l        S
U l        S	U l        SU l        SU l        S
U l        SU l        SU l        SU l        g )N)r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   rQ   rP   r   r  F)requires_gradlatents_meanT)
persistentlatents_stdr      r  i     )r!   r"   r   encoderr  decoderr6   r=   onesregister_buffersumr(  r)  use_slicing
use_tilinguse_framewise_encodinguse_framewise_decodingnum_sample_frames_batch_sizenum_latent_frames_batch_sizetile_sample_min_heighttile_sample_min_widthtile_sample_min_num_framestile_sample_stride_heighttile_sample_stride_widthtile_sample_stride_num_frames)r(   r   r   r   r   r   r!  r   r"  r   r#  r$  r   r   r  rQ   r   r   r   r%  r&  r'  r(  r)  r,  r.  r+   s                             r,   r"   AutoencoderKLLTXVideo.__init__7  s   @ 	(#(1-$;-+!%+$
 )'%9$C5!%+$"7-/+
  {{O#5UKjj/!35I^\dK]KDI )0 c"9:::* 	& *1 1$; <<<+ 	' !
   ',#&+#
 -/),-) '*#%("*,' *-&(+%-.*r.   r<  r=  r>  r?  r@  rA  c                 J   SU l         U=(       d    U R                  U l        U=(       d    U R                  U l        U=(       d    U R                  U l        U=(       d    U R                  U l        U=(       d    U R
                  U l        U=(       d    U R                  U l        g)a  
Enable tiled VAE decoding. When this option is enabled, the VAE will split the input tensor into tiles to
compute decoding and encoding in several steps. This is useful for saving a large amount of memory and to allow
processing larger images.

Args:
    tile_sample_min_height (`int`, *optional*):
        The minimum height required for a sample to be separated into tiles across the height dimension.
    tile_sample_min_width (`int`, *optional*):
        The minimum width required for a sample to be separated into tiles across the width dimension.
    tile_sample_stride_height (`int`, *optional*):
        The minimum amount of overlap between two consecutive vertical tiles. This is to ensure that there are
        no tiling artifacts produced across the height dimension.
    tile_sample_stride_width (`int`, *optional*):
        The stride between two consecutive horizontal tiles. This is to ensure that there are no tiling
        artifacts produced across the width dimension.
TN)r7  r<  r=  r>  r?  r@  rA  )r(   r<  r=  r>  r?  r@  rA  s          r,   enable_tiling#AutoencoderKLLTXVideo.enable_tiling  s}    4 &<&[@[@[#%:%Xd>X>X"*D*gHgHg')B)ddFdFd&(@(aDDaDa%-J-pdNpNp*r.   c                     SU l         g)z
Disable tiled VAE decoding. If `enable_tiling` was previously enabled, this method will go back to computing
decoding in one step.
FN)r7  r(   s    r,   disable_tiling$AutoencoderKLLTXVideo.disable_tiling  s    
  r.   c                     SU l         g)z
Enable sliced VAE decoding. When this option is enabled, the VAE will split the input tensor in slices to
compute decoding in several steps. This is useful to save some memory and allow larger batch sizes.
TNr6  rG  s    r,   enable_slicing$AutoencoderKLLTXVideo.enable_slicing  s    
  r.   c                     SU l         g)z
Disable sliced VAE decoding. If `enable_slicing` was previously enabled, this method will go back to computing
decoding in one step.
FNrK  rG  s    r,   disable_slicing%AutoencoderKLLTXVideo.disable_slicing  s    
 !r.   xc                 *   UR                   u  p#pEnU R                  (       a   X@R                  :  a  U R                  U5      $ U R                  (       a/  X`R
                  :  d  XPR                  :  a  U R                  U5      $ U R                  U5      nU$ r  )	rs   r9  r>  _temporal_tiled_encoder7  r=  r<  tiled_encoder1  )r(   rQ  r   r   r   r   r   encs           r,   _encodeAutoencoderKLLTXVideo._encode  sw    >?gg;
*e&&:8W8W+W..q11??(B(B BfOjOjFj$$Q''ll1o
r.   return_dictc                 >   U R                   (       aY  UR                  S   S:  aF  UR                  S5       Vs/ s H  o0R                  U5      PM     nn[        R
                  " U5      nOU R                  U5      n[        U5      nU(       d  U4$ [        US9$ s  snf )a  
Encode a batch of images into latents.

Args:
    x (`torch.Tensor`): Input batch of images.
    return_dict (`bool`, *optional*, defaults to `True`):
        Whether to return a [`~models.autoencoder_kl.AutoencoderKLOutput`] instead of a plain tuple.

Returns:
        The latent representations of the encoded videos. If `return_dict` is True, a
        [`~models.autoencoder_kl.AutoencoderKLOutput`] is returned, otherwise a plain `tuple` is returned.
r   r   )latent_dist)r6  rs   splitrV  r6   r   r   r   )r(   rQ  rX  x_sliceencoded_slicesh	posteriors          r,   encodeAutoencoderKLLTXVideo.encode  s      
QCD771:N:ll73:NN		.)AQA03	<"y99 Os   Bzri   c                    UR                   u  pEpgnU R                  U R                  -  n	U R                  U R                  -  n
U R                  U R
                  -  nU R                  (       a  Xk:  a  U R                  XUS9$ U R                  (       a  X:  d  Xy:  a  U R                  XUS9$ U R                  X5      nU(       d  U4$ [        US9$ )NrX  sample)rs   r<  r(  r=  r>  r)  r9  _temporal_tiled_decoder7  tiled_decoder2  r   )r(   rb  ri   rX  r   r   r   r   r   tile_latent_min_heighttile_latent_min_widthtile_latent_min_num_framesdecs                r,   _decodeAutoencoderKLLTXVideo._decode  s     ?@gg;
*e!%!<!<@^@^!^ $ : :d>\>\ \%)%D%DHgHg%g"&&:+R..qK.PP?? =A`$$Q+$FFll1#6MC((r.   c                    U R                   (       a  UR                  S   S:  a  UbP  UR                  S5      UR                  S5      4 VVs/ s H   u  pEU R                  XE5      R                  PM"     nnnO9UR                  S5       Vs/ s H  o@R                  U5      R                  PM     nn[
        R                  " U5      nOU R                  X5      R                  nU(       d  U4$ [        US9$ s  snnf s  snf )a  
Decode a batch of images.

Args:
    z (`torch.Tensor`): Input batch of latent vectors.
    return_dict (`bool`, *optional*, defaults to `True`):
        Whether to return a [`~models.vae.DecoderOutput`] instead of a plain tuple.

Returns:
    [`~models.vae.DecoderOutput`] or `tuple`:
        If return_dict is True, a [`~models.vae.DecoderOutput`] is returned, otherwise a plain `tuple` is
        returned.
r   r   re  )r6  rs   r[  rm  rf  r6   r   r   )r(   rb  ri   rX  z_slicet_slicedecoded_slicesdecodeds           r,   decodeAutoencoderKLLTXVideo.decode  s    " 
QSTSZSZ[\S]_c_i_ijk_lRm"Rm>NgDLL299Rm  " OPggVWj!Yj7,,w"7">">j!Yii/Gll1+22G:G,," "Zs   'C6$C<abblend_extentc           	         [        UR                  S   UR                  S   U5      n[        U5       HI  nUS S 2S S 2S S 2U* U-   S S 24   SXC-  -
  -  US S 2S S 2S S 2US S 24   XC-  -  -   US S 2S S 2S S 2US S 24'   MK     U$ )Nr   r   minrs   r   )r(   rv  rw  rx  ys        r,   blend_vAutoencoderKLLTXVideo.blend_v9  s    1771:qwwqz<@|$A Aq<-!*;Q!>?1qGWCWX[\]^`acdfgij]j[k \  AaAq!m % r.   c                    [        UR                  S   UR                  S   U5      n[        U5       HI  nUS S 2S S 2S S 2S S 2U* U-   4   SXC-  -
  -  US S 2S S 2S S 2S S 2U4   XC-  -  -   US S 2S S 2S S 2S S 2U4'   MK     U$ )NrW   r   rz  r(   rv  rw  rx  rQ  s        r,   blend_hAutoencoderKLLTXVideo.blend_hA  s    1771:qwwqz<@|$A Aq!l]Q->!>?1qGWCWX[\]^`acdfgij]j[k \  AaAq!m % r.   c           	         [        UR                  S   UR                  S   U5      n[        U5       HI  nUS S 2S S 2U* U-   S S 2S S 24   SXC-  -
  -  US S 2S S 2US S 2S S 24   XC-  -  -   US S 2S S 2US S 2S S 24'   MK     U$ )Nr   rz  r  s        r,   blend_tAutoencoderKLLTXVideo.blend_tI  s    1772;\B|$A A}q'8!Q!>?1qGWCWX[\]^`acdfgij]j[k \  AaAq!m % r.   c                 *   UR                   u  p#pEnXPR                  -  nX`R                  -  nU R                  U R                  -  n	U R                  U R                  -  n
U R                  U R                  -  nU R
                  U R                  -  nX-
  nX-
  n/ n[        SXPR                  5       H  n/ n[        SX`R
                  5       HQ  nU R                  USS2SS2SS2UUU R                  -   2UUU R                  -   24   5      nUR                  U5        MS     UR                  U5        M     / n[        U5       H  u  nn/ n[        U5       Hj  u  nnUS:  a  U R                  UUS-
     U   UU5      nUS:  a  U R                  UUS-
     UU5      nUR                  USS2SS2SS2SU2SU24   5        Ml     UR                  [        R                  " USS95        M     [        R                  " USS9SS2SS2SS2SU2SU24   nU$ )zEncode a batch of images using a tiled encoder.

Args:
    x (`torch.Tensor`): Input batch of videos.

Returns:
    `torch.Tensor`:
        The latent representation of the encoded videos.
r   Nr   rW   r2   r   )rs   r(  r<  r=  r?  r@  r   r1  r   r   r}  r  r6   r   )r(   rQ  r   r   r   r   r   latent_heightlatent_widthri  rj  tile_latent_stride_heighttile_latent_stride_widthblend_heightblend_widthrowsr   rowjtimeresult_rows
result_rowtilerU  s                           r,   rT  "AutoencoderKLLTXVideo.tiled_encodeQ  s$    ?@gg;
*e"@"@@ > >>!%!<!<@^@^!^ $ : :d>\>\ \$($B$BdFdFd$d!#'#@#@DDbDb#b -I+F q&"@"@AAC1e%B%BC||aAq1t'B'B#BBADLfLfHfDffg 

4  D KK B oFAsJ$S>4 q5<<QUAlKDq5<<AE
D+FD!!$q!Q0J1J0JLeMeLe'e"fg * uyy;< & ii+Aq!^m^]l],RS
r.   c                 z   UR                   u  pEpgnXpR                  -  n	XR                  -  n
U R                  U R                  -  nU R                  U R                  -  nU R                  U R                  -  nU R
                  U R                  -  nU R                  U R                  -
  nU R                  U R
                  -
  n/ n[        SX}5       Hd  n/ n[        SX5       H>  nU R                  USS2SS2SS2UUU-   2UUU-   24   U5      nUR                  U5        M@     UR                  U5        Mf     / n[        U5       H  u  nn/ n[        U5       H~  u  nnUS:  a  U R                  UUS-
     U   UU5      nUS:  a  U R                  UUS-
     UU5      nUR                  USS2SS2SS2SU R                  2SU R
                  24   5        M     UR                  [        R                  " USS95        M     [        R                  " USS9SS2SS2SS2SU	2SU
24   nU(       d  U4$ [        US9$ )a  
Decode a batch of images using a tiled decoder.

Args:
    z (`torch.Tensor`): Input batch of latent vectors.
    return_dict (`bool`, *optional*, defaults to `True`):
        Whether or not to return a [`~models.vae.DecoderOutput`] instead of a plain tuple.

Returns:
    [`~models.vae.DecoderOutput`] or `tuple`:
        If return_dict is True, a [`~models.vae.DecoderOutput`] is returned, otherwise a plain `tuple` is
        returned.
r   Nr   rW   r2   r   re  )rs   r(  r<  r=  r?  r@  r   r2  r   r   r}  r  r6   r   r   )r(   rb  ri   rX  r   r   r   r   r   sample_heightsample_widthri  rj  r  r  r  r  r  r   r  r  r  r  r  r  rl  s                             r,   rh  "AutoencoderKLLTXVideo.tiled_decode  sB   " ?@gg;
*e!?!??===!%!<!<@^@^!^ $ : :d>\>\ \$($B$BdFdFd$d!#'#@#@DDbDb#b 22T5S5SS0043P3PP q&<AC1e>||AaAq17M3M/MqSTWlSlOl&l$most

4  ? KK = oFAsJ$S>4 q5<<QUAlKDq5<<AE
D+FD!!$q!Q0P$2P2P0PRqTXTqTqRq'q"rs * uyy;< & ii+Aq!^m^]l],RS6MC((r.   c           
      x   UR                   u  p#pEnUS-
  U R                  -  S-   nU R                  U R                  -  nU R                  U R                  -  n	X-
  n
/ n[	        SX@R                  5       H  nUS S 2S S 2XU R                  -   S-   2S S 2S S 24   nU R
                  (       a0  XPR                  :  d  X`R                  :  a  U R                  U5      nOU R                  U5      nUS:  a  US S 2S S 2SS 2S S 2S S 24   nUR                  U5        M     / n[        U5       Hm  u  pUS:  a<  U R                  XS-
     X5      nUR                  US S 2S S 2S U	2S S 2S S 24   5        MG  UR                  US S 2S S 2S U	S-   2S S 2S S 24   5        Mo     [        R                  " USS9S S 2S S 2S U24   nU$ )Nr   r   r   r2   )rs   r)  r>  rA  r   r7  r<  r=  rT  r1  r   r   r  r6   r   )r(   rQ  r   r   r   r   r   latent_num_framesrk  tile_latent_stride_num_framesblend_num_framesr  r   r  r  rU  s                   r,   rS  ,AutoencoderKLLTXVideo._temporal_tiled_encode  s   >?gg;
*e'!^0O0OORSS%)%D%DHgHg%g"(,(J(JdNmNm(m%5Uq*&H&HIAQ14#B#BBQFF1LMDF-H-H$HETnTnLn((.||D)1uAq!"aN+JJt J 
 ~GA1u||CAJG!!$q!-K.K-KQPQ'Q"RS!!$q!-P/Lq/P-PRSUV'V"WX & ii
*1a1C2C1C+CD
r.   c           
      Z   UR                   u  pEpgnUS-
  U R                  -  S-   n	U R                  U R                  -  n
U R                  U R                  -  nU R
                  U R                  -  nU R                  U R                  -  nU R
                  U R                  -
  n/ n[        SXm5       H  nUS S 2S S 2UUU-   S-   2S S 2S S 24   nU R                  (       aB  UR                   S   U:  d  UR                   S   U
:  a  U R                  UUSS9R                  nOU R                  UU5      nUS:  a  US S 2S S 2S S2S S 2S S 24   nUR                  U5        M     / n[        U5       H  u  nnUS:  aJ  U R                  UUS-
     UU5      nUS S 2S S 2S U R                  2S S 2S S 24   nUR                  U5        MV  UR                  US S 2S S 2S U R                  S-   2S S 2S S 24   5        M     [        R                   " USS9S S 2S S 2S U	24   nU(       d  U4$ [#        US	9$ )
Nr   r   r4   rm   Trd  r   r2   re  )rs   r)  r<  r(  r=  r>  rA  r   r7  rh  rf  r2  r   r   r  r6   r   r   )r(   rb  ri   rX  r   r   r   r   r   num_sample_framesri  rj  rk  r  r  r  r   r  rs  r  rl  s                        r,   rg  ,AutoencoderKLLTXVideo._temporal_tiled_decode  s5    ?@gg;
*e'!^t/N/NNQRR!%!<!<@^@^!^ $ : :d>\>\ \%)%D%DHgHg%g"(,(J(JdNmNm(m%::T=_=__q*DAQ1q#==AA1aGHDDJJrN5J$JdjjY[n_uNu++D$D+IPP,,tT21u!!QQ/2JJw E 
 ~GAt1u||CAJ6FGAq"FD$F$F"F1LM!!$'!!$q!-Ut/Q/QTU/U-UWXZ['["\] & ii
*1a1C2C1C+CD6MC((r.   rf  sample_posteriorrj   c                     UnU R                  U5      R                  nU(       a  UR                  US9nOUR                  5       nU R	                  X5      n	U(       d  U	R                  4$ U	$ )N)rj   )r`  rZ  rf  modert  )
r(   rf  ri   r  rX  rj   rQ  r_  rb  rl  s
             r,   r;   AutoencoderKLLTXVideo.forward  s`     KKN..	  9 5A Akk!"JJ= 
r.   )r2  r1  r;  r:  r(  r)  r<  r>  r=  r?  rA  r@  r9  r8  r6  r7  )r   r   r  r  r  r  r
  r
  r	  r	  )FFFFFr  r  r  FrW   r   r|   g      ?TFNN)NNNNNN)r0   N)T)NT)NFTN)(r>   r?   r@   rA   r   r   r   rB   r   rC   rD   r   r"   r   rD  rH  rL  rO  r6   rE   rV  r
   r   r   r   r`  r   rm  rt  r}  r  r  rT  rh  rS  rg  r   r;   rF   rG   rH   s   @r,   r  r    s   %N (,$ ".B-
 7K,;4C4M<U1T+K.J+7&+!% ##$)-*.;j/j/ j/ 	j/
 "#s(Oj/  S/j/ %*#s(Oj/  S/j/ #(S/j/ "'tSy!1j/  */tSy)9!j/" $D#I.#j/$ sCx%j/& !s+'j/( sCx)j/*  $+j/, -j/. /j/0 1j/2 3j/4 5j/6 7j/8 $'9j/: %(;j/< 
=j/ j/\ 15/34859489= q ( q  (} q %-SM	 q
 $,E? q #+5/ q (0 q 
 qD  ! %,,  37::,0:	"E*F$GG	H: :6 Y]))%-ell%;)QU)	}ell*	+)* X\--%-ell%;-QU-	}ell*	+- -@ %,, c ell  %,, c ell  %,, c ell 1ell 1u|| 1h RV:):)%-ell%;:)JN:)	}ell*	+:)x 9L > RV$)$)%-ell%;$)JN$)	}ell*	+$)R (,!& /3 u||$ 	
  EOO, 
u||U\\)	* r.   r  )'typingr   r   r   r6   torch.nnr%   configuration_utilsr   r   loadersr	   utils.accelerate_utilsr
   activationsr   
embeddingsr   modeling_outputsr   modeling_utilsr   normalizationr   vaer   r   Moduler   rJ   r   r   r   r   r   r   r   r  r   r.   r,   <module>r     s     * )   B - 8 ( B 2 ' # <0299 0fqBII qh.BII .b1")) 1hg")) gTmRYY mbP Pf{		 {|V		 VrY		 YxHJ5K Hr.   