


"""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 			__snake_case   (lowerCamelCase			,		lowerCamelCase			,		unittest.TestCase  ):
							__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   __a   (     self:    int     ,							A_:    Optional[Any]     ,							A_:    str     ,							A_:    Optional[int]=False   ):
										__lowerCamelCase          =      super()._prepare_for_class(A_     ,							A_     ,							return_labels=A_   )

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

										return inputs_dict




							class 			__snake_case   (lowerCamelCase  ):




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




														def   __a   (     self:    Any   ):
																	__lowerCamelCase          =      ids_tensor([self.batch_size, self.seq_length]     ,							self.vocab_size   )

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

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

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

																	__lowerCamelCase          =      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   __a   (     self:    int     ,							A_:    Tuple     ,							A_:    Tuple     ,							A_:    Union[str, Any]     ,							A_:    Dict     ,							A_:    Any     ,							A_:    Optional[Any]     ,							A_:    Optional[Any]   ):
																	__lowerCamelCase          =      TFMobileBertModel(config=A_   )
																	__lowerCamelCase          =      {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
																	__lowerCamelCase          =      model(A_   )

																	__lowerCamelCase          =      [input_ids, input_mask]
																	__lowerCamelCase          =      model(A_   )

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




														def   __a   (     self:    str     ,							A_:    List[str]     ,							A_:    Optional[int]     ,							A_:    Optional[Any]     ,							A_:    Dict     ,							A_:    Union[str, Any]     ,							A_:    Dict     ,							A_:    List[str]   ):
																	__lowerCamelCase          =      TFMobileBertForNextSentencePrediction(config=A_   )
																	__lowerCamelCase          =      {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
																	__lowerCamelCase          =      model(A_   )
																	self.parent.assertEqual(result.logits.shape     ,							(self.batch_size, 2)   )




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




														def   __a   (     self:    Optional[int]     ,							A_:    Dict     ,							A_:    Union[str, Any]     ,							A_:    Union[str, Any]     ,							A_:    Any     ,							A_:    Tuple     ,							A_:    str     ,							A_:    Union[str, Any]   ):
																	__lowerCamelCase          =      self.num_choices
																	__lowerCamelCase          =      TFMobileBertForMultipleChoice(config=A_   )
																	__lowerCamelCase          =      tf.tile(tf.expand_dims(A_     ,							1   )     ,							(1, self.num_choices, 1)   )
																	__lowerCamelCase          =      tf.tile(tf.expand_dims(A_     ,							1   )     ,							(1, self.num_choices, 1)   )
																	__lowerCamelCase          =      tf.tile(tf.expand_dims(A_     ,							1   )     ,							(1, self.num_choices, 1)   )
																	__lowerCamelCase          =      {
																	    """input_ids""": multiple_choice_inputs_ids,
																	    """attention_mask""": multiple_choice_input_mask,
																	    """token_type_ids""": multiple_choice_token_type_ids,
																	}
																	__lowerCamelCase          =      model(A_   )
																	self.parent.assertEqual(result.logits.shape     ,							(self.batch_size, self.num_choices)   )




														def   __a   (     self:    List[Any]     ,							A_:    Optional[Any]     ,							A_:    Optional[int]     ,							A_:    Optional[Any]     ,							A_:    int     ,							A_:    Any     ,							A_:    int     ,							A_:    str   ):
																	__lowerCamelCase          =      self.num_labels
																	__lowerCamelCase          =      TFMobileBertForTokenClassification(config=A_   )
																	__lowerCamelCase          =      {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
																	__lowerCamelCase          =      model(A_   )
																	self.parent.assertEqual(result.logits.shape     ,							(self.batch_size, self.seq_length, self.num_labels)   )




														def   __a   (     self:    Dict     ,							A_:    int     ,							A_:    int     ,							A_:    Union[str, Any]     ,							A_:    int     ,							A_:    Union[str, Any]     ,							A_:    Any     ,							A_:    Union[str, Any]   ):
																	__lowerCamelCase          =      TFMobileBertForQuestionAnswering(config=A_   )
																	__lowerCamelCase          =      {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
																	__lowerCamelCase          =      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   __a   (     self:    List[str]   ):
																	__lowerCamelCase          =      self.prepare_config_and_inputs()
																	(
																	    (
																	    __lowerCamelCase
																	)     ,(
																	    __lowerCamelCase
																	)     ,(
																	    __lowerCamelCase
																	)     ,(
																	    __lowerCamelCase
																	)     ,(
																	    __lowerCamelCase
																	)     ,(
																	    __lowerCamelCase
																	)     ,(
																	    __lowerCamelCase
																	)     ,
																	)          =      config_and_inputs
																	__lowerCamelCase          =      {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
																	return config, inputs_dict




							def   __a   (     self:    Tuple   ):
										__lowerCamelCase          =      TFMobileBertModelTest.TFMobileBertModelTester(self   )
										__lowerCamelCase          =      ConfigTester(self     ,							config_class=A_     ,							hidden_size=37   )




							def   __a   (     self:    List[Any]   ):
										self.config_tester.run_common_tests()




							def   __a   (     self:    Union[str, Any]   ):
										__lowerCamelCase          =      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_model(*A_   )




							def   __a   (     self:    Any   ):
										__lowerCamelCase          =      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_masked_lm(*A_   )




							def   __a   (     self:    int   ):
										__lowerCamelCase          =      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_multiple_choice(*A_   )




							def   __a   (     self:    Union[str, Any]   ):
										__lowerCamelCase          =      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*A_   )




							def   __a   (     self:    List[Any]   ):
										__lowerCamelCase          =      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_pretraining(*A_   )




							def   __a   (     self:    List[Any]   ):
										__lowerCamelCase          =      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_question_answering(*A_   )




							def   __a   (     self:    Optional[Any]   ):
										__lowerCamelCase          =      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_sequence_classification(*A_   )




							def   __a   (     self:    int   ):
										__lowerCamelCase          =      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_token_classification(*A_   )




							@slow
							def   __a   (     self:    Union[str, Any]   ):
										# for model_name in TF_MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										for model_name in ["google/mobilebert-uncased"]:
													__lowerCamelCase          =      TFMobileBertModel.from_pretrained(A_   )
													self.assertIsNotNone(A_   )




@require_tf
class 			__snake_case   (unittest.TestCase  ):




							@slow
							def   __a   (     self:    int   ):
										__lowerCamelCase          =      TFMobileBertForPreTraining.from_pretrained("""google/mobilebert-uncased"""   )
										__lowerCamelCase          =      tf.constant([[0, 1, 2, 3, 4, 5]]   )
										__lowerCamelCase          =      model(A_   )[0]

										__lowerCamelCase          =      [1, 6, 3_05_22]
										self.assertEqual(output.shape     ,							A_   )

										__lowerCamelCase          =      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]     ,							A_     ,							atol=1E-4   )








"""simple docstring"""




def       a_ (	lowercase__ :str,			lowercase__ :int     ):
			return [sentence[i : i + ngram_size] for i in range(len(lowercase__     ) - ngram_size + 1     )]


if __name__ == "__main__":
					from doctest import testmod

					testmod()




