



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


 """simple docstring"""


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







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

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

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

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

        _UpperCAmelCase  :						Any			     = self.get_config()

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







 def _snake_case (							self							)		->   Optional[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=a_  ,initializer_range=self.initializer_range  ,)







 def _snake_case (							self  ,a_  ,a_  ,a_  ,a_  ,a_  ,a_  ,a_							)		->   Optional[int]:
        _UpperCAmelCase  :						Dict			     = MobileBertModel(config=a_							)
        model.to(a_							)
        model.eval()
        _UpperCAmelCase  :						List[str]			     = model(a_  ,attention_mask=a_  ,token_type_ids=a_							)
        _UpperCAmelCase  :						Optional[Any]			     = model(a_  ,token_type_ids=a_							)
        _UpperCAmelCase  :						Dict			     = model(a_							)

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







 def _snake_case (							self  ,a_  ,a_  ,a_  ,a_  ,a_  ,a_  ,a_							)		->   List[str]:
        _UpperCAmelCase  :						List[str]			     = MobileBertForNextSentencePrediction(config=a_							)
        model.to(a_							)
        model.eval()
        _UpperCAmelCase  :						List[str]			     = model(
            a_  ,attention_mask=a_  ,token_type_ids=a_  ,labels=a_  ,)
        self.parent.assertEqual(result.logits.shape  ,(self.batch_size, 2)							)







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







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







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







 def _snake_case (							self							)		->   Any:
        _UpperCAmelCase  :						Optional[int]			     = self.prepare_config_and_inputs()
        (
            (
            _UpperCAmelCase
        )     ,(
            _UpperCAmelCase
        )     ,(
            _UpperCAmelCase
        )     ,(
            _UpperCAmelCase
        )     ,(
            _UpperCAmelCase
        )     ,(
            _UpperCAmelCase
        )     ,(
            _UpperCAmelCase
        )     ,
        )  :						Optional[int]			     = config_and_inputs
        _UpperCAmelCase  :						Union[str, Any]			     = {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
        return config, inputs_dict







@require_torch
class 							lowercase       (  _lowerCamelCase					,					_lowerCamelCase					,					unittest.TestCase				):


 """simple docstring"""


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







 def _snake_case (							self  ,a_  ,a_  ,a_=False							)		->   int:
        _UpperCAmelCase  :						Union[str, Any]			     = super()._prepare_for_class(a_  ,a_  ,return_labels=a_							)

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







 def _snake_case (							self							)		->   Any:
        _UpperCAmelCase  :						Union[str, Any]			     = MobileBertModelTester(self							)
        _UpperCAmelCase  :						Optional[int]			     = ConfigTester(self  ,config_class=a_  ,hidden_size=37							)







 def _snake_case (							self							)		->   Union[str, Any]:
        self.config_tester.run_common_tests()







 def _snake_case (							self							)		->   str:
        _UpperCAmelCase  :						Any			     = self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_model(*a_							)







 def _snake_case (							self							)		->   Any:
        _UpperCAmelCase  :						int			     = self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_masked_lm(*a_							)







 def _snake_case (							self							)		->   Any:
        _UpperCAmelCase  :						int			     = self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_multiple_choice(*a_							)







 def _snake_case (							self							)		->   List[str]:
        _UpperCAmelCase  :						str			     = self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*a_							)







 def _snake_case (							self							)		->   Tuple:
        _UpperCAmelCase  :						List[str]			     = self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_pretraining(*a_							)







 def _snake_case (							self							)		->   Union[str, Any]:
        _UpperCAmelCase  :						Optional[Any]			     = self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_question_answering(*a_							)







 def _snake_case (							self							)		->   List[Any]:
        _UpperCAmelCase  :						Dict			     = self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_sequence_classification(*a_							)







 def _snake_case (							self							)		->   str:
        _UpperCAmelCase  :						Optional[Any]			     = self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_token_classification(*a_							)







def     snake_case_					(     lowerCAmelCase_		)->		Optional[Any]:





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


 """simple docstring"""


 @slow
 def _snake_case (							self							)		->   Tuple:
        _UpperCAmelCase  :						Optional[Any]			     = MobileBertModel.from_pretrained("""google/mobilebert-uncased"""							).to(a_							)
        _UpperCAmelCase  :						str			     = _long_tensor([[101, 7_110, 1_005, 1_056, 2_023, 11_333, 17_413, 1_029, 102]]							)
        with torch.no_grad():
               _UpperCAmelCase  :						List[str]			     = model(a_							)[0]
        _UpperCAmelCase  :						int			     = torch.Size((1, 9, 512)							)
        self.assertEqual(output.shape  ,a_							)
        _UpperCAmelCase  :						Dict			     = torch.tensor(
            [
                [
                    [-2.4_7_3_6_5_2_6E0_7, 8.2_6_9_1_6_5_6E0_4, 1.6_5_2_1_8_3_8E0_5],
                    [-5.7_5_4_1_7_0_4E-0_1, 3.9_0_5_6_0_2_2E0_0, 4.4_0_1_1_5_0_7E0_0],
                    [2.6_0_4_7_3_5_9E0_0, 1.5_6_7_7_6_5_2E0_0, -1.7_3_2_4_1_8_8E-0_1],
                ]
            ]  ,device=a_  ,)

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

        self.assertTrue(lower_bound and upper_bound							)








'''simple docstring'''






from numpy import exp, pi, sqrt




def     snake_case_					(     lowerCAmelCase_       ,   lowerCAmelCase_ = 0.0       ,   lowerCAmelCase_ = 1.0		)->		int:





       '''simple docstring'''


       return 1 / sqrt(2 * pi * sigma**2		) * exp(-((x - mu) ** 2) / (2 * sigma**2)		)


if __name__ == "__main__":
   import doctest

   doctest.testmod()



