


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				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE=13				,       _SCREAMING_SNAKE_CASE=7				,       _SCREAMING_SNAKE_CASE=True				,       _SCREAMING_SNAKE_CASE=True				,       _SCREAMING_SNAKE_CASE=True				,       _SCREAMING_SNAKE_CASE=True				,       _SCREAMING_SNAKE_CASE=99				,       _SCREAMING_SNAKE_CASE=64				,       _SCREAMING_SNAKE_CASE=32				,       _SCREAMING_SNAKE_CASE=5				,       _SCREAMING_SNAKE_CASE=4				,       _SCREAMING_SNAKE_CASE=37				,       _SCREAMING_SNAKE_CASE="gelu"				,       _SCREAMING_SNAKE_CASE=0.1				,       _SCREAMING_SNAKE_CASE=0.1				,       _SCREAMING_SNAKE_CASE=512				,       _SCREAMING_SNAKE_CASE=16				,       _SCREAMING_SNAKE_CASE=2				,       _SCREAMING_SNAKE_CASE=0.0_2				,       _SCREAMING_SNAKE_CASE=3				,       _SCREAMING_SNAKE_CASE=4				,       _SCREAMING_SNAKE_CASE=None				,       )->str:




         '''simple docstring'''





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



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




         '''simple docstring'''





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

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

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

         A_      :				Any		     =					None
         A_      :				Dict		     =					None
         A_      :				List[str]		     =					None
         if self.use_labels:
           A_      :				str		     =					ids_tensor([self.batch_size]				,       self.type_sequence_label_size		)
           A_      :				Dict		     =					ids_tensor([self.batch_size, self.seq_length]				,       self.num_labels		)
           A_      :				Union[str, Any]		     =					ids_tensor([self.batch_size]				,       self.num_choices		)

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

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



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




         '''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=_SCREAMING_SNAKE_CASE				,       initializer_range=self.initializer_range				,       )



       def        _snake_case       (      self				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		)->List[str]:




         '''simple docstring'''





         A_      :				Tuple		     =					MobileBertModel(config=_SCREAMING_SNAKE_CASE		)
         model.to(_SCREAMING_SNAKE_CASE		)
         model.eval()
         A_      :				Any		     =					model(_SCREAMING_SNAKE_CASE				,       attention_mask=_SCREAMING_SNAKE_CASE				,       token_type_ids=_SCREAMING_SNAKE_CASE		)
         A_      :				List[Any]		     =					model(_SCREAMING_SNAKE_CASE				,       token_type_ids=_SCREAMING_SNAKE_CASE		)
         A_      :				int		     =					model(_SCREAMING_SNAKE_CASE		)

         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        _snake_case       (      self				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		)->Any:




         '''simple docstring'''





         A_      :				str		     =					MobileBertForMaskedLM(config=_SCREAMING_SNAKE_CASE		)
         model.to(_SCREAMING_SNAKE_CASE		)
         model.eval()
         A_      :				List[str]		     =					model(_SCREAMING_SNAKE_CASE				,       attention_mask=_SCREAMING_SNAKE_CASE				,       token_type_ids=_SCREAMING_SNAKE_CASE				,       labels=_SCREAMING_SNAKE_CASE		)
         self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.seq_length, self.vocab_size)		)



       def        _snake_case       (      self				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		)->Optional[Any]:




         '''simple docstring'''





         A_      :				Optional[int]		     =					MobileBertForNextSentencePrediction(config=_SCREAMING_SNAKE_CASE		)
         model.to(_SCREAMING_SNAKE_CASE		)
         model.eval()
         A_      :				Dict		     =					model(
             _SCREAMING_SNAKE_CASE				,       attention_mask=_SCREAMING_SNAKE_CASE				,       token_type_ids=_SCREAMING_SNAKE_CASE				,       labels=_SCREAMING_SNAKE_CASE				,       )
         self.parent.assertEqual(result.logits.shape				,       (self.batch_size, 2)		)



       def        _snake_case       (      self				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		)->Dict:




         '''simple docstring'''





         A_      :				str		     =					MobileBertForPreTraining(config=_SCREAMING_SNAKE_CASE		)
         model.to(_SCREAMING_SNAKE_CASE		)
         model.eval()
         A_      :				Dict		     =					model(
             _SCREAMING_SNAKE_CASE				,       attention_mask=_SCREAMING_SNAKE_CASE				,       token_type_ids=_SCREAMING_SNAKE_CASE				,       labels=_SCREAMING_SNAKE_CASE				,       next_sentence_label=_SCREAMING_SNAKE_CASE				,       )
         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        _snake_case       (      self				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		)->Dict:




         '''simple docstring'''





         A_      :				Any		     =					MobileBertForQuestionAnswering(config=_SCREAMING_SNAKE_CASE		)
         model.to(_SCREAMING_SNAKE_CASE		)
         model.eval()
         A_      :				Any		     =					model(
             _SCREAMING_SNAKE_CASE				,       attention_mask=_SCREAMING_SNAKE_CASE				,       token_type_ids=_SCREAMING_SNAKE_CASE				,       start_positions=_SCREAMING_SNAKE_CASE				,       end_positions=_SCREAMING_SNAKE_CASE				,       )
         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        _snake_case       (      self				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		)->int:




         '''simple docstring'''





         A_      :				Dict		     =					self.num_labels
         A_      :				Dict		     =					MobileBertForSequenceClassification(_SCREAMING_SNAKE_CASE		)
         model.to(_SCREAMING_SNAKE_CASE		)
         model.eval()
         A_      :				List[str]		     =					model(_SCREAMING_SNAKE_CASE				,       attention_mask=_SCREAMING_SNAKE_CASE				,       token_type_ids=_SCREAMING_SNAKE_CASE				,       labels=_SCREAMING_SNAKE_CASE		)
         self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.num_labels)		)



       def        _snake_case       (      self				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		)->Optional[int]:




         '''simple docstring'''





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



       def        _snake_case       (      self				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		)->Any:




         '''simple docstring'''





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







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




         '''simple docstring'''





         A_      :				Optional[int]		     =					self.prepare_config_and_inputs()
         (
             (
             A_
         )     ,       (
             A_
         )     ,       (
             A_
         )     ,       (
             A_
         )     ,       (
             A_
         )     ,       (
             A_
         )     ,       (
             A_
         )     ,       
         )      :				Dict		     =					config_and_inputs
         A_      :				Dict		     =					{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
         return config, inputs_dict






@require_torch
class   _lowerCamelCase (					UpperCamelCase					,							UpperCamelCase					,							unittest.TestCase			):




       """simple docstring"""
       snake_case      =    (
           (
               MobileBertModel,
               MobileBertForMaskedLM,
               MobileBertForMultipleChoice,
               MobileBertForNextSentencePrediction,
               MobileBertForPreTraining,
               MobileBertForQuestionAnswering,
               MobileBertForSequenceClassification,
               MobileBertForTokenClassification,
           )
           if is_torch_available()
           else ()
       )
       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 {}
       )
       snake_case      =    True



       def        _snake_case       (      self				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE=False		)->Dict:




         '''simple docstring'''





         A_      :				List[str]		     =					super()._prepare_for_class(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       return_labels=_SCREAMING_SNAKE_CASE		)

         if return_labels:
           if model_class in get_values(_SCREAMING_SNAKE_CASE		):
             A_      :				Any		     =					torch.zeros(
                 (self.model_tester.batch_size, self.model_tester.seq_length)				,       dtype=torch.long				,       device=_SCREAMING_SNAKE_CASE		)
             A_      :				Optional[int]		     =					torch.zeros(
                 self.model_tester.batch_size				,       dtype=torch.long				,       device=_SCREAMING_SNAKE_CASE		)
         return inputs_dict



       def        _snake_case       (      self		)->int:




         '''simple docstring'''





         A_      :				Union[str, Any]		     =					MobileBertModelTester(self		)
         A_      :				Optional[int]		     =					ConfigTester(self				,       config_class=_SCREAMING_SNAKE_CASE				,       hidden_size=37		)



       def        _snake_case       (      self		)->Any:




         '''simple docstring'''





         self.config_tester.run_common_tests()



       def        _snake_case       (      self		)->List[Any]:




         '''simple docstring'''





         A_      :				str		     =					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_model(*_SCREAMING_SNAKE_CASE		)



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




         '''simple docstring'''





         A_      :				List[str]		     =					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_masked_lm(*_SCREAMING_SNAKE_CASE		)



       def        _snake_case       (      self		)->Tuple:




         '''simple docstring'''





         A_      :				Optional[Any]		     =					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_multiple_choice(*_SCREAMING_SNAKE_CASE		)



       def        _snake_case       (      self		)->Optional[Any]:




         '''simple docstring'''





         A_      :				Union[str, Any]		     =					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*_SCREAMING_SNAKE_CASE		)



       def        _snake_case       (      self		)->List[Any]:




         '''simple docstring'''





         A_      :				Tuple		     =					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_pretraining(*_SCREAMING_SNAKE_CASE		)



       def        _snake_case       (      self		)->List[Any]:




         '''simple docstring'''





         A_      :				Optional[int]		     =					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_question_answering(*_SCREAMING_SNAKE_CASE		)



       def        _snake_case       (      self		)->int:




         '''simple docstring'''





         A_      :				Optional[Any]		     =					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_sequence_classification(*_SCREAMING_SNAKE_CASE		)







       def        _snake_case       (      self		)->Any:




         '''simple docstring'''





         A_      :				Optional[int]		     =					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_token_classification(*_SCREAMING_SNAKE_CASE		)




def   _SCREAMING_SNAKE_CASE	( SCREAMING_SNAKE_CASE       ):
  return torch.tensor(
      SCREAMING_SNAKE_CASE ,			dtype=torch.long ,			device=SCREAMING_SNAKE_CASE ,			)


UpperCamelCase								=	1e-3







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




       """simple docstring"""



       @slow
       def        _snake_case       (      self		)->List[str]:




         '''simple docstring'''





         A_      :				List[str]		     =					MobileBertModel.from_pretrained('''google/mobilebert-uncased'''		).to(_SCREAMING_SNAKE_CASE		)
         A_      :				Optional[int]		     =					_long_tensor([[101, 7110, 1005, 1056, 2023, 1_1333, 1_7413, 1029, 102]]		)
         with torch.no_grad():
           A_      :				Tuple		     =					model(_SCREAMING_SNAKE_CASE		)[0]
         A_      :				Optional[int]		     =					torch.Size((1, 9, 512)		)
         self.assertEqual(output.shape				,       _SCREAMING_SNAKE_CASE		)
         A_      :				Optional[Any]		     =					torch.tensor(
             [
                 [
                     [-2.4_736_526e07, 8.2_691_656e04, 1.6_521_838e05],
                     [-5.7_541_704e-01, 3.9_056_022e00, 4.4_011_507e00],
                     [2.6_047_359e00, 1.5_677_652e00, -1.7_324_188e-01],
                 ]
             ]				,       device=_SCREAMING_SNAKE_CASE				,       )

         # 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
         A_      :				Optional[Any]		     =					torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE		)
         A_      :				int		     =					torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE		)

         self.assertTrue(lower_bound and upper_bound		)








import inspect
import warnings
from typing import Any, Dict, Optional, Union

from packaging import version


def   _SCREAMING_SNAKE_CASE	( *SCREAMING_SNAKE_CASE ,			SCREAMING_SNAKE_CASE = None ,			SCREAMING_SNAKE_CASE=True ,			SCREAMING_SNAKE_CASE=2       ):
  from .. import __version__

  A_      :				Union[str, Any]		     =					take_from
  A_      :				Optional[Any]		     =					()
  if not isinstance(args[0] ,			SCREAMING_SNAKE_CASE       ):
    A_      :				List[str]		     =					(args,)

  for attribute, version_name, message in args:
    if version.parse(version.parse(SCREAMING_SNAKE_CASE       ).base_version       ) >= version.parse(SCREAMING_SNAKE_CASE       ):
      raise ValueError(
          f'''The deprecation tuple {(attribute, version_name, message)} should be removed since diffusers\''''
          f''' version {__version__} is >= {version_name}'''       )

    A_      :				List[Any]		     =					None
    if isinstance(SCREAMING_SNAKE_CASE ,			SCREAMING_SNAKE_CASE       ) and attribute in deprecated_kwargs:
      values += (deprecated_kwargs.pop(SCREAMING_SNAKE_CASE       ),)
      A_      :				Optional[Any]		     =					f'''The `{attribute}` argument is deprecated and will be removed in version {version_name}.'''
    elif hasattr(SCREAMING_SNAKE_CASE ,			SCREAMING_SNAKE_CASE       ):
      values += (getattr(SCREAMING_SNAKE_CASE ,			SCREAMING_SNAKE_CASE       ),)
      A_      :				int		     =					f'''The `{attribute}` attribute is deprecated and will be removed in version {version_name}.'''
    elif deprecated_kwargs is None:
      A_      :				List[Any]		     =					f'''`{attribute}` is deprecated and will be removed in version {version_name}.'''

    if warning is not None:
      A_      :				Union[str, Any]		     =					warning + ''' ''' if standard_warn else ''''''
      warnings.warn(warning + message ,			SCREAMING_SNAKE_CASE ,			stacklevel=SCREAMING_SNAKE_CASE       )

  if isinstance(SCREAMING_SNAKE_CASE ,			SCREAMING_SNAKE_CASE       ) and len(SCREAMING_SNAKE_CASE       ) > 0:
    A_      :				Dict		     =					inspect.getouterframes(inspect.currentframe()       )[1]
    A_      :				Optional[int]		     =					call_frame.filename
    A_      :				Optional[int]		     =					call_frame.lineno
    A_      :				str		     =					call_frame.function
    A_     ,       A_      :				List[str]		     =					next(iter(deprecated_kwargs.items()       )       )
    raise TypeError(f'''{function} in {filename} line {line_number-1} got an unexpected keyword argument `{key}`'''       )

  if len(SCREAMING_SNAKE_CASE       ) == 0:
    return
  elif len(SCREAMING_SNAKE_CASE       ) == 1:
    return values[0]
  return values




