





'''simple docstring'''






import inspect
import unittest

from transformers import MobileNetVaConfig
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 MobileNetVaForImageClassification, MobileNetVaModel
    from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
    from PIL import Image

    from transformers import MobileNetVaImageProcessor
class 					__snake_case  (					__SCREAMING_SNAKE_CASE							):






   '''simple docstring'''





   def 		__UpperCamelCase						(							self       ):
    snake_case__     :					str    =  self.config_class(**self.inputs_dict       )
    self.parent.assertTrue(hasattr(__SCREAMING_SNAKE_CASE	,		"""tf_padding"""       )       )
    self.parent.assertTrue(hasattr(__SCREAMING_SNAKE_CASE	,		"""depth_multiplier"""       )       )



class 					__snake_case  :






   '''simple docstring'''





   def __init__(							self	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE=1_3	,		__SCREAMING_SNAKE_CASE=3	,		__SCREAMING_SNAKE_CASE=3_2	,		__SCREAMING_SNAKE_CASE=0.25	,		__SCREAMING_SNAKE_CASE=8	,		__SCREAMING_SNAKE_CASE=True	,		__SCREAMING_SNAKE_CASE=1_0_2_4	,		__SCREAMING_SNAKE_CASE=3_2	,		__SCREAMING_SNAKE_CASE="relu6"	,		__SCREAMING_SNAKE_CASE=0.1	,		__SCREAMING_SNAKE_CASE=0.02	,		__SCREAMING_SNAKE_CASE=True	,		__SCREAMING_SNAKE_CASE=True	,		__SCREAMING_SNAKE_CASE=1_0	,		__SCREAMING_SNAKE_CASE=None	,		):
    snake_case__     :					Dict    =  parent
    snake_case__     :					Dict    =  batch_size
    snake_case__     :					int    =  num_channels
    snake_case__     :					Optional[Any]    =  image_size
    snake_case__     :					Optional[Any]    =  depth_multiplier
    snake_case__     :					Tuple    =  min_depth
    snake_case__     :					int    =  tf_padding
    snake_case__     :					List[Any]    =  int(last_hidden_size * depth_multiplier       )
    snake_case__     :					Tuple    =  output_stride
    snake_case__     :					Optional[Any]    =  hidden_act
    snake_case__     :					Optional[int]    =  classifier_dropout_prob
    snake_case__     :					Optional[Any]    =  use_labels
    snake_case__     :					Tuple    =  is_training
    snake_case__     :					Dict    =  num_labels
    snake_case__     :					int    =  initializer_range
    snake_case__     :					Optional[Any]    =  scope

   def 		__UpperCamelCase						(							self       ):
    snake_case__     :					Optional[Any]    =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

    snake_case__     :					List[Any]    =  None
    snake_case__     :					List[str]    =  None
    if self.use_labels:
     snake_case__     :					Union[str, Any]    =  ids_tensor([self.batch_size]	,		self.num_labels       )
     snake_case__     :					Optional[Any]    =  ids_tensor([self.batch_size, self.image_size, self.image_size]	,		self.num_labels       )

    snake_case__     :					List[Any]    =  self.get_config()

    return config, pixel_values, labels, pixel_labels

   def 		__UpperCamelCase						(							self       ):
    return MobileNetVaConfig(
        num_channels=self.num_channels	,		image_size=self.image_size	,		depth_multiplier=self.depth_multiplier	,		min_depth=self.min_depth	,		tf_padding=self.tf_padding	,		hidden_act=self.hidden_act	,		classifier_dropout_prob=self.classifier_dropout_prob	,		initializer_range=self.initializer_range	,		)

   def 		__UpperCamelCase						(							self	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       ):
    snake_case__     :					Optional[int]    =  MobileNetVaModel(config=__SCREAMING_SNAKE_CASE       )
    model.to(__SCREAMING_SNAKE_CASE       )
    model.eval()
    snake_case__     :					List[Any]    =  model(__SCREAMING_SNAKE_CASE       )
    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 		__UpperCamelCase						(							self	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       ):
    snake_case__     :					str    =  self.num_labels
    snake_case__     :					Tuple    =  MobileNetVaForImageClassification(__SCREAMING_SNAKE_CASE       )
    model.to(__SCREAMING_SNAKE_CASE       )
    model.eval()
    snake_case__     :					Optional[Any]    =  model(__SCREAMING_SNAKE_CASE	,		labels=__SCREAMING_SNAKE_CASE       )
    self.parent.assertEqual(result.logits.shape	,		(self.batch_size, self.num_labels)       )


   def 		__UpperCamelCase						(							self       ):
    snake_case__     :					str    =  self.prepare_config_and_inputs()
    snake_case__  ,							snake_case__  ,							snake_case__  ,							snake_case__     :					Optional[int]    =  config_and_inputs
    snake_case__     :					Optional[int]    =  {"""pixel_values""": pixel_values}
    return config, inputs_dict



