

"""simple docstring"""





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 				SCREAMING_SNAKE_CASE					:






					"""simple docstring"""



					@property
					def        __lowerCAmelCase (       self			:				Tuple		):
						return self.get_dummy_input()







					@property
					def        __lowerCAmelCase (       self			:				Dict		):
						if self.block_type == "down":
							return (4, 3_2, 1_6, 1_6)
						elif self.block_type == "mid":
							return (4, 3_2, 3_2, 3_2)
						elif self.block_type == "up":
							return (4, 3_2, 6_4, 6_4)

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







					def        __lowerCAmelCase (       self			:				int   ,lowercase_			:				str=True   ,lowercase_			:				List[str]=False   ,lowercase_			:				str=False   ,lowercase_			:				List[str]=False   ,):
						lowerCAmelCase__	:						Any  				=	4
						lowerCAmelCase__	:						Union[str, Any]  				=	3_2
						lowerCAmelCase__	:						int  				=	(3_2, 3_2)

						lowerCAmelCase__	:						str  				=	torch.manual_seed(0		)
						lowerCAmelCase__	:						List[Any]  				=	torch.device(lowercase_		)
						lowerCAmelCase__	:						Optional[int]  				=	(batch_size, num_channels) + sizes
						lowerCAmelCase__	:						Union[str, Any]  				=	randn_tensor(lowercase_   ,generator=lowercase_   ,device=lowercase_		)
						lowerCAmelCase__	:						int  				=	{'''hidden_states''': hidden_states}

						if include_temb:
							lowerCAmelCase__	:						List[str]  				=	1_2_8
							lowerCAmelCase__	:						Tuple  				=	randn_tensor((batch_size, temb_channels)   ,generator=lowercase_   ,device=lowercase_		)

						if include_res_hidden_states_tuple:
							lowerCAmelCase__	:						Optional[int]  				=	torch.manual_seed(1		)
							lowerCAmelCase__	:						Optional[Any]  				=	(randn_tensor(lowercase_   ,generator=lowercase_   ,device=lowercase_		),)

						if include_encoder_hidden_states:
							lowerCAmelCase__	:						int  				=	floats_tensor((batch_size, 3_2, 3_2)		).to(lowercase_		)

						if include_skip_sample:
							lowerCAmelCase__	:						List[str]  				=	randn_tensor(((batch_size, 3) + sizes)   ,generator=lowercase_   ,device=lowercase_		)

						return dummy_input







					def        __lowerCAmelCase (       self			:				Optional[int]		):
						lowerCAmelCase__	:						List[Any]  				=	{
						    '''in_channels''': 3_2,
						    '''out_channels''': 3_2,
						    '''temb_channels''': 1_2_8,
						}
						if self.block_type == "up":
							lowerCAmelCase__	:						str  				=	3_2

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

						lowerCAmelCase__	:						Union[str, Any]  				=	self.dummy_input
						return init_dict, inputs_dict







					def        __lowerCAmelCase (       self			:				List[str]   ,lowercase_			:				Optional[int]		):
						lowerCAmelCase__				,lowerCAmelCase__	:						Optional[int]  				=	self.prepare_init_args_and_inputs_for_common()
						lowerCAmelCase__	:						Dict  				=	self.block_class(**lowercase_		)
						unet_block.to(lowercase_		)
						unet_block.eval()

						with torch.no_grad():
							lowerCAmelCase__	:						Any  				=	unet_block(**lowercase_		)

						if isinstance(lowercase_   ,lowercase_		):
							lowerCAmelCase__	:						Optional[Any]  				=	output[0]

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

						lowerCAmelCase__	:						List[str]  				=	output[0, -1, -3:, -3:]
						lowerCAmelCase__	:						List[str]  				=	torch.tensor(lowercase_		).to(lowercase_		)
						assert torch_all_close(output_slice.flatten()   ,lowercase_   ,atol=5E-3		)







					@unittest.skipIf(torch_device == '''mps'''   ,'''Training is not supported in mps'''		)
					def        __lowerCAmelCase (       self			:				Optional[int]		):
						lowerCAmelCase__				,lowerCAmelCase__	:						Optional[Any]  				=	self.prepare_init_args_and_inputs_for_common()
						lowerCAmelCase__	:						Optional[int]  				=	self.block_class(**lowercase_		)
						model.to(lowercase_		)
						model.train()
						lowerCAmelCase__	:						Dict  				=	model(**lowercase_		)

						if isinstance(lowercase_   ,lowercase_		):
							lowerCAmelCase__	:						Union[str, Any]  				=	output[0]

						lowerCAmelCase__	:						int  				=	torch.device(lowercase_		)
						lowerCAmelCase__	:						List[str]  				=	randn_tensor(output.shape   ,device=lowercase_		)
						lowerCAmelCase__	:						int  				=	torch.nn.functional.mse_loss(lowercase_   ,lowercase_		)
						loss.backward()







"""simple docstring"""





from ..utils import DummyObject, requires_backends







class 				SCREAMING_SNAKE_CASE					(		metaclass=a_       ):






					"""simple docstring"""



					lowercase__				 =       ["speech"]







					def __init__(       self			:				Tuple   ,*lowercase_			:				Tuple   ,**lowercase_			:				List[str]		):
						requires_backends(self   ,['''speech''']		)







class 				SCREAMING_SNAKE_CASE					(		metaclass=a_       ):






					"""simple docstring"""



					lowercase__				 =       ["speech"]







					def __init__(       self			:				Union[str, Any]   ,*lowercase_			:				List[str]   ,**lowercase_			:				Any		):
						requires_backends(self   ,['''speech''']		)




