






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 		lowercase_					(	lowercase   ):






      '''simple docstring'''




      def       __lowerCAmelCase					(      self      :						Dict  )    ->List[Any]:




       """simple docstring"""


       a		  =      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 		lowercase_					:






      '''simple docstring'''




      def __init__(      self      :						List[str] ,					__UpperCAmelCase      :						List[Any] ,					__UpperCAmelCase      :						Tuple=13 ,					__UpperCAmelCase      :						Optional[Any]=32 ,					__UpperCAmelCase      :						int=2 ,					__UpperCAmelCase      :						Union[str, Any]=3 ,					__UpperCAmelCase      :						Dict=640 ,					__UpperCAmelCase      :						str=4 ,					__UpperCAmelCase      :						int="silu" ,					__UpperCAmelCase      :						List[Any]=3 ,					__UpperCAmelCase      :						Union[str, Any]=32 ,					__UpperCAmelCase      :						Optional[Any]=0.1 ,					__UpperCAmelCase      :						Optional[Any]=0.1 ,					__UpperCAmelCase      :						List[str]=0.1 ,					__UpperCAmelCase      :						List[Any]=0.02 ,					__UpperCAmelCase      :						Tuple=True ,					__UpperCAmelCase      :						str=True ,					__UpperCAmelCase      :						Any=10 ,					__UpperCAmelCase      :						List[str]=None ,					)    ->Any:




       """simple docstring"""


       a		  =      parent
       a		  =      batch_size
       a		  =      image_size
       a		  =      patch_size
       a		  =      num_channels
       a		  =      last_hidden_size
       a		  =      num_attention_heads
       a		  =      hidden_act
       a		  =      conv_kernel_size
       a		  =      output_stride
       a		  =      hidden_dropout_prob
       a		  =      attention_probs_dropout_prob
       a		  =      classifier_dropout_prob
       a		  =      use_labels
       a		  =      is_training
       a		  =      num_labels
       a		  =      initializer_range
       a		  =      scope







      def       __lowerCAmelCase					(      self      :						Union[str, Any]  )    ->Optional[Any]:




       """simple docstring"""


       a		  =      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

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

       a		  =      self.get_config()

       return config, pixel_values, labels, pixel_labels







      def       __lowerCAmelCase					(      self      :						Tuple  )    ->int:




       """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      :						int ,					__UpperCAmelCase      :						Dict ,					__UpperCAmelCase      :						Union[str, Any] ,					__UpperCAmelCase      :						List[str] ,					__UpperCAmelCase      :						Optional[int]  )    ->Any:




       """simple docstring"""


       a		  =      MobileViTModel(config=__UpperCAmelCase  )
       model.to(__UpperCAmelCase  )
       model.eval()
       a		  =      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       __lowerCAmelCase					(      self      :						Any ,					__UpperCAmelCase      :						int ,					__UpperCAmelCase      :						Dict ,					__UpperCAmelCase      :						Tuple ,					__UpperCAmelCase      :						Dict  )    ->Tuple:




       """simple docstring"""


       a		  =      self.num_labels
       a		  =      MobileViTForImageClassification(__UpperCAmelCase  )
       model.to(__UpperCAmelCase  )
       model.eval()
       a		  =      model(__UpperCAmelCase ,					labels=__UpperCAmelCase  )
       self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.num_labels)  )







      def       __lowerCAmelCase					(      self      :						Union[str, Any] ,					__UpperCAmelCase      :						Union[str, Any] ,					__UpperCAmelCase      :						Union[str, Any] ,					__UpperCAmelCase      :						Tuple ,					__UpperCAmelCase      :						Optional[Any]  )    ->Optional[int]:




       """simple docstring"""


       a		  =      self.num_labels
       a		  =      MobileViTForSemanticSegmentation(__UpperCAmelCase  )
       model.to(__UpperCAmelCase  )
       model.eval()
       a		  =      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,
           ) ,					)
       a		  =      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       __lowerCAmelCase					(      self      :						Dict  )    ->Tuple:




       """simple docstring"""


       a		  =      self.prepare_config_and_inputs()
       a  ,     a  ,     a  ,     a		  =      config_and_inputs
       a		  =      {'''pixel_values''': pixel_values}
       return config, inputs_dict






@require_torch
class 		lowercase_					(	lowercase ,  lowercase ,  unittest.TestCase   ):






      '''simple docstring'''




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

      __snake_case       =  False
      __snake_case       =  False
      __snake_case       =  False
      __snake_case       =  False







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




       """simple docstring"""


       a		  =      MobileViTModelTester(self  )
       a		  =      MobileViTConfigTester(self ,					config_class=__UpperCAmelCase ,					has_text_modality=__UpperCAmelCase  )







      def       __lowerCAmelCase					(      self      :						List[Any]  )    ->int:




       """simple docstring"""


       self.config_tester.run_common_tests()







      @unittest.skip(reason='''MobileViT does not use inputs_embeds'''  )
      def       __lowerCAmelCase					(      self      :						int  )    ->Union[str, Any]:




       """simple docstring"""


       pass







      @unittest.skip(reason='''MobileViT does not support input and output embeddings'''  )
      def       __lowerCAmelCase					(      self      :						Dict  )    ->Union[str, Any]:




       """simple docstring"""


       pass







      @unittest.skip(reason='''MobileViT does not output attentions'''  )
      def       __lowerCAmelCase					(      self      :						Dict  )    ->List[str]:




       """simple docstring"""


       pass







      def       __lowerCAmelCase					(      self      :						Dict  )    ->Union[str, Any]:




       """simple docstring"""


       a  ,     a		  =      self.model_tester.prepare_config_and_inputs_for_common()

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

        a		  =      ['''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       __lowerCAmelCase					(      self      :						str  )    ->Optional[int]:




       """simple docstring"""


       pass







      def       __lowerCAmelCase					(      self      :						List[str]  )    ->Dict:




       """simple docstring"""


       a		  =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*__UpperCAmelCase  )







      def       __lowerCAmelCase					(      self      :						Optional[Any]  )    ->List[str]:




       """simple docstring"""


       def check_hidden_states_output(__UpperCAmelCase      :						Union[str, Any] ,					__UpperCAmelCase      :						List[str] ,					__UpperCAmelCase      :						Tuple  ):
        a		  =      model_class(__UpperCAmelCase  )
        model.to(__UpperCAmelCase  )
        model.eval()

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

        a		  =      outputs.hidden_states

        a		  =      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		  =      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		  =      self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
        a		  =      True
        check_hidden_states_output(__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase  )

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

        check_hidden_states_output(__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase  )







      def       __lowerCAmelCase					(      self      :						Union[str, Any]  )    ->Optional[int]:




       """simple docstring"""


       a		  =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*__UpperCAmelCase  )







      def       __lowerCAmelCase					(      self      :						Optional[Any]  )    ->str:




       """simple docstring"""


       a		  =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_semantic_segmentation(*__UpperCAmelCase  )







      @slow
      def       __lowerCAmelCase					(      self      :						Optional[Any]  )    ->List[str]:




       """simple docstring"""


       for model_name in MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        a		  =      MobileViTModel.from_pretrained(__UpperCAmelCase  )
        self.assertIsNotNone(__UpperCAmelCase  )
def 						_a    (      )							->			Union[str, Any]:
 a		  =      Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' )
 return image






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






      '''simple docstring'''




      @cached_property
      def       __lowerCAmelCase					(      self      :						Any  )    ->List[str]:




       """simple docstring"""


       return MobileViTImageProcessor.from_pretrained('''apple/mobilevit-xx-small'''  ) if is_vision_available() else None







      @slow
      def       __lowerCAmelCase					(      self      :						Dict  )    ->Any:




       """simple docstring"""


       a		  =      MobileViTForImageClassification.from_pretrained('''apple/mobilevit-xx-small'''  ).to(__UpperCAmelCase  )

       a		  =      self.default_image_processor
       a		  =      prepare_img()
       a		  =      image_processor(images=__UpperCAmelCase ,					return_tensors='''pt'''  ).to(__UpperCAmelCase  )

       # forward pass
       with torch.no_grad():
        a		  =      model(**__UpperCAmelCase  )

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

       a		  =      torch.tensor([-1.9364, -1.2327, -0.4653]  ).to(__UpperCAmelCase  )

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







      @slow
      def       __lowerCAmelCase					(      self      :						List[Any]  )    ->Union[str, Any]:




       """simple docstring"""


       a		  =      MobileViTForSemanticSegmentation.from_pretrained('''apple/deeplabv3-mobilevit-xx-small'''  )
       a		  =      model.to(__UpperCAmelCase  )

       a		  =      MobileViTImageProcessor.from_pretrained('''apple/deeplabv3-mobilevit-xx-small'''  )

       a		  =      prepare_img()
       a		  =      image_processor(images=__UpperCAmelCase ,					return_tensors='''pt'''  ).to(__UpperCAmelCase  )

       # forward pass
       with torch.no_grad():
        a		  =      model(**__UpperCAmelCase  )
       a		  =      outputs.logits

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

       a		  =      torch.tensor(
           [
               [[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]],
           ] ,					device=__UpperCAmelCase ,					)

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







      @slow
      def       __lowerCAmelCase					(      self      :						Union[str, Any]  )    ->Tuple:




       """simple docstring"""


       a		  =      MobileViTForSemanticSegmentation.from_pretrained('''apple/deeplabv3-mobilevit-xx-small'''  )
       a		  =      model.to(__UpperCAmelCase  )

       a		  =      MobileViTImageProcessor.from_pretrained('''apple/deeplabv3-mobilevit-xx-small'''  )

       a		  =      prepare_img()
       a		  =      image_processor(images=__UpperCAmelCase ,					return_tensors='''pt'''  ).to(__UpperCAmelCase  )

       # forward pass
       with torch.no_grad():
        a		  =      model(**__UpperCAmelCase  )

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

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

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










import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from .tokenization_electra import ElectraTokenizer


UpperCAmelCase__				   =		{"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"}

UpperCAmelCase__				   =		{
    "vocab_file": {
        "google/electra-small-generator": (
            "https://huggingface.co/google/electra-small-generator/resolve/main/vocab.txt"
        ),
        "google/electra-base-generator": "https://huggingface.co/google/electra-base-generator/resolve/main/vocab.txt",
        "google/electra-large-generator": (
            "https://huggingface.co/google/electra-large-generator/resolve/main/vocab.txt"
        ),
        "google/electra-small-discriminator": (
            "https://huggingface.co/google/electra-small-discriminator/resolve/main/vocab.txt"
        ),
        "google/electra-base-discriminator": (
            "https://huggingface.co/google/electra-base-discriminator/resolve/main/vocab.txt"
        ),
        "google/electra-large-discriminator": (
            "https://huggingface.co/google/electra-large-discriminator/resolve/main/vocab.txt"
        ),
    },
    "tokenizer_file": {
        "google/electra-small-generator": (
            "https://huggingface.co/google/electra-small-generator/resolve/main/tokenizer.json"
        ),
        "google/electra-base-generator": (
            "https://huggingface.co/google/electra-base-generator/resolve/main/tokenizer.json"
        ),
        "google/electra-large-generator": (
            "https://huggingface.co/google/electra-large-generator/resolve/main/tokenizer.json"
        ),
        "google/electra-small-discriminator": (
            "https://huggingface.co/google/electra-small-discriminator/resolve/main/tokenizer.json"
        ),
        "google/electra-base-discriminator": (
            "https://huggingface.co/google/electra-base-discriminator/resolve/main/tokenizer.json"
        ),
        "google/electra-large-discriminator": (
            "https://huggingface.co/google/electra-large-discriminator/resolve/main/tokenizer.json"
        ),
    },
}

UpperCAmelCase__				   =		{
    "google/electra-small-generator": 512,
    "google/electra-base-generator": 512,
    "google/electra-large-generator": 512,
    "google/electra-small-discriminator": 512,
    "google/electra-base-discriminator": 512,
    "google/electra-large-discriminator": 512,
}

UpperCAmelCase__				   =		{
    "google/electra-small-generator": {"do_lower_case": True},
    "google/electra-base-generator": {"do_lower_case": True},
    "google/electra-large-generator": {"do_lower_case": True},
    "google/electra-small-discriminator": {"do_lower_case": True},
    "google/electra-base-discriminator": {"do_lower_case": True},
    "google/electra-large-discriminator": {"do_lower_case": True},
}






class 		lowercase_					(	lowercase   ):






      '''simple docstring'''




      __snake_case       =  VOCAB_FILES_NAMES
      __snake_case       =  PRETRAINED_VOCAB_FILES_MAP
      __snake_case       =  PRETRAINED_INIT_CONFIGURATION
      __snake_case       =  PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
      __snake_case       =  ElectraTokenizer







      def __init__(      self      :						Dict ,					__UpperCAmelCase      :						int=None ,					__UpperCAmelCase      :						str=None ,					__UpperCAmelCase      :						Optional[int]=True ,					__UpperCAmelCase      :						str="[UNK]" ,					__UpperCAmelCase      :						Any="[SEP]" ,					__UpperCAmelCase      :						str="[PAD]" ,					__UpperCAmelCase      :						Optional[Any]="[CLS]" ,					__UpperCAmelCase      :						Union[str, Any]="[MASK]" ,					__UpperCAmelCase      :						List[str]=True ,					__UpperCAmelCase      :						Tuple=None ,					**__UpperCAmelCase      :						Optional[int] ,					)    ->str:




       """simple docstring"""


       super().__init__(
           __UpperCAmelCase ,					tokenizer_file=__UpperCAmelCase ,					do_lower_case=__UpperCAmelCase ,					unk_token=__UpperCAmelCase ,					sep_token=__UpperCAmelCase ,					pad_token=__UpperCAmelCase ,					cls_token=__UpperCAmelCase ,					mask_token=__UpperCAmelCase ,					tokenize_chinese_chars=__UpperCAmelCase ,					strip_accents=__UpperCAmelCase ,					**__UpperCAmelCase ,					)

       a		  =      json.loads(self.backend_tokenizer.normalizer.__getstate__()  )
       if (
           normalizer_state.get('''lowercase''' ,					__UpperCAmelCase  ) != do_lower_case
           or normalizer_state.get('''strip_accents''' ,					__UpperCAmelCase  ) != strip_accents
           or normalizer_state.get('''handle_chinese_chars''' ,					__UpperCAmelCase  ) != tokenize_chinese_chars
       ):
        a		  =      getattr(__UpperCAmelCase ,					normalizer_state.pop('''type'''  )  )
        a		  =      do_lower_case
        a		  =      strip_accents
        a		  =      tokenize_chinese_chars
        a		  =      normalizer_class(**__UpperCAmelCase  )

       a		  =      do_lower_case







      def       __lowerCAmelCase					(      self      :						List[Any] ,					__UpperCAmelCase      :						Optional[int] ,					__UpperCAmelCase      :						Tuple=None  )    ->str:




       """simple docstring"""


       a		  =      [self.cls_token_id] + token_ids_a + [self.sep_token_id]

       if token_ids_a:
        output += token_ids_a + [self.sep_token_id]

       return output







      def       __lowerCAmelCase					(      self      :						Optional[Any] ,					__UpperCAmelCase      :						List[int] ,					__UpperCAmelCase      :						Optional[List[int]] = None  )    ->List[int]:




       """simple docstring"""


       a		  =      [self.sep_token_id]
       a		  =      [self.cls_token_id]
       if token_ids_a is None:
        return len(cls + token_ids_a + sep  ) * [0]
       return len(cls + token_ids_a + sep  ) * [0] + len(token_ids_a + sep  ) * [1]







      def       __lowerCAmelCase					(      self      :						List[str] ,					__UpperCAmelCase      :						str ,					__UpperCAmelCase      :						Optional[str] = None  )    ->Tuple[str]:




       """simple docstring"""


       a		  =      self._tokenizer.model.save(__UpperCAmelCase ,					name=__UpperCAmelCase  )
       return tuple(__UpperCAmelCase  )


