
    +h                        S SK r S SKJrJrJrJrJrJr  S SKr	S SK
r
S SKJrJr  SSKJr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JrJr  SSKJr  SSK J!r!  SSK"J#r#  SSK$J%r%  \" 5       (       a  S SK&J's  J(r)  Sr*OSr*\RV                  " \,5      r-Sr.    S!S\/S\/S\0S\04S jjr1    S"S\\/   S\\\2\
Rf                  4      S\\\/      S\\\0      4S jjr4S#S jr5 " S S \#\\5      r6g)$    N)AnyCallableDictListOptionalUnion)T5EncoderModelT5TokenizerFast   )MultiPipelineCallbacksPipelineCallback)FromSingleFileMixinLTXVideoLoraLoaderMixin)AutoencoderKLLTXVideo)LTXVideoTransformer3DModel)FlowMatchEulerDiscreteScheduler)is_torch_xla_availableloggingreplace_example_docstring)randn_tensor)VideoProcessor   )DiffusionPipeline   )LTXPipelineOutputTFax  
    Examples:
        ```py
        >>> import torch
        >>> from diffusers import LTXPipeline
        >>> from diffusers.utils import export_to_video

        >>> pipe = LTXPipeline.from_pretrained("Lightricks/LTX-Video", torch_dtype=torch.bfloat16)
        >>> pipe.to("cuda")

        >>> prompt = "A woman with long brown hair and light skin smiles at another woman with long blonde hair. The woman with brown hair wears a black jacket and has a small, barely noticeable mole on her right cheek. The camera angle is a close-up, focused on the woman with brown hair's face. The lighting is warm and natural, likely from the setting sun, casting a soft glow on the scene. The scene appears to be real-life footage"
        >>> negative_prompt = "worst quality, inconsistent motion, blurry, jittery, distorted"

        >>> video = pipe(
        ...     prompt=prompt,
        ...     negative_prompt=negative_prompt,
        ...     width=704,
        ...     height=480,
        ...     num_frames=161,
        ...     num_inference_steps=50,
        ... ).frames[0]
        >>> export_to_video(video, "output.mp4", fps=24)
        ```
