






'''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 	SCREAMING_SNAKE_CASE					:






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






			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[Any]					)			->			Union[str, Any]:
							a_		:  List[Any]											=      ids_tensor([self.batch_size, self.seq_length]  ,						self.vocab_size					)

							a_		:  List[Any]											=      None
							if self.use_input_mask:
											a_		:  int											=      random_attention_mask([self.batch_size, self.seq_length]					)

							a_		:  str											=      None
							if self.use_token_type_ids:
											a_		:  Union[str, Any]											=      ids_tensor([self.batch_size, self.seq_length]  ,						self.type_vocab_size					)

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

							a_		:  Union[str, Any]											=      self.get_config()

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






			def 			SCREAMING_SNAKE_CASE      ( self      :			Dict					)			->			Any:
							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=__SCREAMING_SNAKE_CASE  ,						initializer_range=self.initializer_range  ,						)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Dict  ,						__SCREAMING_SNAKE_CASE      :			Tuple  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]  ,						__SCREAMING_SNAKE_CASE      :			Tuple  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]  ,						__SCREAMING_SNAKE_CASE      :			Dict  ,						__SCREAMING_SNAKE_CASE      :			Dict  ,						__SCREAMING_SNAKE_CASE      :			List[str]					)			->			Tuple:
							a_		:  List[str]											=      MobileBertModel(config=__SCREAMING_SNAKE_CASE					)
							model.to(__SCREAMING_SNAKE_CASE					)
							model.eval()
							a_		:  List[str]											=      model(__SCREAMING_SNAKE_CASE  ,						attention_mask=__SCREAMING_SNAKE_CASE  ,						token_type_ids=__SCREAMING_SNAKE_CASE					)
							a_		:  Tuple											=      model(__SCREAMING_SNAKE_CASE  ,						token_type_ids=__SCREAMING_SNAKE_CASE					)
							a_		:  List[Any]											=      model(__SCREAMING_SNAKE_CASE					)

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






			def 			SCREAMING_SNAKE_CASE      ( self      :			List[Any]  ,						__SCREAMING_SNAKE_CASE      :			Any  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]  ,						__SCREAMING_SNAKE_CASE      :			Dict  ,						__SCREAMING_SNAKE_CASE      :			str  ,						__SCREAMING_SNAKE_CASE      :			Union[str, Any]  ,						__SCREAMING_SNAKE_CASE      :			List[str]  ,						__SCREAMING_SNAKE_CASE      :			Tuple					)			->			Any:
							a_		:  int											=      MobileBertForNextSentencePrediction(config=__SCREAMING_SNAKE_CASE					)
							model.to(__SCREAMING_SNAKE_CASE					)
							model.eval()
							a_		:  Union[str, Any]											=      model(
							    __SCREAMING_SNAKE_CASE  ,						attention_mask=__SCREAMING_SNAKE_CASE  ,						token_type_ids=__SCREAMING_SNAKE_CASE  ,						labels=__SCREAMING_SNAKE_CASE  ,						)
							self.parent.assertEqual(result.logits.shape  ,						(self.batch_size, 2)					)






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






			def 			SCREAMING_SNAKE_CASE      ( self      :			List[str]  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]  ,						__SCREAMING_SNAKE_CASE      :			List[Any]  ,						__SCREAMING_SNAKE_CASE      :			Any  ,						__SCREAMING_SNAKE_CASE      :			str					)			->			List[Any]:
							a_		:  Optional[int]											=      self.num_labels
							a_		:  Tuple											=      MobileBertForTokenClassification(config=__SCREAMING_SNAKE_CASE					)
							model.to(__SCREAMING_SNAKE_CASE					)
							model.eval()
							a_		:  str											=      model(__SCREAMING_SNAKE_CASE  ,						attention_mask=__SCREAMING_SNAKE_CASE  ,						token_type_ids=__SCREAMING_SNAKE_CASE  ,						labels=__SCREAMING_SNAKE_CASE					)
							self.parent.assertEqual(result.logits.shape  ,						(self.batch_size, self.seq_length, self.num_labels)					)






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







			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[int]					)			->			Optional[int]:
							a_		:  Tuple											=      self.prepare_config_and_inputs()
							(
							    (
							    a_
							)				,			(
							    a_
							)				,			(
							    a_
							)				,			(
							    a_
							)				,			(
							    a_
							)				,			(
							    a_
							)				,			(
							    a_
							)				,			
							)		:  List[str]											=      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 	SCREAMING_SNAKE_CASE					(					SCREAMING_SNAKE_CASE_	,   SCREAMING_SNAKE_CASE_	,   unittest.TestCase       ):
			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 			SCREAMING_SNAKE_CASE      ( self      :			Any  ,						__SCREAMING_SNAKE_CASE      :			Dict  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]  ,						__SCREAMING_SNAKE_CASE      :			Dict=False					)			->			List[str]:
							a_		:  int											=      super()._prepare_for_class(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE  ,						return_labels=__SCREAMING_SNAKE_CASE					)

							if return_labels:
											if model_class in get_values(__SCREAMING_SNAKE_CASE					):
															a_		:  List[Any]											=      torch.zeros(
															    (self.model_tester.batch_size, self.model_tester.seq_length)  ,						dtype=torch.long  ,						device=__SCREAMING_SNAKE_CASE					)
															a_		:  int											=      torch.zeros(
															    self.model_tester.batch_size  ,						dtype=torch.long  ,						device=__SCREAMING_SNAKE_CASE					)
							return inputs_dict






			def 			SCREAMING_SNAKE_CASE      ( self      :			Any					)			->			int:
							a_		:  Dict											=      MobileBertModelTester(self					)
							a_		:  Union[str, Any]											=      ConfigTester(self  ,						config_class=__SCREAMING_SNAKE_CASE  ,						hidden_size=37					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[int]					)			->			str:
							self.config_tester.run_common_tests()






			def 			SCREAMING_SNAKE_CASE      ( self      :			List[str]					)			->			int:
							a_		:  str											=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_model(*__SCREAMING_SNAKE_CASE					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Union[str, Any]					)			->			Dict:
							a_		:  Union[str, Any]											=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_masked_lm(*__SCREAMING_SNAKE_CASE					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Dict					)			->			Union[str, Any]:
							a_		:  int											=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_multiple_choice(*__SCREAMING_SNAKE_CASE					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[int]					)			->			str:
							a_		:  List[Any]											=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*__SCREAMING_SNAKE_CASE					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Union[str, Any]					)			->			Tuple:
							a_		:  Optional[int]											=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_pretraining(*__SCREAMING_SNAKE_CASE					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[Any]					)			->			int:
							a_		:  List[str]											=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_question_answering(*__SCREAMING_SNAKE_CASE					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			int					)			->			Optional[Any]:
							a_		:  List[Any]											=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_sequence_classification(*__SCREAMING_SNAKE_CASE					)







			def 			SCREAMING_SNAKE_CASE      ( self      :			List[Any]					)			->			List[str]:
							a_		:  Union[str, Any]											=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_token_classification(*__SCREAMING_SNAKE_CASE					)





def 		_UpperCAmelCase   (							__A							:   Dict      ):
				return torch.tensor(
				    __A						,		dtype=torch.long						,		device=__A						,		)


__lowerCAmelCase       		=      1E-3





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






			@slow
			def 			SCREAMING_SNAKE_CASE      ( self      :			Any					)			->			Optional[Any]:
							a_		:  Union[str, Any]											=      MobileBertModel.from_pretrained('''google/mobilebert-uncased'''					).to(__SCREAMING_SNAKE_CASE					)
							a_		:  Dict											=      _long_tensor([[101, 7110, 1005, 1056, 2023, 1_1333, 1_7413, 1029, 102]]					)
							with torch.no_grad():
											a_		:  List[str]											=      model(__SCREAMING_SNAKE_CASE					)[0]
							a_		:  List[Any]											=      torch.Size((1, 9, 512)					)
							self.assertEqual(output.shape  ,						__SCREAMING_SNAKE_CASE					)
							a_		:  List[str]											=      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=__SCREAMING_SNAKE_CASE  ,						)

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

							self.assertTrue(lower_bound and upper_bound					)












'''simple docstring'''







import warnings


warnings.warn(
    'memory_utils has been reorganized to utils.memory. Import `find_executable_batchsize` from the main `__init__`: '
    '`from accelerate import find_executable_batch_size` to avoid this warning.',
    FutureWarning,
)




