



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   __lowerCAmelCase  :
					"""simple docstring"""






					@property
					def      _a				(  self       :							Tuple       ):






								"""simple docstring"""


								return self.get_dummy_input()



					@property
					def      _a				(  self       :							List[str]       ):






								"""simple docstring"""


								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      _a				(  self       :							List[Any]					,       _snake_case       :							List[Any]=True					,       _snake_case       :							int=False					,       _snake_case       :							List[str]=False					,       _snake_case       :							str=False					,       ):






								"""simple docstring"""


								A__	  =     4
								A__	  =     32
								A__	  =     (32, 32)

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

								if include_temb:
											A__	  =     1_28
											A__	  =     randn_tensor((batch_size, temb_channels)					,       generator=_snake_case					,       device=_snake_case       )

								if include_res_hidden_states_tuple:
											A__	  =     torch.manual_seed(1       )
											A__	  =     (randn_tensor(_snake_case					,       generator=_snake_case					,       device=_snake_case       ),)

								if include_encoder_hidden_states:
											A__	  =     floats_tensor((batch_size, 32, 32)       ).to(_snake_case       )

								if include_skip_sample:
											A__	  =     randn_tensor(((batch_size, 3) + sizes)					,       generator=_snake_case					,       device=_snake_case       )

								return dummy_input



					def      _a				(  self       :							Dict       ):






								"""simple docstring"""


								A__	  =     {
								    'in_channels': 32,
								    'out_channels': 32,
								    'temb_channels': 1_28,
								}
								if self.block_type == "up":
											A__	  =     32

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

								A__	  =     self.dummy_input
								return init_dict, inputs_dict



					def      _a				(  self       :							Tuple					,       _snake_case       :							Optional[int]       ):






								"""simple docstring"""


								A__  ,			A__	  =     self.prepare_init_args_and_inputs_for_common()
								A__	  =     self.block_class(**_snake_case       )
								unet_block.to(_snake_case       )
								unet_block.eval()

								with torch.no_grad():
											A__	  =     unet_block(**_snake_case       )

								if isinstance(_snake_case					,       _snake_case       ):
											A__	  =     output[0]

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

								A__	  =     output[0, -1, -3:, -3:]
								A__	  =     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      _a				(  self       :							Union[str, Any]       ):






								"""simple docstring"""


								A__  ,			A__	  =     self.prepare_init_args_and_inputs_for_common()
								A__	  =     self.block_class(**_snake_case       )
								model.to(_snake_case       )
								model.train()
								A__	  =     model(**_snake_case       )

								if isinstance(_snake_case					,       _snake_case       ):
											A__	  =     output[0]

								A__	  =     torch.device(_snake_case       )
								A__	  =     randn_tensor(output.shape					,       device=_snake_case       )
								A__	  =     torch.nn.functional.mse_loss(_snake_case					,       _snake_case       )
								loss.backward()









SCREAMING_SNAKE_CASE__   			=   '''
# Transformers installation
! pip install transformers datasets
# To install from source instead of the last release, comment the command above and uncomment the following one.
# ! pip install git+https://github.com/huggingface/transformers.git
'''

SCREAMING_SNAKE_CASE__   			=   [{'''type''': '''code''', '''content''': INSTALL_CONTENT}]
SCREAMING_SNAKE_CASE__   			=   {
    '''{processor_class}''': '''FakeProcessorClass''',
    '''{model_class}''': '''FakeModelClass''',
    '''{object_class}''': '''FakeObjectClass''',
}




