

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	:    Any			,						lowerCamelCase__	:    Any			,						lowerCamelCase__	:    Union[str, Any]=1_3			,						lowerCamelCase__	:    Any=7			,						lowerCamelCase__	:    Any=True			,						lowerCamelCase__	:    Dict=True			,						lowerCamelCase__	:    Any=True			,						lowerCamelCase__	:    Union[str, Any]=True			,						lowerCamelCase__	:    List[Any]=9_9			,						lowerCamelCase__	:    Optional[int]=6_4			,						lowerCamelCase__	:    Dict=3_2			,						lowerCamelCase__	:    Union[str, Any]=5			,						lowerCamelCase__	:    List[str]=4			,						lowerCamelCase__	:    Any=3_7			,						lowerCamelCase__	:    Any="gelu"			,						lowerCamelCase__	:    List[Any]=0.1			,						lowerCamelCase__	:    str=0.1			,						lowerCamelCase__	:    Optional[Any]=5_1_2			,						lowerCamelCase__	:    List[str]=1_6			,						lowerCamelCase__	:    Dict=2			,						lowerCamelCase__	:    Optional[int]=0.02			,						lowerCamelCase__	:    List[Any]=3			,						lowerCamelCase__	:    Optional[Any]=4			,						lowerCamelCase__	:    Tuple=None			,						) ->			Any:
         """simple docstring"""





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

   def     UpperCamelCase					(				self	:    Optional[int]       ) ->			Optional[Any]:
         """simple docstring"""





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

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

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

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

         A_							       =		self.get_config()

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

   def     UpperCamelCase					(				self	:    List[str]       ) ->			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=lowerCamelCase__			,						initializer_range=self.initializer_range			,						)

   def     UpperCamelCase					(				self	:    Dict			,						lowerCamelCase__	:    Union[str, Any]			,						lowerCamelCase__	:    List[Any]			,						lowerCamelCase__	:    List[Any]			,						lowerCamelCase__	:    Tuple			,						lowerCamelCase__	:    Union[str, Any]			,						lowerCamelCase__	:    str			,						lowerCamelCase__	:    Optional[int]       ) ->			Union[str, Any]:
         """simple docstring"""





         A_							       =		MobileBertModel(config=lowerCamelCase__       )
         model.to(lowerCamelCase__       )
         model.eval()
         A_							       =		model(lowerCamelCase__			,						attention_mask=lowerCamelCase__			,						token_type_ids=lowerCamelCase__       )
         A_							       =		model(lowerCamelCase__			,						token_type_ids=lowerCamelCase__       )
         A_							       =		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     UpperCamelCase					(				self	:    Tuple			,						lowerCamelCase__	:    Optional[Any]			,						lowerCamelCase__	:    Any			,						lowerCamelCase__	:    Union[str, Any]			,						lowerCamelCase__	:    Tuple			,						lowerCamelCase__	:    int			,						lowerCamelCase__	:    Tuple			,						lowerCamelCase__	:    Dict       ) ->			Union[str, Any]:
         """simple docstring"""





         A_							       =		MobileBertForMaskedLM(config=lowerCamelCase__       )
         model.to(lowerCamelCase__       )
         model.eval()
         A_							       =		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     UpperCamelCase					(				self	:    Tuple			,						lowerCamelCase__	:    List[str]			,						lowerCamelCase__	:    Union[str, Any]			,						lowerCamelCase__	:    List[Any]			,						lowerCamelCase__	:    Any			,						lowerCamelCase__	:    Tuple			,						lowerCamelCase__	:    int			,						lowerCamelCase__	:    int       ) ->			Dict:
         """simple docstring"""





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

   def     UpperCamelCase					(				self	:    Dict			,						lowerCamelCase__	:    Optional[Any]			,						lowerCamelCase__	:    int			,						lowerCamelCase__	:    List[Any]			,						lowerCamelCase__	:    Any			,						lowerCamelCase__	:    str			,						lowerCamelCase__	:    Dict			,						lowerCamelCase__	:    Tuple       ) ->			Dict:
         """simple docstring"""





         A_							       =		MobileBertForPreTraining(config=lowerCamelCase__       )
         model.to(lowerCamelCase__       )
         model.eval()
         A_							       =		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     UpperCamelCase					(				self	:    Dict			,						lowerCamelCase__	:    int			,						lowerCamelCase__	:    Optional[Any]			,						lowerCamelCase__	:    List[str]			,						lowerCamelCase__	:    Optional[int]			,						lowerCamelCase__	:    str			,						lowerCamelCase__	:    Tuple			,						lowerCamelCase__	:    Optional[int]       ) ->			List[str]:
         """simple docstring"""





         A_							       =		MobileBertForQuestionAnswering(config=lowerCamelCase__       )
         model.to(lowerCamelCase__       )
         model.eval()
         A_							       =		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     UpperCamelCase					(				self	:    Optional[Any]			,						lowerCamelCase__	:    Any			,						lowerCamelCase__	:    Optional[int]			,						lowerCamelCase__	:    Any			,						lowerCamelCase__	:    int			,						lowerCamelCase__	:    Dict			,						lowerCamelCase__	:    Optional[int]			,						lowerCamelCase__	:    List[str]       ) ->			Any:
         """simple docstring"""





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

   def     UpperCamelCase					(				self	:    str			,						lowerCamelCase__	:    Optional[int]			,						lowerCamelCase__	:    Union[str, Any]			,						lowerCamelCase__	:    int			,						lowerCamelCase__	:    int			,						lowerCamelCase__	:    Optional[int]			,						lowerCamelCase__	:    Optional[Any]			,						lowerCamelCase__	:    List[Any]       ) ->			Tuple:
         """simple docstring"""





         A_							       =		self.num_labels
         A_							       =		MobileBertForTokenClassification(config=lowerCamelCase__       )
         model.to(lowerCamelCase__       )
         model.eval()
         A_							       =		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     UpperCamelCase					(				self	:    Tuple			,						lowerCamelCase__	:    List[Any]			,						lowerCamelCase__	:    str			,						lowerCamelCase__	:    Tuple			,						lowerCamelCase__	:    Optional[int]			,						lowerCamelCase__	:    Tuple			,						lowerCamelCase__	:    str			,						lowerCamelCase__	:    int       ) ->			Optional[Any]:
         """simple docstring"""





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





   def     UpperCamelCase					(				self	:    Dict       ) ->			Dict:
         """simple docstring"""





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

   def     UpperCamelCase					(				self	:    str			,						lowerCamelCase__	:    Any			,						lowerCamelCase__	:    Optional[int]			,						lowerCamelCase__	:    Dict=False       ) ->			Tuple:
         """simple docstring"""





         A_							       =		super()._prepare_for_class(lowerCamelCase__			,						lowerCamelCase__			,						return_labels=lowerCamelCase__       )

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

   def     UpperCamelCase					(				self	:    Union[str, Any]       ) ->			Optional[Any]:
         """simple docstring"""





         A_							       =		MobileBertModelTester(self       )
         A_							       =		ConfigTester(self			,						config_class=lowerCamelCase__			,						hidden_size=3_7       )

   def     UpperCamelCase					(				self	:    str       ) ->			int:
         """simple docstring"""





         self.config_tester.run_common_tests()

   def     UpperCamelCase					(				self	:    Optional[int]       ) ->			Tuple:
         """simple docstring"""





         A_							       =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_model(*lowerCamelCase__       )

   def     UpperCamelCase					(				self	:    Any       ) ->			int:
         """simple docstring"""





         A_							       =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_masked_lm(*lowerCamelCase__       )

   def     UpperCamelCase					(				self	:    str       ) ->			int:
         """simple docstring"""





         A_							       =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_multiple_choice(*lowerCamelCase__       )

   def     UpperCamelCase					(				self	:    Optional[int]       ) ->			Dict:
         """simple docstring"""





         A_							       =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*lowerCamelCase__       )

   def     UpperCamelCase					(				self	:    int       ) ->			Optional[int]:
         """simple docstring"""





         A_							       =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_pretraining(*lowerCamelCase__       )

   def     UpperCamelCase					(				self	:    Optional[Any]       ) ->			List[str]:
         """simple docstring"""





         A_							       =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_question_answering(*lowerCamelCase__       )

   def     UpperCamelCase					(				self	:    Dict       ) ->			Optional[int]:
         """simple docstring"""





         A_							       =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_sequence_classification(*lowerCamelCase__       )





   def     UpperCamelCase					(				self	:    List[Any]       ) ->			Any:
         """simple docstring"""





         A_							       =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mobilebert_for_token_classification(*lowerCamelCase__       )







