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__				(   UpperCAmelCase       ,UpperCAmelCase       ,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   SCREAMING_SNAKE_CASE__     (       self	:	str  ,  lowercase__	:	Tuple  ,  lowercase__	:	int  ,  lowercase__	:	Optional[Any]=False						):
										_lowerCAmelCase							=							super()._prepare_for_class(lowercase__  ,  lowercase__  ,  return_labels=lowercase__						)

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

										return inputs_dict



			class        lowerCamelCase__				(   UpperCAmelCase				):





						def __init__(       self	:	str  ,  lowercase__	:	Any  ,  lowercase__	:	List[Any]=13  ,  lowercase__	:	Any=7  ,  lowercase__	:	Union[str, Any]=True  ,  lowercase__	:	str=True  ,  lowercase__	:	Optional[int]=True  ,  lowercase__	:	Dict=True  ,  lowercase__	:	Tuple=99  ,  lowercase__	:	Any=32  ,  lowercase__	:	str=32  ,  lowercase__	:	Any=2  ,  lowercase__	:	Optional[int]=4  ,  lowercase__	:	List[Any]=37  ,  lowercase__	:	List[str]="gelu"  ,  lowercase__	:	List[str]=0.1  ,  lowercase__	:	List[str]=0.1  ,  lowercase__	:	List[str]=5_12  ,  lowercase__	:	Dict=16  ,  lowercase__	:	Dict=2  ,  lowercase__	:	str=0.0_2  ,  lowercase__	:	Optional[Any]=3  ,  lowercase__	:	int=4  ,  lowercase__	:	int=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   SCREAMING_SNAKE_CASE__     (       self	:	Union[str, 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   SCREAMING_SNAKE_CASE__     (       self	:	Dict  ,  lowercase__	:	Dict  ,  lowercase__	:	Optional[Any]  ,  lowercase__	:	Any  ,  lowercase__	:	str  ,  lowercase__	:	Dict  ,  lowercase__	:	Tuple  ,  lowercase__	:	int						):
													_lowerCAmelCase							=							TFMobileBertModel(config=lowercase__						)
													_lowerCAmelCase							=							{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
													_lowerCAmelCase							=							model(lowercase__						)

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

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





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





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





						def   SCREAMING_SNAKE_CASE__     (       self	:	Dict  ,  lowercase__	:	List[str]  ,  lowercase__	:	Optional[int]  ,  lowercase__	:	str  ,  lowercase__	:	Any  ,  lowercase__	:	Any  ,  lowercase__	:	int  ,  lowercase__	:	str						):
													_lowerCAmelCase							=							self.num_choices
													_lowerCAmelCase							=							TFMobileBertForMultipleChoice(config=lowercase__						)
													_lowerCAmelCase							=							tf.tile(tf.expand_dims(lowercase__  ,  1						)  ,  (1, self.num_choices, 1)						)
													_lowerCAmelCase							=							tf.tile(tf.expand_dims(lowercase__  ,  1						)  ,  (1, self.num_choices, 1)						)
													_lowerCAmelCase							=							tf.tile(tf.expand_dims(lowercase__  ,  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(lowercase__						)
													self.parent.assertEqual(result.logits.shape  ,  (self.batch_size, self.num_choices)						)





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





						def   SCREAMING_SNAKE_CASE__     (       self	:	Dict  ,  lowercase__	:	Optional[int]  ,  lowercase__	:	List[str]  ,  lowercase__	:	Any  ,  lowercase__	:	List[Any]  ,  lowercase__	:	Optional[Any]  ,  lowercase__	:	Optional[Any]  ,  lowercase__	:	Optional[int]						):
													_lowerCAmelCase							=							TFMobileBertForQuestionAnswering(config=lowercase__						)
													_lowerCAmelCase							=							{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
													_lowerCAmelCase							=							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   SCREAMING_SNAKE_CASE__     (       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   SCREAMING_SNAKE_CASE__     (       self	:	str						):
										_lowerCAmelCase							=							TFMobileBertModelTest.TFMobileBertModelTester(self						)
										_lowerCAmelCase							=							ConfigTester(self  ,  config_class=lowercase__  ,  hidden_size=37						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	int						):
										self.config_tester.run_common_tests()





			def   SCREAMING_SNAKE_CASE__     (       self	:	Optional[int]						):
										_lowerCAmelCase							=							self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_model(*lowercase__						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Dict						):
										_lowerCAmelCase							=							self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_masked_lm(*lowercase__						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Optional[int]						):
										_lowerCAmelCase							=							self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_multiple_choice(*lowercase__						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	str						):
										_lowerCAmelCase							=							self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*lowercase__						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	str						):
										_lowerCAmelCase							=							self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_pretraining(*lowercase__						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Dict						):
										_lowerCAmelCase							=							self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_question_answering(*lowercase__						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Optional[int]						):
										_lowerCAmelCase							=							self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_sequence_classification(*lowercase__						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Dict						):
										_lowerCAmelCase							=							self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_token_classification(*lowercase__						)





			@slow
			def   SCREAMING_SNAKE_CASE__     (       self	:	Tuple						):
										# for model_name in TF_MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										for model_name in ["google/mobilebert-uncased"]:
																	_lowerCAmelCase							=							TFMobileBertModel.from_pretrained(lowercase__						)
																	self.assertIsNotNone(lowercase__						)







@require_tf
class        lowerCamelCase__				(   unittest.TestCase				):





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

										_lowerCAmelCase							=							[1, 6, 3_05_22]
										self.assertEqual(output.shape  ,  lowercase__						)

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







from ...configuration_utils import PretrainedConfig
from ...utils import logging


_lowercase:	List[Any]       			=		logging.get_logger(__name__)

_lowercase:	int       			=		{
    '''alibaba-damo/mgp-str-base''': '''https://huggingface.co/alibaba-damo/mgp-str-base/resolve/main/config.json''',
}



class        lowerCamelCase__				(   UpperCAmelCase				):
			UpperCamelCase__    							="mgp-str"





			def __init__(       self	:	int  ,  lowercase__	:	List[Any]=[32, 1_28]  ,  lowercase__	:	str=4  ,  lowercase__	:	List[str]=3  ,  lowercase__	:	Tuple=27  ,  lowercase__	:	Optional[Any]=38  ,  lowercase__	:	Optional[Any]=5_02_57  ,  lowercase__	:	Dict=3_05_22  ,  lowercase__	:	Dict=7_68  ,  lowercase__	:	Tuple=12  ,  lowercase__	:	int=12  ,  lowercase__	:	int=4.0  ,  lowercase__	:	Tuple=True  ,  lowercase__	:	List[Any]=False  ,  lowercase__	:	int=1e-5  ,  lowercase__	:	int=0.0  ,  lowercase__	:	Optional[Any]=0.0  ,  lowercase__	:	List[Any]=0.0  ,  lowercase__	:	Union[str, Any]=False  ,  lowercase__	:	str=0.0_2  ,  **lowercase__	:	Optional[int]  ,  ):
										super().__init__(**lowercase__						)

										_lowerCAmelCase							=							image_size
										_lowerCAmelCase							=							patch_size
										_lowerCAmelCase							=							num_channels
										_lowerCAmelCase							=							max_token_length
										_lowerCAmelCase							=							num_character_labels
										_lowerCAmelCase							=							num_bpe_labels
										_lowerCAmelCase							=							num_wordpiece_labels
										_lowerCAmelCase							=							hidden_size
										_lowerCAmelCase							=							num_hidden_layers
										_lowerCAmelCase							=							num_attention_heads
										_lowerCAmelCase							=							mlp_ratio
										_lowerCAmelCase							=							distilled
										_lowerCAmelCase							=							layer_norm_eps
										_lowerCAmelCase							=							drop_rate
										_lowerCAmelCase							=							qkv_bias
										_lowerCAmelCase							=							attn_drop_rate
										_lowerCAmelCase							=							drop_path_rate
										_lowerCAmelCase							=							output_aa_attentions
										_lowerCAmelCase							=							initializer_range