@require_torch
class 					__snake_case  (					__SCREAMING_SNAKE_CASE		,				__SCREAMING_SNAKE_CASE		,				unittest.TestCase							):






   '''simple docstring'''





   lowerCamelCase__    							=      (MobileNetVaModel, MobileNetVaForImageClassification) if is_torch_available() else ()
   lowerCamelCase__    							=      (
       {'''feature-extraction''': MobileNetVaModel, '''image-classification''': MobileNetVaForImageClassification}
       if is_torch_available()
       else {}
   )

   lowerCamelCase__    							=      False
   lowerCamelCase__    							=      False
   lowerCamelCase__    							=      False
   lowerCamelCase__    							=      False

   def 		__UpperCamelCase						(							self       ):
    snake_case__     :					List[str]    =  MobileNetVaModelTester(self       )
    snake_case__     :					str    =  MobileNetVaConfigTester(self	,		config_class=__SCREAMING_SNAKE_CASE	,		has_text_modality=__SCREAMING_SNAKE_CASE       )

   def 		__UpperCamelCase						(							self       ):
    self.config_tester.run_common_tests()

   @unittest.skip(reason="""MobileNetV1 does not use inputs_embeds"""       )
   def 		__UpperCamelCase						(							self       ):
    pass

   @unittest.skip(reason="""MobileNetV1 does not support input and output embeddings"""       )
   def 		__UpperCamelCase						(							self       ):
    pass

   @unittest.skip(reason="""MobileNetV1 does not output attentions"""       )
   def 		__UpperCamelCase						(							self       ):
    pass

   def 		__UpperCamelCase						(							self       ):
    snake_case__  ,							snake_case__     :					Tuple    =  self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
     snake_case__     :					Optional[int]    =  model_class(__SCREAMING_SNAKE_CASE       )
     snake_case__     :					List[Any]    =  inspect.signature(model.forward       )
     # signature.parameters is an OrderedDict => so arg_names order is deterministic
     snake_case__     :					Dict    =  [*signature.parameters.keys()]

     snake_case__     :					Optional[int]    =  ["""pixel_values"""]
     self.assertListEqual(arg_names[:1]	,		__SCREAMING_SNAKE_CASE       )

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

   def 		__UpperCamelCase						(							self       ):
    def check_hidden_states_output(__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       ):
     snake_case__     :					List[str]    =  model_class(__SCREAMING_SNAKE_CASE       )
     model.to(__SCREAMING_SNAKE_CASE       )
     model.eval()

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

     snake_case__     :					Optional[Any]    =  outputs.hidden_states

     snake_case__     :					int    =  2_6
     self.assertEqual(len(__SCREAMING_SNAKE_CASE       )	,		__SCREAMING_SNAKE_CASE       )

    snake_case__  ,							snake_case__     :					Optional[Any]    =  self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
     snake_case__     :					Optional[Any]    =  True
     check_hidden_states_output(__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       )

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

     check_hidden_states_output(__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       )

   def 		__UpperCamelCase						(							self       ):
    snake_case__     :					Dict    =  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_image_classification(*__SCREAMING_SNAKE_CASE       )


   @slow
   def 		__UpperCamelCase						(							self       ):
    for model_name in MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     snake_case__     :					Dict    =  MobileNetVaModel.from_pretrained(__SCREAMING_SNAKE_CASE       )
     self.assertIsNotNone(__SCREAMING_SNAKE_CASE       )



def        UpperCamelCase__		(						)				->			List[str]:
 '''simple docstring'''






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





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






   '''simple docstring'''





   @cached_property
   def 		__UpperCamelCase						(							self       ):
    return (
        MobileNetVaImageProcessor.from_pretrained("""google/mobilenet_v1_1.0_224"""       ) if is_vision_available() else None
    )


   @slow
   def 		__UpperCamelCase						(							self       ):
    snake_case__     :					int    =  MobileNetVaForImageClassification.from_pretrained("""google/mobilenet_v1_1.0_224"""       ).to(__SCREAMING_SNAKE_CASE       )

    snake_case__     :					Any    =  self.default_image_processor
    snake_case__     :					List[str]    =  prepare_img()
    snake_case__     :					Optional[Any]    =  image_processor(images=__SCREAMING_SNAKE_CASE	,		return_tensors="""pt"""       ).to(__SCREAMING_SNAKE_CASE       )

    # forward pass
    with torch.no_grad():
     snake_case__     :					Optional[Any]    =  model(**__SCREAMING_SNAKE_CASE       )

    # verify the logits
    snake_case__     :					List[Any]    =  torch.Size((1, 1_0_0_1)       )
    self.assertEqual(outputs.logits.shape	,		__SCREAMING_SNAKE_CASE       )

    snake_case__     :					List[Any]    =  torch.tensor([-4.1739, -1.1233, 3.1205]       ).to(__SCREAMING_SNAKE_CASE       )

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








