


import inspect
import unittest

from transformers import MobileViTConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch

			from transformers import MobileViTForImageClassification, MobileViTForSemanticSegmentation, MobileViTModel
			from transformers.models.mobilevit.modeling_mobilevit import MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
			from PIL import Image

			from transformers import MobileViTImageProcessor

class 							a						(_SCREAMING_SNAKE_CASE ):




							"""simple docstring"""


							def        __snake_case   (				self			:    Optional[Any]							)      ->       Union[str, Any]:
														__snake_case						:     Optional[int]									=  self.config_class(**self.inputs_dict							)
														self.parent.assertTrue(hasattr(__UpperCAmelCase       ,      "hidden_sizes"							)							)
														self.parent.assertTrue(hasattr(__UpperCAmelCase       ,      "neck_hidden_sizes"							)							)
														self.parent.assertTrue(hasattr(__UpperCAmelCase       ,      "num_attention_heads"							)							)

class 							a						:




							"""simple docstring"""


							def __init__(				self			:    Any       ,      lowerCamelCase			:    List[str]       ,      lowerCamelCase			:    Tuple=13       ,      lowerCamelCase			:    Union[str, Any]=32       ,      lowerCamelCase			:    int=2       ,      lowerCamelCase			:    Union[str, Any]=3       ,      lowerCamelCase			:    List[Any]=640       ,      lowerCamelCase			:    Union[str, Any]=4       ,      lowerCamelCase			:    Union[str, Any]="silu"       ,      lowerCamelCase			:    List[Any]=3       ,      lowerCamelCase			:    Union[str, Any]=32       ,      lowerCamelCase			:    Optional[Any]=0.1       ,      lowerCamelCase			:    List[Any]=0.1       ,      lowerCamelCase			:    str=0.1       ,      lowerCamelCase			:    List[str]=0.02       ,      lowerCamelCase			:    int=True       ,      lowerCamelCase			:    List[str]=True       ,      lowerCamelCase			:    int=10       ,      lowerCamelCase			:    Optional[int]=None       ,      )      ->       Optional[Any]:
														__snake_case						:     Union[str, Any]									=  parent
														__snake_case						:     int									=  batch_size
														__snake_case						:     Tuple									=  image_size
														__snake_case						:     Dict									=  patch_size
														__snake_case						:     Dict									=  num_channels
														__snake_case						:     List[str]									=  last_hidden_size
														__snake_case						:     Any									=  num_attention_heads
														__snake_case						:     Dict									=  hidden_act
														__snake_case						:     str									=  conv_kernel_size
														__snake_case						:     Tuple									=  output_stride
														__snake_case						:     str									=  hidden_dropout_prob
														__snake_case						:     Union[str, Any]									=  attention_probs_dropout_prob
														__snake_case						:     str									=  classifier_dropout_prob
														__snake_case						:     Optional[int]									=  use_labels
														__snake_case						:     str									=  is_training
														__snake_case						:     Union[str, Any]									=  num_labels
														__snake_case						:     List[Any]									=  initializer_range
														__snake_case						:     int									=  scope

							def        __snake_case   (				self			:    Any							)      ->       Optional[Any]:
														__snake_case						:     int									=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

														__snake_case						:     int									=  None
														__snake_case						:     Optional[Any]									=  None
														if self.use_labels:
																					__snake_case						:     Union[str, Any]									=  ids_tensor([self.batch_size]       ,      self.num_labels							)
																					__snake_case						:     Union[str, Any]									=  ids_tensor([self.batch_size, self.image_size, self.image_size]       ,      self.num_labels							)

														__snake_case						:     int									=  self.get_config()

														return config, pixel_values, labels, pixel_labels

							def        __snake_case   (				self			:    str							)      ->       Union[str, Any]:
														return MobileViTConfig(
														    image_size=self.image_size       ,      patch_size=self.patch_size       ,      num_channels=self.num_channels       ,      num_attention_heads=self.num_attention_heads       ,      hidden_act=self.hidden_act       ,      conv_kernel_size=self.conv_kernel_size       ,      output_stride=self.output_stride       ,      hidden_dropout_prob=self.hidden_dropout_prob       ,      attention_probs_dropout_prob=self.attention_probs_dropout_prob       ,      classifier_dropout_prob=self.classifier_dropout_prob       ,      initializer_range=self.initializer_range       ,      )

							def        __snake_case   (				self			:    Optional[int]       ,      lowerCamelCase			:    List[Any]       ,      lowerCamelCase			:    str       ,      lowerCamelCase			:    Dict       ,      lowerCamelCase			:    Tuple							)      ->       Any:
														__snake_case						:     Union[str, Any]									=  MobileViTModel(config=__UpperCAmelCase							)
														model.to(__UpperCAmelCase							)
														model.eval()
														__snake_case						:     Tuple									=  model(__UpperCAmelCase							)
														self.parent.assertEqual(
														    result.last_hidden_state.shape       ,      (
														        self.batch_size,
														        self.last_hidden_size,
														        self.image_size // self.output_stride,
														        self.image_size // self.output_stride,
														    )       ,      )

							def        __snake_case   (				self			:    Union[str, Any]       ,      lowerCamelCase			:    List[Any]       ,      lowerCamelCase			:    Tuple       ,      lowerCamelCase			:    Optional[Any]       ,      lowerCamelCase			:    Optional[int]							)      ->       Tuple:
														__snake_case						:     List[str]									=  self.num_labels
														__snake_case						:     Tuple									=  MobileViTForImageClassification(__UpperCAmelCase							)
														model.to(__UpperCAmelCase							)
														model.eval()
														__snake_case						:     Tuple									=  model(__UpperCAmelCase       ,      labels=__UpperCAmelCase							)
														self.parent.assertEqual(result.logits.shape       ,      (self.batch_size, self.num_labels)							)

							def        __snake_case   (				self			:    int       ,      lowerCamelCase			:    int       ,      lowerCamelCase			:    List[str]       ,      lowerCamelCase			:    Union[str, Any]       ,      lowerCamelCase			:    List[str]							)      ->       List[Any]:
														__snake_case						:     Any									=  self.num_labels
														__snake_case						:     Dict									=  MobileViTForSemanticSegmentation(__UpperCAmelCase							)
														model.to(__UpperCAmelCase							)
														model.eval()
														__snake_case						:     Dict									=  model(__UpperCAmelCase							)
														self.parent.assertEqual(
														    result.logits.shape       ,      (
														        self.batch_size,
														        self.num_labels,
														        self.image_size // self.output_stride,
														        self.image_size // self.output_stride,
														    )       ,      )
														__snake_case						:     Union[str, Any]									=  model(__UpperCAmelCase       ,      labels=__UpperCAmelCase							)
														self.parent.assertEqual(
														    result.logits.shape       ,      (
														        self.batch_size,
														        self.num_labels,
														        self.image_size // self.output_stride,
														        self.image_size // self.output_stride,
														    )       ,      )






							def        __snake_case   (				self			:    str							)      ->       Tuple:
														__snake_case						:     Any									=  self.prepare_config_and_inputs()
														__snake_case						:     Dict									=  config_and_inputs
														__snake_case						:     Any									=  {'''pixel_values''': pixel_values}
														return config, inputs_dict

