
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	(	_lowerCAmelCase , _lowerCAmelCase , unittest.TestCase      ):



      """simple docstring"""







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




      def 		snake_case_			(      self		:       Dict				,       _snake_case		:       str				,       _snake_case		:       List[Any]				,       _snake_case		:       List[str]=False				):
          __lowercase		:			int	    =       super()._prepare_for_class(_lowerCAmelCase				,       _lowerCAmelCase				,       return_labels=_lowerCAmelCase				)

          if return_labels:
              if model_class in get_values(_lowerCAmelCase				):
                  __lowercase		:			Any	    =       tf.zeros(self.model_tester.batch_size				,       dtype=tf.intaa				)

          return inputs_dict


      class 						__lowerCAmelCase	(	_lowerCAmelCase      ):



            """simple docstring"""







            def __init__(      self		:       int				,       _snake_case		:       Union[str, Any]				,       _snake_case		:       Tuple=13				,       _snake_case		:       Dict=7				,       _snake_case		:       Optional[int]=True				,       _snake_case		:       List[str]=True				,       _snake_case		:       Dict=True				,       _snake_case		:       Tuple=True				,       _snake_case		:       int=99				,       _snake_case		:       Optional[int]=32				,       _snake_case		:       Tuple=32				,       _snake_case		:       Union[str, Any]=2				,       _snake_case		:       Optional[int]=4				,       _snake_case		:       int=37				,       _snake_case		:       List[Any]="gelu"				,       _snake_case		:       Any=0.1				,       _snake_case		:       Union[str, Any]=0.1				,       _snake_case		:       Optional[int]=512				,       _snake_case		:       List[str]=16				,       _snake_case		:       Any=2				,       _snake_case		:       List[str]=0.02				,       _snake_case		:       Dict=3				,       _snake_case		:       int=4				,       _snake_case		:       str=None				,       ):
                __lowercase		:			Optional[Any]	    =       parent
                __lowercase		:			Optional[Any]	    =       batch_size
                __lowercase		:			List[Any]	    =       seq_length
                __lowercase		:			Optional[int]	    =       is_training
                __lowercase		:			Dict	    =       use_input_mask
                __lowercase		:			Union[str, Any]	    =       use_token_type_ids
                __lowercase		:			Any	    =       use_labels
                __lowercase		:			int	    =       vocab_size
                __lowercase		:			int	    =       hidden_size
                __lowercase		:			str	    =       num_hidden_layers
                __lowercase		:			int	    =       num_attention_heads
                __lowercase		:			str	    =       intermediate_size
                __lowercase		:			Union[str, Any]	    =       hidden_act
                __lowercase		:			Optional[int]	    =       hidden_dropout_prob
                __lowercase		:			Tuple	    =       attention_probs_dropout_prob
                __lowercase		:			Tuple	    =       max_position_embeddings
                __lowercase		:			List[str]	    =       type_vocab_size
                __lowercase		:			Optional[Any]	    =       type_sequence_label_size
                __lowercase		:			Union[str, Any]	    =       initializer_range
                __lowercase		:			Tuple	    =       num_labels
                __lowercase		:			List[Any]	    =       num_choices
                __lowercase		:			Optional[Any]	    =       scope
                __lowercase		:			Optional[Any]	    =       embedding_size




            def 		snake_case_			(      self		:       Tuple				):
                __lowercase		:			Dict	    =       ids_tensor([self.batch_size, self.seq_length]				,       self.vocab_size				)

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

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

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

                __lowercase		:			Optional[int]	    =       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 		snake_case_			(      self		:       Optional[int]				,       _snake_case		:       Optional[Any]				,       _snake_case		:       int				,       _snake_case		:       int				,       _snake_case		:       Any				,       _snake_case		:       Tuple				,       _snake_case		:       List[str]				,       _snake_case		:       Dict				):
                __lowercase		:			Union[str, Any]	    =       TFMobileBertModel(config=_lowerCAmelCase				)
                __lowercase		:			List[Any]	    =       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
                __lowercase		:			Optional[int]	    =       model(_lowerCAmelCase				)

                __lowercase		:			List[Any]	    =       [input_ids, input_mask]
                __lowercase		:			List[str]	    =       model(_lowerCAmelCase				)

                __lowercase		:			List[Any]	    =       model(_lowerCAmelCase				)

                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		:       Dict				,       _snake_case		:       Optional[int]				,       _snake_case		:       List[Any]				,       _snake_case		:       Optional[int]				,       _snake_case		:       Optional[Any]				,       _snake_case		:       List[str]				,       _snake_case		:       Tuple				,       _snake_case		:       str				):
                __lowercase		:			str	    =       TFMobileBertForMaskedLM(config=_lowerCAmelCase				)
                __lowercase		:			int	    =       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
                __lowercase		:			int	    =       model(_lowerCAmelCase				)
                self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.seq_length, self.vocab_size)				)




            def 		snake_case_			(      self		:       Union[str, Any]				,       _snake_case		:       str				,       _snake_case		:       Any				,       _snake_case		:       str				,       _snake_case		:       Optional[Any]				,       _snake_case		:       int				,       _snake_case		:       List[Any]				,       _snake_case		:       Optional[int]				):
                __lowercase		:			str	    =       TFMobileBertForNextSentencePrediction(config=_lowerCAmelCase				)
                __lowercase		:			List[Any]	    =       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
                __lowercase		:			Optional[Any]	    =       model(_lowerCAmelCase				)
                self.parent.assertEqual(result.logits.shape				,       (self.batch_size, 2)				)




            def 		snake_case_			(      self		:       List[str]				,       _snake_case		:       Tuple				,       _snake_case		:       Optional[Any]				,       _snake_case		:       List[Any]				,       _snake_case		:       str				,       _snake_case		:       Dict				,       _snake_case		:       int				,       _snake_case		:       Dict				):
                __lowercase		:			str	    =       TFMobileBertForPreTraining(config=_lowerCAmelCase				)
                __lowercase		:			str	    =       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
                __lowercase		:			Union[str, Any]	    =       model(_lowerCAmelCase				)
                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		:       Optional[Any]				,       _snake_case		:       List[Any]				,       _snake_case		:       Optional[Any]				,       _snake_case		:       List[str]				,       _snake_case		:       List[Any]				,       _snake_case		:       Union[str, Any]				,       _snake_case		:       Union[str, Any]				,       _snake_case		:       Optional[Any]				):
                __lowercase		:			List[Any]	    =       self.num_labels
                __lowercase		:			Any	    =       TFMobileBertForSequenceClassification(config=_lowerCAmelCase				)
                __lowercase		:			Union[str, Any]	    =       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
                __lowercase		:			List[Any]	    =       model(_lowerCAmelCase				)
                self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.num_labels)				)




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




            def 		snake_case_			(      self		:       Dict				,       _snake_case		:       Dict				,       _snake_case		:       Optional[int]				,       _snake_case		:       str				,       _snake_case		:       Tuple				,       _snake_case		:       Dict				,       _snake_case		:       Optional[Any]				,       _snake_case		:       Optional[int]				):
                __lowercase		:			List[Any]	    =       self.num_labels
                __lowercase		:			List[Any]	    =       TFMobileBertForTokenClassification(config=_lowerCAmelCase				)
                __lowercase		:			int	    =       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
                __lowercase		:			Optional[int]	    =       model(_lowerCAmelCase				)
                self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.seq_length, self.num_labels)				)




            def 		snake_case_			(      self		:       int				,       _snake_case		:       Union[str, Any]				,       _snake_case		:       str				,       _snake_case		:       str				,       _snake_case		:       List[str]				,       _snake_case		:       str				,       _snake_case		:       Any				,       _snake_case		:       str				):
                __lowercase		:			List[str]	    =       TFMobileBertForQuestionAnswering(config=_lowerCAmelCase				)
                __lowercase		:			Optional[Any]	    =       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
                __lowercase		:			Dict	    =       model(_lowerCAmelCase				)
                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		:       Union[str, Any]				):
                __lowercase		:			Dict	    =       self.prepare_config_and_inputs()
                (
                    (
                    __lowercase						
                )    ,      (
                    __lowercase						
                )    ,      (
                    __lowercase						
                )    ,      (
                    __lowercase						
                )    ,      (
                    __lowercase						
                )    ,      (
                    __lowercase						
                )    ,      (
                    __lowercase						
                )    ,      						
                )		:			Union[str, Any]	    =       config_and_inputs
                __lowercase		:			Dict	    =       {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
                return config, inputs_dict






      def 		snake_case_			(      self		:       List[str]				):
          __lowercase		:			Optional[Any]	    =       TFMobileBertModelTest.TFMobileBertModelTester(self				)
          __lowercase		:			str	    =       ConfigTester(self				,       config_class=_lowerCAmelCase				,       hidden_size=37				)




      def 		snake_case_			(      self		:       Any				):
          self.config_tester.run_common_tests()




      def 		snake_case_			(      self		:       Optional[int]				):
          __lowercase		:			List[str]	    =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_model(*_lowerCAmelCase				)




      def 		snake_case_			(      self		:       List[Any]				):
          __lowercase		:			int	    =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_masked_lm(*_lowerCAmelCase				)




      def 		snake_case_			(      self		:       str				):
          __lowercase		:			str	    =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_multiple_choice(*_lowerCAmelCase				)




      def 		snake_case_			(      self		:       str				):
          __lowercase		:			Optional[int]	    =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*_lowerCAmelCase				)




      def 		snake_case_			(      self		:       int				):
          __lowercase		:			Any	    =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_pretraining(*_lowerCAmelCase				)




      def 		snake_case_			(      self		:       Union[str, Any]				):
          __lowercase		:			Tuple	    =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_question_answering(*_lowerCAmelCase				)




      def 		snake_case_			(      self		:       List[Any]				):
          __lowercase		:			int	    =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_sequence_classification(*_lowerCAmelCase				)




      def 		snake_case_			(      self		:       int				):
          __lowercase		:			List[Any]	    =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_token_classification(*_lowerCAmelCase				)




      @slow
      def 		snake_case_			(      self		:       Dict				):







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






