


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





      '''simple docstring'''






      def __init__(self    :    int   ,						UpperCamelCase    :    Optional[int]   ,						UpperCamelCase    :    Any=13   ,						UpperCamelCase    :    List[Any]=7   ,						UpperCamelCase    :    Optional[int]=True   ,						UpperCamelCase    :    List[str]=True   ,						UpperCamelCase    :    List[Any]=True   ,						UpperCamelCase    :    Optional[Any]=True   ,						UpperCamelCase    :    Union[str, Any]=99   ,						UpperCamelCase    :    List[str]=64   ,						UpperCamelCase    :    List[str]=32   ,						UpperCamelCase    :    Optional[int]=5   ,						UpperCamelCase    :    Any=4   ,						UpperCamelCase    :    Union[str, Any]=37   ,						UpperCamelCase    :    Dict="gelu"   ,						UpperCamelCase    :    int=0.1   ,						UpperCamelCase    :    List[Any]=0.1   ,						UpperCamelCase    :    List[Any]=512   ,						UpperCamelCase    :    Union[str, Any]=16   ,						UpperCamelCase    :    int=2   ,						UpperCamelCase    :    Tuple=0.02   ,						UpperCamelCase    :    Optional[int]=3   ,						UpperCamelCase    :    Optional[Any]=4   ,						UpperCamelCase    :    str=None   ,						):

             '''simple docstring'''







             lowercase__      			= parent
             lowercase__      			= batch_size
             lowercase__      			= seq_length
             lowercase__      			= is_training
             lowercase__      			= use_input_mask
             lowercase__      			= use_token_type_ids
             lowercase__      			= use_labels
             lowercase__      			= vocab_size
             lowercase__      			= hidden_size
             lowercase__      			= embedding_size
             lowercase__      			= num_hidden_layers
             lowercase__      			= num_attention_heads
             lowercase__      			= intermediate_size
             lowercase__      			= hidden_act
             lowercase__      			= hidden_dropout_prob
             lowercase__      			= attention_probs_dropout_prob
             lowercase__      			= max_position_embeddings
             lowercase__      			= type_vocab_size
             lowercase__      			= type_sequence_label_size
             lowercase__      			= initializer_range
             lowercase__      			= num_labels
             lowercase__      			= num_choices
             lowercase__      			= scope



      def       UpperCamelCase__		(self    :    str ):

             '''simple docstring'''







             lowercase__      			= ids_tensor([self.batch_size, self.seq_length]   ,						self.vocab_size )

             lowercase__      			= None
             if self.use_input_mask:
                    lowercase__      			= random_attention_mask([self.batch_size, self.seq_length] )

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

             lowercase__      			= None
             lowercase__      			= None
             lowercase__      			= None
             if self.use_labels:
                    lowercase__      			= ids_tensor([self.batch_size]   ,						self.type_sequence_label_size )
                    lowercase__      			= ids_tensor([self.batch_size, self.seq_length]   ,						self.num_labels )
                    lowercase__      			= ids_tensor([self.batch_size]   ,						self.num_choices )

             lowercase__      			= self.get_config()

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



      def       UpperCamelCase__		(self    :    Dict ):

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



      def       UpperCamelCase__		(self    :    Optional[Any]   ,						UpperCamelCase    :    Any   ,						UpperCamelCase    :    Optional[int]   ,						UpperCamelCase    :    List[str]   ,						UpperCamelCase    :    int   ,						UpperCamelCase    :    Dict   ,						UpperCamelCase    :    Union[str, Any]   ,						UpperCamelCase    :    Optional[Any] ):

             '''simple docstring'''







             lowercase__      			= MobileBertModel(config=UpperCamelCase )
             model.to(UpperCamelCase )
             model.eval()
             lowercase__      			= model(UpperCamelCase   ,						attention_mask=UpperCamelCase   ,						token_type_ids=UpperCamelCase )
             lowercase__      			= model(UpperCamelCase   ,						token_type_ids=UpperCamelCase )
             lowercase__      			= model(UpperCamelCase )

             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       UpperCamelCase__		(self    :    str   ,						UpperCamelCase    :    str   ,						UpperCamelCase    :    Optional[Any]   ,						UpperCamelCase    :    str   ,						UpperCamelCase    :    Optional[Any]   ,						UpperCamelCase    :    Optional[int]   ,						UpperCamelCase    :    List[str]   ,						UpperCamelCase    :    List[Any] ):

             '''simple docstring'''







             lowercase__      			= MobileBertForMaskedLM(config=UpperCamelCase )
             model.to(UpperCamelCase )
             model.eval()
             lowercase__      			= model(UpperCamelCase   ,						attention_mask=UpperCamelCase   ,						token_type_ids=UpperCamelCase   ,						labels=UpperCamelCase )
             self.parent.assertEqual(result.logits.shape   ,						(self.batch_size, self.seq_length, self.vocab_size) )



      def       UpperCamelCase__		(self    :    Optional[Any]   ,						UpperCamelCase    :    Optional[int]   ,						UpperCamelCase    :    str   ,						UpperCamelCase    :    Any   ,						UpperCamelCase    :    Dict   ,						UpperCamelCase    :    Dict   ,						UpperCamelCase    :    Union[str, Any]   ,						UpperCamelCase    :    str ):

             '''simple docstring'''







             lowercase__      			= MobileBertForNextSentencePrediction(config=UpperCamelCase )
             model.to(UpperCamelCase )
             model.eval()
             lowercase__      			= model(
                 UpperCamelCase   ,						attention_mask=UpperCamelCase   ,						token_type_ids=UpperCamelCase   ,						labels=UpperCamelCase   ,						)
             self.parent.assertEqual(result.logits.shape   ,						(self.batch_size, 2) )



      def       UpperCamelCase__		(self    :    int   ,						UpperCamelCase    :    int   ,						UpperCamelCase    :    List[str]   ,						UpperCamelCase    :    List[str]   ,						UpperCamelCase    :    Any   ,						UpperCamelCase    :    Any   ,						UpperCamelCase    :    Union[str, Any]   ,						UpperCamelCase    :    Union[str, Any] ):

             '''simple docstring'''







             lowercase__      			= MobileBertForPreTraining(config=UpperCamelCase )
             model.to(UpperCamelCase )
             model.eval()
             lowercase__      			= model(
                 UpperCamelCase   ,						attention_mask=UpperCamelCase   ,						token_type_ids=UpperCamelCase   ,						labels=UpperCamelCase   ,						next_sentence_label=UpperCamelCase   ,						)
             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       UpperCamelCase__		(self    :    str   ,						UpperCamelCase    :    List[Any]   ,						UpperCamelCase    :    Optional[int]   ,						UpperCamelCase    :    Tuple   ,						UpperCamelCase    :    Optional[int]   ,						UpperCamelCase    :    Tuple   ,						UpperCamelCase    :    Union[str, Any]   ,						UpperCamelCase    :    Optional[Any] ):

             '''simple docstring'''







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

             '''simple docstring'''







             lowercase__      			= self.num_labels
             lowercase__      			= MobileBertForSequenceClassification(UpperCamelCase )
             model.to(UpperCamelCase )
             model.eval()
             lowercase__      			= model(UpperCamelCase   ,						attention_mask=UpperCamelCase   ,						token_type_ids=UpperCamelCase   ,						labels=UpperCamelCase )
             self.parent.assertEqual(result.logits.shape   ,						(self.batch_size, self.num_labels) )



      def       UpperCamelCase__		(self    :    List[str]   ,						UpperCamelCase    :    List[str]   ,						UpperCamelCase    :    Optional[Any]   ,						UpperCamelCase    :    Dict   ,						UpperCamelCase    :    Dict   ,						UpperCamelCase    :    Dict   ,						UpperCamelCase    :    Tuple   ,						UpperCamelCase    :    List[str] ):

             '''simple docstring'''







             lowercase__      			= self.num_labels
             lowercase__      			= MobileBertForTokenClassification(config=UpperCamelCase )
             model.to(UpperCamelCase )
             model.eval()
             lowercase__      			= model(UpperCamelCase   ,						attention_mask=UpperCamelCase   ,						token_type_ids=UpperCamelCase   ,						labels=UpperCamelCase )
             self.parent.assertEqual(result.logits.shape   ,						(self.batch_size, self.seq_length, self.num_labels) )



      def       UpperCamelCase__		(self    :    List[str]   ,						UpperCamelCase    :    Union[str, Any]   ,						UpperCamelCase    :    int   ,						UpperCamelCase    :    List[str]   ,						UpperCamelCase    :    Union[str, Any]   ,						UpperCamelCase    :    Any   ,						UpperCamelCase    :    Any   ,						UpperCamelCase    :    Dict ):

             '''simple docstring'''







             lowercase__      			= self.num_choices
             lowercase__      			= MobileBertForMultipleChoice(config=UpperCamelCase )
             model.to(UpperCamelCase )
             model.eval()
             lowercase__      			= input_ids.unsqueeze(1 ).expand(-1   ,						self.num_choices   ,						-1 ).contiguous()
             lowercase__      			= token_type_ids.unsqueeze(1 ).expand(-1   ,						self.num_choices   ,						-1 ).contiguous()
             lowercase__      			= input_mask.unsqueeze(1 ).expand(-1   ,						self.num_choices   ,						-1 ).contiguous()
             lowercase__      			= model(
                 UpperCamelCase   ,						attention_mask=UpperCamelCase   ,						token_type_ids=UpperCamelCase   ,						labels=UpperCamelCase   ,						)
             self.parent.assertEqual(result.logits.shape   ,						(self.batch_size, self.num_choices) )






      def       UpperCamelCase__		(self    :    Dict ):

             '''simple docstring'''







             lowercase__      			= self.prepare_config_and_inputs()
             (
                 (
                 lowercase__
             )			,(
                 lowercase__
             )			,(
                 lowercase__
             )			,(
                 lowercase__
             )			,(
                 lowercase__
             )			,(
                 lowercase__
             )			,(
                 lowercase__
             )			,
             )      			= config_and_inputs
             lowercase__      			= {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
             return config, inputs_dict






@require_torch
class   __lowerCAmelCase						(lowercase_  ,     lowercase_  ,     unittest.TestCase     ):





      '''simple docstring'''






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



      def       UpperCamelCase__		(self    :    List[str]   ,						UpperCamelCase    :    List[str]   ,						UpperCamelCase    :    Any   ,						UpperCamelCase    :    Any=False ):

             '''simple docstring'''







             lowercase__      			= super()._prepare_for_class(UpperCamelCase   ,						UpperCamelCase   ,						return_labels=UpperCamelCase )

             if return_labels:
                    if model_class in get_values(UpperCamelCase ):
                           lowercase__      			= torch.zeros(
                               (self.model_tester.batch_size, self.model_tester.seq_length)   ,						dtype=torch.long   ,						device=UpperCamelCase )
                           lowercase__      			= torch.zeros(
                               self.model_tester.batch_size   ,						dtype=torch.long   ,						device=UpperCamelCase )
             return inputs_dict



      def       UpperCamelCase__		(self    :    Optional[Any] ):

             '''simple docstring'''







             lowercase__      			= MobileBertModelTester(self )
             lowercase__      			= ConfigTester(self   ,						config_class=UpperCamelCase   ,						hidden_size=37 )



      def       UpperCamelCase__		(self    :    Any ):

             '''simple docstring'''







             self.config_tester.run_common_tests()



      def       UpperCamelCase__		(self    :    Any ):

             '''simple docstring'''







             lowercase__      			= self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mobilebert_model(*UpperCamelCase )



      def       UpperCamelCase__		(self    :    Optional[int] ):

             '''simple docstring'''







             lowercase__      			= self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mobilebert_for_masked_lm(*UpperCamelCase )



      def       UpperCamelCase__		(self    :    Optional[Any] ):

             '''simple docstring'''







             lowercase__      			= self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mobilebert_for_multiple_choice(*UpperCamelCase )



      def       UpperCamelCase__		(self    :    List[str] ):

             '''simple docstring'''







             lowercase__      			= self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*UpperCamelCase )



      def       UpperCamelCase__		(self    :    Dict ):

             '''simple docstring'''







             lowercase__      			= self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mobilebert_for_pretraining(*UpperCamelCase )



      def       UpperCamelCase__		(self    :    List[str] ):

             '''simple docstring'''







             lowercase__      			= self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mobilebert_for_question_answering(*UpperCamelCase )



      def       UpperCamelCase__		(self    :    Dict ):

             '''simple docstring'''







             lowercase__      			= self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mobilebert_for_sequence_classification(*UpperCamelCase )






      def       UpperCamelCase__		(self    :    Union[str, Any] ):

             '''simple docstring'''







             lowercase__      			= self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mobilebert_for_token_classification(*UpperCamelCase )






