


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     __UpperCAmelCase							:


     """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  ,       ):
        __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 	snake_case_  (	self    ):
        __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 	snake_case_  (	self    ):
        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    ):
        __a					=  MobileBertModel(config=__A    )
        model.to(__A    )
        model.eval()
        __a					=  model(__A  ,       attention_mask=__A  ,       token_type_ids=__A    )
        __a					=  model(__A  ,       token_type_ids=__A    )
        __a					=  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    ):
        __a					=  MobileBertForMaskedLM(config=__A    )
        model.to(__A    )
        model.eval()
        __a					=  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    ):
        __a					=  MobileBertForNextSentencePrediction(config=__A    )
        model.to(__A    )
        model.eval()
        __a					=  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    ):
        __a					=  MobileBertForPreTraining(config=__A    )
        model.to(__A    )
        model.eval()
        __a					=  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    ):
        __a					=  MobileBertForQuestionAnswering(config=__A    )
        model.to(__A    )
        model.eval()
        __a					=  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    ):
        __a					=  self.num_labels
        __a					=  MobileBertForSequenceClassification(__A    )
        model.to(__A    )
        model.eval()
        __a					=  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    ):
        __a					=  self.num_labels
        __a					=  MobileBertForTokenClassification(config=__A    )
        model.to(__A    )
        model.eval()
        __a					=  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    ):
        __a					=  self.num_choices
        __a					=  MobileBertForMultipleChoice(config=__A    )
        model.to(__A    )
        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(
            __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    ):
        __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     __UpperCAmelCase							(       __A ,					__A ,					unittest.TestCase							):


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


     def 	snake_case_  (	self  ,       __A  ,       __A  ,       __A=False    ):
        __a					=  super()._prepare_for_class(__A  ,       __A  ,       return_labels=__A    )

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


     def 	snake_case_  (	self    ):
        __a					=  MobileBertModelTester(self    )
        __a					=  ConfigTester(self  ,       config_class=__A  ,       hidden_size=37    )


     def 	snake_case_  (	self    ):
        self.config_tester.run_common_tests()


     def 	snake_case_  (	self    ):
        __a					=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_model(*__A    )


     def 	snake_case_  (	self    ):
        __a					=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_masked_lm(*__A    )


     def 	snake_case_  (	self    ):
        __a					=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_multiple_choice(*__A    )


     def 	snake_case_  (	self    ):
        __a					=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*__A    )


     def 	snake_case_  (	self    ):
        __a					=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_pretraining(*__A    )


     def 	snake_case_  (	self    ):
        __a					=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_question_answering(*__A    )


     def 	snake_case_  (	self    ):
        __a					=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_sequence_classification(*__A    )


     def 	snake_case_  (	self    ):
        __a					=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mobilebert_for_token_classification(*__A    )







def  a   (lowerCAmelCase__   ):
   return torch.tensor(
       lowerCAmelCase__		,				dtype=torch.long		,				device=lowerCAmelCase__		,				)


SCREAMING_SNAKE_CASE							    =							1E-3


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


     """simple docstring"""


     @slow
     def 	snake_case_  (	self    ):
        __a					=  MobileBertModel.from_pretrained("""google/mobilebert-uncased"""    ).to(__A    )
        __a					=  _long_tensor([[101, 7110, 1005, 1056, 2023, 11333, 17413, 1029, 102]]    )
        with torch.no_grad():
           __a					=  model(__A    )[0]
        __a					=  torch.Size((1, 9, 512)    )
        self.assertEqual(output.shape  ,       __A    )
        __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=__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
        __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    )









from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCamelCase__					:  Optional[int]      							=				logging.get_logger(__name__)

lowerCamelCase__					:  Tuple      							=				{
    'microsoft/swinv2-tiny-patch4-window8-256': (
        'https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256/resolve/main/config.json'
    ),
}





class 			lowerCamelCase_				( _SCREAMING_SNAKE_CASE   ):


 '''simple docstring'''







 lowercase_							       =       "swinv2"

 lowercase_							       =       {
     "num_attention_heads": "num_heads",
     "num_hidden_layers": "num_layers",
 }



 def __init__(				self    :						Dict							,							_lowerCAmelCase    :						Optional[Any]=224							,							_lowerCAmelCase    :						Optional[int]=4							,							_lowerCAmelCase    :						Tuple=3							,							_lowerCAmelCase    :						Tuple=96							,							_lowerCAmelCase    :						Dict=[2, 2, 6, 2]							,							_lowerCAmelCase    :						Optional[Any]=[3, 6, 12, 24]							,							_lowerCAmelCase    :						str=7							,							_lowerCAmelCase    :						List[Any]=4.0							,							_lowerCAmelCase    :						List[str]=True							,							_lowerCAmelCase    :						List[Any]=0.0							,							_lowerCAmelCase    :						List[Any]=0.0							,							_lowerCAmelCase    :						Any=0.1							,							_lowerCAmelCase    :						List[Any]="gelu"							,							_lowerCAmelCase    :						str=False							,							_lowerCAmelCase    :						str=0.02							,							_lowerCAmelCase    :						List[Any]=1E-5							,							_lowerCAmelCase    :						str=32							,							**_lowerCAmelCase    :						List[Any]							,							):
       super().__init__(**_lowerCAmelCase		)

       SCREAMING_SNAKE_CASE_      		=     image_size
       SCREAMING_SNAKE_CASE_      		=     patch_size
       SCREAMING_SNAKE_CASE_      		=     num_channels
       SCREAMING_SNAKE_CASE_      		=     embed_dim
       SCREAMING_SNAKE_CASE_      		=     depths
       SCREAMING_SNAKE_CASE_      		=     len(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     num_heads
       SCREAMING_SNAKE_CASE_      		=     window_size
       SCREAMING_SNAKE_CASE_      		=     mlp_ratio
       SCREAMING_SNAKE_CASE_      		=     qkv_bias
       SCREAMING_SNAKE_CASE_      		=     hidden_dropout_prob
       SCREAMING_SNAKE_CASE_      		=     attention_probs_dropout_prob
       SCREAMING_SNAKE_CASE_      		=     drop_path_rate
       SCREAMING_SNAKE_CASE_      		=     hidden_act
       SCREAMING_SNAKE_CASE_      		=     use_absolute_embeddings
       SCREAMING_SNAKE_CASE_      		=     layer_norm_eps
       SCREAMING_SNAKE_CASE_      		=     initializer_range
       SCREAMING_SNAKE_CASE_      		=     encoder_stride
       # we set the hidden_size attribute in order to make Swinv2 work with VisionEncoderDecoderModel
       # this indicates the channel dimension after the last stage of the model
       SCREAMING_SNAKE_CASE_      		=     int(embed_dim * 2 ** (len(_lowerCAmelCase		) - 1)		)
       SCREAMING_SNAKE_CASE_      		=     (0, 0, 0, 0)