|
| 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 torch |
| 17 | + |
| 18 | +from diffusers import GlmImageTransformer2DModel |
| 19 | +from diffusers.utils.torch_utils import randn_tensor |
| 20 | + |
| 21 | +from ...testing_utils import enable_full_determinism, torch_device |
| 22 | +from ..testing_utils import ( |
| 23 | + BaseModelTesterConfig, |
| 24 | + ModelTesterMixin, |
| 25 | + TrainingTesterMixin, |
| 26 | +) |
| 27 | + |
| 28 | + |
| 29 | +enable_full_determinism() |
| 30 | + |
| 31 | + |
| 32 | +class GlmImageTransformerTesterConfig(BaseModelTesterConfig): |
| 33 | + @property |
| 34 | + def model_class(self): |
| 35 | + return GlmImageTransformer2DModel |
| 36 | + |
| 37 | + @property |
| 38 | + def main_input_name(self) -> str: |
| 39 | + return "hidden_states" |
| 40 | + |
| 41 | + @property |
| 42 | + def output_shape(self) -> tuple: |
| 43 | + return (4, 8, 8) |
| 44 | + |
| 45 | + @property |
| 46 | + def input_shape(self) -> tuple: |
| 47 | + return (4, 8, 8) |
| 48 | + |
| 49 | + @property |
| 50 | + def generator(self): |
| 51 | + return torch.Generator("cpu").manual_seed(0) |
| 52 | + |
| 53 | + def get_init_dict(self) -> dict: |
| 54 | + return { |
| 55 | + "patch_size": 2, |
| 56 | + "in_channels": 4, |
| 57 | + "out_channels": 4, |
| 58 | + "num_layers": 1, |
| 59 | + "attention_head_dim": 8, |
| 60 | + "num_attention_heads": 2, |
| 61 | + "text_embed_dim": 32, |
| 62 | + "time_embed_dim": 16, |
| 63 | + "condition_dim": 8, |
| 64 | + "prior_vq_quantizer_codebook_size": 64, |
| 65 | + } |
| 66 | + |
| 67 | + def get_dummy_inputs(self, batch_size: int = 1) -> dict[str, torch.Tensor]: |
| 68 | + num_channels = 4 |
| 69 | + height = width = 8 |
| 70 | + sequence_length = 12 |
| 71 | + |
| 72 | + return { |
| 73 | + "hidden_states": randn_tensor( |
| 74 | + (batch_size, num_channels, height, width), generator=self.generator, device=torch_device |
| 75 | + ), |
| 76 | + "encoder_hidden_states": randn_tensor( |
| 77 | + (batch_size, sequence_length, 32), generator=self.generator, device=torch_device |
| 78 | + ), |
| 79 | + "prior_token_id": torch.randint(0, 64, size=(batch_size,), generator=self.generator).to(torch_device), |
| 80 | + "prior_token_drop": torch.zeros(batch_size, dtype=torch.bool, device=torch_device), |
| 81 | + "timestep": torch.randint(0, 1000, size=(batch_size,), generator=self.generator).to(torch_device), |
| 82 | + "target_size": torch.tensor([[height, width]] * batch_size, dtype=torch.float32).to(torch_device), |
| 83 | + "crop_coords": torch.tensor([[0, 0]] * batch_size, dtype=torch.float32).to(torch_device), |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | +class TestGlmImageTransformer(GlmImageTransformerTesterConfig, ModelTesterMixin): |
| 88 | + pass |
| 89 | + |
| 90 | + |
| 91 | +class TestGlmImageTransformerTraining(GlmImageTransformerTesterConfig, TrainingTesterMixin): |
| 92 | + def test_gradient_checkpointing_is_applied(self): |
| 93 | + expected_set = {"GlmImageTransformer2DModel"} |
| 94 | + super().test_gradient_checkpointing_is_applied(expected_set=expected_set) |
0 commit comments