




'''simple docstring'''
import unittest

import torch

from diffusers import UNetaDModel
from diffusers.utils import floats_tensor, slow, torch_device

from .test_modeling_common import ModelTesterMixin, UNetTesterMixin






class        SCREAMING_SNAKE_CASE(				UpperCamelCase__   ,      UpperCamelCase__   ,      unittest.TestCase     ):
       snake_case_ :     List[Any]    =					UNetaDModel
       snake_case_ :     int    =					"""sample"""

       @property
       def     snake_case__			(    self     )    ->					str:

           """simple docstring"""



           __lowercase							=						4
           __lowercase							=						14
           __lowercase							=						16

           __lowercase							=						floats_tensor((batch_size, num_features, seq_len)     ).to(_a     )
           __lowercase							=						torch.tensor([10] * batch_size     ).to(_a     )

           return {"sample": noise, "timestep": time_step}

       @property
       def     snake_case__			(    self     )    ->					Dict:

           """simple docstring"""



           return (4, 14, 16)

       @property
       def     snake_case__			(    self     )    ->					Union[str, Any]:

           """simple docstring"""



           return (4, 14, 16)

       def     snake_case__			(    self     )    ->					List[str]:

           """simple docstring"""



           pass

       def     snake_case__			(    self     )    ->					List[str]:

           """simple docstring"""



           pass

       @unittest.skipIf(torch_device == """mps"""					,		"""mish op not supported in MPS"""     )
       def     snake_case__			(    self     )    ->					List[Any]:

           """simple docstring"""



           super().test_determinism()

       @unittest.skipIf(torch_device == """mps"""					,		"""mish op not supported in MPS"""     )
       def     snake_case__			(    self     )    ->					str:

           """simple docstring"""



           super().test_outputs_equivalence()

       @unittest.skipIf(torch_device == """mps"""					,		"""mish op not supported in MPS"""     )
       def     snake_case__			(    self     )    ->					Tuple:

           """simple docstring"""



           super().test_from_save_pretrained()

       @unittest.skipIf(torch_device == """mps"""					,		"""mish op not supported in MPS"""     )
       def     snake_case__			(    self     )    ->					Any:

           """simple docstring"""



           super().test_from_save_pretrained_variant()

       @unittest.skipIf(torch_device == """mps"""					,		"""mish op not supported in MPS"""     )
       def     snake_case__			(    self     )    ->					Optional[int]:

           """simple docstring"""



           super().test_model_from_pretrained()

       @unittest.skipIf(torch_device == """mps"""					,		"""mish op not supported in MPS"""     )
       def     snake_case__			(    self     )    ->					List[Any]:

           """simple docstring"""



           super().test_output()

       def     snake_case__			(    self     )    ->					List[Any]:

           """simple docstring"""



           __lowercase							=						{
               """block_out_channels""": (32, 64, 128, 256),
               """in_channels""": 14,
               """out_channels""": 14,
               """time_embedding_type""": """positional""",
               """use_timestep_embedding""": True,
               """flip_sin_to_cos""": False,
               """freq_shift""": 1.0,
               """out_block_type""": """OutConv1DBlock""",
               """mid_block_type""": """MidResTemporalBlock1D""",
               """down_block_types""": ("""DownResnetBlock1D""", """DownResnetBlock1D""", """DownResnetBlock1D""", """DownResnetBlock1D"""),
               """up_block_types""": ("""UpResnetBlock1D""", """UpResnetBlock1D""", """UpResnetBlock1D"""),
               """act_fn""": """mish""",
           }
           __lowercase							=						self.dummy_input
           return init_dict, inputs_dict

       @unittest.skipIf(torch_device == """mps"""					,		"""mish op not supported in MPS"""     )
       def     snake_case__			(    self     )    ->					int:

           """simple docstring"""



           __lowercase				,__lowercase							=						UNetaDModel.from_pretrained(
               """bglick13/hopper-medium-v2-value-function-hor32"""					,		output_loading_info=_a					,		subfolder="""unet"""     )
           self.assertIsNotNone(_a     )
           self.assertEqual(len(loading_info["""missing_keys"""]     )					,		0     )

           model.to(_a     )
           __lowercase							=						model(**self.dummy_input     )

           assert image is not None, "Make sure output is not None"

       @unittest.skipIf(torch_device == """mps"""					,		"""mish op not supported in MPS"""     )
       def     snake_case__			(    self     )    ->					Tuple:

           """simple docstring"""



           __lowercase							=						UNetaDModel.from_pretrained("""bglick13/hopper-medium-v2-value-function-hor32"""					,		subfolder="""unet"""     )
           torch.manual_seed(0     )
           if torch.cuda.is_available():
               torch.cuda.manual_seed_all(0     )

           __lowercase							=						model.config.in_channels
           __lowercase							=						16
           __lowercase							=						torch.randn((1, seq_len, num_features)     ).permute(
               0					,		2					,		1     )  # match original, we can update values and remove
           __lowercase							=						torch.full((num_features,)					,		0     )

           with torch.no_grad():
               __lowercase							=						model(_a					,		_a     ).sample.permute(0					,		2					,		1     )

           __lowercase							=						output[0, -3:, -3:].flatten()
           # fmt: off
           __lowercase							=						torch.tensor([-2.13_71_72, 1.1_42_60_16, 0.3_68_86_87, -0.76_69_22, 0.7_30_31_46, 0.11_03_88_64, -0.4_76_06_33, 0.13_27_01_72, 0.02_59_13_48]     )
           # fmt: on
           self.assertTrue(torch.allclose(_a					,		_a					,		rtol=1E-3     )     )

       def     snake_case__			(    self     )    ->					Dict:

           """simple docstring"""



           pass




       @slow
       def     snake_case__			(    self     )    ->					int:

           """simple docstring"""



           __lowercase							=						"""harmonai/maestro-150k"""
           __lowercase							=						UNetaDModel.from_pretrained(_a					,		subfolder="""unet"""     )
           model.to(_a     )

           __lowercase							=						6_5536
           __lowercase							=						torch.sin(torch.arange(_a     )[None, None, :].repeat(1					,		2					,		1     )     ).to(_a     )
           __lowercase							=						torch.tensor([1]     ).to(_a     )

           with torch.no_grad():
               __lowercase							=						model(_a					,		_a     ).sample

           __lowercase							=						output.abs().sum()
           __lowercase							=						output.abs().max()

           assert (output_sum - 224.0896).abs() < 0.5
           assert (output_max - 0.06_07).abs() < 4E-4






