

"""simple docstring"""
import unittest

from transformers import MobileBertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch

    from transformers import (
        MODEL_FOR_PRETRAINING_MAPPING,
        MobileBertForMaskedLM,
        MobileBertForMultipleChoice,
        MobileBertForNextSentencePrediction,
        MobileBertForPreTraining,
        MobileBertForQuestionAnswering,
        MobileBertForSequenceClassification,
        MobileBertForTokenClassification,
        MobileBertModel,
    )

class 	lowerCamelCase     :


  '''simple docstring'''







  def __init__(self				,    _lowerCamelCase				,    _lowerCamelCase=13				,    _lowerCamelCase=7				,    _lowerCamelCase=True				,    _lowerCamelCase=True				,    _lowerCamelCase=True				,    _lowerCamelCase=True				,    _lowerCamelCase=99				,    _lowerCamelCase=64				,    _lowerCamelCase=32				,    _lowerCamelCase=5				,    _lowerCamelCase=4				,    _lowerCamelCase=37				,    _lowerCamelCase="gelu"				,    _lowerCamelCase=0.1				,    _lowerCamelCase=0.1				,    _lowerCamelCase=512				,    _lowerCamelCase=16				,    _lowerCamelCase=2				,    _lowerCamelCase=0.02				,    _lowerCamelCase=3				,    _lowerCamelCase=4				,    _lowerCamelCase=None				,    ):


        """simple docstring"""

        UpperCAmelCase__       :				Union[str, Any]						 =  parent
        UpperCAmelCase__       :				Tuple						 =  batch_size
        UpperCAmelCase__       :				Tuple						 =  seq_length
        UpperCAmelCase__       :				Tuple						 =  is_training
        UpperCAmelCase__       :				Tuple						 =  use_input_mask
        UpperCAmelCase__       :				int						 =  use_token_type_ids
        UpperCAmelCase__       :				Optional[Any]						 =  use_labels
        UpperCAmelCase__       :				int						 =  vocab_size
        UpperCAmelCase__       :				List[Any]						 =  hidden_size
        UpperCAmelCase__       :				Any						 =  embedding_size
        UpperCAmelCase__       :				Any						 =  num_hidden_layers
        UpperCAmelCase__       :				Optional[Any]						 =  num_attention_heads
        UpperCAmelCase__       :				int						 =  intermediate_size
        UpperCAmelCase__       :				Tuple						 =  hidden_act
        UpperCAmelCase__       :				Dict						 =  hidden_dropout_prob
        UpperCAmelCase__       :				Tuple						 =  attention_probs_dropout_prob
        UpperCAmelCase__       :				Union[str, Any]						 =  max_position_embeddings
        UpperCAmelCase__       :				Optional[Any]						 =  type_vocab_size
        UpperCAmelCase__       :				List[Any]						 =  type_sequence_label_size
        UpperCAmelCase__       :				Dict						 =  initializer_range
        UpperCAmelCase__       :				Optional[int]						 =  num_labels
        UpperCAmelCase__       :				Optional[int]						 =  num_choices
        UpperCAmelCase__       :				str						 =  scope



  def       _a	(self     ):


        """simple docstring"""

        UpperCAmelCase__       :				List[str]						 =  ids_tensor([self.batch_size, self.seq_length]				,    self.vocab_size     )

        UpperCAmelCase__       :				List[Any]						 =  None
        if self.use_input_mask:
              UpperCAmelCase__       :				List[Any]						 =  random_attention_mask([self.batch_size, self.seq_length]     )

        UpperCAmelCase__       :				Optional[Any]						 =  None
        if self.use_token_type_ids:
              UpperCAmelCase__       :				List[str]						 =  ids_tensor([self.batch_size, self.seq_length]				,    self.type_vocab_size     )

        UpperCAmelCase__       :				Tuple						 =  None
        UpperCAmelCase__       :				Dict						 =  None
        UpperCAmelCase__       :				int						 =  None
        if self.use_labels:
              UpperCAmelCase__       :				Tuple						 =  ids_tensor([self.batch_size]				,    self.type_sequence_label_size     )
              UpperCAmelCase__       :				Optional[Any]						 =  ids_tensor([self.batch_size, self.seq_length]				,    self.num_labels     )
              UpperCAmelCase__       :				str						 =  ids_tensor([self.batch_size]				,    self.num_choices     )

        UpperCAmelCase__       :				Optional[int]						 =  self.get_config()

        return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels



  def       _a	(self     ):


        """simple docstring"""

        return MobileBertConfig(
            vocab_size=self.vocab_size				,    hidden_size=self.hidden_size				,    num_hidden_layers=self.num_hidden_layers				,    num_attention_heads=self.num_attention_heads				,    intermediate_size=self.intermediate_size				,    embedding_size=self.embedding_size				,    hidden_act=self.hidden_act				,    hidden_dropout_prob=self.hidden_dropout_prob				,    attention_probs_dropout_prob=self.attention_probs_dropout_prob				,    max_position_embeddings=self.max_position_embeddings				,    type_vocab_size=self.type_vocab_size				,    is_decoder=_a				,    initializer_range=self.initializer_range				,    )



  def       _a	(self				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase     ):


        """simple docstring"""

        UpperCAmelCase__       :				List[str]						 =  MobileBertModel(config=_a     )
        model.to(_a     )
        model.eval()
        UpperCAmelCase__       :				List[Any]						 =  model(_a				,    attention_mask=_a				,    token_type_ids=_a     )
        UpperCAmelCase__       :				Union[str, Any]						 =  model(_a				,    token_type_ids=_a     )
        UpperCAmelCase__       :				int						 =  model(_a     )

        self.parent.assertEqual(result.last_hidden_state.shape				,    (self.batch_size, self.seq_length, self.hidden_size)     )
        self.parent.assertEqual(result.pooler_output.shape				,    (self.batch_size, self.hidden_size)     )



  def       _a	(self				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase     ):


        """simple docstring"""

        UpperCAmelCase__       :				Optional[int]						 =  MobileBertForMaskedLM(config=_a     )
        model.to(_a     )
        model.eval()
        UpperCAmelCase__       :				Tuple						 =  model(_a				,    attention_mask=_a				,    token_type_ids=_a				,    labels=_a     )
        self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.seq_length, self.vocab_size)     )



  def       _a	(self				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase     ):


        """simple docstring"""

        UpperCAmelCase__       :				List[str]						 =  MobileBertForNextSentencePrediction(config=_a     )
        model.to(_a     )
        model.eval()
        UpperCAmelCase__       :				Dict						 =  model(
            _a				,    attention_mask=_a				,    token_type_ids=_a				,    labels=_a				,    )
        self.parent.assertEqual(result.logits.shape				,    (self.batch_size, 2)     )



  def       _a	(self				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase     ):


        """simple docstring"""

        UpperCAmelCase__       :				Dict						 =  MobileBertForPreTraining(config=_a     )
        model.to(_a     )
        model.eval()
        UpperCAmelCase__       :				Tuple						 =  model(
            _a				,    attention_mask=_a				,    token_type_ids=_a				,    labels=_a				,    next_sentence_label=_a				,    )
        self.parent.assertEqual(result.prediction_logits.shape				,    (self.batch_size, self.seq_length, self.vocab_size)     )
        self.parent.assertEqual(result.seq_relationship_logits.shape				,    (self.batch_size, 2)     )



  def       _a	(self				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase     ):


        """simple docstring"""

        UpperCAmelCase__       :				int						 =  MobileBertForQuestionAnswering(config=_a     )
        model.to(_a     )
        model.eval()
        UpperCAmelCase__       :				Any						 =  model(
            _a				,    attention_mask=_a				,    token_type_ids=_a				,    start_positions=_a				,    end_positions=_a				,    )
        self.parent.assertEqual(result.start_logits.shape				,    (self.batch_size, self.seq_length)     )
        self.parent.assertEqual(result.end_logits.shape				,    (self.batch_size, self.seq_length)     )



  def       _a	(self				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase     ):


        """simple docstring"""

        UpperCAmelCase__       :				Tuple						 =  self.num_labels
        UpperCAmelCase__       :				Optional[int]						 =  MobileBertForSequenceClassification(_a     )
        model.to(_a     )
        model.eval()
        UpperCAmelCase__       :				int						 =  model(_a				,    attention_mask=_a				,    token_type_ids=_a				,    labels=_a     )
        self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.num_labels)     )



  def       _a	(self				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase     ):


        """simple docstring"""

        UpperCAmelCase__       :				Union[str, Any]						 =  self.num_labels
        UpperCAmelCase__       :				Optional[Any]						 =  MobileBertForTokenClassification(config=_a     )
        model.to(_a     )
        model.eval()
        UpperCAmelCase__       :				Optional[int]						 =  model(_a				,    attention_mask=_a				,    token_type_ids=_a				,    labels=_a     )
        self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.seq_length, self.num_labels)     )



  def       _a	(self				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase     ):


        """simple docstring"""

        UpperCAmelCase__       :				Optional[Any]						 =  self.num_choices
        UpperCAmelCase__       :				str						 =  MobileBertForMultipleChoice(config=_a     )
        model.to(_a     )
        model.eval()
        UpperCAmelCase__       :				Union[str, Any]						 =  input_ids.unsqueeze(1     ).expand(-1				,    self.num_choices				,    -1     ).contiguous()
        UpperCAmelCase__       :				int						 =  token_type_ids.unsqueeze(1     ).expand(-1				,    self.num_choices				,    -1     ).contiguous()
        UpperCAmelCase__       :				Dict						 =  input_mask.unsqueeze(1     ).expand(-1				,    self.num_choices				,    -1     ).contiguous()
        UpperCAmelCase__       :				int						 =  model(
            _a				,    attention_mask=_a				,    token_type_ids=_a				,    labels=_a				,    )
        self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.num_choices)     )




  def       _a	(self     ):


        """simple docstring"""

        UpperCAmelCase__       :				Any						 =  self.prepare_config_and_inputs()
        (
            UpperCAmelCase__					
        )       :				Optional[Any]						 =  config_and_inputs
        UpperCAmelCase__       :				List[str]						 =  {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
        return config, inputs_dict




@require_torch
class 	lowerCamelCase     (    __lowercase , __lowercase , unittest.TestCase					):


  '''simple docstring'''







  SCREAMING_SNAKE_CASE 	=   (
      (
          MobileBertModel,
          MobileBertForMaskedLM,
          MobileBertForMultipleChoice,
          MobileBertForNextSentencePrediction,
          MobileBertForPreTraining,
          MobileBertForQuestionAnswering,
          MobileBertForSequenceClassification,
          MobileBertForTokenClassification,
      )
      if is_torch_available()
      else ()
  )
  SCREAMING_SNAKE_CASE 	=   (
      {
          "feature-extraction": MobileBertModel,
          "fill-mask": MobileBertForMaskedLM,
          "question-answering": MobileBertForQuestionAnswering,
          "text-classification": MobileBertForSequenceClassification,
          "token-classification": MobileBertForTokenClassification,
          "zero-shot": MobileBertForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )
  SCREAMING_SNAKE_CASE 	=   True



  def       _a	(self				,    _lowerCamelCase				,    _lowerCamelCase				,    _lowerCamelCase=False     ):


        """simple docstring"""

        UpperCAmelCase__       :				Any						 =  super()._prepare_for_class(_a				,    _a				,    return_labels=_a     )

        if return_labels:
              if model_class in get_values(_a     ):
                    UpperCAmelCase__       :				Dict						 =  torch.zeros(
                        (self.model_tester.batch_size, self.model_tester.seq_length)				,    dtype=torch.long				,    device=_a     )
                    UpperCAmelCase__       :				Tuple						 =  torch.zeros(
                        self.model_tester.batch_size				,    dtype=torch.long				,    device=_a     )
        return inputs_dict



  def       _a	(self     ):


        """simple docstring"""

        UpperCAmelCase__       :				int						 =  MobileBertModelTester(self     )
        UpperCAmelCase__       :				Tuple						 =  ConfigTester(self				,    config_class=_a				,    hidden_size=37     )



  def       _a	(self     ):


        """simple docstring"""

        self.config_tester.run_common_tests()



  def       _a	(self     ):


        """simple docstring"""

        UpperCAmelCase__       :				str						 =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_model(*_a     )



  def       _a	(self     ):


        """simple docstring"""

        UpperCAmelCase__       :				Tuple						 =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_masked_lm(*_a     )



  def       _a	(self     ):


        """simple docstring"""

        UpperCAmelCase__       :				int						 =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_multiple_choice(*_a     )



  def       _a	(self     ):


        """simple docstring"""

        UpperCAmelCase__       :				Tuple						 =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*_a     )



  def       _a	(self     ):


        """simple docstring"""

        UpperCAmelCase__       :				List[str]						 =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_pretraining(*_a     )



  def       _a	(self     ):


        """simple docstring"""

        UpperCAmelCase__       :				List[Any]						 =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_question_answering(*_a     )



  def       _a	(self     ):


        """simple docstring"""

        UpperCAmelCase__       :				Union[str, Any]						 =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_sequence_classification(*_a     )




  def       _a	(self     ):


        """simple docstring"""

        UpperCAmelCase__       :				Union[str, Any]						 =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_token_classification(*_a     )




def  a__		(		lowerCAmelCase  )     ->      Tuple:
      return torch.tensor(
          __a       ,     dtype=torch.long       ,     device=__a       ,     )


_A						 =   1E-3





@require_torch
@require_sentencepiece
@require_tokenizers
class 	lowerCamelCase     (    unittest.TestCase					):


  '''simple docstring'''







  @slow
  def       _a	(self     ):


        """simple docstring"""

        UpperCAmelCase__       :				List[Any]						 =  MobileBertModel.from_pretrained("""google/mobilebert-uncased"""     ).to(_a     )
        UpperCAmelCase__       :				Optional[Any]						 =  _long_tensor([[101, 7110, 1005, 1056, 2023, 11333, 17413, 1029, 102]]     )
        with torch.no_grad():
              UpperCAmelCase__       :				Dict						 =  model(_a     )[0]
        UpperCAmelCase__       :				List[Any]						 =  torch.Size((1, 9, 512)     )
        self.assertEqual(output.shape				,    _a     )
        UpperCAmelCase__       :				Union[str, Any]						 =  torch.tensor(
            [
                [
                    [-2.4_73_65_26e07, 8.2_69_16_56e04, 1.6_52_18_38e05],
                    [-5.7_54_17_04e-01, 3.9_05_60_22e00, 4.4_01_15_07e00],
                    [2.6_04_73_59e00, 1.5_67_76_52e00, -1.7_32_41_88e-01],
                ]
            ]				,    device=_a				,    )

        # MobileBERT results range from 10e0 to 10e8. Even a 0.0000001% difference with a value of 10e8 results in a
        # ~1 difference, it's therefore not a good idea to measure using addition.
        # Here, we instead divide the expected result with the result in order to obtain ~1. We then check that the
        # result is held between bounds: 1 - TOLERANCE < expected_result / result < 1 + TOLERANCE
        UpperCAmelCase__       :				Optional[int]						 =  torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE     )
        UpperCAmelCase__       :				str						 =  torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE     )

        self.assertTrue(lower_bound and upper_bound     )




