-
Notifications
You must be signed in to change notification settings - Fork 6k
[Single File] Add single file support for Wan T2V/I2V #10991
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
tests/single_file/test_model_wan_autoencoder_single_file.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# coding=utf-8 | ||
# Copyright 2025 HuggingFace Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import gc | ||
import unittest | ||
|
||
from diffusers import ( | ||
AutoencoderKLWan, | ||
) | ||
from diffusers.utils.testing_utils import ( | ||
backend_empty_cache, | ||
enable_full_determinism, | ||
require_torch_accelerator, | ||
torch_device, | ||
) | ||
|
||
|
||
enable_full_determinism() | ||
|
||
|
||
@require_torch_accelerator | ||
class AutoencoderKLWanSingleFileTests(unittest.TestCase): | ||
model_class = AutoencoderKLWan | ||
ckpt_path = ( | ||
"https://huggingface.co/Comfy-Org/Wan_2.1_ComfyUI_repackaged/blob/main/split_files/vae/wan_2.1_vae.safetensors" | ||
) | ||
repo_id = "Wan-AI/Wan2.1-T2V-1.3B-Diffusers" | ||
|
||
def setUp(self): | ||
super().setUp() | ||
gc.collect() | ||
backend_empty_cache(torch_device) | ||
|
||
def tearDown(self): | ||
super().tearDown() | ||
gc.collect() | ||
backend_empty_cache(torch_device) | ||
|
||
def test_single_file_components(self): | ||
model = self.model_class.from_pretrained(self.repo_id, subfolder="vae") | ||
model_single_file = self.model_class.from_single_file(self.ckpt_path) | ||
|
||
PARAMS_TO_IGNORE = ["torch_dtype", "_name_or_path", "_use_default_values", "_diffusers_version"] | ||
for param_name, param_value in model_single_file.config.items(): | ||
if param_name in PARAMS_TO_IGNORE: | ||
continue | ||
assert ( | ||
model.config[param_name] == param_value | ||
), f"{param_name} differs between single file loading and pretrained loading" |
93 changes: 93 additions & 0 deletions
93
tests/single_file/test_model_wan_transformer3d_single_file.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# coding=utf-8 | ||
# Copyright 2025 HuggingFace Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import gc | ||
import unittest | ||
|
||
import torch | ||
|
||
from diffusers import ( | ||
WanTransformer3DModel, | ||
) | ||
from diffusers.utils.testing_utils import ( | ||
backend_empty_cache, | ||
enable_full_determinism, | ||
require_big_gpu_with_torch_cuda, | ||
require_torch_accelerator, | ||
torch_device, | ||
) | ||
|
||
|
||
enable_full_determinism() | ||
|
||
|
||
@require_torch_accelerator | ||
class WanTransformer3DModelText2VideoSingleFileTest(unittest.TestCase): | ||
model_class = 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" | ||
repo_id = "Wan-AI/Wan2.1-T2V-1.3B-Diffusers" | ||
|
||
def setUp(self): | ||
super().setUp() | ||
gc.collect() | ||
backend_empty_cache(torch_device) | ||
|
||
def tearDown(self): | ||
super().tearDown() | ||
gc.collect() | ||
backend_empty_cache(torch_device) | ||
|
||
def test_single_file_components(self): | ||
model = self.model_class.from_pretrained(self.repo_id, subfolder="transformer") | ||
model_single_file = self.model_class.from_single_file(self.ckpt_path) | ||
|
||
PARAMS_TO_IGNORE = ["torch_dtype", "_name_or_path", "_use_default_values", "_diffusers_version"] | ||
for param_name, param_value in model_single_file.config.items(): | ||
if param_name in PARAMS_TO_IGNORE: | ||
continue | ||
assert ( | ||
model.config[param_name] == param_value | ||
), f"{param_name} differs between single file loading and pretrained loading" | ||
|
||
|
||
@require_big_gpu_with_torch_cuda | ||
@require_torch_accelerator | ||
class WanTransformer3DModelImage2VideoSingleFileTest(unittest.TestCase): | ||
model_class = WanTransformer3DModel | ||
ckpt_path = "https://huggingface.co/Comfy-Org/Wan_2.1_ComfyUI_repackaged/blob/main/split_files/diffusion_models/wan2.1_i2v_480p_14B_fp8_e4m3fn.safetensors" | ||
repo_id = "Wan-AI/Wan2.1-I2V-14B-480P-Diffusers" | ||
torch_dtype = torch.float8_e4m3fn | ||
|
||
def setUp(self): | ||
super().setUp() | ||
gc.collect() | ||
backend_empty_cache(torch_device) | ||
|
||
def tearDown(self): | ||
super().tearDown() | ||
gc.collect() | ||
backend_empty_cache(torch_device) | ||
|
||
def test_single_file_components(self): | ||
model = self.model_class.from_pretrained(self.repo_id, subfolder="transformer", torch_dtype=self.torch_dtype) | ||
model_single_file = self.model_class.from_single_file(self.ckpt_path, torch_dtype=self.torch_dtype) | ||
|
||
PARAMS_TO_IGNORE = ["torch_dtype", "_name_or_path", "_use_default_values", "_diffusers_version"] | ||
for param_name, param_value in model_single_file.config.items(): | ||
if param_name in PARAMS_TO_IGNORE: | ||
continue | ||
assert ( | ||
model.config[param_name] == param_value | ||
), f"{param_name} differs between single file loading and pretrained loading" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yiyixuxu Discovered an issue when running the slow test for the transformer. The diffusers implementation has this extra
norm_added_q
key which the original does not. When converting from the original checkpoint there is no weight to assign to this norm, so it remain a meta tensor, so we run into an error when setting the model to a device.Removing this, and then adding
norm_added_q
to the_keys_to_ignore_on_load_unexpected
in the transformer so that the warning about extra keys in the Diffusers version is suppressed.Ideal solution is to update the weights in the model repo, but that could take time. This is a fix for the meantime
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good!