Path: blob/main/tests/pipelines/latent_diffusion/test_latent_diffusion_uncond.py
1448 views
# coding=utf-81# Copyright 2023 HuggingFace Inc.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.1415import unittest1617import numpy as np18import torch19from transformers import CLIPTextConfig, CLIPTextModel2021from diffusers import DDIMScheduler, LDMPipeline, UNet2DModel, VQModel22from diffusers.utils.testing_utils import require_torch, slow, torch_device232425torch.backends.cuda.matmul.allow_tf32 = False262728class LDMPipelineFastTests(unittest.TestCase):29@property30def dummy_uncond_unet(self):31torch.manual_seed(0)32model = UNet2DModel(33block_out_channels=(32, 64),34layers_per_block=2,35sample_size=32,36in_channels=3,37out_channels=3,38down_block_types=("DownBlock2D", "AttnDownBlock2D"),39up_block_types=("AttnUpBlock2D", "UpBlock2D"),40)41return model4243@property44def dummy_vq_model(self):45torch.manual_seed(0)46model = VQModel(47block_out_channels=[32, 64],48in_channels=3,49out_channels=3,50down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"],51up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"],52latent_channels=3,53)54return model5556@property57def dummy_text_encoder(self):58torch.manual_seed(0)59config = CLIPTextConfig(60bos_token_id=0,61eos_token_id=2,62hidden_size=32,63intermediate_size=37,64layer_norm_eps=1e-05,65num_attention_heads=4,66num_hidden_layers=5,67pad_token_id=1,68vocab_size=1000,69)70return CLIPTextModel(config)7172def test_inference_uncond(self):73unet = self.dummy_uncond_unet74scheduler = DDIMScheduler()75vae = self.dummy_vq_model7677ldm = LDMPipeline(unet=unet, vqvae=vae, scheduler=scheduler)78ldm.to(torch_device)79ldm.set_progress_bar_config(disable=None)8081generator = torch.manual_seed(0)82image = ldm(generator=generator, num_inference_steps=2, output_type="numpy").images8384generator = torch.manual_seed(0)85image_from_tuple = ldm(generator=generator, num_inference_steps=2, output_type="numpy", return_dict=False)[0]8687image_slice = image[0, -3:, -3:, -1]88image_from_tuple_slice = image_from_tuple[0, -3:, -3:, -1]8990assert image.shape == (1, 64, 64, 3)91expected_slice = np.array([0.8512, 0.818, 0.6411, 0.6808, 0.4465, 0.5618, 0.46, 0.6231, 0.5172])92tolerance = 1e-2 if torch_device != "mps" else 3e-29394assert np.abs(image_slice.flatten() - expected_slice).max() < tolerance95assert np.abs(image_from_tuple_slice.flatten() - expected_slice).max() < tolerance969798@slow99@require_torch100class LDMPipelineIntegrationTests(unittest.TestCase):101def test_inference_uncond(self):102ldm = LDMPipeline.from_pretrained("CompVis/ldm-celebahq-256")103ldm.to(torch_device)104ldm.set_progress_bar_config(disable=None)105106generator = torch.manual_seed(0)107image = ldm(generator=generator, num_inference_steps=5, output_type="numpy").images108109image_slice = image[0, -3:, -3:, -1]110111assert image.shape == (1, 256, 256, 3)112expected_slice = np.array([0.4399, 0.44975, 0.46825, 0.474, 0.4359, 0.4581, 0.45095, 0.4341, 0.4447])113tolerance = 1e-2 if torch_device != "mps" else 3e-2114115assert np.abs(image_slice.flatten() - expected_slice).max() < tolerance116117118