


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 		__magic_name__						(snake_case_				):





     '''simple docstring'''



     def 	SCREAMING_SNAKE_CASE__						(					self:Any   ):
       snake_case__						= self.config_class(**self.inputs_dict   )
       self.parent.assertTrue(hasattr(_a     ,		'''width_multiplier'''   )   )




class 		__magic_name__						:





     '''simple docstring'''



     def __init__(					self:Tuple     ,		_a:Optional[Any]     ,		_a:Optional[int]=13     ,		_a:Any=64     ,		_a:Union[str, Any]=2     ,		_a:List[Any]=3     ,		_a:Optional[Any]="swish"     ,		_a:Any=3     ,		_a:str=32     ,		_a:Optional[int]=0.1     ,		_a:Optional[int]=0.02     ,		_a:Optional[Any]=True     ,		_a:Optional[Any]=True     ,		_a:List[str]=10     ,		_a:List[str]=None     ,		_a:str=0.25     ,		_a:Tuple=0.0     ,		_a:int=0.0     ,		):
       snake_case__						= parent
       snake_case__						= batch_size
       snake_case__						= image_size
       snake_case__						= patch_size
       snake_case__						= num_channels
       snake_case__						= make_divisible(5_12 * width_multiplier     ,		divisor=8   )
       snake_case__						= hidden_act
       snake_case__						= conv_kernel_size
       snake_case__						= output_stride
       snake_case__						= classifier_dropout_prob
       snake_case__						= use_labels
       snake_case__						= is_training
       snake_case__						= num_labels
       snake_case__						= initializer_range
       snake_case__						= scope
       snake_case__						= width_multiplier
       snake_case__						= ffn_dropout
       snake_case__						= attn_dropout

     def 	SCREAMING_SNAKE_CASE__						(					self:Any   ):
       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 	SCREAMING_SNAKE_CASE__						(					self:List[Any]   ):
       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 	SCREAMING_SNAKE_CASE__						(					self:Optional[int]     ,		_a:Dict     ,		_a:List[Any]     ,		_a:str     ,		_a:Optional[int]   ):
       snake_case__						= MobileViTVaModel(config=_a   )
       model.to(_a   )
       model.eval()
       snake_case__						= model(_a   )
       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 	SCREAMING_SNAKE_CASE__						(					self:Dict     ,		_a:Tuple     ,		_a:str     ,		_a:List[str]     ,		_a:Union[str, Any]   ):
       snake_case__						= self.num_labels
       snake_case__						= MobileViTVaForImageClassification(_a   )
       model.to(_a   )
       model.eval()
       snake_case__						= model(_a     ,		labels=_a   )
       self.parent.assertEqual(result.logits.shape     ,		(self.batch_size, self.num_labels)   )

     def 	SCREAMING_SNAKE_CASE__						(					self:Optional[int]     ,		_a:List[str]     ,		_a:List[str]     ,		_a:str     ,		_a:List[str]   ):
       snake_case__						= self.num_labels
       snake_case__						= MobileViTVaForSemanticSegmentation(_a   )
       model.to(_a   )
       model.eval()
       snake_case__						= model(_a   )
       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__						= model(_a     ,		labels=_a   )
       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 	SCREAMING_SNAKE_CASE__						(					self:List[str]   ):
       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_torch
