Path: blob/main/tests/pipelines/stable_diffusion/test_onnx_stable_diffusion.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 tempfile16import unittest1718import numpy as np1920from diffusers import (21DDIMScheduler,22DPMSolverMultistepScheduler,23EulerAncestralDiscreteScheduler,24EulerDiscreteScheduler,25LMSDiscreteScheduler,26OnnxStableDiffusionPipeline,27PNDMScheduler,28)29from diffusers.utils.testing_utils import is_onnx_available, nightly, require_onnxruntime, require_torch_gpu3031from ...test_pipelines_onnx_common import OnnxPipelineTesterMixin323334if is_onnx_available():35import onnxruntime as ort363738class OnnxStableDiffusionPipelineFastTests(OnnxPipelineTesterMixin, unittest.TestCase):39hub_checkpoint = "hf-internal-testing/tiny-random-OnnxStableDiffusionPipeline"4041def get_dummy_inputs(self, seed=0):42generator = np.random.RandomState(seed)43inputs = {44"prompt": "A painting of a squirrel eating a burger",45"generator": generator,46"num_inference_steps": 2,47"guidance_scale": 7.5,48"output_type": "numpy",49}50return inputs5152def test_pipeline_default_ddim(self):53pipe = OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint, provider="CPUExecutionProvider")54pipe.set_progress_bar_config(disable=None)5556inputs = self.get_dummy_inputs()57image = pipe(**inputs).images58image_slice = image[0, -3:, -3:, -1]5960assert image.shape == (1, 128, 128, 3)61expected_slice = np.array([0.65072, 0.58492, 0.48219, 0.55521, 0.53180, 0.55939, 0.50697, 0.39800, 0.46455])6263assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-26465def test_pipeline_pndm(self):66pipe = OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint, provider="CPUExecutionProvider")67pipe.scheduler = PNDMScheduler.from_config(pipe.scheduler.config, skip_prk_steps=True)68pipe.set_progress_bar_config(disable=None)6970inputs = self.get_dummy_inputs()71image = pipe(**inputs).images72image_slice = image[0, -3:, -3:, -1]7374assert image.shape == (1, 128, 128, 3)75expected_slice = np.array([0.65863, 0.59425, 0.49326, 0.56313, 0.53875, 0.56627, 0.51065, 0.39777, 0.46330])7677assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-27879def test_pipeline_lms(self):80pipe = OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint, provider="CPUExecutionProvider")81pipe.scheduler = LMSDiscreteScheduler.from_config(pipe.scheduler.config)82pipe.set_progress_bar_config(disable=None)8384inputs = self.get_dummy_inputs()85image = pipe(**inputs).images86image_slice = image[0, -3:, -3:, -1]8788assert image.shape == (1, 128, 128, 3)89expected_slice = np.array([0.53755, 0.60786, 0.47402, 0.49488, 0.51869, 0.49819, 0.47985, 0.38957, 0.44279])9091assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-29293def test_pipeline_euler(self):94pipe = OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint, provider="CPUExecutionProvider")95pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config)96pipe.set_progress_bar_config(disable=None)9798inputs = self.get_dummy_inputs()99image = pipe(**inputs).images100image_slice = image[0, -3:, -3:, -1]101102assert image.shape == (1, 128, 128, 3)103expected_slice = np.array([0.53755, 0.60786, 0.47402, 0.49488, 0.51869, 0.49819, 0.47985, 0.38957, 0.44279])104105assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-2106107def test_pipeline_euler_ancestral(self):108pipe = OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint, provider="CPUExecutionProvider")109pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)110pipe.set_progress_bar_config(disable=None)111112inputs = self.get_dummy_inputs()113image = pipe(**inputs).images114image_slice = image[0, -3:, -3:, -1]115116assert image.shape == (1, 128, 128, 3)117expected_slice = np.array([0.53817, 0.60812, 0.47384, 0.49530, 0.51894, 0.49814, 0.47984, 0.38958, 0.44271])118119assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-2120121def test_pipeline_dpm_multistep(self):122pipe = OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint, provider="CPUExecutionProvider")123pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)124pipe.set_progress_bar_config(disable=None)125126inputs = self.get_dummy_inputs()127image = pipe(**inputs).images128image_slice = image[0, -3:, -3:, -1]129130assert image.shape == (1, 128, 128, 3)131expected_slice = np.array([0.53895, 0.60808, 0.47933, 0.49608, 0.51886, 0.49950, 0.48053, 0.38957, 0.44200])132133assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-2134135136@nightly137@require_onnxruntime138@require_torch_gpu139class OnnxStableDiffusionPipelineIntegrationTests(unittest.TestCase):140@property141def gpu_provider(self):142return (143"CUDAExecutionProvider",144{145"gpu_mem_limit": "15000000000", # 15GB146"arena_extend_strategy": "kSameAsRequested",147},148)149150@property151def gpu_options(self):152options = ort.SessionOptions()153options.enable_mem_pattern = False154return options155156def test_inference_default_pndm(self):157# using the PNDM scheduler by default158sd_pipe = OnnxStableDiffusionPipeline.from_pretrained(159"CompVis/stable-diffusion-v1-4",160revision="onnx",161safety_checker=None,162feature_extractor=None,163provider=self.gpu_provider,164sess_options=self.gpu_options,165)166sd_pipe.set_progress_bar_config(disable=None)167168prompt = "A painting of a squirrel eating a burger"169np.random.seed(0)170output = sd_pipe([prompt], guidance_scale=6.0, num_inference_steps=10, output_type="np")171image = output.images172173image_slice = image[0, -3:, -3:, -1]174175assert image.shape == (1, 512, 512, 3)176expected_slice = np.array([0.0452, 0.0390, 0.0087, 0.0350, 0.0617, 0.0364, 0.0544, 0.0523, 0.0720])177178assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-3179180def test_inference_ddim(self):181ddim_scheduler = DDIMScheduler.from_pretrained(182"runwayml/stable-diffusion-v1-5", subfolder="scheduler", revision="onnx"183)184sd_pipe = OnnxStableDiffusionPipeline.from_pretrained(185"runwayml/stable-diffusion-v1-5",186revision="onnx",187scheduler=ddim_scheduler,188safety_checker=None,189feature_extractor=None,190provider=self.gpu_provider,191sess_options=self.gpu_options,192)193sd_pipe.set_progress_bar_config(disable=None)194195prompt = "open neural network exchange"196generator = np.random.RandomState(0)197output = sd_pipe([prompt], guidance_scale=7.5, num_inference_steps=10, generator=generator, output_type="np")198image = output.images199image_slice = image[0, -3:, -3:, -1]200201assert image.shape == (1, 512, 512, 3)202expected_slice = np.array([0.2867, 0.1974, 0.1481, 0.7294, 0.7251, 0.6667, 0.4194, 0.5642, 0.6486])203204assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-3205206def test_inference_k_lms(self):207lms_scheduler = LMSDiscreteScheduler.from_pretrained(208"runwayml/stable-diffusion-v1-5", subfolder="scheduler", revision="onnx"209)210sd_pipe = OnnxStableDiffusionPipeline.from_pretrained(211"runwayml/stable-diffusion-v1-5",212revision="onnx",213scheduler=lms_scheduler,214safety_checker=None,215feature_extractor=None,216provider=self.gpu_provider,217sess_options=self.gpu_options,218)219sd_pipe.set_progress_bar_config(disable=None)220221prompt = "open neural network exchange"222generator = np.random.RandomState(0)223output = sd_pipe([prompt], guidance_scale=7.5, num_inference_steps=10, generator=generator, output_type="np")224image = output.images225image_slice = image[0, -3:, -3:, -1]226227assert image.shape == (1, 512, 512, 3)228expected_slice = np.array([0.2306, 0.1959, 0.1593, 0.6549, 0.6394, 0.5408, 0.5065, 0.6010, 0.6161])229230assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-3231232def test_intermediate_state(self):233number_of_steps = 0234235def test_callback_fn(step: int, timestep: int, latents: np.ndarray) -> None:236test_callback_fn.has_been_called = True237nonlocal number_of_steps238number_of_steps += 1239if step == 0:240assert latents.shape == (1, 4, 64, 64)241latents_slice = latents[0, -3:, -3:, -1]242expected_slice = np.array(243[-0.6772, -0.3835, -1.2456, 0.1905, -1.0974, 0.6967, -1.9353, 0.0178, 1.0167]244)245246assert np.abs(latents_slice.flatten() - expected_slice).max() < 1e-3247elif step == 5:248assert latents.shape == (1, 4, 64, 64)249latents_slice = latents[0, -3:, -3:, -1]250expected_slice = np.array(251[-0.3351, 0.2241, -0.1837, -0.2325, -0.6577, 0.3393, -0.0241, 0.5899, 1.3875]252)253254assert np.abs(latents_slice.flatten() - expected_slice).max() < 1e-3255256test_callback_fn.has_been_called = False257258pipe = OnnxStableDiffusionPipeline.from_pretrained(259"runwayml/stable-diffusion-v1-5",260revision="onnx",261safety_checker=None,262feature_extractor=None,263provider=self.gpu_provider,264sess_options=self.gpu_options,265)266pipe.set_progress_bar_config(disable=None)267268prompt = "Andromeda galaxy in a bottle"269270generator = np.random.RandomState(0)271pipe(272prompt=prompt,273num_inference_steps=5,274guidance_scale=7.5,275generator=generator,276callback=test_callback_fn,277callback_steps=1,278)279assert test_callback_fn.has_been_called280assert number_of_steps == 6281282def test_stable_diffusion_no_safety_checker(self):283pipe = OnnxStableDiffusionPipeline.from_pretrained(284"runwayml/stable-diffusion-v1-5",285revision="onnx",286safety_checker=None,287feature_extractor=None,288provider=self.gpu_provider,289sess_options=self.gpu_options,290)291assert isinstance(pipe, OnnxStableDiffusionPipeline)292assert pipe.safety_checker is None293294image = pipe("example prompt", num_inference_steps=2).images[0]295assert image is not None296297# check that there's no error when saving a pipeline with one of the models being None298with tempfile.TemporaryDirectory() as tmpdirname:299pipe.save_pretrained(tmpdirname)300pipe = OnnxStableDiffusionPipeline.from_pretrained(tmpdirname)301302# sanity check that the pipeline still works303assert pipe.safety_checker is None304image = pipe("example prompt", num_inference_steps=2).images[0]305assert image is not None306307308