def 							_lowerCamelCase   (			SCREAMING_SNAKE_CASE   ):
      '''simple docstring'''







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


__lowercase				      =						1E-3





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

   @slow
   def     UpperCamelCase					(				self	:    Dict       ) ->			List[Any]:
         """simple docstring"""





         A_							       =		MobileBertModel.from_pretrained('''google/mobilebert-uncased'''       ).to(lowerCamelCase__       )
         A_							       =		_long_tensor([[1_0_1, 7_1_1_0, 1_0_0_5, 1_0_5_6, 2_0_2_3, 1_1_3_3_3, 1_7_4_1_3, 1_0_2_9, 1_0_2]]       )
         with torch.no_grad():
               A_							       =		model(lowerCamelCase__       )[0]
         A_							       =		torch.Size((1, 9, 5_1_2)       )
         self.assertEqual(output.shape			,						lowerCamelCase__       )
         A_							       =		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_							       =		torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE       )
         A_							       =		torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE       )

         self.assertTrue(lower_bound and upper_bound       )








# this script reports modified .py files under the desired list of top-level sub-dirs passed as a list of arguments, e.g.:
#   python ./utils/get_modified_files.py utils src tests examples
#
# it uses git to find the forking point and which files were modified - i.e. files not under git won't be considered
# since the output of this script is fed into Makefile commands it doesn't print a newline after the results

import re
import subprocess
import sys


__lowercase				      =						subprocess.check_output("""git merge-base main HEAD""".split()).decode("""utf-8""")
__lowercase				      =						(
    subprocess.check_output(f'git diff --diff-filter=d --name-only {fork_point_sha}'.split()).decode("""utf-8""").split()
)

__lowercase				      =						"""|""".join(sys.argv[1:])
__lowercase				      =						re.compile(rf'^({joined_dirs}).*?\.py$')

__lowercase				      =						[x for x in modified_files if regex.match(x)]
print(""" """.join(relevant_modified_files), end="""""")





