





"""simple docstring"""





import inspect
import unittest

from transformers import MobileViTVaConfig
from transformers.testing_utils import require_torch, require_torch_multi_gpu, 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 MobileViTVaForImageClassification, MobileViTVaForSemanticSegmentation, MobileViTVaModel
       from transformers.models.mobilevitva.modeling_mobilevitva import (
           MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST,
           make_divisible,
       )


if is_vision_available():
       from PIL import Image

       from transformers import MobileViTImageProcessor

class 		lowercase					(					_UpperCAmelCase					):





      def     _snake_case			(       self )		->     List[Any]:
            lowerCAmelCase  						=       self.config_class(**self.inputs_dict )
            self.parent.assertTrue(hasattr(lowercase      ,						"""width_multiplier""" ) )







class 		lowercase					:





      def __init__(       self      ,						lowercase      ,						lowercase=13      ,						lowercase=64      ,						lowercase=2      ,						lowercase=3      ,						lowercase="swish"      ,						lowercase=3      ,						lowercase=32      ,						lowercase=0.1      ,						lowercase=0.02      ,						lowercase=True      ,						lowercase=True      ,						lowercase=10      ,						lowercase=None      ,						lowercase=0.25      ,						lowercase=0.0      ,						lowercase=0.0      ,						)		->     List[str]:
            lowerCAmelCase  						=       parent
            lowerCAmelCase  						=       batch_size
            lowerCAmelCase  						=       image_size
            lowerCAmelCase  						=       patch_size
            lowerCAmelCase  						=       num_channels
            lowerCAmelCase  						=       make_divisible(512 * width_multiplier      ,						divisor=8 )
            lowerCAmelCase  						=       hidden_act
            lowerCAmelCase  						=       conv_kernel_size
            lowerCAmelCase  						=       output_stride
            lowerCAmelCase  						=       classifier_dropout_prob
            lowerCAmelCase  						=       use_labels
            lowerCAmelCase  						=       is_training
            lowerCAmelCase  						=       num_labels
            lowerCAmelCase  						=       initializer_range
            lowerCAmelCase  						=       scope
            lowerCAmelCase  						=       width_multiplier
            lowerCAmelCase  						=       ffn_dropout
            lowerCAmelCase  						=       attn_dropout





      def     _snake_case			(       self )		->     Union[str, Any]:
            lowerCAmelCase  						=       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

            lowerCAmelCase  						=       None
            lowerCAmelCase  						=       None
            if self.use_labels:
                  lowerCAmelCase  						=       ids_tensor([self.batch_size]      ,						self.num_labels )
                  lowerCAmelCase  						=       ids_tensor([self.batch_size, self.image_size, self.image_size]      ,						self.num_labels )

            lowerCAmelCase  						=       self.get_config()

            return config, pixel_values, labels, pixel_labels





      def     _snake_case			(       self )		->     List[str]:
            return MobileViTVaConfig(
                image_size=self.image_size      ,						patch_size=self.patch_size      ,						num_channels=self.num_channels      ,						hidden_act=self.hidden_act      ,						conv_kernel_size=self.conv_kernel_size      ,						output_stride=self.output_stride      ,						classifier_dropout_prob=self.classifier_dropout_prob      ,						initializer_range=self.initializer_range      ,						width_multiplier=self.width_multiplier      ,						ffn_dropout=self.ffn_dropout_prob      ,						attn_dropout=self.attn_dropout_prob      ,						)





      def     _snake_case			(       self      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase )		->     List[Any]:
            lowerCAmelCase  						=       MobileViTVaModel(config=lowercase )
            model.to(lowercase )
            model.eval()
            lowerCAmelCase  						=       model(lowercase )
            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      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase )		->     Union[str, Any]:
            lowerCAmelCase  						=       self.num_labels
            lowerCAmelCase  						=       MobileViTVaForImageClassification(lowercase )
            model.to(lowercase )
            model.eval()
            lowerCAmelCase  						=       model(lowercase      ,						labels=lowercase )
            self.parent.assertEqual(result.logits.shape      ,						(self.batch_size, self.num_labels) )





      def     _snake_case			(       self      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase )		->     Optional[int]:
            lowerCAmelCase  						=       self.num_labels
            lowerCAmelCase  						=       MobileViTVaForSemanticSegmentation(lowercase )
            model.to(lowercase )
            model.eval()
            lowerCAmelCase  						=       model(lowercase )
            self.parent.assertEqual(
                result.logits.shape      ,						(
                    self.batch_size,
                    self.num_labels,
                    self.image_size // self.output_stride,
                    self.image_size // self.output_stride,
                )      ,						)
            lowerCAmelCase  						=       model(lowercase      ,						labels=lowercase )
            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:
            lowerCAmelCase  						=       self.prepare_config_and_inputs()
            lowerCAmelCase						,		lowerCAmelCase						,		lowerCAmelCase						,		lowerCAmelCase  						=       config_and_inputs
            lowerCAmelCase  						=       {"""pixel_values""": pixel_values}
            return config, inputs_dict







