CoolFace
Apppublic

declare-lab/tango2

sourceHugging Faceupdated 2y agoView on Hugging Face
92likes
test_models_vq.py95 linesDownload Raw Back to models
1# coding=utf-82# Copyright 2023 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 at7#8#     http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# 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 and14# limitations under the License.15 16import unittest17 18import torch19 20from diffusers import VQModel21from diffusers.utils import floats_tensor, torch_device22 23from ..test_modeling_common import ModelTesterMixin24 25 26torch.backends.cuda.matmul.allow_tf32 = False27 28 29class VQModelTests(ModelTesterMixin, unittest.TestCase):30    model_class = VQModel31 32    @property33    def dummy_input(self, sizes=(32, 32)):34        batch_size = 435        num_channels = 336 37        image = floats_tensor((batch_size, num_channels) + sizes).to(torch_device)38 39        return {"sample": image}40 41    @property42    def input_shape(self):43        return (3, 32, 32)44 45    @property46    def output_shape(self):47        return (3, 32, 32)48 49    def prepare_init_args_and_inputs_for_common(self):50        init_dict = {51            "block_out_channels": [32, 64],52            "in_channels": 3,53            "out_channels": 3,54            "down_block_types": ["DownEncoderBlock2D", "DownEncoderBlock2D"],55            "up_block_types": ["UpDecoderBlock2D", "UpDecoderBlock2D"],56            "latent_channels": 3,57        }58        inputs_dict = self.dummy_input59        return init_dict, inputs_dict60 61    def test_forward_signature(self):62        pass63 64    def test_training(self):65        pass66 67    def test_from_pretrained_hub(self):68        model, loading_info = VQModel.from_pretrained("fusing/vqgan-dummy", output_loading_info=True)69        self.assertIsNotNone(model)70        self.assertEqual(len(loading_info["missing_keys"]), 0)71 72        model.to(torch_device)73        image = model(**self.dummy_input)74 75        assert image is not None, "Make sure output is not None"76 77    def test_output_pretrained(self):78        model = VQModel.from_pretrained("fusing/vqgan-dummy")79        model.to(torch_device).eval()80 81        torch.manual_seed(0)82        if torch.cuda.is_available():83            torch.cuda.manual_seed_all(0)84 85        image = torch.randn(1, model.config.in_channels, model.config.sample_size, model.config.sample_size)86        image = image.to(torch_device)87        with torch.no_grad():88            output = model(image).sample89 90        output_slice = output[0, -1, -3:, -3:].flatten().cpu()91        # fmt: off92        expected_output_slice = torch.tensor([-0.0153, -0.4044, -0.1880, -0.5161, -0.2418, -0.4072, -0.1612, -0.0633, -0.0143])93        # fmt: on94        self.assertTrue(torch.allclose(output_slice, expected_output_slice, atol=1e-3))95