def _SCREAMING_SNAKE_CASE      (A       )       ->       Optional[int]:






       """simple docstring"""







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


lowerCamelCase							:   str  						=	1e-3

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





      '''simple docstring'''






      @slow
      def       UpperCamelCase__		(self    :    int ):

             '''simple docstring'''







             lowercase__      			= MobileBertModel.from_pretrained('''google/mobilebert-uncased''' ).to(UpperCamelCase )
             lowercase__      			= _long_tensor([[101, 7110, 1005, 1056, 2023, 11333, 17413, 1029, 102]] )
             with torch.no_grad():
                    lowercase__      			= model(UpperCamelCase )[0]
             lowercase__      			= torch.Size((1, 9, 512) )
             self.assertEqual(output.shape   ,						UpperCamelCase )
             lowercase__      			= torch.tensor(
                 [
                     [
                         [-2.4_73_65_26E07, 8.2_69_16_56E04, 1.6_52_18_38E05],
                         [-5.7_54_17_04E-01, 3.9_05_60_22E00, 4.4_01_15_07E00],
                         [2.6_04_73_59E00, 1.5_67_76_52E00, -1.7_32_41_88E-01],
                     ]
                 ]   ,						device=UpperCamelCase   ,						)

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

             self.assertTrue(lower_bound and upper_bound )












