o
    pi                     @   s  d dl Z d dlmZmZmZmZmZmZ d dlZd dl	m
Z
mZmZmZ ddlmZ ddlmZmZmZ ddlmZmZmZmZ ddlmZ dd	lmZ dd
lmZmZm Z m!Z!m"Z"m#Z# ddl$m%Z%m&Z&m'Z'm(Z( ddl)m*Z* ddl+m,Z, ddl-m.Z. ddl/m0Z0 ddl1m2Z2m3Z3 ddl4m5Z5 e&6e7Z8dZ9	d#dej:deej; de<fddZ=				d$dee> deee<ej?f  deee>  deee@  fdd ZAG d!d" d"e2e3eeee.e0	ZBdS )%    N)AnyCallableDictListOptionalUnion)CLIPImageProcessorCLIPTextModelCLIPTokenizerCLIPVisionModelWithProjection   )PipelineImageInput)IPAdapterMixinStableDiffusionLoraLoaderMixinTextualInversionLoaderMixin)AutoencoderKLImageProjectionUNet2DConditionModelUNetMotionModel)adjust_lora_scale_text_encoder)MotionAdapter)DDIMSchedulerDPMSolverMultistepSchedulerEulerAncestralDiscreteSchedulerEulerDiscreteSchedulerLMSDiscreteSchedulerPNDMScheduler)USE_PEFT_BACKENDloggingscale_lora_layersunscale_lora_layers)randn_tensor)VideoProcessor   )FreeInitMixin)AnimateDiffFreeNoiseMixin)DiffusionPipelineStableDiffusionMixin   )AnimateDiffPipelineOutputa  
    Examples:
        ```py
        >>> import imageio
        >>> import requests
        >>> import torch
        >>> from diffusers import AnimateDiffVideoToVideoPipeline, DDIMScheduler, MotionAdapter
        >>> from diffusers.utils import export_to_gif
        >>> from io import BytesIO
        >>> from PIL import Image

        >>> adapter = MotionAdapter.from_pretrained(
        ...     "guoyww/animatediff-motion-adapter-v1-5-2", torch_dtype=torch.float16
        ... )
        >>> pipe = AnimateDiffVideoToVideoPipeline.from_pretrained(
        ...     "SG161222/Realistic_Vision_V5.1_noVAE", motion_adapter=adapter
        ... ).to("cuda")
        >>> pipe.scheduler = DDIMScheduler(
        ...     beta_schedule="linear", steps_offset=1, clip_sample=False, timespace_spacing="linspace"
        ... )


        >>> def load_video(file_path: str):
        ...     images = []

        ...     if file_path.startswith(("http://", "https://")):
        ...         # If the file_path is a URL
        ...         response = requests.get(file_path)
        ...         response.raise_for_status()
        ...         content = BytesIO(response.content)
        ...         vid = imageio.get_reader(content)
        ...     else:
        ...         # Assuming it's a local file path
        ...         vid = imageio.get_reader(file_path)

        ...     for frame in vid:
        ...         pil_image = Image.fromarray(frame)
        ...         images.append(pil_image)

        ...     return images


        >>> video = load_video(
        ...     "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/animatediff-vid2vid-input-1.gif"
        ... )
        >>> output = pipe(
        ...     video=video, prompt="panda playing a guitar, on a boat, in the ocean, high quality", strength=0.5
        ... )
        >>> frames = output.frames[0]
        >>> export_to_gif(frames, "animation.gif")
        ```
sampleencoder_output	generatorsample_modec                 C   sR   t | dr|dkr| j|S t | dr|dkr| j S t | dr%| jS td)Nlatent_distr*   argmaxlatentsz3Could not access latents of provided encoder_output)hasattrr.   r*   moder0   AttributeError)r+   r,   r-    r4   ~/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/diffusers/pipelines/animatediff/pipeline_animatediff_video2video.pyretrieve_latentsd   s   

r6   num_inference_stepsdevice	timestepssigmasc                 K   s  |dur|durt d|dur>dtt| jj v }|s(t d| j d| jd||d| | j}t	|}||fS |durpdtt| jj v }|sZt d| j d| jd||d	| | j}t	|}||fS | j|fd
|i| | j}||fS )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.
    NzYOnly one of `timesteps` or `sigmas` can be passed. Please choose one to set custom valuesr9   zThe current scheduler class zx's `set_timesteps` does not support custom timestep schedules. Please check whether you are using the correct scheduler.)r9   r8   r:   zv's `set_timesteps` does not support custom sigmas schedules. Please check whether you are using the correct scheduler.)r:   r8   r8   r4   )

