




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       a__   ( snake_case__			,    unittest.TestCase				):
						_a							:   int =   DDIMPipeline
						_a							:   List[Any] =   UNCONDITIONAL_IMAGE_GENERATION_PARAMS
						_a							:   List[Any] =   PipelineTesterMixin.required_optional_params - {
						    """num_images_per_prompt""",
						    """latents""",
						    """callback""",
						    """callback_steps""",
						}
						_a							:   Optional[int] =   UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS
						_a							:   Optional[Any] =   False






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											torch.manual_seed(0      )
											__lowerCAmelCase           =	UNetaDModel(
											    block_out_channels=(3_2, 6_4)		,      layers_per_block=2		,      sample_size=3_2		,      in_channels=3		,      out_channels=3		,      down_block_types=("DownBlock2D", "AttnDownBlock2D")		,      up_block_types=("AttnUpBlock2D", "UpBlock2D")		,      )
											__lowerCAmelCase           =	DDIMScheduler()
											__lowerCAmelCase           =	{"unet": unet, "scheduler": scheduler}
											return components






						def     __SCREAMING_SNAKE_CASE(       self		,      _A		,      _A=0      ):




											"""simple docstring"""



											if str(_A      ).startswith("mps"      ):
																__lowerCAmelCase           =	torch.manual_seed(_A      )
											else:
																__lowerCAmelCase           =	torch.Generator(device=_A      ).manual_seed(_A      )
											__lowerCAmelCase           =	{
											    "batch_size": 1,
											    "generator": generator,
											    "num_inference_steps": 2,
											    "output_type": "numpy",
											}
											return inputs






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase           =	"cpu"

											__lowerCAmelCase           =	self.get_dummy_components()
											__lowerCAmelCase           =	self.pipeline_class(**_A      )
											pipe.to(_A      )
											pipe.set_progress_bar_config(disable=_A      )

											__lowerCAmelCase           =	self.get_dummy_inputs(_A      )
											__lowerCAmelCase           =	pipe(**_A      ).images
											__lowerCAmelCase           =	image[0, -3:, -3:, -1]

											self.assertEqual(image.shape		,      (1, 3_2, 3_2, 3)      )
											__lowerCAmelCase           =	np.array(
											    [1.0_0_0E0_0, 5.7_1_7E-0_1, 4.7_1_7E-0_1, 1.0_0_0E0_0, 0.0_0_0E0_0, 1.0_0_0E0_0, 3.0_0_0E-0_4, 0.0_0_0E0_0, 9.0_0_0E-0_4]      )
											__lowerCAmelCase           =	np.abs(image_slice.flatten() - expected_slice      ).max()
											self.assertLessEqual(_A		,      1E-3      )






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3      )






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											super().test_save_load_local(expected_max_difference=3E-3      )






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



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






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



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






@slow
@require_torch_gpu
class       a__   ( unittest.TestCase				):






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase           =	"google/ddpm-cifar10-32"

											__lowerCAmelCase           =	UNetaDModel.from_pretrained(_A      )
											__lowerCAmelCase           =	DDIMScheduler()

											__lowerCAmelCase           =	DDIMPipeline(unet=_A		,      scheduler=_A      )
											ddim.to(_A      )
											ddim.set_progress_bar_config(disable=_A      )

											__lowerCAmelCase           =	torch.manual_seed(0      )
											__lowerCAmelCase           =	ddim(generator=_A		,      eta=0.0		,      output_type="numpy"      ).images

											__lowerCAmelCase           =	image[0, -3:, -3:, -1]

											assert image.shape == (1, 3_2, 3_2, 3)
											__lowerCAmelCase           =	np.array([0.17_23, 0.16_17, 0.16_00, 0.16_26, 0.14_97, 0.15_13, 0.15_05, 0.14_42, 0.14_53]      )

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






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase           =	"google/ddpm-ema-bedroom-256"

											__lowerCAmelCase           =	UNetaDModel.from_pretrained(_A      )
											__lowerCAmelCase           =	DDIMScheduler.from_pretrained(_A      )

											__lowerCAmelCase           =	DDIMPipeline(unet=_A		,      scheduler=_A      )
											ddpm.to(_A      )
											ddpm.set_progress_bar_config(disable=_A      )

											__lowerCAmelCase           =	torch.manual_seed(0      )
											__lowerCAmelCase           =	ddpm(generator=_A		,      output_type="numpy"      ).images

											__lowerCAmelCase           =	image[0, -3:, -3:, -1]

											assert image.shape == (1, 2_5_6, 2_5_6, 3)
											__lowerCAmelCase           =	np.array([0.00_60, 0.02_01, 0.03_44, 0.00_24, 0.00_18, 0.00_02, 0.00_22, 0.00_00, 0.00_69]      )

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










import unittest

import numpy as np
import torch

from diffusers import ScoreSdeVePipeline, ScoreSdeVeScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()
class       a__   ( unittest.TestCase				):






						@property
						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											torch.manual_seed(0      )
											__lowerCAmelCase           =	UNetaDModel(
											    block_out_channels=(3_2, 6_4)		,      layers_per_block=2		,      sample_size=3_2		,      in_channels=3		,      out_channels=3		,      down_block_types=("DownBlock2D", "AttnDownBlock2D")		,      up_block_types=("AttnUpBlock2D", "UpBlock2D")		,      )
											return model






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase           =	self.dummy_uncond_unet
											__lowerCAmelCase           =	ScoreSdeVeScheduler()

											__lowerCAmelCase           =	ScoreSdeVePipeline(unet=_A		,      scheduler=_A      )
											sde_ve.to(_A      )
											sde_ve.set_progress_bar_config(disable=_A      )

											__lowerCAmelCase           =	torch.manual_seed(0      )
											__lowerCAmelCase           =	sde_ve(num_inference_steps=2		,      output_type="numpy"		,      generator=_A      ).images

											__lowerCAmelCase           =	torch.manual_seed(0      )
											__lowerCAmelCase           =	sde_ve(num_inference_steps=2		,      output_type="numpy"		,      generator=_A		,      return_dict=_A      )[
											    0
											]

											__lowerCAmelCase           =	image[0, -3:, -3:, -1]
											__lowerCAmelCase           =	image_from_tuple[0, -3:, -3:, -1]

											assert image.shape == (1, 3_2, 3_2, 3)
											__lowerCAmelCase           =	np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0]      )

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






@slow
@require_torch
class       a__   ( unittest.TestCase				):






						def     __SCREAMING_SNAKE_CASE(       self      ):




											"""simple docstring"""



											__lowerCAmelCase           =	"google/ncsnpp-church-256"
											__lowerCAmelCase           =	UNetaDModel.from_pretrained(_A      )

											__lowerCAmelCase           =	ScoreSdeVeScheduler.from_pretrained(_A      )

											__lowerCAmelCase           =	ScoreSdeVePipeline(unet=_A		,      scheduler=_A      )
											sde_ve.to(_A      )
											sde_ve.set_progress_bar_config(disable=_A      )

											__lowerCAmelCase           =	torch.manual_seed(0      )
											__lowerCAmelCase           =	sde_ve(num_inference_steps=1_0		,      output_type="numpy"		,      generator=_A      ).images

											__lowerCAmelCase           =	image[0, -3:, -3:, -1]

											assert image.shape == (1, 2_5_6, 2_5_6, 3)

											__lowerCAmelCase           =	np.array([0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0]      )

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




