

import unittest

import numpy as np
import torch

from diffusers import DDIMPipeline, DDIMScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device

from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()






class _UpperCamelCase				(				__A				,						unittest.TestCase ):
			'''simple docstring'''







			lowerCamelCase__							     =DDIMPipeline
			lowerCamelCase__							     =UNCONDITIONAL_IMAGE_GENERATION_PARAMS
			lowerCamelCase__							     =PipelineTesterMixin.required_optional_params - {
			    'num_images_per_prompt',
			    'latents',
			    'callback',
			    'callback_steps',
			}
			lowerCamelCase__							     =UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS
			lowerCamelCase__							     =False
			def 				__UpperCamelCase			(      self				:			Union[str, Any]						)				->							List[str]:

				"""simple docstring"""

				torch.manual_seed(0						)
				SCREAMING_SNAKE_CASE :   int					  =							UNetaDModel(
				    block_out_channels=(32, 64)   ,   layers_per_block=2   ,   sample_size=32   ,   in_channels=3   ,   out_channels=3   ,   down_block_types=("DownBlock2D", "AttnDownBlock2D")   ,   up_block_types=("AttnUpBlock2D", "UpBlock2D")   ,   )
				SCREAMING_SNAKE_CASE :   Optional[int]					  =							DDIMScheduler()
				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							{"unet": unet, "scheduler": scheduler}
				return components
			def 				__UpperCamelCase			(      self				:			Tuple   ,   a				:			Tuple   ,   a				:			List[Any]=0						)				->							Union[str, Any]:

				"""simple docstring"""

				if str(a						).startswith("mps"						):
					SCREAMING_SNAKE_CASE :   Optional[int]					  =							torch.manual_seed(a						)
				else:
					SCREAMING_SNAKE_CASE :   Optional[int]					  =							torch.Generator(device=a						).manual_seed(a						)
				SCREAMING_SNAKE_CASE :   List[str]					  =							{
				    "batch_size": 1,
				    "generator": generator,
				    "num_inference_steps": 2,
				    "output_type": "numpy",
				}
				return inputs
			def 				__UpperCamelCase			(      self				:			Union[str, Any]						)				->							int:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   Any					  =							"cpu"

				SCREAMING_SNAKE_CASE :   Any					  =							self.get_dummy_components()
				SCREAMING_SNAKE_CASE :   Dict					  =							self.pipeline_class(**a						)
				pipe.to(a						)
				pipe.set_progress_bar_config(disable=a						)

				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							self.get_dummy_inputs(a						)
				SCREAMING_SNAKE_CASE :   Any					  =							pipe(**a						).images
				SCREAMING_SNAKE_CASE :   Tuple					  =							image[0, -3:, -3:, -1]

				self.assertEqual(image.shape   ,   (1, 32, 32, 3)						)
				SCREAMING_SNAKE_CASE :   Tuple					  =							np.array(
				    [1.0_00e00, 5.7_17e-01, 4.7_17e-01, 1.0_00e00, 0.0_00e00, 1.0_00e00, 3.0_00e-04, 0.0_00e00, 9.0_00e-04]						)
				SCREAMING_SNAKE_CASE :   List[Any]					  =							np.abs(image_slice.flatten() - expected_slice						).max()
				self.assertLessEqual(a   ,   1e-3						)
			def 				__UpperCamelCase			(      self				:			List[str]						)				->							Union[str, Any]:

				"""simple docstring"""

				super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3						)
			def 				__UpperCamelCase			(      self				:			int						)				->							Optional[Any]:

				"""simple docstring"""

				super().test_save_load_local(expected_max_difference=3e-3						)
			def 				__UpperCamelCase			(      self				:			Any						)				->							Optional[int]:

				"""simple docstring"""

				super().test_save_load_optional_components(expected_max_difference=3e-3						)






			def 				__UpperCamelCase			(      self				:			Tuple						)				->							List[Any]:

				"""simple docstring"""

				super().test_inference_batch_single_identical(expected_max_diff=3e-3						)






@slow
@require_torch_gpu
class _UpperCamelCase				(				unittest.TestCase ):
			'''simple docstring'''







			def 				__UpperCamelCase			(      self				:			Dict						)				->							Any:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   List[Any]					  =							"google/ddpm-cifar10-32"

				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							UNetaDModel.from_pretrained(a						)
				SCREAMING_SNAKE_CASE :   Optional[int]					  =							DDIMScheduler()

				SCREAMING_SNAKE_CASE :   Dict					  =							DDIMPipeline(unet=a   ,   scheduler=a						)
				ddim.to(a						)
				ddim.set_progress_bar_config(disable=a						)

				SCREAMING_SNAKE_CASE :   Optional[int]					  =							torch.manual_seed(0						)
				SCREAMING_SNAKE_CASE :   Any					  =							ddim(generator=a   ,   eta=0.0   ,   output_type="numpy"						).images

				SCREAMING_SNAKE_CASE :   Optional[int]					  =							image[0, -3:, -3:, -1]

				assert image.shape == (1, 32, 32, 3)
				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							np.array([0.1723, 0.1617, 0.1600, 0.1626, 0.1497, 0.1513, 0.1505, 0.1442, 0.1453]						)

				assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-2






			def 				__UpperCamelCase			(      self				:			Dict						)				->							Dict:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   List[str]					  =							"google/ddpm-ema-bedroom-256"

				SCREAMING_SNAKE_CASE :   Any					  =							UNetaDModel.from_pretrained(a						)
				SCREAMING_SNAKE_CASE :   List[Any]					  =							DDIMScheduler.from_pretrained(a						)

				SCREAMING_SNAKE_CASE :   int					  =							DDIMPipeline(unet=a   ,   scheduler=a						)
				ddpm.to(a						)
				ddpm.set_progress_bar_config(disable=a						)

				SCREAMING_SNAKE_CASE :   List[str]					  =							torch.manual_seed(0						)
				SCREAMING_SNAKE_CASE :   List[Any]					  =							ddpm(generator=a   ,   output_type="numpy"						).images

				SCREAMING_SNAKE_CASE :   str					  =							image[0, -3:, -3:, -1]

				assert image.shape == (1, 256, 256, 3)
				SCREAMING_SNAKE_CASE :   Any					  =							np.array([0.0060, 0.0201, 0.0344, 0.0024, 0.0018, 0.0002, 0.0022, 0.0000, 0.0069]						)

				assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-2


