




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   A:



	'''simple docstring'''


	def __init__(    self :  Dict						,							A_ :  Dict						,							A_ :  Optional[Any]=13						,							A_ :  List[Any]=7						,							A_ :  Any=True						,							A_ :  str=True						,							A_ :  Optional[int]=True						,							A_ :  Optional[int]=True						,							A_ :  Union[str, Any]=99						,							A_ :  Any=64						,							A_ :  Any=32						,							A_ :  List[str]=5						,							A_ :  List[str]=4						,							A_ :  Dict=37						,							A_ :  Union[str, Any]="gelu"						,							A_ :  Optional[Any]=0.1						,							A_ :  List[Any]=0.1						,							A_ :  Any=512						,							A_ :  List[Any]=16						,							A_ :  Any=2						,							A_ :  Any=0.02						,							A_ :  Optional[Any]=3						,							A_ :  int=4						,							A_ :  List[str]=None						,							)       ->     int:

							"""simple docstring"""






							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_							=   embedding_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

	def     a__			(    self :  Tuple  )       ->     str:

							"""simple docstring"""






							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_							=   self.get_config()

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

	def     a__			(    self :  List[Any]  )       ->     Union[str, Any]:

							"""simple docstring"""






							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=A_						,							initializer_range=self.initializer_range						,							)

	def     a__			(    self :  int						,							A_ :  Tuple						,							A_ :  Optional[Any]						,							A_ :  List[str]						,							A_ :  Union[str, Any]						,							A_ :  str						,							A_ :  Optional[Any]						,							A_ :  Dict  )       ->     Optional[Any]:

							"""simple docstring"""






							lowerCamelCase_							=   MobileBertModel(config=A_  )
							model.to(A_  )
							model.eval()
							lowerCamelCase_							=   model(A_						,							attention_mask=A_						,							token_type_ids=A_  )
							lowerCamelCase_							=   model(A_						,							token_type_ids=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_ :  Optional[int]						,							A_ :  Dict						,							A_ :  Optional[Any]						,							A_ :  Union[str, Any]						,							A_ :  str						,							A_ :  int						,							A_ :  List[str]  )       ->     List[str]:

							"""simple docstring"""






							lowerCamelCase_							=   MobileBertForMaskedLM(config=A_  )
							model.to(A_  )
							model.eval()
							lowerCamelCase_							=   model(A_						,							attention_mask=A_						,							token_type_ids=A_						,							labels=A_  )
							self.parent.assertEqual(result.logits.shape						,							(self.batch_size, self.seq_length, self.vocab_size)  )

	def     a__			(    self :  Dict						,							A_ :  List[str]						,							A_ :  List[str]						,							A_ :  Optional[int]						,							A_ :  Optional[Any]						,							A_ :  Optional[int]						,							A_ :  int						,							A_ :  Any  )       ->     Dict:

							"""simple docstring"""






							lowerCamelCase_							=   MobileBertForNextSentencePrediction(config=A_  )
							model.to(A_  )
							model.eval()
							lowerCamelCase_							=   model(
							    A_						,							attention_mask=A_						,							token_type_ids=A_						,							labels=A_						,							)
							self.parent.assertEqual(result.logits.shape						,							(self.batch_size, 2)  )

	def     a__			(    self :  int						,							A_ :  int						,							A_ :  List[str]						,							A_ :  Optional[Any]						,							A_ :  Any						,							A_ :  str						,							A_ :  Optional[Any]						,							A_ :  Tuple  )       ->     str:

							"""simple docstring"""






							lowerCamelCase_							=   MobileBertForPreTraining(config=A_  )
							model.to(A_  )
							model.eval()
							lowerCamelCase_							=   model(
							    A_						,							attention_mask=A_						,							token_type_ids=A_						,							labels=A_						,							next_sentence_label=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 :  Any						,							A_ :  int						,							A_ :  List[Any]						,							A_ :  Union[str, Any]						,							A_ :  Tuple						,							A_ :  List[str]						,							A_ :  Optional[int]						,							A_ :  int  )       ->     Optional[Any]:

							"""simple docstring"""






							lowerCamelCase_							=   MobileBertForQuestionAnswering(config=A_  )
							model.to(A_  )
							model.eval()
							lowerCamelCase_							=   model(
							    A_						,							attention_mask=A_						,							token_type_ids=A_						,							start_positions=A_						,							end_positions=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 :  Tuple						,							A_ :  Optional[Any]						,							A_ :  str						,							A_ :  List[Any]						,							A_ :  Any						,							A_ :  Union[str, Any]						,							A_ :  Optional[int]						,							A_ :  List[str]  )       ->     Dict:

							"""simple docstring"""






							lowerCamelCase_							=   self.num_labels
							lowerCamelCase_							=   MobileBertForSequenceClassification(A_  )
							model.to(A_  )
							model.eval()
							lowerCamelCase_							=   model(A_						,							attention_mask=A_						,							token_type_ids=A_						,							labels=A_  )
							self.parent.assertEqual(result.logits.shape						,							(self.batch_size, self.num_labels)  )

	def     a__			(    self :  Union[str, Any]						,							A_ :  Tuple						,							A_ :  Union[str, Any]						,							A_ :  Optional[Any]						,							A_ :  Tuple						,							A_ :  List[Any]						,							A_ :  Any						,							A_ :  List[Any]  )       ->     Dict:

							"""simple docstring"""






							lowerCamelCase_							=   self.num_labels
							lowerCamelCase_							=   MobileBertForTokenClassification(config=A_  )
							model.to(A_  )
							model.eval()
							lowerCamelCase_							=   model(A_						,							attention_mask=A_						,							token_type_ids=A_						,							labels=A_  )
							self.parent.assertEqual(result.logits.shape						,							(self.batch_size, self.seq_length, self.num_labels)  )

	def     a__			(    self :  Tuple						,							A_ :  int						,							A_ :  int						,							A_ :  Dict						,							A_ :  Dict						,							A_ :  Optional[Any]						,							A_ :  Dict						,							A_ :  Tuple  )       ->     Optional[int]:

							"""simple docstring"""






							lowerCamelCase_							=   self.num_choices
							lowerCamelCase_							=   MobileBertForMultipleChoice(config=A_  )
							model.to(A_  )
							model.eval()
							lowerCamelCase_							=   input_ids.unsqueeze(1  ).expand(-1						,							self.num_choices						,							-1  ).contiguous()
							lowerCamelCase_							=   token_type_ids.unsqueeze(1  ).expand(-1						,							self.num_choices						,							-1  ).contiguous()
							lowerCamelCase_							=   input_mask.unsqueeze(1  ).expand(-1						,							self.num_choices						,							-1  ).contiguous()
							lowerCamelCase_							=   model(
							    A_						,							attention_mask=A_						,							token_type_ids=A_						,							labels=A_						,							)
							self.parent.assertEqual(result.logits.shape						,							(self.batch_size, self.num_choices)  )





	def     a__			(    self :  int  )       ->     Optional[int]:

							"""simple docstring"""






							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