import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding

class 				UpperCAmelCase_		(			__lowercase   ):
							"""simple docstring"""







							UpperCAmelCase__       :						str	    =    ["image_processor", "tokenizer"]
							UpperCAmelCase__       :						Optional[int]	    =    "CLIPImageProcessor"
							UpperCAmelCase__       :						Tuple	    =    ("XLMRobertaTokenizer", "XLMRobertaTokenizerFast")


							def __init__(			self       ,							_a=None       ,							_a=None       ,							**_a       )    ->    Any:
												_a	:    int         =		None
												if "feature_extractor" in kwargs:
																	warnings.warn(
																	    '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'''
																	    ''' instead.'''       ,							_a       ,							)
																	_a	:    int         =		kwargs.pop('''feature_extractor'''       )

												_a	:    int         =		image_processor if image_processor is not None else feature_extractor
												if image_processor is None:
																	raise ValueError('''You need to specify an `image_processor`.'''       )
												if tokenizer is None:
																	raise ValueError('''You need to specify a `tokenizer`.'''       )

												super().__init__(_a       ,							_a       )


							def __call__(			self       ,							_a=None       ,							_a=None       ,							_a=None       ,							**_a       )    ->    List[Any]:

												if text is None and images is None:
																	raise ValueError('''You have to specify either text or images. Both cannot be none.'''       )

												if text is not None:
																	_a	:    str         =		self.tokenizer(_a       ,							return_tensors=_a       ,							**_a       )

												if images is not None:
																	_a	:    Optional[Any]         =		self.image_processor(_a       ,							return_tensors=_a       ,							**_a       )

												if text is not None and images is not None:
																	_a	:    List[Any]         =		image_features.pixel_values
																	return encoding
												elif text is not None:
																	return encoding
												else:
																	return BatchEncoding(data=dict(**_a       )       ,							tensor_type=_a       )


							def 				__lowercase				(			self       ,							*_a       ,							**_a       )    ->    Optional[Any]:
												return self.tokenizer.batch_decode(*_a       ,							**_a       )


							def 				__lowercase				(			self       ,							*_a       ,							**_a       )    ->    Optional[int]:
												return self.tokenizer.decode(*_a       ,							**_a       )




							@property
							def 				__lowercase				(			self       )    ->    Optional[int]:
												_a	:    Dict         =		self.tokenizer.model_input_names
												_a	:    Dict         =		self.image_processor.model_input_names
												return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names       )       )



