

import unittest

import numpy as np
import torch

from diffusers import DDPMPipeline, DDPMScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device


enable_full_determinism()







class 		__magic_name__     (				unittest.TestCase				):

   '''simple docstring'''




   @property
   def 					_UpperCAmelCase					(							self			)				->       Any:






     """simple docstring"""






     torch.manual_seed(0			)
     a__    =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'''),       )
     return model



   def 					_UpperCAmelCase					(							self			)				->       List[str]:






     """simple docstring"""






     a__    ='''cpu'''
     a__    =self.dummy_uncond_unet
     a__    =DDPMScheduler()

     a__    =DDPMPipeline(unet=SCREAMING_SNAKE_CASE_,       scheduler=SCREAMING_SNAKE_CASE_			)
     ddpm.to(SCREAMING_SNAKE_CASE_			)
     ddpm.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_			)

     a__    =torch.Generator(device=SCREAMING_SNAKE_CASE_			).manual_seed(0			)
     a__    =ddpm(generator=SCREAMING_SNAKE_CASE_,       num_inference_steps=2,       output_type='''numpy'''			).images

     a__    =torch.Generator(device=SCREAMING_SNAKE_CASE_			).manual_seed(0			)
     a__    =ddpm(generator=SCREAMING_SNAKE_CASE_,       num_inference_steps=2,       output_type='''numpy''',       return_dict=SCREAMING_SNAKE_CASE_			)[0]

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

     assert image.shape == (1, 32, 32, 3)
     a__    =np.array(
         [9.956E-01, 5.785E-01, 4.675E-01, 9.930E-01, 0.0, 1.000, 1.199E-03, 2.648E-04, 5.101E-04]			)

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



   def 					_UpperCAmelCase					(							self			)				->       int:






     """simple docstring"""






     a__    =self.dummy_uncond_unet
     a__    =DDPMScheduler(prediction_type='''sample'''			)

     a__    =DDPMPipeline(unet=SCREAMING_SNAKE_CASE_,       scheduler=SCREAMING_SNAKE_CASE_			)
     ddpm.to(SCREAMING_SNAKE_CASE_			)
     ddpm.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_			)

     a__    =torch.manual_seed(0			)
     a__    =ddpm(generator=SCREAMING_SNAKE_CASE_,       num_inference_steps=2,       output_type='''numpy'''			).images

     a__    =torch.manual_seed(0			)
     a__    =ddpm(generator=SCREAMING_SNAKE_CASE_,       num_inference_steps=2,       output_type='''numpy'''			)[0]

     a__    =image[0, -3:, -3:, -1]
     a__    =image_eps[0, -3:, -3:, -1]

     assert image.shape == (1, 32, 32, 3)
     a__    =1E-2 if torch_device != '''mps''' else 3E-2
     assert np.abs(image_slice.flatten() - image_eps_slice.flatten()			).max() < tolerance







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

   '''simple docstring'''




   def 					_UpperCAmelCase					(							self			)				->       Optional[int]:






     """simple docstring"""






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

     a__    =UNetaDModel.from_pretrained(SCREAMING_SNAKE_CASE_			)
     a__    =DDPMScheduler.from_pretrained(SCREAMING_SNAKE_CASE_			)

     a__    =DDPMPipeline(unet=SCREAMING_SNAKE_CASE_,       scheduler=SCREAMING_SNAKE_CASE_			)
     ddpm.to(SCREAMING_SNAKE_CASE_			)
     ddpm.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_			)

     a__    =torch.manual_seed(0			)
     a__    =ddpm(generator=SCREAMING_SNAKE_CASE_,       output_type='''numpy'''			).images

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

     assert image.shape == (1, 32, 32, 3)
     a__    =np.array([0.4200, 0.3588, 0.1939, 0.3847, 0.3382, 0.2647, 0.4155, 0.3582, 0.3385]			)
     assert np.abs(image_slice.flatten() - expected_slice			).max() < 1E-2






'''simple docstring'''


def 					SCREAMING_SNAKE_CASE(   __lowercase	)			-> int:
     if not isinstance(__lowercase		,				__lowercase	):
          raise ValueError('''double_factorial() only accepts integral values'''	)
     if n < 0:
          raise ValueError('''double_factorial() not defined for negative values'''	)
     return 1 if n <= 1 else n * double_factorial(n - 2	)


if __name__ == "__main__":
     import doctest

     doctest.testmod()