@require_tf
class 						__lowerCAmelCase	(	unittest.TestCase      ):



      """simple docstring"""







      @slow
      def 		snake_case_			(      self		:       Any				):
          __lowercase		:			List[Any]	    =       TFMobileBertForPreTraining.from_pretrained('''google/mobilebert-uncased'''				)
          __lowercase		:			Dict	    =       tf.constant([[0, 1, 2, 3, 4, 5]]				)
          __lowercase		:			Any	    =       model(_lowerCAmelCase				)[0]

          __lowercase		:			Union[str, Any]	    =       [1, 6, 3_0522]
          self.assertEqual(output.shape				,       _lowerCAmelCase				)

          __lowercase		:			Optional[Any]	    =       tf.constant(
              [
                  [
                      [-4.5_91_95_47, -9.24_82_95, -9.64_52_56],
                      [-6.7_30_61_75, -6.44_02_84, -6.6_05_28_37],
                      [-7.2_74_35_06, -6.7_84_79_15, -6.02_46_73],
                  ]
              ]				)
          tf.debugging.assert_near(output[:, :3, :3]				,       _lowerCAmelCase				,       atol=1E-4				)




from __future__ import annotations


def     UpperCAmelCase_  (						__lowerCAmelCase	) ->						int:
    if not nums:
        return 0
    __lowercase		:			List[Any]	    =       nums[0]
    __lowercase		:			Union[str, Any]	    =       0
    for num in nums[1:]:
        __lowercase    ,      __lowercase		:			Dict	    =       (
            max_excluding + num,
            max(__lowerCAmelCase	,					__lowerCAmelCase	),
        )
    return max(__lowerCAmelCase	,					__lowerCAmelCase	)


if __name__ == "__main__":
 import doctest

 doctest.testmod()


