|
| 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 HunyuanDiT2DModel |
| 21 | +from diffusers.utils.testing_utils import ( |
| 22 | + enable_full_determinism, |
| 23 | + torch_device, |
| 24 | +) |
| 25 | + |
| 26 | +from ..test_modeling_common import ModelTesterMixin |
| 27 | + |
| 28 | + |
| 29 | +enable_full_determinism() |
| 30 | + |
| 31 | + |
| 32 | +class HunyuanDiTTests(ModelTesterMixin, unittest.TestCase): |
| 33 | + model_class = HunyuanDiT2DModel |
| 34 | + main_input_name = "hidden_states" |
| 35 | + |
| 36 | + @property |
| 37 | + def dummy_input(self): |
| 38 | + batch_size = 2 |
| 39 | + num_channels = 4 |
| 40 | + height = width = 8 |
| 41 | + embedding_dim = 8 |
| 42 | + sequence_length = 4 |
| 43 | + sequence_length_t5 = 4 |
| 44 | + |
| 45 | + hidden_states = torch.randn((batch_size, num_channels, height, width)).to(torch_device) |
| 46 | + encoder_hidden_states = torch.randn((batch_size, sequence_length, embedding_dim)).to(torch_device) |
| 47 | + text_embedding_mask = torch.ones(size=(batch_size, sequence_length)).to(torch_device) |
| 48 | + encoder_hidden_states_t5 = torch.randn((batch_size, sequence_length_t5, embedding_dim)).to(torch_device) |
| 49 | + text_embedding_mask_t5 = torch.ones(size=(batch_size, sequence_length_t5)).to(torch_device) |
| 50 | + timestep = torch.randint(0, 1000, size=(batch_size,), dtype=encoder_hidden_states.dtype).to(torch_device) |
| 51 | + |
| 52 | + original_size = [1024, 1024] |
| 53 | + target_size = [16, 16] |
| 54 | + crops_coords_top_left = [0, 0] |
| 55 | + add_time_ids = list(original_size + target_size + crops_coords_top_left) |
| 56 | + add_time_ids = torch.tensor([add_time_ids, add_time_ids], dtype=encoder_hidden_states.dtype).to(torch_device) |
| 57 | + style = torch.zeros(size=(batch_size,), dtype=int).to(torch_device) |
| 58 | + image_rotary_emb = [ |
| 59 | + torch.ones(size=(1, 8), dtype=encoder_hidden_states.dtype), |
| 60 | + torch.zeros(size=(1, 8), dtype=encoder_hidden_states.dtype), |
| 61 | + ] |
| 62 | + |
| 63 | + return { |
| 64 | + "hidden_states": hidden_states, |
| 65 | + "encoder_hidden_states": encoder_hidden_states, |
| 66 | + "text_embedding_mask": text_embedding_mask, |
| 67 | + "encoder_hidden_states_t5": encoder_hidden_states_t5, |
| 68 | + "text_embedding_mask_t5": text_embedding_mask_t5, |
| 69 | + "timestep": timestep, |
| 70 | + "image_meta_size": add_time_ids, |
| 71 | + "style": style, |
| 72 | + "image_rotary_emb": image_rotary_emb, |
| 73 | + } |
| 74 | + |
| 75 | + @property |
| 76 | + def input_shape(self): |
| 77 | + return (4, 8, 8) |
| 78 | + |
| 79 | + @property |
| 80 | + def output_shape(self): |
| 81 | + return (8, 8, 8) |
| 82 | + |
| 83 | + def prepare_init_args_and_inputs_for_common(self): |
| 84 | + init_dict = { |
| 85 | + "sample_size": 8, |
| 86 | + "patch_size": 2, |
| 87 | + "in_channels": 4, |
| 88 | + "num_layers": 1, |
| 89 | + "attention_head_dim": 8, |
| 90 | + "num_attention_heads": 2, |
| 91 | + "cross_attention_dim": 8, |
| 92 | + "cross_attention_dim_t5": 8, |
| 93 | + "pooled_projection_dim": 4, |
| 94 | + "hidden_size": 16, |
| 95 | + "text_len": 4, |
| 96 | + "text_len_t5": 4, |
| 97 | + "activation_fn": "gelu-approximate", |
| 98 | + } |
| 99 | + inputs_dict = self.dummy_input |
| 100 | + return init_dict, inputs_dict |
| 101 | + |
| 102 | + def test_output(self): |
| 103 | + super().test_output( |
| 104 | + expected_output_shape=(self.dummy_input[self.main_input_name].shape[0],) + self.output_shape |
| 105 | + ) |
| 106 | + |
| 107 | + @unittest.skip("HunyuanDIT use a custom processor HunyuanAttnProcessor2_0") |
| 108 | + def test_set_xformers_attn_processor_for_determinism(self): |
| 109 | + pass |
| 110 | + |
| 111 | + @unittest.skip("HunyuanDIT use a custom processor HunyuanAttnProcessor2_0") |
| 112 | + def test_set_attn_processor_for_determinism(self): |
| 113 | + pass |
0 commit comments