






'''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 UpperCAmelCase_       (     __A					,							__A					,							unittest.TestCase					):




      """simple docstring"""




      UpperCamelCase_             =      (
          (
              TFMobileBertModel,
              TFMobileBertForMaskedLM,
              TFMobileBertForNextSentencePrediction,
              TFMobileBertForPreTraining,
              TFMobileBertForQuestionAnswering,
              TFMobileBertForSequenceClassification,
              TFMobileBertForTokenClassification,
              TFMobileBertForMultipleChoice,
          )
          if is_tf_available()
          else ()
      )
      UpperCamelCase_             =      (
          {
              '''feature-extraction''': TFMobileBertModel,
              '''fill-mask''': TFMobileBertForMaskedLM,
              '''question-answering''': TFMobileBertForQuestionAnswering,
              '''text-classification''': TFMobileBertForSequenceClassification,
              '''token-classification''': TFMobileBertForTokenClassification,
              '''zero-shot''': TFMobileBertForSequenceClassification,
          }
          if is_tf_available()
          else {}
      )
      UpperCamelCase_             =      False
      UpperCamelCase_             =      False






      def 					A__   (			self      :		List[str]  ,   UpperCAmelCase      :		Optional[Any]  ,   UpperCAmelCase      :		List[str]  ,   UpperCAmelCase      :		Union[str, Any]=False		)	->	Union[str, Any]:


       '''simple docstring'''

       lowercase       : Dict						=super()._prepare_for_class(UpperCAmelCase  ,   UpperCAmelCase  ,   return_labels=UpperCAmelCase		)

       if return_labels:
        if model_class in get_values(UpperCAmelCase		):
         lowercase       : Union[str, Any]						=tf.zeros(self.model_tester.batch_size  ,   dtype=tf.intaa		)

       return inputs_dict
      class UpperCAmelCase_       (     __A					):




            """simple docstring"""




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


             '''simple docstring'''

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






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


             '''simple docstring'''

             lowercase       : Any						=ids_tensor([self.batch_size, self.seq_length]  ,   self.vocab_size		)

             lowercase       : int						=None
             if self.use_input_mask:
              lowercase       : Optional[int]						=random_attention_mask([self.batch_size, self.seq_length]		)

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

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

             lowercase       : Dict						=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 					A__   (			self      :		Any  ,   UpperCAmelCase      :		Union[str, Any]  ,   UpperCAmelCase      :		int  ,   UpperCAmelCase      :		Tuple  ,   UpperCAmelCase      :		Optional[int]  ,   UpperCAmelCase      :		List[Any]  ,   UpperCAmelCase      :		List[str]  ,   UpperCAmelCase      :		List[str]		)	->	List[Any]:


             '''simple docstring'''

             lowercase       : int						=TFMobileBertModel(config=UpperCAmelCase		)
             lowercase       : Optional[int]						={'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
             lowercase       : List[Any]						=model(UpperCAmelCase		)

             lowercase       : Optional[Any]						=[input_ids, input_mask]
             lowercase       : Union[str, Any]						=model(UpperCAmelCase		)

             lowercase       : List[Any]						=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 					A__   (			self      :		int  ,   UpperCAmelCase      :		Optional[Any]  ,   UpperCAmelCase      :		Any  ,   UpperCAmelCase      :		List[str]  ,   UpperCAmelCase      :		Tuple  ,   UpperCAmelCase      :		int  ,   UpperCAmelCase      :		str  ,   UpperCAmelCase      :		Dict		)	->	Dict:


             '''simple docstring'''

             lowercase       : Optional[Any]						=TFMobileBertForMaskedLM(config=UpperCAmelCase		)
             lowercase       : Union[str, Any]						={'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
             lowercase       : Any						=model(UpperCAmelCase		)
             self.parent.assertEqual(result.logits.shape  ,   (self.batch_size, self.seq_length, self.vocab_size)		)






            def 					A__   (			self      :		Optional[int]  ,   UpperCAmelCase      :		Dict  ,   UpperCAmelCase      :		int  ,   UpperCAmelCase      :		str  ,   UpperCAmelCase      :		str  ,   UpperCAmelCase      :		int  ,   UpperCAmelCase      :		Optional[int]  ,   UpperCAmelCase      :		Any		)	->	Optional[int]:


             '''simple docstring'''

             lowercase       : Dict						=TFMobileBertForNextSentencePrediction(config=UpperCAmelCase		)
             lowercase       : Union[str, Any]						={'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
             lowercase       : Optional[int]						=model(UpperCAmelCase		)
             self.parent.assertEqual(result.logits.shape  ,   (self.batch_size, 2)		)






            def 					A__   (			self      :		Tuple  ,   UpperCAmelCase      :		int  ,   UpperCAmelCase      :		Dict  ,   UpperCAmelCase      :		List[str]  ,   UpperCAmelCase      :		List[Any]  ,   UpperCAmelCase      :		List[str]  ,   UpperCAmelCase      :		Any  ,   UpperCAmelCase      :		Optional[Any]		)	->	Dict:


             '''simple docstring'''

             lowercase       : Dict						=TFMobileBertForPreTraining(config=UpperCAmelCase		)
             lowercase       : Dict						={'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
             lowercase       : List[str]						=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 					A__   (			self      :		Dict  ,   UpperCAmelCase      :		int  ,   UpperCAmelCase      :		Union[str, Any]  ,   UpperCAmelCase      :		Dict  ,   UpperCAmelCase      :		Union[str, Any]  ,   UpperCAmelCase      :		str  ,   UpperCAmelCase      :		Optional[int]  ,   UpperCAmelCase      :		Any		)	->	str:


             '''simple docstring'''

             lowercase       : List[Any]						=self.num_labels
             lowercase       : Tuple						=TFMobileBertForSequenceClassification(config=UpperCAmelCase		)
             lowercase       : int						={'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
             lowercase       : int						=model(UpperCAmelCase		)
             self.parent.assertEqual(result.logits.shape  ,   (self.batch_size, self.num_labels)		)






            def 					A__   (			self      :		Union[str, Any]  ,   UpperCAmelCase      :		int  ,   UpperCAmelCase      :		Dict  ,   UpperCAmelCase      :		Optional[Any]  ,   UpperCAmelCase      :		Any  ,   UpperCAmelCase      :		Optional[int]  ,   UpperCAmelCase      :		List[str]  ,   UpperCAmelCase      :		Optional[Any]		)	->	List[str]:


             '''simple docstring'''

             lowercase       : List[Any]						=self.num_choices
             lowercase       : Tuple						=TFMobileBertForMultipleChoice(config=UpperCAmelCase		)
             lowercase       : Union[str, Any]						=tf.tile(tf.expand_dims(UpperCAmelCase  ,   1		)  ,   (1, self.num_choices, 1)		)
             lowercase       : Optional[int]						=tf.tile(tf.expand_dims(UpperCAmelCase  ,   1		)  ,   (1, self.num_choices, 1)		)
             lowercase       : Dict						=tf.tile(tf.expand_dims(UpperCAmelCase  ,   1		)  ,   (1, self.num_choices, 1)		)
             lowercase       : Optional[int]						={
                 '''input_ids''': multiple_choice_inputs_ids,
                 '''attention_mask''': multiple_choice_input_mask,
                 '''token_type_ids''': multiple_choice_token_type_ids,
             }
             lowercase       : Tuple						=model(UpperCAmelCase		)
             self.parent.assertEqual(result.logits.shape  ,   (self.batch_size, self.num_choices)		)






            def 					A__   (			self      :		List[str]  ,   UpperCAmelCase      :		Any  ,   UpperCAmelCase      :		List[Any]  ,   UpperCAmelCase      :		Optional[int]  ,   UpperCAmelCase      :		Tuple  ,   UpperCAmelCase      :		int  ,   UpperCAmelCase      :		List[Any]  ,   UpperCAmelCase      :		Optional[int]		)	->	int:


             '''simple docstring'''

             lowercase       : Dict						=self.num_labels
             lowercase       : List[str]						=TFMobileBertForTokenClassification(config=UpperCAmelCase		)
             lowercase       : int						={'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
             lowercase       : int						=model(UpperCAmelCase		)
             self.parent.assertEqual(result.logits.shape  ,   (self.batch_size, self.seq_length, self.num_labels)		)






            def 					A__   (			self      :		Tuple  ,   UpperCAmelCase      :		Dict  ,   UpperCAmelCase      :		Tuple  ,   UpperCAmelCase      :		Any  ,   UpperCAmelCase      :		List[str]  ,   UpperCAmelCase      :		int  ,   UpperCAmelCase      :		Dict  ,   UpperCAmelCase      :		List[str]		)	->	List[Any]:


             '''simple docstring'''

             lowercase       : Optional[Any]						=TFMobileBertForQuestionAnswering(config=UpperCAmelCase		)
             lowercase       : int						={'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
             lowercase       : Tuple						=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 					A__   (			self      :		List[Any]		)	->	List[str]:


             '''simple docstring'''

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






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


       '''simple docstring'''

       lowercase       : str						=TFMobileBertModelTest.TFMobileBertModelTester(self		)
       lowercase       : Any						=ConfigTester(self  ,   config_class=UpperCAmelCase  ,   hidden_size=37		)






      def 					A__   (			self      :		List[str]		)	->	int:


       '''simple docstring'''

       self.config_tester.run_common_tests()






      def 					A__   (			self      :		Tuple		)	->	str:


       '''simple docstring'''

       lowercase       : Union[str, Any]						=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_model(*UpperCAmelCase		)






      def 					A__   (			self      :		Tuple		)	->	int:


       '''simple docstring'''

       lowercase       : int						=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_masked_lm(*UpperCAmelCase		)






      def 					A__   (			self      :		str		)	->	Tuple:


       '''simple docstring'''

       lowercase       : str						=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_multiple_choice(*UpperCAmelCase		)






      def 					A__   (			self      :		int		)	->	int:


       '''simple docstring'''

       lowercase       : Optional[int]						=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*UpperCAmelCase		)






      def 					A__   (			self      :		Any		)	->	int:


       '''simple docstring'''

       lowercase       : Tuple						=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_pretraining(*UpperCAmelCase		)






      def 					A__   (			self      :		Dict		)	->	int:


       '''simple docstring'''

       lowercase       : Optional[Any]						=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_question_answering(*UpperCAmelCase		)






      def 					A__   (			self      :		Optional[int]		)	->	Dict:


       '''simple docstring'''

       lowercase       : Optional[int]						=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_sequence_classification(*UpperCAmelCase		)






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


       '''simple docstring'''

       lowercase       : Optional[Any]						=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_token_classification(*UpperCAmelCase		)






      @slow
      def 					A__   (			self      :		Optional[Any]		)	->	Optional[int]:


       '''simple docstring'''

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





@require_tf
class UpperCAmelCase_       (     unittest.TestCase					):




      """simple docstring"""




      @slow
      def 					A__   (			self      :		Any		)	->	Dict:


       '''simple docstring'''

       lowercase       : Any						=TFMobileBertForPreTraining.from_pretrained('''google/mobilebert-uncased'''		)
       lowercase       : Optional[Any]						=tf.constant([[0, 1, 2, 3, 4, 5]]		)
       lowercase       : Dict						=model(UpperCAmelCase		)[0]

       lowercase       : Optional[int]						=[1, 6, 3_0522]
       self.assertEqual(output.shape  ,   UpperCAmelCase		)

       lowercase       : Dict						=tf.constant(
           [
               [
                   [-4.5_9_1_9_5_4_7, -9.2_4_8_2_9_5, -9.6_4_5_2_5_6],
                   [-6.7_3_0_6_1_7_5, -6.4_4_0_2_8_4, -6.6_0_5_2_8_3_7],
                   [-7.2_7_4_3_5_0_6, -6.7_8_4_7_9_1_5, -6.0_2_4_6_7_3],
               ]
           ]		)
       tf.debugging.assert_near(output[:, :3, :3]  ,   UpperCAmelCase  ,   atol=1e-4		)











'''simple docstring'''



import argparse
from collections import defaultdict


def 				lowercase_					(     __A			:							Optional[Any]       ,   __A			:							Optional[int]       ,   __A			:							str       ,   __A			:							Tuple       ,   __A			:							int      )							->   Dict:



 """simple docstring"""




 lowercase       : int						=F'{file}_{class_name}_{test_name}'
 done_test[_id] += 1

 with open(__A       ,   '''r'''      ) as f:
  lowercase       : List[Any]						=f.readlines()

 lowercase       : Tuple						=F'class {class_name}('
 lowercase       : Optional[Any]						=F'{4 * " "}def {test_name}('
 lowercase       : Union[str, Any]						=F'{8 * " "}{correct_line.split()[0]}'
 lowercase       : List[Any]						=F'{1_6 * " "}{correct_line.split()[0]}'
 lowercase       : int						=False
 lowercase       : Dict						=False
 lowercase       : Optional[Any]						=False
 lowercase       : str						=False
 lowercase       : str						=0
 lowercase       : Optional[Any]						=0

 lowercase       : List[Any]						=[]
 for line in lines:
  if line.startswith(__A      ):
   lowercase       : List[str]						=True
  elif in_class and line.startswith(__A      ):
   lowercase       : int						=True
  elif in_class and in_func and (line.startswith(__A      ) or line.startswith(__A      )):
   lowercase       : str						=len(line.split(correct_line.split()[0]      )[0]      )
   count += 1

   if count == done_test[_id]:
    lowercase       : Dict						=True

  if in_class and in_func and in_line:
   if ")" not in line:
    continue
   else:
    lowercase       : Any						=True

  if in_class and in_func and in_line and insert_line:
   new_lines.append(F'{spaces * " "}{correct_line}'      )
   lowercase       : List[str]						=False
  else:
   new_lines.append(__A      )

 with open(__A       ,   '''w'''      ) as f:
  for line in new_lines:
   f.write(__A      )


def 				lowercase_					(     __A			:							Any       ,   __A			:							List[Any]=None      )							->   str:



 """simple docstring"""




 if fail is not None:
  with open(__A       ,   '''r'''      ) as f:
   lowercase       : Optional[Any]						={l.strip() for l in f.readlines()}
 else:
  lowercase       : int						=None

 with open(__A       ,   '''r'''      ) as f:
  lowercase       : Union[str, Any]						=f.readlines()

 lowercase       : Union[str, Any]						=defaultdict(__A      )
 for line in correct_lines:
  lowercase	,		lowercase	,		lowercase	,		lowercase       : Optional[int]						=line.split(''';'''      )
  if test_failures is None or "::".join([file, class_name, test_name]      ) in test_failures:
   overwrite_file(__A       ,   __A       ,   __A       ,   __A       ,   __A      )


if __name__ == "__main__":
      SCREAMING_SNAKE_CASE							=						argparse.ArgumentParser()
      parser.add_argument('--correct_filename', help='filename of tests with expected result')
      parser.add_argument('--fail_filename', help='filename of test failures', type=str, default=None)
      SCREAMING_SNAKE_CASE							=						parser.parse_args()

      main(args.correct_filename, args.fail_filename)



