






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


    '''simple docstring'''

    @property
    def    a__				(					self     :     int     )							->							Any:

     '''simple docstring'''




     return self.get_dummy_input()

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

     '''simple docstring'''




     lowerCamelCase__  =     4
     lowerCamelCase__  =     32
     lowerCamelCase__  =     (32, 32)

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

     if include_temb:
      lowerCamelCase__  =     128
      lowerCamelCase__  =     randn_tensor((batch_size, temb_channels)				,     generator=__lowerCamelCase				,     device=__lowerCamelCase     )

     if include_res_hidden_states_tuple:
      lowerCamelCase__  =     torch.manual_seed(1     )
      lowerCamelCase__  =     (randn_tensor(__lowerCamelCase				,     generator=__lowerCamelCase				,     device=__lowerCamelCase     ),)

     if include_encoder_hidden_states:
      lowerCamelCase__  =     floats_tensor((batch_size, 32, 32)     ).to(__lowerCamelCase     )

     if include_skip_sample:
      lowerCamelCase__  =     randn_tensor(((batch_size, 3) + sizes)				,     generator=__lowerCamelCase				,     device=__lowerCamelCase     )

     return dummy_input

    def    a__				(					self     :     Optional[int]     )							->							str:

     '''simple docstring'''




     lowerCamelCase__  =     {
         "in_channels": 32,
         "out_channels": 32,
         "temb_channels": 128,
     }
     if self.block_type == "up":
      lowerCamelCase__  =     32

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

     lowerCamelCase__  =     self.dummy_input
     return init_dict, inputs_dict

    def    a__				(					self     :     Dict				,     __lowerCamelCase     :     Optional[Any]     )							->							Dict:

     '''simple docstring'''




     lowerCamelCase__  ,	lowerCamelCase__  =     self.prepare_init_args_and_inputs_for_common()
     lowerCamelCase__  =     self.block_class(**__lowerCamelCase     )
     unet_block.to(__lowerCamelCase     )
     unet_block.eval()

     with torch.no_grad():
      lowerCamelCase__  =     unet_block(**__lowerCamelCase     )

     if isinstance(__lowerCamelCase				,     __lowerCamelCase     ):
      lowerCamelCase__  =     output[0]

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

     lowerCamelCase__  =     output[0, -1, -3:, -3:]
     lowerCamelCase__  =     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    a__				(					self     :     str     )							->							int:

     '''simple docstring'''




     lowerCamelCase__  ,	lowerCamelCase__  =     self.prepare_init_args_and_inputs_for_common()
     lowerCamelCase__  =     self.block_class(**__lowerCamelCase     )
     model.to(__lowerCamelCase     )
     model.train()
     lowerCamelCase__  =     model(**__lowerCamelCase     )

     if isinstance(__lowerCamelCase				,     __lowerCamelCase     ):
      lowerCamelCase__  =     output[0]

     lowerCamelCase__  =     torch.device(__lowerCamelCase     )
     lowerCamelCase__  =     randn_tensor(output.shape				,     device=__lowerCamelCase     )
     lowerCamelCase__  =     torch.nn.functional.mse_loss(__lowerCamelCase				,     __lowerCamelCase     )
     loss.backward()









'''simple docstring'''





from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


__A							:							Optional[Any]  							=		logging.get_logger(__name__)

__A							:							int  							=		{
    """google/vit-base-patch16-224""": """https://huggingface.co/vit-base-patch16-224/resolve/main/config.json""",
    # See all ViT models at https://huggingface.co/models?filter=vit
}
class 				lowercase       (							_lowerCamelCase      ):


    '''simple docstring'''

    lowerCAmelCase__      				=							"vit"

    def __init__(					self     :     Optional[int]				,     __lowerCamelCase     :     Tuple=768				,     __lowerCamelCase     :     Tuple=12				,     __lowerCamelCase     :     Tuple=12				,     __lowerCamelCase     :     int=3072				,     __lowerCamelCase     :     Optional[Any]="gelu"				,     __lowerCamelCase     :     Optional[int]=0.0				,     __lowerCamelCase     :     Tuple=0.0				,     __lowerCamelCase     :     Optional[Any]=0.0_2				,     __lowerCamelCase     :     int=1E-12				,     __lowerCamelCase     :     Tuple=224				,     __lowerCamelCase     :     int=16				,     __lowerCamelCase     :     Optional[int]=3				,     __lowerCamelCase     :     List[str]=True				,     __lowerCamelCase     :     str=16				,     **__lowerCamelCase     :     Optional[int]				,     )							->							str:

     '''simple docstring'''




     super().__init__(**__lowerCamelCase     )

     lowerCamelCase__  =     hidden_size
     lowerCamelCase__  =     num_hidden_layers
     lowerCamelCase__  =     num_attention_heads
     lowerCamelCase__  =     intermediate_size
     lowerCamelCase__  =     hidden_act
     lowerCamelCase__  =     hidden_dropout_prob
     lowerCamelCase__  =     attention_probs_dropout_prob
     lowerCamelCase__  =     initializer_range
     lowerCamelCase__  =     layer_norm_eps
     lowerCamelCase__  =     image_size
     lowerCamelCase__  =     patch_size
     lowerCamelCase__  =     num_channels
     lowerCamelCase__  =     qkv_bias
     lowerCamelCase__  =     encoder_stride
class 				lowercase       (							_lowerCamelCase      ):


    '''simple docstring'''

    lowerCAmelCase__      				=							version.parse("1.11"      )

    @property
    def    a__				(					self     :     Optional[int]     )							->							Mapping[str, Mapping[int, str]]:

     '''simple docstring'''




     return OrderedDict(
         [
             ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}),
         ]     )




    @property
    def    a__				(					self     :     Optional[int]     )							->							float:

     '''simple docstring'''




     return 1E-4

