code: 
  
  
  from __future__ import annotations
  
  import inspect
  import unittest
  
  from transformers import MobileViTConfig
  from transformers.file_utils import is_tf_available, is_vision_available
  from transformers.testing_utils import require_tf, slow
  
  from ...test_configuration_common import ConfigTester
  from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
  from ...test_pipeline_mixin import PipelineTesterMixin
  
  
  if is_tf_available():
  						import numpy as np
  						import tensorflow as tf
  
  						from transformers import TFMobileViTForImageClassification, TFMobileViTForSemanticSegmentation, TFMobileViTModel
  						from transformers.models.mobilevit.modeling_tf_mobilevit import TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST
  
  
  if is_vision_available():
  						from PIL import Image
  
  						from transformers import MobileViTImageProcessor
  
  
  
  class 			a      (      UpperCamelCase_   ):
  
  
  
  
  
  
  		def       lowerCAmelCase_  (  self							)->			str:
  						'''simple docstring'''
  
  
  						A__     :							List[str]   				=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      :
  
  
  
  
  
  
  		def __init__(  self	,     __UpperCamelCase	,     __UpperCamelCase=13	,     __UpperCamelCase=32	,     __UpperCamelCase=2	,     __UpperCamelCase=3	,     __UpperCamelCase=6_40	,     __UpperCamelCase=4	,     __UpperCamelCase="silu"	,     __UpperCamelCase=3	,     __UpperCamelCase=32	,     __UpperCamelCase=0.1	,     __UpperCamelCase=0.1	,     __UpperCamelCase=0.1	,     __UpperCamelCase=0.02	,     __UpperCamelCase=True	,     __UpperCamelCase=True	,     __UpperCamelCase=10	,     __UpperCamelCase=None	,     )->			Optional[Any]:
  						'''simple docstring'''
  
  
  						A__     :							Tuple   				=parent
  						A__     :							Dict   				=batch_size
  						A__     :							Any   				=image_size
  						A__     :							str   				=patch_size
  						A__     :							Union[str, Any]   				=num_channels
  						A__     :							Optional[int]   				=last_hidden_size
  						A__     :							Optional[Any]   				=num_attention_heads
  						A__     :							Any   				=hidden_act
  						A__     :							List[str]   				=conv_kernel_size
  						A__     :							str   				=output_stride
  						A__     :							List[Any]   				=hidden_dropout_prob
  						A__     :							Any   				=attention_probs_dropout_prob
  						A__     :							List[Any]   				=classifier_dropout_prob
  						A__     :							int   				=use_labels
  						A__     :							Optional[Any]   				=is_training
  						A__     :							Union[str, Any]   				=num_labels
  						A__     :							Tuple   				=initializer_range
  						A__     :							Optional[Any]   				=scope
  
  
  
  
  
  
  		def       lowerCAmelCase_  (  self							)->			Optional[Any]:
  						'''simple docstring'''
  
  
  						A__     :							Dict   				=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)
  
  						A__     :							int   				=None
  						A__     :							Optional[Any]   				=None
  						if self.use_labels:
  										A__     :							int   				=ids_tensor([self.batch_size]	,     self.num_labels							)
  										A__     :							Tuple   				=ids_tensor([self.batch_size, self.image_size, self.image_size]	,     self.num_labels							)
  
  						A__     :							Optional[int]   				=self.get_config()
  
  						return config, pixel_values, labels, pixel_labels
  
  
  
  
  
  
  		def       lowerCAmelCase_  (  self							)->			Dict:
  						'''simple docstring'''
  
  
  						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       lowerCAmelCase_  (  self	,     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase							)->			Union[str, Any]:
  						'''simple docstring'''
  
  
  						A__     :							str   				=TFMobileViTModel(config=__UpperCamelCase							)
  						A__     :							List[str]   				=model(__UpperCamelCase	,     training=__UpperCamelCase							)
  						A__     :							Tuple   				=self.image_size // self.output_stride
  						self.parent.assertEqual(
  						    result.last_hidden_state.shape	,     (self.batch_size, self.last_hidden_size, expected_height, expected_width)							)
  
  
  
  
  
  
  		def       lowerCAmelCase_  (  self	,     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase							)->			Any:
  						'''simple docstring'''
  
  
  						A__     :							List[str]   				=self.num_labels
  						A__     :							List[Any]   				=TFMobileViTForImageClassification(__UpperCamelCase							)
  						A__     :							Optional[Any]   				=model(__UpperCamelCase	,     labels=__UpperCamelCase	,     training=__UpperCamelCase							)
  						self.parent.assertEqual(result.logits.shape	,     (self.batch_size, self.num_labels)							)
  
  
  
  
  
  
  		def       lowerCAmelCase_  (  self	,     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase							)->			Optional[Any]:
  						'''simple docstring'''
  
  
  						A__     :							Tuple   				=self.num_labels
  						A__     :							Tuple   				=TFMobileViTForSemanticSegmentation(__UpperCamelCase							)
  						A__     :							List[Any]   				=self.image_size // self.output_stride
  
  						A__     :							Any   				=model(__UpperCamelCase	,     training=__UpperCamelCase							)
  						self.parent.assertEqual(
  						    result.logits.shape	,     (self.batch_size, self.num_labels, expected_height, expected_width)							)
  
  						A__     :							Dict   				=model(__UpperCamelCase	,     labels=__UpperCamelCase	,     training=__UpperCamelCase							)
  						self.parent.assertEqual(
  						    result.logits.shape	,     (self.batch_size, self.num_labels, expected_height, expected_width)							)
  
  
  
  
  
  
  		def       lowerCAmelCase_  (  self							)->			Dict:
  						'''simple docstring'''
  
  
  						A__     :							str   				=self.prepare_config_and_inputs()
  						A__				,  A__				,  A__				,  A__     :							Dict   				=config_and_inputs
  						A__     :							List[str]   				={'''pixel_values''': pixel_values}
  						return config, inputs_dict
  
  
  
  
  
  
  
  @require_tf
  class 			a      (      UpperCamelCase_      ,UpperCamelCase_      ,unittest.TestCase   ):
  		__lowercase													=		(
  		    (TFMobileViTModel, TFMobileViTForImageClassification, TFMobileViTForSemanticSegmentation)
  		    if is_tf_available()
  		    else ()
  		)
  		__lowercase													=		(
  		    {"""feature-extraction""": TFMobileViTModel, """image-classification""": TFMobileViTForImageClassification}
  		    if is_tf_available()
  		    else {}
  		)
  
  		__lowercase													=		False
  		__lowercase													=		False
  		__lowercase													=		False
  		__lowercase													=		False
  		__lowercase													=		False
  
  
  
  
  
  
  		def       lowerCAmelCase_  (  self							)->			str:
  						'''simple docstring'''
  
  
  						A__     :							Optional[int]   				=TFMobileViTModelTester(self							)
  						A__     :							Union[str, Any]   				=TFMobileViTConfigTester(self	,     config_class=__UpperCamelCase	,     has_text_modality=__UpperCamelCase							)
  
  
  
  
  
  
  		def       lowerCAmelCase_  (  self							)->			List[str]:
  						'''simple docstring'''
  
  
  						self.config_tester.run_common_tests()
  
  
  
  
  
  
  		@unittest.skip(reason='''MobileViT does not use inputs_embeds'''							)
  		def       lowerCAmelCase_  (  self							)->			List[str]:
  						'''simple docstring'''
  
  
  						pass
  
  
  
  
  
  
  		@unittest.skip(reason='''MobileViT does not support input and output embeddings'''							)
  		def       lowerCAmelCase_  (  self							)->			Optional[Any]:
  						'''simple docstring'''
  
  
  						pass
  
  
  
  
  
  
  		@unittest.skip(reason='''MobileViT does not output attentions'''							)
  		def       lowerCAmelCase_  (  self							)->			Tuple:
  						'''simple docstring'''
  
  
  						pass
  
  
  
  
  
  
  		def       lowerCAmelCase_  (  self							)->			Any:
  						'''simple docstring'''
  
  
  						A__				,  A__     :							Tuple   				=self.model_tester.prepare_config_and_inputs_for_common()
  
  						for model_class in self.all_model_classes:
  										A__     :							Union[str, Any]   				=model_class(__UpperCamelCase							)
  										A__     :							List[Any]   				=inspect.signature(model.call							)
  										# signature.parameters is an OrderedDict => so arg_names order is deterministic
  										A__     :							Optional[int]   				=[*signature.parameters.keys()]
  
  										A__     :							Union[str, Any]   				=['''pixel_values''']
  										self.assertListEqual(arg_names[:1]	,     __UpperCamelCase							)
  
  
  
  
  
  
  		def       lowerCAmelCase_  (  self							)->			Union[str, Any]:
  						'''simple docstring'''
  
  
  						A__     :							str   				=self.model_tester.prepare_config_and_inputs()
  						self.model_tester.create_and_check_model(*__UpperCamelCase							)
  
  
  
  
  
  
  		def       lowerCAmelCase_  (  self							)->			Optional[Any]:
  						'''simple docstring'''
  
  
  						def check_hidden_states_output(__UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase							):
  										A__     :							Optional[Any]   				=model_class(__UpperCamelCase							)
  
  										A__     :							Union[str, Any]   				=model(**self._prepare_for_class(__UpperCamelCase	,     __UpperCamelCase							)							)
  
  										A__     :							Tuple   				=outputs.hidden_states
  
  										A__     :							Optional[Any]   				=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.
  										A__     :							Tuple   				=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							)
  
  						A__				,  A__     :							List[Any]   				=self.model_tester.prepare_config_and_inputs_for_common()
  
  						for model_class in self.all_model_classes:
  										A__     :							List[Any]   				=True
  										check_hidden_states_output(__UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase							)
  
  										# check that output_hidden_states also work using config
  										del inputs_dict["output_hidden_states"]
  										A__     :							List[str]   				=True
  
  										check_hidden_states_output(__UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase							)
  
  
  
  
  
  
  		def       lowerCAmelCase_  (  self							)->			Union[str, Any]:
  						'''simple docstring'''
  
  
  						A__     :							Any   				=self.model_tester.prepare_config_and_inputs()
  						self.model_tester.create_and_check_for_image_classification(*__UpperCamelCase							)
  
  
  
  
  
  
  		def       lowerCAmelCase_  (  self							)->			List[Any]:
  						'''simple docstring'''
  
  
  						A__     :							List[Any]   				=self.model_tester.prepare_config_and_inputs()
  						self.model_tester.create_and_check_for_semantic_segmentation(*__UpperCamelCase							)
  
  
  
  
  
  
  		@unittest.skipIf(
  		    not is_tf_available() or len(tf.config.list_physical_devices('''GPU'''							)							) == 0	,     reason='''TF does not support backprop for grouped convolutions on CPU.'''	,     )
  		def       lowerCAmelCase_  (  self							)->			List[str]:
  						'''simple docstring'''
  
  
  						super().test_dataset_conversion()
  
  
  
  
  
  
  		def       lowerCAmelCase_  (  self	,     __UpperCamelCase	,     __UpperCamelCase	,     __UpperCamelCase=2E-1	,     __UpperCamelCase=2E-1							)->			List[str]:
  						'''simple docstring'''
  
  
  						self.assertTrue(np.allclose(__UpperCamelCase	,     __UpperCamelCase	,     atol=__UpperCamelCase	,     rtol=__UpperCamelCase							)							)
  
  
  
  
  
  
  		@unittest.skipIf(
  		    not is_tf_available() or len(tf.config.list_physical_devices('''GPU'''							)							) == 0	,     reason='''TF does not support backprop for grouped convolutions on CPU.'''	,     )
  		@slow
  		def       lowerCAmelCase_  (  self							)->			Tuple:
  						'''simple docstring'''
  
  
  						A__				,  A__     :							str   				=self.model_tester.prepare_config_and_inputs_for_common()
  
  						for model_class in self.all_model_classes:
  										# Since `TFMobileViTModel` cannot operate with the default `fit()` method.
  										if model_class.__name__ != "TFMobileViTModel":
  														A__     :							Optional[Any]   				=model_class(__UpperCamelCase							)
  														if getattr(__UpperCamelCase	,     '''hf_compute_loss'''	,     __UpperCamelCase							):
  																		super().test_keras_fit()
  
  
  
  
  
  
  		def       lowerCAmelCase_  (  self							)->			Optional[int]:
  						'''simple docstring'''
  
  
  						A__				,  A__     :							str   				=self.model_tester.prepare_config_and_inputs_for_common()
  						for model_class in self.all_model_classes:
  										# set an ignore index to correctly test the masked loss used in
  										# `TFMobileViTForSemanticSegmentation`.
  										if model_class.__name__ != "TFMobileViTForSemanticSegmentation":
  														A__     :							List[str]   				=5
  
  										A__     :							Tuple   				=model_class(__UpperCamelCase							)
  										if getattr(__UpperCamelCase	,     '''hf_compute_loss'''	,     __UpperCamelCase							):
  														# The number of elements in the loss should be the same as the number of elements in the label
  														A__     :							Any   				=self._prepare_for_class(inputs_dict.copy()	,     __UpperCamelCase	,     return_labels=__UpperCamelCase							)
  														A__     :							Optional[Any]   				=prepared_for_class[
  														    sorted(prepared_for_class.keys() - inputs_dict.keys()	,     reverse=__UpperCamelCase							)[0]
  														]
  														A__     :							Union[str, Any]   				=added_label.shape.as_list()[:1]
  
  														# Test that model correctly compute the loss with kwargs
  														A__     :							Dict   				=self._prepare_for_class(inputs_dict.copy()	,     __UpperCamelCase	,     return_labels=__UpperCamelCase							)
  														A__     :							str   				={'''input_ids''', '''pixel_values''', '''input_features'''}
  														A__     :							str   				=possible_input_names.intersection(set(__UpperCamelCase							)							).pop()
  														A__     :							Union[str, Any]   				=prepared_for_class.pop(__UpperCamelCase							)
  
  														A__     :							Any   				=model(__UpperCamelCase	,     **__UpperCamelCase							)[0]
  														self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]							)
  
  														# Test that model correctly compute the loss when we mask some positions
  														A__     :							Tuple   				=self._prepare_for_class(inputs_dict.copy()	,     __UpperCamelCase	,     return_labels=__UpperCamelCase							)
  														A__     :							int   				={'''input_ids''', '''pixel_values''', '''input_features'''}
  														A__     :							Tuple   				=possible_input_names.intersection(set(__UpperCamelCase							)							).pop()
  														A__     :							Optional[int]   				=prepared_for_class.pop(__UpperCamelCase							)
  														if "labels" in prepared_for_class:
  																		A__     :							Dict   				=prepared_for_class['''labels'''].numpy()
  																		if len(labels.shape							) > 1 and labels.shape[1] != 1:
  																						# labels[0] = -100
  																						A__     :							Any   				=tf.convert_to_tensor(__UpperCamelCase							)
  																						A__     :							Optional[Any]   				=model(__UpperCamelCase	,     **__UpperCamelCase							)[0]
  																						self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]							)
  																						self.assertTrue(not np.any(np.isnan(loss.numpy()							)							)							)
  
                  # Test that model correctly compute the loss with a dict
  														A__     :							List[Any]   				=self._prepare_for_class(inputs_dict.copy()	,     __UpperCamelCase	,     return_labels=__UpperCamelCase							)
  														A__     :							List[str]   				=model(__UpperCamelCase							)[0]
  														self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]							)
  
  														# Test that model correctly compute the loss with a tuple
  														A__     :							str   				=self._prepare_for_class(inputs_dict.copy()	,     __UpperCamelCase	,     return_labels=__UpperCamelCase							)
  
  														# Get keys that were added with the _prepare_for_class function
  														A__     :							Optional[Any]   				=prepared_for_class.keys() - inputs_dict.keys()
  														A__     :							Union[str, Any]   				=inspect.signature(model.call							).parameters
  														A__     :							Dict   				=list(signature.keys()							)
  
  														# Create a dictionary holding the location of the tensors in the tuple
  														A__     :							Optional[int]   				={0: input_name}
  														for label_key in label_keys:
  																		A__     :							Optional[int]   				=signature_names.index(__UpperCamelCase							)
  																		A__     :							Optional[Any]   				=label_key
  														A__     :							int   				=sorted(tuple_index_mapping.items()							)
  														# Initialize a list with their default values, update the values and convert to a tuple
  														A__     :							Any   				=[]
  
  														for name in signature_names:
  																		if name != "kwargs":
  																						list_input.append(signature[name].default							)
  
  														for index, value in sorted_tuple_index_mapping:
  																		A__     :							Tuple   				=prepared_for_class[value]
  
  														A__     :							List[str]   				=tuple(__UpperCamelCase							)
  
  														# Send to model
  														A__     :							Optional[int]   				=model(tuple_input[:-1]							)[0]
  
  														self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]							)
  
  
  
  
  
  
  		@slow
  		def       lowerCAmelCase_  (  self							)->			Any:
  						'''simple docstring'''
  
  
  						for model_name in TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
  										A__     :							List[str]   				=TFMobileViTModel.from_pretrained(__UpperCamelCase							)
  										self.assertIsNotNone(__UpperCamelCase							)
  
  
  
  
  
  
  
  def   SCREAMING_SNAKE_CASE__						(							)			->							Optional[int]:
  				A__     :							Dict   				=Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''	)
  				return image
  
  
  
  
  @require_tf
  class 			a      (      unittest.TestCase   ):
  
  
  
  
  
  
  		@slow
  		def       lowerCAmelCase_  (  self							)->			List[Any]:
  						'''simple docstring'''
  
  
  						A__     :							Union[str, Any]   				=TFMobileViTForImageClassification.from_pretrained('''apple/mobilevit-xx-small'''							)
  
  						A__     :							Dict   				=MobileViTImageProcessor.from_pretrained('''apple/mobilevit-xx-small'''							)
  						A__     :							List[Any]   				=prepare_img()
  						A__     :							List[str]   				=image_processor(images=__UpperCamelCase	,     return_tensors='''tf'''							)
  
  						# forward pass
  						A__     :							Any   				=model(**__UpperCamelCase	,     training=__UpperCamelCase							)
  
  						# verify the logits
  						A__     :							Dict   				=tf.TensorShape((1, 10_00)							)
  						self.assertEqual(outputs.logits.shape	,     __UpperCamelCase							)
  
  						A__     :							List[str]   				=tf.constant([-1.9364, -1.2327, -0.4653]							)
  
  						tf.debugging.assert_near(outputs.logits[0, :3]	,     __UpperCamelCase	,     atol=1E-4	,     rtol=1E-04							)
  
  
  
  
  
  
  		@slow
  		def       lowerCAmelCase_  (  self							)->			int:
  						'''simple docstring'''
  
  
  						A__     :							Tuple   				=TFMobileViTForSemanticSegmentation.from_pretrained('''apple/deeplabv3-mobilevit-xx-small'''							)
  
  						A__     :							Union[str, Any]   				=MobileViTImageProcessor.from_pretrained('''apple/deeplabv3-mobilevit-xx-small'''							)
  
  						A__     :							List[str]   				=prepare_img()
  						A__     :							str   				=image_processor(images=__UpperCamelCase	,     return_tensors='''tf'''							)
  
  						# forward pass
  						A__     :							Any   				=model(inputs.pixel_values	,     training=__UpperCamelCase							)
  						A__     :							Optional[int]   				=outputs.logits
  
  						# verify the logits
  						A__     :							List[Any]   				=tf.TensorShape((1, 21, 32, 32)							)
  						self.assertEqual(logits.shape	,     __UpperCamelCase							)
  
  						A__     :							Tuple   				=tf.constant(
  						    [
  						        [[6.9713, 6.9786, 7.2422], [7.2893, 7.2825, 7.4446], [7.6580, 7.8797, 7.9420]],
  						        [[-10.6869, -10.3250, -10.3471], [-10.4228, -9.9868, -9.7132], [-11.0405, -11.0221, -10.7318]],
  						        [[-3.3089, -2.8539, -2.6740], [-3.2706, -2.5621, -2.5108], [-3.2534, -2.6615, -2.6651]],
  						    ]							)
  
  						tf.debugging.assert_near(logits[0, :3, :3, :3]	,     __UpperCamelCase	,     rtol=1E-4	,     atol=1E-4							)
  
  
  
  
style_context: 
  
  
  def   SCREAMING_SNAKE_CASE__						(							snake_case_	)			->							int:
  				if number != int(snake_case_	):
  								raise ValueError('''factorial() only accepts integral values'''	)
  				if number < 0:
  								raise ValueError('''factorial() not defined for negative values'''	)
  				A__     :							Union[str, Any]   				=1
  				for i in range(1,	number + 1	):
  								value *= i
  				return value
  
  
  
  def   SCREAMING_SNAKE_CASE__						(							snake_case_	)			->							int:
  				if not isinstance(snake_case_,	snake_case_	):
  								raise ValueError('''factorial() only accepts integral values'''	)
  				if n < 0:
  								raise ValueError('''factorial() not defined for negative values'''	)
  				return 1 if n == 0 or n == 1 else n * factorial(n - 1	)
  
  
  if __name__ == "__main__":
  						import doctest
  
  						doctest.testmod()
  
  						__lowerCamelCase       :       Any				   =			int(input("Enter a positive integer: ").strip() or 0)
  						print(F"factorial{n} is {factorial(n)}")
  
  
  
  