From 465e12e5b4cb9b3130e9a17cbd1b8ef4d9b4d564 Mon Sep 17 00:00:00 2001 From: Yupeng Shi Date: Wed, 5 Mar 2025 23:43:03 +0800 Subject: [PATCH 1/4] fix wan i2v pipeline bugs --- .../pipelines/wan/pipeline_wan_i2v.py | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/diffusers/pipelines/wan/pipeline_wan_i2v.py b/src/diffusers/pipelines/wan/pipeline_wan_i2v.py index 5dd80ce2d6ae..9c0066f9744b 100644 --- a/src/diffusers/pipelines/wan/pipeline_wan_i2v.py +++ b/src/diffusers/pipelines/wan/pipeline_wan_i2v.py @@ -19,7 +19,7 @@ import PIL import regex as re import torch -from transformers import AutoTokenizer, CLIPImageProcessor, CLIPVisionModelWithProjection, UMT5EncoderModel +from transformers import AutoTokenizer, CLIPImageProcessor, CLIPVisionModel, UMT5EncoderModel from ...callbacks import MultiPipelineCallbacks, PipelineCallback from ...image_processor import PipelineImageInput @@ -46,19 +46,27 @@ Examples: ```python >>> 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-1.3B-720P-Diffusers + >>> # 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, torch_dtype=torch.bfloat16) + >>> pipe = WanImageToVideoPipeline.from_pretrained(model_id, vae=vae, image_encoder=image_encoder, torch_dtype=torch.bfloat16) >>> pipe.to("cuda") - >>> height, width = 480, 832 >>> image = load_image( ... "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/astronaut.jpg" - ... ).resize((width, height)) + ... ) + >>> 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." @@ -66,9 +74,9 @@ >>> 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, num_frames=81, guidance_scale=5.0 + ... 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=15) + >>> export_to_video(output, "output.mp4", fps=16) ``` """ @@ -137,7 +145,7 @@ def __init__( self, tokenizer: AutoTokenizer, text_encoder: UMT5EncoderModel, - image_encoder: CLIPVisionModelWithProjection, + image_encoder: CLIPVisionModel, image_processor: CLIPImageProcessor, transformer: WanTransformer3DModel, vae: AutoencoderKLWan, @@ -204,7 +212,7 @@ def _get_t5_prompt_embeds( def encode_image(self, image: PipelineImageInput): image = self.image_processor(images=image, return_tensors="pt").to(self.device) image_embeds = self.image_encoder(**image, output_hidden_states=True) - return image_embeds.hidden_states[-1] + return image_embeds.hidden_states[-2] # Copied from diffusers.pipelines.wan.pipeline_wan.WanPipeline.encode_prompt def encode_prompt( From 46702877605dfc56965ab0c679049adb0cc877ef Mon Sep 17 00:00:00 2001 From: Yupeng Shi Date: Thu, 6 Mar 2025 00:40:26 +0800 Subject: [PATCH 2/4] update t2v example --- src/diffusers/pipelines/wan/pipeline_wan.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/diffusers/pipelines/wan/pipeline_wan.py b/src/diffusers/pipelines/wan/pipeline_wan.py index fd6135878492..35ed7ce28949 100644 --- a/src/diffusers/pipelines/wan/pipeline_wan.py +++ b/src/diffusers/pipelines/wan/pipeline_wan.py @@ -45,27 +45,31 @@ Examples: ```python >>> import torch - >>> from diffusers import AutoencoderKLWan, WanPipeline >>> 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) + >>> flow_shift = 5.0 # 5.0 for 720P, 3.0 for 480P + >>> scheduler = UniPCMultistepScheduler(prediction_type='flow_prediction', use_flow_sigmas=True, num_train_timesteps=1000, flow_shift=flow_shift) >>> pipe = WanPipeline.from_pretrained(model_id, vae=vae, torch_dtype=torch.bfloat16) + >>> pipe.scheduler = scheduler >>> pipe.to("cuda") - >>> prompt = "A cat walks on the grass, realistic" + >>> 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=480, - ... width=832, + ... height=720, + ... width=1280, ... num_frames=81, ... guidance_scale=5.0, - ... ).frames[0] - >>> export_to_video(output, "output.mp4", fps=15) + ... ).frames[0] + >>> export_to_video(output, "output.mp4", fps=16) ``` """ From efbdd3449b89115b6b47e5c6df6b0049a838b036 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Thu, 6 Mar 2025 02:50:49 +0000 Subject: [PATCH 3/4] Apply style fixes --- src/diffusers/pipelines/wan/pipeline_wan.py | 11 ++++++++--- src/diffusers/pipelines/wan/pipeline_wan_i2v.py | 16 +++++++++++++--- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/diffusers/pipelines/wan/pipeline_wan.py b/src/diffusers/pipelines/wan/pipeline_wan.py index 35ed7ce28949..665ab823dffc 100644 --- a/src/diffusers/pipelines/wan/pipeline_wan.py +++ b/src/diffusers/pipelines/wan/pipeline_wan.py @@ -52,8 +52,13 @@ >>> # 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) - >>> flow_shift = 5.0 # 5.0 for 720P, 3.0 for 480P - >>> scheduler = UniPCMultistepScheduler(prediction_type='flow_prediction', use_flow_sigmas=True, num_train_timesteps=1000, flow_shift=flow_shift) + >>> flow_shift = 5.0 # 5.0 for 720P, 3.0 for 480P + >>> scheduler = UniPCMultistepScheduler( + ... prediction_type="flow_prediction", + ... use_flow_sigmas=True, + ... num_train_timesteps=1000, + ... flow_shift=flow_shift, + ... ) >>> pipe = WanPipeline.from_pretrained(model_id, vae=vae, torch_dtype=torch.bfloat16) >>> pipe.scheduler = scheduler >>> pipe.to("cuda") @@ -68,7 +73,7 @@ ... width=1280, ... num_frames=81, ... guidance_scale=5.0, - ... ).frames[0] + ... ).frames[0] >>> export_to_video(output, "output.mp4", fps=16) ``` """ diff --git a/src/diffusers/pipelines/wan/pipeline_wan_i2v.py b/src/diffusers/pipelines/wan/pipeline_wan_i2v.py index 9c0066f9744b..24eb5586c34b 100644 --- a/src/diffusers/pipelines/wan/pipeline_wan_i2v.py +++ b/src/diffusers/pipelines/wan/pipeline_wan_i2v.py @@ -53,9 +53,13 @@ >>> # 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) + >>> 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 = WanImageToVideoPipeline.from_pretrained( + ... model_id, vae=vae, image_encoder=image_encoder, torch_dtype=torch.bfloat16 + ... ) >>> pipe.to("cuda") >>> image = load_image( @@ -74,7 +78,13 @@ >>> 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 + ... 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) ``` From 9bfacc4f1947dda9f5732ba975362dad018fdd70 Mon Sep 17 00:00:00 2001 From: YiYi Xu Date: Thu, 6 Mar 2025 18:42:15 -1000 Subject: [PATCH 4/4] Apply suggestions from code review --- src/diffusers/pipelines/wan/pipeline_wan.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/diffusers/pipelines/wan/pipeline_wan.py b/src/diffusers/pipelines/wan/pipeline_wan.py index 665ab823dffc..b1ac912969aa 100644 --- a/src/diffusers/pipelines/wan/pipeline_wan.py +++ b/src/diffusers/pipelines/wan/pipeline_wan.py @@ -52,15 +52,9 @@ >>> # 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) - >>> flow_shift = 5.0 # 5.0 for 720P, 3.0 for 480P - >>> scheduler = UniPCMultistepScheduler( - ... prediction_type="flow_prediction", - ... use_flow_sigmas=True, - ... num_train_timesteps=1000, - ... flow_shift=flow_shift, - ... ) >>> pipe = WanPipeline.from_pretrained(model_id, vae=vae, torch_dtype=torch.bfloat16) - >>> pipe.scheduler = scheduler + >>> 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."