|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2025 HuggingFace Inc. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import gc |
| 17 | +import unittest |
| 18 | + |
| 19 | +import torch |
| 20 | + |
| 21 | +from diffusers import ( |
| 22 | + WanTransformer3DModel, |
| 23 | +) |
| 24 | +from diffusers.utils.testing_utils import ( |
| 25 | + backend_empty_cache, |
| 26 | + enable_full_determinism, |
| 27 | + require_big_gpu_with_torch_cuda, |
| 28 | + require_torch_accelerator, |
| 29 | + torch_device, |
| 30 | +) |
| 31 | + |
| 32 | + |
| 33 | +enable_full_determinism() |
| 34 | + |
| 35 | + |
| 36 | +@require_torch_accelerator |
| 37 | +class WanTransformer3DModelText2VideoSingleFileTest(unittest.TestCase): |
| 38 | + model_class = WanTransformer3DModel |
| 39 | + 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" |
| 40 | + repo_id = "Wan-AI/Wan2.1-T2V-1.3B-Diffusers" |
| 41 | + |
| 42 | + def setUp(self): |
| 43 | + super().setUp() |
| 44 | + gc.collect() |
| 45 | + backend_empty_cache(torch_device) |
| 46 | + |
| 47 | + def tearDown(self): |
| 48 | + super().tearDown() |
| 49 | + gc.collect() |
| 50 | + backend_empty_cache(torch_device) |
| 51 | + |
| 52 | + def test_single_file_components(self): |
| 53 | + model = self.model_class.from_pretrained(self.repo_id, subfolder="transformer") |
| 54 | + model_single_file = self.model_class.from_single_file(self.ckpt_path) |
| 55 | + |
| 56 | + PARAMS_TO_IGNORE = ["torch_dtype", "_name_or_path", "_use_default_values", "_diffusers_version"] |
| 57 | + for param_name, param_value in model_single_file.config.items(): |
| 58 | + if param_name in PARAMS_TO_IGNORE: |
| 59 | + continue |
| 60 | + assert ( |
| 61 | + model.config[param_name] == param_value |
| 62 | + ), f"{param_name} differs between single file loading and pretrained loading" |
| 63 | + |
| 64 | + |
| 65 | +@require_big_gpu_with_torch_cuda |
| 66 | +@require_torch_accelerator |
| 67 | +class WanTransformer3DModelImage2VideoSingleFileTest(unittest.TestCase): |
| 68 | + model_class = WanTransformer3DModel |
| 69 | + 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" |
| 70 | + repo_id = "Wan-AI/Wan2.1-I2V-14B-480P-Diffusers" |
| 71 | + torch_dtype = torch.float8_e4m3fn |
| 72 | + |
| 73 | + def setUp(self): |
| 74 | + super().setUp() |
| 75 | + gc.collect() |
| 76 | + backend_empty_cache(torch_device) |
| 77 | + |
| 78 | + def tearDown(self): |
| 79 | + super().tearDown() |
| 80 | + gc.collect() |
| 81 | + backend_empty_cache(torch_device) |
| 82 | + |
| 83 | + def test_single_file_components(self): |
| 84 | + model = self.model_class.from_pretrained(self.repo_id, subfolder="transformer", torch_dtype=self.torch_dtype) |
| 85 | + model_single_file = self.model_class.from_single_file(self.ckpt_path, torch_dtype=self.torch_dtype) |
| 86 | + |
| 87 | + PARAMS_TO_IGNORE = ["torch_dtype", "_name_or_path", "_use_default_values", "_diffusers_version"] |
| 88 | + for param_name, param_value in model_single_file.config.items(): |
| 89 | + if param_name in PARAMS_TO_IGNORE: |
| 90 | + continue |
| 91 | + assert ( |
| 92 | + model.config[param_name] == param_value |
| 93 | + ), f"{param_name} differs between single file loading and pretrained loading" |
0 commit comments