
import unittest
from typing import Tuple

import torch

from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device
from diffusers.utils.testing_utils import require_torch







@require_torch
class 			A_  :




	'''simple docstring'''


	@property
	def       SCREAMING_SNAKE_CASE__		(			self			):
		return self.get_dummy_input()





	@property
	def       SCREAMING_SNAKE_CASE__		(			self			):
		if self.block_type == "down":
			return (4, 32, 16, 16)
		elif self.block_type == "mid":
			return (4, 32, 32, 32)
		elif self.block_type == "up":
			return (4, 32, 64, 64)

		raise ValueError(F'''\'{self.block_type}\' is not a supported block_type. Set it to \'up\', \'mid\', or \'down\'.'''			)





	def       SCREAMING_SNAKE_CASE__		(			self						,       snake_case=True						,       snake_case=False						,       snake_case=False						,       snake_case=False						,       ):
		lowercase	    =       4
		lowercase	    =       32
		lowercase	    =       (32, 32)

		lowercase	    =       torch.manual_seed(0			)
		lowercase	    =       torch.device(snake_case			)
		lowercase	    =       (batch_size, num_channels) + sizes
		lowercase	    =       randn_tensor(snake_case						,       generator=snake_case						,       device=snake_case			)
		lowercase	    =       {'hidden_states': hidden_states}

		if include_temb:
			lowercase	    =       128
			lowercase	    =       randn_tensor((batch_size, temb_channels)						,       generator=snake_case						,       device=snake_case			)

		if include_res_hidden_states_tuple:
			lowercase	    =       torch.manual_seed(1			)
			lowercase	    =       (randn_tensor(snake_case						,       generator=snake_case						,       device=snake_case			),)

		if include_encoder_hidden_states:
			lowercase	    =       floats_tensor((batch_size, 32, 32)			).to(snake_case			)

		if include_skip_sample:
			lowercase	    =       randn_tensor(((batch_size, 3) + sizes)						,       generator=snake_case						,       device=snake_case			)

		return dummy_input





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       {
		    'in_channels': 32,
		    'out_channels': 32,
		    'temb_channels': 128,
		}
		if self.block_type == "up":
			lowercase	    =       32

		if self.block_type == "mid":
			init_dict.pop('out_channels'			)

		lowercase	    =       self.dummy_input
		return init_dict, inputs_dict





	def       SCREAMING_SNAKE_CASE__		(			self						,       snake_case			):
		lowercase    ,   lowercase	    =       self.prepare_init_args_and_inputs_for_common()
		lowercase	    =       self.block_class(**snake_case			)
		unet_block.to(snake_case			)
		unet_block.eval()

		with torch.no_grad():
			lowercase	    =       unet_block(**snake_case			)

		if isinstance(snake_case						,       snake_case			):
			lowercase	    =       output[0]

		self.assertEqual(output.shape						,       self.output_shape			)

		lowercase	    =       output[0, -1, -3:, -3:]
		lowercase	    =       torch.tensor(snake_case			).to(snake_case			)
		assert torch_all_close(output_slice.flatten()						,       snake_case						,       atol=5E-3			)





	@unittest.skipIf(torch_device == 'mps'						,       'Training is not supported in mps'			)
	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase    ,   lowercase	    =       self.prepare_init_args_and_inputs_for_common()
		lowercase	    =       self.block_class(**snake_case			)
		model.to(snake_case			)
		model.train()
		lowercase	    =       model(**snake_case			)

		if isinstance(snake_case						,       snake_case			):
			lowercase	    =       output[0]

		lowercase	    =       torch.device(snake_case			)
		lowercase	    =       randn_tensor(output.shape						,       device=snake_case			)
		lowercase	    =       torch.nn.functional.mse_loss(snake_case						,       snake_case			)
		loss.backward()





"""simple docstring"""






import unittest

import numpy as np
import torch

from diffusers import KarrasVePipeline, KarrasVeScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()






class      A(  unittest.TestCase		):

		"""simple docstring"""





		@property
		def   _UpperCamelCase( self   )      ->	Union[str, Any]:

					"""simple docstring"""



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

					"""simple docstring"""



					_UpperCamelCase					:Dict					     =	self.dummy_uncond_unet
					_UpperCamelCase					:List[Any]					     =	KarrasVeScheduler()

					_UpperCamelCase					:Optional[Any]					     =	KarrasVePipeline(unet=SCREAMING_SNAKE_CASE__					,				scheduler=SCREAMING_SNAKE_CASE__   )
					pipe.to(SCREAMING_SNAKE_CASE__   )
					pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__   )

					_UpperCamelCase					:Dict					     =	torch.manual_seed(0   )
					_UpperCamelCase					:List[Any]					     =	pipe(num_inference_steps=2					,				generator=SCREAMING_SNAKE_CASE__					,				output_type='''numpy'''   ).images

					_UpperCamelCase					:List[Any]					     =	torch.manual_seed(0   )
					_UpperCamelCase					:Dict					     =	pipe(num_inference_steps=2					,				generator=SCREAMING_SNAKE_CASE__					,				output_type='''numpy'''					,				return_dict=SCREAMING_SNAKE_CASE__   )[0]

					_UpperCamelCase					:Dict					     =	image[0, -3:, -3:, -1]
					_UpperCamelCase					:List[Any]					     =	image_from_tuple[0, -3:, -3:, -1]

					assert image.shape == (1, 32, 32, 3)
					_UpperCamelCase					:List[Any]					     =	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		):

		"""simple docstring"""





		def   _UpperCamelCase( self   )      ->	Union[str, Any]:

					"""simple docstring"""



					_UpperCamelCase					:List[Any]					     =	'''google/ncsnpp-celebahq-256'''
					_UpperCamelCase					:Any					     =	UNetaDModel.from_pretrained(SCREAMING_SNAKE_CASE__   )
					_UpperCamelCase					:Optional[int]					     =	KarrasVeScheduler()

					_UpperCamelCase					:Optional[int]					     =	KarrasVePipeline(unet=SCREAMING_SNAKE_CASE__					,				scheduler=SCREAMING_SNAKE_CASE__   )
					pipe.to(SCREAMING_SNAKE_CASE__   )
					pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__   )

					_UpperCamelCase					:List[str]					     =	torch.manual_seed(0   )
					_UpperCamelCase					:List[Any]					     =	pipe(num_inference_steps=20					,				generator=SCREAMING_SNAKE_CASE__					,				output_type='''numpy'''   ).images

					_UpperCamelCase					:Dict					     =	image[0, -3:, -3:, -1]
					assert image.shape == (1, 2_56, 2_56, 3)
					_UpperCamelCase					:Any					     =	np.array([0.5_7_8, 0.5_8_1_1, 0.5_9_2_4, 0.5_8_0_9, 0.5_8_7, 0.5_8_8_6, 0.5_8_6_1, 0.5_8_0_2, 0.5_8_6]   )

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

