
    +h%.                         S SK JrJrJ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  SSKJr  SSKJr  \
" \5      r SS\R.                  S\\R0                     S\4S jjr " S S\5      rg)    )ListOptionalUnionN   )PipelineImageInput)AutoencoderKLLTXVideo)
get_logger)randn_tensor)VideoProcessor   )DiffusionPipeline   )LTXLatentUpsamplerModel)LTXPipelineOutputencoder_output	generatorsample_modec                    [        U S5      (       a!  US:X  a  U R                  R                  U5      $ [        U S5      (       a   US:X  a  U R                  R                  5       $ [        U S5      (       a  U R                  $ [        S5      e)Nlatent_distsampleargmaxlatentsz3Could not access latents of provided encoder_output)hasattrr   r   moder   AttributeError)r   r   r   s      n/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/pipelines/ltx/pipeline_ltx_latent_upsample.pyretrieve_latentsr   !   s}     ~}--+2I))00;;		/	/K84K))..00		+	+%%%RSS    c                   f  ^  \ rS rSrSrS\S\SS4U 4S jjr      S%S\\	R                     S	\S
\\	R                     S\\	R                     S\\	R                     S\\	R                     S\	R                  4S jjrS&S\	R                  S\	R                  S\4S jjr\ S&S\	R                  S\	R                  S\	R                  S\S\	R                  4
S jj5       r\ S&S\	R                  S\	R                  S\	R                  S\S\	R                  4
S jj5       rS rS rS rS rS r\	R4                  " 5                 S'S\\\      S\S\S\\	R                     S\\\\   4   S\\\\\   4      S \S\\\	R                  \\	R                     4      S!\\   S"\4S# jj5       r S$r!U =r"$ )(LTXLatentUpsamplePipeline.    vaelatent_upsamplerreturnNc                   > [         TU ]  5         U R                  XS9  [        U SS 5      b  U R                  R
                  OSU l        [        U SS 5      b  U R                  R                  OSU l        [        U R                  S9U l
        g )N)r#   r$   r#          )vae_scale_factor)super__init__register_modulesgetattrr#   spatial_compression_ratiovae_spatial_compression_ratiotemporal_compression_ratiovae_temporal_compression_ratior   video_processor)selfr#   r$   	__class__s      r   r+   "LTXLatentUpsamplePipeline.__init__1   s    
 	#I 3:$t2L2XDHH..^` 	* 4;43M3YDHH//_` 	+  .t?a?abr   video