@require_torch
class 							a						(_SCREAMING_SNAKE_CASE	,							_SCREAMING_SNAKE_CASE	,							unittest.TestCase ):




							"""simple docstring"""


							__UpperCAmelCase       :				Any	    =    (
							    (MobileViTModel, MobileViTForImageClassification, MobileViTForSemanticSegmentation)
							    if is_torch_available()
							    else ()
							)
							__UpperCAmelCase       :				int	    =    (
							    {
							        '''feature-extraction''': MobileViTModel,
							        '''image-classification''': MobileViTForImageClassification,
							        '''image-segmentation''': MobileViTForSemanticSegmentation,
							    }
							    if is_torch_available()
							    else {}
							)

							__UpperCAmelCase       :				Optional[Any]	    =    False
							__UpperCAmelCase       :				Tuple	    =    False
							__UpperCAmelCase       :				Dict	    =    False
							__UpperCAmelCase       :				Any	    =    False

							def        __snake_case   (				self			:    Optional[Any]							)      ->       str:
														__snake_case						:     str									=  MobileViTModelTester(self							)
														__snake_case						:     Dict									=  MobileViTConfigTester(self       ,      config_class=__UpperCAmelCase       ,      has_text_modality=__UpperCAmelCase							)

							def        __snake_case   (				self			:    Any							)      ->       Tuple:
														self.config_tester.run_common_tests()

							@unittest.skip(reason="MobileViT does not use inputs_embeds"							)
							def        __snake_case   (				self			:    Dict							)      ->       Dict:
														pass

							@unittest.skip(reason="MobileViT does not support input and output embeddings"							)
							def        __snake_case   (				self			:    Union[str, Any]							)      ->       Tuple:
														pass

							@unittest.skip(reason="MobileViT does not output attentions"							)
							def        __snake_case   (				self			:    int							)      ->       str:
														pass

							def        __snake_case   (				self			:    int							)      ->       List[str]:
														__snake_case						:     Optional[Any]									=  self.model_tester.prepare_config_and_inputs_for_common()

														for model_class in self.all_model_classes:
																					__snake_case						:     Optional[Any]									=  model_class(__UpperCAmelCase							)
																					__snake_case						:     Optional[Any]									=  inspect.signature(model.forward							)
																					# signature.parameters is an OrderedDict => so arg_names order is deterministic
																					__snake_case						:     Tuple									=  [*signature.parameters.keys()]

																					__snake_case						:     Union[str, Any]									=  ['''pixel_values''']
																					self.assertListEqual(arg_names[:1]       ,      __UpperCAmelCase							)

							@unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."							)
							def        __snake_case   (				self			:    Optional[Any]							)      ->       Any:
														pass

							def        __snake_case   (				self			:    Tuple							)      ->       Any:
														__snake_case						:     Any									=  self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*__UpperCAmelCase							)

							def        __snake_case   (				self			:    Any							)      ->       Union[str, Any]:
														def check_hidden_states_output(lowerCamelCase			:    Optional[int]       ,      lowerCamelCase			:    List[str]       ,      lowerCamelCase			:    Tuple							):
																					__snake_case						:     Dict									=  model_class(__UpperCAmelCase							)
																					model.to(__UpperCAmelCase							)
																					model.eval()

																					with torch.no_grad():
																												__snake_case						:     str									=  model(**self._prepare_for_class(__UpperCAmelCase       ,      __UpperCAmelCase							)							)

																					__snake_case						:     List[Any]									=  outputs.hidden_states

																					__snake_case						:     Tuple									=  5
																					self.assertEqual(len(__UpperCAmelCase							)       ,      __UpperCAmelCase							)

																					# MobileViT's feature maps are of shape (batch_size, num_channels, height, width)
																					# with the width and height being successively divided by 2.
																					__snake_case						:     Dict									=  2
																					for i in range(len(__UpperCAmelCase							)							):
																												self.assertListEqual(
																												    list(hidden_states[i].shape[-2:]							)       ,      [self.model_tester.image_size // divisor, self.model_tester.image_size // divisor]       ,      )
																												divisor *= 2

																					self.assertEqual(self.model_tester.output_stride       ,      divisor // 2							)

														__snake_case						:     Tuple									=  self.model_tester.prepare_config_and_inputs_for_common()

														for model_class in self.all_model_classes:
																					__snake_case						:     Union[str, Any]									=  True
																					check_hidden_states_output(__UpperCAmelCase       ,      __UpperCAmelCase       ,      __UpperCAmelCase							)

																					# check that output_hidden_states also work using config
																					del inputs_dict["output_hidden_states"]
																					__snake_case						:     Union[str, Any]									=  True

																					check_hidden_states_output(__UpperCAmelCase       ,      __UpperCAmelCase       ,      __UpperCAmelCase							)

							def        __snake_case   (				self			:    Dict							)      ->       Optional[int]:
														__snake_case						:     Tuple									=  self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_image_classification(*__UpperCAmelCase							)

							def        __snake_case   (				self			:    Tuple							)      ->       Optional[int]:
														__snake_case						:     Optional[int]									=  self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_semantic_segmentation(*__UpperCAmelCase							)






							@slow
							def        __snake_case   (				self			:    Dict							)      ->       Dict:
														for model_name in MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					__snake_case						:     Optional[Any]									=  MobileViTModel.from_pretrained(__UpperCAmelCase							)
																					self.assertIsNotNone(__UpperCAmelCase							)







def 							lowerCAmelCase_	(   ):
							__snake_case						:     Optional[int]									=  Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"      )
							return image

@require_torch
@require_vision
class 							a						(unittest.TestCase ):




							"""simple docstring"""


							@cached_property
							def        __snake_case   (				self			:    str							)      ->       Optional[int]:
														return MobileViTImageProcessor.from_pretrained("apple/mobilevit-xx-small"							) if is_vision_available() else None

							@slow
							def        __snake_case   (				self			:    List[str]							)      ->       Tuple:
														__snake_case						:     Dict									=  MobileViTForImageClassification.from_pretrained("apple/mobilevit-xx-small"							).to(__UpperCAmelCase							)

														__snake_case						:     Any									=  self.default_image_processor
														__snake_case						:     List[str]									=  prepare_img()
														__snake_case						:     List[str]									=  image_processor(images=__UpperCAmelCase       ,      return_tensors="pt"							).to(__UpperCAmelCase							)

														# forward pass
														with torch.no_grad():
																					__snake_case						:     Union[str, Any]									=  model(**__UpperCAmelCase							)

														# verify the logits
														__snake_case						:     str									=  torch.Size((1, 1000)							)
														self.assertEqual(outputs.logits.shape       ,      __UpperCAmelCase							)

														__snake_case						:     str									=  torch.tensor([-1.93_64, -1.23_27, -0.46_53]							).to(__UpperCAmelCase							)

														self.assertTrue(torch.allclose(outputs.logits[0, :3]       ,      __UpperCAmelCase       ,      atol=1E-4							)							)

							@slow
							def        __snake_case   (				self			:    Union[str, Any]							)      ->       str:
														__snake_case						:     str									=  MobileViTForSemanticSegmentation.from_pretrained("apple/deeplabv3-mobilevit-xx-small"							)
														__snake_case						:     List[Any]									=  model.to(__UpperCAmelCase							)

														__snake_case						:     int									=  MobileViTImageProcessor.from_pretrained("apple/deeplabv3-mobilevit-xx-small"							)

														__snake_case						:     str									=  prepare_img()
														__snake_case						:     List[str]									=  image_processor(images=__UpperCAmelCase       ,      return_tensors="pt"							).to(__UpperCAmelCase							)

														# forward pass
														with torch.no_grad():
																					__snake_case						:     Optional[Any]									=  model(**__UpperCAmelCase							)
														__snake_case						:     Optional[int]									=  outputs.logits

														# verify the logits
														__snake_case						:     Any									=  torch.Size((1, 21, 32, 32)							)
														self.assertEqual(logits.shape       ,      __UpperCAmelCase							)

														__snake_case						:     int									=  torch.tensor(
														    [
														        [[6.97_13, 6.97_86, 7.24_22], [7.28_93, 7.28_25, 7.44_46], [7.65_80, 7.87_97, 7.94_20]],
														        [[-10.6869, -10.3250, -10.3471], [-10.4228, -9.98_68, -9.71_32], [-11.0405, -11.0221, -10.7318]],
														        [[-3.30_89, -2.85_39, -2.67_40], [-3.27_06, -2.56_21, -2.51_08], [-3.25_34, -2.66_15, -2.66_51]],
														    ]       ,      device=__UpperCAmelCase       ,      )

														self.assertTrue(torch.allclose(logits[0, :3, :3, :3]       ,      __UpperCAmelCase       ,      atol=1E-4							)							)






							@slow
							def        __snake_case   (				self			:    str							)      ->       Union[str, Any]:
														__snake_case						:     Tuple									=  MobileViTForSemanticSegmentation.from_pretrained("apple/deeplabv3-mobilevit-xx-small"							)
														__snake_case						:     str									=  model.to(__UpperCAmelCase							)

														__snake_case						:     Union[str, Any]									=  MobileViTImageProcessor.from_pretrained("apple/deeplabv3-mobilevit-xx-small"							)

														__snake_case						:     str									=  prepare_img()
														__snake_case						:     Union[str, Any]									=  image_processor(images=__UpperCAmelCase       ,      return_tensors="pt"							).to(__UpperCAmelCase							)

														# forward pass
														with torch.no_grad():
																					__snake_case						:     Optional[Any]									=  model(**__UpperCAmelCase							)

														__snake_case						:     Optional[int]									=  outputs.logits.detach().cpu()

														__snake_case						:     Tuple									=  image_processor.post_process_semantic_segmentation(outputs=__UpperCAmelCase       ,      target_sizes=[(50, 60)]							)
														__snake_case						:     str									=  torch.Size((50, 60)							)
														self.assertEqual(segmentation[0].shape       ,      __UpperCAmelCase							)

														__snake_case						:     Optional[Any]									=  image_processor.post_process_semantic_segmentation(outputs=__UpperCAmelCase							)
														__snake_case						:     int									=  torch.Size((32, 32)							)
														self.assertEqual(segmentation[0].shape       ,      __UpperCAmelCase							)




import math

import tensorflow as tf
from packaging import version






def        snake_case__		(    lowerCamelCase_		):
							A      :					Optional[int]      		=					tf.convert_to_tensor(lowerCamelCase_		)
							A      :					Union[str, Any]      		=					0.5 * (1.0 + tf.math.erf(x / tf.cast(tf.sqrt(2.0		)     ,				x.dtype		)		))

							return x * cdf






def        snake_case__		(    lowerCamelCase_		):
							A      :					List[str]      		=					tf.convert_to_tensor(lowerCamelCase_		)
							A      :					Any      		=					tf.cast(math.pi     ,				x.dtype		)
							A      :					int      		=					tf.cast(0.044_715     ,				x.dtype		)
							A      :					Tuple      		=					0.5 * (1.0 + tf.tanh(tf.sqrt(2.0 / pi		) * (x + coeff * tf.pow(lowerCamelCase_     ,				3		))		))

							return x * cdf






def        snake_case__		(    lowerCamelCase_		):
							A      :					Union[str, Any]      		=					tf.convert_to_tensor(lowerCamelCase_		)

							return x * tf.tanh(tf.math.softplus(lowerCamelCase_		)		)






def        snake_case__		(    lowerCamelCase_		):
							A      :					Optional[int]      		=					tf.convert_to_tensor(lowerCamelCase_		)
							A      :					List[Any]      		=					tf.cast(0.044_715     ,				x.dtype		)
							A      :					str      		=					tf.cast(0.7_978_845_608     ,				x.dtype		)

							return 0.5 * x * (1.0 + tf.tanh(x * coeffa * (1.0 + coeffa * x * x)		))






def        snake_case__		(    lowerCamelCase_		):
							A      :					List[str]      		=					tf.convert_to_tensor(lowerCamelCase_		)
							A      :					Any      		=					tf.cast(1.702     ,				x.dtype		)
							return x * tf.math.sigmoid(coeff * x		)






def        snake_case__		(    lowerCamelCase_		):
							return tf.clip_by_value(_gelu(lowerCamelCase_		)     ,				-10     ,				10		)






def        snake_case__		(    lowerCamelCase_     ,				lowerCamelCase_=-1		):
							A		,		A      :					Dict      		=					tf.split(lowerCamelCase_     ,				2     ,				axis=lowerCamelCase_		)
							return a * tf.math.sigmoid(lowerCamelCase_		)


if version.parse(tf.version.VERSION) >= version.parse("2.4"):






		def        snake_case__		(    lowerCamelCase_		):
									return tf.keras.activations.gelu(lowerCamelCase_     ,				approximate=lowerCamelCase_		)

		lowercase			:				Tuple   =     tf.keras.activations.gelu

		lowercase			:				Union[str, Any]   =     approximate_gelu_wrap
else:
		lowercase			:				str   =     _gelu
		lowercase			:				Dict   =     _gelu_new


lowercase			:				Union[str, Any]   =     {
    "gelu": gelu,
    "gelu_10": gelu_aa,
    "gelu_fast": gelu_fast,
    "gelu_new": gelu_new,
    "glu": glu,
    "mish": mish,
    "quick_gelu": quick_gelu,
    "relu": tf.keras.activations.relu,
    "sigmoid": tf.keras.activations.sigmoid,
    "silu": tf.keras.activations.swish,
    "swish": tf.keras.activations.swish,
    "tanh": tf.keras.activations.tanh,
}






def        snake_case__		(    lowerCamelCase_		):
							if activation_string in ACTaFN:
														return ACTaFN[activation_string]
							else:
														raise KeyError(F'function {activation_string} not found in ACT2FN mapping {list(ACTaFN.keys()		)}'		)