base_seq_lenmax_seq_len
base_shift	max_shiftc                 4    XC-
  X!-
  -  nX5U-  -
  nX-  U-   nU$ N )image_seq_lenr   r   r   r   mbmus           ^/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/pipelines/ltx/pipeline_ltx.pycalculate_shiftr(   F   s3     
	K$>?A%%A		Q	BI    num_inference_stepsdevice	timestepssigmasc                    Ub  Ub  [        S5      eUb  S[        [        R                  " U R                  5      R
                  R                  5       5      ;   nU(       d  [        SU R                   S35      eU R                  " S
X2S.UD6  U R                  n[        U5      nX14$ Ub  S[        [        R                  " U R                  5      R
                  R                  5       5      ;   nU(       d  [        SU R                   S35      eU R                  " S
XBS.UD6  U R                  n[        U5      nX14$ U R                  " U4S	U0UD6  U R                  nX14$ )a  
Calls the scheduler's `set_timesteps` method and retrieves timesteps from the scheduler after the call. Handles
custom timesteps. Any kwargs will be supplied to `scheduler.set_timesteps`.

Args:
    scheduler (`SchedulerMixin`):
        The scheduler to get timesteps from.
    num_inference_steps (`int`):
        The number of diffusion steps used when generating samples with a pre-trained model. If used, `timesteps`
        must be `None`.
    device (`str` or `torch.device`, *optional*):
        The device to which the timesteps should be moved to. If `None`, the timesteps are not moved.
    timesteps (`List[int]`, *optional*):
        Custom timesteps used to override the timestep spacing strategy of the scheduler. If `timesteps` is passed,
        `num_inference_steps` and `sigmas` must be `None`.
    sigmas (`List[float]`, *optional*):
        Custom sigmas used to override the timestep spacing strategy of the scheduler. If `sigmas` is passed,
        `num_inference_steps` and `timesteps` must be `None`.

Returns:
    `Tuple[torch.Tensor, int]`: A tuple where the first element is the timestep schedule from the scheduler and the
    second element is the number of inference steps.
zYOnly one of `timesteps` or `sigmas` can be passed. Please choose one to set custom valuesr,   zThe current scheduler class zx's `set_timesteps` does not support custom timestep schedules. Please check whether you are using the correct scheduler.)r,   r+   r-   zv's `set_timesteps` does not support custom sigmas schedules. Please check whether you are using the correct scheduler.)r-   r+   r+   r"   )

ValueErrorsetinspect	signatureset_timesteps
parameterskeys	__class__r,   len)	schedulerr*   r+   r,   r-   kwargsaccepts_timestepsaccept_sigmass           r'   retrieve_timestepsr<   T   s}   > !3tuu'3w/@/@AXAX/Y/d/d/i/i/k+ll .y/B/B.C Da b  	M)MfM''	!)n )) 
	 C(9(9):Q:Q(R(](](b(b(d$ee.y/B/B.C D_ `  	GvGG''	!)n )) 	 3MFMfM''	))r)   c                     UR                  [        [        SUR                  5      5      SS9nU R                  [        [        SU R                  5      5      SS9nXU-  -  nX%-  SU-
  U -  -   n U $ )a  
Rescales `noise_cfg` tensor based on `guidance_rescale` to improve image quality and fix overexposure. Based on
Section 3.4 from [Common Diffusion Noise Schedules and Sample Steps are
Flawed](https://huggingface.co/papers/2305.08891).

Args:
    noise_cfg (`torch.Tensor`):
        The predicted noise tensor for the guided diffusion process.
    noise_pred_text (`torch.Tensor`):
        The predicted noise tensor for the text-guided diffusion process.
    guidance_rescale (`float`, *optional*, defaults to 0.0):
        A rescale factor applied to the noise predictions.

Returns:
    noise_cfg (`torch.Tensor`): The rescaled noise prediction tensor.
r   T)dimkeepdim)stdlistrangendim)	noise_cfgnoise_pred_textguidance_rescalestd_textstd_cfgnoise_pred_rescaleds         r'   rescale_noise_cfgrJ      s{    " ""tE!_5I5I,J'KUY"ZHmmU1inn%= >mMG#''9: 6!>N:NR[9[[Ir)   c            5         ^  \ rS rSrSrSr/ r/ SQrS\S\	S\
S\S	\4
U 4S
 jjr     SLS\\\\   4   S\S\S\\R(                     S\\R*                     4
S jjr          SMS\\\\   4   S\\\\\   4      S\S\S\\R0                     S\\R0                     S\\R0                     S\\R0                     S\S\\R(                     S\\R*                     4S jjr     SNS jr\SOS\R0                  S\S\S \R0                  4S! jj5       r\ SOS\R0                  S"\S#\S$\S\S\S \R0                  4S% jj5       r\ SPS\R0                  S&\R0                  S'\R0                  S(\S \R0                  4
S) jj5       r\ SPS\R0                  S&\R0                  S'\R0                  S(\S \R0                  4
S* jj5       r          SQS.\S/\S#\S$\S"\S\\R*                     S\\R(                     S0\\RB                     S\\R0                     S \R0                  4S1 jjr"\#S2 5       r$\#S3 5       r%\#S4 5       r&\#S5 5       r'\#S6 5       r(\#S7 5       r)\#S8 5       r*\RV                  " 5       \," \-5      SSS+S,S-S9S:SS;S<SSSSSSSS<SS=SSSS/S4S\\\\   4   S\\\\\   4      S#\S$\S"\S>\S?\S@\\   SA\SB\S\\   S0\\\RB                  \\RB                     4      S\\R0                     S\\R0                     S\\R0                     S\\R0                     S\\R0                     SC\\\\   4   SD\\\\\   4      SE\\   SF\SG\\.\\/4      SH\\0\\\./S4      SI\\   S\42SJ jj5       5       r1SKr2U =r3$ )RLTXPipeline   af  
Pipeline for text-to-video generation.

Reference: https://github.com/Lightricks/LTX-Video

Args:
    transformer ([`LTXVideoTransformer3DModel`]):
        Conditional Transformer architecture to denoise the encoded video latents.
    scheduler ([`FlowMatchEulerDiscreteScheduler`]):
        A scheduler to be used in combination with `transformer` to denoise the encoded image latents.
    vae ([`AutoencoderKLLTXVideo`]):
        Variational Auto-Encoder (VAE) Model to encode and decode images to and from latent representations.
    text_encoder ([`T5EncoderModel`]):
        [T5](https://huggingface.co/docs/transformers/en/model_doc/t5#transformers.T5EncoderModel), specifically
        the [google/t5-v1_1-xxl](https://huggingface.co/google/t5-v1_1-xxl) variant.
    tokenizer (`CLIPTokenizer`):
        Tokenizer of class
        [CLIPTokenizer](https://huggingface.co/docs/transformers/en/model_doc/clip#transformers.CLIPTokenizer).
    tokenizer (`T5TokenizerFast`):
        Second Tokenizer of class
        [T5TokenizerFast](https://huggingface.co/docs/transformers/en/model_doc/t5#transformers.T5TokenizerFast).
ztext_encoder->transformer->vae)latentsprompt_embedsnegative_prompt_embedsr8   vaetext_encoder	tokenizertransformerc                 X  > [         TU ]  5         U R                  UUUUU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 SS 5      b   U R                  R                  R                  OSU l        [        U S5      b   U R                  R                  R                  OSU l        [        U R                  S9U l        [        U SS 5      b  U R"                  R$                  U l        g S	U l        g )
N)rQ   rR   rS   rT   r8   rQ          rT   r   )vae_scale_factorrS      )super__init__register_modulesgetattrrQ   spatial_compression_ratiovae_spatial_compression_ratiotemporal_compression_ratiovae_temporal_compression_ratiorT   config
patch_sizetransformer_spatial_patch_sizepatch_size_ttransformer_temporal_patch_sizer   video_processorrS   model_max_lengthtokenizer_max_length)selfr8   rQ   rR   rS   rT   r6   s         r'   r[   LTXPipeline.__init__   s#    	%# 	 	
 3:$t2L2XDHH..^` 	* 4;43M3YDHH//_` 	+ 3:$t2T2`D##..fg 	+ 5<D-4P4\D##00bc 	,  .t?a?ab/6t[$/O/[DNN++ 	!ad 	!r)   Nr   rY   promptnum_videos_per_promptmax_sequence_lengthr+   dtypec           	         U=(       d    U R                   nU=(       d    U R                  R                  n[        U[        5      (       a  U/OUn[        U5      nU R                  USUSSSS9nUR                  nUR                  n	U	R                  5       R                  U5      n	U R                  USSS9R                  n
U
R                  S   UR                  S   :  a]  [        R                  " X5      (       dB  U R                  R                  U
S S 2US-
  S24   5      n[        R!                  S	U S
U 35        U R                  UR                  U5      5      S   nUR                  XTS9nUR                  u  pnUR#                  SUS5      nUR%                  Xb-  US5      nU	R%                  US5      n	U	R#                  US5      n	X4$ )N
max_lengthTpt)paddingrq   
truncationadd_special_tokensreturn_tensorslongest)rs   rv   r   zXThe following part of your input was truncated because `max_sequence_length` is set to  z	 tokens: r   )ro   r+   )_execution_devicerR   ro   
isinstancestrr7   rS   	input_idsattention_maskbooltoshapetorchequalbatch_decodeloggerwarningrepeatview)rj   rl   rm   rn   r+   ro   
batch_sizetext_inputstext_input_idsprompt_attention_maskuntruncated_idsremoved_textrO   _seq_lens                  r'   _get_t5_prompt_embeds!LTXPipeline._get_t5_prompt_embeds   s    14110**00'44&&[
nn *# % 
 %.. + : : 5 : : < ? ? G..SW.Xbb  $(<(<R(@@UcIuIu>>66qJ]`aJadfJfGf7ghLNN'(	,A
 )).*;*;F*CDQG%((u(D &++A%,,Q0EqI%**:+MwXZ[ 5 : ::r J 5 < <=RTU V33r)   Tnegative_promptdo_classifier_free_guidancerO   rP   r   negative_prompt_attention_maskc                 <   U
=(       d    U R                   n
[        U[        5      (       a  U/OUnUb  [        U5      nOUR                  S   nUc  U R                  UUU	U
US9u  pWU(       a  Uc  U=(       d    Sn[        U[        5      (       a  X/-  OUnUb;  [        U5      [        U5      La$  [        S[        U5       S[        U5       S35      eU[        U5      :w  a!  [        SU S[        U5       S	U SU S
3	5      eU R                  UUU	U
US9u  phXWXh4$ )ab  
Encodes the prompt into text encoder hidden states.

Args:
    prompt (`str` or `List[str]`, *optional*):
        prompt to be encoded
    negative_prompt (`str` or `List[str]`, *optional*):
        The prompt or prompts not to guide the image generation. If not defined, one has to pass
        `negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `guidance_scale` is
        less than `1`).
    do_classifier_free_guidance (`bool`, *optional*, defaults to `True`):
        Whether to use classifier free guidance or not.
    num_videos_per_prompt (`int`, *optional*, defaults to 1):
        Number of videos that should be generated per prompt. torch device to place the resulting embeddings on
    prompt_embeds (`torch.Tensor`, *optional*):
        Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not
        provided, text embeddings will be generated from `prompt` input argument.
    negative_prompt_embeds (`torch.Tensor`, *optional*):
        Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt
        weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input
        argument.
    device: (`torch.device`, *optional*):
        torch device
    dtype: (`torch.dtype`, *optional*):
        torch dtype
r   )rl   rm   rn   r+   ro    z?`negative_prompt` should be the same type to `prompt`, but got z != .z`negative_prompt`: z has batch size z, but `prompt`: zT. Please make sure that passed `negative_prompt` matches the batch size of `prompt`.)	ry   rz   r{   r7   r   r   type	TypeErrorr/   )rj   rl   r   r   rm   rO   rP   r   r   rn   r+   ro   r   s                r'   encode_promptLTXPipeline.encode_prompt  sy   P 1411'44&&VJ&,,Q/J 373M3M&;$7 4N 40M '+A+I-3O@J?\_@`@`j+<<fuO!d6l$:O&OUVZ[jVkUl mV~Q(  s?33 )/)::J3K_J` ax/
| <33  FJE_E_&&;$7 F` FB" 5Kkkr)   c	           
      :  ^  US-  S:w  d	  US-  S:w  a  [        SU SU S35      eUbW  [        U 4S jU 5       5      (       d=  [        ST R                   SU V	s/ s H  oT R                  ;  d  M  U	PM     sn	 35      eUb  Ub  [        S	U S
U S35      eUc  Uc  [        S5      eUbA  [        U[        5      (       d,  [        U[
        5      (       d  [        S[        U5       35      eUb  Uc  [        S5      eUb  Uc  [        S5      eUb  Ub  UR                  UR                  :w  a&  [        SUR                   SUR                   S35      eUR                  UR                  :w  a&  [        SUR                   SUR                   S35      eg g g s  sn	f )NrV   r   z8`height` and `width` have to be divisible by 32 but are z and r   c              3   @   >#    U  H  oTR                   ;   v   M     g 7fr!   )_callback_tensor_inputs).0krj   s     r'   	<genexpr>+LTXPipeline.check_inputs.<locals>.<genexpr>|  s      F
7Y!---7Ys   z2`callback_on_step_end_tensor_inputs` has to be in z, but found zCannot forward both `prompt`: z and `prompt_embeds`: z2. Please make sure to only forward one of the two.zeProvide either `prompt` or `prompt_embeds`. Cannot leave both `prompt` and `prompt_embeds` undefined.z2`prompt` has to be of type `str` or `list` but is zEMust provide `prompt_attention_mask` when specifying `prompt_embeds`.zWMust provide `negative_prompt_attention_mask` when specifying `negative_prompt_embeds`.zu`prompt_embeds` and `negative_prompt_embeds` must have the same shape when passed directly, but got: `prompt_embeds` z != `negative_prompt_embeds` z`prompt_attention_mask` and `negative_prompt_attention_mask` must have the same shape when passed directly, but got: `prompt_attention_mask` z% != `negative_prompt_attention_mask` )r/   allr   rz   r{   rA   r   r   )
rj   rl   heightwidth"callback_on_step_end_tensor_inputsrO   rP   r   r   r   s
   `         r'   check_inputsLTXPipeline.check_inputsn  sK    B;!urzQWX^W__dejdkklmnn-9# F
7YF
 C
 C
 DTEaEaDbbn  |^  pH  |^vw  ko  kG  kG  bGpq  |^  pH  oI  J  -";08N}o ^0 0  ^ 5w  FC)@)@TZ\`IaIaQRVW]R^Q_`aa$)>)Fdee!-2P2Xvww$)?)K""&<&B&BB --:-@-@,A B.445Q8 
 %**.L.R.RR 55J5P5P4Q R6<<=Q@  S *L$+ pHs   F1FrN   rc   re   returnc           
          U R                   u  p4pVnXR-  nXa-  n	Xq-  n
U R                  USUUU	UU
U5      n U R                  SSSSSSSS	5      R                  SS	5      R                  SS5      n U $ )
Nrx   r   r         r   r         )r   reshapepermuteflatten)rN   rc   re   r   num_channels
num_framesr   r   post_patch_num_framespost_patch_heightpost_patch_widths              r'   _pack_latentsLTXPipeline._pack_latents  s     ?Fmm;
*e * :"0 .//!	
 //!Q1aAq9AA!QGOOPQSTUr)   r   r   r   c           
          U R                  S5      nU R                  XaX#SXTU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 $ )
Nr   rx   r   r   r   r   r   r   r   )sizer   r   r   )rN   r   r   r   rc   re   r   s          r'   _unpack_latentsLTXPipeline._unpack_latents  st     \\!_
//*&\gqr//!Q1aAq9AA!QGOOPQSTU]]^_abcr)   latents_meanlatents_std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   rx   r   r   r+   ro   rN   r   r   r   s       r'   _normalize_latentsLTXPipeline._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 $ r   r   r   s       r'   _denormalize_latents LTXPipeline._denormalize_latents  su    
 $((B1a8;;GNNGMMZ!&&q"aA699'..'--X'.8<Gr)           r   num_channels_latents	generatorc
                 t   U	b  U	R                  XvS9$ X0R                  -  nX@R                  -  nUS-
  U R                  -  S-   nXXSU4n
[        U[        5      (       a*  [        U5      U:w  a  [        S[        U5       SU S35      e[        XXvS9n	U R                  XR                  U R                  5      n	U	$ )Nr+   ro   r   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   r+   ro   )r   r_   ra   rz   rA   r7   r/   r   r   rd   rf   )rj   r   r   r   r   r   ro   r+   r   rN   r   s              r'   prepare_latentsLTXPipeline.prepare_latents  s     ::V:99===;;; 1n)L)LLqP
:uMi&&3y>Z+GA#i.AQ R&<'gi 
 u&V$$88$:^:^
 r)   c                     U R                   $ r!   _guidance_scalerj   s    r'   guidance_scaleLTXPipeline.guidance_scale  s    ###r)   c                     U R                   $ r!   )_guidance_rescaler   s    r'   rF   LTXPipeline.guidance_rescale      %%%r)   c                      U R                   S:  $ )N      ?r   r   s    r'   r   'LTXPipeline.do_classifier_free_guidance  s    ##c))r)   c                     U R                   $ r!   )_num_timestepsr   s    r'   num_timestepsLTXPipeline.num_timesteps  s    """r)   c                     U R                   $ r!   )_current_timestepr   s    r'   current_timestepLTXPipeline.current_timestep  r   r)   c                     U R                   $ r!   )_attention_kwargsr   s    r'   attention_kwargsLTXPipeline.attention_kwargs  r   r)   c                     U R                   $ r!   )
_interruptr   s    r'   	interruptLTXPipeline.interrupt  s    r)      2   r           pil
frame_rater*   r,   r   rF   decode_timestepdecode_noise_scaleoutput_typereturn_dictr   callback_on_step_endr   c                 r   [        U[        [        45      (       a  UR                  nU R	                  UUUUUUUUS9  Xl        Xl        UU l        SU l        SU l	        Ub  [        U[        5      (       a  SnO3Ub!  [        U[        5      (       a  [        U5      nOUR                  S   nU R                  nU R                  UUU R                   UUUUUUUS9
u  nnnnU R                   (       a.  ["        R$                  " UU/SS9n["        R$                  " UU/SS9nU R&                  R(                  R*                  nU R-                  UU-  UUUU["        R.                  UUU5	      nUS-
  U R0                  -  S-   nX0R2                  -  nX@R2                  -  nUU-  U-  n [4        R6                  " SSU-  U5      n![9        U U R:                  R(                  R=                  S	S
5      U R:                  R(                  R=                  SS5      U R:                  R(                  R=                  SS5      U R:                  R(                  R=                  SS5      5      n"[?        U R:                  UUUU!U"S9u  p[A        [        U5      XpR:                  RB                  -  -
  S5      n#[        U5      U l"        U R0                  U-  U R2                  U R2                  4n$U RG                  US9 n%[I        U5       GH  u  n&n'U RJ                  (       a  M  U'U l	        U R                   (       a  ["        R$                  " U/S-  5      OUn(U(RM                  URN                  5      n(U'RQ                  U(R                  S   5      n)U R&                  RS                  S5         U R'                  U(UU)UUUUU$USS9
S   n*SSS5        W*RU                  5       n*U R                   (       aN  U*RW                  S5      u  n+n,U+U RX                  U,U+-
  -  -   n*U RZ                  S:  a  []        U*U,U RZ                  S9n*U R:                  R_                  U*U'USS9S   nUbJ  0 n-U H  n.[a        5       U.   U-U.'   M     U" U U&U'U-5      n/U/Rc                  SU5      nU/Rc                  SU5      nU&[        U5      S-
  :X  d)  U&S-   U#:  a0  U&S-   U R:                  RB                  -  S:X  a  U%Re                  5         [f        (       d  GM  [h        Rj                  " 5         GM     SSS5        US:X  a  Un0GOU Rm                  UUUUU Rn                  U Rp                  5      nU Rs                  XRt                  Rv                  U Rt                  Rx                  U Rt                  R(                  Rz                  5      nURM                  URN                  5      nU Rt                  R(                  R|                  (       d  Sn)O[        UR                  UUURN                  S9n1[        U[        5      (       d  U/U-  nUc  UnO[        U[        5      (       d  U/U-  n["        R                  " UUURN                  S9n)["        R                  " UUURN                  S9SS2SSSS4   nSU-
  U-  UU1-  -   nURM                  U Rt                  RN                  5      nU Rt                  R                  UU)SS9S   n0U R                  R                  U0US9n0U R                  5         U(       d  U04$ [        U0S9$ ! , (       d  f       GNi= f! , (       d  f       GN%= f)u  
Function invoked when calling the pipeline for generation.

Args:
    prompt (`str` or `List[str]`, *optional*):
        The prompt or prompts to guide the image generation. If not defined, one has to pass `prompt_embeds`.
        instead.
    height (`int`, defaults to `512`):
        The height in pixels of the generated image. This is set to 480 by default for the best results.
    width (`int`, defaults to `704`):
        The width in pixels of the generated image. This is set to 848 by default for the best results.
    num_frames (`int`, defaults to `161`):
        The number of video frames to generate
    num_inference_steps (`int`, *optional*, defaults to 50):
        The number of denoising steps. More denoising steps usually lead to a higher quality image at the
        expense of slower inference.
    timesteps (`List[int]`, *optional*):
        Custom timesteps to use for the denoising process with schedulers which support a `timesteps` argument
        in their `set_timesteps` method. If not defined, the default behavior when `num_inference_steps` is
        passed will be used. Must be in descending order.
    guidance_scale (`float`, defaults to `3 `):
        Guidance scale as defined in [Classifier-Free Diffusion
        Guidance](https://huggingface.co/papers/2207.12598). `guidance_scale` is defined as `w` of equation 2.
        of [Imagen Paper](https://huggingface.co/papers/2205.11487). Guidance scale is enabled by setting
        `guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to
        the text `prompt`, usually at the expense of lower image quality.
    guidance_rescale (`float`, *optional*, defaults to 0.0):
        Guidance rescale factor proposed by [Common Diffusion Noise Schedules and Sample Steps are
        Flawed](https://arxiv.org/pdf/2305.08891.pdf) `guidance_scale` is defined as `φ` in equation 16. of
        [Common Diffusion Noise Schedules and Sample Steps are Flawed](https://arxiv.org/pdf/2305.08891.pdf).
        Guidance rescale factor should fix overexposure when using zero terminal SNR.
    num_videos_per_prompt (`int`, *optional*, defaults to 1):
        The number of videos to generate per prompt.
    generator (`torch.Generator` or `List[torch.Generator]`, *optional*):
        One or a list of [torch generator(s)](https://pytorch.org/docs/stable/generated/torch.Generator.html)
        to make generation deterministic.
    latents (`torch.Tensor`, *optional*):
        Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image
        generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
        tensor will ge generated by sampling using the supplied random `generator`.
    prompt_embeds (`torch.Tensor`, *optional*):
        Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not
        provided, text embeddings will be generated from `prompt` input argument.
    prompt_attention_mask (`torch.Tensor`, *optional*):
        Pre-generated attention mask for text embeddings.
    negative_prompt_embeds (`torch.FloatTensor`, *optional*):
        Pre-generated negative text embeddings. For PixArt-Sigma this negative prompt should be "". If not
        provided, negative_prompt_embeds will be generated from `negative_prompt` input argument.
    negative_prompt_attention_mask (`torch.FloatTensor`, *optional*):
        Pre-generated attention mask for negative text embeddings.
    decode_timestep (`float`, defaults to `0.0`):
        The timestep at which generated video is decoded.
    decode_noise_scale (`float`, defaults to `None`):
        The interpolation factor between random noise and denoised latents at the decode timestep.
    output_type (`str`, *optional*, defaults to `"pil"`):
        The output format of the generate image. Choose between
        [PIL](https://pillow.readthedocs.io/en/stable/): `PIL.Image.Image` or `np.array`.
    return_dict (`bool`, *optional*, defaults to `True`):
        Whether or not to return a [`~pipelines.ltx.LTXPipelineOutput`] instead of a plain tuple.
    attention_kwargs (`dict`, *optional*):
        A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under
        `self.processor` in
        [diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py).
    callback_on_step_end (`Callable`, *optional*):
        A function that calls at the end of each denoising steps during the inference. The function is called
        with the following arguments: `callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int,
        callback_kwargs: Dict)`. `callback_kwargs` will include a list of all tensors as specified by
        `callback_on_step_end_tensor_inputs`.
    callback_on_step_end_tensor_inputs (`List`, *optional*):
        The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list
        will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the
        `._callback_tensor_inputs` attribute of your pipeline class.
    max_sequence_length (`int` defaults to `128 `):
        Maximum sequence length to use with the `prompt`.

Examples:

Returns:
    [`~pipelines.ltx.LTXPipelineOutput`] or `tuple`:
        If `return_dict` is `True`, [`~pipelines.ltx.LTXPipelineOutput`] is returned, otherwise a `tuple` is
        returned where the first element is a list with the generated images.
)rl   r   r   r   rO   rP   r   r   FNr   r   )
rl   r   r   rm   rO   rP   r   r   rn   r+   )r>   r   base_image_seq_len   max_image_seq_len   r         ?r   ffffff?)r-   r&   )totalr   cond_uncond)
hidden_statesencoder_hidden_statestimestepencoder_attention_maskr   r   r   rope_interpolation_scaler   r   )rF   )r   rN   rO   latentr   r   )r   )frames)Frz   r   r   tensor_inputsr   r   r   r   r   r   r{   rA   r7   r   ry   r   r   r   catrT   rb   in_channelsr   float32ra   r_   nplinspacer(   r8   getr<   maxorderr   progress_bar	enumerater   r   ro   expandcache_contextfloatchunkr   rF   rJ   steplocalspopupdateXLA_AVAILABLExm	mark_stepr   rd   rf   r   rQ   r   r   r   timestep_conditioningr   tensordecoderg   postprocess_videomaybe_free_model_hooksr   )2rj   rl   r   r   r   r   r   r*   r,   r   rF   rm   r   rN   rO   r   rP   r   r   r   r   r   r   r   r   rn   r   r+   r   latent_num_frameslatent_heightlatent_widthvideo_sequence_lengthr-   r&   num_warmup_stepsr  r  itlatent_model_inputr  
noise_prednoise_pred_uncondrE   callback_kwargsr   callback_outputsvideonoises2                                                     r'   __call__LTXPipeline.__call__  s   b *-=?U,VWW1E1S1S. 	/Q'#9"7+I 	 		
  .!1!1!% *VS"9"9JJvt$<$<VJ&,,Q/J'' +(,(H(H"7'#9"7+I 3  
	
!"* ++!II'=}&MSTUM$)II/MOd.ekl$m!  $//66BB&&.. MM

 (!^0S0SSVWW"D"DD B BB 1M AL PS!&9"9;NO!NN!!%%&:C@NN!!%%&94@NN!!%%lC8NN!!%%k48
 *<NN*
&	 s9~0CnnFZFZ0ZZ\]^!)n //*<....$
  %89\!),1>>)*&AEAaAaUYYy1}%=gn"%7%:%:=;N;N%O" 88$6$<$<Q$?@%%33MB!%!1!1&8.;!)/D#4,*1I)9$) "2 " "J C (--/
339C9I9I!9L6%!2T5H5HO^oLo5p!pJ,,q0%6&$J_J_&

 ..--j!WRW-XYZ['3&(O?-3Xa[* @';D!Q'X$.229gFG$4$8$8-$XM I**A9I/IqSTuX\XfXfXlXlNlpqNq '') =LLNm - :r ("E**!3344G //..0D0DdhhooFdFdG jj!4!45G88??88$W]]iPV^e^k^kl!/488'6&7*&DO%-)8&#$6==*<)=
)J& <<gmm\%*\\2DV[b[h[h%itT4-&" 11W<?QTY?YYjj0GHHOOGX5OI!LE((::5k:ZE 	##%8O ..o CB :9s,   )B.\'\3D7\'/\'
\$\''
\6)r   r   r   r   r   r   ri   rd   rf   r_   ra   rg   )Nr   rY   NN)
NTr   NNNNrY   NN)NNNNN)r   r   )r   )	r   rY   r   r   r   NNNN)4__name__
__module____qualname____firstlineno____doc__model_cpu_offload_seq_optional_componentsr   r   r   r	   r
   r   r[   r   r{   r   intr   r   r+   ro   r   r~   Tensorr   r   staticmethodr   r   r  r   r   	Generatorr   propertyr   rF   r   r   r   r   r   no_gradr   EXAMPLE_DOC_STRINGr   r   r   r3  __static_attributes____classcell__)r6   s   @r'   rL   rL      s   . =T"
2"
 #"
 %	"

 #"
 0"
L )-%&#&)-'+.4c49n%.4  #.4 !	.4
 &.4 $.4h <@,0%&049=8<AE#&)-'+Qlc49n%Ql "%T#Y"78Ql &*	Ql
  #Ql  -Ql !) 6Ql  (5Ql )1(>Ql !Ql &Ql $Qlp ,0#"'+3j u||  PS \a\h\h  , rs		+.	8;	DG	UX	lo			 	 nq-2\\HMfk	  nq-2\\HMfk	  $''+)-/3*. " 	
   $ & EOO, %,,' 
B $ $ & & * * # # & & & &   ]]_12 )-;?#%# !"%/0MQ*.048<9=AE58BF%* 59KO9B#&5v/c49n%v/ "%T#Y"78v/ 	v/
 v/ v/ v/ !v/ 9v/ v/  v/  (}v/ E%//43H"HIJv/ %,,'v/  -v/   (5!v/" !) 6#v/$ )1(>%v/& ud5k12'v/( %U5$u++=%>?)v/* c]+v/, -v/. #4S>2/v/0 'xc40@$0F'GH1v/2 -1I3v/4 !5v/ 3 v/r)   rL   )r   r   r   r   )NNNN)r   )7r1   typingr   r   r   r   r   r   numpyr  r   transformersr	   r
   	callbacksr   r   loadersr   r   models.autoencodersr   models.transformersr   
schedulersr   utilsr   r   r   utils.torch_utilsr   rg   r   pipeline_utilsr   pipeline_outputr   torch_xla.core.xla_modelcore	xla_modelr  r  
get_loggerr5  r   rB  r<  r  r(   r{   r+   r<   rJ   rL   r"   r)   r'   <module>rU     s#    = =   8 A C 8 = 9 O O - - . . ))MM			H	% : 

 
 	

 
  *.15%)$(8*!#8* U3,-.8* S	"	8*
 T%[!8*x4e
/#%8:Q e
/r)   