test_amused.py 6.4 KB
Newer Older
Will Berman's avatar
Will Berman committed
1
# coding=utf-8
2
# Copyright 2024 HuggingFace Inc.
Will Berman's avatar
Will Berman committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer

from diffusers import AmusedPipeline, AmusedScheduler, UVit2DModel, VQModel
Dhruv Nair's avatar
Dhruv Nair committed
23
24
25
26
27
28
from diffusers.utils.testing_utils import (
    enable_full_determinism,
    require_torch_gpu,
    slow,
    torch_device,
)
Will Berman's avatar
Will Berman committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()


class AmusedPipelineFastTests(PipelineTesterMixin, unittest.TestCase):
    pipeline_class = AmusedPipeline
    params = TEXT_TO_IMAGE_PARAMS | {"encoder_hidden_states", "negative_encoder_hidden_states"}
    batch_params = TEXT_TO_IMAGE_BATCH_PARAMS

    def get_dummy_components(self):
        torch.manual_seed(0)
        transformer = UVit2DModel(
45
            hidden_size=8,
Will Berman's avatar
Will Berman committed
46
47
            use_bias=False,
            hidden_dropout=0.0,
48
            cond_embed_dim=8,
Will Berman's avatar
Will Berman committed
49
50
            micro_cond_encode_dim=2,
            micro_cond_embed_dim=10,
51
            encoder_hidden_size=8,
Will Berman's avatar
Will Berman committed
52
            vocab_size=32,
53
54
55
            codebook_size=8,
            in_channels=8,
            block_out_channels=8,
Will Berman's avatar
Will Berman committed
56
57
58
59
60
61
62
            num_res_blocks=1,
            downsample=True,
            upsample=True,
            block_num_heads=1,
            num_hidden_layers=1,
            num_attention_heads=1,
            attention_dropout=0.0,
63
            intermediate_size=8,
Will Berman's avatar
Will Berman committed
64
65
66
67
68
69
70
            layer_norm_eps=1e-06,
            ln_elementwise_affine=True,
        )
        scheduler = AmusedScheduler(mask_token_id=31)
        torch.manual_seed(0)
        vqvae = VQModel(
            act_fn="silu",
71
            block_out_channels=[8],
Dhruv Nair's avatar
Dhruv Nair committed
72
            down_block_types=["DownEncoderBlock2D"],
Will Berman's avatar
Will Berman committed
73
            in_channels=3,
74
75
76
77
            latent_channels=8,
            layers_per_block=1,
            norm_num_groups=8,
            num_vq_embeddings=8,
Will Berman's avatar
Will Berman committed
78
            out_channels=3,
79
            sample_size=8,
Dhruv Nair's avatar
Dhruv Nair committed
80
            up_block_types=["UpDecoderBlock2D"],
Will Berman's avatar
Will Berman committed
81
82
83
84
85
86
87
            mid_block_add_attention=False,
            lookup_from_codebook=True,
        )
        torch.manual_seed(0)
        text_encoder_config = CLIPTextConfig(
            bos_token_id=0,
            eos_token_id=2,
88
89
            hidden_size=8,
            intermediate_size=8,
Will Berman's avatar
Will Berman committed
90
            layer_norm_eps=1e-05,
91
92
            num_attention_heads=1,
            num_hidden_layers=1,
Will Berman's avatar
Will Berman committed
93
94
            pad_token_id=1,
            vocab_size=1000,
95
            projection_dim=8,
Will Berman's avatar
Will Berman committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
        )
        text_encoder = CLIPTextModelWithProjection(text_encoder_config)
        tokenizer = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip")
        components = {
            "transformer": transformer,
            "scheduler": scheduler,
            "vqvae": vqvae,
            "text_encoder": text_encoder,
            "tokenizer": tokenizer,
        }
        return components

    def get_dummy_inputs(self, device, seed=0):
        if str(device).startswith("mps"):
            generator = torch.manual_seed(seed)
        else:
            generator = torch.Generator(device=device).manual_seed(seed)
        inputs = {
            "prompt": "A painting of a squirrel eating a burger",
            "generator": generator,
            "num_inference_steps": 2,
            "output_type": "np",
            "height": 4,
            "width": 4,
        }
        return inputs

    def test_inference_batch_consistent(self, batch_sizes=[2]):
        self._test_inference_batch_consistent(batch_sizes=batch_sizes, batch_generator=False)

    @unittest.skip("aMUSEd does not support lists of generators")
    def test_inference_batch_single_identical(self):
        ...