class 		__magic_name__						(snake_case_   ,snake_case_   ,unittest.TestCase				):





     '''simple docstring'''



     __lowercase				:							Tuple	   =       (
         (MobileViTVaModel, MobileViTVaForImageClassification, MobileViTVaForSemanticSegmentation)
         if is_torch_available()
         else ()
     )

     __lowercase				:							Optional[Any]	   =       (
         {
             'feature-extraction': MobileViTVaModel,
             'image-classification': MobileViTVaForImageClassification,
             'image-segmentation': MobileViTVaForSemanticSegmentation,
         }
         if is_torch_available()
         else {}
     )

     __lowercase				:							Optional[Any]	   =       False
     __lowercase				:							Union[str, Any]	   =       False
     __lowercase				:							Union[str, Any]	   =       False
     __lowercase				:							Union[str, Any]	   =       False

     def 	SCREAMING_SNAKE_CASE__						(					self:int   ):
       snake_case__						= MobileViTVaModelTester(self   )
       snake_case__						= MobileViTVaConfigTester(self     ,		config_class=_a     ,		has_text_modality=_a   )

     def 	SCREAMING_SNAKE_CASE__						(					self:int   ):
       self.config_tester.run_common_tests()

     @unittest.skip(reason='''MobileViTV2 does not use inputs_embeds'''   )
     def 	SCREAMING_SNAKE_CASE__						(					self:Dict   ):
       pass

     @unittest.skip(reason='''MobileViTV2 does not support input and output embeddings'''   )
     def 	SCREAMING_SNAKE_CASE__						(					self:int   ):
       pass

     @unittest.skip(reason='''MobileViTV2 does not output attentions'''   )
     def 	SCREAMING_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 	SCREAMING_SNAKE_CASE__						(					self:Optional[Any]   ):
       pass

     @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''   )
     def 	SCREAMING_SNAKE_CASE__						(					self:str   ):
       pass

     def 	SCREAMING_SNAKE_CASE__						(					self:Tuple   ):
       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(_a   )
         snake_case__						= inspect.signature(model.forward   )
         # 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]     ,		_a   )

     def 	SCREAMING_SNAKE_CASE__						(					self:Optional[int]   ):
       snake_case__						= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_a   )

     def 	SCREAMING_SNAKE_CASE__						(					self:Tuple   ):
       def check_hidden_states_output(_a:Tuple     ,		_a:str     ,		_a:Any   ):
         snake_case__						= model_class(_a   )
         model.to(_a   )
         model.eval()

         with torch.no_grad():
           snake_case__						= model(**self._prepare_for_class(_a     ,		_a   )   )

         snake_case__						= outputs.hidden_states

         snake_case__						= 5
         self.assertEqual(len(_a   )     ,		_a   )

         # MobileViTV2'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(_a   )   ):
           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(_a     ,		_a     ,		_a   )

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

         check_hidden_states_output(_a     ,		_a     ,		_a   )

     def 	SCREAMING_SNAKE_CASE__						(					self:Tuple   ):
       snake_case__						= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*_a   )

     def 	SCREAMING_SNAKE_CASE__						(					self:List[Any]   ):
       snake_case__						= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_semantic_segmentation(*_a   )







     @slow
     def 	SCREAMING_SNAKE_CASE__						(					self:Any   ):
       for model_name in MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         snake_case__						= MobileViTVaModel.from_pretrained(_a   )
         self.assertIsNotNone(_a   )







def SCREAMING_SNAKE_CASE	(			)    ->				str:
  snake_case__						= Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''				)
  return image


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





     '''simple docstring'''



     @cached_property
     def 	SCREAMING_SNAKE_CASE__						(					self:List[Any]   ):
       return (
           MobileViTImageProcessor.from_pretrained('''apple/mobilevitv2-1.0-imagenet1k-256'''   )
           if is_vision_available()
           else None
       )

     @slow
     def 	SCREAMING_SNAKE_CASE__						(					self:Tuple   ):
       snake_case__						= MobileViTVaForImageClassification.from_pretrained('''apple/mobilevitv2-1.0-imagenet1k-256'''   ).to(
           _a   )

       snake_case__						= self.default_image_processor
       snake_case__						= prepare_img()
       snake_case__						= image_processor(images=_a     ,		return_tensors='''pt'''   ).to(_a   )

       # forward pass
       with torch.no_grad():
         snake_case__						= model(**_a   )

       # verify the logits
       snake_case__						= torch.Size((1, 10_00)   )
       self.assertEqual(outputs.logits.shape     ,		_a   )

       snake_case__						= torch.tensor([-1.6_336e00, -7.3_204e-02, -5.1_883e-01]   ).to(_a   )

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

     @slow
     def 	SCREAMING_SNAKE_CASE__						(					self:Union[str, Any]   ):
       snake_case__						= MobileViTVaForSemanticSegmentation.from_pretrained('''shehan97/mobilevitv2-1.0-voc-deeplabv3'''   )
       snake_case__						= model.to(_a   )

       snake_case__						= MobileViTImageProcessor.from_pretrained('''shehan97/mobilevitv2-1.0-voc-deeplabv3'''   )

       snake_case__						= prepare_img()
       snake_case__						= image_processor(images=_a     ,		return_tensors='''pt'''   ).to(_a   )

       # forward pass
       with torch.no_grad():
         snake_case__						= model(**_a   )
       snake_case__						= outputs.logits

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

       snake_case__						= torch.tensor(
           [
               [[7.0863, 7.1525, 6.8201], [6.6931, 6.8770, 6.8933], [6.2978, 7.0366, 6.9636]],
               [[-3.7134, -3.6712, -3.6675], [-3.5825, -3.3549, -3.4777], [-3.3435, -3.3979, -3.2857]],
               [[-2.9329, -2.8003, -2.7369], [-3.0564, -2.4780, -2.0207], [-2.6889, -1.9298, -1.7640]],
           ]     ,		device=_a     ,		)

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







     @slow
     def 	SCREAMING_SNAKE_CASE__						(					self:Any   ):
       snake_case__						= MobileViTVaForSemanticSegmentation.from_pretrained('''shehan97/mobilevitv2-1.0-voc-deeplabv3'''   )
       snake_case__						= model.to(_a   )

       snake_case__						= MobileViTImageProcessor.from_pretrained('''shehan97/mobilevitv2-1.0-voc-deeplabv3'''   )

       snake_case__						= prepare_img()
       snake_case__						= image_processor(images=_a     ,		return_tensors='''pt'''   ).to(_a   )

       # forward pass
       with torch.no_grad():
         snake_case__						= model(**_a   )

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

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

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









