




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






   @property
   def 	_lowerCAmelCase   (		self       :		int   )			->						Dict:

         """simple docstring"""
         return self.get_dummy_input()






   @property
   def 	_lowerCAmelCase   (		self       :		int   )			->						Any:

         """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 	_lowerCAmelCase   (		self       :		List[str]   ,     UpperCamelCase       :		str=True   ,     UpperCamelCase       :		Optional[Any]=False   ,     UpperCamelCase       :		Any=False   ,     UpperCamelCase       :		Optional[Any]=False   ,     )			->						Tuple:

         """simple docstring"""
         lowerCAmelCase__      :			List[str]   =	4
         lowerCAmelCase__      :			Tuple   =	32
         lowerCAmelCase__      :			List[Any]   =	(32, 32)

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

         if include_temb:
               lowerCAmelCase__      :			List[str]   =	1_28
               lowerCAmelCase__      :			int   =	randn_tensor((batch_size, temb_channels)   ,     generator=_SCREAMING_SNAKE_CASE   ,     device=_SCREAMING_SNAKE_CASE   )

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

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

         if include_skip_sample:
               lowerCAmelCase__      :			Optional[int]   =	randn_tensor(((batch_size, 3) + sizes)   ,     generator=_SCREAMING_SNAKE_CASE   ,     device=_SCREAMING_SNAKE_CASE   )

         return dummy_input






   def 	_lowerCAmelCase   (		self       :		Any   )			->						Tuple:

         """simple docstring"""
         lowerCAmelCase__      :			List[str]   =	{
             '''in_channels''': 32,
             '''out_channels''': 32,
             '''temb_channels''': 1_28,
         }
         if self.block_type == "up":
               lowerCAmelCase__      :			int   =	32

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

         lowerCAmelCase__      :			Optional[int]   =	self.dummy_input
         return init_dict, inputs_dict






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

         """simple docstring"""
         lowerCAmelCase__      :			Dict   =	self.prepare_init_args_and_inputs_for_common()
         lowerCAmelCase__      :			Tuple   =	self.block_class(**_SCREAMING_SNAKE_CASE   )
         unet_block.to(_SCREAMING_SNAKE_CASE   )
         unet_block.eval()

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

         if isinstance(_SCREAMING_SNAKE_CASE   ,     _SCREAMING_SNAKE_CASE   ):
               lowerCAmelCase__      :			List[Any]   =	output[0]

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

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






   @unittest.skipIf(torch_device == """mps"""   ,     """Training is not supported in mps"""   )
   def 	_lowerCAmelCase   (		self       :		Optional[int]   )			->						Optional[int]:

         """simple docstring"""
         lowerCAmelCase__      :			int   =	self.prepare_init_args_and_inputs_for_common()
         lowerCAmelCase__      :			List[Any]   =	self.block_class(**_SCREAMING_SNAKE_CASE   )
         model.to(_SCREAMING_SNAKE_CASE   )
         model.train()
         lowerCAmelCase__      :			Optional[Any]   =	model(**_SCREAMING_SNAKE_CASE   )

         if isinstance(_SCREAMING_SNAKE_CASE   ,     _SCREAMING_SNAKE_CASE   ):
               lowerCAmelCase__      :			List[Any]   =	output[0]

         lowerCAmelCase__      :			Tuple   =	torch.device(_SCREAMING_SNAKE_CASE   )
         lowerCAmelCase__      :			Optional[Any]   =	randn_tensor(output.shape   ,     device=_SCREAMING_SNAKE_CASE   )
         lowerCAmelCase__      :			str   =	torch.nn.functional.mse_loss(_SCREAMING_SNAKE_CASE   ,     _SCREAMING_SNAKE_CASE   )
         loss.backward()











"""simple docstring"""


def 	lowercase_				(			__UpperCAmelCase				)						->    str:
      if not all(char in """01""" for char in bin_string				):
            raise ValueError("""Non-binary value was passed to the function"""				)
      if not bin_string:
            raise ValueError("""Empty string was passed to the function"""				)
      lowerCAmelCase__      :			List[str]   =	""""""
      while len(__UpperCAmelCase				) % 3 != 0:
            lowerCAmelCase__      :			Optional[Any]   =	"""0""" + bin_string
      lowerCAmelCase__      :			List[Any]   =	[
          bin_string[index : index + 3]
          for index in range(len(__UpperCAmelCase				)				)
          if index % 3 == 0
      ]
      for bin_group in bin_string_in_3_list:
            lowerCAmelCase__      :			Optional[int]   =	0
            for index, val in enumerate(__UpperCAmelCase				):
                  oct_val += int(2 ** (2 - index) * int(__UpperCAmelCase				)				)
            oct_string += str(__UpperCAmelCase				)
      return oct_string


if __name__ == "__main__":
  from doctest import testmod

  testmod()





