


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







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







     def 							lowercase__       (						self :						List[Any]      ):
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		ids_tensor([self.batch_size, self.seq_length]   ,   self.vocab_size      )

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

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

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

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

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







     def 							lowercase__       (						self :						int      ):
           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=snake_case__   ,   initializer_range=self.initializer_range   ,   )







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







     def 							lowercase__       (						self :						str   ,   _lowercase :						int   ,   _lowercase :						Dict   ,   _lowercase :						List[str]   ,   _lowercase :						Optional[int]   ,   _lowercase :						int   ,   _lowercase :						Any   ,   _lowercase :						str      ):
           SCREAMING_SNAKE_CASE__       :  Tuple						     =		MobileBertForNextSentencePrediction(config=snake_case__      )
           model.to(snake_case__      )
           model.eval()
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		model(
               snake_case__   ,   attention_mask=snake_case__   ,   token_type_ids=snake_case__   ,   labels=snake_case__   ,   )
           self.parent.assertEqual(result.logits.shape   ,   (self.batch_size, 2)      )







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







     def 							lowercase__       (						self :						str   ,   _lowercase :						Union[str, Any]   ,   _lowercase :						Optional[int]   ,   _lowercase :						List[Any]   ,   _lowercase :						Union[str, Any]   ,   _lowercase :						List[Any]   ,   _lowercase :						Dict   ,   _lowercase :						List[str]      ):
           SCREAMING_SNAKE_CASE__       :  Any						     =		self.num_labels
           SCREAMING_SNAKE_CASE__       :  Optional[Any]						     =		MobileBertForTokenClassification(config=snake_case__      )
           model.to(snake_case__      )
           model.eval()
           SCREAMING_SNAKE_CASE__       :  int						     =		model(snake_case__   ,   attention_mask=snake_case__   ,   token_type_ids=snake_case__   ,   labels=snake_case__      )
           self.parent.assertEqual(result.logits.shape   ,   (self.batch_size, self.seq_length, self.num_labels)      )







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







     def 							lowercase__       (						self :						List[str]      ):
           SCREAMING_SNAKE_CASE__       :  Any						     =		self.prepare_config_and_inputs()
           (
               SCREAMING_SNAKE_CASE__   
           )       :  Optional[int]						     =		config_and_inputs
           SCREAMING_SNAKE_CASE__       :  Dict						     =		{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
           return config, inputs_dict




@require_torch
class 							lowercase  (						A_    ,       A_    ,       unittest.TestCase			):
     lowerCamelCase			:			List[str]      = (
         (
             MobileBertModel,
             MobileBertForMaskedLM,
             MobileBertForMultipleChoice,
             MobileBertForNextSentencePrediction,
             MobileBertForPreTraining,
             MobileBertForQuestionAnswering,
             MobileBertForSequenceClassification,
             MobileBertForTokenClassification,
         )
         if is_torch_available()
         else ()
     )
     lowerCamelCase			:			int      = (
         {
             "feature-extraction": MobileBertModel,
             "fill-mask": MobileBertForMaskedLM,
             "question-answering": MobileBertForQuestionAnswering,
             "text-classification": MobileBertForSequenceClassification,
             "token-classification": MobileBertForTokenClassification,
             "zero-shot": MobileBertForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )
     lowerCamelCase			:			int      = True







     def 							lowercase__       (						self :						Any   ,   _lowercase :						List[Any]   ,   _lowercase :						Optional[int]   ,   _lowercase :						Optional[Any]=False      ):
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		super()._prepare_for_class(snake_case__   ,   snake_case__   ,   return_labels=snake_case__      )

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







     def 							lowercase__       (						self :						Union[str, Any]      ):
           SCREAMING_SNAKE_CASE__       :  Dict						     =		MobileBertModelTester(self      )
           SCREAMING_SNAKE_CASE__       :  Optional[int]						     =		ConfigTester(self   ,   config_class=snake_case__   ,   hidden_size=37      )







     def 							lowercase__       (						self :						Optional[Any]      ):
           self.config_tester.run_common_tests()







     def 							lowercase__       (						self :						Optional[Any]      ):
           SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mobilebert_model(*snake_case__      )







     def 							lowercase__       (						self :						List[str]      ):
           SCREAMING_SNAKE_CASE__       :  Optional[Any]						     =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mobilebert_for_masked_lm(*snake_case__      )







     def 							lowercase__       (						self :						str      ):
           SCREAMING_SNAKE_CASE__       :  Any						     =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mobilebert_for_multiple_choice(*snake_case__      )







     def 							lowercase__       (						self :						List[str]      ):
           SCREAMING_SNAKE_CASE__       :  str						     =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*snake_case__      )







     def 							lowercase__       (						self :						List[str]      ):
           SCREAMING_SNAKE_CASE__       :  Any						     =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mobilebert_for_pretraining(*snake_case__      )







     def 							lowercase__       (						self :						List[str]      ):
           SCREAMING_SNAKE_CASE__       :  Dict						     =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mobilebert_for_question_answering(*snake_case__      )







     def 							lowercase__       (						self :						str      ):
           SCREAMING_SNAKE_CASE__       :  Optional[int]						     =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mobilebert_for_sequence_classification(*snake_case__      )







     def 							lowercase__       (						self :						List[str]      ):
           SCREAMING_SNAKE_CASE__       :  List[str]						     =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mobilebert_for_token_classification(*snake_case__      )




def    a   (							A__							)	-> str:




      '''simple docstring'''

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


a_ :Dict  							=			1e-3





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







     @slow
     def 							lowercase__       (						self :						Dict      ):
           SCREAMING_SNAKE_CASE__       :  Optional[Any]						     =		MobileBertModel.from_pretrained('''google/mobilebert-uncased'''      ).to(snake_case__      )
           SCREAMING_SNAKE_CASE__       :  str						     =		_long_tensor([[1_01, 71_10, 10_05, 10_56, 20_23, 1_13_33, 1_74_13, 10_29, 1_02]]      )
           with torch.no_grad():
                 SCREAMING_SNAKE_CASE__       :  Dict						     =		model(snake_case__      )[0]
           SCREAMING_SNAKE_CASE__       :  str						     =		torch.Size((1, 9, 5_12)      )
           self.assertEqual(output.shape   ,   snake_case__      )
           SCREAMING_SNAKE_CASE__       :  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=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
           SCREAMING_SNAKE_CASE__       :  Optional[Any]						     =		torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE      )
           SCREAMING_SNAKE_CASE__       :  List[str]						     =		torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE      )

           self.assertTrue(lower_bound and upper_bound      )








