

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 ,   lowercase__ ,   lowercase__=1_3 ,   lowercase__=7 ,   lowercase__=True ,   lowercase__=True ,   lowercase__=True ,   lowercase__=True ,   lowercase__=9_9 ,   lowercase__=6_4 ,   lowercase__=3_2 ,   lowercase__=5 ,   lowercase__=4 ,   lowercase__=3_7 ,   lowercase__="gelu" ,   lowercase__=0.1 ,   lowercase__=0.1 ,   lowercase__=5_1_2 ,   lowercase__=1_6 ,   lowercase__=2 ,   lowercase__=0.02 ,   lowercase__=3 ,   lowercase__=4 ,   lowercase__=None ,   ):







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







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







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







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







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







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







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







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







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







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







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







             '''simple docstring'''



             __A    =self.prepare_config_and_inputs()
             (
                 __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        lowerCAmelCase__  (					_SCREAMING_SNAKE_CASE				,							_SCREAMING_SNAKE_CASE				,							unittest.TestCase			):



       '''simple docstring'''







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

       def     __UpperCamelCase     (							self ,   lowercase__ ,   lowercase__ ,   lowercase__=False     ):







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







             '''simple docstring'''



             __A    =MobileBertModelTester(self     )
             __A    =ConfigTester(self ,   config_class=lowerCAmelCase_ ,   hidden_size=3_7     )

       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             self.config_tester.run_common_tests()

       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             __A    =self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mobilebert_model(*lowerCAmelCase_     )

       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             __A    =self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mobilebert_for_masked_lm(*lowerCAmelCase_     )

       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             __A    =self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mobilebert_for_multiple_choice(*lowerCAmelCase_     )

       def     __UpperCamelCase     (							self     ):







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







             '''simple docstring'''



             __A    =self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mobilebert_for_pretraining(*lowerCAmelCase_     )

       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             __A    =self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mobilebert_for_question_answering(*lowerCAmelCase_     )

       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             __A    =self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mobilebert_for_sequence_classification(*lowerCAmelCase_     )


       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             __A    =self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mobilebert_for_token_classification(*lowerCAmelCase_     )





def      A__					(			__A  :						Union[str, Any]			)					->List[str]:
      return torch.tensor(
          snake_case__     ,				dtype=torch.long     ,				device=snake_case__     ,				)


_lowerCamelCase      :					Dict					    =      1e-3





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



       '''simple docstring'''







       @slow
       def     __UpperCamelCase     (							self     ):







             '''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.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=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     )










"""simple docstring"""
from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


_lowerCAmelCase       =				{
    "configuration_timesformer": ["TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP", "TimesformerConfig"],
}

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							_lowerCAmelCase       =				[
							    "TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST",
							    "TimesformerModel",
							    "TimesformerForVideoClassification",
							    "TimesformerPreTrainedModel",
							]

if TYPE_CHECKING:
							from .configuration_timesformer import TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimesformerConfig

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_timesformer import (
														    TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
														    TimesformerForVideoClassification,
														    TimesformerModel,
														    TimesformerPreTrainedModel,
														)

else:
							import sys

							_lowerCAmelCase       =				_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)