@require_torch
class 		lowercase					(					_UpperCAmelCase     ,  _UpperCAmelCase     ,  unittest.TestCase					):
      _SCREAMING_SNAKE_CASE					=      (
          (MobileViTVaModel, MobileViTVaForImageClassification, MobileViTVaForSemanticSegmentation)
          if is_torch_available()
          else ()
      )

      _SCREAMING_SNAKE_CASE					=      (
          {
              'feature-extraction': MobileViTVaModel,
              'image-classification': MobileViTVaForImageClassification,
              'image-segmentation': MobileViTVaForSemanticSegmentation,
          }
          if is_torch_available()
          else {}
      )

      _SCREAMING_SNAKE_CASE					=      False
      _SCREAMING_SNAKE_CASE					=      False
      _SCREAMING_SNAKE_CASE					=      False
      _SCREAMING_SNAKE_CASE					=      False





      def     _snake_case			(       self )		->     Dict:
            lowerCAmelCase  						=       MobileViTVaModelTester(self )
            lowerCAmelCase  						=       MobileViTVaConfigTester(self      ,						config_class=lowercase      ,						has_text_modality=lowercase )





      def     _snake_case			(       self )		->     Optional[int]:
            self.config_tester.run_common_tests()





      @unittest.skip(reason="""MobileViTV2 does not use inputs_embeds""" )
      def     _snake_case			(       self )		->     List[str]:
            pass





      @unittest.skip(reason="""MobileViTV2 does not support input and output embeddings""" )
      def     _snake_case			(       self )		->     Optional[Any]:
            pass





      @unittest.skip(reason="""MobileViTV2 does not output attentions""" )
      def     _snake_case			(       self )		->     Any:
            pass





      @require_torch_multi_gpu
      @unittest.skip(reason="""Got `CUDA error: misaligned address` for tests after this one being run.""" )
      def     _snake_case			(       self )		->     str:
            pass





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





      def     _snake_case			(       self )		->     Optional[Any]:
            lowerCAmelCase						,		lowerCAmelCase  						=       self.model_tester.prepare_config_and_inputs_for_common()

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

                  lowerCAmelCase  						=       ["""pixel_values"""]
                  self.assertListEqual(arg_names[:1]      ,						lowercase )





      def     _snake_case			(       self )		->     Optional[int]:
            lowerCAmelCase  						=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*lowercase )





      def     _snake_case			(       self )		->     List[str]:
            def check_hidden_states_output(lowercase      ,						lowercase      ,						lowercase ):
                  lowerCAmelCase  						=       model_class(lowercase )
                  model.to(lowercase )
                  model.eval()

                  with torch.no_grad():
                        lowerCAmelCase  						=       model(**self._prepare_for_class(lowercase      ,						lowercase ) )

                  lowerCAmelCase  						=       outputs.hidden_states

                  lowerCAmelCase  						=       5
                  self.assertEqual(len(lowercase )      ,						lowercase )

                  # MobileViTV2's feature maps are of shape (batch_size, num_channels, height, width)
                  # with the width and height being successively divided by 2.
                  lowerCAmelCase  						=       2
                  for i in range(len(lowercase ) ):
                        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 )

            lowerCAmelCase						,		lowerCAmelCase  						=       self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                  lowerCAmelCase  						=       True
                  check_hidden_states_output(lowercase      ,						lowercase      ,						lowercase )

                  # check that output_hidden_states also work using config
                  del inputs_dict["output_hidden_states"]
                  lowerCAmelCase  						=       True

                  check_hidden_states_output(lowercase      ,						lowercase      ,						lowercase )





      def     _snake_case			(       self )		->     str:
            lowerCAmelCase  						=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_image_classification(*lowercase )





      def     _snake_case			(       self )		->     Dict:
            lowerCAmelCase  						=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_semantic_segmentation(*lowercase )





      @slow
      def     _snake_case			(       self )		->     Union[str, Any]:
            for model_name in MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                  lowerCAmelCase  						=       MobileViTVaModel.from_pretrained(lowercase )
                  self.assertIsNotNone(lowercase )







