



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     ):
     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 		snake_case_		(				self ,						a__ ,						a__ ,						a__=False):
         A__		 =					super()._prepare_for_class(a__ ,						a__ ,						return_labels=a__)

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

         return inputs_dict



     class     _UpperCAmelCase			(       A__     ):




          def __init__(				self ,						a__ ,						a__=1_3 ,						a__=7 ,						a__=True ,						a__=True ,						a__=True ,						a__=True ,						a__=9_9 ,						a__=3_2 ,						a__=3_2 ,						a__=2 ,						a__=4 ,						a__=3_7 ,						a__="gelu" ,						a__=0.1 ,						a__=0.1 ,						a__=5_1_2 ,						a__=1_6 ,						a__=2 ,						a__=0.0_2 ,						a__=3 ,						a__=4 ,						a__=None ,						):
              A__		 =					parent
              A__		 =					batch_size
              A__		 =					seq_length
              A__		 =					is_training
              A__		 =					use_input_mask
              A__		 =					use_token_type_ids
              A__		 =					use_labels
              A__		 =					vocab_size
              A__		 =					hidden_size
              A__		 =					num_hidden_layers
              A__		 =					num_attention_heads
              A__		 =					intermediate_size
              A__		 =					hidden_act
              A__		 =					hidden_dropout_prob
              A__		 =					attention_probs_dropout_prob
              A__		 =					max_position_embeddings
              A__		 =					type_vocab_size
              A__		 =					type_sequence_label_size
              A__		 =					initializer_range
              A__		 =					num_labels
              A__		 =					num_choices
              A__		 =					scope
              A__		 =					embedding_size




          def 		snake_case_		(				self):
              A__		 =					ids_tensor([self.batch_size, self.seq_length] ,						self.vocab_size)

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

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

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

              A__		 =					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 ,						a__ ,						a__ ,						a__ ,						a__ ,						a__ ,						a__ ,						a__):
              A__		 =					TFMobileBertModel(config=a__)
              A__		 =					{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
              A__		 =					model(a__)

              A__		 =					[input_ids, input_mask]
              A__		 =					model(a__)

              A__		 =					model(a__)

              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 ,						a__ ,						a__ ,						a__ ,						a__ ,						a__ ,						a__ ,						a__):
              A__		 =					TFMobileBertForMaskedLM(config=a__)
              A__		 =					{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
              A__		 =					model(a__)
              self.parent.assertEqual(result.logits.shape ,						(self.batch_size, self.seq_length, self.vocab_size))




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




          def 		snake_case_		(				self ,						a__ ,						a__ ,						a__ ,						a__ ,						a__ ,						a__ ,						a__):
              A__		 =					TFMobileBertForPreTraining(config=a__)
              A__		 =					{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
              A__		 =					model(a__)
              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 ,						a__ ,						a__ ,						a__ ,						a__ ,						a__ ,						a__ ,						a__):
              A__		 =					self.num_labels
              A__		 =					TFMobileBertForSequenceClassification(config=a__)
              A__		 =					{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
              A__		 =					model(a__)
              self.parent.assertEqual(result.logits.shape ,						(self.batch_size, self.num_labels))




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




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




          def 		snake_case_		(				self ,						a__ ,						a__ ,						a__ ,						a__ ,						a__ ,						a__ ,						a__):
              A__		 =					TFMobileBertForQuestionAnswering(config=a__)
              A__		 =					{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
              A__		 =					model(a__)
              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):
              A__		 =					self.prepare_config_and_inputs()
              (
                  (
                  A__
              ) ,	(
                  A__
              ) ,	(
                  A__
              ) ,	(
                  A__
              ) ,	(
                  A__
              ) ,	(
                  A__
              ) ,	(
                  A__
              ) ,	
              )		 =					config_and_inputs
              A__		 =					{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
              return config, inputs_dict







     def 		snake_case_		(				self):
         A__		 =					TFMobileBertModelTest.TFMobileBertModelTester(self)
         A__		 =					ConfigTester(self ,						config_class=a__ ,						hidden_size=3_7)




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




     def 		snake_case_		(				self):
         A__		 =					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_model(*a__)




     def 		snake_case_		(				self):
         A__		 =					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_masked_lm(*a__)




     def 		snake_case_		(				self):
         A__		 =					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_multiple_choice(*a__)




     def 		snake_case_		(				self):
         A__		 =					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*a__)




     def 		snake_case_		(				self):
         A__		 =					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_pretraining(*a__)




     def 		snake_case_		(				self):
         A__		 =					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_question_answering(*a__)




     def 		snake_case_		(				self):
         A__		 =					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_sequence_classification(*a__)




     def 		snake_case_		(				self):
         A__		 =					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_token_classification(*a__)




     @slow
     def 		snake_case_		(				self):
         # for model_name in TF_MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         for model_name in ["google/mobilebert-uncased"]:
             A__		 =					TFMobileBertModel.from_pretrained(a__)
             self.assertIsNotNone(a__)







@require_tf
class     _UpperCAmelCase			(       unittest.TestCase     ):




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

         A__		 =					[1, 6, 3_0_5_2_2]
         self.assertEqual(output.shape ,						a__)

         A__		 =					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] ,						a__ ,						atol=1e-4)





def 		lowerCAmelCase__    (					UpperCamelCase_				:       Optional[int] ,       UpperCamelCase_				:       List[str] ,       UpperCamelCase_				:       List[Any] ,       UpperCamelCase_				:       str	)->  int:
    if height >= 1:
        move_tower(height - 1 ,       UpperCamelCase_ ,       UpperCamelCase_ ,       UpperCamelCase_	)
        move_disk(UpperCamelCase_ ,       UpperCamelCase_	)
        move_tower(height - 1 ,       UpperCamelCase_ ,       UpperCamelCase_ ,       UpperCamelCase_	)



def 		lowerCAmelCase__    (					UpperCamelCase_				:       Optional[int] ,       UpperCamelCase_				:       List[Any]	)->  str:
    print('''moving disk from''' ,       UpperCamelCase_ ,       '''to''' ,       UpperCamelCase_	)



def 		lowerCAmelCase__    (					)->  Tuple:
    A__		 =					int(input('''Height of hanoi: '''	).strip()	)
    move_tower(UpperCamelCase_ ,       '''A''' ,       '''B''' ,       '''C'''	)


if __name__ == "__main__":
   main()
