


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

     """simple docstring"""






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


          '''simple docstring'''





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


     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          __A	:       List[str]      =							ids_tensor([self.batch_size, self.seq_length]			,     self.vocab_size		)

          __A	:       Dict      =							None
          if self.use_input_mask:
               __A	:       int      =							random_attention_mask([self.batch_size, self.seq_length]		)

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

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

          __A	:       Tuple      =							self.get_config()

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


     def lowerCAmelCase__  (   self		):


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


     def lowerCAmelCase__  (   self			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase		):


          '''simple docstring'''





          __A	:       List[Any]      =							MobileBertModel(config=lowerCamelCase		)
          model.to(lowerCamelCase		)
          model.eval()
          __A	:       int      =							model(lowerCamelCase			,     attention_mask=lowerCamelCase			,     token_type_ids=lowerCamelCase		)
          __A	:       Dict      =							model(lowerCamelCase			,     token_type_ids=lowerCamelCase		)
          __A	:       Dict      =							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			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase		):


          '''simple docstring'''





          __A	:       int      =							MobileBertForMaskedLM(config=lowerCamelCase		)
          model.to(lowerCamelCase		)
          model.eval()
          __A	:       Any      =							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			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase		):


          '''simple docstring'''





          __A	:       Tuple      =							MobileBertForNextSentencePrediction(config=lowerCamelCase		)
          model.to(lowerCamelCase		)
          model.eval()
          __A	:       Union[str, Any]      =							model(
              lowerCamelCase			,     attention_mask=lowerCamelCase			,     token_type_ids=lowerCamelCase			,     labels=lowerCamelCase			,     )
          self.parent.assertEqual(result.logits.shape			,     (self.batch_size, 2)		)


     def lowerCAmelCase__  (   self			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase		):


          '''simple docstring'''





          __A	:       Union[str, Any]      =							MobileBertForPreTraining(config=lowerCamelCase		)
          model.to(lowerCamelCase		)
          model.eval()
          __A	:       Any      =							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			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase		):


          '''simple docstring'''





          __A	:       List[str]      =							MobileBertForQuestionAnswering(config=lowerCamelCase		)
          model.to(lowerCamelCase		)
          model.eval()
          __A	:       str      =							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			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase		):


          '''simple docstring'''





          __A	:       List[Any]      =							self.num_labels
          __A	:       Dict      =							MobileBertForSequenceClassification(lowerCamelCase		)
          model.to(lowerCamelCase		)
          model.eval()
          __A	:       int      =							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			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase		):


          '''simple docstring'''





          __A	:       List[Any]      =							self.num_labels
          __A	:       Optional[Any]      =							MobileBertForTokenClassification(config=lowerCamelCase		)
          model.to(lowerCamelCase		)
          model.eval()
          __A	:       List[str]      =							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			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase		):


          '''simple docstring'''





          __A	:       Dict      =							self.num_choices
          __A	:       str      =							MobileBertForMultipleChoice(config=lowerCamelCase		)
          model.to(lowerCamelCase		)
          model.eval()
          __A	:       Any      =							input_ids.unsqueeze(1		).expand(-1			,     self.num_choices			,     -1		).contiguous()
          __A	:       Dict      =							token_type_ids.unsqueeze(1		).expand(-1			,     self.num_choices			,     -1		).contiguous()
          __A	:       Any      =							input_mask.unsqueeze(1		).expand(-1			,     self.num_choices			,     -1		).contiguous()
          __A	:       Optional[int]      =							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		):


          '''simple docstring'''





          __A	:       Tuple      =							self.prepare_config_and_inputs()
          (
              (
              __A
          )  ,(
              __A
          )  ,(
              __A
          )  ,(
              __A
          )  ,(
              __A
          )  ,(
              __A
          )  ,(
              __A
          )  ,
          )	:       Dict      =							config_and_inputs
          __A	:       Optional[Any]      =							{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
          return config, inputs_dict







@require_torch
class 							__magic_name__	(       lowerCAmelCase    ,		lowerCAmelCase    ,		unittest.TestCase ):

     """simple docstring"""






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


     def lowerCAmelCase__  (   self			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase=False		):


          '''simple docstring'''





          __A	:       Optional[Any]      =							super()._prepare_for_class(lowerCamelCase			,     lowerCamelCase			,     return_labels=lowerCamelCase		)

          if return_labels:
               if model_class in get_values(lowerCamelCase		):
                    __A	:       List[str]      =							torch.zeros(
                        (self.model_tester.batch_size, self.model_tester.seq_length)			,     dtype=torch.long			,     device=lowerCamelCase		)
                    __A	:       List[str]      =							torch.zeros(
                        self.model_tester.batch_size			,     dtype=torch.long			,     device=lowerCamelCase		)
          return inputs_dict


     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          __A	:       List[Any]      =							MobileBertModelTester(self		)
          __A	:       List[str]      =							ConfigTester(self			,     config_class=lowerCamelCase			,     hidden_size=37		)


     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          self.config_tester.run_common_tests()


     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          __A	:       List[str]      =							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_model(*lowerCamelCase		)


     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          __A	:       Optional[Any]      =							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_masked_lm(*lowerCamelCase		)


     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          __A	:       Tuple      =							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_multiple_choice(*lowerCamelCase		)


     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          __A	:       List[str]      =							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*lowerCamelCase		)


     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          __A	:       Optional[int]      =							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_pretraining(*lowerCamelCase		)


     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          __A	:       Tuple      =							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_question_answering(*lowerCamelCase		)


     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          __A	:       Optional[int]      =							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_sequence_classification(*lowerCamelCase		)


     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          __A	:       Optional[int]      =							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_token_classification(*lowerCamelCase		)


def 		_lowercase (SCREAMING_SNAKE_CASE							):




     '''simple docstring'''





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


_UpperCamelCase 							=  1e-3







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

     """simple docstring"""






     @slow
     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





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

          self.assertTrue(lower_bound and upper_bound		)





'''simple docstring'''


def 		_lowercase (SCREAMING_SNAKE_CASE							):




     '''simple docstring'''





     __A	:       List[str]      =							1
     __A	:       Union[str, Any]      =							2
     while i * i <= n:
          __A	:       List[Any]      =							0
          while n % i == 0:
               n //= i
               multiplicity += 1
          n_divisors *= multiplicity + 1
          i += 1
     if n > 1:
          n_divisors *= 2
     return n_divisors


def 		_lowercase ():




     '''simple docstring'''





     __A	:       Optional[Any]      =							1
     __A	:       Dict      =							1

     while True:
          i += 1
          t_num += i

          if count_divisors(SCREAMING_SNAKE_CASE							) > 500:
               break

     return t_num


if __name__ == "__main__":
      print(solution())