def      UpperCamelCase   (       __lowerCamelCase	:					str = "The quick brown fox jumps over the lazy dog"							,   ):
       snake_case			:  Dict     							= set()

       # Replace all the whitespace in our sentence
       snake_case			:  List[Any]     							= input_str.replace(" "							,   ""       )
       for alpha in input_str:
              if "a" <= alpha.lower() <= "z":
                     frequency.add(alpha.lower()       )
       return len(__lowerCamelCase       ) == 26


def      UpperCamelCase   (       __lowerCamelCase	:					str = "The quick brown fox jumps over the lazy dog"							,   ):
       snake_case			:  Optional[int]     							= [False] * 26
       for char in input_str:
              if char.islower():
                     snake_case			:  Dict     							= True
              elif char.isupper():
                     snake_case			:  Optional[Any]     							= True
       return all(__lowerCamelCase       )


def      UpperCamelCase   (       __lowerCamelCase	:					str = "The quick brown fox jumps over the lazy dog"							,   ):
       return len({char for char in input_str.lower() if char.isalpha()}       ) == 26


def      UpperCamelCase   (       ):
       from timeit import timeit

       snake_case			:  Tuple     							= "from __main__ import is_pangram, is_pangram_faster, is_pangram_fastest"
       print(timeit("is_pangram()"							,   setup=__lowerCamelCase       )       )
       print(timeit("is_pangram_faster()"							,   setup=__lowerCamelCase       )       )
       print(timeit("is_pangram_fastest()"							,   setup=__lowerCamelCase       )       )
       # 5.348480500048026, 2.6477354579837993, 1.8470395830227062
       # 5.036091582966037, 2.644472333951853,  1.8869528750656173


if __name__ == "__main__":
   import doctest

   doctest.testmod()
   benchmark()