class        SCREAMING_SNAKE_CASE(				UpperCamelCase__   ,      UpperCamelCase__   ,      unittest.TestCase     ):
       snake_case_ :     Union[str, Any]    =					UNetaDModel
       snake_case_ :     Dict    =					"""sample"""

       @property
       def     snake_case__			(    self     )    ->					str:

           """simple docstring"""



           __lowercase							=						4
           __lowercase							=						14
           __lowercase							=						16

           __lowercase							=						floats_tensor((batch_size, num_features, seq_len)     ).to(_a     )
           __lowercase							=						torch.tensor([10] * batch_size     ).to(_a     )

           return {"sample": noise, "timestep": time_step}

       @property
       def     snake_case__			(    self     )    ->					str:

           """simple docstring"""



           return (4, 14, 16)

       @property
       def     snake_case__			(    self     )    ->					Optional[Any]:

           """simple docstring"""



           return (4, 14, 1)

       @unittest.skipIf(torch_device == """mps"""					,		"""mish op not supported in MPS"""     )
       def     snake_case__			(    self     )    ->					Optional[Any]:

           """simple docstring"""



           super().test_determinism()

       @unittest.skipIf(torch_device == """mps"""					,		"""mish op not supported in MPS"""     )
       def     snake_case__			(    self     )    ->					Tuple:

           """simple docstring"""



           super().test_outputs_equivalence()

       @unittest.skipIf(torch_device == """mps"""					,		"""mish op not supported in MPS"""     )
       def     snake_case__			(    self     )    ->					Union[str, Any]:

           """simple docstring"""



           super().test_from_save_pretrained()

       @unittest.skipIf(torch_device == """mps"""					,		"""mish op not supported in MPS"""     )
       def     snake_case__			(    self     )    ->					int:

           """simple docstring"""



           super().test_from_save_pretrained_variant()

       @unittest.skipIf(torch_device == """mps"""					,		"""mish op not supported in MPS"""     )
       def     snake_case__			(    self     )    ->					str:

           """simple docstring"""



           super().test_model_from_pretrained()

       @unittest.skipIf(torch_device == """mps"""					,		"""mish op not supported in MPS"""     )
       def     snake_case__			(    self     )    ->					Optional[Any]:

           """simple docstring"""



           __lowercase				,__lowercase							=						self.prepare_init_args_and_inputs_for_common()
           __lowercase							=						self.model_class(**_a     )
           model.to(_a     )
           model.eval()

           with torch.no_grad():
               __lowercase							=						model(**_a     )

               if isinstance(_a					,		_a     ):
                   __lowercase							=						output.sample

           self.assertIsNotNone(_a     )
           __lowercase							=						torch.Size((inputs_dict["""sample"""].shape[0], 1)     )
           self.assertEqual(output.shape					,		_a					,		"""Input and output shapes do not match"""     )

       def     snake_case__			(    self     )    ->					str:

           """simple docstring"""



           pass

       def     snake_case__			(    self     )    ->					Any:

           """simple docstring"""



           pass

       def     snake_case__			(    self     )    ->					Optional[Any]:

           """simple docstring"""



           __lowercase							=						{
               """in_channels""": 14,
               """out_channels""": 14,
               """down_block_types""": ["""DownResnetBlock1D""", """DownResnetBlock1D""", """DownResnetBlock1D""", """DownResnetBlock1D"""],
               """up_block_types""": [],
               """out_block_type""": """ValueFunction""",
               """mid_block_type""": """ValueFunctionMidBlock1D""",
               """block_out_channels""": [32, 64, 128, 256],
               """layers_per_block""": 1,
               """downsample_each_block""": True,
               """use_timestep_embedding""": True,
               """freq_shift""": 1.0,
               """flip_sin_to_cos""": False,
               """time_embedding_type""": """positional""",
               """act_fn""": """mish""",
           }
           __lowercase							=						self.dummy_input
           return init_dict, inputs_dict

       @unittest.skipIf(torch_device == """mps"""					,		"""mish op not supported in MPS"""     )
       def     snake_case__			(    self     )    ->					Dict:

           """simple docstring"""



           __lowercase				,__lowercase							=						UNetaDModel.from_pretrained(
               """bglick13/hopper-medium-v2-value-function-hor32"""					,		output_loading_info=_a					,		subfolder="""value_function"""     )
           self.assertIsNotNone(_a     )
           self.assertEqual(len(vf_loading_info["""missing_keys"""]     )					,		0     )

           value_function.to(_a     )
           __lowercase							=						value_function(**self.dummy_input     )

           assert image is not None, "Make sure output is not None"

       @unittest.skipIf(torch_device == """mps"""					,		"""mish op not supported in MPS"""     )
       def     snake_case__			(    self     )    ->					List[Any]:

           """simple docstring"""



           __lowercase				,__lowercase							=						UNetaDModel.from_pretrained(
               """bglick13/hopper-medium-v2-value-function-hor32"""					,		output_loading_info=_a					,		subfolder="""value_function"""     )
           torch.manual_seed(0     )
           if torch.cuda.is_available():
               torch.cuda.manual_seed_all(0     )

           __lowercase							=						value_function.config.in_channels
           __lowercase							=						14
           __lowercase							=						torch.randn((1, seq_len, num_features)     ).permute(
               0					,		2					,		1     )  # match original, we can update values and remove
           __lowercase							=						torch.full((num_features,)					,		0     )

           with torch.no_grad():
               __lowercase							=						value_function(_a					,		_a     ).sample

           # fmt: off
           __lowercase							=						torch.tensor([165.25] * seq_len     )
           # fmt: on
           self.assertTrue(torch.allclose(_a					,		_a					,		rtol=1E-3     )     )




       def     snake_case__			(    self     )    ->					int:

           """simple docstring"""



           pass








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


