

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



 """simple docstring"""



 lowerCAmelCase__  					=      (
     (
         TFMobileBertModel,
         TFMobileBertForMaskedLM,
         TFMobileBertForNextSentencePrediction,
         TFMobileBertForPreTraining,
         TFMobileBertForQuestionAnswering,
         TFMobileBertForSequenceClassification,
         TFMobileBertForTokenClassification,
         TFMobileBertForMultipleChoice,
     )
     if is_tf_available()
     else ()
 )
 lowerCAmelCase__  					=      (
     {
         "feature-extraction": TFMobileBertModel,
         "fill-mask": TFMobileBertForMaskedLM,
         "question-answering": TFMobileBertForQuestionAnswering,
         "text-classification": TFMobileBertForSequenceClassification,
         "token-classification": TFMobileBertForTokenClassification,
         "zero-shot": TFMobileBertForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 lowerCAmelCase__  					=      False
 lowerCAmelCase__  					=      False
 def       UpperCAmelCase__		(		self					:       int					,							__SCREAMING_SNAKE_CASE					:       Union[str, Any]					,							__SCREAMING_SNAKE_CASE					:       Tuple					,							__SCREAMING_SNAKE_CASE					:       Tuple=False			)  -> Optional[Any]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      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			):
                __SCREAMING_SNAKE_CASE     =      tf.zeros(self.model_tester.batch_size					,							dtype=tf.intaa			)

      return inputs_dict







 class 						lowerCAmelCase__   (      a					):



  """simple docstring"""



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


       """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					:       Optional[int]			)  -> 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					:       Optional[int]					,							__SCREAMING_SNAKE_CASE					:       List[Any]					,							__SCREAMING_SNAKE_CASE					:       Optional[int]					,							__SCREAMING_SNAKE_CASE					:       Optional[int]					,							__SCREAMING_SNAKE_CASE					:       Union[str, Any]					,							__SCREAMING_SNAKE_CASE					:       Tuple					,							__SCREAMING_SNAKE_CASE					:       Tuple					,							__SCREAMING_SNAKE_CASE					:       int			)  -> Optional[int]:


       """simple docstring"""




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

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

       __SCREAMING_SNAKE_CASE     =      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       UpperCAmelCase__		(		self					:       Dict					,							__SCREAMING_SNAKE_CASE					:       str					,							__SCREAMING_SNAKE_CASE					:       Dict					,							__SCREAMING_SNAKE_CASE					:       Any					,							__SCREAMING_SNAKE_CASE					:       Dict					,							__SCREAMING_SNAKE_CASE					:       Any					,							__SCREAMING_SNAKE_CASE					:       int					,							__SCREAMING_SNAKE_CASE					:       Tuple			)  -> int:


       """simple docstring"""




       __SCREAMING_SNAKE_CASE     =      TFMobileBertForMaskedLM(config=__SCREAMING_SNAKE_CASE			)
       __SCREAMING_SNAKE_CASE     =      {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
       __SCREAMING_SNAKE_CASE     =      model(__SCREAMING_SNAKE_CASE			)
       self.parent.assertEqual(result.logits.shape					,							(self.batch_size, self.seq_length, self.vocab_size)			)
  def       UpperCAmelCase__		(		self					:       List[Any]					,							__SCREAMING_SNAKE_CASE					:       Optional[Any]					,							__SCREAMING_SNAKE_CASE					:       Tuple					,							__SCREAMING_SNAKE_CASE					:       int					,							__SCREAMING_SNAKE_CASE					:       Optional[Any]					,							__SCREAMING_SNAKE_CASE					:       str					,							__SCREAMING_SNAKE_CASE					:       List[Any]					,							__SCREAMING_SNAKE_CASE					:       Tuple			)  -> str:


       """simple docstring"""




       __SCREAMING_SNAKE_CASE     =      TFMobileBertForNextSentencePrediction(config=__SCREAMING_SNAKE_CASE			)
       __SCREAMING_SNAKE_CASE     =      {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
       __SCREAMING_SNAKE_CASE     =      model(__SCREAMING_SNAKE_CASE			)
       self.parent.assertEqual(result.logits.shape					,							(self.batch_size, 2)			)
  def       UpperCAmelCase__		(		self					:       str					,							__SCREAMING_SNAKE_CASE					:       str					,							__SCREAMING_SNAKE_CASE					:       List[str]					,							__SCREAMING_SNAKE_CASE					:       Union[str, Any]					,							__SCREAMING_SNAKE_CASE					:       List[Any]					,							__SCREAMING_SNAKE_CASE					:       Optional[Any]					,							__SCREAMING_SNAKE_CASE					:       Tuple					,							__SCREAMING_SNAKE_CASE					:       str			)  -> Dict:


       """simple docstring"""




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


       """simple docstring"""




       __SCREAMING_SNAKE_CASE     =      self.num_labels
       __SCREAMING_SNAKE_CASE     =      TFMobileBertForSequenceClassification(config=__SCREAMING_SNAKE_CASE			)
       __SCREAMING_SNAKE_CASE     =      {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
       __SCREAMING_SNAKE_CASE     =      model(__SCREAMING_SNAKE_CASE			)
       self.parent.assertEqual(result.logits.shape					,							(self.batch_size, self.num_labels)			)
  def       UpperCAmelCase__		(		self					:       Optional[Any]					,							__SCREAMING_SNAKE_CASE					:       Optional[Any]					,							__SCREAMING_SNAKE_CASE					:       int					,							__SCREAMING_SNAKE_CASE					:       int					,							__SCREAMING_SNAKE_CASE					:       Union[str, Any]					,							__SCREAMING_SNAKE_CASE					:       List[str]					,							__SCREAMING_SNAKE_CASE					:       Any					,							__SCREAMING_SNAKE_CASE					:       str			)  -> str:


       """simple docstring"""




       __SCREAMING_SNAKE_CASE     =      self.num_choices
       __SCREAMING_SNAKE_CASE     =      TFMobileBertForMultipleChoice(config=__SCREAMING_SNAKE_CASE			)
       __SCREAMING_SNAKE_CASE     =      tf.tile(tf.expand_dims(__SCREAMING_SNAKE_CASE					,							1			)					,							(1, self.num_choices, 1)			)
       __SCREAMING_SNAKE_CASE     =      tf.tile(tf.expand_dims(__SCREAMING_SNAKE_CASE					,							1			)					,							(1, self.num_choices, 1)			)
       __SCREAMING_SNAKE_CASE     =      tf.tile(tf.expand_dims(__SCREAMING_SNAKE_CASE					,							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(__SCREAMING_SNAKE_CASE			)
       self.parent.assertEqual(result.logits.shape					,							(self.batch_size, self.num_choices)			)
  def       UpperCAmelCase__		(		self					:       Optional[Any]					,							__SCREAMING_SNAKE_CASE					:       int					,							__SCREAMING_SNAKE_CASE					:       str					,							__SCREAMING_SNAKE_CASE					:       Tuple					,							__SCREAMING_SNAKE_CASE					:       Dict					,							__SCREAMING_SNAKE_CASE					:       Dict					,							__SCREAMING_SNAKE_CASE					:       List[str]					,							__SCREAMING_SNAKE_CASE					:       List[str]			)  -> Optional[Any]:


       """simple docstring"""




       __SCREAMING_SNAKE_CASE     =      self.num_labels
       __SCREAMING_SNAKE_CASE     =      TFMobileBertForTokenClassification(config=__SCREAMING_SNAKE_CASE			)
       __SCREAMING_SNAKE_CASE     =      {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
       __SCREAMING_SNAKE_CASE     =      model(__SCREAMING_SNAKE_CASE			)
       self.parent.assertEqual(result.logits.shape					,							(self.batch_size, self.seq_length, self.num_labels)			)
  def       UpperCAmelCase__		(		self					:       int					,							__SCREAMING_SNAKE_CASE					:       Union[str, Any]					,							__SCREAMING_SNAKE_CASE					:       str					,							__SCREAMING_SNAKE_CASE					:       Optional[int]					,							__SCREAMING_SNAKE_CASE					:       Optional[int]					,							__SCREAMING_SNAKE_CASE					:       Optional[Any]					,							__SCREAMING_SNAKE_CASE					:       str					,							__SCREAMING_SNAKE_CASE					:       Dict			)  -> Any:


       """simple docstring"""




       __SCREAMING_SNAKE_CASE     =      TFMobileBertForQuestionAnswering(config=__SCREAMING_SNAKE_CASE			)
       __SCREAMING_SNAKE_CASE     =      {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
       __SCREAMING_SNAKE_CASE     =      model(__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       UpperCAmelCase__		(		self					:       Union[str, Any]			)  -> Tuple:


       """simple docstring"""




       __SCREAMING_SNAKE_CASE     =      self.prepare_config_and_inputs()
       (
           (
           __SCREAMING_SNAKE_CASE
       )   ,			(
           __SCREAMING_SNAKE_CASE
       )   ,			(
           __SCREAMING_SNAKE_CASE
       )   ,			(
           __SCREAMING_SNAKE_CASE
       )   ,			(
           __SCREAMING_SNAKE_CASE
       )   ,			(
           __SCREAMING_SNAKE_CASE
       )   ,			(
           __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[str]			)  -> List[str]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      TFMobileBertModelTest.TFMobileBertModelTester(self			)
      __SCREAMING_SNAKE_CASE     =      ConfigTester(self					,							config_class=__SCREAMING_SNAKE_CASE					,							hidden_size=37			)
 def       UpperCAmelCase__		(		self					:       str			)  -> Dict:


      """simple docstring"""




      self.config_tester.run_common_tests()
 def       UpperCAmelCase__		(		self					:       Union[str, Any]			)  -> Optional[Any]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_model(*__SCREAMING_SNAKE_CASE			)
 def       UpperCAmelCase__		(		self					:       Tuple			)  -> Optional[Any]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_masked_lm(*__SCREAMING_SNAKE_CASE			)
 def       UpperCAmelCase__		(		self					:       Any			)  -> int:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_multiple_choice(*__SCREAMING_SNAKE_CASE			)
 def       UpperCAmelCase__		(		self					:       Tuple			)  -> Dict:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*__SCREAMING_SNAKE_CASE			)
 def       UpperCAmelCase__		(		self					:       Any			)  -> Optional[Any]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_pretraining(*__SCREAMING_SNAKE_CASE			)
 def       UpperCAmelCase__		(		self					:       Dict			)  -> List[str]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_question_answering(*__SCREAMING_SNAKE_CASE			)
 def       UpperCAmelCase__		(		self					:       Dict			)  -> Tuple:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_sequence_classification(*__SCREAMING_SNAKE_CASE			)
 def       UpperCAmelCase__		(		self					:       int			)  -> Any:


      """simple docstring"""




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


 @slow
 def       UpperCAmelCase__		(		self					:       str			)  -> Optional[Any]:


      """simple docstring"""




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







@require_tf
class 						lowerCAmelCase__   (      unittest.TestCase					):



 """simple docstring"""



 @slow
 def       UpperCAmelCase__		(		self					:       Optional[int]			)  -> Union[str, Any]:


      """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(__SCREAMING_SNAKE_CASE			)[0]

      __SCREAMING_SNAKE_CASE     =      [1, 6, 30_522]
      self.assertEqual(output.shape					,							__SCREAMING_SNAKE_CASE			)

      __SCREAMING_SNAKE_CASE     =      tf.constant(
          [
              [
                  [-4.5919547, -9.248295, -9.645256],
                  [-6.7306175, -6.440284, -6.6052837],
                  [-7.2743506, -6.7847915, -6.024673],
              ]
          ]			)
      tf.debugging.assert_near(output[:, :3, :3]					,							__SCREAMING_SNAKE_CASE					,							atol=1E-4			)









'''simple docstring'''



from __future__ import annotations







def    a__       (  a__      ,    a__      ,    a__						):




     """simple docstring"""


     if len(a__						) == 0:
          raise ValueError("""find_max() arg is an empty sequence"""						)
     if (
         left >= len(a__						)
         or left < -len(a__						)
         or right >= len(a__						)
         or right < -len(a__						)
     ):
          raise IndexError("""list index out of range"""						)
     if left == right:
          return nums[left]
     __SCREAMING_SNAKE_CASE     =      (left + right) >> 1  # the middle
     __SCREAMING_SNAKE_CASE     =      find_max(a__      ,    a__      ,    a__						)  # find max in range[left, mid]
     __SCREAMING_SNAKE_CASE     =      find_max(a__      ,    mid + 1      ,    a__						)  # find max in range[mid + 1, right]

     return left_max if left_max >= right_max else right_max


if __name__ == "__main__":
  import doctest

  doctest.testmod(verbose=True)






