VideoModelStudio / docs /diffusers /Using Diffusers for Wan.md
jbilcke-hf's picture
jbilcke-hf HF staff
working on the new Preview tab
c8cb798

A newer version of the Gradio SDK is available: 5.23.0

Upgrade

Wan

Wan 2.1 by the Alibaba Wan Team.

Make sure to check out the Schedulers guide to learn how to explore the tradeoff between scheduler speed and quality, and see the reuse components across pipelines section to learn how to efficiently load the same components into multiple pipelines.

Recommendations for inference:

  • VAE in torch.float32 for better decoding quality.
  • num_frames should be of the form 4 * k + 1, for example 49 or 81.
  • For smaller resolution videos, try lower values of shift (between 2.0 to 5.0) in the Scheduler. For larger resolution videos, try higher values (between 7.0 and 12.0). The default value is 3.0 for Wan.

Using a custom scheduler

Wan can be used with many different schedulers, each with their own benefits regarding speed and generation quality. By default, Wan uses the UniPCMultistepScheduler(prediction_type="flow_prediction", use_flow_sigmas=True, flow_shift=3.0) scheduler. You can use a different scheduler as follows:

Copied

from diffusers import FlowMatchEulerDiscreteScheduler, UniPCMultistepScheduler, WanPipeline

scheduler_a = FlowMatchEulerDiscreteScheduler(shift=5.0) scheduler_b = UniPCMultistepScheduler(prediction_type="flow_prediction", use_flow_sigmas=True, flow_shift=4.0)

pipe = WanPipeline.from_pretrained("Wan-AI/Wan2.1-T2V-1.3B-Diffusers", scheduler=<CUSTOM_SCHEDULER_HERE>)

# or, pipe.scheduler = <CUSTOM_SCHEDULER_HERE>

Using single file loading with Wan

The WanTransformer3DModel and AutoencoderKLWan models support loading checkpoints in their original format via the from_single_file loading method.

Copied

import torch from diffusers import WanPipeline, WanTransformer3DModel

ckpt_path = "https://huggingface.co/Comfy-Org/Wan\_2.1\_ComfyUI\_repackaged/blob/main/split\_files/diffusion\_models/wan2.1\_t2v\_1.3B\_bf16.safetensors" transformer = WanTransformer3DModel.from_single_file(ckpt_path, torch_dtype=torch.bfloat16)

pipe = WanPipeline.from_pretrained("Wan-AI/Wan2.1-T2V-1.3B-Diffusers", transformer=transformer)

WanPipeline

class diffusers.WanPipeline

< source >

( tokenizer: AutoTokenizertext_encoder: UMT5EncoderModeltransformer: WanTransformer3DModelvae: AutoencoderKLWanscheduler: FlowMatchEulerDiscreteScheduler )

Parameters

  • tokenizer (T5Tokenizer) β€” Tokenizer from T5, specifically the google/umt5-xxl variant.
  • text_encoder (T5EncoderModel) β€” T5, specifically the google/umt5-xxl variant.
  • transformer (WanTransformer3DModel) β€” 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.

Pipeline for text-to-video generation using Wan.

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.).

__call__

< source >

( prompt: typing.Union[str, typing.List[str]] = Nonenegative_prompt: typing.Union[str, typing.List[str]] = Noneheight: int = 480width: int = 832num_frames: int = 81num_inference_steps: int = 50guidance_scale: float = 5.0num_videos_per_prompt: typing.Optional[int] = 1generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = Nonelatents: typing.Optional[torch.Tensor] = Noneprompt_embeds: typing.Optional[torch.Tensor] = Nonenegative_prompt_embeds: typing.Optional[torch.Tensor] = Noneoutput_type: typing.Optional[str] = 'np'return_dict: bool = Trueattention_kwargs: typing.Optional[typing.Dict[str, typing.Any]] = Nonecallback_on_step_end: typing.Union[typing.Callable[[int, int, typing.Dict], NoneType], diffusers.callbacks.PipelineCallback, diffusers.callbacks.MultiPipelineCallbacks, NoneType] = Nonecallback_on_step_end_tensor_inputs: typing.List[str] = ['latents']max_sequence_length: int = 512 ) β†’ export const metadata = 'undefined';~WanPipelineOutput or tuple

Expand 16 parameters

Parameters

  • 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 480) β€” The height in pixels of the generated image.
  • width (int, defaults to 832) β€” The width in pixels of the generated image.
  • num_frames (int, defaults to 81) β€” The number of frames in the generated video.
  • num_inference_steps (int, defaults to 50) β€” 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 5.0) β€” Guidance scale as defined in Classifier-Free Diffusion Guidance. guidance_scale is defined as w of equation 2. of Imagen Paper. 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.
  • 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 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.
  • output_type (str, optional, defaults to "pil") β€” 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.
  • 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.
  • autocast_dtype (torch.dtype, optional, defaults to torch.bfloat16) β€” The dtype to use for the torch.amp.autocast.

