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




   @property
   def 			__a		(					self    :       Optional[int]   ):



     """simple docstring"""






     return self.get_dummy_input()




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



     """simple docstring"""






     SCREAMING_SNAKE_CASE__    							=   4
     SCREAMING_SNAKE_CASE__    							=   32
     SCREAMING_SNAKE_CASE__    							=   (32, 32)

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

     if include_temb:
       SCREAMING_SNAKE_CASE__    							=   1_28
       SCREAMING_SNAKE_CASE__    							=   randn_tensor((batch_size, temb_channels) ,			generator=UpperCamelCase__ ,			device=UpperCamelCase__   )

     if include_res_hidden_states_tuple:
       SCREAMING_SNAKE_CASE__    							=   torch.manual_seed(1   )
       SCREAMING_SNAKE_CASE__    							=   (randn_tensor(UpperCamelCase__ ,			generator=UpperCamelCase__ ,			device=UpperCamelCase__   ),)

     if include_encoder_hidden_states:
       SCREAMING_SNAKE_CASE__    							=   floats_tensor((batch_size, 32, 32)   ).to(UpperCamelCase__   )

     if include_skip_sample:
       SCREAMING_SNAKE_CASE__    							=   randn_tensor(((batch_size, 3) + sizes) ,			generator=UpperCamelCase__ ,			device=UpperCamelCase__   )

     return dummy_input




   def 			__a		(					self    :       Tuple   ):



     """simple docstring"""






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

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

     SCREAMING_SNAKE_CASE__    							=   self.dummy_input
     return init_dict, inputs_dict




   def 			__a		(					self    :       Dict ,			_lowercase    :       Union[str, Any]   ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__    							=   self.prepare_init_args_and_inputs_for_common()
     SCREAMING_SNAKE_CASE__    							=   self.block_class(**UpperCamelCase__   )
     unet_block.to(UpperCamelCase__   )
     unet_block.eval()

     with torch.no_grad():
       SCREAMING_SNAKE_CASE__    							=   unet_block(**UpperCamelCase__   )

     if isinstance(UpperCamelCase__ ,			UpperCamelCase__   ):
       SCREAMING_SNAKE_CASE__    							=   output[0]

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

     SCREAMING_SNAKE_CASE__    							=   output[0, -1, -3:, -3:]
     SCREAMING_SNAKE_CASE__    							=   torch.tensor(UpperCamelCase__   ).to(UpperCamelCase__   )
     assert torch_all_close(output_slice.flatten() ,			UpperCamelCase__ ,			atol=5E-3   )




   @unittest.skipIf(torch_device == """mps""" ,			"""Training is not supported in mps"""   )
   def 			__a		(					self    :       int   ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__    							=   self.prepare_init_args_and_inputs_for_common()
     SCREAMING_SNAKE_CASE__    							=   self.block_class(**UpperCamelCase__   )
     model.to(UpperCamelCase__   )
     model.train()
     SCREAMING_SNAKE_CASE__    							=   model(**UpperCamelCase__   )

     if isinstance(UpperCamelCase__ ,			UpperCamelCase__   ):
       SCREAMING_SNAKE_CASE__    							=   output[0]

     SCREAMING_SNAKE_CASE__    							=   torch.device(UpperCamelCase__   )
     SCREAMING_SNAKE_CASE__    							=   randn_tensor(output.shape ,			device=UpperCamelCase__   )
     SCREAMING_SNAKE_CASE__    							=   torch.nn.functional.mse_loss(UpperCamelCase__ ,			UpperCamelCase__   )
     loss.backward()

from __future__ import annotations

from collections.abc import Iterator
from typing import Any







class __snake_case       :




   def __init__(					self    :       Union[str, Any] ,			_lowercase    :       Any   ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__    							=   data
     SCREAMING_SNAKE_CASE__    							=   None







class __snake_case       :




   def __init__(					self    :       List[Any]   ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__    							=   None
     SCREAMING_SNAKE_CASE__    							=   None




   def __iter__(					self    :       Tuple   ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__    							=   self.head
     while self.head:
       yield node.data
       SCREAMING_SNAKE_CASE__    							=   node.next
       if node == self.head:
         break




   def __len__(					self    :       int   ):



     """simple docstring"""






     return sum(1 for _ in self   )




   def __repr__(					self    :       Optional[int]   ):



     """simple docstring"""






     return "->".join(str(_lowercase   ) for item in iter(self   )   )




   def 			__a		(					self    :       Optional[int] ,			_lowercase    :       Any   ):



     """simple docstring"""






     self.insert_nth(len(self   ) ,			_lowercase   )




   def 			__a		(					self    :       Tuple ,			_lowercase    :       Any   ):



     """simple docstring"""






     self.insert_nth(0 ,			_lowercase   )




   def 			__a		(					self    :       Tuple ,			_lowercase    :       int ,			_lowercase    :       Any   ):



     """simple docstring"""






     if index < 0 or index > len(self   ):
       raise IndexError("""list index out of range."""   )
     SCREAMING_SNAKE_CASE__    							=   Node(_lowercase   )
     if self.head is None:
       SCREAMING_SNAKE_CASE__    							=   new_node  # first node points itself
       SCREAMING_SNAKE_CASE__    							=   SCREAMING_SNAKE_CASE__    							=   new_node
     elif index == 0:  # insert at head
       SCREAMING_SNAKE_CASE__    							=   self.head
       SCREAMING_SNAKE_CASE__    							=   SCREAMING_SNAKE_CASE__    							=   new_node
     else:
       SCREAMING_SNAKE_CASE__    							=   self.head
       for _ in range(index - 1   ):
         SCREAMING_SNAKE_CASE__    							=   temp.next
       SCREAMING_SNAKE_CASE__    							=   temp.next
       SCREAMING_SNAKE_CASE__    							=   new_node
       if index == len(self   ) - 1:  # insert at tail
         SCREAMING_SNAKE_CASE__    							=   new_node




   def 			__a		(					self    :       Dict   ):



     """simple docstring"""






     return self.delete_nth(0   )




   def 			__a		(					self    :       str   ):



     """simple docstring"""






     return self.delete_nth(len(self   ) - 1   )




   def 			__a		(					self    :       str ,			_lowercase    :       int = 0   ):



     """simple docstring"""






     if not 0 <= index < len(self   ):
       raise IndexError("""list index out of range."""   )
     SCREAMING_SNAKE_CASE__    							=   self.head
     if self.head == self.tail:  # just one node
       SCREAMING_SNAKE_CASE__    							=   SCREAMING_SNAKE_CASE__    							=   None
     elif index == 0:  # delete head node
       SCREAMING_SNAKE_CASE__    							=   self.tail.next.next
       SCREAMING_SNAKE_CASE__    							=   self.head.next
     else:
       SCREAMING_SNAKE_CASE__    							=   self.head
       for _ in range(index - 1   ):
         SCREAMING_SNAKE_CASE__    							=   temp.next
       SCREAMING_SNAKE_CASE__    							=   temp.next
       SCREAMING_SNAKE_CASE__    							=   temp.next.next
       if index == len(self   ) - 1:  # delete at tail
         SCREAMING_SNAKE_CASE__    							=   temp
     return delete_node.data




   def 			__a		(					self    :       Optional[int]   ):



     """simple docstring"""






     return len(self   ) == 0





def 			__SCREAMING_SNAKE_CASE						(      )							->      None:







  """simple docstring"""





  SCREAMING_SNAKE_CASE__    							=   CircularLinkedList()
  assert len(__UpperCamelCase			) == 0
  assert circular_linked_list.is_empty() is True
  assert str(__UpperCamelCase			) == ""

  try:
    circular_linked_list.delete_front()
    raise AssertionError  # This should not happen
  except IndexError:
    assert True  # This should happen

  try:
    circular_linked_list.delete_tail()
    raise AssertionError  # This should not happen
  except IndexError:
    assert True  # This should happen

  try:
    circular_linked_list.delete_nth(-1			)
    raise AssertionError
  except IndexError:
    assert True

  try:
    circular_linked_list.delete_nth(0			)
    raise AssertionError
  except IndexError:
    assert True

  assert circular_linked_list.is_empty() is True
  for i in range(5			):
    assert len(__UpperCamelCase			) == i
    circular_linked_list.insert_nth(__UpperCamelCase				,  i + 1			)
  assert str(__UpperCamelCase			) == "->".join(str(__UpperCamelCase			) for i in range(1				,  6			)			)

  circular_linked_list.insert_tail(6			)
  assert str(__UpperCamelCase			) == "->".join(str(__UpperCamelCase			) for i in range(1				,  7			)			)
  circular_linked_list.insert_head(0			)
  assert str(__UpperCamelCase			) == "->".join(str(__UpperCamelCase			) for i in range(0				,  7			)			)

  assert circular_linked_list.delete_front() == 0
  assert circular_linked_list.delete_tail() == 6
  assert str(__UpperCamelCase			) == "->".join(str(__UpperCamelCase			) for i in range(1				,  6			)			)
  assert circular_linked_list.delete_nth(2			) == 3

  circular_linked_list.insert_nth(2				,  3			)
  assert str(__UpperCamelCase			) == "->".join(str(__UpperCamelCase			) for i in range(1				,  6			)			)

  assert circular_linked_list.is_empty() is False


if __name__ == "__main__":
   import doctest

   doctest.testmod()
