

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




				@property
				def 					a							(      self ):
											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   ,     snake_case=True   ,     snake_case=False   ,     snake_case=False   ,     snake_case=False   ,     ):
											snake_case_           =					4
											snake_case_           =					32
											snake_case_           =					(32, 32)

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

											if include_temb:
																		snake_case_           =					128
																		snake_case_           =					randn_tensor((batch_size, temb_channels)   ,     generator=A__   ,     device=A__ )

											if include_res_hidden_states_tuple:
																		snake_case_           =					torch.manual_seed(1 )
																		snake_case_           =					(randn_tensor(A__   ,     generator=A__   ,     device=A__ ),)

											if include_encoder_hidden_states:
																		snake_case_           =					floats_tensor((batch_size, 32, 32) ).to(A__ )

											if include_skip_sample:
																		snake_case_           =					randn_tensor(((batch_size, 3) + sizes)   ,     generator=A__   ,     device=A__ )

											return dummy_input




				def 					a							(      self ):
											snake_case_           =					{
											    """in_channels""": 32,
											    """out_channels""": 32,
											    """temb_channels""": 128,
											}
											if self.block_type == "up":
																		snake_case_           =					32

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

											snake_case_           =					self.dummy_input
											return init_dict, inputs_dict




				def 					a							(      self   ,     snake_case ):
											snake_case_           =					self.prepare_init_args_and_inputs_for_common()
											snake_case_           =					self.block_class(**A__ )
											unet_block.to(A__ )
											unet_block.eval()

											with torch.no_grad():
																		snake_case_           =					unet_block(**A__ )

											if isinstance(A__   ,     A__ ):
																		snake_case_           =					output[0]

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

											snake_case_           =					output[0, -1, -3:, -3:]
											snake_case_           =					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 					a							(      self ):
											snake_case_           =					self.prepare_init_args_and_inputs_for_common()
											snake_case_           =					self.block_class(**A__ )
											model.to(A__ )
											model.train()
											snake_case_           =					model(**A__ )

											if isinstance(A__   ,     A__ ):
																		snake_case_           =					output[0]

											snake_case_           =					torch.device(A__ )
											snake_case_           =					randn_tensor(output.shape   ,     device=A__ )
											snake_case_           =					torch.nn.functional.mse_loss(A__   ,     A__ )
											loss.backward()







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


_UpperCAmelCase	:     int		       = logging.get_logger(__name__)

_UpperCAmelCase	:     Any		       = {
    """facebook/levit-128S""": """https://huggingface.co/facebook/levit-128S/resolve/main/config.json""",
    # See all LeViT models at https://huggingface.co/models?filter=levit
}







class 				lowercase       ( lowercase_    ):
				__SCREAMING_SNAKE_CASE      :				int     =    '''levit'''




				def __init__(      self   ,     snake_case=224   ,     snake_case=3   ,     snake_case=3   ,     snake_case=2   ,     snake_case=1   ,     snake_case=16   ,     snake_case=[128, 256, 384]   ,     snake_case=[4, 8, 12]   ,     snake_case=[4, 4, 4]   ,     snake_case=[16, 16, 16]   ,     snake_case=0   ,     snake_case=[2, 2, 2]   ,     snake_case=[2, 2, 2]   ,     snake_case=0.02   ,     **snake_case   ,     ):
											super().__init__(**snake_case )
											snake_case_           =					image_size
											snake_case_           =					num_channels
											snake_case_           =					kernel_size
											snake_case_           =					stride
											snake_case_           =					padding
											snake_case_           =					hidden_sizes
											snake_case_           =					num_attention_heads
											snake_case_           =					depths
											snake_case_           =					key_dim
											snake_case_           =					drop_path_rate
											snake_case_           =					patch_size
											snake_case_           =					attention_ratio
											snake_case_           =					mlp_ratio
											snake_case_           =					initializer_range
											snake_case_           =					[
											    ['Subsample', key_dim[0], hidden_sizes[0] // key_dim[0], 4, 2, 2],
											    ['Subsample', key_dim[0], hidden_sizes[1] // key_dim[0], 4, 2, 2],
											]







class 				lowercase       ( lowercase_    ):
				__SCREAMING_SNAKE_CASE      :				Dict     =    version.parse('''1.11'''    )




				@property
				def 					a							(      self ):
											return OrderedDict(
											    [
											        ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}),
											    ] )




				@property
				def 					a							(      self ):
											return 1e-4




