





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


							'''simple docstring'''



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






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


							'''simple docstring'''



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

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

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

							snake_case_						:    List[Any]				=  None
							snake_case_						:    Dict				=  None
							snake_case_						:    Tuple				=  None
							if self.use_labels:
											snake_case_						:    Any				=  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_						:    List[Any]				=  ids_tensor([self.batch_size]					,							self.num_choices    )

							snake_case_						:    Optional[int]				=  self.get_config()

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






			def 			lowerCamelCase  (self    )     ->   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__    )     ->   Dict:


							'''simple docstring'''



							snake_case_						:    List[str]				=  MobileBertModel(config=__magic_name__    )
							model.to(__magic_name__    )
							model.eval()
							snake_case_						:    Tuple				=  model(__magic_name__					,							attention_mask=__magic_name__					,							token_type_ids=__magic_name__    )
							snake_case_						:    Optional[int]				=  model(__magic_name__					,							token_type_ids=__magic_name__    )
							snake_case_						:    Optional[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__    )     ->   int:


							'''simple docstring'''



							snake_case_						:    Any				=  MobileBertForMaskedLM(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__    )
							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__    )     ->   List[str]:


							'''simple docstring'''



							snake_case_						:    int				=  MobileBertForNextSentencePrediction(config=__magic_name__    )
							model.to(__magic_name__    )
							model.eval()
							snake_case_						:    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__    )     ->   str:


							'''simple docstring'''



							snake_case_						:    Any				=  MobileBertForPreTraining(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__					,							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__    )     ->   List[str]:


							'''simple docstring'''



							snake_case_						:    Tuple				=  MobileBertForQuestionAnswering(config=__magic_name__    )
							model.to(__magic_name__    )
							model.eval()
							snake_case_						:    Any				=  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__    )     ->   str:


							'''simple docstring'''



							snake_case_						:    Optional[int]				=  self.num_labels
							snake_case_						:    Tuple				=  MobileBertForSequenceClassification(__magic_name__    )
							model.to(__magic_name__    )
							model.eval()
							snake_case_						:    List[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_						:    Dict				=  MobileBertForTokenClassification(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, self.seq_length, self.num_labels)    )






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


							'''simple docstring'''



							snake_case_						:    Tuple				=  self.num_choices
							snake_case_						:    str				=  MobileBertForMultipleChoice(config=__magic_name__    )
							model.to(__magic_name__    )
							model.eval()
							snake_case_						:    Dict				=  input_ids.unsqueeze(1    ).expand(-1					,							self.num_choices					,							-1    ).contiguous()
							snake_case_						:    Tuple				=  token_type_ids.unsqueeze(1    ).expand(-1					,							self.num_choices					,							-1    ).contiguous()
							snake_case_						:    int				=  input_mask.unsqueeze(1    ).expand(-1					,							self.num_choices					,							-1    ).contiguous()
							snake_case_						:    List[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_choices)    )






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


							'''simple docstring'''



							snake_case_						:    Optional[int]				=  self.prepare_config_and_inputs()
							(
							    (
							    snake_case_
							)			,							(
							    snake_case_
							)			,							(
							    snake_case_
							)			,							(
							    snake_case_
							)			,							(
							    snake_case_
							)			,							(
							    snake_case_
							)			,							(
							    snake_case_
							)			,							
							)						:    List[Any]				=  config_and_inputs
							snake_case_						:    List[str]				=  {'''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_     :     int       			=      (
			    (
			        MobileBertModel,
			        MobileBertForMaskedLM,
			        MobileBertForMultipleChoice,
			        MobileBertForNextSentencePrediction,
			        MobileBertForPreTraining,
			        MobileBertForQuestionAnswering,
			        MobileBertForSequenceClassification,
			        MobileBertForTokenClassification,
			    )
			    if is_torch_available()
			    else ()
			)
			lowerCamelCase_     :     Union[str, Any]       			=      (
			    {
			        '''feature-extraction''': MobileBertModel,
			        '''fill-mask''': MobileBertForMaskedLM,
			        '''question-answering''': MobileBertForQuestionAnswering,
			        '''text-classification''': MobileBertForSequenceClassification,
			        '''token-classification''': MobileBertForTokenClassification,
			        '''zero-shot''': MobileBertForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)
			lowerCamelCase_     :     Union[str, Any]       			=      True






			def 			lowerCamelCase  (self					,							__magic_name__					,							__magic_name__					,							__magic_name__=False    )     ->   List[str]:


							'''simple docstring'''



							snake_case_						:    Dict				=  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_						:    Tuple				=  torch.zeros(
															    (self.model_tester.batch_size, self.model_tester.seq_length)					,							dtype=torch.long					,							device=__magic_name__    )
															snake_case_						:    int				=  torch.zeros(
															    self.model_tester.batch_size					,							dtype=torch.long					,							device=__magic_name__    )
							return inputs_dict






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


							'''simple docstring'''



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






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


							'''simple docstring'''



							self.config_tester.run_common_tests()






			def 			lowerCamelCase  (self    )     ->   Tuple:


							'''simple docstring'''



							snake_case_						:    Union[str, Any]				=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_model(*__magic_name__    )






			def 			lowerCamelCase  (self    )     ->   Any:


							'''simple docstring'''



							snake_case_						:    Optional[int]				=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_masked_lm(*__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_multiple_choice(*__magic_name__    )






			def 			lowerCamelCase  (self    )     ->   str:


							'''simple docstring'''



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






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


							'''simple docstring'''



							snake_case_						:    List[str]				=  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    )     ->   Dict:


							'''simple docstring'''



							snake_case_						:    Optional[Any]				=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_sequence_classification(*__magic_name__    )






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


							'''simple docstring'''



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






def      lowerCamelCase_						(				_UpperCamelCase )		->						Optional[int]:






				"""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    )     ->   Optional[Any]:


							'''simple docstring'''



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

							self.assertTrue(lower_bound and upper_bound    )







from math import factorial

lowerCAmelCase_    =				{str(digit): factorial(digit) for digit in range(1_0)}






def      lowerCamelCase_						(				_UpperCamelCase )		->						int:






				"""simple docstring"""


				if not isinstance(_UpperCamelCase ,				_UpperCamelCase ):
								raise TypeError('''Parameter number must be int''' )

				if number < 0:
								raise ValueError('''Parameter number must be greater than or equal to 0''' )

				# Converts number in string to iterate on its digits and adds its factorial.
				return sum(DIGIT_FACTORIAL[digit] for digit in str(_UpperCamelCase ) )






def      lowerCamelCase_						(				_UpperCamelCase = 60 ,				_UpperCamelCase = 1_000_000 )		->						int:






				"""simple docstring"""


				if not isinstance(_UpperCamelCase ,				_UpperCamelCase ) or not isinstance(_UpperCamelCase ,				_UpperCamelCase ):
								raise TypeError('''Parameters chain_length and number_limit must be int''' )

				if chain_length <= 0 or number_limit <= 0:
								raise ValueError(
								    '''Parameters chain_length and number_limit must be greater than 0''' )

				# the counter for the chains with the exact desired length
				snake_case_						:    Optional[Any]				=  0
				# the cached sizes of the previous chains
				snake_case_						:    dict[int, int]				=  {}

				for start_chain_element in range(1 ,				_UpperCamelCase ):
								# The temporary set will contain the elements of the chain
								snake_case_						:    List[str]				=  set()
								snake_case_						:    List[Any]				=  0

								# Stop computing the chain when you find a cached size, a repeating item or the
								# length is greater then the desired one.
								snake_case_						:    Any				=  start_chain_element
								while (
								    chain_element not in chain_sets_lengths
								    and chain_element not in chain_set
								    and chain_set_length <= chain_length
								):
												chain_set.add(_UpperCamelCase )
												chain_set_length += 1
												snake_case_						:    List[Any]				=  digit_factorial_sum(_UpperCamelCase )

								if chain_element in chain_sets_lengths:
												chain_set_length += chain_sets_lengths[chain_element]

								snake_case_						:    List[str]				=  chain_set_length

								# If chain contains the exact amount of elements increase the counter
								if chain_set_length == chain_length:
												chains_counter += 1

				return chains_counter


if __name__ == "__main__":
	import doctest

	doctest.testmod()
	print(F'''{solution()}''')