import json
from typing import List, Optional, Tuple

from tokenizers import pre_tokenizers, processors

from ...tokenization_utils_base import AddedToken, BatchEncoding
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_roberta import RobertaTokenizer


SCREAMING_SNAKE_CASE						:str			=  logging.get_logger(__name__)

SCREAMING_SNAKE_CASE						:Dict			=  {'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_file''': '''tokenizer.json'''}

SCREAMING_SNAKE_CASE						:str			=  {
    '''vocab_file''': {
        '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/vocab.json''',
        '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/vocab.json''',
        '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/vocab.json''',
        '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/vocab.json''',
        '''roberta-base-openai-detector''': '''https://huggingface.co/roberta-base-openai-detector/resolve/main/vocab.json''',
        '''roberta-large-openai-detector''': (
            '''https://huggingface.co/roberta-large-openai-detector/resolve/main/vocab.json'''
        ),
    },
    '''merges_file''': {
        '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/merges.txt''',
        '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/merges.txt''',
        '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/merges.txt''',
        '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/merges.txt''',
        '''roberta-base-openai-detector''': '''https://huggingface.co/roberta-base-openai-detector/resolve/main/merges.txt''',
        '''roberta-large-openai-detector''': (
            '''https://huggingface.co/roberta-large-openai-detector/resolve/main/merges.txt'''
        ),
    },
    '''tokenizer_file''': {
        '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/tokenizer.json''',
        '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/tokenizer.json''',
        '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/tokenizer.json''',
        '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/tokenizer.json''',
        '''roberta-base-openai-detector''': (
            '''https://huggingface.co/roberta-base-openai-detector/resolve/main/tokenizer.json'''
        ),
        '''roberta-large-openai-detector''': (
            '''https://huggingface.co/roberta-large-openai-detector/resolve/main/tokenizer.json'''
        ),
    },
}

SCREAMING_SNAKE_CASE						:Tuple			=  {
    '''roberta-base''': 5_12,
    '''roberta-large''': 5_12,
    '''roberta-large-mnli''': 5_12,
    '''distilroberta-base''': 5_12,
    '''roberta-base-openai-detector''': 5_12,
    '''roberta-large-openai-detector''': 5_12,
}


