Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
shivamshrirao
GitHub Repository: shivamshrirao/diffusers
Path: blob/main/tests/models/test_models_vq.py
1448 views
1
# coding=utf-8
2
# 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 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 VQModel
21
from diffusers.utils import floats_tensor, torch_device
22
23
from ..test_modeling_common import ModelTesterMixin
24
25
26
torch.backends.cuda.matmul.allow_tf32 = False
27
28
29
class VQModelTests(ModelTesterMixin, unittest.TestCase):
30
model_class = VQModel
31
32
@property
33
def dummy_input(self, sizes=(32, 32)):
34
batch_size = 4
35
num_channels = 3
36
37
image = floats_tensor((batch_size, num_channels) + sizes).to(torch_device)
38
39
return {"sample": image}
40
41
@property
42
def input_shape(self):
43
return (3, 32, 32)
44
45
@property
46
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_input
59
return init_dict, inputs_dict
60
61
def test_forward_signature(self):
62
pass
63
64
def test_training(self):
65
pass
66
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).sample
89
90
output_slice = output[0, -1, -3:, -3:].flatten().cpu()
91
# fmt: off
92
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: on
94
self.assertTrue(torch.allclose(output_slice, expected_output_slice, atol=1e-3))
95
96