|
| 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 AutoencoderOobleck |
| 21 | +from diffusers.utils.testing_utils import ( |
| 22 | + enable_full_determinism, |
| 23 | + floats_tensor, |
| 24 | + require_torch_accelerator_with_training, |
| 25 | + torch_all_close, |
| 26 | + torch_device, |
| 27 | +) |
| 28 | + |
| 29 | +from ..test_modeling_common import ModelTesterMixin, UNetTesterMixin |
| 30 | + |
| 31 | + |
| 32 | +enable_full_determinism() |
| 33 | + |
| 34 | + |
| 35 | +class AutoencoderOobleckTests(ModelTesterMixin, UNetTesterMixin, unittest.TestCase): |
| 36 | + model_class = AutoencoderOobleck |
| 37 | + main_input_name = "sample" |
| 38 | + base_precision = 1e-2 |
| 39 | + |
| 40 | + def get_autoencoder_oobleck_config(self, block_out_channels=None): |
| 41 | + init_dict = { |
| 42 | + "encoder_hidden_size": 12, |
| 43 | + "decoder_channels": 12, |
| 44 | + "decoder_input_channels": 6, |
| 45 | + "audio_channels": 2, |
| 46 | + "downsampling_ratios": [2, 4], |
| 47 | + "channel_multiples": [1, 2], |
| 48 | + } |
| 49 | + return init_dict |
| 50 | + |
| 51 | + @property |
| 52 | + def dummy_input(self): |
| 53 | + batch_size = 4 |
| 54 | + num_channels = 2 |
| 55 | + seq_len = 24 |
| 56 | + |
| 57 | + waveform = floats_tensor((batch_size, num_channels, seq_len)).to(torch_device) |
| 58 | + |
| 59 | + return {"sample": waveform, "sample_posterior": False} |
| 60 | + |
| 61 | + @property |
| 62 | + def input_shape(self): |
| 63 | + return (2, 24) |
| 64 | + |
| 65 | + @property |
| 66 | + def output_shape(self): |
| 67 | + return (2, 24) |
| 68 | + |
| 69 | + def prepare_init_args_and_inputs_for_common(self): |
| 70 | + init_dict = self.get_autoencoder_oobleck_config() |
| 71 | + inputs_dict = self.dummy_input |
| 72 | + return init_dict, inputs_dict |
| 73 | + |
| 74 | + def test_enable_disable_slicing(self): |
| 75 | + init_dict, inputs_dict = self.prepare_init_args_and_inputs_for_common() |
| 76 | + |
| 77 | + torch.manual_seed(0) |
| 78 | + model = self.model_class(**init_dict).to(torch_device) |
| 79 | + |
| 80 | + inputs_dict.update({"return_dict": False}) |
| 81 | + |
| 82 | + torch.manual_seed(0) |
| 83 | + output_without_slicing = model(**inputs_dict, generator=torch.manual_seed(0))[0] |
| 84 | + |
| 85 | + torch.manual_seed(0) |
| 86 | + model.enable_slicing() |
| 87 | + output_with_slicing = model(**inputs_dict, generator=torch.manual_seed(0))[0] |
| 88 | + |
| 89 | + self.assertLess( |
| 90 | + (output_without_slicing.detach().cpu().numpy() - output_with_slicing.detach().cpu().numpy()).max(), |
| 91 | + 0.5, |
| 92 | + "VAE slicing should not affect the inference results", |
| 93 | + ) |
| 94 | + |
| 95 | + torch.manual_seed(0) |
| 96 | + model.disable_slicing() |
| 97 | + output_without_slicing_2 = model(**inputs_dict, generator=torch.manual_seed(0))[0] |
| 98 | + |
| 99 | + self.assertEqual( |
| 100 | + output_without_slicing.detach().cpu().numpy().all(), |
| 101 | + output_without_slicing_2.detach().cpu().numpy().all(), |
| 102 | + "Without slicing outputs should match with the outputs when slicing is manually disabled.", |
| 103 | + ) |
| 104 | + |
| 105 | + @require_torch_accelerator_with_training |
| 106 | + def test_gradient_checkpointing(self): |
| 107 | + # enable deterministic behavior for gradient checkpointing |
| 108 | + # (TODO: sayakpaul): should be grouped in https://github.com/huggingface/diffusers/pull/9494 |
| 109 | + init_dict, inputs_dict = self.prepare_init_args_and_inputs_for_common() |
| 110 | + model = self.model_class(**init_dict) |
| 111 | + model.to(torch_device) |
| 112 | + |
| 113 | + assert not model.is_gradient_checkpointing and model.training |
| 114 | + |
| 115 | + out = model(**inputs_dict).sample |
| 116 | + # run the backwards pass on the model. For backwards pass, for simplicity purpose, |
| 117 | + # we won't calculate the loss and rather backprop on out.sum() |
| 118 | + model.zero_grad() |
| 119 | + |
| 120 | + labels = torch.randn_like(out) |
| 121 | + loss = (out - labels).mean() |
| 122 | + loss.backward() |
| 123 | + |
| 124 | + # re-instantiate the model now enabling gradient checkpointing |
| 125 | + model_2 = self.model_class(**init_dict) |
| 126 | + # clone model |
| 127 | + model_2.load_state_dict(model.state_dict()) |
| 128 | + model_2.to(torch_device) |
| 129 | + model_2.enable_gradient_checkpointing() |
| 130 | + |
| 131 | + assert model_2.is_gradient_checkpointing and model_2.training |
| 132 | + |
| 133 | + out_2 = model_2(**inputs_dict).sample |
| 134 | + # run the backwards pass on the model. For backwards pass, for simplicity purpose, |
| 135 | + # we won't calculate the loss and rather backprop on out.sum() |
| 136 | + model_2.zero_grad() |
| 137 | + loss_2 = (out_2 - labels).mean() |
| 138 | + loss_2.backward() |
| 139 | + |
| 140 | + # compare the output and parameters gradients |
| 141 | + self.assertTrue((loss - loss_2).abs() < 1e-5) |
| 142 | + named_params = dict(model.named_parameters()) |
| 143 | + named_params_2 = dict(model_2.named_parameters()) |
| 144 | + for name, param in named_params.items(): |
| 145 | + self.assertTrue(torch_all_close(param.grad.data, named_params_2[name].grad.data, atol=5e-5)) |
| 146 | + |
| 147 | + @unittest.skip("Test unsupported.") |
| 148 | + def test_forward_with_norm_groups(self): |
| 149 | + pass |
| 150 | + |
| 151 | + @unittest.skip("No attention module used in this model") |
| 152 | + def test_set_attn_processor_for_determinism(self): |
| 153 | + return |
0 commit comments