ValueErrorsetinspect	signatureset_timesteps
parameterskeys	__class__r9   len)	schedulerr7   r8   r9   r:   kwargsaccepts_timestepsaccept_sigmasr4   r4   r5   retrieve_timestepsr   s2   rH   c                4       s  e Zd ZdZdZg dZg dZ		dQdedede	d	e
d
edeeeeeeef dedef fddZ					dRdeej deej dee dee fddZdSddZdd ZdTdedejfddZdTdefdd Zd!d" Z 								dUd#d$Z!d%d& Z"		dVdefd'd(Z#e$d)d* Z%e$d+d, Z&e$d-d. Z'e$d/d0 Z(e$d1d2 Z)e* ddddd3ddd4d5dd6d7ddddddd8d9dddd:gdfd;e+e+e,  d<eee-e+e- f  d=ee d>ee d?ed@ee+e  dAee+e  dBedCedDeee-e+e- f  dEee dFedGeeej.e+ej. f  d:eej deej deej dHee, dIee+ej  dJee- dKe/dLee0e-e1f  dee dMee2eee0gdf  dNe+e- def2dOdPZ3  Z4S )WAnimateDiffVideoToVideoPipelineaw  
    Pipeline for video-to-video generation.

    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.TextualInversionLoaderMixin.load_textual_inversion`] for loading textual inversion embeddings
        - [`~loaders.StableDiffusionLoraLoaderMixin.load_lora_weights`] for loading LoRA weights
        - [`~loaders.StableDiffusionLoraLoaderMixin.save_lora_weights`] for saving LoRA weights
        - [`~loaders.IPAdapterMixin.load_ip_adapter`] for loading IP Adapters

    Args:
        vae ([`AutoencoderKL`]):
            Variational Auto-Encoder (VAE) Model to encode and decode images to and from latent representations.
        text_encoder ([`CLIPTextModel`]):
            Frozen text-encoder ([clip-vit-large-patch14](https://huggingface.co/openai/clip-vit-large-patch14)).
        tokenizer (`CLIPTokenizer`):
            A [`~transformers.CLIPTokenizer`] to tokenize text.
        unet ([`UNet2DConditionModel`]):
            A [`UNet2DConditionModel`] used to create a UNetMotionModel to denoise the encoded video latents.
        motion_adapter ([`MotionAdapter`]):
            A [`MotionAdapter`] to be used in combination with `unet` to denoise the encoded video latents.
        scheduler ([`SchedulerMixin`]):
            A scheduler to be used in combination with `unet` to denoise the encoded image latents. Can be one of
            [`DDIMScheduler`], [`LMSDiscreteScheduler`], or [`PNDMScheduler`].
    z&text_encoder->image_encoder->unet->vae)feature_extractorimage_encodermotion_adapter)r0   prompt_embedsnegative_prompt_embedsNvaetext_encoder	tokenizerunetrL   rD   rJ   rK   c	           	   
      sd   t    t|trt||}| j||||||||d dt| jj	j
d  | _t| jd| _d S )N)rO   rP   rQ   rR   rL   rD   rJ   rK   r#   r(   )vae_scale_factor)super__init__
isinstancer   r   from_unet2dregister_modulesrC   rO   configblock_out_channelsrS   r"   video_processor)	selfrO   rP   rQ   rR   rL   rD   rJ   rK   rB   r4   r5   rU      s   


z(AnimateDiffVideoToVideoPipeline.__init__rM   rN   
lora_scale	clip_skipc
              
   C   s  |durt | tr|| _tst| j| nt| j| |dur't |tr'd}
n|dur5t |tr5t	|}
n|j
d }
|du rt | trJ| || j}| j|d| jjddd}|j}| j|ddd	j}|j
d
 |j
d
 krt||s| j|dd| jjd d
f }td| jj d|  t| jjdr| jjjr|j|}nd}|	du r| j|||d}|d }n| j|||dd}|d
 |	d   }| jj|}| jdur| jj}n| jdur| jj}n|j}|j||d}|j
\}}}|d|d}||| |d
}|r|du r|du rdg|
 }nC|dur8t |t |ur8t!dt | dt | dt |trB|g}n|
t	|kr\t"d| dt	| d| d|
 d	|}t | trk| || j}|j
d }| j|d|ddd}t| jjdr| jjjr|j|}nd}| j|j||d}|d }|r|j
d }|j||d}|d|d}||
| |d
}| jdurt | trtrt#| j| ||fS )a  
        Encodes the prompt into text encoder hidden states.

        Args:
            prompt (`str` or `List[str]`, *optional*):
                prompt to be encoded
            device: (`torch.device`):
                torch device
            num_images_per_prompt (`int`):
                number of images that should be generated per prompt
            do_classifier_free_guidance (`bool`):
                whether to use classifier free guidance or not
            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`).
            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.
            lora_scale (`float`, *optional*):
                A LoRA scale that will be applied to all LoRA layers of the text encoder if LoRA layers are loaded.
            clip_skip (`int`, *optional*):
                Number of layers to be skipped from CLIP while computing the prompt embeddings. A value of 1 means that
                the output of the pre-final layer will be used for computing the prompt embeddings.
        Nr(   r   
max_lengthTpt)paddingr`   
truncationreturn_tensorslongest)rb   rd   z\The following part of your input was truncated because CLIP can only handle sequences up to z	 tokens: use_attention_mask)attention_mask)rh   output_hidden_states)dtyper8    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`.)$rV   r   _lora_scaler   r   rP   r   strlistrC   shaper   maybe_convert_promptrQ   model_max_length	input_idstorchequalbatch_decodeloggerwarningr1   rY   rg   rh   to
text_modelfinal_layer_normrj   rR   repeatviewtype	TypeErrorr;   r    )r\   promptr8   num_images_per_promptdo_classifier_free_guidancenegative_promptrM   rN   r^   r_   
batch_sizetext_inputstext_input_idsuntruncated_idsremoved_textrh   prompt_embeds_dtypebs_embedseq_len_uncond_tokensr`   uncond_inputr4   r4   r5   encode_prompt   s   +











z-AnimateDiffVideoToVideoPipeline.encode_promptc           
      C   s   t | j j}t|tjs| j|ddj}|j	||d}|rH| j|ddj
d }|j|dd}| jt|ddj
d }|j|dd}||fS | |j}|j|dd}t|}	||	fS )	Nra   )rd   r8   rj   T)ri   r   dim)nextrK   r@   rj   rV   rt   TensorrJ   pixel_valuesry   hidden_statesrepeat_interleave
zeros_likeimage_embeds)
r\   imager8   r   ri   rj   image_enc_hidden_statesuncond_image_enc_hidden_statesr   uncond_image_embedsr4   r4   r5   encode_image  s(   

z,AnimateDiffVideoToVideoPipeline.encode_imagec                 C   sl  g }|rg }|d u ret |ts|g}t|t| jjjkr/tdt| dt| jjj dt|| jjjD ],\}}	t |	t }
| 	||d|
\}}|
|d d d f  |rc|
|d d d f  q7n|D ]}|rw|d\}}|
| |
| qgg }t|D ]0\}}tj|g| dd}|rtj|| g| dd}tj||gdd}|j|d}|
| q|S )	NzK`ip_adapter_image` must have same length as the number of IP Adapters. Got z images and z IP Adapters.r(   r#   r   r   )r8   )rV   ro   rC   rR   encoder_hid_projimage_projection_layersr;   zipr   r   appendchunk	enumeratert   catry   )r\   ip_adapter_imageip_adapter_image_embedsr8   r   r   r   negative_image_embedssingle_ip_adapter_imageimage_proj_layeroutput_hidden_statesingle_image_embedssingle_negative_image_embedsir4   r4   r5   prepare_ip_adapter_image_embeds  sH   


z?AnimateDiffVideoToVideoPipeline.prepare_ip_adapter_image_embeds   decode_chunk_sizereturnc                 C   sR   g }t dt||D ]}||||  }t| j||d}|| q
t|S )Nr   )r,   )rangerC   r6   rO   encoder   rt   r   )r\   videor,   r   r0   r   batch_videor4   r4   r5   encode_video  s   
z,AnimateDiffVideoToVideoPipeline.encode_videoc                 C   s   d| j jj | }|j\}}}}}|ddddd|| |||}g }td|jd |D ]}	||	|	|  }
| j |
j}
|	|
 q-t
|}|d d d f ||df|jdd   ddddd}| }|S )Nr(   r   r#   r      rf   )rO   rY   scaling_factorrp   permutereshaper   decoder*   r   rt   r   float)r\   r0   r   r   channels
num_framesheightwidthr   r   batch_latentsr4   r4   r5   decode_latents   s   "
8z.AnimateDiffVideoToVideoPipeline.decode_latentsc                 C   sX   dt t| jjj v }i }|r||d< dt t| jjj v }|r*||d< |S )Netar,   )r<   r=   r>   rD   stepr@   rA   )r\   r,   r   accepts_etaextra_step_kwargsaccepts_generatorr4   r4   r5   prepare_extra_step_kwargs  s   z9AnimateDiffVideoToVideoPipeline.prepare_extra_step_kwargsc                    s  |dk s|dkrt d| |d dks|d dkr&t d| d| d|d urGt fdd	|D sGt d
 j d fdd|D  |d urZ|d urZt d| d| d|d u rf|d u rft d|d ur}t|ts}t|ts}t dt| |d ur|	d urt d| d|	 d|d ur|	d ur|j|	jkrt d|j d|	j d|d ur|d urt d|
d ur|d urt d|d urt|tst dt| |d jdvrt d|d j dd S d S )Nr   r(   z2The value of strength should in [0.0, 1.0] but is    z7`height` and `width` have to be divisible by 8 but are z and rl   c                 3   s    | ]}| j v V  qd S N_callback_tensor_inputs.0kr\   r4   r5   	<genexpr>9  s    

z?AnimateDiffVideoToVideoPipeline.check_inputs.<locals>.<genexpr>z2`callback_on_step_end_tensor_inputs` has to be in z, but found c                    s   g | ]	}| j vr|qS r4   r   r   r   r4   r5   
<listcomp>=  s    z@AnimateDiffVideoToVideoPipeline.check_inputs.<locals>.<listcomp>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 z'Cannot forward both `negative_prompt`: z and `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` z3Only one of `video` or `latents` should be providedzProvide either `ip_adapter_image` or `ip_adapter_image_embeds`. Cannot leave both `ip_adapter_image` and `ip_adapter_image_embeds` defined.z:`ip_adapter_image_embeds` has to be of type `list` but is )r   r   zF`ip_adapter_image_embeds` has to be a list of 3D or 4D tensors but is D)	r;   allr   rV   rn   ro   r~   rp   ndim)r\   r   strengthr   r   r   r0   r   rM   rN   r   r   "callback_on_step_end_tensor_inputsr4   r   r5   check_inputs$  sh   
z,AnimateDiffVideoToVideoPipeline.check_inputsc                 C   s@   t t|| |}t|| d}||| jj d  }||| fS )Nr   )minintmaxrD   order)r\   r7   r9   r   r8   init_timestept_startr4   r4   r5   get_timestepsl  s   z-AnimateDiffVideoToVideoPipeline.get_timestepsc                    s&  |
d u r
j d }n|
j d }||||j |j f}ttr4t|kr4tdt d| d|
d u rjjjrI	 jj
tjd ttrpt|kratdt d| d fddt|D }n fd	dD }tj|d
d}jjjrj
| |
|}jjj| }||j d
 kr||j d
  d
krd| d|j d
  d}t|||j d
 kr||j d
  d
krtd|j d
  d| dtj|gd
d}t|j ||d}j|||d
dddd}
|
S ||
j kr
td|d|
j |
j
||d}
|
S )Nr(   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.)rj   c                    s(   g | ]} | |  d qS r   r   	unsqueeze)r   r   r   r,   r\   r   r4   r5   r     s    zCAnimateDiffVideoToVideoPipeline.prepare_latents.<locals>.<listcomp>c                    s    g | ]} | d qS r   r   )r   vid)r   r,   r\   r4   r5   r     s     r   r   zYou have passed z# text prompts (`prompt`), but only zp initial images (`image`). Please make sure to update your script to pass as many initial images as text promptsz'Cannot duplicate `image` of batch size z to z text prompts.)r,   r8   rj   r   r   z!`latents` expected to have shape=z, but found latents.shape=)rp   rS   rV   ro   rC   r;   rO   rY   force_upcastr   ry   rt   float32r   r   r   r!   rD   	add_noiser   )r\   r   r   r   num_channels_latentsr   timesteprj   r8   r,   r0   r   r   rp   init_latentserror_messagenoiser4   r   r5   prepare_latentsu  sf   




  z/AnimateDiffVideoToVideoPipeline.prepare_latentsc                 C      | j S r   _guidance_scaler   r4   r4   r5   guidance_scale     z.AnimateDiffVideoToVideoPipeline.guidance_scalec                 C   r   r   )
_clip_skipr   r4   r4   r5   r_     r   z)AnimateDiffVideoToVideoPipeline.clip_skipc                 C   s
   | j dkS )Nr(   r   r   r4   r4   r5   r     s   
z;AnimateDiffVideoToVideoPipeline.do_classifier_free_guidancec                 C   r   r   )_cross_attention_kwargsr   r4   r4   r5   cross_attention_kwargs  r   z6AnimateDiffVideoToVideoPipeline.cross_attention_kwargsc                 C   r   r   )_num_timestepsr   r4   r4   r5   num_timesteps  r   z-AnimateDiffVideoToVideoPipeline.num_timesteps2   g      @g?r(   g        pilTr0   r   r   r   r   r7   r9   r:   r   r   r   num_videos_per_promptr   r,   r   r   output_typereturn_dictr   callback_on_step_endr   c           0      C   s  |p	| j jj| j }|p| j jj| j }d}| j||	|||
|||||||d || _|| _|| _|dur<t|t	r<d}n|durJt|t
rJt|}n|jd }| j}| jdur^| jddnd}| j|||| j|
|||| jd	\}}| jr|t||g}|dus|dur| ||||| | j}t| j||||\}}| |||	|\}}|dd || }|du r| jj|||d}|dddd	d
}|j||jd}| j jj}| j |||||| ||j||||d}| !||} |dus|durd|ind}!| j"r| j#nd}"t$|"D ]}#| j"r(| %||#|||j|\}}t|}| |||	|\}}t|| _&t||| jj'  }$| j(| j&d}%t)|D ]\}&}'| jrSt|gd n|}(| j*|(|'}(| j |(|'|| j|!dj+})| jr{|),d\}*}+|*||+|*   })| jj-|)|'|fi | j.}|duri },|D ]
}-t/ |- |,|-< q|| |&|'|,}.|.0d|}|.0d|}|.0d|}|&t|d ks|&d |$kr|&d | jj' dkr|%1  qCW d   n	1 sw   Y  q|dkr|}n| 2||}/| jj3|/|d}| 4  |s|fS t5|dS )u  
        The call function to the pipeline for generation.

        Args:
            video (`List[PipelineImageInput]`):
                The input video to condition the generation on. Must be a list of images/frames of the video.
            prompt (`str` or `List[str]`, *optional*):
                The prompt or prompts to guide image generation. If not defined, you need to pass `prompt_embeds`.
            height (`int`, *optional*, defaults to `self.unet.config.sample_size * self.vae_scale_factor`):
                The height in pixels of the generated video.
            width (`int`, *optional*, defaults to `self.unet.config.sample_size * self.vae_scale_factor`):
                The width in pixels of the generated video.
            num_inference_steps (`int`, *optional*, defaults to 50):
                The number of denoising steps. More denoising steps usually lead to a higher quality videos 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.
            sigmas (`List[float]`, *optional*):
                Custom sigmas to use for the denoising process with schedulers which support a `sigmas` argument in
                their `set_timesteps` method. If not defined, the default behavior when `num_inference_steps` is passed
                will be used.
            strength (`float`, *optional*, defaults to 0.8):
                Higher strength leads to more differences between original video and generated video.
            guidance_scale (`float`, *optional*, defaults to 7.5):
                A higher guidance scale value encourages the model to generate images closely linked to the text
                `prompt` at the expense of lower image quality. Guidance scale is enabled when `guidance_scale > 1`.
            negative_prompt (`str` or `List[str]`, *optional*):
                The prompt or prompts to guide what to not include in image generation. If not defined, you need to
                pass `negative_prompt_embeds` instead. Ignored when not using guidance (`guidance_scale < 1`).
            eta (`float`, *optional*, defaults to 0.0):
                Corresponds to parameter eta (η) from the [DDIM](https://arxiv.org/abs/2010.02502) paper. Only applies
                to the [`~schedulers.DDIMScheduler`], and is ignored in other schedulers.
            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 video
                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`. Latents should be of shape
                `(batch_size, num_channel, num_frames, height, width)`.
            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 negative text embeddings. Can be used to easily tweak text inputs (prompt weighting). If
                not provided, `negative_prompt_embeds` are generated from the `negative_prompt` input argument.
            ip_adapter_image: (`PipelineImageInput`, *optional*):
                Optional image input to work with IP Adapters.
            ip_adapter_image_embeds (`List[torch.Tensor]`, *optional*):
                Pre-generated image embeddings for IP-Adapter. It should be a list of length same as number of
                IP-adapters. Each element should be a tensor of shape `(batch_size, num_images, emb_dim)`. It should
                contain the negative image embedding if `do_classifier_free_guidance` is set to `True`. If not
                provided, embeddings are computed from the `ip_adapter_image` input argument.
            output_type (`str`, *optional*, defaults to `"pil"`):
                The output format of the generated video. Choose between `torch.Tensor`, `PIL.Image` or `np.array`.
            return_dict (`bool`, *optional*, defaults to `True`):
                Whether or not to return a [`AnimateDiffPipelineOutput`] instead of a plain tuple.
            cross_attention_kwargs (`dict`, *optional*):
                A kwargs dictionary that if specified is passed along to the [`AttentionProcessor`] as defined in
                [`self.processor`](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py).
            clip_skip (`int`, *optional*):
                Number of layers to be skipped from CLIP while computing the prompt embeddings. A value of 1 means that
                the output of the pre-final layer will be used for computing the prompt embeddings.
            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.
            decode_chunk_size (`int`, defaults to `16`):
                The number of frames to decode at a time when calling `decode_latents` method.

        Examples:

        Returns:
            [`pipelines.animatediff.pipeline_output.AnimateDiffPipelineOutput`] or `tuple`:
                If `return_dict` is `True`, [`pipelines.animatediff.pipeline_output.AnimateDiffPipelineOutput`] is
                returned, otherwise a `tuple` is returned where the first element is a list with the generated frames.
        r(   )r   r   r   r   r   rM   rN   r   r0   r   r   r   Nr   scale)rM   rN   r^   r_   )r   r   r#   r   r   r   )r   r   r   r   r   r   rj   r8   r,   r0   r   r   )total)encoder_hidden_statesr   added_cond_kwargsr0   rM   rN   latent)r   r   )frames)6rR   rY   sample_sizerS   r   r   r   r   rV   rn   ro   rC   rp   _execution_devicer   getr   r   r_   rt   r   r   rH   rD   r   r|   r[   preprocess_videor   ry   rj   in_channelsr   r   free_init_enabled_free_init_num_itersr   _apply_free_initr   r   progress_barr   scale_model_inputr*   r   r   prev_samplelocalspopupdater   postprocess_videomaybe_free_model_hooksr)   )0r\   r   r   r   r   r7   r9   r:   r   r   r   r   r   r,   r0   rM   rN   r   r   r   r   r   r_   r   r   r   r   r8   text_encoder_lora_scaler   latent_timestepr   r   r  num_free_init_itersfree_init_iternum_warmup_stepsr  r   tlatent_model_input
