
"""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       lowerCAmelCase_		(				_UpperCamelCase				,				_UpperCamelCase				,				unittest.TestCase ):



							'''simple docstring'''





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




							def    A__						( self    ,				snake_case_    ,				snake_case_    ,				snake_case_=False				)       ->  List[Any]:
													__lowerCAmelCase     =  super()._prepare_for_class(_UpperCAmelCase    ,				_UpperCAmelCase    ,				return_labels=_UpperCAmelCase				)

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

													return inputs_dict
							class       lowerCAmelCase_		(				_UpperCamelCase ):



														'''simple docstring'''





														def __init__( self    ,				snake_case_    ,				snake_case_=13    ,				snake_case_=7    ,				snake_case_=True    ,				snake_case_=True    ,				snake_case_=True    ,				snake_case_=True    ,				snake_case_=99    ,				snake_case_=32    ,				snake_case_=32    ,				snake_case_=2    ,				snake_case_=4    ,				snake_case_=37    ,				snake_case_="gelu"    ,				snake_case_=0.1    ,				snake_case_=0.1    ,				snake_case_=512    ,				snake_case_=16    ,				snake_case_=2    ,				snake_case_=0.02    ,				snake_case_=3    ,				snake_case_=4    ,				snake_case_=None    ,				)       ->  Dict:
																				__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    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_				)       ->  Tuple:
																				__lowerCAmelCase     =  TFMobileBertModel(config=_UpperCAmelCase				)
																				__lowerCAmelCase     =  {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
																				__lowerCAmelCase     =  model(_UpperCAmelCase				)

																				__lowerCAmelCase     =  [input_ids, input_mask]
																				__lowerCAmelCase     =  model(_UpperCAmelCase				)

																				__lowerCAmelCase     =  model(_UpperCAmelCase				)

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




														def    A__						( self    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_				)       ->  Optional[int]:
																				__lowerCAmelCase     =  TFMobileBertForNextSentencePrediction(config=_UpperCAmelCase				)
																				__lowerCAmelCase     =  {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
																				__lowerCAmelCase     =  model(_UpperCAmelCase				)
																				self.parent.assertEqual(result.logits.shape    ,				(self.batch_size, 2)				)




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




														def    A__						( self    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_				)       ->  Tuple:
																				__lowerCAmelCase     =  self.num_choices
																				__lowerCAmelCase     =  TFMobileBertForMultipleChoice(config=_UpperCAmelCase				)
																				__lowerCAmelCase     =  tf.tile(tf.expand_dims(_UpperCAmelCase    ,				1				)    ,				(1, self.num_choices, 1)				)
																				__lowerCAmelCase     =  tf.tile(tf.expand_dims(_UpperCAmelCase    ,				1				)    ,				(1, self.num_choices, 1)				)
																				__lowerCAmelCase     =  tf.tile(tf.expand_dims(_UpperCAmelCase    ,				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(_UpperCAmelCase				)
																				self.parent.assertEqual(result.logits.shape    ,				(self.batch_size, self.num_choices)				)




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




														def    A__						( self    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_    ,				snake_case_				)       ->  str:
																				__lowerCAmelCase     =  TFMobileBertForQuestionAnswering(config=_UpperCAmelCase				)
																				__lowerCAmelCase     =  {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
																				__lowerCAmelCase     =  model(_UpperCAmelCase				)
																				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				)       ->  Optional[Any]:
																				__lowerCAmelCase     =  self.prepare_config_and_inputs()
																				(
																				    __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				)       ->  Optional[int]:
													__lowerCAmelCase     =  TFMobileBertModelTest.TFMobileBertModelTester(self				)
													__lowerCAmelCase     =  ConfigTester(self    ,				config_class=_UpperCAmelCase    ,				hidden_size=37				)




							def    A__						( self				)       ->  Dict:
													self.config_tester.run_common_tests()




							def    A__						( self				)       ->  List[str]:
													__lowerCAmelCase     =  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_mobilebert_model(*_UpperCAmelCase				)




							def    A__						( self				)       ->  str:
													__lowerCAmelCase     =  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_mobilebert_for_masked_lm(*_UpperCAmelCase				)




							def    A__						( self				)       ->  Any:
													__lowerCAmelCase     =  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_mobilebert_for_multiple_choice(*_UpperCAmelCase				)




							def    A__						( self				)       ->  Optional[int]:
													__lowerCAmelCase     =  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*_UpperCAmelCase				)




							def    A__						( self				)       ->  Optional[Any]:
													__lowerCAmelCase     =  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_mobilebert_for_pretraining(*_UpperCAmelCase				)




							def    A__						( self				)       ->  Tuple:
													__lowerCAmelCase     =  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_mobilebert_for_question_answering(*_UpperCAmelCase				)




							def    A__						( self				)       ->  Any:
													__lowerCAmelCase     =  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_mobilebert_for_sequence_classification(*_UpperCAmelCase				)




							def    A__						( self				)       ->  Union[str, Any]:
													__lowerCAmelCase     =  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_mobilebert_for_token_classification(*_UpperCAmelCase				)




							@slow
							def    A__						( self				)       ->  List[str]:
													# for model_name in TF_MOBILEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													for model_name in ["google/mobilebert-uncased"]:
																			__lowerCAmelCase     =  TFMobileBertModel.from_pretrained(_UpperCAmelCase				)
																			self.assertIsNotNone(_UpperCAmelCase				)
@require_tf
class       lowerCAmelCase_		(				unittest.TestCase ):



							'''simple docstring'''





							@slow
							def    A__						( self				)       ->  Dict:
													__lowerCAmelCase     =  TFMobileBertForPreTraining.from_pretrained("""google/mobilebert-uncased"""				)
													__lowerCAmelCase     =  tf.constant([[0, 1, 2, 3, 4, 5]]				)
													__lowerCAmelCase     =  model(_UpperCAmelCase				)[0]

													__lowerCAmelCase     =  [1, 6, 30_522]
													self.assertEqual(output.shape    ,				_UpperCAmelCase				)

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





'''simple docstring'''

from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tensorflow_text_available, is_torch_available


__lowerCAmelCase		     =  {
    '''configuration_ernie''': ['''ERNIE_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ErnieConfig''', '''ErnieOnnxConfig'''],
}

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__lowerCAmelCase		     =  [
							    '''ERNIE_PRETRAINED_MODEL_ARCHIVE_LIST''',
							    '''ErnieForCausalLM''',
							    '''ErnieForMaskedLM''',
							    '''ErnieForMultipleChoice''',
							    '''ErnieForNextSentencePrediction''',
							    '''ErnieForPreTraining''',
							    '''ErnieForQuestionAnswering''',
							    '''ErnieForSequenceClassification''',
							    '''ErnieForTokenClassification''',
							    '''ErnieModel''',
							    '''ErniePreTrainedModel''',
							]

if TYPE_CHECKING:
							from .configuration_ernie import ERNIE_PRETRAINED_CONFIG_ARCHIVE_MAP, ErnieConfig, ErnieOnnxConfig

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_ernie import (
														    ERNIE_PRETRAINED_MODEL_ARCHIVE_LIST,
														    ErnieForCausalLM,
														    ErnieForMaskedLM,
														    ErnieForMultipleChoice,
														    ErnieForNextSentencePrediction,
														    ErnieForPreTraining,
														    ErnieForQuestionAnswering,
														    ErnieForSequenceClassification,
														    ErnieForTokenClassification,
														    ErnieModel,
														    ErniePreTrainedModel,
														)

else:
							import sys

							__lowerCAmelCase		     =  _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)




