






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



	'''simple docstring'''




	@property
	def 							_UpperCamelCase      (	self )	->   Optional[int]:
								torch.manual_seed(0 )
								snake_case_           =							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 )	->   str:
								snake_case_           =							'cpu'
								snake_case_           =							self.dummy_uncond_unet
								snake_case_           =							DDPMScheduler()

								snake_case_           =							DDPMPipeline(unet=a	,      scheduler=a )
								ddpm.to(a )
								ddpm.set_progress_bar_config(disable=a )

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

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

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

								assert image.shape == (1, 32, 32, 3)
								snake_case_           =							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 )	->   Optional[Any]:
								snake_case_           =							self.dummy_uncond_unet
								snake_case_           =							DDPMScheduler(prediction_type='sample' )

								snake_case_           =							DDPMPipeline(unet=a	,      scheduler=a )
								ddpm.to(a )
								ddpm.set_progress_bar_config(disable=a )

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

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

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

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



	'''simple docstring'''




	def 							_UpperCamelCase      (	self )	->   int:
								snake_case_           =							'google/ddpm-cifar10-32'

								snake_case_           =							UNetaDModel.from_pretrained(a )
								snake_case_           =							DDPMScheduler.from_pretrained(a )

								snake_case_           =							DDPMPipeline(unet=a	,      scheduler=a )
								ddpm.to(a )
								ddpm.set_progress_bar_config(disable=a )

								snake_case_           =							torch.manual_seed(0 )
								snake_case_           =							ddpm(generator=a	,      output_type='numpy' ).images

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

								assert image.shape == (1, 32, 32, 3)
								snake_case_           =							np.array([0.4_200, 0.3_588, 0.1_939, 0.3_847, 0.3_382, 0.2_647, 0.4_155, 0.3_582, 0.3_385] )
								assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2













import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
	import transformer_engine.pytorch as te





def 						__UpperCAmelCase      (					a_							,					a_=True							,					a_=True							,					a_=True):
							if not is_fpa_available():
														raise ImportError('Using `convert_model` requires transformer_engine to be installed.')
							for name, module in model.named_children():
														if isinstance(a_							,					nn.Linear) and to_transformer_engine and _convert_linear:
																					# Return early if the linear layer weights are not multiples of 16
																					if any(p % 16 != 0 for p in module.weight.shape):
																												return
																					snake_case_           =							module.bias is not None
																					snake_case_           =							te.Linear(
																					    module.in_features							,					module.out_features							,					bias=a_							,					params_dtype=module.weight.dtype)
																					snake_case_           =							module.weight.data.clone()
																					if has_bias:
																												snake_case_           =							module.bias.data.clone()

																					setattr(a_							,					a_							,					a_)
														elif isinstance(a_							,					nn.LayerNorm) and to_transformer_engine and _convert_ln:
																					snake_case_           =							te.LayerNorm(module.normalized_shape[0]							,					eps=module.eps							,					params_dtype=module.weight.dtype)
																					snake_case_           =							module.weight.data.clone()
																					snake_case_           =							module.bias.data.clone()

																					setattr(a_							,					a_							,					a_)
														elif isinstance(a_							,					te.Linear) and not to_transformer_engine and _convert_linear:
																					snake_case_           =							module.bias is not None
																					snake_case_           =							nn.Linear(
																					    module.in_features							,					module.out_features							,					bias=a_							,					params_dtype=module.weight.dtype)
																					snake_case_           =							module.weight.data.clone()
																					if has_bias:
																												snake_case_           =							module.bias.data.clone()

																					setattr(a_							,					a_							,					a_)
														elif isinstance(a_							,					te.LayerNorm) and not to_transformer_engine and _convert_ln:
																					snake_case_           =							nn.LayerNorm(module.normalized_shape[0]							,					eps=module.eps							,					params_dtype=module.weight.dtype)
																					snake_case_           =							module.weight.data.clone()
																					snake_case_           =							module.bias.data.clone()

																					setattr(a_							,					a_							,					a_)
														else:
																					convert_model(
																					    a_							,					to_transformer_engine=a_							,					_convert_linear=_convert_linear							,					_convert_ln=_convert_ln							,					)





def 						__UpperCAmelCase      (					a_):
							if not is_fpa_available():
														raise ImportError('Using `has_transformer_engine_layers` requires transformer_engine to be installed.')
							for m in model.modules():
														if isinstance(a_							,					(te.LayerNorm, te.Linear)):
																					return True
							return False





