





'''simple docstring'''

from __future__ import annotations

import unittest

from transformers import MobileBertConfig, is_tf_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
    import tensorflow as tf

    from transformers import (
        TF_MODEL_FOR_PRETRAINING_MAPPING,
        TFMobileBertForMaskedLM,
        TFMobileBertForMultipleChoice,
        TFMobileBertForNextSentencePrediction,
        TFMobileBertForPreTraining,
        TFMobileBertForQuestionAnswering,
        TFMobileBertForSequenceClassification,
        TFMobileBertForTokenClassification,
        TFMobileBertModel,
    )







@require_tf
class __a (   SCREAMING_SNAKE_CASE_,   SCREAMING_SNAKE_CASE_,   unittest.TestCase			):
   __UpperCamelCase   : Optional[Any]	      =	(
       (
           TFMobileBertModel,
           TFMobileBertForMaskedLM,
           TFMobileBertForNextSentencePrediction,
           TFMobileBertForPreTraining,
           TFMobileBertForQuestionAnswering,
           TFMobileBertForSequenceClassification,
           TFMobileBertForTokenClassification,
           TFMobileBertForMultipleChoice,
       )
       if is_tf_available()
       else ()
   )
   __UpperCamelCase   : List[str]	      =	(
       {
           'feature-extraction': TFMobileBertModel,
           'fill-mask': TFMobileBertForMaskedLM,
           'question-answering': TFMobileBertForQuestionAnswering,
           'text-classification': TFMobileBertForSequenceClassification,
           'token-classification': TFMobileBertForTokenClassification,
           'zero-shot': TFMobileBertForSequenceClassification,
       }
       if is_tf_available()
       else {}
   )
   __UpperCamelCase   : int	      =	False
   __UpperCamelCase   : List[Any]	      =	False





   def      UpperCAmelCase__     (					self				:			Any				,lowerCamelCase				:			List[Any]				,lowerCamelCase				:			int				,lowerCamelCase				:			List[Any]=False    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       super()._prepare_for_class(UpperCamelCase__				,UpperCamelCase__				,return_labels=UpperCamelCase__    )

      if return_labels:
         if model_class in get_values(UpperCamelCase__    ):
            __SCREAMING_SNAKE_CASE      							=       tf.zeros(self.model_tester.batch_size				,dtype=tf.intaa    )

      return inputs_dict







   class __a (   SCREAMING_SNAKE_CASE_			):





      def __init__(					self				:			List[Any]				,lowerCamelCase				:			int				,lowerCamelCase				:			Dict=13				,lowerCamelCase				:			Optional[Any]=7				,lowerCamelCase				:			Optional[Any]=True				,lowerCamelCase				:			Optional[int]=True				,lowerCamelCase				:			Tuple=True				,lowerCamelCase				:			List[Any]=True				,lowerCamelCase				:			Optional[Any]=99				,lowerCamelCase				:			Optional[int]=32				,lowerCamelCase				:			Any=32				,lowerCamelCase				:			Dict=2				,lowerCamelCase				:			Any=4				,lowerCamelCase				:			str=37				,lowerCamelCase				:			Optional[Any]="gelu"				,lowerCamelCase				:			Dict=0.1				,lowerCamelCase				:			str=0.1				,lowerCamelCase				:			str=512				,lowerCamelCase				:			Optional[int]=16				,lowerCamelCase				:			List[str]=2				,lowerCamelCase				:			Any=0.02				,lowerCamelCase				:			Optional[int]=3				,lowerCamelCase				:			Union[str, Any]=4				,lowerCamelCase				:			Optional[Any]=None				,):

         '''simple docstring'''

         __SCREAMING_SNAKE_CASE      							=       parent
         __SCREAMING_SNAKE_CASE      							=       batch_size
         __SCREAMING_SNAKE_CASE      							=       seq_length
         __SCREAMING_SNAKE_CASE      							=       is_training
         __SCREAMING_SNAKE_CASE      							=       use_input_mask
         __SCREAMING_SNAKE_CASE      							=       use_token_type_ids
         __SCREAMING_SNAKE_CASE      							=       use_labels
         __SCREAMING_SNAKE_CASE      							=       vocab_size
         __SCREAMING_SNAKE_CASE      							=       hidden_size
         __SCREAMING_SNAKE_CASE      							=       num_hidden_layers
         __SCREAMING_SNAKE_CASE      							=       num_attention_heads
         __SCREAMING_SNAKE_CASE      							=       intermediate_size
         __SCREAMING_SNAKE_CASE      							=       hidden_act
         __SCREAMING_SNAKE_CASE      							=       hidden_dropout_prob
         __SCREAMING_SNAKE_CASE      							=       attention_probs_dropout_prob
         __SCREAMING_SNAKE_CASE      							=       max_position_embeddings
         __SCREAMING_SNAKE_CASE      							=       type_vocab_size
         __SCREAMING_SNAKE_CASE      							=       type_sequence_label_size
         __SCREAMING_SNAKE_CASE      							=       initializer_range
         __SCREAMING_SNAKE_CASE      							=       num_labels
         __SCREAMING_SNAKE_CASE      							=       num_choices
         __SCREAMING_SNAKE_CASE      							=       scope
         __SCREAMING_SNAKE_CASE      							=       embedding_size





      def      UpperCAmelCase__     (					self				:			List[str]    ):

         '''simple docstring'''

         __SCREAMING_SNAKE_CASE      							=       ids_tensor([self.batch_size, self.seq_length]				,self.vocab_size    )

         __SCREAMING_SNAKE_CASE      							=       None
         if self.use_input_mask:
            __SCREAMING_SNAKE_CASE      							=       random_attention_mask([self.batch_size, self.seq_length]    )

         __SCREAMING_SNAKE_CASE      							=       None
         if self.use_token_type_ids:
            __SCREAMING_SNAKE_CASE      							=       ids_tensor([self.batch_size, self.seq_length]				,self.type_vocab_size    )

         __SCREAMING_SNAKE_CASE      							=       None
         __SCREAMING_SNAKE_CASE      							=       None
         __SCREAMING_SNAKE_CASE      							=       None
         if self.use_labels:
            __SCREAMING_SNAKE_CASE      							=       ids_tensor([self.batch_size]				,self.type_sequence_label_size    )
            __SCREAMING_SNAKE_CASE      							=       ids_tensor([self.batch_size, self.seq_length]				,self.num_labels    )
            __SCREAMING_SNAKE_CASE      							=       ids_tensor([self.batch_size]				,self.num_choices    )

         __SCREAMING_SNAKE_CASE      							=       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				,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				,initializer_range=self.initializer_range				,embedding_size=self.embedding_size				,)

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





      def      UpperCAmelCase__     (					self				:			Dict				,lowerCamelCase				:			Union[str, Any]				,lowerCamelCase				:			Any				,lowerCamelCase				:			Optional[int]				,lowerCamelCase				:			int				,lowerCamelCase				:			Optional[Any]				,lowerCamelCase				:			Any				,lowerCamelCase				:			str    ):

         '''simple docstring'''

         __SCREAMING_SNAKE_CASE      							=       TFMobileBertModel(config=UpperCamelCase__    )
         __SCREAMING_SNAKE_CASE      							=       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
         __SCREAMING_SNAKE_CASE      							=       model(UpperCamelCase__    )

         __SCREAMING_SNAKE_CASE      							=       [input_ids, input_mask]
         __SCREAMING_SNAKE_CASE      							=       model(UpperCamelCase__    )

         __SCREAMING_SNAKE_CASE      							=       model(UpperCamelCase__    )

         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      UpperCAmelCase__     (					self				:			List[str]				,lowerCamelCase				:			List[str]				,lowerCamelCase				:			str				,lowerCamelCase				:			Optional[Any]				,lowerCamelCase				:			Any				,lowerCamelCase				:			int				,lowerCamelCase				:			Tuple				,lowerCamelCase				:			Optional[Any]    ):

         '''simple docstring'''

         __SCREAMING_SNAKE_CASE      							=       TFMobileBertForMaskedLM(config=UpperCamelCase__    )
         __SCREAMING_SNAKE_CASE      							=       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
         __SCREAMING_SNAKE_CASE      							=       model(UpperCamelCase__    )
         self.parent.assertEqual(result.logits.shape				,(self.batch_size, self.seq_length, self.vocab_size)    )





      def      UpperCAmelCase__     (					self				:			str				,lowerCamelCase				:			Tuple				,lowerCamelCase				:			str				,lowerCamelCase				:			Union[str, Any]				,lowerCamelCase				:			int				,lowerCamelCase				:			List[str]				,lowerCamelCase				:			Any				,lowerCamelCase				:			int    ):

         '''simple docstring'''

         __SCREAMING_SNAKE_CASE      							=       TFMobileBertForNextSentencePrediction(config=UpperCamelCase__    )
         __SCREAMING_SNAKE_CASE      							=       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
         __SCREAMING_SNAKE_CASE      							=       model(UpperCamelCase__    )
         self.parent.assertEqual(result.logits.shape				,(self.batch_size, 2)    )





      def      UpperCAmelCase__     (					self				:			int				,lowerCamelCase				:			Any				,lowerCamelCase				:			List[str]				,lowerCamelCase				:			Union[str, Any]				,lowerCamelCase				:			List[str]				,lowerCamelCase				:			Optional[Any]				,lowerCamelCase				:			Optional[Any]				,lowerCamelCase				:			Tuple    ):

         '''simple docstring'''

         __SCREAMING_SNAKE_CASE      							=       TFMobileBertForPreTraining(config=UpperCamelCase__    )
         __SCREAMING_SNAKE_CASE      							=       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
         __SCREAMING_SNAKE_CASE      							=       model(UpperCamelCase__    )
         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      UpperCAmelCase__     (					self				:			Dict				,lowerCamelCase				:			Optional[Any]				,lowerCamelCase				:			Dict				,lowerCamelCase				:			List[str]				,lowerCamelCase				:			int				,lowerCamelCase				:			Any				,lowerCamelCase				:			List[Any]				,lowerCamelCase				:			Optional[int]    ):

         '''simple docstring'''

         __SCREAMING_SNAKE_CASE      							=       self.num_labels
         __SCREAMING_SNAKE_CASE      							=       TFMobileBertForSequenceClassification(config=UpperCamelCase__    )
         __SCREAMING_SNAKE_CASE      							=       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
         __SCREAMING_SNAKE_CASE      							=       model(UpperCamelCase__    )
         self.parent.assertEqual(result.logits.shape				,(self.batch_size, self.num_labels)    )





      def      UpperCAmelCase__     (					self				:			Optional[Any]				,lowerCamelCase				:			Optional[Any]				,lowerCamelCase				:			int				,lowerCamelCase				:			List[Any]				,lowerCamelCase				:			Dict				,lowerCamelCase				:			List[Any]				,lowerCamelCase				:			Optional[int]				,lowerCamelCase				:			int    ):

         '''simple docstring'''

         __SCREAMING_SNAKE_CASE      							=       self.num_choices
         __SCREAMING_SNAKE_CASE      							=       TFMobileBertForMultipleChoice(config=UpperCamelCase__    )
         __SCREAMING_SNAKE_CASE      							=       tf.tile(tf.expand_dims(UpperCamelCase__				,1    )				,(1, self.num_choices, 1)    )
         __SCREAMING_SNAKE_CASE      							=       tf.tile(tf.expand_dims(UpperCamelCase__				,1    )				,(1, self.num_choices, 1)    )
         __SCREAMING_SNAKE_CASE      							=       tf.tile(tf.expand_dims(UpperCamelCase__				,1    )				,(1, self.num_choices, 1)    )
         __SCREAMING_SNAKE_CASE      							=       {
             '''input_ids''': multiple_choice_inputs_ids,
             '''attention_mask''': multiple_choice_input_mask,
             '''token_type_ids''': multiple_choice_token_type_ids,
         }
         __SCREAMING_SNAKE_CASE      							=       model(UpperCamelCase__    )
         self.parent.assertEqual(result.logits.shape				,(self.batch_size, self.num_choices)    )





      def      UpperCAmelCase__     (					self				:			int				,lowerCamelCase				:			Union[str, Any]				,lowerCamelCase				:			Union[str, Any]				,lowerCamelCase				:			List[str]				,lowerCamelCase				:			List[Any]				,lowerCamelCase				:			Any				,lowerCamelCase				:			Optional[int]				,lowerCamelCase				:			str    ):

         '''simple docstring'''

         __SCREAMING_SNAKE_CASE      							=       self.num_labels
         __SCREAMING_SNAKE_CASE      							=       TFMobileBertForTokenClassification(config=UpperCamelCase__    )
         __SCREAMING_SNAKE_CASE      							=       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
         __SCREAMING_SNAKE_CASE      							=       model(UpperCamelCase__    )
         self.parent.assertEqual(result.logits.shape				,(self.batch_size, self.seq_length, self.num_labels)    )





      def      UpperCAmelCase__     (					self				:			int				,lowerCamelCase				:			List[Any]				,lowerCamelCase				:			Optional[int]				,lowerCamelCase				:			int				,lowerCamelCase				:			int				,lowerCamelCase				:			Optional[Any]				,lowerCamelCase				:			List[str]				,lowerCamelCase				:			Any    ):

         '''simple docstring'''

         __SCREAMING_SNAKE_CASE      							=       TFMobileBertForQuestionAnswering(config=UpperCamelCase__    )
         __SCREAMING_SNAKE_CASE      							=       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
         __SCREAMING_SNAKE_CASE      							=       model(UpperCamelCase__    )
         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      UpperCAmelCase__     (					self				:			str    ):

         '''simple docstring'''

         __SCREAMING_SNAKE_CASE      							=       self.prepare_config_and_inputs()
         (
             __SCREAMING_SNAKE_CASE							
         )      							=       config_and_inputs
         __SCREAMING_SNAKE_CASE      							=       {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
         return config, inputs_dict





   def      UpperCAmelCase__     (					self				:			List[Any]    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       TFMobileBertModelTest.TFMobileBertModelTester(self    )
      __SCREAMING_SNAKE_CASE      							=       ConfigTester(self				,config_class=UpperCamelCase__				,hidden_size=37    )





   def      UpperCAmelCase__     (					self				:			Optional[int]    ):

      '''simple docstring'''

      self.config_tester.run_common_tests()





   def      UpperCAmelCase__     (					self				:			Optional[Any]    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_model(*UpperCamelCase__    )





   def      UpperCAmelCase__     (					self				:			Optional[int]    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_masked_lm(*UpperCamelCase__    )





   def      UpperCAmelCase__     (					self				:			List[Any]    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_multiple_choice(*UpperCamelCase__    )





   def      UpperCAmelCase__     (					self				:			Dict    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*UpperCamelCase__    )





   def      UpperCAmelCase__     (					self				:			str    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_pretraining(*UpperCamelCase__    )





   def      UpperCAmelCase__     (					self				:			Dict    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_question_answering(*UpperCamelCase__    )





   def      UpperCAmelCase__     (					self				:			Optional[int]    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_sequence_classification(*UpperCamelCase__    )





   def      UpperCAmelCase__     (					self				:			Optional[int]    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_token_classification(*UpperCamelCase__    )





   @slow
   def      UpperCAmelCase__     (					self				:			Optional[int]    ):

      '''simple docstring'''

      for model_name in ["google/mobilebert-uncased"]:
         __SCREAMING_SNAKE_CASE      							=       TFMobileBertModel.from_pretrained(UpperCamelCase__    )
         self.assertIsNotNone(UpperCamelCase__    )







@require_tf
class __a (   unittest.TestCase			):





   @slow
   def      UpperCAmelCase__     (					self				:			Optional[int]    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       TFMobileBertForPreTraining.from_pretrained("""google/mobilebert-uncased"""    )
      __SCREAMING_SNAKE_CASE      							=       tf.constant([[0, 1, 2, 3, 4, 5]]    )
      __SCREAMING_SNAKE_CASE      							=       model(UpperCamelCase__    )[0]

      __SCREAMING_SNAKE_CASE      							=       [1, 6, 3_0522]
      self.assertEqual(output.shape				,UpperCamelCase__    )

      __SCREAMING_SNAKE_CASE      							=       tf.constant(
          [
              [
                  [-4.5_919_547, -9.248_295, -9.645_256],
                  [-6.7_306_175, -6.440_284, -6.6_052_837],
                  [-7.2_743_506, -6.7_847_915, -6.024_673],
              ]
          ]    )
      tf.debugging.assert_near(output[:, :3, :3]				,UpperCamelCase__				,atol=1E-4    )












'''simple docstring'''

from .dependency_versions_table import deps
from .utils.versions import require_version, require_version_core


# define which module versions we always want to check at run time
# (usually the ones defined in `install_requires` in setup.py)
#
# order specific notes:
# - tqdm must be checked before tokenizers

a        =						[
    "python",
    "tqdm",
    "regex",
    "requests",
    "packaging",
    "filelock",
    "numpy",
    "tokenizers",
    "huggingface-hub",
    "safetensors",
    "accelerate",
    "pyyaml",
]

for pkg in pkgs_to_check_at_runtime:
    if pkg in deps:
        if pkg == "tokenizers":
            # must be loaded here, or else tqdm check may fail
            from .utils import is_tokenizers_available

            if not is_tokenizers_available():
                continue  # not required, check version only if installed
        elif pkg == "accelerate":
            # must be loaded here, or else tqdm check may fail
            from .utils import is_accelerate_available

            # Maybe switch to is_torch_available in the future here so that Accelerate is hard dep of
            # Transformers with PyTorch
            if not is_accelerate_available():
                continue  # not required, check version only if installed

        require_version_core(deps[pkg])
    else:
        raise ValueError(F'''can\'t find {pkg} in {deps.keys()}, check dependency_versions_table.py''')

def 						__magic_name__       (							__UpperCAmelCase							,	__UpperCAmelCase=None      )						->					Optional[Any]:

   '''simple docstring'''







   require_version(deps[pkg]							,	__UpperCAmelCase      )





