





"""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  __UpperCamelCase       :






							@property
							def 					__a     (     self   )			->				List[str]:
								return self.get_dummy_input()






							@property
							def 					__a     (     self   )			->				List[str]:
								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   ,      lowerCAmelCase__=True   ,      lowerCAmelCase__=False   ,      lowerCAmelCase__=False   ,      lowerCAmelCase__=False   ,      )			->				List[Any]:
								a   :	Any			=       4
								a   :	List[str]			=       32
								a   :	Dict			=       (32, 32)

								a   :	Union[str, Any]			=       torch.manual_seed(0   )
								a   :	Dict			=       torch.device(lowerCAmelCase__   )
								a   :	Tuple			=       (batch_size, num_channels) + sizes
								a   :	int			=       randn_tensor(lowerCAmelCase__   ,      generator=lowerCAmelCase__   ,      device=lowerCAmelCase__   )
								a   :	str			=       {"hidden_states": hidden_states}

								if include_temb:
									a   :	Dict			=       128
									a   :	Optional[int]			=       randn_tensor((batch_size, temb_channels)   ,      generator=lowerCAmelCase__   ,      device=lowerCAmelCase__   )

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

								if include_encoder_hidden_states:
									a   :	List[str]			=       floats_tensor((batch_size, 32, 32)   ).to(lowerCAmelCase__   )

								if include_skip_sample:
									a   :	Tuple			=       randn_tensor(((batch_size, 3) + sizes)   ,      generator=lowerCAmelCase__   ,      device=lowerCAmelCase__   )

								return dummy_input






							def 					__a     (     self   )			->				Dict:
								a   :	List[Any]			=       {
								    "in_channels": 32,
								    "out_channels": 32,
								    "temb_channels": 128,
								}
								if self.block_type == "up":
									a   :	Optional[Any]			=       32

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

								a   :	List[str]			=       self.dummy_input
								return init_dict, inputs_dict






							def 					__a     (     self   ,      lowerCAmelCase__   )			->				str:
								a   :	Tuple			=       self.prepare_init_args_and_inputs_for_common()
								a   :	Tuple			=       self.block_class(**lowerCAmelCase__   )
								unet_block.to(lowerCAmelCase__   )
								unet_block.eval()

								with torch.no_grad():
									a   :	int			=       unet_block(**lowerCAmelCase__   )

								if isinstance(lowerCAmelCase__   ,      lowerCAmelCase__   ):
									a   :	str			=       output[0]

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

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






							@unittest.skipIf(torch_device == "mps"   ,      "Training is not supported in mps"   )
							def 					__a     (     self   )			->				List[str]:
								a   :	Optional[Any]			=       self.prepare_init_args_and_inputs_for_common()
								a   :	Tuple			=       self.block_class(**lowerCAmelCase__   )
								model.to(lowerCAmelCase__   )
								model.train()
								a   :	Optional[int]			=       model(**lowerCAmelCase__   )

								if isinstance(lowerCAmelCase__   ,      lowerCAmelCase__   ):
									a   :	List[str]			=       output[0]

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









"""simple docstring"""




from ..utils import DummyObject, requires_backends


class  __UpperCamelCase       (		metaclass=a__		):
							lowerCamelCase     :      Optional[Any]         =["""transformers""", """torch""", """note_seq"""]






							def __init__(     self   ,      *lowerCAmelCase__   ,      **lowerCAmelCase__   )			->				Union[str, Any]:
								requires_backends(self   ,      ["transformers", "torch", "note_seq"]   )






							@classmethod
							def 					__a     (     cls   ,      *lowerCAmelCase__   ,      **lowerCAmelCase__   )			->				Any:
								requires_backends(cls   ,      ["transformers", "torch", "note_seq"]   )






							@classmethod
							def 					__a     (     cls   ,      *lowerCAmelCase__   ,      **lowerCAmelCase__   )			->				int:
								requires_backends(cls   ,      ["transformers", "torch", "note_seq"]   )


