



"""simple docstring"""






from collections.abc import Iterator, MutableMapping
from dataclasses import dataclass
from typing import Generic, TypeVar

__snake_case							:     Tuple   =					TypeVar('KEY')
__snake_case							:     Dict   =					TypeVar('VAL')



@dataclass(frozen=__SCREAMING_SNAKE_CASE						,	slots=__SCREAMING_SNAKE_CASE							)
class     A__     (						Generic[KEY, VAL]							):




	'''simple docstring'''





	SCREAMING_SNAKE_CASE					   =			42
	SCREAMING_SNAKE_CASE					   =			42






class     A__     (						_Item							):




	'''simple docstring'''





	def __init__(  self:	int)       -> None:

				"""simple docstring"""

				super().__init__(_SCREAMING_SNAKE_CASE ,    _SCREAMING_SNAKE_CASE)







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

				"""simple docstring"""

				return False






__snake_case							:     List[str]   =					_DeletedItem()



class     A__     (						MutableMapping[KEY, VAL]							):




	'''simple docstring'''





	def __init__(  self:	Dict ,    _SCREAMING_SNAKE_CASE:	int = 8 ,    _SCREAMING_SNAKE_CASE:	float = 0.75)       -> None:

				"""simple docstring"""

				__lowerCAmelCase   :						str				= initial_block_size
				__lowerCAmelCase   :						list[_Item | None]				= [None] * initial_block_size
				assert 0.0 < capacity_factor < 1.0
				__lowerCAmelCase   :						int				= capacity_factor
				__lowerCAmelCase   :						Dict				= 0

	def 							_SCREAMING_SNAKE_CASE     (  self:	Any ,    _SCREAMING_SNAKE_CASE:	KEY)       -> int:

				"""simple docstring"""

				return hash(_SCREAMING_SNAKE_CASE) % len(self._buckets)

	def 							_SCREAMING_SNAKE_CASE     (  self:	Dict ,    _SCREAMING_SNAKE_CASE:	int)       -> int:

				"""simple docstring"""

				return (ind + 1) % len(self._buckets)

	def 							_SCREAMING_SNAKE_CASE     (  self:	Optional[Any] ,    _SCREAMING_SNAKE_CASE:	int ,    _SCREAMING_SNAKE_CASE:	KEY ,    _SCREAMING_SNAKE_CASE:	VAL)       -> bool:

				"""simple docstring"""

				__lowerCAmelCase   :						List[Any]				= self._buckets[ind]
				if not stored:
							__lowerCAmelCase   :						str				= _Item(_SCREAMING_SNAKE_CASE ,    _SCREAMING_SNAKE_CASE)
							self._len += 1
							return True
				elif stored.key == key:
							__lowerCAmelCase   :						Any				= _Item(_SCREAMING_SNAKE_CASE ,    _SCREAMING_SNAKE_CASE)
							return True
				else:
							return False

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

				"""simple docstring"""

				__lowerCAmelCase   :						Dict				= len(self._buckets) * self._capacity_factor
				return len(self) >= int(_SCREAMING_SNAKE_CASE)

	def 							_SCREAMING_SNAKE_CASE     (  self:	str)       -> bool:

				"""simple docstring"""

				if len(self._buckets) <= self._initial_block_size:
							return False
				__lowerCAmelCase   :						List[Any]				= len(self._buckets) * self._capacity_factor / 2
				return len(self) < limit

	def 							_SCREAMING_SNAKE_CASE     (  self:	Tuple ,    _SCREAMING_SNAKE_CASE:	int)       -> None:

				"""simple docstring"""

				__lowerCAmelCase   :						str				= self._buckets
				__lowerCAmelCase   :						List[Any]				= [None] * new_size
				__lowerCAmelCase   :						Dict				= 0
				for item in old_buckets:
							if item:
										self._add_item(item.key ,    item.val)

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

				"""simple docstring"""

				self._resize(len(self._buckets) * 2)

	def 							_SCREAMING_SNAKE_CASE     (  self:	Dict)       -> None:

				"""simple docstring"""

				self._resize(len(self._buckets) // 2)

	def 							_SCREAMING_SNAKE_CASE     (  self:	List[str] ,    _SCREAMING_SNAKE_CASE:	KEY)       -> Iterator[int]:

				"""simple docstring"""

				__lowerCAmelCase   :						List[str]				= self._get_bucket_index(_SCREAMING_SNAKE_CASE)
				for _ in range(len(self._buckets)):
							yield ind
							__lowerCAmelCase   :						Optional[int]				= self._get_next_ind(_SCREAMING_SNAKE_CASE)

	def 							_SCREAMING_SNAKE_CASE     (  self:	Tuple ,    _SCREAMING_SNAKE_CASE:	KEY ,    _SCREAMING_SNAKE_CASE:	VAL)       -> None:

				"""simple docstring"""

				for ind in self._iterate_buckets(_SCREAMING_SNAKE_CASE):
							if self._try_set(_SCREAMING_SNAKE_CASE ,    _SCREAMING_SNAKE_CASE ,    _SCREAMING_SNAKE_CASE):
										break

	def __setitem__(  self:	Union[str, Any] ,    _SCREAMING_SNAKE_CASE:	KEY ,    _SCREAMING_SNAKE_CASE:	VAL)       -> None:

				"""simple docstring"""

				if self._is_full():
							self._size_up()

				self._add_item(_SCREAMING_SNAKE_CASE ,    _SCREAMING_SNAKE_CASE)

	def __delitem__(  self:	List[str] ,    _SCREAMING_SNAKE_CASE:	KEY)       -> None:

				"""simple docstring"""

				for ind in self._iterate_buckets(_SCREAMING_SNAKE_CASE):
							__lowerCAmelCase   :						Tuple				= self._buckets[ind]
							if item is None:
										raise KeyError(_SCREAMING_SNAKE_CASE)
							if item is _deleted:
										continue
							if item.key == key:
										__lowerCAmelCase   :						str				= _deleted
										self._len -= 1
										break
				if self._is_sparse():
							self._size_down()

	def __getitem__(  self:	Optional[int] ,    _SCREAMING_SNAKE_CASE:	KEY)       -> VAL:

				"""simple docstring"""

				for ind in self._iterate_buckets(_SCREAMING_SNAKE_CASE):
							__lowerCAmelCase   :						Any				= self._buckets[ind]
							if item is None:
										break
							if item is _deleted:
										continue
							if item.key == key:
										return item.val
				raise KeyError(_SCREAMING_SNAKE_CASE)

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

				"""simple docstring"""

				return self._len

	def __iter__(  self:	List[Any])       -> Iterator[KEY]:

				"""simple docstring"""

				yield from (item.key for item in self._buckets if item)







	def __repr__(  self:	int)       -> str:

				"""simple docstring"""

				__lowerCAmelCase   :						Optional[Any]				= " ,".join(
				    F"""{item.key}: {item.val}""" for item in self._buckets if item)
				return F"""HashMap({val_string})"""




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




	'''simple docstring'''





	@property
	def 							_SCREAMING_SNAKE_CASE     (  self:	Optional[int])       -> Optional[Any]:

				"""simple docstring"""

				return self.get_dummy_input()

	@property
	def 							_SCREAMING_SNAKE_CASE     (  self:	List[Any])       -> 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 							_SCREAMING_SNAKE_CASE     (  self:	Dict ,    _SCREAMING_SNAKE_CASE:	Union[str, Any]=True ,    _SCREAMING_SNAKE_CASE:	Tuple=False ,    _SCREAMING_SNAKE_CASE:	Union[str, Any]=False ,    _SCREAMING_SNAKE_CASE:	Dict=False ,    )       -> Optional[int]:

				"""simple docstring"""

				__lowerCAmelCase   :						Union[str, Any]				= 4
				__lowerCAmelCase   :						List[str]				= 32
				__lowerCAmelCase   :						str				= (32, 32)

				__lowerCAmelCase   :						int				= torch.manual_seed(0)
				__lowerCAmelCase   :						Dict				= torch.device(_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase   :						str				= (batch_size, num_channels) + sizes
				__lowerCAmelCase   :						Dict				= randn_tensor(_SCREAMING_SNAKE_CASE ,    generator=_SCREAMING_SNAKE_CASE ,    device=_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase   :						str				= {"hidden_states": hidden_states}

				if include_temb:
							__lowerCAmelCase   :						List[str]				= 128
							__lowerCAmelCase   :						Dict				= randn_tensor((batch_size, temb_channels) ,    generator=_SCREAMING_SNAKE_CASE ,    device=_SCREAMING_SNAKE_CASE)

				if include_res_hidden_states_tuple:
							__lowerCAmelCase   :						Dict				= torch.manual_seed(1)
							__lowerCAmelCase   :						int				= (randn_tensor(_SCREAMING_SNAKE_CASE ,    generator=_SCREAMING_SNAKE_CASE ,    device=_SCREAMING_SNAKE_CASE),)

				if include_encoder_hidden_states:
							__lowerCAmelCase   :						int				= floats_tensor((batch_size, 32, 32)).to(_SCREAMING_SNAKE_CASE)

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

				return dummy_input

	def 							_SCREAMING_SNAKE_CASE     (  self:	Tuple)       -> Any:

				"""simple docstring"""

				__lowerCAmelCase   :						Optional[int]				= {
				    "in_channels": 32,
				    "out_channels": 32,
				    "temb_channels": 128,
				}
				if self.block_type == "up":
							__lowerCAmelCase   :						Tuple				= 32

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

				__lowerCAmelCase   :						str				= self.dummy_input
				return init_dict, inputs_dict

	def 							_SCREAMING_SNAKE_CASE     (  self:	List[str] ,    _SCREAMING_SNAKE_CASE:	List[str])       -> Optional[int]:

				"""simple docstring"""

				__lowerCAmelCase		,			__lowerCAmelCase   :						List[Any]				= self.prepare_init_args_and_inputs_for_common()
				__lowerCAmelCase   :						Dict				= 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   :						str				= output[0, -1, -3:, -3:]
				__lowerCAmelCase   :						List[Any]				= 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 							_SCREAMING_SNAKE_CASE     (  self:	int)       -> Tuple:

				"""simple docstring"""

				__lowerCAmelCase		,			__lowerCAmelCase   :						Optional[Any]				= self.prepare_init_args_and_inputs_for_common()
				__lowerCAmelCase   :						str				= self.block_class(**_SCREAMING_SNAKE_CASE)
				model.to(_SCREAMING_SNAKE_CASE)
				model.train()
				__lowerCAmelCase   :						Union[str, Any]				= model(**_SCREAMING_SNAKE_CASE)

				if isinstance(_SCREAMING_SNAKE_CASE ,    _SCREAMING_SNAKE_CASE):
							__lowerCAmelCase   :						Union[str, Any]				= output[0]

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