


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   (	lowerCamelCase_			,  lowerCamelCase_			,  unittest.TestCase							):
     '''simple docstring'''






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

     def   lowerCamelCase__					(self   :						Tuple				, _UpperCAmelCase   :						Union[str, Any]				, _UpperCAmelCase   :						int				, _UpperCAmelCase   :						List[str]=False		)						->       int:




       """simple docstring"""


       lowercase__					      =		super()._prepare_for_class(__snake_case				, __snake_case				, return_labels=__snake_case		)

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

       return inputs_dict



     class 							A   (	lowerCamelCase_							):
          '''simple docstring'''






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




            """simple docstring"""


            lowercase__					      =		parent
            lowercase__					      =		batch_size
            lowercase__					      =		seq_length
            lowercase__					      =		is_training
            lowercase__					      =		use_input_mask
            lowercase__					      =		use_token_type_ids
            lowercase__					      =		use_labels
            lowercase__					      =		vocab_size
            lowercase__					      =		hidden_size
            lowercase__					      =		num_hidden_layers
            lowercase__					      =		num_attention_heads
            lowercase__					      =		intermediate_size
            lowercase__					      =		hidden_act
            lowercase__					      =		hidden_dropout_prob
            lowercase__					      =		attention_probs_dropout_prob
            lowercase__					      =		max_position_embeddings
            lowercase__					      =		type_vocab_size
            lowercase__					      =		type_sequence_label_size
            lowercase__					      =		initializer_range
            lowercase__					      =		num_labels
            lowercase__					      =		num_choices
            lowercase__					      =		scope
            lowercase__					      =		embedding_size

          def   lowerCamelCase__					(self   :						List[str]		)						->       Dict:




            """simple docstring"""


            lowercase__					      =		ids_tensor([self.batch_size, self.seq_length]				, self.vocab_size		)

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

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

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

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




            """simple docstring"""


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

            lowercase__					      =		[input_ids, input_mask]
            lowercase__					      =		model(__snake_case		)

            lowercase__					      =		model(__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   lowerCamelCase__					(self   :						int				, _UpperCAmelCase   :						Dict				, _UpperCAmelCase   :						Tuple				, _UpperCAmelCase   :						List[Any]				, _UpperCAmelCase   :						Union[str, Any]				, _UpperCAmelCase   :						Tuple				, _UpperCAmelCase   :						List[Any]				, _UpperCAmelCase   :						Optional[Any]		)						->       Dict:




            """simple docstring"""


            lowercase__					      =		TFMobileBertForMaskedLM(config=__snake_case		)
            lowercase__					      =		{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
            lowercase__					      =		model(__snake_case		)
            self.parent.assertEqual(result.logits.shape				, (self.batch_size, self.seq_length, self.vocab_size)		)

          def   lowerCamelCase__					(self   :						List[str]				, _UpperCAmelCase   :						Optional[Any]				, _UpperCAmelCase   :						Any				, _UpperCAmelCase   :						List[Any]				, _UpperCAmelCase   :						str				, _UpperCAmelCase   :						Tuple				, _UpperCAmelCase   :						List[Any]				, _UpperCAmelCase   :						Union[str, Any]		)						->       Any:




            """simple docstring"""


            lowercase__					      =		TFMobileBertForNextSentencePrediction(config=__snake_case		)
            lowercase__					      =		{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
            lowercase__					      =		model(__snake_case		)
            self.parent.assertEqual(result.logits.shape				, (self.batch_size, 2)		)

          def   lowerCamelCase__					(self   :						Union[str, Any]				, _UpperCAmelCase   :						Any				, _UpperCAmelCase   :						Optional[int]				, _UpperCAmelCase   :						Union[str, Any]				, _UpperCAmelCase   :						str				, _UpperCAmelCase   :						Union[str, Any]				, _UpperCAmelCase   :						int				, _UpperCAmelCase   :						Optional[Any]		)						->       Optional[int]:




            """simple docstring"""


            lowercase__					      =		TFMobileBertForPreTraining(config=__snake_case		)
            lowercase__					      =		{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
            lowercase__					      =		model(__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   lowerCamelCase__					(self   :						Dict				, _UpperCAmelCase   :						Tuple				, _UpperCAmelCase   :						Tuple				, _UpperCAmelCase   :						Union[str, Any]				, _UpperCAmelCase   :						List[str]				, _UpperCAmelCase   :						List[str]				, _UpperCAmelCase   :						Dict				, _UpperCAmelCase   :						int		)						->       Any:




            """simple docstring"""


            lowercase__					      =		self.num_labels
            lowercase__					      =		TFMobileBertForSequenceClassification(config=__snake_case		)
            lowercase__					      =		{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
            lowercase__					      =		model(__snake_case		)
            self.parent.assertEqual(result.logits.shape				, (self.batch_size, self.num_labels)		)

          def   lowerCamelCase__					(self   :						List[Any]				, _UpperCAmelCase   :						List[str]				, _UpperCAmelCase   :						Optional[Any]				, _UpperCAmelCase   :						Union[str, Any]				, _UpperCAmelCase   :						Tuple				, _UpperCAmelCase   :						int				, _UpperCAmelCase   :						Any				, _UpperCAmelCase   :						Any		)						->       List[Any]:




            """simple docstring"""


            lowercase__					      =		self.num_choices
            lowercase__					      =		TFMobileBertForMultipleChoice(config=__snake_case		)
            lowercase__					      =		tf.tile(tf.expand_dims(__snake_case				, 1		)				, (1, self.num_choices, 1)		)
            lowercase__					      =		tf.tile(tf.expand_dims(__snake_case				, 1		)				, (1, self.num_choices, 1)		)
            lowercase__					      =		tf.tile(tf.expand_dims(__snake_case				, 1		)				, (1, self.num_choices, 1)		)
            lowercase__					      =		{
                """input_ids""": multiple_choice_inputs_ids,
                """attention_mask""": multiple_choice_input_mask,
                """token_type_ids""": multiple_choice_token_type_ids,
            }
            lowercase__					      =		model(__snake_case		)
            self.parent.assertEqual(result.logits.shape				, (self.batch_size, self.num_choices)		)

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




            """simple docstring"""


            lowercase__					      =		self.num_labels
            lowercase__					      =		TFMobileBertForTokenClassification(config=__snake_case		)
            lowercase__					      =		{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
            lowercase__					      =		model(__snake_case		)
            self.parent.assertEqual(result.logits.shape				, (self.batch_size, self.seq_length, self.num_labels)		)

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




            """simple docstring"""


            lowercase__					      =		TFMobileBertForQuestionAnswering(config=__snake_case		)
            lowercase__					      =		{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
            lowercase__					      =		model(__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   lowerCamelCase__					(self   :						Any		)						->       Tuple:




            """simple docstring"""


            lowercase__					      =		self.prepare_config_and_inputs()
            (
                (
                lowercase__ 
            )      ,     (
                lowercase__ 
            )      ,     (
                lowercase__ 
            )      ,     (
                lowercase__ 
            )      ,     (
                lowercase__ 
            )      ,     (
                lowercase__ 
            )      ,     (
                lowercase__ 
            )      ,      
            )					      =		config_and_inputs
            lowercase__					      =		{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
            return config, inputs_dict






     def   lowerCamelCase__					(self   :						int		)						->       str:




       """simple docstring"""


       lowercase__					      =		TFMobileBertModelTest.TFMobileBertModelTester(self		)
       lowercase__					      =		ConfigTester(self				, config_class=__snake_case				, hidden_size=37		)

     def   lowerCamelCase__					(self   :						str		)						->       Any:




       """simple docstring"""


       self.config_tester.run_common_tests()

     def   lowerCamelCase__					(self   :						Tuple		)						->       List[Any]:




       """simple docstring"""


       lowercase__					      =		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_model(*__snake_case		)

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




       """simple docstring"""


       lowercase__					      =		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_masked_lm(*__snake_case		)

     def   lowerCamelCase__					(self   :						Optional[Any]		)						->       Dict:




       """simple docstring"""


       lowercase__					      =		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_multiple_choice(*__snake_case		)

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




       """simple docstring"""


       lowercase__					      =		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*__snake_case		)

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




       """simple docstring"""


       lowercase__					      =		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_pretraining(*__snake_case		)

     def   lowerCamelCase__					(self   :						Any		)						->       List[str]:




       """simple docstring"""


       lowercase__					      =		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_question_answering(*__snake_case		)

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




       """simple docstring"""


       lowercase__					      =		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_sequence_classification(*__snake_case		)

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




       """simple docstring"""


       lowercase__					      =		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_token_classification(*__snake_case		)


     @slow
     def   lowerCamelCase__					(self   :						str		)						->       Optional[int]:




       """simple docstring"""


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






@require_tf
class 							A   (	unittest.TestCase							):
     '''simple docstring'''






     @slow
     def   lowerCamelCase__					(self   :						Tuple		)						->       str:




       """simple docstring"""


       lowercase__					      =		TFMobileBertForPreTraining.from_pretrained("""google/mobilebert-uncased"""		)
       lowercase__					      =		tf.constant([[0, 1, 2, 3, 4, 5]]		)
       lowercase__					      =		model(__snake_case		)[0]

       lowercase__					      =		[1, 6, 3_0522]
       self.assertEqual(output.shape				, __snake_case		)

       lowercase__					      =		tf.constant(
           [
               [
                   [-4.5_919_547, -9.248_295, -9.645_256],
                   [-6.7_306_175, -6.440_284, -6.6_052_837],
                   [-7.2_743_506, -6.7_847_915, -6.024_673],
               ]
           ]		)
       tf.debugging.assert_near(output[:, :3, :3]				, __snake_case				, atol=1E-4		)





import json
import os
import unittest

from transformers import OpenAIGPTTokenizer, OpenAIGPTTokenizerFast
from transformers.models.openai.tokenization_openai import VOCAB_FILES_NAMES
from transformers.testing_utils import require_ftfy, require_spacy, require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin



@require_tokenizers
class 							A   (	UpperCAmelCase__			,  unittest.TestCase							):
					'''simple docstring'''






					A__      =			OpenAIGPTTokenizer
					A__      =			OpenAIGPTTokenizerFast
					A__      =			True
					A__      =			False

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




							"""simple docstring"""


							super().setUp()

							# Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
							lowercase__					      =		[
							    """l""",
							    """o""",
							    """w""",
							    """e""",
							    """r""",
							    """s""",
							    """t""",
							    """i""",
							    """d""",
							    """n""",
							    """w</w>""",
							    """r</w>""",
							    """t</w>""",
							    """lo""",
							    """low""",
							    """er</w>""",
							    """low</w>""",
							    """lowest</w>""",
							    """newer</w>""",
							    """wider</w>""",
							    """<unk>""",
							]
							lowercase__					      =		dict(zip(_UpperCAmelCase				, range(len(_UpperCAmelCase		)		)		)		)
							lowercase__					      =		["""#version: 0.2""", """l o""", """lo w""", """e r</w>""", """"""]

							lowercase__					      =		os.path.join(self.tmpdirname				, VOCAB_FILES_NAMES["""vocab_file"""]		)
							lowercase__					      =		os.path.join(self.tmpdirname				, VOCAB_FILES_NAMES["""merges_file"""]		)
							with open(self.vocab_file				, """w"""		) as fp:
									fp.write(json.dumps(_UpperCAmelCase		)		)
							with open(self.merges_file				, """w"""		) as fp:
									fp.write("""\n""".join(_UpperCAmelCase		)		)

					def   lowerCamelCase__					(self   :						Dict				, _UpperCAmelCase   :						List[Any]		)						->       Any:




							"""simple docstring"""


							return "lower newer", "lower newer"

					def   lowerCamelCase__					(self   :						Dict		)						->       Tuple:




							"""simple docstring"""


							lowercase__					      =		OpenAIGPTTokenizer(self.vocab_file				, self.merges_file		)

							lowercase__					      =		"""lower"""
							lowercase__					      =		["""low""", """er</w>"""]
							lowercase__					      =		tokenizer.tokenize(_UpperCAmelCase		)
							self.assertListEqual(_UpperCAmelCase				, _UpperCAmelCase		)

							lowercase__					      =		tokens + ["""<unk>"""]
							lowercase__					      =		[14, 15, 20]
							self.assertListEqual(tokenizer.convert_tokens_to_ids(_UpperCAmelCase		)				, _UpperCAmelCase		)

					def   lowerCamelCase__					(self   :						Optional[int]				, _UpperCAmelCase   :						Union[str, Any]=15		)						->       Optional[int]:




							"""simple docstring"""


							for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
									with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})'''		):
											lowercase__					      =		self.rust_tokenizer_class.from_pretrained(_UpperCAmelCase				, **_UpperCAmelCase		)

											# Simple input
											lowercase__					      =		"""This is a simple input"""
											lowercase__					      =		["""This is a simple input 1""", """This is a simple input 2"""]
											lowercase__					      =		("""This is a simple input""", """This is a pair""")
											lowercase__					      =		[
											    ("""This is a simple input 1""", """This is a simple input 2"""),
											    ("""This is a simple pair 1""", """This is a simple pair 2"""),
											]

											# Simple input tests
											self.assertRaises(_UpperCAmelCase				, tokenizer_r.encode				, _UpperCAmelCase				, max_length=_UpperCAmelCase				, padding="""max_length"""		)

											# Simple input
											self.assertRaises(_UpperCAmelCase				, tokenizer_r.encode_plus				, _UpperCAmelCase				, max_length=_UpperCAmelCase				, padding="""max_length"""		)

											# Simple input
											self.assertRaises(
											    _UpperCAmelCase				, tokenizer_r.batch_encode_plus				, _UpperCAmelCase				, max_length=_UpperCAmelCase				, padding="""max_length"""				, )

											# Pair input
											self.assertRaises(_UpperCAmelCase				, tokenizer_r.encode				, _UpperCAmelCase				, max_length=_UpperCAmelCase				, padding="""max_length"""		)

											# Pair input
											self.assertRaises(_UpperCAmelCase				, tokenizer_r.encode_plus				, _UpperCAmelCase				, max_length=_UpperCAmelCase				, padding="""max_length"""		)

											# Pair input
											self.assertRaises(
											    _UpperCAmelCase				, tokenizer_r.batch_encode_plus				, _UpperCAmelCase				, max_length=_UpperCAmelCase				, padding="""max_length"""				, )


					def   lowerCamelCase__					(self   :						Tuple		)						->       Any:




							"""simple docstring"""


							pass






@require_ftfy
@require_spacy
@require_tokenizers
class 							A   (	UpperCAmelCase__							):
					'''simple docstring'''






					pass

