code: 
  '''simple docstring'''
  
  
  
  
  
  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  lowerCAmelCase__   (       UpperCamelCase			):
  
  
  
    '''simple docstring'''
  
  
  
  
  
  
    def 		_lowerCamelCase	(self  : Dict    )   ->       Union[str, Any]:
          snake_case_						       =							self.config_class(**self.inputs_dict    )
          self.parent.assertTrue(hasattr(lowerCAmelCase_	, "hidden_sizes"    )    )
          self.parent.assertTrue(hasattr(lowerCAmelCase_	, "neck_hidden_sizes"    )    )
          self.parent.assertTrue(hasattr(lowerCAmelCase_	, "num_attention_heads"    )    )
  
  
  
  class  lowerCAmelCase__   :
  
  
  
    '''simple docstring'''
  
  
  
  
  
  
    def __init__(self  : Dict	, lowerCAmelCase_  : List[str]	, lowerCAmelCase_  : Optional[Any]=13	, lowerCAmelCase_  : str=32	, lowerCAmelCase_  : Dict=2	, lowerCAmelCase_  : Optional[Any]=3	, lowerCAmelCase_  : List[str]=6_40	, lowerCAmelCase_  : Dict=4	, lowerCAmelCase_  : Union[str, Any]="silu"	, lowerCAmelCase_  : Dict=3	, lowerCAmelCase_  : Union[str, Any]=32	, lowerCAmelCase_  : Tuple=0.1	, lowerCAmelCase_  : Any=0.1	, lowerCAmelCase_  : Optional[Any]=0.1	, lowerCAmelCase_  : Optional[Any]=0.02	, lowerCAmelCase_  : Optional[Any]=True	, lowerCAmelCase_  : Union[str, Any]=True	, lowerCAmelCase_  : str=10	, lowerCAmelCase_  : Any=None	, )   ->       Union[str, Any]:
          snake_case_						       =							parent
          snake_case_						       =							batch_size
          snake_case_						       =							image_size
          snake_case_						       =							patch_size
          snake_case_						       =							num_channels
          snake_case_						       =							last_hidden_size
          snake_case_						       =							num_attention_heads
          snake_case_						       =							hidden_act
          snake_case_						       =							conv_kernel_size
          snake_case_						       =							output_stride
          snake_case_						       =							hidden_dropout_prob
          snake_case_						       =							attention_probs_dropout_prob
          snake_case_						       =							classifier_dropout_prob
          snake_case_						       =							use_labels
          snake_case_						       =							is_training
          snake_case_						       =							num_labels
          snake_case_						       =							initializer_range
          snake_case_						       =							scope
  
    def 		_lowerCamelCase	(self  : Any    )   ->       int:
          snake_case_						       =							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )
  
          snake_case_						       =							None
          snake_case_						       =							None
          if self.use_labels:
                snake_case_						       =							ids_tensor([self.batch_size]	, self.num_labels    )
                snake_case_						       =							ids_tensor([self.batch_size, self.image_size, self.image_size]	, self.num_labels    )
  
          snake_case_						       =							self.get_config()
  
          return config, pixel_values, labels, pixel_labels
  
    def 		_lowerCamelCase	(self  : Dict    )   ->       str:
          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  : Union[str, Any]	, lowerCAmelCase_  : List[str]	, lowerCAmelCase_  : Optional[int]	, lowerCAmelCase_  : Optional[Any]	, lowerCAmelCase_  : Tuple    )   ->       Any:
          snake_case_						       =							TFMobileViTModel(config=lowerCAmelCase_    )
          snake_case_						       =							model(lowerCAmelCase_	, training=lowerCAmelCase_    )
          snake_case_						       =							snake_case_						       =							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  : Union[str, Any]	, lowerCAmelCase_  : Union[str, Any]	, lowerCAmelCase_  : Any	, lowerCAmelCase_  : Any	, lowerCAmelCase_  : List[Any]    )   ->       str:
          snake_case_						       =							self.num_labels
          snake_case_						       =							TFMobileViTForImageClassification(lowerCAmelCase_    )
          snake_case_						       =							model(lowerCAmelCase_	, labels=lowerCAmelCase_	, training=lowerCAmelCase_    )
          self.parent.assertEqual(result.logits.shape	, (self.batch_size, self.num_labels)    )
  
    def 		_lowerCamelCase	(self  : str	, lowerCAmelCase_  : str	, lowerCAmelCase_  : List[str]	, lowerCAmelCase_  : str	, lowerCAmelCase_  : List[str]    )   ->       List[Any]:
          snake_case_						       =							self.num_labels
          snake_case_						       =							TFMobileViTForSemanticSegmentation(lowerCAmelCase_    )
          snake_case_						       =							snake_case_						       =							self.image_size // self.output_stride
  
          snake_case_						       =							model(lowerCAmelCase_	, training=lowerCAmelCase_    )
          self.parent.assertEqual(
              result.logits.shape	, (self.batch_size, self.num_labels, expected_height, expected_width)    )
  
          snake_case_						       =							model(lowerCAmelCase_	, labels=lowerCAmelCase_	, training=lowerCAmelCase_    )
          self.parent.assertEqual(
              result.logits.shape	, (self.batch_size, self.num_labels, expected_height, expected_width)    )
  
  
    def 		_lowerCamelCase	(self  : str    )   ->       Union[str, Any]:
          snake_case_						       =							self.prepare_config_and_inputs()
          snake_case_       ,	snake_case_       ,	snake_case_       ,	snake_case_						       =							config_and_inputs
          snake_case_						       =							{"pixel_values": pixel_values}
          return config, inputs_dict
  
  
  
  @require_tf
  class  lowerCAmelCase__   (       UpperCamelCase					,							UpperCamelCase					,							unittest.TestCase			):
  
  
  
    '''simple docstring'''
  
  
  
  
  
  
    _UpperCamelCase						= (
        (TFMobileViTModel, TFMobileViTForImageClassification, TFMobileViTForSemanticSegmentation)
        if is_tf_available()
        else ()
    )
    _UpperCamelCase						= (
        {'feature-extraction': TFMobileViTModel, 'image-classification': TFMobileViTForImageClassification}
        if is_tf_available()
        else {}
    )
  
    _UpperCamelCase						= False
    _UpperCamelCase						= False
    _UpperCamelCase						= False
    _UpperCamelCase						= False
    _UpperCamelCase						= False
  
    def 		_lowerCamelCase	(self  : List[Any]    )   ->       Optional[int]:
          snake_case_						       =							TFMobileViTModelTester(self    )
          snake_case_						       =							TFMobileViTConfigTester(self	, config_class=lowerCAmelCase_	, has_text_modality=lowerCAmelCase_    )
  
    def 		_lowerCamelCase	(self  : str    )   ->       Tuple:
          self.config_tester.run_common_tests()
  
    @unittest.skip(reason="MobileViT does not use inputs_embeds"    )
    def 		_lowerCamelCase	(self  : int    )   ->       List[Any]:
          pass
  
    @unittest.skip(reason="MobileViT does not support input and output embeddings"    )
    def 		_lowerCamelCase	(self  : List[Any]    )   ->       int:
          pass
  
    @unittest.skip(reason="MobileViT does not output attentions"    )
    def 		_lowerCamelCase	(self  : Tuple    )   ->       Tuple:
          pass
  
    def 		_lowerCamelCase	(self  : Any    )   ->       Union[str, Any]:
          snake_case_       ,	snake_case_						       =							self.model_tester.prepare_config_and_inputs_for_common()
  
          for model_class in self.all_model_classes:
                snake_case_						       =							model_class(lowerCAmelCase_    )
                snake_case_						       =							inspect.signature(model.call    )
                # signature.parameters is an OrderedDict => so arg_names order is deterministic
                snake_case_						       =							[*signature.parameters.keys()]
  
                snake_case_						       =							["pixel_values"]
                self.assertListEqual(arg_names[:1]	, lowerCAmelCase_    )
  
    def 		_lowerCamelCase	(self  : Any    )   ->       Dict:
          snake_case_						       =							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*lowerCAmelCase_    )
  
    def 		_lowerCamelCase	(self  : List[str]    )   ->       int:
          def check_hidden_states_output(lowerCAmelCase_  : Optional[int]	, lowerCAmelCase_  : Tuple	, lowerCAmelCase_  : Tuple    ):
                snake_case_						       =							model_class(lowerCAmelCase_    )
  
                snake_case_						       =							model(**self._prepare_for_class(lowerCAmelCase_	, lowerCAmelCase_    )    )
  
                snake_case_						       =							outputs.hidden_states
  
                snake_case_						       =							5
                self.assertEqual(len(lowerCAmelCase_    )	, lowerCAmelCase_    )
  
                # 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_						       =							2
                for i in range(len(lowerCAmelCase_    )    ):
                      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_       ,	snake_case_						       =							self.model_tester.prepare_config_and_inputs_for_common()
  
          for model_class in self.all_model_classes:
                snake_case_						       =							True
                check_hidden_states_output(lowerCAmelCase_	, lowerCAmelCase_	, lowerCAmelCase_    )
  
                # check that output_hidden_states also work using config
                del inputs_dict["output_hidden_states"]
                snake_case_						       =							True
  
                check_hidden_states_output(lowerCAmelCase_	, lowerCAmelCase_	, lowerCAmelCase_    )
  
    def 		_lowerCamelCase	(self  : str    )   ->       Any:
          snake_case_						       =							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase_    )
  
    def 		_lowerCamelCase	(self  : Union[str, Any]    )   ->       Optional[int]:
          snake_case_						       =							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_semantic_segmentation(*lowerCAmelCase_    )
  
    @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]    )   ->       Dict:
          super().test_dataset_conversion()
  
    def 		_lowerCamelCase	(self  : List[str]	, lowerCAmelCase_  : Optional[Any]	, lowerCAmelCase_  : Dict	, lowerCAmelCase_  : List[str]=2E-1	, lowerCAmelCase_  : Any=2E-1    )   ->       Optional[Any]:
          self.assertTrue(np.allclose(lowerCAmelCase_	, lowerCAmelCase_	, atol=lowerCAmelCase_	, rtol=lowerCAmelCase_    )    )
  
    @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    )   ->       Union[str, Any]:
          snake_case_       ,	snake_case_						       =							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":
                      snake_case_						       =							model_class(lowerCAmelCase_    )
                      if getattr(lowerCAmelCase_	, "hf_compute_loss"	, lowerCAmelCase_    ):
                            super().test_keras_fit()
  
    def 		_lowerCamelCase	(self  : List[Any]    )   ->       List[str]:
          snake_case_       ,	snake_case_						       =							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":
                      snake_case_						       =							5
  
                snake_case_						       =							model_class(lowerCAmelCase_    )
                if getattr(lowerCAmelCase_	, "hf_compute_loss"	, lowerCAmelCase_    ):
                      # The number of elements in the loss should be the same as the number of elements in the label
                      snake_case_						       =							self._prepare_for_class(inputs_dict.copy()	, lowerCAmelCase_	, return_labels=lowerCAmelCase_    )
                      snake_case_						       =							prepared_for_class[
                          sorted(prepared_for_class.keys() - inputs_dict.keys()	, reverse=lowerCAmelCase_    )[0]
                      ]
                      snake_case_						       =							added_label.shape.as_list()[:1]
  
                      # Test that model correctly compute the loss with kwargs
                      snake_case_						       =							self._prepare_for_class(inputs_dict.copy()	, lowerCAmelCase_	, return_labels=lowerCAmelCase_    )
                      snake_case_						       =							{"input_ids", "pixel_values", "input_features"}
                      snake_case_						       =							possible_input_names.intersection(set(lowerCAmelCase_    )    ).pop()
                      snake_case_						       =							prepared_for_class.pop(lowerCAmelCase_    )
  
                      snake_case_						       =							model(lowerCAmelCase_	, **lowerCAmelCase_    )[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
                      snake_case_						       =							self._prepare_for_class(inputs_dict.copy()	, lowerCAmelCase_	, return_labels=lowerCAmelCase_    )
                      snake_case_						       =							{"input_ids", "pixel_values", "input_features"}
                      snake_case_						       =							possible_input_names.intersection(set(lowerCAmelCase_    )    ).pop()
                      snake_case_						       =							prepared_for_class.pop(lowerCAmelCase_    )
                      if "labels" in prepared_for_class:
                            snake_case_						       =							prepared_for_class["labels"].numpy()
                            if len(labels.shape    ) > 1 and labels.shape[1] != 1:
                                  # labels[0] = -100
                                  snake_case_						       =							tf.convert_to_tensor(lowerCAmelCase_    )
                                  snake_case_						       =							model(lowerCAmelCase_	, **lowerCAmelCase_    )[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
                      snake_case_						       =							self._prepare_for_class(inputs_dict.copy()	, lowerCAmelCase_	, return_labels=lowerCAmelCase_    )
                      snake_case_						       =							model(lowerCAmelCase_    )[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
                      snake_case_						       =							self._prepare_for_class(inputs_dict.copy()	, lowerCAmelCase_	, return_labels=lowerCAmelCase_    )
  
                      # Get keys that were added with the _prepare_for_class function
                      snake_case_						       =							prepared_for_class.keys() - inputs_dict.keys()
                      snake_case_						       =							inspect.signature(model.call    ).parameters
                      snake_case_						       =							list(signature.keys()    )
  
                      # Create a dictionary holding the location of the tensors in the tuple
                      snake_case_						       =							{0: input_name}
                      for label_key in label_keys:
                            snake_case_						       =							signature_names.index(lowerCAmelCase_    )
                            snake_case_						       =							label_key
                      snake_case_						       =							sorted(tuple_index_mapping.items()    )
                      # Initialize a list with their default values, update the values and convert to a tuple
                      snake_case_						       =							[]
  
                      for name in signature_names:
                            if name != "kwargs":
                                  list_input.append(signature[name].default    )
  
                      for index, value in sorted_tuple_index_mapping:
                            snake_case_						       =							prepared_for_class[value]
  
                      snake_case_						       =							tuple(lowerCAmelCase_    )
  
                      # Send to model
                      snake_case_						       =							model(tuple_input[:-1]    )[0]
  
                      self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]    )
  
  
    @slow
    def 		_lowerCamelCase	(self  : List[str]    )   ->       List[Any]:
          for model_name in TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                snake_case_						       =							TFMobileViTModel.from_pretrained(lowerCAmelCase_    )
                self.assertIsNotNone(lowerCAmelCase_    )
  
  def   snake_case_       (							):
        snake_case_						       =							Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"			)
        return image
  
  
  
  
  @require_tf
  class  lowerCAmelCase__   (       unittest.TestCase			):
  
  
  
    '''simple docstring'''
  
  
  
  
  
  
    @slow
    def 		_lowerCamelCase	(self  : str    )   ->       Tuple:
          snake_case_						       =							TFMobileViTForImageClassification.from_pretrained("apple/mobilevit-xx-small"    )
  
          snake_case_						       =							MobileViTImageProcessor.from_pretrained("apple/mobilevit-xx-small"    )
          snake_case_						       =							prepare_img()
          snake_case_						       =							image_processor(images=lowerCAmelCase_	, return_tensors="tf"    )
  
          # forward pass
          snake_case_						       =							model(**lowerCAmelCase_	, training=lowerCAmelCase_    )
  
          # verify the logits
          snake_case_						       =							tf.TensorShape((1, 10_00)    )
          self.assertEqual(outputs.logits.shape	, lowerCAmelCase_    )
  
          snake_case_						       =							tf.constant([-1.9_364, -1.2_327, -0.4_653]    )
  
          tf.debugging.assert_near(outputs.logits[0, :3]	, lowerCAmelCase_	, atol=1E-4	, rtol=1E-04    )
  
  
    @slow
    def 		_lowerCamelCase	(self  : int    )   ->       Optional[Any]:
          # `from_pt` will be removed
          snake_case_						       =							TFMobileViTForSemanticSegmentation.from_pretrained("apple/deeplabv3-mobilevit-xx-small"    )
  
          snake_case_						       =							MobileViTImageProcessor.from_pretrained("apple/deeplabv3-mobilevit-xx-small"    )
  
          snake_case_						       =							prepare_img()
          snake_case_						       =							image_processor(images=lowerCAmelCase_	, return_tensors="tf"    )
  
          # forward pass
          snake_case_						       =							model(inputs.pixel_values	, training=lowerCAmelCase_    )
          snake_case_						       =							outputs.logits
  
          # verify the logits
          snake_case_						       =							tf.TensorShape((1, 21, 32, 32)    )
          self.assertEqual(logits.shape	, lowerCAmelCase_    )
  
          snake_case_						       =							tf.constant(
              [
                  [[6.9_713, 6.9_786, 7.2_422], [7.2_893, 7.2_825, 7.4_446], [7.6_580, 7.8_797, 7.9_420]],
                  [[-10.6_869, -10.3_250, -10.3_471], [-10.4_228, -9.9_868, -9.7_132], [-11.0_405, -11.0_221, -10.7_318]],
                  [[-3.3_089, -2.8_539, -2.6_740], [-3.2_706, -2.5_621, -2.5_108], [-3.2_534, -2.6_615, -2.6_651]],
              ]    )
  
          tf.debugging.assert_near(logits[0, :3, :3, :3]	, lowerCAmelCase_	, rtol=1E-4	, atol=1E-4    )
  
  
style_context: 
  '''simple docstring'''
  
  
  
  
  
  from typing import TYPE_CHECKING
  
  from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available
  
  
  A           =   {
      "configuration_deta": ["DETA_PRETRAINED_CONFIG_ARCHIVE_MAP", "DetaConfig"],
  }
  
  try:
       if not is_vision_available():
            raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
       pass
  else:
       A           =   ["DetaImageProcessor"]
  
  try:
       if not is_torch_available():
            raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
       pass
  else:
       A           =   [
           "DETA_PRETRAINED_MODEL_ARCHIVE_LIST",
           "DetaForObjectDetection",
           "DetaModel",
           "DetaPreTrainedModel",
       ]
  
  
  if TYPE_CHECKING:
       from .configuration_deta import DETA_PRETRAINED_CONFIG_ARCHIVE_MAP, DetaConfig
  
       try:
            if not is_vision_available():
                 raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
            pass
       else:
            from .image_processing_deta import DetaImageProcessor
  
       try:
            if not is_torch_available():
                 raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
            pass
       else:
            from .modeling_deta import (
                DETA_PRETRAINED_MODEL_ARCHIVE_LIST,
                DetaForObjectDetection,
                DetaModel,
                DetaPreTrainedModel,
            )
  
  else:
       import sys
  
       A           =   _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
  
  