ó
    oM‡jøå  ã                   óò  • S SK r S SKJr  S SKJrJr  S SKrS SKrS SK	r	S SK
Js  Jr  S SKJrJr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Jr  SS
KJr  SSKJ r 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*  SSK+J,r,  \!" 5       (       a  S SK-J.s  J/r0  Sr1OSr1\"Rd                  " \35      r4\ " 5       (       a  S SK5r5Sr6S r7S r8S r9 SS\	Rt                  S\	Rv                  S-  S\<4S jjr= " S S\)\5      r>g) é    N)Údeepcopy)ÚAnyÚCallable)ÚAutoTokenizerÚCLIPImageProcessorÚCLIPVisionModelÚUMT5EncoderModelé   )ÚMultiPipelineCallbacksÚPipelineCallback)ÚPipelineImageInput)ÚWanLoraLoaderMixin)ÚAutoencoderKLWanÚWanAnimateTransformer3DModel)ÚUniPCMultistepScheduler)Úis_ftfy_availableÚis_torch_xla_availableÚloggingÚreplace_example_docstring)Úrandn_tensor)ÚVideoProcessoré   )ÚDiffusionPipelineé   )ÚWanAnimateImageProcessor)ÚWanPipelineOutputTFa’  
    Examples:
        ```python
        >>> import torch
        >>> import numpy as np
        >>> from diffusers import WanAnimatePipeline
        >>> from diffusers.utils import export_to_video, load_image, load_video

        >>> model_id = "Wan-AI/Wan2.2-Animate-14B-Diffusers"
        >>> pipe = WanAnimatePipeline.from_pretrained(model_id, torch_dtype=torch.bfloat16)
        >>> # Optionally upcast the Wan VAE to FP32
        >>> pipe.vae.to(torch.float32)
        >>> pipe.to("cuda")

        >>> # Load the reference character image
        >>> image = load_image(
        ...     "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/astronaut.jpg"
        ... )

        >>> # Load pose and face videos (preprocessed from reference video)
        >>> # Note: Videos should be preprocessed to extract pose keypoints and face features
        >>> # Refer to the Wan-Animate preprocessing documentation for details
        >>> pose_video = load_video("path/to/pose_video.mp4")
        >>> face_video = load_video("path/to/face_video.mp4")

        >>> # CFG is generally not used for Wan Animate
        >>> prompt = (
        ...     "An astronaut hatching from an egg, on the surface of the moon, the darkness and depth of space realised in "
        ...     "the background. High quality, ultrarealistic detail and breath-taking movie-like camera shot."
        ... )

        >>> # Animation mode: Animate the character with the motion from pose/face videos
        >>> output = pipe(
        ...     image=image,
        ...     pose_video=pose_video,
        ...     face_video=face_video,
        ...     prompt=prompt,
        ...     height=height,
        ...     width=width,
        ...     segment_frame_length=77,  # Frame length of each inference segment
        ...     guidance_scale=1.0,
        ...     num_inference_steps=20,
        ...     mode="animate",
        ... ).frames[0]
        >>> export_to_video(output, "output_animation.mp4", fps=30)

        >>> # Replacement mode: Replace a character in the background video
        >>> # Requires additional background_video and mask_video inputs
        >>> background_video = load_video("path/to/background_video.mp4")
        >>> mask_video = load_video("path/to/mask_video.mp4")  # Black areas preserved, white areas generated
        >>> output = pipe(
        ...     image=image,
        ...     pose_video=pose_video,
        ...     face_video=face_video,
        ...     background_video=background_video,
        ...     mask_video=mask_video,
        ...     prompt=prompt,
        ...     height=height,
        ...     width=width,
        ...     segment_frame_length=77,  # Frame length of each inference segment
        ...     guidance_scale=1.0,
        ...     num_inference_steps=20,
        ...     mode="replace",
        ... ).frames[0]
        >>> export_to_video(output, "output_replacement.mp4", fps=30)
        ```