snake_case_  :	Any							  =							logging.get_logger(__name__)

snake_case_  :	Any							  =							{
    '''apple/mobilevit-small''': '''https://huggingface.co/apple/mobilevit-small/resolve/main/config.json''',
    '''apple/mobilevit-x-small''': '''https://huggingface.co/apple/mobilevit-x-small/resolve/main/config.json''',
    '''apple/mobilevit-xx-small''': '''https://huggingface.co/apple/mobilevit-xx-small/resolve/main/config.json''',
    '''apple/deeplabv3-mobilevit-small''': (
        '''https://huggingface.co/apple/deeplabv3-mobilevit-small/resolve/main/config.json'''
    ),
    '''apple/deeplabv3-mobilevit-x-small''': (
        '''https://huggingface.co/apple/deeplabv3-mobilevit-x-small/resolve/main/config.json'''
    ),
    '''apple/deeplabv3-mobilevit-xx-small''': (
        '''https://huggingface.co/apple/deeplabv3-mobilevit-xx-small/resolve/main/config.json'''
    ),
    # See all MobileViT models at https://huggingface.co/models?filter=mobilevit
}

class   A__							(						UpperCamelCase__     ):
						UpperCAmelCase									=  "mobilevit"




						def __init__(				self    :					Tuple ,			_a    :					Any=3 ,			_a    :					Any=256 ,			_a    :					Any=2 ,			_a    :					List[str]=[144, 192, 240] ,			_a    :					Optional[int]=[16, 32, 64, 96, 128, 160, 640] ,			_a    :					Union[str, Any]=4 ,			_a    :					Any=2.0 ,			_a    :					List[str]=4.0 ,			_a    :					Tuple="silu" ,			_a    :					Optional[Any]=3 ,			_a    :					List[str]=32 ,			_a    :					Union[str, Any]=0.1 ,			_a    :					Tuple=0.0 ,			_a    :					Union[str, Any]=0.1 ,			_a    :					Union[str, Any]=0.02 ,			_a    :					Tuple=1E-5 ,			_a    :					Any=True ,			_a    :					str=256 ,			_a    :					Tuple=[6, 12, 18] ,			_a    :					int=0.1 ,			_a    :					str=255 ,			**_a    :					Any ,			)							->   Optional[int]:


										"""simple docstring"""





										super().__init__(**_a     )

										_SCREAMING_SNAKE_CASE	     =num_channels
										_SCREAMING_SNAKE_CASE	     =image_size
										_SCREAMING_SNAKE_CASE	     =patch_size
										_SCREAMING_SNAKE_CASE	     =hidden_sizes
										_SCREAMING_SNAKE_CASE	     =neck_hidden_sizes
										_SCREAMING_SNAKE_CASE	     =num_attention_heads
										_SCREAMING_SNAKE_CASE	     =mlp_ratio
										_SCREAMING_SNAKE_CASE	     =expand_ratio
										_SCREAMING_SNAKE_CASE	     =hidden_act
										_SCREAMING_SNAKE_CASE	     =conv_kernel_size
										_SCREAMING_SNAKE_CASE	     =output_stride
										_SCREAMING_SNAKE_CASE	     =hidden_dropout_prob
										_SCREAMING_SNAKE_CASE	     =attention_probs_dropout_prob
										_SCREAMING_SNAKE_CASE	     =classifier_dropout_prob
										_SCREAMING_SNAKE_CASE	     =initializer_range
										_SCREAMING_SNAKE_CASE	     =layer_norm_eps
										_SCREAMING_SNAKE_CASE	     =qkv_bias

										# decode head attributes for semantic segmentation
										_SCREAMING_SNAKE_CASE	     =aspp_out_channels
										_SCREAMING_SNAKE_CASE	     =atrous_rates
										_SCREAMING_SNAKE_CASE	     =aspp_dropout_prob
										_SCREAMING_SNAKE_CASE	     =semantic_loss_ignore_index

class   A__							(						UpperCamelCase__     ):
						UpperCAmelCase									=  version.parse("1.11"     )




						@property
						def 							__UpperCamelCase (				self    :					Union[str, Any]     )							->   Mapping[str, Mapping[int, str]]:


										"""simple docstring"""





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




						@property
						def 							__UpperCamelCase (				self    :					Dict     )							->   Mapping[str, Mapping[int, str]]:


										"""simple docstring"""





										if self.task == "image-classification":
														return OrderedDict([('''logits''', {0: '''batch'''})]     )
										else:
														return OrderedDict([('''last_hidden_state''', {0: '''batch'''}), ('''pooler_output''', {0: '''batch'''})]     )




						@property
						def 							__UpperCamelCase (				self    :					List[Any]     )							->   float:


										"""simple docstring"""





										return 1E-4