






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




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




 def   __magic_name__						(   self						:				Optional[int]   )						->    Dict:
       SCREAMING_SNAKE_CASE__  :					Any						=ids_tensor([self.batch_size, self.seq_length]       ,   self.vocab_size   )

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

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

       SCREAMING_SNAKE_CASE__  :					int						=None
       SCREAMING_SNAKE_CASE__  :					str						=None
       SCREAMING_SNAKE_CASE__  :					str						=None
       if self.use_labels:
             SCREAMING_SNAKE_CASE__  :					Tuple						=ids_tensor([self.batch_size]       ,   self.type_sequence_label_size   )
             SCREAMING_SNAKE_CASE__  :					List[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__  :					Any						=self.get_config()

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




 def   __magic_name__						(   self						:				Dict   )						->    Union[str, 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=_UpperCAmelCase       ,   initializer_range=self.initializer_range       ,   )




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




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




 def   __magic_name__						(   self						:				Union[str, Any]       ,   __lowercase						:				int       ,   __lowercase						:				int       ,   __lowercase						:				List[str]       ,   __lowercase						:				Any       ,   __lowercase						:				Union[str, Any]       ,   __lowercase						:				List[str]       ,   __lowercase						:				Any   )						->    str:
       SCREAMING_SNAKE_CASE__  :					int						=self.num_labels
       SCREAMING_SNAKE_CASE__  :					List[Any]						=MobileBertForTokenClassification(config=_UpperCAmelCase   )
       model.to(_UpperCAmelCase   )
       model.eval()
       SCREAMING_SNAKE_CASE__  :					Dict						=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   __magic_name__						(   self						:				Optional[int]       ,   __lowercase						:				Union[str, Any]       ,   __lowercase						:				Tuple       ,   __lowercase						:				Dict       ,   __lowercase						:				Dict       ,   __lowercase						:				Union[str, Any]       ,   __lowercase						:				List[str]       ,   __lowercase						:				List[Any]   )						->    Any:
       SCREAMING_SNAKE_CASE__  :					str						=self.num_choices
       SCREAMING_SNAKE_CASE__  :					str						=MobileBertForMultipleChoice(config=_UpperCAmelCase   )
       model.to(_UpperCAmelCase   )
       model.eval()
       SCREAMING_SNAKE_CASE__  :					Optional[int]						=input_ids.unsqueeze(1   ).expand(-1       ,   self.num_choices       ,   -1   ).contiguous()
       SCREAMING_SNAKE_CASE__  :					List[str]						=token_type_ids.unsqueeze(1   ).expand(-1       ,   self.num_choices       ,   -1   ).contiguous()
       SCREAMING_SNAKE_CASE__  :					Any						=input_mask.unsqueeze(1   ).expand(-1       ,   self.num_choices       ,   -1   ).contiguous()
       SCREAMING_SNAKE_CASE__  :					Union[str, Any]						=model(
           _UpperCAmelCase       ,   attention_mask=_UpperCAmelCase       ,   token_type_ids=_UpperCAmelCase       ,   labels=_UpperCAmelCase       ,   )
       self.parent.assertEqual(result.logits.shape       ,   (self.batch_size, self.num_choices)   )




 def   __magic_name__						(   self						:				Any   )						->    Tuple:
       SCREAMING_SNAKE_CASE__  :					Optional[Any]						=self.prepare_config_and_inputs()
       (
           SCREAMING_SNAKE_CASE__   
       )  :					Any						=config_and_inputs
       SCREAMING_SNAKE_CASE__  :					List[Any]						={'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
       return config, inputs_dict






@require_torch
class   __SCREAMING_SNAKE_CASE   (      _UpperCamelCase						,					_UpperCamelCase						,					unittest.TestCase					):
 snake_case_					=				(
     (
         MobileBertModel,
         MobileBertForMaskedLM,
         MobileBertForMultipleChoice,
         MobileBertForNextSentencePrediction,
         MobileBertForPreTraining,
         MobileBertForQuestionAnswering,
         MobileBertForSequenceClassification,
         MobileBertForTokenClassification,
     )
     if is_torch_available()
     else ()
 )
 snake_case_					=				(
     {
         'feature-extraction': MobileBertModel,
         'fill-mask': MobileBertForMaskedLM,
         'question-answering': MobileBertForQuestionAnswering,
         'text-classification': MobileBertForSequenceClassification,
         'token-classification': MobileBertForTokenClassification,
         'zero-shot': MobileBertForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 snake_case_					=				True




 def   __magic_name__						(   self						:				List[Any]       ,   __lowercase						:				Tuple       ,   __lowercase						:				Tuple       ,   __lowercase						:				Optional[int]=False   )						->    Union[str, Any]:
       SCREAMING_SNAKE_CASE__  :					Optional[Any]						=super()._prepare_for_class(_UpperCAmelCase       ,   _UpperCAmelCase       ,   return_labels=_UpperCAmelCase   )

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




 def   __magic_name__						(   self						:				Any   )						->    Tuple:
       SCREAMING_SNAKE_CASE__  :					Optional[Any]						=MobileBertModelTester(self   )
       SCREAMING_SNAKE_CASE__  :					List[Any]						=ConfigTester(self       ,   config_class=_UpperCAmelCase       ,   hidden_size=37   )




 def   __magic_name__						(   self						:				Any   )						->    str:
       self.config_tester.run_common_tests()




 def   __magic_name__						(   self						:				int   )						->    Any:
       SCREAMING_SNAKE_CASE__  :					Tuple						=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_model(*_UpperCAmelCase   )




 def   __magic_name__						(   self						:				Tuple   )						->    Optional[Any]:
       SCREAMING_SNAKE_CASE__  :					List[str]						=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_masked_lm(*_UpperCAmelCase   )




 def   __magic_name__						(   self						:				Optional[Any]   )						->    str:
       SCREAMING_SNAKE_CASE__  :					List[Any]						=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_multiple_choice(*_UpperCAmelCase   )




 def   __magic_name__						(   self						:				Any   )						->    str:
       SCREAMING_SNAKE_CASE__  :					Union[str, Any]						=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*_UpperCAmelCase   )




 def   __magic_name__						(   self						:				str   )						->    Optional[Any]:
       SCREAMING_SNAKE_CASE__  :					Tuple						=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_pretraining(*_UpperCAmelCase   )




 def   __magic_name__						(   self						:				Dict   )						->    Optional[Any]:
       SCREAMING_SNAKE_CASE__  :					List[Any]						=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_question_answering(*_UpperCAmelCase   )




 def   __magic_name__						(   self						:				Tuple   )						->    Optional[int]:
       SCREAMING_SNAKE_CASE__  :					List[Any]						=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_sequence_classification(*_UpperCAmelCase   )




 def   __magic_name__						(   self						:				int   )						->    int:
       SCREAMING_SNAKE_CASE__  :					Dict						=self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_mobilebert_for_token_classification(*_UpperCAmelCase   )





def 			_a(	UpperCamelCase__					:    Optional[Any]				):




      '''simple docstring'''






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


a_											=			1E-3







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




 @slow
 def   __magic_name__						(   self						:				str   )						->    List[Any]:
       SCREAMING_SNAKE_CASE__  :					int						=MobileBertModel.from_pretrained('''google/mobilebert-uncased'''   ).to(_UpperCAmelCase   )
       SCREAMING_SNAKE_CASE__  :					int						=_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__  :					Optional[int]						=model(_UpperCAmelCase   )[0]
       SCREAMING_SNAKE_CASE__  :					int						=torch.Size((1, 9, 5_12)   )
       self.assertEqual(output.shape       ,   _UpperCAmelCase   )
       SCREAMING_SNAKE_CASE__  :					List[Any]						=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=_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
       SCREAMING_SNAKE_CASE__  :					Dict						=torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE   )
       SCREAMING_SNAKE_CASE__  :					List[Any]						=torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE   )

       self.assertTrue(lower_bound and upper_bound   )




'''simple docstring'''

import warnings

from ...utils import logging
from .image_processing_deit import DeiTImageProcessor


__lowerCAmelCase		     =  logging.get_logger(__name__)

class       __magic_name__		(       _UpperCamelCase  ):







							def __init__(				self   :			Tuple      ,*_UpperCAmelCase   :			str      ,**_UpperCAmelCase   :			int						):
									warnings.warn(
									    'The class DeiTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please'
									    ' use DeiTImageProcessor instead.'      ,_UpperCAmelCase      ,)
									super().__init__(*_UpperCAmelCase      ,**_UpperCAmelCase						)