"""simple docstring"""
def 				snake_case      (   A__	,A__						):
			# "extended trapezoidal rule"
			# int(f) = dx/2 * (f1 + 2f2 + ... + fn)
			UpperCAmelCase_     : Dict			     =					(boundary[1] - boundary[0]) / steps
			UpperCAmelCase_     : Optional[int]			     =					boundary[0]
			UpperCAmelCase_     : str			     =					boundary[1]
			UpperCAmelCase_     : Tuple			     =					make_points(A__	,A__	,A__						)
			UpperCAmelCase_     : List[str]			     =					0.0
			y += (h / 2.0) * f(A__						)
			for i in x_i:
						# print(i)
						y += h * f(A__						)
			y += (h / 2.0) * f(A__						)
			return y

def 				snake_case      (   A__	,A__	,A__						):
			UpperCAmelCase_     : Union[str, Any]			     =					a + h
			while x < (b - h):
						yield x
						UpperCAmelCase_     : Optional[Any]			     =					x + h

def 				snake_case      (   A__						):  # enter your function here
			UpperCAmelCase_     : Dict			     =					(x - 0) * (x - 0)
			return y

def 				snake_case      (   ):
			UpperCAmelCase_     : Dict			     =					0.0  # Lower bound of integration
			UpperCAmelCase_     : Optional[int]			     =					1.0  # Upper bound of integration
			UpperCAmelCase_     : Dict			     =					10.0  # define number of steps or resolution
			UpperCAmelCase_     : List[Any]			     =					[a, b]  # define boundary of integration
			UpperCAmelCase_     : Union[str, Any]			     =					method_a(A__	,A__						)
			print(F"""y = {y}"""						)


if __name__ == "__main__":
			main()



