"""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        snake_case :




					"""simple docstring"""

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







					def 	__lowerCAmelCase		(       self     :			Optional[int]    ):
									UpperCAmelCase__    =							ids_tensor([self.batch_size, self.seq_length]					,self.vocab_size    )

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

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

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

									UpperCAmelCase__    =							self.get_config()

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







					def 	__lowerCAmelCase		(       self     :			Tuple    ):
									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=lowerCamelCase__					,initializer_range=self.initializer_range					,)







					def 	__lowerCAmelCase		(       self     :			List[str]					,lowerCamelCase__     :			List[Any]					,lowerCamelCase__     :			Dict					,lowerCamelCase__     :			List[Any]					,lowerCamelCase__     :			Union[str, Any]					,lowerCamelCase__     :			Union[str, Any]					,lowerCamelCase__     :			Optional[Any]					,lowerCamelCase__     :			Dict    ):
									UpperCAmelCase__    =							MobileBertModel(config=lowerCamelCase__    )
									model.to(lowerCamelCase__    )
									model.eval()
									UpperCAmelCase__    =							model(lowerCamelCase__					,attention_mask=lowerCamelCase__					,token_type_ids=lowerCamelCase__    )
									UpperCAmelCase__    =							model(lowerCamelCase__					,token_type_ids=lowerCamelCase__    )
									UpperCAmelCase__    =							model(lowerCamelCase__    )

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







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







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







					def 	__lowerCAmelCase		(       self     :			Union[str, Any]					,lowerCamelCase__     :			Dict					,lowerCamelCase__     :			str					,lowerCamelCase__     :			int					,lowerCamelCase__     :			int					,lowerCamelCase__     :			Optional[Any]					,lowerCamelCase__     :			Optional[Any]					,lowerCamelCase__     :			int    ):
									UpperCAmelCase__    =							self.num_labels
									UpperCAmelCase__    =							MobileBertForTokenClassification(config=lowerCamelCase__    )
									model.to(lowerCamelCase__    )
									model.eval()
									UpperCAmelCase__    =							model(lowerCamelCase__					,attention_mask=lowerCamelCase__					,token_type_ids=lowerCamelCase__					,labels=lowerCamelCase__    )
									self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.seq_length, self.num_labels)    )







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







					def 	__lowerCAmelCase		(       self     :			List[str]    ):
									UpperCAmelCase__    =							self.prepare_config_and_inputs()
									(
									    (
									    UpperCAmelCase__
									)   ,			(
									    UpperCAmelCase__
									)   ,			(
									    UpperCAmelCase__
									)   ,			(
									    UpperCAmelCase__
									)   ,			(
									    UpperCAmelCase__
									)   ,			(
									    UpperCAmelCase__
									)   ,			(
									    UpperCAmelCase__
									)   ,			
									)    =							config_and_inputs
									UpperCAmelCase__    =							{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
									return config, inputs_dict






@require_torch
class        snake_case (				__UpperCAmelCase   ,					__UpperCAmelCase   ,					unittest.TestCase       ):




					"""simple docstring"""

					snake_case__       					=				(
					    (
					        MobileBertModel,
					        MobileBertForMaskedLM,
					        MobileBertForMultipleChoice,
					        MobileBertForNextSentencePrediction,
					        MobileBertForPreTraining,
					        MobileBertForQuestionAnswering,
					        MobileBertForSequenceClassification,
					        MobileBertForTokenClassification,
					    )
					    if is_torch_available()
					    else ()
					)
					snake_case__       					=				(
					    {
					        "feature-extraction": MobileBertModel,
					        "fill-mask": MobileBertForMaskedLM,
					        "question-answering": MobileBertForQuestionAnswering,
					        "text-classification": MobileBertForSequenceClassification,
					        "token-classification": MobileBertForTokenClassification,
					        "zero-shot": MobileBertForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)
					snake_case__       					=				True







					def 	__lowerCAmelCase		(       self     :			Optional[Any]					,lowerCamelCase__     :			Dict					,lowerCamelCase__     :			Any					,lowerCamelCase__     :			Optional[int]=False    ):
									UpperCAmelCase__    =							super()._prepare_for_class(lowerCamelCase__					,lowerCamelCase__					,return_labels=lowerCamelCase__    )

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







					def 	__lowerCAmelCase		(       self     :			List[str]    ):
									UpperCAmelCase__    =							MobileBertModelTester(self    )
									UpperCAmelCase__    =							ConfigTester(self					,config_class=lowerCamelCase__					,hidden_size=37    )







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







					def 	__lowerCAmelCase		(       self     :			Optional[Any]    ):
									UpperCAmelCase__    =							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_model(*lowerCamelCase__    )







					def 	__lowerCAmelCase		(       self     :			Dict    ):
									UpperCAmelCase__    =							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_masked_lm(*lowerCamelCase__    )







					def 	__lowerCAmelCase		(       self     :			List[str]    ):
									UpperCAmelCase__    =							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_multiple_choice(*lowerCamelCase__    )







					def 	__lowerCAmelCase		(       self     :			Any    ):
									UpperCAmelCase__    =							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*lowerCamelCase__    )







					def 	__lowerCAmelCase		(       self     :			List[Any]    ):
									UpperCAmelCase__    =							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_pretraining(*lowerCamelCase__    )







					def 	__lowerCAmelCase		(       self     :			Dict    ):
									UpperCAmelCase__    =							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_question_answering(*lowerCamelCase__    )







					def 	__lowerCAmelCase		(       self     :			List[str]    ):
									UpperCAmelCase__    =							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_sequence_classification(*lowerCamelCase__    )







					def 	__lowerCAmelCase		(       self     :			Dict    ):
									UpperCAmelCase__    =							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mobilebert_for_token_classification(*lowerCamelCase__    )






def 						a_		(      lowerCamelCase ):
				return torch.tensor(
				    lowerCamelCase      ,    dtype=torch.long      ,    device=lowerCamelCase      ,    )


lowerCAmelCase__						:						Dict		  =			1E-3







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




					"""simple docstring"""

					@slow
					def 	__lowerCAmelCase		(       self     :			int    ):
									UpperCAmelCase__    =							MobileBertModel.from_pretrained('google/mobilebert-uncased'    ).to(lowerCamelCase__    )
									UpperCAmelCase__    =							_long_tensor([[101, 7_110, 1_005, 1_056, 2_023, 11_333, 17_413, 1_029, 102]]    )
									with torch.no_grad():
													UpperCAmelCase__    =							model(lowerCamelCase__    )[0]
									UpperCAmelCase__    =							torch.Size((1, 9, 512)    )
									self.assertEqual(output.shape					,lowerCamelCase__    )
									UpperCAmelCase__    =							torch.tensor(
									    [
									        [
									            [-2.473_6526e07, 8.269_1656e04, 1.652_1838e05],
									            [-5.754_1704e-01, 3.905_6022e00, 4.401_1507e00],
									            [2.604_7359e00, 1.567_7652e00, -1.732_4188e-01],
									        ]
									    ]					,device=lowerCamelCase__					,)

									# 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
									UpperCAmelCase__    =							torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE    )
									UpperCAmelCase__    =							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_tf_available, is_torch_available


lowerCAmelCase__						:						int		  =			{
    'configuration_tapas': ['TAPAS_PRETRAINED_CONFIG_ARCHIVE_MAP', 'TapasConfig'],
    'tokenization_tapas': ['TapasTokenizer'],
}

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						lowerCAmelCase__						:						Union[str, Any]		  =			[
						    'TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST',
						    'TapasForMaskedLM',
						    'TapasForQuestionAnswering',
						    'TapasForSequenceClassification',
						    'TapasModel',
						    'TapasPreTrainedModel',
						    'load_tf_weights_in_tapas',
						]
try:
						if not is_tf_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						lowerCAmelCase__						:						Optional[Any]		  =			[
						    'TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST',
						    'TFTapasForMaskedLM',
						    'TFTapasForQuestionAnswering',
						    'TFTapasForSequenceClassification',
						    'TFTapasModel',
						    'TFTapasPreTrainedModel',
						]


if TYPE_CHECKING:
						from .configuration_tapas import TAPAS_PRETRAINED_CONFIG_ARCHIVE_MAP, TapasConfig
						from .tokenization_tapas import TapasTokenizer

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_tapas import (
												    TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST,
												    TapasForMaskedLM,
												    TapasForQuestionAnswering,
												    TapasForSequenceClassification,
												    TapasModel,
												    TapasPreTrainedModel,
												    load_tf_weights_in_tapas,
												)

						try:
												if not is_tf_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_tf_tapas import (
												    TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST,
												    TFTapasForMaskedLM,
												    TFTapasForQuestionAnswering,
												    TFTapasForSequenceClassification,
												    TFTapasModel,
												    TFTapasPreTrainedModel,
												)


else:
						import sys

						lowerCAmelCase__						:						Dict		  =			_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)



