





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




   """simple docstring"""



   __A       :    List[Any]	  =  (
       (
           TFMobileBertModel,
           TFMobileBertForMaskedLM,
           TFMobileBertForNextSentencePrediction,
           TFMobileBertForPreTraining,
           TFMobileBertForQuestionAnswering,
           TFMobileBertForSequenceClassification,
           TFMobileBertForTokenClassification,
           TFMobileBertForMultipleChoice,
       )
       if is_tf_available()
       else ()
   )
   __A       :    Optional[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       :    List[str]	  =  False
   __A       :    Optional[int]	  =  False

   def 							__lowercase		( self ,  lowercase ,  lowercase ,  lowercase=False)      ->						Optional[int]:







          '''simple docstring'''

          a__						:     Any			=    super()._prepare_for_class(lowercase ,  lowercase ,  return_labels=lowercase)

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

          return inputs_dict





   class 			A__   (							__UpperCAmelCase     ):




      """simple docstring"""



      def __init__( self ,  lowercase ,  lowercase=13 ,  lowercase=7 ,  lowercase=True ,  lowercase=True ,  lowercase=True ,  lowercase=True ,  lowercase=99 ,  lowercase=32 ,  lowercase=32 ,  lowercase=2 ,  lowercase=4 ,  lowercase=37 ,  lowercase="gelu" ,  lowercase=0.1 ,  lowercase=0.1 ,  lowercase=512 ,  lowercase=16 ,  lowercase=2 ,  lowercase=0.02 ,  lowercase=3 ,  lowercase=4 ,  lowercase=None ,  )      ->						List[Any]:







             '''simple docstring'''

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

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







             '''simple docstring'''

             a__						:     Union[str, Any]			=    ids_tensor([self.batch_size, self.seq_length] ,  self.vocab_size)

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

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

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

             a__						:     Any			=    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 							__lowercase		( self ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase)      ->						int:







             '''simple docstring'''

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

             a__						:     Optional[Any]			=    [input_ids, input_mask]
             a__						:     Dict			=    model(lowercase)

             a__						:     str			=    model(lowercase)

             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 							__lowercase		( self ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase)      ->						Any:







             '''simple docstring'''

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

      def 							__lowercase		( self ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase)      ->						Dict:







             '''simple docstring'''

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

      def 							__lowercase		( self ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase)      ->						Tuple:







             '''simple docstring'''

             a__						:     str			=    TFMobileBertForPreTraining(config=lowercase)
             a__						:     List[str]			=    {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
             a__						:     int			=    model(lowercase)
             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 							__lowercase		( self ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase)      ->						int:







             '''simple docstring'''

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

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







             '''simple docstring'''

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

      def 							__lowercase		( self ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase)      ->						Dict:







             '''simple docstring'''

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

      def 							__lowercase		( self ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase)      ->						Tuple:







             '''simple docstring'''

             a__						:     List[Any]			=    TFMobileBertForQuestionAnswering(config=lowercase)
             a__						:     Any			=    {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
             a__						:     List[str]			=    model(lowercase)
             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 							__lowercase		( self)      ->						List[str]:







             '''simple docstring'''

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

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







          '''simple docstring'''

          a__						:     Dict			=    TFMobileBertModelTest.TFMobileBertModelTester(self)
          a__						:     Tuple			=    ConfigTester(self ,  config_class=lowercase ,  hidden_size=37)

   def 							__lowercase		( self)      ->						Any:







          '''simple docstring'''

          self.config_tester.run_common_tests()

   def 							__lowercase		( self)      ->						Tuple:







          '''simple docstring'''

          a__						:     Optional[Any]			=    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_model(*lowercase)

   def 							__lowercase		( self)      ->						str:







          '''simple docstring'''

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

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







          '''simple docstring'''

          a__						:     Any			=    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_multiple_choice(*lowercase)

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







          '''simple docstring'''

          a__						:     Tuple			=    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*lowercase)

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







          '''simple docstring'''

          a__						:     Optional[int]			=    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_pretraining(*lowercase)

   def 							__lowercase		( self)      ->						Any:







          '''simple docstring'''

          a__						:     int			=    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_question_answering(*lowercase)

   def 							__lowercase		( self)      ->						Any:







          '''simple docstring'''

          a__						:     Dict			=    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_sequence_classification(*lowercase)

   def 							__lowercase		( self)      ->						int:







          '''simple docstring'''

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

   @slow
   def 							__lowercase		( self)      ->						Dict:







          '''simple docstring'''

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





@require_tf
class 			A__   (							unittest.TestCase     ):




   """simple docstring"""



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







          '''simple docstring'''

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

          a__						:     List[str]			=    [1, 6, 3_0522]
          self.assertEqual(output.shape ,  lowercase)

          a__						:     List[str]			=    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] ,  lowercase ,  atol=1e-4)









def A_     (		A__		,				A__		,				A__    )  ->		float:
       if principal <= 0:
              raise Exception('Principal borrowed must be > 0'    )
       if rate_per_annum < 0:
              raise Exception('Rate of interest must be >= 0'    )
       if years_to_repay <= 0 or not isinstance(A__		,				A__    ):
              raise Exception('Years to repay must be an integer > 0'    )

       # Yearly rate is divided by 12 to get monthly rate
       a__						:     str			=    rate_per_annum / 12

       # Years to repay is multiplied by 12 to get number of payments as payment is monthly
       a__						:     List[Any]			=    years_to_repay * 12

       return (
           principal
           * rate_per_month
           * (1 + rate_per_month) ** number_of_payments
           / ((1 + rate_per_month) ** number_of_payments - 1)
       )


if __name__ == "__main__":
    import doctest

    doctest.testmod()


