





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






			def __init__(self ,							__magic_name__ ,							__magic_name__=13 ,							__magic_name__=7 ,							__magic_name__=True ,							__magic_name__=True ,							__magic_name__=True ,							__magic_name__=True ,							__magic_name__=99 ,							__magic_name__=64 ,							__magic_name__=32 ,							__magic_name__=5 ,							__magic_name__=4 ,							__magic_name__=37 ,							__magic_name__="gelu" ,							__magic_name__=0.1 ,							__magic_name__=0.1 ,							__magic_name__=512 ,							__magic_name__=16 ,							__magic_name__=2 ,							__magic_name__=0.02 ,							__magic_name__=3 ,							__magic_name__=4 ,							__magic_name__=None ,							)					->       Any:


							'''simple docstring'''



							snake_case_						:   Dict           =  parent
							snake_case_						:   Dict           =  batch_size
							snake_case_						:   Optional[Any]           =  seq_length
							snake_case_						:   int           =  is_training
							snake_case_						:   Dict           =  use_input_mask
							snake_case_						:   Any           =  use_token_type_ids
							snake_case_						:   Union[str, Any]           =  use_labels
							snake_case_						:   int           =  vocab_size
							snake_case_						:   Union[str, Any]           =  hidden_size
							snake_case_						:   List[Any]           =  embedding_size
							snake_case_						:   List[Any]           =  num_hidden_layers
							snake_case_						:   str           =  num_attention_heads
							snake_case_						:   Any           =  intermediate_size
							snake_case_						:   int           =  hidden_act
							snake_case_						:   Any           =  hidden_dropout_prob
							snake_case_						:   Optional[Any]           =  attention_probs_dropout_prob
							snake_case_						:   Union[str, Any]           =  max_position_embeddings
							snake_case_						:   List[Any]           =  type_vocab_size
							snake_case_						:   Tuple           =  type_sequence_label_size
							snake_case_						:   Optional[Any]           =  initializer_range
							snake_case_						:   int           =  num_labels
							snake_case_						:   Any           =  num_choices
							snake_case_						:   Optional[Any]           =  scope






			def       lowerCamelCase  (self   )					->       Any:


							'''simple docstring'''



							snake_case_						:   Optional[Any]           =  ids_tensor([self.batch_size, self.seq_length] ,							self.vocab_size   )

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

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

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

							snake_case_						:   List[Any]           =  self.get_config()

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






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


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






			def       lowerCamelCase  (self ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__   )					->       int:


							'''simple docstring'''



							snake_case_						:   str           =  MobileBertModel(config=__magic_name__   )
							model.to(__magic_name__   )
							model.eval()
							snake_case_						:   Optional[Any]           =  model(__magic_name__ ,							attention_mask=__magic_name__ ,							token_type_ids=__magic_name__   )
							snake_case_						:   Optional[Any]           =  model(__magic_name__ ,							token_type_ids=__magic_name__   )
							snake_case_						:   Any           =  model(__magic_name__   )

							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 ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__   )					->       str:


							'''simple docstring'''



							snake_case_						:   Tuple           =  MobileBertForMaskedLM(config=__magic_name__   )
							model.to(__magic_name__   )
							model.eval()
							snake_case_						:   Union[str, Any]           =  model(__magic_name__ ,							attention_mask=__magic_name__ ,							token_type_ids=__magic_name__ ,							labels=__magic_name__   )
							self.parent.assertEqual(result.logits.shape ,							(self.batch_size, self.seq_length, self.vocab_size)   )






			def       lowerCamelCase  (self ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__   )					->       Tuple:


							'''simple docstring'''



							snake_case_						:   List[str]           =  MobileBertForNextSentencePrediction(config=__magic_name__   )
							model.to(__magic_name__   )
							model.eval()
							snake_case_						:   List[str]           =  model(
							    __magic_name__ ,							attention_mask=__magic_name__ ,							token_type_ids=__magic_name__ ,							labels=__magic_name__ ,							)
							self.parent.assertEqual(result.logits.shape ,							(self.batch_size, 2)   )






			def       lowerCamelCase  (self ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__   )					->       Tuple:


							'''simple docstring'''



							snake_case_						:   int           =  MobileBertForPreTraining(config=__magic_name__   )
							model.to(__magic_name__   )
							model.eval()
							snake_case_						:   Dict           =  model(
							    __magic_name__ ,							attention_mask=__magic_name__ ,							token_type_ids=__magic_name__ ,							labels=__magic_name__ ,							next_sentence_label=__magic_name__ ,							)
							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 ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__   )					->       str:


							'''simple docstring'''



							snake_case_						:   Union[str, Any]           =  MobileBertForQuestionAnswering(config=__magic_name__   )
							model.to(__magic_name__   )
							model.eval()
							snake_case_						:   List[str]           =  model(
							    __magic_name__ ,							attention_mask=__magic_name__ ,							token_type_ids=__magic_name__ ,							start_positions=__magic_name__ ,							end_positions=__magic_name__ ,							)
							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 ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__   )					->       Union[str, Any]:


							'''simple docstring'''



							snake_case_						:   str           =  self.num_labels
							snake_case_						:   Tuple           =  MobileBertForSequenceClassification(__magic_name__   )
							model.to(__magic_name__   )
							model.eval()
							snake_case_						:   Union[str, Any]           =  model(__magic_name__ ,							attention_mask=__magic_name__ ,							token_type_ids=__magic_name__ ,							labels=__magic_name__   )
							self.parent.assertEqual(result.logits.shape ,							(self.batch_size, self.num_labels)   )






			def       lowerCamelCase  (self ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__   )					->       Optional[Any]:


							'''simple docstring'''



							snake_case_						:   int           =  self.num_labels
							snake_case_						:   Optional[Any]           =  MobileBertForTokenClassification(config=__magic_name__   )
							model.to(__magic_name__   )
							model.eval()
							snake_case_						:   Optional[int]           =  model(__magic_name__ ,							attention_mask=__magic_name__ ,							token_type_ids=__magic_name__ ,							labels=__magic_name__   )
							self.parent.assertEqual(result.logits.shape ,							(self.batch_size, self.seq_length, self.num_labels)   )






			def       lowerCamelCase  (self ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__   )					->       str:


							'''simple docstring'''



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






			def       lowerCamelCase  (self   )					->       Dict:


							'''simple docstring'''



							snake_case_						:   List[Any]           =  self.prepare_config_and_inputs()
							(
							    snake_case_							
							)						:   int           =  config_and_inputs
							snake_case_						:   List[Any]           =  {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
							return config, inputs_dict




@require_torch
class  __lowerCAmelCase	(      _a,					_a,					unittest.TestCase					):
			lowerCamelCase_					:					Optional[Any]	      =				(
			    (
			        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_					:					Dict	      =				True






			def       lowerCamelCase  (self ,							__magic_name__ ,							__magic_name__ ,							__magic_name__=False   )					->       Optional[int]:


							'''simple docstring'''



							snake_case_						:   Tuple           =  super()._prepare_for_class(__magic_name__ ,							__magic_name__ ,							return_labels=__magic_name__   )

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






			def       lowerCamelCase  (self   )					->       Any:


							'''simple docstring'''



							snake_case_						:   Union[str, Any]           =  MobileBertModelTester(self   )
							snake_case_						:   Any           =  ConfigTester(self ,							config_class=__magic_name__ ,							hidden_size=37   )






			def       lowerCamelCase  (self   )					->       Any:


							'''simple docstring'''



							self.config_tester.run_common_tests()






			def       lowerCamelCase  (self   )					->       int:


							'''simple docstring'''



							snake_case_						:   Tuple           =  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_model(*__magic_name__   )






			def       lowerCamelCase  (self   )					->       Dict:


							'''simple docstring'''



							snake_case_						:   str           =  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_masked_lm(*__magic_name__   )






			def       lowerCamelCase  (self   )					->       Tuple:


							'''simple docstring'''



							snake_case_						:   List[str]           =  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_multiple_choice(*__magic_name__   )






			def       lowerCamelCase  (self   )					->       Optional[Any]:


							'''simple docstring'''



							snake_case_						:   Any           =  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*__magic_name__   )






			def       lowerCamelCase  (self   )					->       int:


							'''simple docstring'''



							snake_case_						:   List[Any]           =  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_pretraining(*__magic_name__   )






			def       lowerCamelCase  (self   )					->       str:


							'''simple docstring'''



							snake_case_						:   Dict           =  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_question_answering(*__magic_name__   )






			def       lowerCamelCase  (self   )					->       Tuple:


							'''simple docstring'''



							snake_case_						:   Dict           =  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_sequence_classification(*__magic_name__   )






			def       lowerCamelCase  (self   )					->       Optional[Any]:


							'''simple docstring'''



							snake_case_						:   Dict           =  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_token_classification(*__magic_name__   )






def 					lowerCamelCase_						(    _UpperCamelCase			)		->						List[str]:






				"""simple docstring"""


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


lowerCAmelCase_    =    1e-3





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






			@slow
			def       lowerCamelCase  (self   )					->       Tuple:


							'''simple docstring'''



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

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

							self.assertTrue(lower_bound and upper_bound   )







from collections.abc import Iterator, MutableMapping
from dataclasses import dataclass
from typing import Generic, TypeVar

lowerCAmelCase_    =    TypeVar('''KEY''')
lowerCAmelCase_    =    TypeVar('''VAL''')



@dataclass(frozen=_a,					slots=_a					)
class  __lowerCAmelCase	(      Generic[KEY, VAL]					):
			lowerCamelCase_					:					KEY
			lowerCamelCase_					:					VAL




class  __lowerCAmelCase	(      _Item					):






			def __init__(self   )					->       None:


							'''simple docstring'''



							super().__init__(__magic_name__ ,							__magic_name__   )






			def __bool__(self   )					->       bool:


							'''simple docstring'''



							return False




lowerCAmelCase_    =    _DeletedItem()



class  __lowerCAmelCase	(      MutableMapping[KEY, VAL]					):






			def __init__(self ,							__magic_name__ = 8 ,							__magic_name__ = 0.75   )					->       None:


							'''simple docstring'''



							snake_case_						:   List[Any]           =  initial_block_size
							snake_case_						:   list[_Item | None]           =  [None] * initial_block_size
							assert 0.0 < capacity_factor < 1.0
							snake_case_						:   List[str]           =  capacity_factor
							snake_case_						:   int           =  0






			def       lowerCamelCase  (self ,							__magic_name__   )					->       int:


							'''simple docstring'''



							return hash(__magic_name__   ) % len(self._buckets   )






			def       lowerCamelCase  (self ,							__magic_name__   )					->       int:


							'''simple docstring'''



							return (ind + 1) % len(self._buckets   )






			def       lowerCamelCase  (self ,							__magic_name__ ,							__magic_name__ ,							__magic_name__   )					->       bool:


							'''simple docstring'''



							snake_case_						:   Optional[int]           =  self._buckets[ind]
							if not stored:
											snake_case_						:   Optional[Any]           =  _Item(__magic_name__ ,							__magic_name__   )
											self._len += 1
											return True
							elif stored.key == key:
											snake_case_						:   List[Any]           =  _Item(__magic_name__ ,							__magic_name__   )
											return True
							else:
											return False






			def       lowerCamelCase  (self   )					->       bool:


							'''simple docstring'''



							snake_case_						:   Any           =  len(self._buckets   ) * self._capacity_factor
							return len(self   ) >= int(__magic_name__   )






			def       lowerCamelCase  (self   )					->       bool:


							'''simple docstring'''



							if len(self._buckets   ) <= self._initial_block_size:
											return False
							snake_case_						:   int           =  len(self._buckets   ) * self._capacity_factor / 2
							return len(self   ) < limit






			def       lowerCamelCase  (self ,							__magic_name__   )					->       None:


							'''simple docstring'''



							snake_case_						:   List[str]           =  self._buckets
							snake_case_						:   int           =  [None] * new_size
							snake_case_						:   Optional[int]           =  0
							for item in old_buckets:
											if item:
															self._add_item(item.key ,							item.val   )






			def       lowerCamelCase  (self   )					->       None:


							'''simple docstring'''



							self._resize(len(self._buckets   ) * 2   )






			def       lowerCamelCase  (self   )					->       None:


							'''simple docstring'''



							self._resize(len(self._buckets   ) // 2   )






			def       lowerCamelCase  (self ,							__magic_name__   )					->       Iterator[int]:


							'''simple docstring'''



							snake_case_						:   Dict           =  self._get_bucket_index(__magic_name__   )
							for _ in range(len(self._buckets   )   ):
											yield ind
											snake_case_						:   Tuple           =  self._get_next_ind(__magic_name__   )






			def       lowerCamelCase  (self ,							__magic_name__ ,							__magic_name__   )					->       None:


							'''simple docstring'''



							for ind in self._iterate_buckets(__magic_name__   ):
											if self._try_set(__magic_name__ ,							__magic_name__ ,							__magic_name__   ):
															break






			def __setitem__(self ,							__magic_name__ ,							__magic_name__   )					->       None:


							'''simple docstring'''



							if self._is_full():
											self._size_up()

							self._add_item(__magic_name__ ,							__magic_name__   )






			def __delitem__(self ,							__magic_name__   )					->       None:


							'''simple docstring'''



							for ind in self._iterate_buckets(__magic_name__   ):
											snake_case_						:   Optional[Any]           =  self._buckets[ind]
											if item is None:
															raise KeyError(__magic_name__   )
											if item is _deleted:
															continue
											if item.key == key:
															snake_case_						:   Union[str, Any]           =  _deleted
															self._len -= 1
															break
							if self._is_sparse():
											self._size_down()






			def __getitem__(self ,							__magic_name__   )					->       VAL:


							'''simple docstring'''



							for ind in self._iterate_buckets(__magic_name__   ):
											snake_case_						:   List[Any]           =  self._buckets[ind]
											if item is None:
															break
											if item is _deleted:
															continue
											if item.key == key:
															return item.val
							raise KeyError(__magic_name__   )






			def __len__(self   )					->       int:


							'''simple docstring'''



							return self._len






			def __iter__(self   )					->       Iterator[KEY]:


							'''simple docstring'''



							yield from (item.key for item in self._buckets if item)






			def __repr__(self   )					->       str:


							'''simple docstring'''



							snake_case_						:   List[str]           =  ''' ,'''.join(
							    F'''{item.key}: {item.val}''' for item in self._buckets if item   )
							return F'''HashMap({val_string})'''
