


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

							def 				lowerCamelCase_			(							self:     List[Any]							)      ->       int:





									"""simple docstring"""







									torch.manual_seed(0							)
									lowercase__						=				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''')   ,						)
									lowercase__						=				DDIMScheduler()
									lowercase__						=				{'''unet''': unet, '''scheduler''': scheduler}
									return components

							def 				lowerCamelCase_			(							self:     Any   ,						UpperCamelCase_:     Optional[int]   ,						UpperCamelCase_:     Union[str, Any]=0							)      ->       Dict:





									"""simple docstring"""







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

							def 				lowerCamelCase_			(							self:     Dict							)      ->       Any:





									"""simple docstring"""







									lowercase__						=				'''cpu'''

									lowercase__						=				self.get_dummy_components()
									lowercase__						=				self.pipeline_class(**_snake_case							)
									pipe.to(_snake_case							)
									pipe.set_progress_bar_config(disable=_snake_case							)

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

									self.assertEqual(image.shape   ,						(1, 32, 32, 3)							)
									lowercase__						=				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]							)
									lowercase__						=				np.abs(image_slice.flatten() - expected_slice							).max()
									self.assertLessEqual(_snake_case   ,						1E-3							)

							def 				lowerCamelCase_			(							self:     str							)      ->       Optional[Any]:





									"""simple docstring"""







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

							def 				lowerCamelCase_			(							self:     int							)      ->       List[Any]:





									"""simple docstring"""







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

							def 				lowerCamelCase_			(							self:     Optional[int]							)      ->       str:





									"""simple docstring"""







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


							def 				lowerCamelCase_			(							self:     Optional[Any]							)      ->       Optional[int]:





									"""simple docstring"""







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


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

							def 				lowerCamelCase_			(							self:     int							)      ->       Dict:





									"""simple docstring"""







									lowercase__						=				'''google/ddpm-cifar10-32'''

									lowercase__						=				UNetaDModel.from_pretrained(_snake_case							)
									lowercase__						=				DDIMScheduler()

									lowercase__						=				DDIMPipeline(unet=_snake_case   ,						scheduler=_snake_case							)
									ddim.to(_snake_case							)
									ddim.set_progress_bar_config(disable=_snake_case							)

									lowercase__						=				torch.manual_seed(0							)
									lowercase__						=				ddim(generator=_snake_case   ,						eta=0.0   ,						output_type='''numpy'''							).images

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

									assert image.shape == (1, 32, 32, 3)
									lowercase__						=				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 				lowerCamelCase_			(							self:     Optional[int]							)      ->       Tuple:





									"""simple docstring"""







									lowercase__						=				'''google/ddpm-ema-bedroom-256'''

									lowercase__						=				UNetaDModel.from_pretrained(_snake_case							)
									lowercase__						=				DDIMScheduler.from_pretrained(_snake_case							)

									lowercase__						=				DDIMPipeline(unet=_snake_case   ,						scheduler=_snake_case							)
									ddpm.to(_snake_case							)
									ddpm.set_progress_bar_config(disable=_snake_case							)

									lowercase__						=				torch.manual_seed(0							)
									lowercase__						=				ddpm(generator=_snake_case   ,						output_type='''numpy'''							).images

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

									assert image.shape == (1, 256, 256, 3)
									lowercase__						=				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






def       _a			(				SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE					):


		"""simple docstring"""




		return base * power(SCREAMING_SNAKE_CASE						,    (exponent - 1)					) if exponent else 1


if __name__ == "__main__":
			print('Raise base to the power of exponent using recursion...')
			lowerCAmelCase	=							int(input('Enter the base: ').strip())
			lowerCAmelCase	=							int(input('Enter the exponent: ').strip())
			lowerCAmelCase	=							power(base, abs(exponent))
			if exponent < 0:  # power() does not properly deal w/ negative exponents
						lowerCAmelCase	=							1 / result
			print(f"""{base} to the power of {exponent} is {result}""")