Returns

export const metadata = 'undefined';

~WanPipelineOutput or tuple

export const metadata = 'undefined';

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 bools indicating whether the corresponding generated image contains β€œnot-safe-for-work” (nsfw) content.

The call function to the pipeline for generation.

Examples:

Copied

>>> import torch >>> from diffusers.utils import export_to_video >>> from diffusers import AutoencoderKLWan, WanPipeline >>> from diffusers.schedulers.scheduling_unipc_multistep import UniPCMultistepScheduler

>>> # Available models: Wan-AI/Wan2.1-T2V-14B-Diffusers, Wan-AI/Wan2.1-T2V-1.3B-Diffusers >>> model_id = "Wan-AI/Wan2.1-T2V-14B-Diffusers" >>> vae = AutoencoderKLWan.from_pretrained(model_id, subfolder="vae", torch_dtype=torch.float32) >>> pipe = WanPipeline.from_pretrained(model_id, vae=vae, torch_dtype=torch.bfloat16) >>> flow_shift = 5.0 # 5.0 for 720P, 3.0 for 480P >>> pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config, flow_shift=flow_shift) >>> pipe.to("cuda")

>>> prompt = "A cat and a dog baking a cake together in a kitchen. The cat is carefully measuring flour, while the dog is stirring the batter with a wooden spoon. The kitchen is cozy, with sunlight streaming through the window." >>> negative_prompt = "Bright tones, overexposed, static, blurred details, subtitles, style, works, paintings, images, static, overall gray, worst quality, low quality, JPEG compression residue, ugly, incomplete, extra fingers, poorly drawn hands, poorly drawn faces, deformed, disfigured, misshapen limbs, fused fingers, still picture, messy background, three legs, many people in the background, walking backwards"

>>> output = pipe( ... prompt=prompt, ... negative_prompt=negative_prompt, ... height=720, ... width=1280, ... num_frames=81, ... guidance_scale=5.0, ... ).frames[0] >>> export_to_video(output, "output.mp4", fps=16)

encode_prompt

< source >

( prompt: typing.Union[str, typing.List[str]]negative_prompt: typing.Union[str, typing.List[str], NoneType] = Nonedo_classifier_free_guidance: bool = Truenum_videos_per_prompt: int = 1prompt_embeds: typing.Optional[torch.Tensor] = Nonenegative_prompt_embeds: typing.Optional[torch.Tensor] = Nonemax_sequence_length: int = 226device: typing.Optional[torch.device] = Nonedtype: typing.Optional[torch.dtype] = None )

Parameters

  • 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

Encodes the prompt into text encoder hidden states.

WanImageToVideoPipeline

class diffusers.WanImageToVideoPipeline

< source >

( tokenizer: AutoTokenizertext_encoder: UMT5EncoderModelimage_encoder: CLIPVisionModelimage_processor: CLIPImageProcessortransformer: WanTransformer3DModelvae: AutoencoderKLWanscheduler: FlowMatchEulerDiscreteScheduler )

Parameters

  • tokenizer (T5Tokenizer) β€” Tokenizer from T5, specifically the google/umt5-xxl variant.
  • text_encoder (T5EncoderModel) β€” T5, specifically the google/umt5-xxl variant.
  • image_encoder (CLIPVisionModel) β€” CLIP, specifically the clip-vit-huge-patch14 variant.
  • transformer (WanTransformer3DModel) β€” 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.

Pipeline for image-to-video generation using Wan.

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.).

__call__

< source >

( image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, typing.List[PIL.Image.Image], typing.List[numpy.ndarray], typing.List[torch.Tensor]]prompt: typing.Union[str, typing.List[str]] = Nonenegative_prompt: typing.Union[str, typing.List[str]] = Noneheight: int = 480width: int = 832num_frames: int = 81num_inference_steps: int = 50guidance_scale: float = 5.0num_videos_per_prompt: typing.Optional[int] = 1generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = Nonelatents: typing.Optional[torch.Tensor] = Noneprompt_embeds: typing.Optional[torch.Tensor] = Nonenegative_prompt_embeds: typing.Optional[torch.Tensor] = Noneoutput_type: typing.Optional[str] = 'np'return_dict: bool = Trueattention_kwargs: typing.Optional[typing.Dict[str, typing.Any]] = Nonecallback_on_step_end: typing.Union[typing.Callable[[int, int, typing.Dict], NoneType], diffusers.callbacks.PipelineCallback, diffusers.callbacks.MultiPipelineCallbacks, NoneType] = Nonecallback_on_step_end_tensor_inputs: typing.List[str] = ['latents']max_sequence_length: int = 512 ) β†’ export const metadata = 'undefined';~WanPipelineOutput or tuple