class      __lowerCAmelCase  (			a       ):






  """simple docstring"""





  _SCREAMING_SNAKE_CASE		     =							VOCAB_FILES_NAMES
  _SCREAMING_SNAKE_CASE		     =							PRETRAINED_VOCAB_FILES_MAP
  _SCREAMING_SNAKE_CASE		     =							PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
  _SCREAMING_SNAKE_CASE		     =							['input_ids', 'attention_mask']
  _SCREAMING_SNAKE_CASE		     =							RobertaTokenizer



  def __init__(  self							:							Tuple     ,					_lowerCAmelCase							:							Union[str, Any]=None     ,					_lowerCAmelCase							:							Tuple=None     ,					_lowerCAmelCase							:							Any=None     ,					_lowerCAmelCase							:							Optional[Any]="replace"     ,					_lowerCAmelCase							:							str="<s>"     ,					_lowerCAmelCase							:							Tuple="</s>"     ,					_lowerCAmelCase							:							Any="</s>"     ,					_lowerCAmelCase							:							Tuple="<s>"     ,					_lowerCAmelCase							:							Any="<unk>"     ,					_lowerCAmelCase							:							Tuple="<pad>"     ,					_lowerCAmelCase							:							List[str]="<mask>"     ,					_lowerCAmelCase							:							str=False     ,					_lowerCAmelCase							:							Union[str, Any]=True     ,					**_lowerCAmelCase							:							int     ,					) ->			str:




     """simple docstring"""
     super().__init__(
         _lowerCAmelCase     ,					_lowerCAmelCase     ,					tokenizer_file=_lowerCAmelCase     ,					errors=_lowerCAmelCase     ,					bos_token=_lowerCAmelCase     ,					eos_token=_lowerCAmelCase     ,					sep_token=_lowerCAmelCase     ,					cls_token=_lowerCAmelCase     ,					unk_token=_lowerCAmelCase     ,					pad_token=_lowerCAmelCase     ,					mask_token=_lowerCAmelCase     ,					add_prefix_space=_lowerCAmelCase     ,					trim_offsets=_lowerCAmelCase     ,					**_lowerCAmelCase     ,					)

     snake_case_            =       json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__()	)
     if pre_tok_state.get("add_prefix_space"     ,					_lowerCAmelCase	) != add_prefix_space:
        snake_case_            =       getattr(_lowerCAmelCase     ,					pre_tok_state.pop("type"	)	)
        snake_case_            =       add_prefix_space
        snake_case_            =       pre_tok_class(**_lowerCAmelCase	)

     snake_case_            =       add_prefix_space

     snake_case_            =       "post_processor"
     snake_case_            =       getattr(self.backend_tokenizer     ,					_lowerCAmelCase     ,					_lowerCAmelCase	)
     if tokenizer_component_instance:
        snake_case_            =       json.loads(tokenizer_component_instance.__getstate__()	)

        # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class`
        if "sep" in state:
           snake_case_            =       tuple(state["sep"]	)
        if "cls" in state:
           snake_case_            =       tuple(state["cls"]	)

        snake_case_            =       False

        if state.get("add_prefix_space"     ,					_lowerCAmelCase	) != add_prefix_space:
           snake_case_            =       add_prefix_space
           snake_case_            =       True

        if state.get("trim_offsets"     ,					_lowerCAmelCase	) != trim_offsets:
           snake_case_            =       trim_offsets
           snake_case_            =       True

        if changes_to_apply:
           snake_case_            =       getattr(_lowerCAmelCase     ,					state.pop("type"	)	)
           snake_case_            =       component_class(**_lowerCAmelCase	)
           setattr(self.backend_tokenizer     ,					_lowerCAmelCase     ,					_lowerCAmelCase	)



  @property
  def        lowerCAmelCase__	(  self							:							Dict	) ->			str:




     """simple docstring"""
     if self._mask_token is None:
        if self.verbose:
           logger.error("Using mask_token, but it is not set yet."	)
        return None
     return str(self._mask_token	)



  @mask_token.setter
  def        lowerCAmelCase__	(  self							:							Tuple     ,					_lowerCAmelCase							:							Tuple	) ->			Optional[int]:




     """simple docstring"""
     snake_case_            =       AddedToken(_lowerCAmelCase     ,					lstrip=_lowerCAmelCase     ,					rstrip=_lowerCAmelCase	) if isinstance(_lowerCAmelCase     ,					_lowerCAmelCase	) else value
     snake_case_            =       value



  def        lowerCAmelCase__	(  self							:							Dict     ,					*_lowerCAmelCase							:							Optional[int]     ,					**_lowerCAmelCase							:							Optional[int]	) ->			BatchEncoding:




     """simple docstring"""
     snake_case_            =       kwargs.get("is_split_into_words"     ,					_lowerCAmelCase	)
     assert self.add_prefix_space or not is_split_into_words, (
         F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True '''
         "to use it with pretokenized inputs."
     )

     return super()._batch_encode_plus(*_lowerCAmelCase     ,					**_lowerCAmelCase	)



  def        lowerCAmelCase__	(  self							:							Optional[Any]     ,					*_lowerCAmelCase							:							str     ,					**_lowerCAmelCase							:							Any	) ->			BatchEncoding:




     """simple docstring"""
     snake_case_            =       kwargs.get("is_split_into_words"     ,					_lowerCAmelCase	)

     assert self.add_prefix_space or not is_split_into_words, (
         F'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True '''
         "to use it with pretokenized inputs."
     )

     return super()._encode_plus(*_lowerCAmelCase     ,					**_lowerCAmelCase	)



  def        lowerCAmelCase__	(  self							:							int     ,					_lowerCAmelCase							:							str     ,					_lowerCAmelCase							:							Optional[str] = None	) ->			Tuple[str]:




     """simple docstring"""
     snake_case_            =       self._tokenizer.model.save(_lowerCAmelCase     ,					name=_lowerCAmelCase	)
     return tuple(_lowerCAmelCase	)



  def        lowerCAmelCase__	(  self							:							Any     ,					_lowerCAmelCase							:							int     ,					_lowerCAmelCase							:							List[Any]=None	) ->			Union[str, Any]:




     """simple docstring"""
     snake_case_            =       [self.bos_token_id] + token_ids_a + [self.eos_token_id]
     if token_ids_a is None:
        return output

     return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id]



  def        lowerCAmelCase__	(  self							:							Union[str, Any]     ,					_lowerCAmelCase							:							List[int]     ,					_lowerCAmelCase							:							Optional[List[int]] = None	) ->			List[int]:




     """simple docstring"""
     snake_case_            =       [self.sep_token_id]
     snake_case_            =       [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]