batch_sizedtypedevicer   r   c           
         Ub  UR                  XCS9$ UR                  X@R                  R                  S9n[        U[        5      (       a|  [        U5      U:w  a  [        S[        U5       SU S35      e[        U5       Vs/ s H;  n[        U R                  R                  X   R                  S5      5      XW   5      PM=     nnODU V	s/ s H7  n	[        U R                  R                  U	R                  S5      5      U5      PM9     nn	[        R                  " USS9R                  U5      nU R                  XR                  R                  U R                  R                  5      nU$ s  snf s  sn	f )Nr9   r8   z/You have passed a list of generators of length z+, but requested an effective batch size of z@. Make sure the batch size matches the length of the generators.r   dim)tor#   r8   
isinstancelistlen
ValueErrorranger   encode	unsqueezetorchcat_normalize_latentslatents_meanlatents_std)
r3   r6   r7   r8   r9   r   r   iinit_latentsvids
             r   prepare_latents)LTXLatentUpsamplePipeline.prepare_latentsB   sV    ::V:99hhnn=i&&9~+ Ec)nEU V  *|+km  affp`q`q[\ 1C1CA1F!GV`q  L gllfk_b,TXX__S]]1=M-NPYZfkLlyy1588?..|XX=R=RTXT\T\ThThi ms   AE+>E0reference_latentsfactorc                 p   UR                  5       n[        UR                  S5      5       Hp  n[        UR                  S5      5       HO  n[        R                  " X%U4   SS9u  px[        R                  " XEU4   SS9u  pXEU4   U
-
  U	-  U-  U-   XEU4'   MQ     Mr     [        R
                  " XU5      nU$ )a  
Applies Adaptive Instance Normalization (AdaIN) to a latent tensor based on statistics from a reference latent
tensor.

Args:
    latent (`torch.Tensor`):
        Input latents to normalize
    reference_latents (`torch.Tensor`):
        The reference latents providing style statistics.
    factor (`float`):
        Blending factor between original and transformed latent. Range: -10.0 to 10.0, Default: 1.0

Returns:
    torch.Tensor: The transformed latent tensor
r   r   Nr<   )clonerC   sizerF   std_meanlerp)r3   r   rP   rQ   resultrK   cr_sdr_meani_sdi_means              r   adain_filter_latent-LTXLatentUpsamplePipeline.adain_filter_latent`   s      w||A'A7<<?+$~~.?1.E4P$~~fTlE!'1!6$ >$FO!t	 , ( GV4r   rI   rJ   scaling_factorc                     UR                  SSSSS5      R                  U R                  U R                  5      nUR                  SSSSS5      R                  U R                  U R                  5      nX-
  U-  U-  n U $ Nr   viewr>   r9   r8   r   rI   rJ   r_   s       r   rH   ,LTXLatentUpsamplePipeline._normalize_latents|   su     $((B1a8;;GNNGMMZ!&&q"aA699'..'--X)^;kIr   c                     UR                  SSSSS5      R                  U R                  U R                  5      nUR                  SSSSS5      R                  U R                  U R                  5      nX-  U-  U-   n U $ ra   rc   re   s       r   _denormalize_latents.LTXLatentUpsamplePipeline._denormalize_latents   su     $((B1a8;;GNNGMMZ!&&q"aA699'..'--X'.8<Gr   c                 8    U R                   R                  5         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.
N)r#   enable_slicingr3   s    r   enable_vae_slicing,LTXLatentUpsamplePipeline.enable_vae_slicing       
 	!r   c                 8    U R                   R                  5         g)z
Disable sliced VAE decoding. If `enable_vae_slicing` was previously enabled, this method will go back to
computing decoding in one step.
N)r#   disable_slicingrl   s    r   disable_vae_slicing-LTXLatentUpsamplePipeline.disable_vae_slicing   s    
 	  "r   c                 8    U R                   R                  5         g)z
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.
N)r#   enable_tilingrl   s    r   enable_vae_tiling+LTXLatentUpsamplePipeline.enable_vae_tiling   s     	 r   c                 8    U R                   R                  5         g)z
Disable tiled VAE decoding. If `enable_vae_tiling` was previously enabled, this method will go back to
computing decoding in one step.
N)r#   disable_tilingrl   s    r   disable_vae_tiling,LTXLatentUpsamplePipeline.disable_vae_tiling   ro   r   c                     X R                   -  S:w  d  X0R                   -  S:w  a  [        SU SU S35      eUb  Ub  [        S5      eUc  Uc  [        S5      eg g )Nr   z8`height` and `width` have to be divisible by 32 but are z and .z1Only one of `video` or `latents` can be provided.z/One of `video` or `latents` has to be provided.)r/   rB   )r3   r6   heightwidthr   s        r   check_inputs&LTXLatentUpsamplePipeline.check_inputs   sx    666!;uGiGi?imn?nWX^W__dejdkklmnn!4PQQ=W_NOO -=r   r~   r   decode_timestepdecode_noise_scaleadain_factoroutput_typereturn_dictc           	         U R                  UUUUS9  Ub  SnOUR                  S   nU R                  nUb  [        U5      nXR                  -  S:w  aU  XR                  -  U R                  -  S-   nUS U n[
        R                  SU R                   S[        U5       SU S35        U R                  R                  XUS9nUR                  U[        R                  S	9nU R                  UU[        R                  UUUS
9nU R                  X@R                  R                  U R                  R                   U R                  R"                  R$                  5      nUR                  U R&                  R(                  5      nU R'                  U5      nUS:  a  U R+                  XU5      nOUnU	S:X  a]  U R-                  X@R                  R                  U R                  R                   U R                  R"                  R$                  5      nUnGOU R                  R"                  R.                  (       d  S nO[1        UR                  XUR(                  S9n[3        U[4        5      (       d  U/U-  nUc  UnO[3        U[4        5      (       d  U/U-  n[        R6                  " X\UR(                  S	9n[        R6                  " XlUR(                  S	9S S 2S S S S 4   nSU-
  U-  UU-  -   nU R                  R9                  XOSS9S   nU R                  R;                  XS9nU R=                  5         U
(       d  U4$ [?        US9$ )N)r6   r~   r   r   r   r   z-Video length expected to be of the form `k * z + 1` but is z. Truncating to z frames.)r~   r   r;   )r6   r7   r8   r9   r   r           latent)r   r9   r8   F)r   )r   )frames) r   shape_execution_devicerA   r1   loggerwarningr2   preprocess_videor>   rF   float32rN   rh   r#   rI   rJ   configr_   r$   r8   r]   rH   timestep_conditioningr
   r?   r@   tensordecodepostprocess_videomaybe_free_model_hooksr   )r3   r6   r~   r   r   r   r   r   r   r   r   r7   r9   
num_frameslatents_upsampledtimestepnoises                    r   __call__"LTXLatentUpsamplePipeline.__call__   sA    		 	 	
 J q)J''UJ???1D"E"EEHkHkknoo  kz*CDDgDgChhuvyz  wA  vB  BR  S]  R^  ^f  g ((99%V[9\EHHF%--H@E&&!-- ' 
 ++XX**DHH,@,@$((//B`B`
 **T22889 11':#../@<XG'G("--..0D0DdhhooFdFdG E88??88$W]]i^e^k^kl!/488'6&7*&DO%-)8&#$6==*<)=
)J& <<gmm\%*\\2D[b[h[h%itT4-&" 11W<?QTY?YYHHOOG5OI!LE((::5:ZE 	##%8O ..r   )r/   r1   r2   )Nr   NNNN)g      ?)
Ni   i  Nr   Nr   NpilT)#__name__
__module____qualname____firstlineno__model_cpu_offload_seqr   r   r+   r   rF   Tensorintr8   r9   	GeneratorrN   floatr]   staticmethodrH   rh   rm   rr   rv   rz   r   no_gradr   r   r   strboolr   __static_attributes____classcell__)r4   s   @r   r    r    .   s   c"c 2c 
	c& )-'+)-/3*.%  $	
 & EOO, %,,' 
<5<< ELL bg 8  or-2\\HMfk	   or-2\\HMfk	 "#!"P ]]_ 59*.58BF!MQ%* \//01\/ \/ 	\/
 %,,'\/ ud5k12\/ %U5$u++=%>?\/ \/ E%//43H"HIJ\/ c]\/ \/ \/r   r    )Nr   )typingr   r   r   rF   image_processorr   modelsr   utilsr	   utils.torch_utilsr
   r2   r   pipeline_utilsr   modeling_latent_upsamplerr   pipeline_outputr   r   r   r   r   r   r   r     r   r   <module>r      sx    ) (  1 +  - - . > . 
H	
 ck
TLL
T-5eoo-F
T\_
Tg/ 1 g/r   