

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

  @property
  def  __lowercase							(	self		:      Union[str, Any]     ):






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

  @property
  def  __lowercase							(	self		:      Dict     ):






   '''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  __lowercase							(	self		:      List[str]       ,A		:      List[str]=True       ,A		:      Optional[int]=False       ,A		:      Any=False       ,A		:      int=False       ,):






   '''simple docstring'''
   UpperCAmelCase__       :	int        =       4
   UpperCAmelCase__       :	Tuple        =       32
   UpperCAmelCase__       :	Optional[Any]        =       (32, 32)

   UpperCAmelCase__       :	Dict        =       torch.manual_seed(0     )
   UpperCAmelCase__       :	int        =       torch.device(A     )
   UpperCAmelCase__       :	List[str]        =       (batch_size, num_channels) + sizes
   UpperCAmelCase__       :	List[Any]        =       randn_tensor(A       ,generator=A       ,device=A     )
   UpperCAmelCase__       :	List[Any]        =       {"""hidden_states""": hidden_states}

   if include_temb:
    UpperCAmelCase__       :	List[Any]        =       128
    UpperCAmelCase__       :	Any        =       randn_tensor((batch_size, temb_channels)       ,generator=A       ,device=A     )

   if include_res_hidden_states_tuple:
    UpperCAmelCase__       :	Union[str, Any]        =       torch.manual_seed(1     )
    UpperCAmelCase__       :	Tuple        =       (randn_tensor(A       ,generator=A       ,device=A     ),)

   if include_encoder_hidden_states:
    UpperCAmelCase__       :	Any        =       floats_tensor((batch_size, 32, 32)     ).to(A     )

   if include_skip_sample:
    UpperCAmelCase__       :	Dict        =       randn_tensor(((batch_size, 3) + sizes)       ,generator=A       ,device=A     )

   return dummy_input

  def  __lowercase							(	self		:      Dict     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Optional[Any]        =       {
       """in_channels""": 32,
       """out_channels""": 32,
       """temb_channels""": 128,
   }
   if self.block_type == "up":
    UpperCAmelCase__       :	Union[str, Any]        =       32

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

   UpperCAmelCase__       :	Union[str, Any]        =       self.dummy_input
   return init_dict, inputs_dict

  def  __lowercase							(	self		:      List[Any]       ,A		:      Optional[Any]     ):






   '''simple docstring'''
   UpperCAmelCase__ ,   UpperCAmelCase__       :	int        =       self.prepare_init_args_and_inputs_for_common()
   UpperCAmelCase__       :	List[Any]        =       self.block_class(**A     )
   unet_block.to(A     )
   unet_block.eval()

   with torch.no_grad():
    UpperCAmelCase__       :	Tuple        =       unet_block(**A     )

   if isinstance(A       ,A     ):
    UpperCAmelCase__       :	Optional[Any]        =       output[0]

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

   UpperCAmelCase__       :	Optional[Any]        =       output[0, -1, -3:, -3:]
   UpperCAmelCase__       :	Dict        =       torch.tensor(A     ).to(A     )
   assert torch_all_close(output_slice.flatten()       ,A       ,atol=5e-3     )




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






   '''simple docstring'''
   UpperCAmelCase__ ,   UpperCAmelCase__       :	int        =       self.prepare_init_args_and_inputs_for_common()
   UpperCAmelCase__       :	List[str]        =       self.block_class(**A     )
   model.to(A     )
   model.train()
   UpperCAmelCase__       :	List[Any]        =       model(**A     )

   if isinstance(A       ,A     ):
    UpperCAmelCase__       :	Tuple        =       output[0]

   UpperCAmelCase__       :	Any        =       torch.device(A     )
   UpperCAmelCase__       :	Dict        =       randn_tensor(output.shape       ,device=A     )
   UpperCAmelCase__       :	str        =       torch.nn.functional.mse_loss(A       ,A     )
   loss.backward()












'''simple docstring'''






import os


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_doctest_list.py
A__							:  Optional[int]    ='''.'''


if __name__ == "__main__":
      A__							:  int    =os.path.join(REPO_PATH, '''utils/documentation_tests.txt''')
      A__							:  Optional[int]    =[]
      A__							:  Tuple    =[]
      with open(doctest_file_path) as fp:
            for line in fp:
                  A__							:  Optional[Any]    =line.strip()
                  A__							:  Union[str, Any]    =os.path.join(REPO_PATH, line)
                  if not (os.path.isfile(path) or os.path.isdir(path)):
                        non_existent_paths.append(line)
                  all_paths.append(path)
      if len(non_existent_paths) > 0:
            A__							:  List[Any]    ='''\n'''.join(non_existent_paths)
            raise ValueError(F"""`utils/documentation_tests.txt` contains non-existent paths:\n{non_existent_paths}""")
      if all_paths != sorted(all_paths):
            raise ValueError('''Files in `utils/documentation_tests.txt` are not in alphabetical order.''')