def 			UpperCAmelCase__							(  ):







      '''simple docstring'''




      lowerCAmelCase  						=       Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""			)
      return image






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





      @cached_property
      def     _snake_case			(       self )		->     List[str]:
            return (
                MobileViTImageProcessor.from_pretrained("""apple/mobilevitv2-1.0-imagenet1k-256""" )
                if is_vision_available()
                else None
            )





      @slow
      def     _snake_case			(       self )		->     List[Any]:
            lowerCAmelCase  						=       MobileViTVaForImageClassification.from_pretrained("""apple/mobilevitv2-1.0-imagenet1k-256""" ).to(
                lowercase )

            lowerCAmelCase  						=       self.default_image_processor
            lowerCAmelCase  						=       prepare_img()
            lowerCAmelCase  						=       image_processor(images=lowercase      ,						return_tensors="""pt""" ).to(lowercase )

            # forward pass
            with torch.no_grad():
                  lowerCAmelCase  						=       model(**lowercase )

            # verify the logits
            lowerCAmelCase  						=       torch.Size((1, 1_000) )
            self.assertEqual(outputs.logits.shape      ,						lowercase )

            lowerCAmelCase  						=       torch.tensor([-1.6336e00, -7.3204e-02, -5.1883e-01] ).to(lowercase )

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





      @slow
      def     _snake_case			(       self )		->     Dict:
            lowerCAmelCase  						=       MobileViTVaForSemanticSegmentation.from_pretrained("""shehan97/mobilevitv2-1.0-voc-deeplabv3""" )
            lowerCAmelCase  						=       model.to(lowercase )

            lowerCAmelCase  						=       MobileViTImageProcessor.from_pretrained("""shehan97/mobilevitv2-1.0-voc-deeplabv3""" )

            lowerCAmelCase  						=       prepare_img()
            lowerCAmelCase  						=       image_processor(images=lowercase      ,						return_tensors="""pt""" ).to(lowercase )

            # forward pass
            with torch.no_grad():
                  lowerCAmelCase  						=       model(**lowercase )
            lowerCAmelCase  						=       outputs.logits

            # verify the logits
            lowerCAmelCase  						=       torch.Size((1, 21, 32, 32) )
            self.assertEqual(logits.shape      ,						lowercase )

            lowerCAmelCase  						=       torch.tensor(
                [
                    [[7.0_863, 7.1_525, 6.8_201], [6.6_931, 6.8_770, 6.8_933], [6.2_978, 7.0_366, 6.9_636]],
                    [[-3.7_134, -3.6_712, -3.6_675], [-3.5_825, -3.3_549, -3.4_777], [-3.3_435, -3.3_979, -3.2_857]],
                    [[-2.9_329, -2.8_003, -2.7_369], [-3.0_564, -2.4_780, -2.0_207], [-2.6_889, -1.9_298, -1.7_640]],
                ]      ,						device=lowercase      ,						)

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





      @slow
      def     _snake_case			(       self )		->     Dict:
            lowerCAmelCase  						=       MobileViTVaForSemanticSegmentation.from_pretrained("""shehan97/mobilevitv2-1.0-voc-deeplabv3""" )
            lowerCAmelCase  						=       model.to(lowercase )

            lowerCAmelCase  						=       MobileViTImageProcessor.from_pretrained("""shehan97/mobilevitv2-1.0-voc-deeplabv3""" )

            lowerCAmelCase  						=       prepare_img()
            lowerCAmelCase  						=       image_processor(images=lowercase      ,						return_tensors="""pt""" ).to(lowercase )

            # forward pass
            with torch.no_grad():
                  lowerCAmelCase  						=       model(**lowercase )

            lowerCAmelCase  						=       outputs.logits.detach().cpu()

            lowerCAmelCase  						=       image_processor.post_process_semantic_segmentation(outputs=lowercase      ,						target_sizes=[(50, 60)] )
            lowerCAmelCase  						=       torch.Size((50, 60) )
            self.assertEqual(segmentation[0].shape      ,						lowercase )

            lowerCAmelCase  						=       image_processor.post_process_semantic_segmentation(outputs=lowercase )
            lowerCAmelCase  						=       torch.Size((32, 32) )
            self.assertEqual(segmentation[0].shape      ,						lowercase )











