Path: blob/main/tests/pipelines/ddim/test_ddim.py
1450 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 torch1920from diffusers import DDIMPipeline, DDIMScheduler, UNet2DModel21from diffusers.utils.testing_utils import require_torch_gpu, slow, torch_device2223from ...pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS24from ...test_pipelines_common import PipelineTesterMixin252627torch.backends.cuda.matmul.allow_tf32 = False282930class DDIMPipelineFastTests(PipelineTesterMixin, unittest.TestCase):31pipeline_class = DDIMPipeline32params = UNCONDITIONAL_IMAGE_GENERATION_PARAMS33required_optional_params = PipelineTesterMixin.required_optional_params - {34"num_images_per_prompt",35"latents",36"callback",37"callback_steps",38}39batch_params = UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS40test_cpu_offload = False4142def get_dummy_components(self):43torch.manual_seed(0)44unet = UNet2DModel(45block_out_channels=(32, 64),46layers_per_block=2,47sample_size=32,48in_channels=3,49out_channels=3,50down_block_types=("DownBlock2D", "AttnDownBlock2D"),51up_block_types=("AttnUpBlock2D", "UpBlock2D"),52)53scheduler = DDIMScheduler()54components = {"unet": unet, "scheduler": scheduler}55return components5657def get_dummy_inputs(self, device, seed=0):58if str(device).startswith("mps"):59generator = torch.manual_seed(seed)60else:61generator = torch.Generator(device=device).manual_seed(seed)62inputs = {63"batch_size": 1,64"generator": generator,65"num_inference_steps": 2,66"output_type": "numpy",67}68return inputs6970def test_inference(self):71device = "cpu"7273components = self.get_dummy_components()74pipe = self.pipeline_class(**components)75pipe.to(device)76pipe.set_progress_bar_config(disable=None)7778inputs = self.get_dummy_inputs(device)79image = pipe(**inputs).images80image_slice = image[0, -3:, -3:, -1]8182self.assertEqual(image.shape, (1, 32, 32, 3))83expected_slice = np.array(84[1.000e00, 5.717e-01, 4.717e-01, 1.000e00, 0.000e00, 1.000e00, 3.000e-04, 0.000e00, 9.000e-04]85)86max_diff = np.abs(image_slice.flatten() - expected_slice).max()87self.assertLessEqual(max_diff, 1e-3)888990@slow91@require_torch_gpu92class DDIMPipelineIntegrationTests(unittest.TestCase):93def test_inference_cifar10(self):94model_id = "google/ddpm-cifar10-32"9596unet = UNet2DModel.from_pretrained(model_id)97scheduler = DDIMScheduler()9899ddim = DDIMPipeline(unet=unet, scheduler=scheduler)100ddim.to(torch_device)101ddim.set_progress_bar_config(disable=None)102103generator = torch.manual_seed(0)104image = ddim(generator=generator, eta=0.0, output_type="numpy").images105106image_slice = image[0, -3:, -3:, -1]107108assert image.shape == (1, 32, 32, 3)109expected_slice = np.array([0.1723, 0.1617, 0.1600, 0.1626, 0.1497, 0.1513, 0.1505, 0.1442, 0.1453])110111assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-2112113def test_inference_ema_bedroom(self):114model_id = "google/ddpm-ema-bedroom-256"115116unet = UNet2DModel.from_pretrained(model_id)117scheduler = DDIMScheduler.from_pretrained(model_id)118119ddpm = DDIMPipeline(unet=unet, scheduler=scheduler)120ddpm.to(torch_device)121ddpm.set_progress_bar_config(disable=None)122123generator = torch.manual_seed(0)124image = ddpm(generator=generator, output_type="numpy").images125126image_slice = image[0, -3:, -3:, -1]127128assert image.shape == (1, 256, 256, 3)129expected_slice = np.array([0.0060, 0.0201, 0.0344, 0.0024, 0.0018, 0.0002, 0.0022, 0.0000, 0.0069])130131assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-2132133134