@require_torch
class   A(						UpperCamelCase							,				UpperCamelCase							,				unittest.TestCase       ):



	'''simple docstring'''


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

	def     a__			(    self :  Optional[Any]						,							A_ :  str						,							A_ :  Any						,							A_ :  Tuple=False  )       ->     List[Any]:

							"""simple docstring"""






							lowerCamelCase_							=   super()._prepare_for_class(A_						,							A_						,							return_labels=A_  )

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

	def     a__			(    self :  int  )       ->     Dict:

							"""simple docstring"""






							lowerCamelCase_							=   MobileBertModelTester(self  )
							lowerCamelCase_							=   ConfigTester(self						,							config_class=A_						,							hidden_size=37  )

	def     a__			(    self :  Any  )       ->     Tuple:

							"""simple docstring"""






							self.config_tester.run_common_tests()

	def     a__			(    self :  str  )       ->     List[str]:

							"""simple docstring"""






							lowerCamelCase_							=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_model(*A_  )

	def     a__			(    self :  int  )       ->     str:

							"""simple docstring"""






							lowerCamelCase_							=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_masked_lm(*A_  )

	def     a__			(    self :  Tuple  )       ->     int:

							"""simple docstring"""






							lowerCamelCase_							=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_multiple_choice(*A_  )

	def     a__			(    self :  List[Any]  )       ->     Any:

							"""simple docstring"""






							lowerCamelCase_							=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*A_  )

	def     a__			(    self :  Any  )       ->     List[Any]:

							"""simple docstring"""






							lowerCamelCase_							=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_pretraining(*A_  )

	def     a__			(    self :  Dict  )       ->     str:

							"""simple docstring"""






							lowerCamelCase_							=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_question_answering(*A_  )

	def     a__			(    self :  Tuple  )       ->     str:

							"""simple docstring"""






							lowerCamelCase_							=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_sequence_classification(*A_  )





	def     a__			(    self :  Optional[Any]  )       ->     str:

							"""simple docstring"""






							lowerCamelCase_							=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_token_classification(*A_  )

def       _SCREAMING_SNAKE_CASE      (							lowercase     :	Union[str, Any]					):


						'''simple docstring'''



						return torch.tensor(
						    lowercase     ,							dtype=torch.long     ,							device=lowercase     ,							)


lowerCamelCase    :		str			   =       1e-3







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



	'''simple docstring'''


	@slow
	def     a__			(    self :  List[str]  )       ->     Union[str, Any]:

							"""simple docstring"""






							lowerCamelCase_							=   MobileBertModel.from_pretrained('google/mobilebert-uncased'  ).to(A_  )
							lowerCamelCase_							=   _long_tensor([[101, 7110, 1005, 1056, 2023, 11333, 17413, 1029, 102]]  )
							with torch.no_grad():
													lowerCamelCase_							=   model(A_  )[0]
							lowerCamelCase_							=   torch.Size((1, 9, 512)  )
							self.assertEqual(output.shape						,							A_  )
							lowerCamelCase_							=   torch.tensor(
							    [
							        [
							            [-2.4_736_526E07, 8.2_691_656E04, 1.6_521_838E05],
							            [-5.7_541_704E-01, 3.9_056_022E00, 4.4_011_507E00],
							            [2.6_047_359E00, 1.5_677_652E00, -1.7_324_188E-01],
							        ]
							    ]						,							device=A_						,							)

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

							self.assertTrue(lower_bound and upper_bound  )









from ..utils import DummyObject, requires_backends







class   A(						metaclass=UpperCamelCase       ):



 '''simple docstring'''


 UpperCamelCase						       =						['''keras_nlp''']

 def __init__(    self :  Optional[int]						,							*A_ :  Any						,							**A_ :  Dict  )       ->     Optional[int]:

       """simple docstring"""






       requires_backends(self						,							['keras_nlp']  )



