
"""simple docstring"""

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 __lowerCAmelCase						( unittest.TestCase     ):





     @property
     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      torch.manual_seed(0 )
      __UpperCamelCase							    =			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 ):

      '''simple docstring'''




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

      __UpperCamelCase							    =			DDPMPipeline(unet=__UpperCAmelCase      ,	scheduler=__UpperCAmelCase )
      ddpm.to(__UpperCAmelCase )
      ddpm.set_progress_bar_config(disable=__UpperCAmelCase )

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

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

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

      assert image.shape == (1, 32, 32, 3)
      __UpperCamelCase							    =			np.array(
          [9.956E-01, 5.785E-01, 4.675E-01, 9.930E-01, 0.0, 1.0_0_0, 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 ):

      '''simple docstring'''




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

      __UpperCamelCase							    =			DDPMPipeline(unet=__UpperCAmelCase      ,	scheduler=__UpperCAmelCase )
      ddpm.to(__UpperCAmelCase )
      ddpm.set_progress_bar_config(disable=__UpperCAmelCase )

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

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

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

      assert image.shape == (1, 32, 32, 3)
      __UpperCamelCase							    =			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 __lowerCAmelCase						( unittest.TestCase     ):





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			'google/ddpm-cifar10-32'

      __UpperCamelCase							    =			UNetaDModel.from_pretrained(__UpperCAmelCase )
      __UpperCamelCase							    =			DDPMScheduler.from_pretrained(__UpperCAmelCase )

      __UpperCamelCase							    =			DDPMPipeline(unet=__UpperCAmelCase      ,	scheduler=__UpperCAmelCase )
      ddpm.to(__UpperCAmelCase )
      ddpm.set_progress_bar_config(disable=__UpperCAmelCase )

      __UpperCamelCase							    =			torch.manual_seed(0 )
      __UpperCamelCase							    =			ddpm(generator=__UpperCAmelCase      ,	output_type='numpy' ).images

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

      assert image.shape == (1, 32, 32, 3)
      __UpperCamelCase							    =			np.array([0.4_2_0_0, 0.3_5_8_8, 0.1_9_3_9, 0.3_8_4_7, 0.3_3_8_2, 0.2_6_4_7, 0.4_1_5_5, 0.3_5_8_2, 0.3_3_8_5] )
      assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2




"""simple docstring"""




def 	UpperCAmelCase       (						UpperCamelCase__ = 100							,     UpperCamelCase__ = 100					):






							"""simple docstring"""





							return max(
							    sum(int(UpperCamelCase__					) for x in str(base**power					)					)
							    for base in range(UpperCamelCase__					)
							    for power in range(UpperCamelCase__					)					)


# Tests
if __name__ == "__main__":
				import doctest

				doctest.testmod()