noise_prednoise_pred_uncondnoise_pred_textcallback_kwargsr   callback_outputsvideo_tensorr4   r4   r5   __call__  s   s


	

	
6
&
z(AnimateDiffVideoToVideoPipeline.__call__)NN)NNNNNr   )r   )NNNNNNNN)Nr   )5__name__
__module____qualname____doc__model_cpu_offload_seq_optional_componentsr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   rU   r   rt   r   r   r   r   r   r   r   r   r   r   r   r   propertyr   r_   r   r   r   no_gradr   r   rn   	Generatorboolr   r   r   r#  __classcell__r4   r4   r]   r5   rI      s4   	*	

 
8-	
H
V







	
rI   )Nr*   )NNNN)Cr=   typingr   r   r   r   r   r   rt   transformersr   r	   r
   r   image_processorr   loadersr   r   r   modelsr   r   r   r   models.lorar   models.unets.unet_motion_modelr   
schedulersr   r   r   r   r   r   utilsr   r   r   r    utils.torch_utilsr!   r[   r"   free_init_utilsr$   free_noise_utilsr%   pipeline_utilsr&   r'   pipeline_outputr)   
get_loggerr$  rw   EXAMPLE_DOC_STRINGr   r,  rn   r6   r   r8   r   rH   rI   r4   r4   r4   r5   <module>   sb     
8




;