






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 			__UpperCAmelCase		(						self						)->						Any:
           return self.get_dummy_input()


      @property
      def 			__UpperCAmelCase		(						self						)->						int:
           if self.block_type == "down":
                return (4, 3_2, 1_6, 1_6)
           elif self.block_type == "mid":
                return (4, 3_2, 3_2, 3_2)
           elif self.block_type == "up":
                return (4, 3_2, 6_4, 6_4)

           raise ValueError(F"""'{self.block_type}' is not a supported block_type. Set it to 'up', 'mid', or 'down'."""						)


      def 			__UpperCAmelCase		(						self       ,    __UpperCamelCase=True       ,    __UpperCamelCase=False       ,    __UpperCamelCase=False       ,    __UpperCamelCase=False       ,    )->						Dict:
           __lowerCAmelCase             =       4
           __lowerCAmelCase             =       3_2
           __lowerCAmelCase             =       (3_2, 3_2)

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

           if include_temb:
                __lowerCAmelCase             =       1_2_8
                __lowerCAmelCase             =       randn_tensor((batch_size, temb_channels)       ,    generator=__UpperCamelCase       ,    device=__UpperCamelCase						)

           if include_res_hidden_states_tuple:
                __lowerCAmelCase             =       torch.manual_seed(1						)
                __lowerCAmelCase             =       (randn_tensor(__UpperCamelCase       ,    generator=__UpperCamelCase       ,    device=__UpperCamelCase						),)

           if include_encoder_hidden_states:
                __lowerCAmelCase             =       floats_tensor((batch_size, 3_2, 3_2)						).to(__UpperCamelCase						)

           if include_skip_sample:
                __lowerCAmelCase             =       randn_tensor(((batch_size, 3) + sizes)       ,    generator=__UpperCamelCase       ,    device=__UpperCamelCase						)

           return dummy_input


      def 			__UpperCAmelCase		(						self						)->						List[Any]:
           __lowerCAmelCase             =       {
               "in_channels": 3_2,
               "out_channels": 3_2,
               "temb_channels": 1_2_8,
           }
           if self.block_type == "up":
                __lowerCAmelCase             =       3_2

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

           __lowerCAmelCase             =       self.dummy_input
           return init_dict, inputs_dict


      def 			__UpperCAmelCase		(						self       ,    __UpperCamelCase						)->						int:
           __lowerCAmelCase      ,			__lowerCAmelCase             =       self.prepare_init_args_and_inputs_for_common()
           __lowerCAmelCase             =       self.block_class(**__UpperCamelCase						)
           unet_block.to(__UpperCamelCase						)
           unet_block.eval()

           with torch.no_grad():
                __lowerCAmelCase             =       unet_block(**__UpperCamelCase						)

           if isinstance(__UpperCamelCase       ,    __UpperCamelCase						):
                __lowerCAmelCase             =       output[0]

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

           __lowerCAmelCase             =       output[0, -1, -3:, -3:]
           __lowerCAmelCase             =       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 			__UpperCAmelCase		(						self						)->						Any:
           __lowerCAmelCase      ,			__lowerCAmelCase             =       self.prepare_init_args_and_inputs_for_common()
           __lowerCAmelCase             =       self.block_class(**__UpperCamelCase						)
           model.to(__UpperCamelCase						)
           model.train()
           __lowerCAmelCase             =       model(**__UpperCamelCase						)

           if isinstance(__UpperCamelCase       ,    __UpperCamelCase						):
                __lowerCAmelCase             =       output[0]

           __lowerCAmelCase             =       torch.device(__UpperCamelCase						)
           __lowerCAmelCase             =       randn_tensor(output.shape       ,    device=__UpperCamelCase						)
           __lowerCAmelCase             =       torch.nn.functional.mse_loss(__UpperCamelCase       ,    __UpperCamelCase						)
           loss.backward()














from __future__ import annotations





def    __lowerCAmelCase   (    __snake_case							):
     __lowerCAmelCase             =       len(__snake_case							)
     # We need to create solution object to save path.
     __lowerCAmelCase             =       [[0 for _ in range(__snake_case							)] for _ in range(__snake_case							)]
     __lowerCAmelCase             =       run_maze(__snake_case       ,			0       ,			0       ,			__snake_case							)
     if solved:
          print("\n".join(str(__snake_case							) for row in solutions							)							)
     else:
          print("No solution exists!"							)
     return solved





def    __lowerCAmelCase   (    __snake_case       ,			__snake_case       ,			__snake_case       ,			__snake_case							):
     __lowerCAmelCase             =       len(__snake_case							)
     # Final check point.
     if i == j == (size - 1):
          __lowerCAmelCase             =       1
          return True

     __lowerCAmelCase             =       (not i < 0) and (not j < 0)  # Check lower bounds
     __lowerCAmelCase             =       (i < size) and (j < size)  # Check upper bounds

     if lower_flag and upper_flag:
          # check for already visited and block points.
          __lowerCAmelCase             =       (not solutions[i][j]) and (not maze[i][j])
          if block_flag:
               # check visited
               __lowerCAmelCase             =       1

               # check for directions
               if (
                   run_maze(__snake_case       ,			i + 1       ,			__snake_case       ,			__snake_case							)
                   or run_maze(__snake_case       ,			__snake_case       ,			j + 1       ,			__snake_case							)
                   or run_maze(__snake_case       ,			i - 1       ,			__snake_case       ,			__snake_case							)
                   or run_maze(__snake_case       ,			__snake_case       ,			j - 1       ,			__snake_case							)
               ):
                    return True

               __lowerCAmelCase             =       0
               return False
     return False


if __name__ == "__main__":
 import doctest

 doctest.testmod()