c                 ó¢   • [         R                  " U 5      n [        R                  " [        R                  " U 5      5      n U R	                  5       $ ©N)ÚftfyÚfix_textÚhtmlÚunescapeÚstrip©Útexts    ÚºC:\Users\johnn\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\diffusers/pipelines/wan/pipeline_wan_animate.pyÚbasic_cleanr'   w   s3   € Ü�=Š=˜Ó€DÜ�=Š=œŸš tÓ,Ó-€DØ�:‰:‹<Ðó    c                 óV   • [         R                  " SSU 5      n U R                  5       n U $ )Nz\s+Ú )ÚreÚsubr#   r$   s    r&   Úwhitespace_cleanr-   }   s$   € Ü�6Š6�&˜#˜tÓ$€DØ�:‰:‹<€DØ€Kr(   c                 ó.   • [        [        U 5      5      n U $ r   )r-   r'   r$   s    r&   Úprompt_cleanr/   ƒ   s   € ÜœK¨Ó-Ó.€DØ€Kr(   Ú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)Úhasattrr4   r5   Úmoder7   ÚAttributeError)r0   r1   r2   s      r&   Úretrieve_latentsr;   ‰   s}   € ô ˆ~˜}×-Ñ-°+ÀÓ2IØ×)Ñ)×0Ñ0°Ó;Ð;Ü	� ×	/Ñ	/°KÀ8Ó4KØ×)Ñ)×.Ñ.Ó0Ð0Ü	� ×	+Ñ	+Ø×%Ñ%Ð%äÐRÓSÐSr(   c            9       ó„	  ^ • \ rS rSrSrSr/ SQrS\S\S\	S\
S	\S
\S\4U 4S jjr     SYS\\\   -  S\S\S\R&                  S-  S\R(                  S-  4
S jjr SZS\S\R&                  S-  4S jjr        S[S\\\   -  S\\\   -  S-  S\S\S\R2                  S-  S\R2                  S-  S\S\R&                  S-  S\R(                  S-  4S jjr      S\S jr    S]S\S \S!\S"\S#\S$\R2                  S-  S\R(                  S-  S\\R&                  -  S%\R2                  4S& jjr     S^S\R2                  S\S'\S(\R:                  \\R:                     -  S-  S\R(                  S-  S\R&                  S-  S%\R2                  4S) jjr               S_S.\R2                  S-  S/\R2                  S-  S0\R2                  S-  S\S1\S2\S3\S4\S5\S6\S7\S'\S(\R:                  \\R:                     -  S-  S\R(                  S-  S\R&                  S-  S%\R2                  4 S8 jjr     S^S9\R2                  S\S'\S(\R:                  \\R:                     -  S-  S\R(                  S-  S\R&                  S-  S%\R2                  4S: jjr         S`S\S;\S3\S4\S<\S\R(                  S-  S\R&                  S-  S(\R:                  \\R:                     -  S-  S=\R2                  S-  S%\!\R2                  \R2                  \R2                  4   4S> jjr"S?\\#   S@\S%\\#   4SA jr$\%SB 5       r&\%SC 5       r'\%SD 5       r(\%SE 5       r)\%SF 5       r*\%SG 5       r+\RX                  " 5       \-" \.5      SSSSS+S,S*SHS-SSSISSSSSSSJSSSS=/S4S\S9\\/R`                  R`                     SK\\/R`                  R`                     S/\\/R`                  R`                     S-  S0\\/R`                  R`                     S-  S\\\   -  S\\\   -  S3\S4\S1\SL\SM\SN\SO\S-  SP\1S\S-  S(\R:                  \\R:                     -  S-  S=\R2                  S-  S\R2                  S-  S\R2                  S-  SQ\R2                  S-  SR\S-  SS\ST\2\\#4   S-  SU\3\\S/\4\5-  4   S-  SV\\   S\46SW jj5       5       r6SXr7U =r8$ )aÚWanAnimatePipelineé–   aÁ	  
Pipeline for unified character animation and replacement using Wan-Animate.

WanAnimatePipeline takes a character image, pose video, and face video as input, and generates a video in two
modes:

1. **Animation mode**: The model generates a video of the character image that mimics the human motion in the input
   pose and face videos. The character is animated based on the provided motion controls, creating a new animated
   video of the character.

2. **Replacement mode**: The model replaces a character in a background video with the provided character image,
   using the pose and face videos for motion control. This mode requires additional `background_video` and
   `mask_video` inputs. The mask video should have black regions where the original content should be preserved and
   white regions where the new character should be generated.

This model inherits from [`DiffusionPipeline`]. Check the superclass documentation for the generic methods
implemented for all pipelines (downloading, saving, running on a particular device, etc.).

The pipeline also inherits the following loading methods:
    - [`~loaders.WanLoraLoaderMixin.load_lora_weights`] for loading LoRA weights

Args:
    tokenizer ([`T5Tokenizer`]):
        Tokenizer from [T5](https://huggingface.co/docs/transformers/en/model_doc/t5#transformers.T5Tokenizer),
        specifically the [google/umt5-xxl](https://huggingface.co/google/umt5-xxl) variant.
    text_encoder ([`T5EncoderModel`]):
        [T5](https://huggingface.co/docs/transformers/en/model_doc/t5#transformers.T5EncoderModel), specifically
        the [google/umt5-xxl](https://huggingface.co/google/umt5-xxl) variant.
    image_encoder ([`CLIPVisionModel`]):
        [CLIP](https://huggingface.co/docs/transformers/model_doc/clip#transformers.CLIPVisionModel), specifically
        the
        [clip-vit-huge-patch14](https://github.com/mlfoundations/open_clip/blob/main/docs/PRETRAINED.md#vit-h14-xlm-roberta-large)
        variant.
    transformer ([`WanAnimateTransformer3DModel`]):
        Conditional Transformer to denoise the input latents.
    scheduler ([`UniPCMultistepScheduler`]):
        A scheduler to be used in combination with `transformer` to denoise the encoded image latents.
    vae ([`AutoencoderKLWan`]):
        Variational Auto-Encoder (VAE) Model to encode and decode videos to and from latent representations.
    image_processor ([`CLIPImageProcessor`]):
        Image processor for preprocessing images before encoding.
z-text_encoder->image_encoder->transformer->vae)r7   Úprompt_embedsÚnegative_prompt_embedsÚ	tokenizerÚtext_encoderÚvaeÚ	schedulerÚimage_processorÚimage_encoderÚtransformerc           
      ó:  >• [         T	U ]  5         U R                  UUUUUUUS9  [        U SS 5      (       a   U R                  R
                  R                  OSU l        [        U SS 5      (       a   U R                  R
                  R                  OSU l	        [        U R                  S9U l        [        U R                  SSS9U l        U R                  b#  U R                  R
                  R                  S	S  OS
n[        U R                  USSS9U l        XPl        g )N)rC   rB   rA   rF   rG   rD   rE   rC   é   é   )Úvae_scale_factorFT)rK   Údo_normalizeÚdo_convert_grayscaleéþÿÿÿ)r   r   Úbilinearr   )rK   Úspatial_patch_sizeÚresampleÚ
fill_color)ÚsuperÚ__init__Úregister_modulesÚgetattrrC   ÚconfigÚscale_factor_temporalÚvae_scale_factor_temporalÚscale_factor_spatialÚvae_scale_factor_spatialr   Úvideo_processorÚvideo_processor_for_maskrG   Ú
patch_sizer   Úvae_image_processorrE   )
ÚselfrA   rB   rC   rD   rE   rF   rG   rP   Ú	__class__s
            €r&   rT   ÚWanAnimatePipeline.__init__Å   s  ø€ ô 	‰ÑÔà×ÑØØ%ØØ'Ø#ØØ+ð 	ñ 	
ô SZÐZ^Ð`eÐgk×RlÑRl¨¯©¯©×)NÒ)NÐrsˆÔ&ÜPWÐX\Ð^cÐei×PjÑPj¨¯©¯©×(LÒ(LÐpqˆÔ%Ü-¸t×?\Ñ?\Ñ]ˆÔÜ(6Ø!×:Ñ:ÈÐeiñ)
ˆÔ%ð IM×HXÑHXÑHd˜T×-Ñ-×4Ñ4×?Ñ?ÀÀÑDÐjpÐÜ#;Ø!×:Ñ:Ø1ØØñ	$
ˆÔ ð  /Õr(   Nr   é   ÚpromptÚnum_videos_per_promptÚmax_sequence_lengthÚdeviceÚdtypec                 óØ  • U=(       d    U R                   nU=(       d    U R                  R                  n[        U[        5      (       a  U/OUnU Vs/ s H  n[        U5      PM     nn[        U5      nU R                  USUSSSSS9nUR                  UR                  p©U
R                  S5      R                  SS9R                  5       nU R                  U	R                  U5      U
R                  U5      5      R                  nUR                  XTS9n[        XË5       VVs/ s H
  u  pmUS U PM     nnn[         R"                  " U Vs/ s HJ  n[         R$                  " XfR'                  X6R)                  S5      -
  UR)                  S5      5      /5      PML     snSS9nUR*                  u  pïnUR-                  SUS5      nUR/                  Xr-  US	5      nU$ s  snf s  snnf s  snf )
NÚ
max_lengthTÚpt)Úpaddingrj   Ú
truncationÚadd_special_tokensÚreturn_attention_maskÚreturn_tensorsr   r   ©Údim©rh   rg   éÿÿÿÿ)Ú_execution_devicerB   rh   Ú
isinstanceÚstrr/   ÚlenrA   Ú	input_idsÚattention_maskÚgtÚsumÚlongÚtoÚlast_hidden_stateÚzipÚtorchÚstackÚcatÚ	new_zerosÚsizeÚshapeÚrepeatÚview)r`   rd   re   rf   rg   rh   ÚuÚ
batch_sizeÚtext_inputsÚtext_input_idsÚmaskÚseq_lensr?   ÚvÚ_Úseq_lens                   r&   Ú_get_t5_prompt_embedsÚ(WanAnimatePipeline._get_t5_prompt_embedsë   sÂ  € ð ×1˜4×1Ñ1ˆØ×0˜×*Ñ*×0Ñ0ˆä'¨´×4Ñ4�&‘¸&ˆÙ+1Ó2ª6 a”,˜q–/©6ˆÐ2Ü˜“[ˆ
à—n‘nØØ Ø*ØØ#Ø"&Øð %ð 
ˆð  +×4Ñ4°k×6PÑ6P˜Ø—7‘7˜1“:—>‘> a�>Ð(×-Ñ-Ó/ˆà×)Ñ)¨.×*;Ñ*;¸FÓ*CÀTÇWÁWÈVÃ_ÓU×gÑgˆØ%×(Ñ(¨uÐ(ÐDˆÜ+.¨}Ô+GÔHÒ+G¡4 1˜˜2˜A›Ñ+GˆÑHÜŸšÙ^kÓlÒ^kÐYZŒU�YŠY˜Ÿ;™;Ð':¿V¹VÀA»YÑ'FÈÏÉÈqË	ÓRÐSÖTÑ^kÑlÐrsñ
ˆð
 &×+Ñ+‰ˆ�AØ%×,Ñ,¨QÐ0EÀqÓIˆØ%×*Ñ*¨:Ñ+MÈwÐXZÓ[ˆàÐùò7 3ùó" Iùâls   ÁGÄ$G!ÅAG'Úimagec                 ó´   • U=(       d    U R                   nU R                  USS9R                  U5      nU R                  " S0 UDSS0D6nUR                  S   $ )Nrk   )Úimagesrp   Úoutput_hidden_statesTrN   © )ru   rE   r~   rF   Úhidden_states)r`   r”   rg   Úimage_embedss       r&   Úencode_imageÚWanAnimatePipeline.encode_image  s]   € ð
 ×1˜4×1Ñ1ˆØ×$Ñ$¨EÀ$Ð$ÐG×JÑJÈ6ÓRˆØ×)Ò)ÑM¨EÑMÈÒMˆØ×)Ñ)¨"Ñ-Ð-r(   TÚnegative_promptÚdo_classifier_free_guidancer?   r@   c
                 ó2  • 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nU(       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nXV4$ )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   )rd   re   rf   rg   rh   Ú 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`.)	ru   rv   rw   rx   r†   r’   ÚtypeÚ	TypeErrorÚ
ValueError)r`   rd   r�   rž   re   r?   r@   rf   rg   rh   rŠ   s              r&   Úencode_promptÚ WanAnimatePipeline.encode_prompt   sk  € ðL ×1˜4×1Ñ1ˆä'¨´×4Ñ4�&‘¸&ˆØÑÜ˜V›‰Jà&×,Ñ,¨QÑ/ˆJàÑ Ø ×6Ñ6ØØ&;Ø$7ØØð 7ð ˆMö 'Ð+AÑ+IØ-×3°ˆOÜ@JÈ?Ô\_×@`Ñ@`˜jÐ+<Ò<ÐfuˆOàÑ!¤d¨6£l¼$¸Ó:OÒ&OÜØUÔVZÐ[jÓVkÐUlð mÜ˜V›�~ Qð(óð ð œs ?Ó3Ó3Ü Ø)¨/Ð):Ð:JÌ3ÈÓK_ÐJ`ð aØ�xÐ/°
¨|ð <3ð3óð ð &*×%?Ñ%?Ø&Ø&;Ø$7ØØð &@ð &Ð"ð Ð4Ð4r(   c           
      ó0  ^ • Ub  Ub  [        SU SU S35      eUc  Uc  [        S5      eUb_  [        U[        R                  5      (       d@  [        U[        R
                  R
                  5      (       d  [        S[        U5       35      eUc  [        S5      eUc  [        S5      e[        U[        5      (       a  [        U[        5      (       d  [        S5      e[        U5      S	:X  d  [        U5      S	:X  a  [        S
5      eUS:X  a  Ub  Uc  [        S5      eUS:X  a5  [        U[        5      (       a  [        U[        5      (       d  [        S5      eUS-  S	:w  d	  U	S-  S	:w  a  [        SU SU	 S35      eUbX  [        U 4S jU 5       5      (       d>  [        ST R                   SU Vs/ s H  nUT R                  ;  d  M  UPM     sn 35      eUb  U
b  [        SU SU
 S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bA  [        U[        5      (       d,  [        U[        5      (       d  [        S[        U5       35      eUb5  [        U[        5      (       a  US;  a  [        S[        U5       SU 35      eUb6  [        U[        5      (       a  US;  a  [        S [        U5       SU 35      eg g s  snf )!NzCannot forward both `image`: z and `image_embeds`: z2. Please make sure to only forward one of the two.zbProvide either `image` or `prompt_embeds`. Cannot leave both `image` and `image_embeds` undefined.zE`image` has to be of type `torch.Tensor` or `PIL.Image.Image` but is z:Provide `pose_video`. Cannot leave `pose_video` undefined.z:Provide `face_video`. Cannot leave `face_video` undefined.z:`pose_video` and `face_video` must be lists of PIL images.r   z>`pose_video` and `face_video` must contain at least one frame.Úreplacez„Provide `background_video` and `mask_video`. Cannot leave both `background_video` and `mask_video` undefined when mode is `replace`.zW`background_video` and `mask_video` must be lists of PIL images when mode is `replace`.é   z8`height` and `width` have to be divisible by 16 but are z and r¡   c              3   ó@   >#   • U  H  oTR                   ;   v •  M     g 7fr   )Ú_callback_tensor_inputs)Ú.0Úkr`   s     €r&   Ú	<genexpr>Ú2WanAnimatePipeline.check_inputs.<locals>.<genexpr>¡  s   øé € ð F
Ú7Y°!�×-Ñ-Ö-Ò7Yùs   ƒz2`callback_on_step_end_tensor_inputs` has to be in z, but found zCannot forward both `prompt`: z and `prompt_embeds`: z'Cannot forward both `negative_prompt`: z and `negative_prompt_embeds`: 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 z;`negative_prompt` has to be of type `str` or `list` but is )Úanimater¨   zM`mode` has to be of type `str` and in ('animate', 'replace') but its type is z and value is )r   é   zV`prev_segment_conditioning_frames` has to be of type `int` and 1 or 5 but its type is )r¤   rv   r�   ÚTensorÚPILÚImager¢   Úlistrx   Úallr«   rw   Úint)r`   rd   r�   r”   Ú
pose_videoÚ
face_videoÚbackground_videoÚ
mask_videoÚheightÚwidthr?   r@   rš   Ú"callback_on_step_end_tensor_inputsr9   Ú prev_segment_conditioning_framesr­   s   `                r&   Úcheck_inputsÚWanAnimatePipeline.check_inputsq  s±  ø€ ð$ Ñ Ñ!9ÜØ/°¨wÐ6KÈLÈ>ð Z0ð 0óð ð ‰=˜\Ñ1ÜØtóð ð Ñ¤Z°´u·|±|×%DÑ%DÌZÐX]Ô_b×_hÑ_h×_nÑ_n×MoÑMoÜÐdÔeiÐjoÓepÐdqÐrÓsÐsØÑÜÐYÓZÐZØÑÜÐYÓZÐZÜ˜*¤d×+Ñ+´:¸jÌ$×3OÑ3OÜÐYÓZÐZÜˆz‹?˜aÓ¤3 z£?°aÓ#7ÜÐ]Ó^Ð^Ø�9ÓÐ"2Ñ":¸jÑ>PÜð5óð ð �9Ó¤jÐ1AÄ4×&HÑ&HÔPZÐ[eÔgk×PlÑPlÜÐvÓwÐwà�B‰;˜!Ó˜u r™z¨Q›ÜÐWÐX^ÐW_Ð_dÐejÐdkÐklÐmÓnÐnà-Ñ9Ä#ô F
Ù7YóF
÷ C
ñ C
ô ØDÀT×EaÑEaÐDbð cÙ BÓlÒ B˜1ÀaÈt×OkÑOkÑFk—QÑ BÑlÐmðoóð ð
 Ñ -Ñ";ÜØ0°°Ð8NÈ}Èoð ^0ð 0óð ð Ñ(Ð-CÑ-OÜØ9¸/Ð9JÐJið  kAð  jBð B0ð 0óð ð ‰^ Ñ 5ÜØwóð ð Ñ¬°F¼C×)@Ñ)@ÌÐTZÔ\`×IaÑIaÜÐQÔRVÐW]ÓR^ÐQ_Ð`ÓaÐaØÑ(Ü˜?¬C×0Ñ0¼ÀOÔUY×9ZÑ9ZäÐZÔ[_Ð`oÓ[pÐZqÐrÓsÐsàÑ¤Z°´c×%:Ñ%:¸dÐJ`Ó>`ÜØ_Ô`dÐeiÓ`jÐ_kÐkyÐz~Ðyð  Aóð ð ,Ñ7ÜÐ;¼S×AÑAÐEeÐmsÓEsäðÜÐ9Ó:Ð;¸>ÐJjÐIkðmóð ð Ftð 8ùò; ms   Æ1LÇ	LrŠ   Úlatent_tÚlatent_hÚlatent_wÚmask_lenÚmask_pixel_valuesÚreturnc	           
      ó¢  • Uc"  [         R                  " USUS-
  S-  S-   X4XxS9n	OUR                  5       R                  X‡S9n	SU	S S 2S S 2S U24'   U	S S 2S S 2SS24   n
[         R                  " U
SU R
                  S9n
[         R                  " X©S S 2S S 2SS 24   /SS9n	U	R                  US	U R
                  X45      R                  SS5      n	U	$ )
Nr   rI   rs   ©rg   rh   r   r   )rr   Úrepeatsrq   rt   )	r�   ÚzerosÚcloner~   Úrepeat_interleaverY   Úconcatrˆ   Ú	transpose)r`   rŠ   rÂ   rÃ   rÄ   rÅ   rÆ   rh   rg   Úmask_lat_sizeÚfirst_frame_masks              r&   Úget_i2v_maskÚWanAnimatePipeline.get_i2v_maskË  sí   € ð Ñ$Ü!ŸKšKØ˜A ¨1¡°Ñ1°AÑ5°xÐQVñ‰Mð .×3Ñ3Ó5×8Ñ8ÀÐ8ÐTˆMØ)*ˆ’aš˜I˜X˜I�oÑ&Ø(ªªA¨q°¨s¨Ñ3Ðä ×2Ò2Ð3CÈÐTX×TrÑTrÑsÐÜŸšÐ&6ÂaÊÈAÉBÀhÑ8OÐ%PÐVWÑXˆØ%×*Ñ*Ø˜˜D×:Ñ:¸Hó
ç
‰)�A�q‹/ð 	ð Ðr(   r2   r1   c           
      óþ  • U=(       d    U R                   R                  nUR                  S:X  a  UR                  S5      nUR                  u      pxn	X€R
                  -  n
X�R
                  -  nUR                  XeS9n[        U[        5      (       aJ  U Vs/ s H&  n[        U R                   R                  U5      XÃS9PM(     nn[        R                  " U5      nO%[        U R                   R                  U5      XC5      n[        R                  " U R                   R                  R                  5      R!                  SU R                   R                  R"                  SSS5      R                  UR$                  UR                  5      nS[        R                  " U R                   R                  R&                  5      R!                  SU R                   R                  R"                  SSS5      R                  UR$                  UR                  5      -  nXÞ-
  U-  nUR                  S   S:X  a  US:”  a  UR)                  USSSS5      nU R+                  USX«SS XV5      n[        R                  " UU/SS	9nU$ s  snf )
NrI   r   rÉ   ©r1   r2   r   ç      ð?r   rt   rq   )rC   rh   ÚndimÚ	unsqueezer†   r[   r~   rv   rµ   r;   Úencoder�   rƒ   ÚtensorrW   Úlatents_meanrˆ   Úz_dimrg   Úlatents_stdÚexpandrÒ   )r`   r”   rŠ   r2   r1   rh   rg   r�   r¼   r½   Úlatent_heightÚlatent_widthÚgÚref_image_latentsrÛ   Úlatents_recip_stdÚreference_image_maskÚreference_image_latentss                     r&   Úprepare_reference_image_latentsÚ2WanAnimatePipeline.prepare_reference_image_latentsè  s1  € ð ×'˜Ÿ™Ÿ™ˆØ�:‰:˜‹?à—O‘O AÓ&ˆEà!&§¡Ñˆˆ1ˆa˜Ø×"?Ñ"?Ñ?ˆØ× =Ñ =Ñ=ˆð —‘ �Ð4ˆÜ�i¤×&Ñ&ñ iró!ÚhqÐcdÔ  §¡§¡°Ó!7À1Ô^Ñhqð ð !ô !&§	¢	Ð*;Ó <Ñä 0°·±·±ÀÓ1GÈÓ `Ðô �LŠL˜Ÿ™Ÿ™×5Ñ5Ó6ß‰T�!�T—X‘X—_‘_×*Ñ*¨A¨q°!Ó4ß‰RÐ!×(Ñ(Ð*;×*AÑ*AÓBð 	ð
  ¤%§,¢,¨t¯x©x¯©×/JÑ/JÓ"K×"PÑ"PÐQRÐTX×T\ÑT\×TcÑTc×TiÑTiÐklÐnoÐqrÓ"s×"vÑ"vØ×$Ñ$Ð&7×&=Ñ&=ó#
ñ 
Ðð /Ñ=ÐARÑRÐð ×"Ñ" 1Ñ%¨Ó*¨z¸A«~Ø 1× 8Ñ 8¸ÀRÈÈRÐQSÓ TÐð  $×0Ñ0°¸QÀÐ]^Ð`dÐfkÓtÐÜ"'§)¢)Ð-AÐCTÐ,UÐ[\Ñ"]Ðà&Ð&ùò3!s   Â-I:éM   éÐ  é   r°   Úprev_segment_cond_videorº   r»   Úsegment_frame_lengthÚstart_framer¼   r½   Úprev_segment_cond_framesÚtaskÚinterpolation_modec                 óÞ  • U=(       d    U R                   R                  nUc?  U
S:X  a  US S 2S S 2S U	24   R                  U5      nOUSX—U4n[        R                  " UXïS9nUR
                  u  nnnnnUS-
  U R                  -  S-   nXpR                  -  nX€R                  -  nUU:w  d  UU:w  au  [        SU SU SU SU S3	5        UR                  SS	5      R                  S
S5      n[        R                  " XU4US9nUR                  S
US45      R                  SS	5      nU
S:X  a  US S 2S S 2U	S 24   R                  U5      nOXY-
  n[        R                  " UUUXxXïS9nUR                  US9n[        R                  " UU/S	S9n[        U[         5      (       aÔ  U[#        U5      :X  aW  [%        U5       VVs/ s H>  u  nn['        U R                   R)                  UU   R+                  S
5      5      UU5      PM@     nnnOWUS:X  a7  U Vs/ s H)  n['        U R                   R)                  U5      UU5      PM+     nnO[-        S[#        U5       SU 35      e[        R                  " U5      nO%['        U R                   R)                  U5      XÜ5      n[        R.                  " U R                   R0                  R2                  5      R5                  SU R                   R0                  R6                  SSS5      R                  UR8                  UR                  5      nS[        R.                  " U R                   R0                  R:                  5      R5                  SU R                   R0                  R6                  SSS5      R                  UR8                  UR                  5      -  n UU-
  U -  nU
S:X  an  SU-
  nUR=                  S
S	SSS5      nUR                  S
S5      n[        R                  " UUU4SS9nUR                  S
US45      n!U!R=                  S
S	SSS5      n!OS n!U R?                  UUUUUS
:”  a  U	OS
U!UUS9n"[        R                  " U"U/SS9nU$ s  snnf s  snf )Nr¨   r
   rs   r   z,Interpolating prev segment cond video from (ú, ú) to (Ú)r   r   )r…   r9   rt   ©rh   rq   z:The batch size of the prev segment video should be either z or 1 but is rÖ   rI   Únearest)rÅ   rÆ   rh   rg   ) rC   rh   r~   r�   rË   r†   rY   r[   ÚprintrÏ   ÚflattenÚFÚinterpolateÚ	unflattenrƒ   rv   rµ   rx   Ú	enumerater;   rÙ   rØ   r¤   rÚ   rW   rÛ   rˆ   rÜ   rg   rÝ   ÚpermuterÒ   )#r`   rë   rº   r»   rŠ   rì   rí   r¼   r½   rî   rï   rð   r2   r1   rh   rg   Úcond_frames_shapeÚdata_batch_sizeÚchannelsr�   Úsegment_heightÚsegment_widthÚnum_latent_framesrß   rà   Úremaining_segmentÚremaining_segment_framesÚfull_segment_cond_videoÚirá   Úprev_segment_cond_latentsrÛ   rã   rÆ   Úprev_segment_cond_masks#                                      r&   Ú!prepare_prev_segment_cond_latentsÚ4WanAnimatePipeline.prepare_prev_segment_cond_latents  sÁ  € ð* ×'˜Ÿ™Ÿ™ˆØ"Ñ*Ø�yÓ Ø*:º1ºaÐAZÐBZÐAZÐ;ZÑ*[×*^Ñ*^Ð_dÓ*eÑ'à%/°Ð4LÐV[Ð$\Ð!Ü*/¯+ª+Ð6GÈuÑ*dÐ'àF]×FcÑFcÑCˆ˜ 1 n°mØ1°AÑ5¸$×:XÑ:XÑXÐ[\Ñ\ÐØ×"?Ñ"?Ñ?ˆØ× =Ñ =Ñ=ˆØ˜VÓ# }¸Ó'=ÜØ>¸}¸oÈRÐP^ÐO_Ð_eÐfkÐelÐlnÐouÐnvÐvwÐxôð '>×&GÑ&GÈÈ1Ó&M×&UÑ&UÐVWÐYZÓ&[Ð#Ü&'§m¢mØ'°u¨oÐDVñ'Ð#ð '>×&GÑ&GÈÈJÐXZÐK[Ó&\×&fÑ&fÐghÐjkÓ&lÐ#ð �9ÓØ 0²²AÐ7OÑ7PÐ1PÑ Q× TÑ TÐUZÓ [Ñà';Ñ'VÐ$Ü %§¢Ø˜HÐ&>ÀÐUZñ!Ðð
 #:×"<Ñ"<À5Ð"<Ð"IÐÜ"'§)¢)Ð-DÐFWÐ,XÐ^_Ñ"`Ðä�i¤×&Ñ&Ø¤# i£.Ó0ô !*¨)Ô 4ô-â 4™˜˜1ô % T§X¡X§_¡_Ð5LÈQÑ5O×5YÑ5YÐZ[Ó5\Ó%]Ð_`ÐbmÖnÙ 4ð *ñ -Ð)ð ! AÓ%ñ iró-ÚhqÐcdÔ$ T§X¡X§_¡_Ð5LÓ%MÈqÐR]Ö^Ñhqð *ð -Ð)ô !ØPÔQTÐU^ÓQ_ÐP`ð aØ'Ð(ð*óð ô ).¯	ª	Ð2KÓ(LÑ%ä(8Ø—‘—‘Ð 7Ó8¸)ó)Ð%ô
 �LŠL˜Ÿ™Ÿ™×5Ñ5Ó6ß‰T�!�T—X‘X—_‘_×*Ñ*¨A¨q°!Ó4ß‰RÐ)×0Ñ0Ð2K×2QÑ2QÓRð 	ð
  ¤%§,¢,¨t¯x©x¯©×/JÑ/JÓ"K×"PÑ"PÐQRÐTX×T\ÑT\×TcÑTc×TiÑTiÐklÐnoÐqrÓ"s×"vÑ"vØ%×,Ñ,Ð.G×.MÑ.Mó#
ñ 
Ðð &?ÀÑ%MÐQbÑ$bÐ!ð �9ÓØ˜Z™ˆJØ#×+Ñ+¨A¨q°!°Q¸Ó:ˆJØ#×+Ñ+¨A¨qÓ1ˆJÜŸš z¸ÈÐ8UÐ\eÑfˆJØ *× 4Ñ 4°Q¸ÀRÐ8HÓ IÐØ 1× 9Ñ 9¸!¸QÀÀ1ÀaÓ HÑà $ÐØ!%×!2Ñ!2ØØØØØ1<¸q³Ñ-ÀaØ/ØØð "3ð 	"
Ðô %*§I¢IÐ/EÐG`Ð.aÐghÑ$iÐ!Ø(Ð(ùók-ùò-s   Æ>AQ$È0Q*r¸   c           	      óü  • UR                  Xeb  UOU R                  R                  S9n[        U[        5      (       aJ  U Vs/ s H&  n[        U R                  R                  U5      XsS9PM(     nn[        R                  " U5      nO%[        U R                  R                  U5      XC5      n[        R                  " U R                  R                  R                  5      R                  SU R                  R                  R                  SSS5      R                  UR                  UR                  5      n	S[        R                  " U R                  R                  R                  5      R                  SU R                  R                  R                  SSS5      R                  UR                  UR                  5      -  n
X‰-
  U
-  nUR                   S   S:X  a  US:”  a  UR#                  USSSS5      nU$ s  snf )NrÉ   rÕ   r   rÖ   r   rt   )r~   rC   rh   rv   rµ   r;   rÙ   r�   rƒ   rÚ   rW   rÛ   rˆ   rÜ   rg   rÝ   r†   rÞ   )r`   r¸   rŠ   r2   r1   rh   rg   rá   Úpose_latentsrÛ   rã   s              r&   Úprepare_pose_latentsÚ'WanAnimatePipeline.prepare_pose_latents�  s¢  € ð  —]‘]¨&ÑAR¹ÐX\×X`ÑX`×XfÑXf�]Ðgˆ
Ü�i¤×&Ñ&ámvóÚmvÐhiÔ  §¡§¡°Ó!<ÈÔcÑmvð ð ô !Ÿ9š9 \Ó2‰Lä+¨D¯H©H¯O©O¸JÓ,GÈÓ`ˆLô �LŠL˜Ÿ™Ÿ™×5Ñ5Ó6ß‰T�!�T—X‘X—_‘_×*Ñ*¨A¨q°!Ó4ß‰R�×#Ñ# \×%7Ñ%7Ó8ð 	ð
  ¤%§,¢,¨t¯x©x¯©×/JÑ/JÓ"K×"PÑ"PÐQRÐTX×T\ÑT\×TcÑTc×TiÑTiÐklÐnoÐqrÓ"s×"vÑ"vØ×Ñ ×!3Ñ!3ó#
ñ 
Ðð %Ñ3Ð7HÑHˆØ×Ñ˜aÑ  AÓ%¨*°q«.Ø'×.Ñ.¨z¸2¸rÀ2ÀrÓJˆLØÐùò%s   Á-G9Únum_channels_latentsÚ
num_framesr7   c
                 ó2  • US-
  U R                   -  S-   n
X0R                  -  nX@R                  -  nXU
S-   X¼4n[        U[        5      (       a*  [	        U5      U:w  a  [        S[	        U5       SU S35      eU	c  [        XØXvS9n	U	$ U	R                  XvS9n	U	$ )Nr   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.)r1   rg   rh   rÉ   )rY   r[   rv   rµ   rx   r¤   r   r~   )r`   rŠ   r  r¼   r½   r  rh   rg   r1   r7   r  rß   rà   r†   s                 r&   Úprepare_latentsÚ"WanAnimatePipeline.prepare_latents­  sÃ   € ð (¨!™^°×0NÑ0NÑNÐQRÑRÐØ×"?Ñ"?Ñ?ˆØ× =Ñ =Ñ=ˆàÐ3DÀqÑ3HÈ-ÐfˆÜ�i¤×&Ñ&¬3¨y«>¸ZÓ+GÜØAÄ#ÀiÃ.ÐAQð RØ&˜<Ð'gðióð ð
 ‰?Ü" 5ÀfÑZˆGð ˆð —j‘j¨�jÐ<ˆGàˆr(   ÚframesÚnum_target_framesc                 óì   • SnSn/ n[        U5      U:  a^  UR                  [        X   5      5        U(       a  US-  nOUS-  nUS:X  d  U[        U5      S-
  :X  a  U(       + n[        U5      U:  a  M^  U$ )a  
Pads an array-like video `frames` to `num_target_frames` using a "reflect"-like strategy. The frame dimension
is assumed to be the first dimension. In the 1D case, we can visualize this strategy as follows:

pad_video_frames([1, 2, 3, 4, 5], 10) -> [1, 2, 3, 4, 5, 4, 3, 2, 1, 2]
r   Fr   )rx   Úappendr   )r`   r  r  ÚidxÚflipÚtarget_framess         r&   Úpad_video_framesÚ#WanAnimatePipeline.pad_video_framesË  s€   € ð ˆØˆØˆÜ�-Ó Ð#4Ó4Ø× Ñ ¤¨&©+Ó!6Ô7ÞØ�q‘‘à�q‘�Ø�a‹x˜3¤# f£+°¡/Ó1Ø”x�ô �-Ó Ð#4Õ4ð Ðr(   c                 ó   • U R                   $ r   ©Ú_guidance_scale©r`   s    r&   Úguidance_scaleÚ!WanAnimatePipeline.guidance_scaleà  s   € à×#Ñ#Ð#r(   c                 ó    • U R                   S:„  $ )Nr   r  r!  s    r&   rž   Ú.WanAnimatePipeline.do_classifier_free_guidanceä  s   € à×#Ñ# aÑ'Ð'r(   c                 ó   • U R                   $ r   )Ú_num_timestepsr!  s    r&   Únum_timestepsÚ WanAnimatePipeline.num_timestepsè  s   € à×"Ñ"Ð"r(   c                 ó   • U R                   $ r   )Ú_current_timestepr!  s    r&   Úcurrent_timestepÚ#WanAnimatePipeline.current_timestepì  ó   € à×%Ñ%Ð%r(   c                 ó   • U R                   $ r   )Ú
_interruptr!  s    r&   Ú	interruptÚWanAnimatePipeline.interruptð  s   € à�‰Ðr(   c                 ó   • U R                   $ r   )Ú_attention_kwargsr!  s    r&   Úattention_kwargsÚ#WanAnimatePipeline.attention_kwargsô  r.  r(   é   rÖ   Únpr¹   Únum_inference_stepsr9   r¿   Úmotion_encode_batch_sizer"  rš   Úoutput_typeÚreturn_dictr5  Úcallback_on_step_endr¾   c                 ó¬  • [        U[        [        45      (       a  UR                  nU R	                  UUUUUUUUU	UUUUUU5        X R
                  -  S:w  aA  [        R                  SU R
                   S35        X R
                  -  U R
                  -  S-   n
[        U
S5      n
Xðl	        UU l
        SU l        SU l        U R                  nUb  [        U[        5      (       a  SnO3Ub!  [        U[        5      (       a  [!        U5      nOUR"                  S   n[!        U5      nX­-
  nUU-
  U-  n U S:X  a  Sn!OUU -
  n!UU!-   n"U"U-  n#U R%                  UUU R&                  UUUUUS9u  nnU R(                  R*                  n$UR-                  U$5      nUb  UR-                  U$5      nU R.                  R1                  U5      u  n%n&U%U:w  d  U&U	:w  a"  [        R                  SU& S	U% S
U	 S	U S3	5        U R2                  R5                  XU	SS9R-                  U[6        R8                  S9n'Uc  U R;                  UU5      nUR=                  UU-  SS5      nUR-                  U$5      nU R?                  UU"5      nU R?                  UU"5      nUS   R@                  u  n(n)U)U:w  d  U(U	:w  a"  [        R                  SU( S	U) S
U	 S	U S3	5        U R.                  RC                  X(U	S9R-                  U[6        R8                  S9nUS   R@                  u  n*n+U R(                  RD                  RF                  n,U*U,:w  d  U+U,:w  a"  [        R                  SU* S	U+ S
U, S	U, S3	5        U R.                  RC                  UU,U,S9R-                  U[6        R8                  S9nUS:X  a�  U R?                  UU"5      nU R?                  UU"5      nU R.                  RC                  XHU	S9R-                  U[6        R8                  S9nU RH                  RC                  XXU	S9R-                  U[6        R8                  S9nU RJ                  RM                  UUS9  U RJ                  RN                  n-U RP                  RD                  RR                  n.U RU                  U'UU-  UUS9n/Sn0U
n1/ n2Sn3[W        U#5       GH|  n4U0U-   U:  d   eU RY                  UU-  U.UU	U
[6        R8                  UUU0S:X  a  UOSS9	nUSS2SS2U0U124   n5USS2SS2U0U124   n6U6R[                  UU-  SSSS5      n6U6R-                  U$S9n6U0S:”  a,  U3SS2SS2U* S24   R]                  5       R_                  5       n7OSn7US:X  aM  USS2SS2U0U124   n8USS2SS2U0U124   n9U8R[                  UU-  SSSS5      n8U9R[                  UU-  SSSS5      n9OSn8Sn9U Ra                  U5UU-  UUS9n:U:R-                  U$S9n:U Rc                  U7U8U9UU-  U
U0UU	UUUUS9n;[6        Rd                  " U/U;/SS9n<[!        U-5      X°RJ                  Rf                  -  -
  n=[!        U-5      U l4        U Rk                  US9 n>[m        U-5       GHñ  u  n?n@U Rn                  (       a  M  W@U l        [6        Rd                  " UU</SS9R-                  U$5      nAU@R[                  UR"                  S   5      nBU R(                  Rq                  S5         U R)                  UAUBUUU:U6UUSS9	S   nCSSS5        U R&                  (       aQ  U6S-  S-
  nDU R(                  Rq                  S5         U R)                  WAWBUUU:UDUUSS9	S   nEUEUWCUE-
  -  -   nCSSS5        U RJ                  Rs                  WCW@USS9S   nUb\  0 nFU H  nG[u        5       UG   WFUG'   M     U" U U?W@WF5      nHUHRw                  SU5      nUHRw                  S U5      nUHRw                  S!U5      nU?[!        U-5      S-
  :X  d)  U?S-   U=:”  a0  U?S-   U RJ                  Rf                  -  S:X  a  U>Ry                  5         [z        (       d  GMÜ  [|        R~                  " 5         GMô     SSS5        UR-                  U RP                  R*                  5      n[6        R€                  " U RP                  RD                  R‚                  5      R…                  SU RP                  RD                  RR                  SSS5      R-                  UR†                  UR*                  5      nIS"[6        R€                  " U RP                  RD                  Rˆ                  5      R…                  SU RP                  RD                  RR                  SSS5      R-                  UR†                  UR*                  5      -  nJUUJ-  UI-   nU RP                  R‹                  USS2SS2SS24   SS9S   n3U0S:”  a  U3SS2SS2US24   n3U2R�                  U35        U0U-  n0U1U-  n1U RJ                  RM                  UUS9  U RJ                  RN                  n-GM     SU l        U0U-   U:¼  d   eUS#:X  d<  [6        Rd                  " U2SS9SS2SS2SU24   nKU R.                  R�                  UKUS$9nKOUnKU R‘                  5         U(       d  WK4$ [“        WKS%9$ ! , (       d  f       GN²= f! , (       d  f       GNb= f! , (       d  f       GN{= f)&a…  
The call function to the pipeline for generation.

Args:
    image (`PipelineImageInput`):
        The input character image to condition the generation on. Must be an image, a list of images or a
        `torch.Tensor`.
    pose_video (`list[PIL.Image.Image]`):
        The input pose video to condition the generation on. Must be a list of PIL images.
    face_video (`list[PIL.Image.Image]`):
        The input face video to condition the generation on. Must be a list of PIL images.
    background_video (`list[PIL.Image.Image]`, *optional*):
        When mode is `"replace"`, the input background video to condition the generation on. Must be a list of
        PIL images.
    mask_video (`list[PIL.Image.Image]`, *optional*):
        When mode is `"replace"`, the input mask video to condition the generation on. Must be a list of PIL
        images.
    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.
    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`).
    mode (`str`, defaults to `"animation"`):
        The mode of the generation. Choose between `"animate"` and `"replace"`.
    prev_segment_conditioning_frames (`int`, defaults to `1`):
        The number of frames from the previous video segment to be used for temporal guidance. Recommended to
        be 1 or 5. In general, should be 4N + 1, where N is a non-negative integer.
    motion_encode_batch_size (`int`, *optional*):
        The batch size for batched encoding of the face video via the motion encoder. This allows trading off
        inference speed for lower memory usage by setting a smaller batch size. Will default to
        `self.transformer.config.motion_encoder_batch_size` if not set.
    height (`int`, defaults to `720`):
        The height of the generated video.
    width (`int`, defaults to `1280`):
        The width of the generated video.
    segment_frame_length (`int`, defaults to `77`):
        The number of frames in each generated video segment. The total frames of video generated will be equal
        to the number of frames in `pose_video`; we will generate the video in segments until we have hit this
        length. In general, should be 4N + 1, where N is a non-negative integer.
    num_inference_steps (`int`, defaults to `20`):
        The number of denoising steps. More denoising steps usually lead to a higher quality image at the
        expense of slower inference.
    guidance_scale (`float`, defaults to `1.0`):
        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. By default, CFG is not used in Wan
        Animate inference.
    num_videos_per_prompt (`int`, *optional*, defaults to 1):
        The number of images to generate per prompt.
    generator (`torch.Generator` or `list[torch.Generator]`, *optional*):
        A [`torch.Generator`](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 is 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 (prompt weighting). If not
        provided, text embeddings are generated from the `prompt` input argument.
    negative_prompt_embeds (`torch.Tensor`, *optional*):
        Pre-generated text embeddings. Can be used to easily tweak text inputs (prompt weighting). If not
        provided, text embeddings are generated from the `negative_prompt` input argument.
    image_embeds (`torch.Tensor`, *optional*):
        Pre-generated image embeddings. Can be used to easily tweak image inputs (weighting). If not provided,
        image embeddings are generated from the `image` input argument.
    output_type (`str`, *optional*, defaults to `"np"`):
        The output format of the generated image. Choose between `PIL.Image` or `np.array`.
    return_dict (`bool`, *optional*, defaults to `True`):
        Whether or not to return a [`WanPipelineOutput`] 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`, `PipelineCallback`, `MultiPipelineCallbacks`, *optional*):
        A function or a subclass of `PipelineCallback` or `MultiPipelineCallbacks` that is called at the end of
        each denoising step during the inference. 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 `512`):
        The maximum sequence length of the text encoder. If the prompt is longer than this, it will be
        truncated. If the prompt is shorter, it will be padded to this length.

Examples:

Returns:
    [`~WanPipelineOutput`] or `tuple`:
        If `return_dict` is `True`, [`WanPipelineOutput`] is returned, otherwise a `tuple` is returned where
        the first element is a list with the generated images and the second element is a list of `bool`s
        indicating whether the corresponding generated image contains "not-safe-for-work" (nsfw) content.
r   z2`segment_frame_length - 1` has to be divisible by z!. Rounding to the nearest number.NFr   )rd   r�   rž   re   r?   r@   rf   rg   z Reshaping reference image from (rò   ró   rô   Úfill)r¼   r½   Úresize_moderõ   zReshaping pose video from ()r¼   r½   zReshaping face video from (r¨   )rg   )r1   rg   )r  r¼   r½   r  rh   rg   r1   r7   rt   )rº   r»   rŠ   rì   rí   r¼   r½   rî   rï   r1   rg   r   rq   )ÚtotalÚcond)	r™   ÚtimestepÚencoder_hidden_statesÚencoder_hidden_states_imageÚpose_hidden_statesÚface_pixel_valuesr:  r5  r<  Úuncond)r<  r7   r?   r@   rÖ   Úlatent)r;  )r  )Jrv   r   r   Útensor_inputsrÀ   rY   ÚloggerÚwarningÚmaxr   r4  r+  r0  ru   rw   rµ   rx   r†   r¥   rž   rG   rh   r~   r\   Úget_default_height_widthr_   Ú
preprocessr�   Úfloat32r›   r‡   r  r…   Úpreprocess_videorW   Úmotion_encoder_sizer]   rD   Úset_timestepsÚ	timestepsrC   rÜ   ræ   Úranger  rÞ   rÌ   Údetachr  r
  rƒ   Úorderr'  Úprogress_barrü   r1  Úcache_contextÚstepÚlocalsÚpopÚupdateÚXLA_AVAILABLEÚxmÚ	mark_steprÚ   rÛ   rˆ   rg   rÝ   Údecoder  Úpostprocess_videoÚmaybe_free_model_hooksr   )Lr`   r”   r¸   r¹   rº   r»   rd   r�   r¼   r½   rì   r9  r9   r¿   r:  r"  re   r1   r7   r?   r@   rš   r;  r<  r5  r=  r¾   rf   rg   rŠ   Úcond_video_framesÚeffective_segment_lengthÚlast_segment_framesÚnum_padding_framesr  Únum_segmentsÚtransformer_dtypeÚimage_heightÚimage_widthÚimage_pixelsÚpose_video_widthÚpose_video_heightÚface_video_widthÚface_video_heightÚexpected_face_sizerT  r  rå   ÚstartÚendÚall_out_framesÚ
out_framesr�   Úpose_video_segmentÚface_video_segmentrë   Úbackground_video_segmentÚmask_video_segmentr  r  Úreference_latentsÚnum_warmup_stepsrX  r  ÚtÚlatent_model_inputrC  Ú
noise_predÚface_pixel_values_uncondÚnoise_uncondÚcallback_kwargsr­   Úcallback_outputsrÛ   rã   ÚvideosL                                                                               r&   Ú__call__ÚWanAnimatePipeline.__call__ø  s#  € ôF Ð*Ô-=Ô?UÐ,V×WÑWØ1E×1SÑ1SÐ.ð 	×ÑØØØØØØØØØØØ"ØØ.ØØ,ô	
ð$  ×"@Ñ"@Ñ@ÀAÓEÜ�N‰NØDÀT×EcÑEcÐDdð e#ð $ôð
 %×(FÑ(FÑFÈ×IgÑIgÑgÐjkÑkð !ô  #Ð#7¸Ó;Ðà-ÔØ!1ˆÔØ!%ˆÔØˆŒà×'Ñ'ˆð Ñ¤*¨V´S×"9Ñ"9Ø‰JØÑ¤J¨v´t×$<Ñ$<Ü˜V›‰Jà&×,Ñ,¨QÑ/ˆJô   
›OÐØ#7Ñ#ZÐ Ø0Ð3SÑSÐWoÑoÐØ !Ó#Ø!"Ñà!9Ð<OÑ!OÐØ-Ð0BÑBÐØ(Ð,DÑDˆð 15×0BÑ0BØØ+Ø(,×(HÑ(HØ"7Ø'Ø#9Ø 3Øð 1Cð 	1
Ñ-ˆÐ-ð !×,Ñ,×2Ñ2ÐØ%×(Ñ(Ð):Ó;ˆØ!Ñ-Ø%;×%>Ñ%>Ð?PÓ%QÐ"ð %)×$8Ñ$8×$QÑ$QÐRWÓ$XÑ!ˆ�kØ˜6Ó! [°EÓ%9Ü�N‰NÐ=¸k¸]È"È\ÈNÐZ`ÐafÐ`gÐgiÐjpÐiqÐqrÐsÔtØ×/Ñ/×:Ñ:¸5ÐW\ÐjpÐ:Ðq×tÑtØœ%Ÿ-™-ð uð 
ˆð
 ÑØ×,Ñ,¨U°FÓ;ˆLØ#×*Ñ*¨:Ð8MÑ+MÈqÐRSÓTˆØ#—‘Ð'8Ó9ˆð ×*Ñ*¨:Ð7HÓIˆ
Ø×*Ñ*¨:Ð7HÓIˆ
ð /9¸©m×.@Ñ.@Ñ+ÐÐ+Ø Ó&Ð*:¸eÓ*CÜ�N‰NØ-Ð.>Ð-?¸rÐBSÐATÐTZÐ[`ÐZaÐacÐdjÐckÐklÐmôð ×)Ñ)×:Ñ:¸:Ð\aÐ:Ðb×eÑeØœ%Ÿ-™-ð fð 
ˆ
ð /9¸©m×.@Ñ.@Ñ+ÐÐ+Ø!×-Ñ-×4Ñ4×HÑHÐØÐ1Ó1Ð5FÐJ\Ó5\Ü�N‰NØ-Ð.>Ð-?¸rÐBSÐATÐTZÐ[mÐZnð oØ&Ð' qð*ôð ×)Ñ)×:Ñ:ØÐ1Ð9Kð ;ð 
ç
‰"ˆVœ5Ÿ=™=ˆ"Ð
)ð 	ð �9ÓØ#×4Ñ4Ð5EÐGXÓYÐØ×.Ñ.¨zÐ;LÓMˆJà#×3Ñ3×DÑDÐEUÐlqÐDÐr×uÑuØœeŸm™mð  vð  Ðð ×6Ñ6×GÑGÈ
ÐinÐGÐo×rÑrØœeŸm™mð sð ˆJð
 	�‰×$Ñ$Ð%8ÀÐ$ÑHØ—N‘N×,Ñ,ˆ	ð  $Ÿx™xŸ™×4Ñ4Ðð #'×"FÑ"FØ˜*Ð'<Ñ<È	ÐZ`ð #Gð #
Ðð
 ˆØ"ˆØˆØˆ
ä�|×$ˆAØÐ;Ñ;Ð>OÓOÐOÐOð ×*Ñ*ØÐ2Ñ2Ø%9ØØØ/Ü—m‘mØØ#Ø#(¨A£:™°4ð +ð 
ˆGð ",ªAªq°%¸°)¨OÑ!<ÐØ!+ªAªq°%¸°)¨OÑ!<Ðà!3×!:Ñ!:¸:ÐH]Ñ;]Ð_aÐceÐgiÐkmÓ!nÐØ!3×!6Ñ!6Ð=NÐ!6Ð!OÐà�q‹yØ*4²QºÐ<\Ð;\Ñ;]Ð5]Ñ*^×*dÑ*dÓ*f×*mÑ*mÓ*oÑ'à*.Ð'à�yÓ Ø+;ºAºqÀ%ÈÀ)¸OÑ+LÐ(Ø%/²²1°e¸C°i°Ñ%@Ð"à+C×+JÑ+JØÐ!6Ñ6¸¸BÀÀBó,Ð(ð &8×%>Ñ%>¸zÐLaÑ?aÐceÐgiÐkmÐoqÓ%rÑ"à+/Ð(Ø%)Ð"à×4Ñ4Ø" JÐ1FÑ$FÐR[Ðdjð 5ð ˆLð (Ÿ?™?Ð1B˜?ÐCˆLà(,×(NÑ(NØ'Ø!9Ø-Ø%Ð(=Ñ=Ø%9Ø!ØØØ)IØØ#Øð )Oð )Ð%ô  !&§	¢	Ð+BÐD]Ð*^ÐdeÑ fÐô  # 9›~Ð0CÇnÁn×FZÑFZÑ0ZÑZÐÜ"% i£.ˆDÔà×"Ñ"Ð)<Ð"Ñ=ÀÜ% i×0‘D�A�qØ—~—~Ù à-.�DÔ*ô */¯ª°GÐ=NÐ3OÐUVÑ)W×)ZÑ)ZÐ[lÓ)mÐ&Ø Ÿx™x¨¯©°aÑ(8Ó9�Hà×)Ñ)×7Ñ7¸Õ?Ø%)×%5Ñ%5Ø*<Ø%-Ø2?Ø8DØ/;Ø.@Ø5MØ-=Ø(-ð &6ð 
&ð ñ
&˜
÷ @ð ×7×7à3EÈÑ3IÈAÑ3MÐ0Ø!×-Ñ-×;Ñ;¸HÕEØ+/×+;Ñ+;Ø.@Ø)1Ø6LØ<HØ3?Ø2JØ9QØ1AØ,1ð ,<ð 
,ð  ñ
,!˜Lð *6¸È*ÐWcÑJcÑ8dÑ)d˜J÷ Fð #Ÿn™n×1Ñ1°*¸aÀÐV[Ð1Ð\Ð]^Ñ_�Gà+Ñ7Ø*,˜Û!C˜AÜ17³¸!±˜O¨AÓ.ñ "Dá+?ÀÀaÈÈOÓ+\Ð(à"2×"6Ñ"6°yÀ'Ó"J˜Ø(8×(<Ñ(<¸_ÈmÓ(\˜Ø1A×1EÑ1EÐF^Ð`vÓ1wÐ.ð œC 	›N¨QÑ.Ó.°A¸±EÐ=MÓ3MÐSTÐWXÑSXÐ\`×\jÑ\j×\pÑ\pÑRpÐtuÓRuØ$×+Ñ+Ô-ç$•}ÜŸšŸñu 1÷ >ðz —j‘j §¡§¡Ó0ˆGô —’˜TŸX™XŸ_™_×9Ñ9Ó:ß‘�a˜Ÿ™Ÿ™×.Ñ.°°1°aÓ8ß‘�G—N‘N G§M¡MÓ2ð ð
 !$¤e§l¢l°4·8±8·?±?×3NÑ3NÓ&O×&TÑ&TØ�4—8‘8—?‘?×(Ñ(¨!¨Q°ó'ç‰b�—‘ §¡Ó/ñ!0Ðð Ð 1Ñ1°LÑ@ˆGàŸ™Ÿ™¨²²A°q±r°Ñ):È˜ÐNÈqÑQˆJà�q‹yØ'ªª1Ð.NÑ.OÐ(OÑP�
Ø×!Ñ! *Ô-àÐ-Ñ-ˆEØÐ+Ñ+ˆCð �N‰N×(Ñ(Ð)<ÀVÐ(ÑLØŸ™×0Ñ0‹Iñm %ðp "&ˆÔØÐ7Ñ7Ð;LÓLÐLÐLà˜hÓ&Ü—I’I˜n°!Ñ4²QºÐ;MÐ<MÐ;MÐ5MÑNˆEØ×(Ñ(×:Ñ:¸5ÈkÐ:ÐZ‰EàˆEð 	×#Ñ#Ô%æØ�8ˆOä ¨Ñ.Ð.÷u @Ö?ú÷  FÖEú÷7 >Ö=úsD   ÙBkÛj 	Û5<kÜ1&j2	ÝCkà6kê 
j/ê*kê2
kê<kë
k	)r4  r+  r   r0  r'  rE   r_   r[   rY   r\   r]   )Nr   rc   NNr   )NTr   NNéâ   NN)NNNNNN)r   NNÚcuda)r   r6   NNN)NNNr   rè   r   ré   rê   r   r°   Úbicubicr6   NNN)r©   ré   rê   rè   NNNN)9Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Úmodel_cpu_offload_seqr«   r   r	   r   r   r   r   r   rT   rw   rµ   r·   r�   rg   rh   r’   r   r›   Úboolr²   r¥   rÀ   rÒ   Ú	Generatorræ   r
  r  Útupler  r   r  Úpropertyr"  rž   r(  r,  r1  r5  Úno_gradr   ÚEXAMPLE_DOC_STRINGr³   r´   ÚfloatÚdictr   r   r   r„  Ú__static_attributes__Ú__classcell__)ra   s   @r&   r=   r=   –   sT  ø† ñ)ðV LÐÚTÐð$/à ð$/ð 'ð$/ð ð	$/ð
 +ð$/ð ,ð$/ð 'ð$/ð 2÷$/ðP #'Ø%&Ø#&Ø&*Ø$(ñ'à�d˜3‘i‘ð'ð  #ð'ð !ð	'ð
 —‘˜tÑ#ð'ð �{‰{˜TÑ!õ'ðZ '+ñ.à!ð.ð —‘˜tÑ#õ.ð 37Ø,0Ø%&Ø-1Ø6:Ø#&Ø&*Ø$(ñO5à�d˜3‘i‘ðO5ð ˜t C™y™¨4Ñ/ðO5ð &*ð	O5ð
  #ðO5ð —|‘| dÑ*ðO5ð !&§¡¨tÑ 3ðO5ð !ðO5ð —‘˜tÑ#ðO5ð �{‰{˜TÑ!õO5ðx Ø#ØØ+/ØØ)-ô!Xð@ Ø15Ø$(Ø%+ñàðð ðð ð	ð
 ðð ðð !Ÿ<™<¨$Ñ.ðð �{‰{˜TÑ!ðð �e—l‘lÑ"ðð 
�‰õð@ Ø#ØDHØ$(Ø&*ñ0'à�|‰|ð0'ð ð0'ð ð	0'ð
 —?‘? T¨%¯/©/Ñ%:Ñ:¸TÑAð0'ð �{‰{˜TÑ!ð0'ð —‘˜tÑ#ð0'ð 
�‰õ0'ðh 8<Ø04Ø*.ØØ$&ØØØØ()ØØ"+Ø#ØDHØ$(Ø&*ñ!q)à!&§¡°Ñ!4ðq)ð  Ÿ,™,¨Ñ-ðq)ð —L‘L 4Ñ'ð	q)ð
 ðq)ð "ðq)ð ðq)ð ðq)ð ðq)ð #&ðq)ð ðq)ð  ðq)ð ðq)ð —?‘? T¨%¯/©/Ñ%:Ñ:¸TÑAðq)ð �{‰{˜TÑ!ðq)ð  —‘˜tÑ#ð!q)ð" 
�‰õ#q)ðl Ø#ØDHØ$(Ø&*ñà—L‘Lðð ðð ð	ð
 —?‘? T¨%¯/©/Ñ%:Ñ:¸TÑAðð �{‰{˜TÑ!ðð —‘˜tÑ#ðð 
�‰õðF %'ØØØØ$(Ø&*ØDHØ'+ñàðð "ðð ð	ð
 ðð ðð �{‰{˜TÑ!ðð —‘˜tÑ#ðð —?‘? T¨%¯/©/Ñ%:Ñ:¸TÑAðð —‘ Ñ$ðð 
ˆu�|‰|˜UŸ\™\¨5¯<©<Ð7Ñ	8õð< t¨C¡yð ÀSð ÈTÐRUÉYô ð* ñ$ó ð$ð ñ(ó ð(ð ñ#ó ð#ð ñ&ó ð&ð ñó ðð ñ&ó ð&ð ‡]‚]ƒ_ÙÐ1Ó2ð :>Ø37Ø"&Ø+/ØØØ$&Ø#%ØØ01Ø/3Ø #Ø,-ØDHØ'+Ø-1Ø6:Ø,0Ø"&Ø Ø26ØmqØ9B¸Ø#&ñ9x/à!ðx/ð ˜Ÿ™Ÿ™Ñ)ðx/ð ˜Ÿ™Ÿ™Ñ)ð	x/ð
 ˜sŸy™yŸ™Ñ/°$Ñ6ðx/ð ˜Ÿ™Ÿ™Ñ)¨DÑ0ðx/ð �d˜3‘i‘ðx/ð ˜t C™y™ðx/ð ðx/ð ðx/ð "ðx/ð !ðx/ð ðx/ð +.ðx/ð #&¨¡*ðx/ð  ð!x/ð"  # T™zð#x/ð$ —?‘? T¨%¯/©/Ñ%:Ñ:¸TÑAð%x/ð& —‘ Ñ$ð'x/ð( —|‘| dÑ*ð)x/ð* !&§¡¨tÑ 3ð+x/ð, —l‘l TÑ)ð-x/ð. ˜4‘Zð/x/ð0 ð1x/ð2 ˜s C˜x™.¨4Ñ/ð3x/ð4 '¨¨S°$Ð'7Ð9IÐLbÑ9bÐ'bÑcÐfjÑjð5x/ð6 -1°©Ið7x/ð8 !ô9x/ó 3ó öx/r(   r=   )Nr5   )?r!   Úcopyr   Útypingr   r   r³   Úregexr+   r�   Útorch.nn.functionalÚnnÚ
functionalrù   Útransformersr   r   r   r	   Ú	callbacksr   r   rE   r   Úloadersr   Úmodelsr   r   Ú
schedulersr   Úutilsr   r   r   r   Úutils.torch_utilsr   r\   r   Úpipeline_utilsr   r   Úpipeline_outputr   Útorch_xla.core.xla_modelÚcoreÚ	xla_modelr_  r^  Ú
get_loggerr‰  rK  r   r”  r'   r-   r/   r²   r�  rw   r;   r=   r˜   r(   r&   Ú<module>r¬     sà   ðó Ý ß  ã 
Û Û ß Ð ß ]Ó ]ç AÝ 1Ý )ß DÝ 1ß bÓ bÝ -Ý -Ý .Ý 5Ý .ñ ×Ñß)Ð)à�Mà€Mà	×	Ò	˜HÓ	%€á×ÑÛðBÐ òJòòð `hñ
TØ—L‘Lð
TØ-2¯_©_¸tÑ-Cð
TØY\õ
Tô\/Ð*Ð,>õ \/r(   