'''simple docstring'''



import os
import re
from shutil import copyfile
from typing import List, Optional, Tuple

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


__UpperCamelCase   :							int         =   logging.get_logger(__name__)

__UpperCamelCase   :							List[str]         =   {
    """vocab_file""": """vocab.txt""",
    """merges_file""": """bpe.codes""",
}

__UpperCamelCase   :							List[str]         =   {
    """vocab_file""": {
        """vinai/phobert-base""": """https://huggingface.co/vinai/phobert-base/resolve/main/vocab.txt""",
        """vinai/phobert-large""": """https://huggingface.co/vinai/phobert-large/resolve/main/vocab.txt""",
    },
    """merges_file""": {
        """vinai/phobert-base""": """https://huggingface.co/vinai/phobert-base/resolve/main/bpe.codes""",
        """vinai/phobert-large""": """https://huggingface.co/vinai/phobert-large/resolve/main/bpe.codes""",
    },
}

__UpperCamelCase   :							Union[str, Any]         =   {
    """vinai/phobert-base""": 256,
    """vinai/phobert-large""": 256,
}



def  __UpperCAmelCase						(					SCREAMING_SNAKE_CASE__:						int     )	->      List[str]:

   """simple docstring"""
   __a					  =     set()
   __a					  =     word[0]
   for char in word[1:]:
      pairs.add((prev_char, char)     )
      __a					  =     char

   __a					  =     set(SCREAMING_SNAKE_CASE__     )
   return pairs