'''simple docstring'''






from ...configuration_utils import PretrainedConfig
from ...utils import logging


A_							: Any						 =	logging.get_logger(__name__)

A_							: Any						 =	{
    """microsoft/markuplm-base""": """https://huggingface.co/microsoft/markuplm-base/resolve/main/config.json""",
    """microsoft/markuplm-large""": """https://huggingface.co/microsoft/markuplm-large/resolve/main/config.json""",
}



class 							lowercase       (  _lowerCamelCase				):


 """simple docstring"""


 UpperCAmelCase							=  """markuplm"""







 def __init__(							self  ,a_=30_522  ,a_=768  ,a_=12  ,a_=12  ,a_=3_072  ,a_="gelu"  ,a_=0.1  ,a_=0.1  ,a_=512  ,a_=2  ,a_=0.02  ,a_=1E-1_2  ,a_=0  ,a_=0  ,a_=2  ,a_=256  ,a_=1_024  ,a_=216  ,a_=1_001  ,a_=32  ,a_=50  ,a_="absolute"  ,a_=True  ,a_=None  ,**a_  ,)		->   Union[str, Any]:
        super().__init__(
            pad_token_id=a_  ,bos_token_id=a_  ,eos_token_id=a_  ,**a_  ,)
        _UpperCAmelCase  :						Optional[int]			     = vocab_size
        _UpperCAmelCase  :						Tuple			     = hidden_size
        _UpperCAmelCase  :						str			     = num_hidden_layers
        _UpperCAmelCase  :						Dict			     = num_attention_heads
        _UpperCAmelCase  :						int			     = hidden_act
        _UpperCAmelCase  :						Optional[Any]			     = intermediate_size
        _UpperCAmelCase  :						Tuple			     = hidden_dropout_prob
        _UpperCAmelCase  :						List[str]			     = attention_probs_dropout_prob
        _UpperCAmelCase  :						Optional[int]			     = max_position_embeddings
        _UpperCAmelCase  :						Tuple			     = type_vocab_size
        _UpperCAmelCase  :						Dict			     = initializer_range
        _UpperCAmelCase  :						List[Any]			     = layer_norm_eps
        _UpperCAmelCase  :						Optional[Any]			     = position_embedding_type
        _UpperCAmelCase  :						Any			     = use_cache
        _UpperCAmelCase  :						List[Any]			     = classifier_dropout
        # additional properties
        _UpperCAmelCase  :						Dict			     = max_depth
        _UpperCAmelCase  :						Union[str, Any]			     = max_xpath_tag_unit_embeddings
        _UpperCAmelCase  :						Optional[int]			     = max_xpath_subs_unit_embeddings
        _UpperCAmelCase  :						List[Any]			     = tag_pad_id
        _UpperCAmelCase  :						Tuple			     = subs_pad_id
        _UpperCAmelCase  :						List[str]			     = xpath_unit_hidden_size