@slow
@require_torch_gpu
class AmusedPipelineSlowTests(unittest.TestCase):
    def test_amused_256(self):
135
        pipe = AmusedPipeline.from_pretrained("amused/amused-256")
Will Berman's avatar
Will Berman committed
136
137
138
139
        pipe.to(torch_device)
        image = pipe("dog", generator=torch.Generator().manual_seed(0), num_inference_steps=2, output_type="np").images
        image_slice = image[0, -3:, -3:, -1].flatten()
        assert image.shape == (1, 256, 256, 3)
Dhruv Nair's avatar
Dhruv Nair committed
140
141
        expected_slice = np.array([0.4011, 0.3992, 0.379, 0.3856, 0.3772, 0.3711, 0.3919, 0.385, 0.3625])
        assert np.abs(image_slice - expected_slice).max() < 0.003
Will Berman's avatar
Will Berman committed
142
143

    def test_amused_256_fp16(self):
144
        pipe = AmusedPipeline.from_pretrained("amused/amused-256", variant="fp16", torch_dtype=torch.float16)
Will Berman's avatar
Will Berman committed
145
146
147
148
149
        pipe.to(torch_device)
        image = pipe("dog", generator=torch.Generator().manual_seed(0), num_inference_steps=2, output_type="np").images
        image_slice = image[0, -3:, -3:, -1].flatten()
        assert image.shape == (1, 256, 256, 3)
        expected_slice = np.array([0.0554, 0.05129, 0.0344, 0.0452, 0.0476, 0.0271, 0.0495, 0.0527, 0.0158])
Dhruv Nair's avatar
Dhruv Nair committed
150
        assert np.abs(image_slice - expected_slice).max() < 0.007
Will Berman's avatar
Will Berman committed
151
152

    def test_amused_512(self):
153
        pipe = AmusedPipeline.from_pretrained("amused/amused-512")
Will Berman's avatar
Will Berman committed
154
155
156
157
158
        pipe.to(torch_device)
        image = pipe("dog", generator=torch.Generator().manual_seed(0), num_inference_steps=2, output_type="np").images
        image_slice = image[0, -3:, -3:, -1].flatten()

        assert image.shape == (1, 512, 512, 3)
Dhruv Nair's avatar
Dhruv Nair committed
159
160
        expected_slice = np.array([0.1199, 0.1171, 0.1229, 0.1188, 0.1210, 0.1147, 0.1260, 0.1346, 0.1152])
        assert np.abs(image_slice - expected_slice).max() < 0.003
Will Berman's avatar
Will Berman committed
161
162

    def test_amused_512_fp16(self):
163
        pipe = AmusedPipeline.from_pretrained("amused/amused-512", variant="fp16", torch_dtype=torch.float16)
Will Berman's avatar
Will Berman committed
164
165
166
167
168
        pipe.to(torch_device)
        image = pipe("dog", generator=torch.Generator().manual_seed(0), num_inference_steps=2, output_type="np").images
        image_slice = image[0, -3:, -3:, -1].flatten()

        assert image.shape == (1, 512, 512, 3)
Dhruv Nair's avatar
Dhruv Nair committed
169
170
        expected_slice = np.array([0.1509, 0.1492, 0.1531, 0.1485, 0.1501, 0.1465, 0.1581, 0.1690, 0.1499])
        assert np.abs(image_slice - expected_slice).max() < 0.003