



'''simple docstring'''

import unittest

from transformers import MobileBertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch

							from transformers import (
							    MODEL_FOR_PRETRAINING_MAPPING,
							    MobileBertForMaskedLM,
							    MobileBertForMultipleChoice,
							    MobileBertForNextSentencePrediction,
							    MobileBertForPreTraining,
							    MobileBertForQuestionAnswering,
							    MobileBertForSequenceClassification,
							    MobileBertForTokenClassification,
							    MobileBertModel,
							)

class       __magic_name__		:







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







							def 				__lowercase     (				self   :			Optional[int]						):
									_a	: Any								=				ids_tensor([self.batch_size, self.seq_length]      ,self.vocab_size						)

									_a	: str								=				None
									if self.use_input_mask:
											_a	: Tuple								=				random_attention_mask([self.batch_size, self.seq_length]						)

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

									_a	: int								=				None
									_a	: str								=				None
									_a	: str								=				None
									if self.use_labels:
											_a	: Tuple								=				ids_tensor([self.batch_size]      ,self.type_sequence_label_size						)
											_a	: List[Any]								=				ids_tensor([self.batch_size, self.seq_length]      ,self.num_labels						)
											_a	: Union[str, Any]								=				ids_tensor([self.batch_size]      ,self.num_choices						)

									_a	: Any								=				self.get_config()

									return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels







							def 				__lowercase     (				self   :			Dict						):
									return 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      ,embedding_size=self.embedding_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      ,is_decoder=_UpperCAmelCase      ,initializer_range=self.initializer_range      ,)







							def 				__lowercase     (				self   :			Tuple      ,_UpperCAmelCase   :			Optional[Any]      ,_UpperCAmelCase   :			List[Any]      ,_UpperCAmelCase   :			Any      ,_UpperCAmelCase   :			str      ,_UpperCAmelCase   :			List[str]      ,_UpperCAmelCase   :			Dict      ,_UpperCAmelCase   :			str						):
									_a	: str								=				MobileBertModel(config=_UpperCAmelCase						)
									model.to(_UpperCAmelCase						)
									model.eval()
									_a	: Tuple								=				model(_UpperCAmelCase      ,attention_mask=_UpperCAmelCase      ,token_type_ids=_UpperCAmelCase						)
									_a	: List[Any]								=				model(_UpperCAmelCase      ,token_type_ids=_UpperCAmelCase						)
									_a	: List[Any]								=				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 				__lowercase     (				self   :			str      ,_UpperCAmelCase   :			int      ,_UpperCAmelCase   :			str      ,_UpperCAmelCase   :			Optional[Any]      ,_UpperCAmelCase   :			List[str]      ,_UpperCAmelCase   :			List[str]      ,_UpperCAmelCase   :			List[Any]      ,_UpperCAmelCase   :			Dict						):
									_a	: int								=				MobileBertForMaskedLM(config=_UpperCAmelCase						)
									model.to(_UpperCAmelCase						)
									model.eval()
									_a	: Union[str, Any]								=				model(_UpperCAmelCase      ,attention_mask=_UpperCAmelCase      ,token_type_ids=_UpperCAmelCase      ,labels=_UpperCAmelCase						)
									self.parent.assertEqual(result.logits.shape      ,(self.batch_size, self.seq_length, self.vocab_size)						)







							def 				__lowercase     (				self   :			Dict      ,_UpperCAmelCase   :			Dict      ,_UpperCAmelCase   :			Any      ,_UpperCAmelCase   :			int      ,_UpperCAmelCase   :			List[str]      ,_UpperCAmelCase   :			List[str]      ,_UpperCAmelCase   :			List[Any]      ,_UpperCAmelCase   :			Tuple						):
									_a	: Optional[int]								=				MobileBertForNextSentencePrediction(config=_UpperCAmelCase						)
									model.to(_UpperCAmelCase						)
									model.eval()
									_a	: str								=				model(
									    _UpperCAmelCase      ,attention_mask=_UpperCAmelCase      ,token_type_ids=_UpperCAmelCase      ,labels=_UpperCAmelCase      ,)
									self.parent.assertEqual(result.logits.shape      ,(self.batch_size, 2)						)







							def 				__lowercase     (				self   :			Any      ,_UpperCAmelCase   :			Optional[int]      ,_UpperCAmelCase   :			int      ,_UpperCAmelCase   :			Optional[Any]      ,_UpperCAmelCase   :			List[str]      ,_UpperCAmelCase   :			Dict      ,_UpperCAmelCase   :			Any      ,_UpperCAmelCase   :			List[Any]						):
									_a	: int								=				MobileBertForPreTraining(config=_UpperCAmelCase						)
									model.to(_UpperCAmelCase						)
									model.eval()
									_a	: str								=				model(
									    _UpperCAmelCase      ,attention_mask=_UpperCAmelCase      ,token_type_ids=_UpperCAmelCase      ,labels=_UpperCAmelCase      ,next_sentence_label=_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 				__lowercase     (				self   :			int      ,_UpperCAmelCase   :			Union[str, Any]      ,_UpperCAmelCase   :			str      ,_UpperCAmelCase   :			Tuple      ,_UpperCAmelCase   :			int      ,_UpperCAmelCase   :			int      ,_UpperCAmelCase   :			Optional[Any]      ,_UpperCAmelCase   :			str						):
									_a	: int								=				MobileBertForQuestionAnswering(config=_UpperCAmelCase						)
									model.to(_UpperCAmelCase						)
									model.eval()
									_a	: List[Any]								=				model(
									    _UpperCAmelCase      ,attention_mask=_UpperCAmelCase      ,token_type_ids=_UpperCAmelCase      ,start_positions=_UpperCAmelCase      ,end_positions=_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 				__lowercase     (				self   :			Union[str, Any]      ,_UpperCAmelCase   :			str      ,_UpperCAmelCase   :			List[Any]      ,_UpperCAmelCase   :			Optional[Any]      ,_UpperCAmelCase   :			str      ,_UpperCAmelCase   :			int      ,_UpperCAmelCase   :			Union[str, Any]      ,_UpperCAmelCase   :			List[Any]						):
									_a	: Any								=				self.num_labels
									_a	: Optional[Any]								=				MobileBertForSequenceClassification(_UpperCAmelCase						)
									model.to(_UpperCAmelCase						)
									model.eval()
									_a	: Optional[Any]								=				model(_UpperCAmelCase      ,attention_mask=_UpperCAmelCase      ,token_type_ids=_UpperCAmelCase      ,labels=_UpperCAmelCase						)
									self.parent.assertEqual(result.logits.shape      ,(self.batch_size, self.num_labels)						)







							def 				__lowercase     (				self   :			Union[str, Any]      ,_UpperCAmelCase   :			int      ,_UpperCAmelCase   :			int      ,_UpperCAmelCase   :			List[str]      ,_UpperCAmelCase   :			Any      ,_UpperCAmelCase   :			Union[str, Any]      ,_UpperCAmelCase   :			List[str]      ,_UpperCAmelCase   :			Any						):
									_a	: int								=				self.num_labels
									_a	: List[Any]								=				MobileBertForTokenClassification(config=_UpperCAmelCase						)
									model.to(_UpperCAmelCase						)
									model.eval()
									_a	: Dict								=				model(_UpperCAmelCase      ,attention_mask=_UpperCAmelCase      ,token_type_ids=_UpperCAmelCase      ,labels=_UpperCAmelCase						)
									self.parent.assertEqual(result.logits.shape      ,(self.batch_size, self.seq_length, self.num_labels)						)







							def 				__lowercase     (				self   :			Optional[int]      ,_UpperCAmelCase   :			Union[str, Any]      ,_UpperCAmelCase   :			Tuple      ,_UpperCAmelCase   :			Dict      ,_UpperCAmelCase   :			Dict      ,_UpperCAmelCase   :			Union[str, Any]      ,_UpperCAmelCase   :			List[str]      ,_UpperCAmelCase   :			List[Any]						):
									_a	: str								=				self.num_choices
									_a	: str								=				MobileBertForMultipleChoice(config=_UpperCAmelCase						)
									model.to(_UpperCAmelCase						)
									model.eval()
									_a	: Optional[int]								=				input_ids.unsqueeze(1						).expand(-1      ,self.num_choices      ,-1						).contiguous()
									_a	: List[str]								=				token_type_ids.unsqueeze(1						).expand(-1      ,self.num_choices      ,-1						).contiguous()
									_a	: Any								=				input_mask.unsqueeze(1						).expand(-1      ,self.num_choices      ,-1						).contiguous()
									_a	: Union[str, Any]								=				model(
									    _UpperCAmelCase      ,attention_mask=_UpperCAmelCase      ,token_type_ids=_UpperCAmelCase      ,labels=_UpperCAmelCase      ,)
									self.parent.assertEqual(result.logits.shape      ,(self.batch_size, self.num_choices)						)







							def 				__lowercase     (				self   :			Any						):
									_a	: Optional[Any]								=				self.prepare_config_and_inputs()
									(
									    (
									    _a
									)			,   (
									    _a
									)			,   (
									    _a
									)			,   (
									    _a
									)			,   (
									    _a
									)			,   (
									    _a
									)			,   (
									    _a
									)			,   
									)	: Any								=				config_and_inputs
									_a	: List[Any]								=				{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
									return config, inputs_dict

@require_torch
class       __magic_name__		(       _UpperCamelCase     ,		_UpperCamelCase     ,		unittest.TestCase  ):
							lowerCAmelCase			:       Dict  							=   (
							    (
							        MobileBertModel,
							        MobileBertForMaskedLM,
							        MobileBertForMultipleChoice,
							        MobileBertForNextSentencePrediction,
							        MobileBertForPreTraining,
							        MobileBertForQuestionAnswering,
							        MobileBertForSequenceClassification,
							        MobileBertForTokenClassification,
							    )
							    if is_torch_available()
							    else ()
							)
							lowerCAmelCase			:       List[str]  							=   (
							    {
							        'feature-extraction': MobileBertModel,
							        'fill-mask': MobileBertForMaskedLM,
							        'question-answering': MobileBertForQuestionAnswering,
							        'text-classification': MobileBertForSequenceClassification,
							        'token-classification': MobileBertForTokenClassification,
							        'zero-shot': MobileBertForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							lowerCAmelCase			:       Optional[Any]  							=   True







							def 				__lowercase     (				self   :			List[Any]      ,_UpperCAmelCase   :			Tuple      ,_UpperCAmelCase   :			Tuple      ,_UpperCAmelCase   :			Optional[int]=False						):
									_a	: Optional[Any]								=				super()._prepare_for_class(_UpperCAmelCase      ,_UpperCAmelCase      ,return_labels=_UpperCAmelCase						)

									if return_labels:
											if model_class in get_values(_UpperCAmelCase						):
													_a	: Tuple								=				torch.zeros(
													    (self.model_tester.batch_size, self.model_tester.seq_length)      ,dtype=torch.long      ,device=_UpperCAmelCase						)
													_a	: Tuple								=				torch.zeros(
													    self.model_tester.batch_size      ,dtype=torch.long      ,device=_UpperCAmelCase						)
									return inputs_dict







							def 				__lowercase     (				self   :			Any						):
									_a	: Optional[Any]								=				MobileBertModelTester(self						)
									_a	: List[Any]								=				ConfigTester(self      ,config_class=_UpperCAmelCase      ,hidden_size=37						)







							def 				__lowercase     (				self   :			Any						):
									self.config_tester.run_common_tests()







							def 				__lowercase     (				self   :			int						):
									_a	: Tuple								=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_model(*_UpperCAmelCase						)







							def 				__lowercase     (				self   :			Tuple						):
									_a	: List[str]								=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_masked_lm(*_UpperCAmelCase						)







							def 				__lowercase     (				self   :			Optional[Any]						):
									_a	: List[Any]								=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_multiple_choice(*_UpperCAmelCase						)







							def 				__lowercase     (				self   :			Any						):
									_a	: Union[str, Any]								=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*_UpperCAmelCase						)







							def 				__lowercase     (				self   :			str						):
									_a	: Tuple								=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_pretraining(*_UpperCAmelCase						)







							def 				__lowercase     (				self   :			Dict						):
									_a	: List[Any]								=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_question_answering(*_UpperCAmelCase						)







							def 				__lowercase     (				self   :			Tuple						):
									_a	: List[Any]								=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_sequence_classification(*_UpperCAmelCase						)







							def 				__lowercase     (				self   :			int						):
									_a	: Dict								=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_token_classification(*_UpperCAmelCase						)



def 	__lowerCamelCase	( lowerCAmelCase_    )     -> str:
		return torch.tensor(
		    lowerCAmelCase_		,     dtype=torch.long		,     device=lowerCAmelCase_		,     )


__lowerCAmelCase		     =  1e-3

@require_torch
@require_sentencepiece
@require_tokenizers
class       __magic_name__		(       unittest.TestCase  ):







							@slow
							def 				__lowercase     (				self   :			str						):
									_a	: int								=				MobileBertModel.from_pretrained('google/mobilebert-uncased'						).to(_UpperCAmelCase						)
									_a	: int								=				_long_tensor([[101, 7110, 1005, 1056, 2023, 11333, 17413, 1029, 102]]						)
									with torch.no_grad():
											_a	: Optional[int]								=				model(_UpperCAmelCase						)[0]
									_a	: int								=				torch.Size((1, 9, 512)						)
									self.assertEqual(output.shape      ,_UpperCAmelCase						)
									_a	: List[Any]								=				torch.tensor(
									    [
									        [
									            [-2.4_73_65_26E07, 8.2_69_16_56E04, 1.6_52_18_38E05],
									            [-5.7_54_17_04E-01, 3.9_05_60_22E00, 4.4_01_15_07E00],
									            [2.6_04_73_59E00, 1.5_67_76_52E00, -1.7_32_41_88E-01],
									        ]
									    ]      ,device=_UpperCAmelCase      ,)

									# MobileBERT results range from 10e0 to 10e8. Even a 0.0000001% difference with a value of 10e8 results in a
									# ~1 difference, it's therefore not a good idea to measure using addition.
									# Here, we instead divide the expected result with the result in order to obtain ~1. We then check that the
									# result is held between bounds: 1 - TOLERANCE < expected_result / result < 1 + TOLERANCE
									_a	: Dict								=				torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE						)
									_a	: List[Any]								=				torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE						)

									self.assertTrue(lower_bound and upper_bound						)









'''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__)




