





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    _lowercase  (    self							)   ->						Optional[Any]:



        '''simple docstring'''




        return self.get_dummy_input()







      @property
      def    _lowercase  (    self							)   ->						Any:



        '''simple docstring'''




        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    _lowercase  (    self		,      lowerCAmelCase__=True		,      lowerCAmelCase__=False		,      lowerCAmelCase__=False		,      lowerCAmelCase__=False		,      )   ->						List[str]:



        '''simple docstring'''




        a__	:					List[str]										=4
        a__	:					str										=3_2
        a__	:					Dict										=(3_2, 3_2)

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

        if include_temb:
          a__	:					List[Any]										=1_2_8
          a__	:					List[str]										=randn_tensor((batch_size, temb_channels)		,      generator=lowerCAmelCase__		,      device=lowerCAmelCase__							)

        if include_res_hidden_states_tuple:
          a__	:					Optional[int]										=torch.manual_seed(1							)
          a__	:					Tuple										=(randn_tensor(lowerCAmelCase__		,      generator=lowerCAmelCase__		,      device=lowerCAmelCase__							),)

        if include_encoder_hidden_states:
          a__	:					Dict										=floats_tensor((batch_size, 3_2, 3_2)							).to(lowerCAmelCase__							)

        if include_skip_sample:
          a__	:					Dict										=randn_tensor(((batch_size, 3) + sizes)		,      generator=lowerCAmelCase__		,      device=lowerCAmelCase__							)

        return dummy_input







      def    _lowercase  (    self							)   ->						Optional[int]:



        '''simple docstring'''




        a__	:					Optional[int]										={
            "in_channels": 3_2,
            "out_channels": 3_2,
            "temb_channels": 1_2_8,
        }
        if self.block_type == "up":
          a__	:					Any										=3_2

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

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







      def    _lowercase  (    self		,      lowerCAmelCase__							)   ->						str:



        '''simple docstring'''




        a__					,							a__	:					Union[str, Any]										=self.prepare_init_args_and_inputs_for_common()
        a__	:					Dict										=self.block_class(**lowerCAmelCase__							)
        unet_block.to(lowerCAmelCase__							)
        unet_block.eval()

        with torch.no_grad():
          a__	:					List[Any]										=unet_block(**lowerCAmelCase__							)

        if isinstance(lowerCAmelCase__		,      lowerCAmelCase__							):
          a__	:					str										=output[0]

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

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







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



        '''simple docstring'''




        a__					,							a__	:					Dict										=self.prepare_init_args_and_inputs_for_common()
        a__	:					Any										=self.block_class(**lowerCAmelCase__							)
        model.to(lowerCAmelCase__							)
        model.train()
        a__	:					int										=model(**lowerCAmelCase__							)

        if isinstance(lowerCAmelCase__		,      lowerCAmelCase__							):
          a__	:					List[str]										=output[0]

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













import numpy as np
def      _A       (		SCREAMING_SNAKE_CASE     :   np.array						):





  """simple docstring"""



  return 1 / (1 + np.exp(-vector						))


if __name__ == "__main__":
     import doctest

     doctest.testmod()