import gc
import unittest

import numpy as np
import torch

from diffusers import StableDiffusionKDiffusionPipeline
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()






@slow
@require_torch_gpu
class _UpperCamelCase				(				unittest.TestCase ):
			'''simple docstring'''







			def 				__UpperCamelCase			(      self				:			Dict						)				->							Tuple:

				"""simple docstring"""

				super().tearDown()
				gc.collect()
				torch.cuda.empty_cache()
			def 				__UpperCamelCase			(      self				:			Optional[int]						)				->							int:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   Optional[Any]					  =							StableDiffusionKDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4"						)
				SCREAMING_SNAKE_CASE :   str					  =							sd_pipe.to(a						)
				sd_pipe.set_progress_bar_config(disable=a						)

				sd_pipe.set_scheduler("sample_euler"						)

				SCREAMING_SNAKE_CASE :   Optional[int]					  =							"A painting of a squirrel eating a burger"
				SCREAMING_SNAKE_CASE :   Any					  =							torch.manual_seed(0						)
				SCREAMING_SNAKE_CASE :   List[Any]					  =							sd_pipe([prompt]   ,   generator=a   ,   guidance_scale=9.0   ,   num_inference_steps=20   ,   output_type="np"						)

				SCREAMING_SNAKE_CASE :   Tuple					  =							output.images

				SCREAMING_SNAKE_CASE :   Any					  =							image[0, -3:, -3:, -1]

				assert image.shape == (1, 512, 512, 3)
				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							np.array([0.0447, 0.0492, 0.0468, 0.0408, 0.0383, 0.0408, 0.0354, 0.0380, 0.0339]						)

				assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-2
			def 				__UpperCamelCase			(      self				:			List[Any]						)				->							Optional[int]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   int					  =							StableDiffusionKDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1-base"						)
				SCREAMING_SNAKE_CASE :   Tuple					  =							sd_pipe.to(a						)
				sd_pipe.set_progress_bar_config(disable=a						)

				sd_pipe.set_scheduler("sample_euler"						)

				SCREAMING_SNAKE_CASE :   List[str]					  =							"A painting of a squirrel eating a burger"
				SCREAMING_SNAKE_CASE :   List[str]					  =							torch.manual_seed(0						)
				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							sd_pipe([prompt]   ,   generator=a   ,   guidance_scale=9.0   ,   num_inference_steps=20   ,   output_type="np"						)

				SCREAMING_SNAKE_CASE :   List[Any]					  =							output.images

				SCREAMING_SNAKE_CASE :   Tuple					  =							image[0, -3:, -3:, -1]

				assert image.shape == (1, 512, 512, 3)
				SCREAMING_SNAKE_CASE :   int					  =							np.array([0.1237, 0.1320, 0.1438, 0.1359, 0.1390, 0.1132, 0.1277, 0.1175, 0.1112]						)

				assert np.abs(image_slice.flatten() - expected_slice						).max() < 5e-1






			def 				__UpperCamelCase			(      self				:			Tuple						)				->							List[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   Dict					  =							StableDiffusionKDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1-base"						)
				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							sd_pipe.to(a						)
				sd_pipe.set_progress_bar_config(disable=a						)

				sd_pipe.set_scheduler("sample_dpmpp_2m"						)

				SCREAMING_SNAKE_CASE :   str					  =							"A painting of a squirrel eating a burger"
				SCREAMING_SNAKE_CASE :   Any					  =							torch.manual_seed(0						)
				SCREAMING_SNAKE_CASE :   str					  =							sd_pipe(
				    [prompt]   ,   generator=a   ,   guidance_scale=7.5   ,   num_inference_steps=15   ,   output_type="np"   ,   use_karras_sigmas=a   ,   )

				SCREAMING_SNAKE_CASE :   str					  =							output.images

				SCREAMING_SNAKE_CASE :   Optional[int]					  =							image[0, -3:, -3:, -1]

				assert image.shape == (1, 512, 512, 3)
				SCREAMING_SNAKE_CASE :   int					  =							np.array(
				    [0.1138_1689, 0.1211_2921, 0.138_9457, 0.1254_9606, 0.124_4964, 0.1083_1517, 0.1156_2866, 0.1086_7816, 0.1049_9048]						)

				assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-2