|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2024 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 unittest |
| 17 | + |
| 18 | +import torch |
| 19 | + |
| 20 | +from diffusers import AutoencoderKLHunyuanVideo |
| 21 | +from diffusers.utils.testing_utils import ( |
| 22 | + enable_full_determinism, |
| 23 | + floats_tensor, |
| 24 | + torch_device, |
| 25 | +) |
| 26 | + |
| 27 | +from ..test_modeling_common import ModelTesterMixin, UNetTesterMixin |
| 28 | + |
| 29 | + |
| 30 | +enable_full_determinism() |
| 31 | + |
| 32 | + |
| 33 | +class AutoencoderKLHunyuanVideoTests(ModelTesterMixin, UNetTesterMixin, unittest.TestCase): |
| 34 | + model_class = AutoencoderKLHunyuanVideo |
| 35 | + main_input_name = "sample" |
| 36 | + base_precision = 1e-2 |
| 37 | + |
| 38 | + def get_autoencoder_kl_hunyuan_video_config(self): |
| 39 | + return { |
| 40 | + "in_channels": 3, |
| 41 | + "out_channels": 3, |
| 42 | + "latent_channels": 4, |
| 43 | + "down_block_types": ( |
| 44 | + "HunyuanVideoDownBlock3D", |
| 45 | + "HunyuanVideoDownBlock3D", |
| 46 | + ), |
| 47 | + "up_block_types": ( |
| 48 | + "HunyuanVideoUpBlock3D", |
| 49 | + "HunyuanVideoUpBlock3D", |
| 50 | + ), |
| 51 | + "block_out_channels": (8, 8, 8, 8), |
| 52 | + "layers_per_block": 1, |
| 53 | + "act_fn": "silu", |
| 54 | + "norm_num_groups": 4, |
| 55 | + "scaling_factor": 0.476986, |
| 56 | + "spatial_compression_ratio": 8, |
| 57 | + "temporal_compression_ratio": 4, |
| 58 | + "mid_block_add_attention": True, |
| 59 | + } |
| 60 | + |
| 61 | + @property |
| 62 | + def dummy_input(self): |
| 63 | + batch_size = 2 |
| 64 | + num_frames = 9 |
| 65 | + num_channels = 3 |
| 66 | + sizes = (16, 16) |
| 67 | + |
| 68 | + image = floats_tensor((batch_size, num_channels, num_frames) + sizes).to(torch_device) |
| 69 | + |
| 70 | + return {"sample": image} |
| 71 | + |
| 72 | + @property |
| 73 | + def input_shape(self): |
| 74 | + return (3, 9, 16, 16) |
| 75 | + |
| 76 | + @property |
| 77 | + def output_shape(self): |
| 78 | + return (3, 9, 16, 16) |
| 79 | + |
| 80 | + def prepare_init_args_and_inputs_for_common(self): |
| 81 | + init_dict = self.get_autoencoder_kl_hunyuan_video_config() |
| 82 | + inputs_dict = self.dummy_input |
| 83 | + return init_dict, inputs_dict |
| 84 | + |
| 85 | + def test_enable_disable_tiling(self): |
| 86 | + init_dict, inputs_dict = self.prepare_init_args_and_inputs_for_common() |
| 87 | + |
| 88 | + torch.manual_seed(0) |
| 89 | + model = self.model_class(**init_dict).to(torch_device) |
| 90 | + |
| 91 | + inputs_dict.update({"return_dict": False}) |
| 92 | + |
| 93 | + torch.manual_seed(0) |
| 94 | + output_without_tiling = model(**inputs_dict, generator=torch.manual_seed(0))[0] |
| 95 | + |
| 96 | + torch.manual_seed(0) |
| 97 | + model.enable_tiling() |
| 98 | + output_with_tiling = model(**inputs_dict, generator=torch.manual_seed(0))[0] |
| 99 | + |
| 100 | + self.assertLess( |
| 101 | + (output_without_tiling.detach().cpu().numpy() - output_with_tiling.detach().cpu().numpy()).max(), |
| 102 | + 0.5, |
| 103 | + "VAE tiling should not affect the inference results", |
| 104 | + ) |
| 105 | + |
| 106 | + torch.manual_seed(0) |
| 107 | + model.disable_tiling() |
| 108 | + output_without_tiling_2 = model(**inputs_dict, generator=torch.manual_seed(0))[0] |
| 109 | + |
| 110 | + self.assertEqual( |
| 111 | + output_without_tiling.detach().cpu().numpy().all(), |
| 112 | + output_without_tiling_2.detach().cpu().numpy().all(), |
| 113 | + "Without tiling outputs should match with the outputs when tiling is manually disabled.", |
| 114 | + ) |
| 115 | + |
| 116 | + def test_enable_disable_slicing(self): |
| 117 | + init_dict, inputs_dict = self.prepare_init_args_and_inputs_for_common() |
| 118 | + |
| 119 | + torch.manual_seed(0) |
| 120 | + model = self.model_class(**init_dict).to(torch_device) |
| 121 | + |
| 122 | + inputs_dict.update({"return_dict": False}) |
| 123 | + |
| 124 | + torch.manual_seed(0) |
| 125 | + output_without_slicing = model(**inputs_dict, generator=torch.manual_seed(0))[0] |
| 126 | + |
| 127 | + torch.manual_seed(0) |
| 128 | + model.enable_slicing() |
| 129 | + output_with_slicing = model(**inputs_dict, generator=torch.manual_seed(0))[0] |
| 130 | + |
| 131 | + self.assertLess( |
| 132 | + (output_without_slicing.detach().cpu().numpy() - output_with_slicing.detach().cpu().numpy()).max(), |
| 133 | + 0.5, |
| 134 | + "VAE slicing should not affect the inference results", |
| 135 | + ) |
| 136 | + |
| 137 | + torch.manual_seed(0) |
| 138 | + model.disable_slicing() |
| 139 | + output_without_slicing_2 = model(**inputs_dict, generator=torch.manual_seed(0))[0] |
| 140 | + |
| 141 | + self.assertEqual( |
| 142 | + output_without_slicing.detach().cpu().numpy().all(), |
| 143 | + output_without_slicing_2.detach().cpu().numpy().all(), |
| 144 | + "Without slicing outputs should match with the outputs when slicing is manually disabled.", |
| 145 | + ) |
| 146 | + |
| 147 | + def test_gradient_checkpointing_is_applied(self): |
| 148 | + expected_set = { |
| 149 | + "HunyuanVideoDecoder3D", |
| 150 | + "HunyuanVideoDownBlock3D", |
| 151 | + "HunyuanVideoEncoder3D", |
| 152 | + "HunyuanVideoMidBlock3D", |
| 153 | + "HunyuanVideoUpBlock3D", |
| 154 | + } |
| 155 | + super().test_gradient_checkpointing_is_applied(expected_set=expected_set) |
| 156 | + |
| 157 | + @unittest.skip("Unsupported test.") |
| 158 | + def test_outputs_equivalence(self): |
| 159 | + pass |
0 commit comments