Expand 20 parameters

Parameters

  • image (PipelineImageInput) β€” The input image to condition the generation on. Must be an image, a list of images or a torch.Tensor.
  • 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).
  • height (int, defaults to 480) β€” The height of the generated video.
  • width (int, defaults to 832) β€” The width of the generated video.
  • num_frames (int, defaults to 81) β€” The number of frames in the generated video.
  • num_inference_steps (int, defaults to 50) β€” 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 5.0) β€” Guidance scale as defined in Classifier-Free Diffusion Guidance. guidance_scale is defined as w of equation 2. of Imagen Paper. 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.
  • 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 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.
  • output_type (str, optional, defaults to "pil") β€” 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.
  • 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, optional, defaults to 512) β€” The maximum sequence length of the prompt.
  • shift (float, optional, defaults to 5.0) β€” The shift of the flow.
  • autocast_dtype (torch.dtype, optional, defaults to torch.bfloat16) β€” The dtype to use for the torch.amp.autocast.

Returns

export const metadata = 'undefined';

~WanPipelineOutput or tuple

export const metadata = 'undefined';

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 bools indicating whether the corresponding generated image contains β€œnot-safe-for-work” (nsfw) content.

The call function to the pipeline for generation.

Examples:

Copied

>>> import torch >>> import numpy as np >>> from diffusers import AutoencoderKLWan, WanImageToVideoPipeline >>> from diffusers.utils import export_to_video, load_image >>> from transformers import CLIPVisionModel

>>> # Available models: Wan-AI/Wan2.1-I2V-14B-480P-Diffusers, Wan-AI/Wan2.1-I2V-14B-720P-Diffusers >>> model_id = "Wan-AI/Wan2.1-I2V-14B-480P-Diffusers" >>> image_encoder = CLIPVisionModel.from_pretrained( ... model_id, subfolder="image_encoder", torch_dtype=torch.float32 ... ) >>> vae = AutoencoderKLWan.from_pretrained(model_id, subfolder="vae", torch_dtype=torch.float32) >>> pipe = WanImageToVideoPipeline.from_pretrained( ... model_id, vae=vae, image_encoder=image_encoder, torch_dtype=torch.bfloat16 ... ) >>> pipe.to("cuda")

>>> image = load_image( ... "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/astronaut.jpg" ... ) >>> max_area = 480 * 832 >>> aspect_ratio = image.height / image.width >>> mod_value = pipe.vae_scale_factor_spatial * pipe.transformer.config.patch_size[1] >>> height = round(np.sqrt(max_area * aspect_ratio)) // mod_value * mod_value >>> width = round(np.sqrt(max_area / aspect_ratio)) // mod_value * mod_value >>> image = image.resize((width, height)) >>> 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." ... ) >>> negative_prompt = "Bright tones, overexposed, static, blurred details, subtitles, style, works, paintings, images, static, overall gray, worst quality, low quality, JPEG compression residue, ugly, incomplete, extra fingers, poorly drawn hands, poorly drawn faces, deformed, disfigured, misshapen limbs, fused fingers, still picture, messy background, three legs, many people in the background, walking backwards"

>>> output = pipe( ... image=image, ... prompt=prompt, ... negative_prompt=negative_prompt, ... height=height, ... width=width, ... num_frames=81, ... guidance_scale=5.0, ... ).frames[0] >>> export_to_video(output, "output.mp4", fps=16)

encode_prompt

< source >

( prompt: typing.Union[str, typing.List[str]]negative_prompt: typing.Union[str, typing.List[str], NoneType] = Nonedo_classifier_free_guidance: bool = Truenum_videos_per_prompt: int = 1prompt_embeds: typing.Optional[torch.Tensor] = Nonenegative_prompt_embeds: typing.Optional[torch.Tensor] = Nonemax_sequence_length: int = 226device: typing.Optional[torch.device] = Nonedtype: typing.Optional[torch.dtype] = None )

Parameters

  • 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

Encodes the prompt into text encoder hidden states.

WanPipelineOutput

class diffusers.pipelines.wan.pipeline_output.WanPipelineOutput

< source >

( frames: Tensor )

Parameters

  • frames (torch.Tensor, np.ndarray, or List[List[PIL.Image.Image]]) β€” List of video outputs - It can be a nested list of length batch_size, with each sub-list containing denoised PIL image sequences of length num_frames. It can also be a NumPy array or Torch tensor of shape (batch_size, num_frames, channels, height, width).

Output class for Wan pipelines.

< > Update on GitHub

CogVideoX

←Value-guided sampling Wuerstchenβ†’