class 				__SCREAMING_SNAKE_CASE							(				_lowerCAmelCase  ):
      __a	       =VOCAB_FILES_NAMES
      __a	       =PRETRAINED_VOCAB_FILES_MAP
      __a	       =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES




      def __init__(	self			, lowerCamelCase			, lowerCamelCase			, lowerCamelCase="<s>"			, lowerCamelCase="</s>"			, lowerCamelCase="</s>"			, lowerCamelCase="<s>"			, lowerCamelCase="<unk>"			, lowerCamelCase="<pad>"			, lowerCamelCase="<mask>"			, **lowerCamelCase			, )				->str:



         '''simple docstring'''
         super().__init__(
             bos_token=lowerCamelCase			, eos_token=lowerCamelCase			, unk_token=lowerCamelCase			, sep_token=lowerCamelCase			, cls_token=lowerCamelCase			, pad_token=lowerCamelCase			, mask_token=lowerCamelCase			, **lowerCamelCase			, )

         __a					  =     vocab_file
         __a					  =     merges_file

         __a					  =     {}
         __a					  =     0
         __a					  =     1
         __a					  =     2
         __a					  =     3

         self.add_from_file(lowerCamelCase   )

         __a					  =     {v: k for k, v in self.encoder.items()}

         with open(lowerCamelCase			, encoding='utf-8'   ) as merges_handle:
            __a					  =     merges_handle.read().split('\n'   )[:-1]
         __a					  =     [tuple(merge.split()[:-1]   ) for merge in merges]
         __a					  =     dict(zip(lowerCamelCase			, range(len(lowerCamelCase   )   )   )   )
         __a					  =     {}




      def 			__UpperCamelCase				(	self			, lowerCamelCase			, lowerCamelCase = None   )				->List[int]:



         '''simple docstring'''

         if token_ids_a is None:
            return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
         __a					  =     [self.cls_token_id]
         __a					  =     [self.sep_token_id]
         return cls + token_ids_a + sep + sep + token_ids_a + sep




      def 			__UpperCamelCase				(	self			, lowerCamelCase			, lowerCamelCase = None			, lowerCamelCase = False   )				->List[int]:



         '''simple docstring'''

         if already_has_special_tokens:
            return super().get_special_tokens_mask(
                token_ids_a=lowerCamelCase			, token_ids_a=lowerCamelCase			, already_has_special_tokens=lowerCamelCase   )

         if token_ids_a is None:
            return [1] + ([0] * len(lowerCamelCase   )) + [1]
         return [1] + ([0] * len(lowerCamelCase   )) + [1, 1] + ([0] * len(lowerCamelCase   )) + [1]




      def 			__UpperCamelCase				(	self			, lowerCamelCase			, lowerCamelCase = 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 + sep + token_ids_a + sep   ) * [0]




      @property
      def 			__UpperCamelCase				(	self   )				->Union[str, Any]:



         '''simple docstring'''
         return len(self.encoder   )




      def 			__UpperCamelCase				(	self   )				->Optional[int]:



         '''simple docstring'''
         return dict(self.encoder			, **self.added_tokens_encoder   )




      def 			__UpperCamelCase				(	self			, lowerCamelCase   )				->List[str]:



         '''simple docstring'''
         if token in self.cache:
            return self.cache[token]
         __a					  =     tuple(lowerCamelCase   )
         __a					  =     tuple(list(word[:-1]   ) + [word[-1] + '</w>']   )
         __a					  =     get_pairs(lowerCamelCase   )

         if not pairs:
            return token

         while True:
            __a					  =     min(lowerCamelCase			, key=lambda lowerCamelCase   : self.bpe_ranks.get(lowerCamelCase			, float('inf'   )   )   )
            if bigram not in self.bpe_ranks:
               break
            __a  ,      __a					  =     bigram
            __a					  =     []
            __a					  =     0
            while i < len(lowerCamelCase   ):
               try:
                  __a					  =     word.index(lowerCamelCase			, lowerCamelCase   )
               except ValueError:
                  new_word.extend(word[i:]   )
                  break
               else:
                  new_word.extend(word[i:j]   )
                  __a					  =     j

               if word[i] == first and i < len(lowerCamelCase   ) - 1 and word[i + 1] == second:
                  new_word.append(first + second   )
                  i += 2
               else:
                  new_word.append(word[i]   )
                  i += 1
            __a					  =     tuple(lowerCamelCase   )
            __a					  =     new_word
            if len(lowerCamelCase   ) == 1:
               break
            else:
               __a					  =     get_pairs(lowerCamelCase   )
         __a					  =     '@@ '.join(lowerCamelCase   )
         __a					  =     word[:-4]
         __a					  =     word
         return word




      def 			__UpperCamelCase				(	self			, lowerCamelCase   )				->Dict:



         '''simple docstring'''
         __a					  =     []

         __a					  =     re.findall(r'\S+\n?'			, lowerCamelCase   )

         for token in words:
            split_tokens.extend(list(self.bpe(lowerCamelCase   ).split(' '   )   )   )
         return split_tokens




      def 			__UpperCamelCase				(	self			, lowerCamelCase   )				->List[Any]:



         '''simple docstring'''
         return self.encoder.get(lowerCamelCase			, self.encoder.get(self.unk_token   )   )




      def 			__UpperCamelCase				(	self			, lowerCamelCase   )				->List[Any]:



         '''simple docstring'''
         return self.decoder.get(lowerCamelCase			, self.unk_token   )




      def 			__UpperCamelCase				(	self			, lowerCamelCase   )				->Dict:



         '''simple docstring'''
         __a					  =     ' '.join(lowerCamelCase   ).replace('@@ '			, ''   ).strip()
         return out_string




      def 			__UpperCamelCase				(	self			, lowerCamelCase			, lowerCamelCase = None   )				->Tuple[str]:



         '''simple docstring'''
         if not os.path.isdir(lowerCamelCase   ):
            logger.error(F"""Vocabulary path ({save_directory}) should be a directory"""   )
            return
         __a					  =     os.path.join(
             lowerCamelCase			, (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file']   )
         __a					  =     os.path.join(
             lowerCamelCase			, (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['merges_file']   )

         if os.path.abspath(self.vocab_file   ) != os.path.abspath(lowerCamelCase   ):
            copyfile(self.vocab_file			, lowerCamelCase   )

         if os.path.abspath(self.merges_file   ) != os.path.abspath(lowerCamelCase   ):
            copyfile(self.merges_file			, lowerCamelCase   )

         return out_vocab_file, out_merge_file






      def 			__UpperCamelCase				(	self			, lowerCamelCase   )				->str:



         '''simple docstring'''
         if isinstance(lowerCamelCase			, lowerCamelCase   ):
            try:
               with open(lowerCamelCase			, 'r'			, encoding='utf-8'   ) as fd:
                  self.add_from_file(lowerCamelCase   )
            except FileNotFoundError as fnfe:
               raise fnfe
            except UnicodeError:
               raise Exception(F"""Incorrect encoding detected in {f}, please rebuild the dataset"""   )
            return

         __a					  =     f.readlines()
         for lineTmp in lines:
            __a					  =     lineTmp.strip()
            __a					  =     line.rfind(' '   )
            if idx == -1:
               raise ValueError('Incorrect dictionary format, expected \'<token> <cnt>\''   )
            __a					  =     line[:idx]
            __a					  =     len(self.encoder   )