






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    ,	_lowerCAmelCase    ,	_lowerCAmelCase=13    ,	_lowerCAmelCase=7    ,	_lowerCAmelCase=True    ,	_lowerCAmelCase=True    ,	_lowerCAmelCase=True    ,	_lowerCAmelCase=True    ,	_lowerCAmelCase=99    ,	_lowerCAmelCase=64    ,	_lowerCAmelCase=32    ,	_lowerCAmelCase=5    ,	_lowerCAmelCase=4    ,	_lowerCAmelCase=37    ,	_lowerCAmelCase="gelu"    ,	_lowerCAmelCase=0.1    ,	_lowerCAmelCase=0.1    ,	_lowerCAmelCase=512    ,	_lowerCAmelCase=16    ,	_lowerCAmelCase=2    ,	_lowerCAmelCase=0.02    ,	_lowerCAmelCase=3    ,	_lowerCAmelCase=4    ,	_lowerCAmelCase=None    ,	):



          '''simple docstring'''
          lowerCAmelCase__   :Dict      							=				parent
          lowerCAmelCase__   :Dict      							=				batch_size
          lowerCAmelCase__   :str      							=				seq_length
          lowerCAmelCase__   :Union[str, Any]      							=				is_training
          lowerCAmelCase__   :Optional[int]      							=				use_input_mask
          lowerCAmelCase__   :List[Any]      							=				use_token_type_ids
          lowerCAmelCase__   :List[Any]      							=				use_labels
          lowerCAmelCase__   :List[str]      							=				vocab_size
          lowerCAmelCase__   :Any      							=				hidden_size
          lowerCAmelCase__   :Union[str, Any]      							=				embedding_size
          lowerCAmelCase__   :Optional[int]      							=				num_hidden_layers
          lowerCAmelCase__   :List[str]      							=				num_attention_heads
          lowerCAmelCase__   :Any      							=				intermediate_size
          lowerCAmelCase__   :Tuple      							=				hidden_act
          lowerCAmelCase__   :Any      							=				hidden_dropout_prob
          lowerCAmelCase__   :Tuple      							=				attention_probs_dropout_prob
          lowerCAmelCase__   :Dict      							=				max_position_embeddings
          lowerCAmelCase__   :Any      							=				type_vocab_size
          lowerCAmelCase__   :List[str]      							=				type_sequence_label_size
          lowerCAmelCase__   :str      							=				initializer_range
          lowerCAmelCase__   :Union[str, Any]      							=				num_labels
          lowerCAmelCase__   :Optional[int]      							=				num_choices
          lowerCAmelCase__   :str      							=				scope
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :List[Any]      							=				ids_tensor([self.batch_size, self.seq_length]    ,	self.vocab_size		)

          lowerCAmelCase__   :Dict      							=				None
          if self.use_input_mask:
              lowerCAmelCase__   :List[str]      							=				random_attention_mask([self.batch_size, self.seq_length]		)

          lowerCAmelCase__   :Dict      							=				None
          if self.use_token_type_ids:
              lowerCAmelCase__   :Optional[int]      							=				ids_tensor([self.batch_size, self.seq_length]    ,	self.type_vocab_size		)

          lowerCAmelCase__   :int      							=				None
          lowerCAmelCase__   :Dict      							=				None
          lowerCAmelCase__   :Dict      							=				None
          if self.use_labels:
              lowerCAmelCase__   :Union[str, Any]      							=				ids_tensor([self.batch_size]    ,	self.type_sequence_label_size		)
              lowerCAmelCase__   :Dict      							=				ids_tensor([self.batch_size, self.seq_length]    ,	self.num_labels		)
              lowerCAmelCase__   :Union[str, Any]      							=				ids_tensor([self.batch_size]    ,	self.num_choices		)

          lowerCAmelCase__   :List[str]      							=				self.get_config()

          return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
      def 			snake_case_      (   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 			snake_case_      (   self    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase		):



          '''simple docstring'''
          lowerCAmelCase__   :str      							=				MobileBertModel(config=_lowerCAmelCase		)
          model.to(_lowerCAmelCase		)
          model.eval()
          lowerCAmelCase__   :List[str]      							=				model(_lowerCAmelCase    ,	attention_mask=_lowerCAmelCase    ,	token_type_ids=_lowerCAmelCase		)
          lowerCAmelCase__   :Dict      							=				model(_lowerCAmelCase    ,	token_type_ids=_lowerCAmelCase		)
          lowerCAmelCase__   :Tuple      							=				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 			snake_case_      (   self    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase		):



          '''simple docstring'''
          lowerCAmelCase__   :str      							=				MobileBertForMaskedLM(config=_lowerCAmelCase		)
          model.to(_lowerCAmelCase		)
          model.eval()
          lowerCAmelCase__   :int      							=				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 			snake_case_      (   self    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase		):



          '''simple docstring'''
          lowerCAmelCase__   :List[Any]      							=				MobileBertForNextSentencePrediction(config=_lowerCAmelCase		)
          model.to(_lowerCAmelCase		)
          model.eval()
          lowerCAmelCase__   :List[str]      							=				model(
              _lowerCAmelCase    ,	attention_mask=_lowerCAmelCase    ,	token_type_ids=_lowerCAmelCase    ,	labels=_lowerCAmelCase    ,	)
          self.parent.assertEqual(result.logits.shape    ,	(self.batch_size, 2)		)
      def 			snake_case_      (   self    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase		):



          '''simple docstring'''
          lowerCAmelCase__   :int      							=				MobileBertForPreTraining(config=_lowerCAmelCase		)
          model.to(_lowerCAmelCase		)
          model.eval()
          lowerCAmelCase__   :Optional[Any]      							=				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 			snake_case_      (   self    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase		):



          '''simple docstring'''
          lowerCAmelCase__   :List[Any]      							=				MobileBertForQuestionAnswering(config=_lowerCAmelCase		)
          model.to(_lowerCAmelCase		)
          model.eval()
          lowerCAmelCase__   :Tuple      							=				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 			snake_case_      (   self    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase		):



          '''simple docstring'''
          lowerCAmelCase__   :Union[str, Any]      							=				self.num_labels
          lowerCAmelCase__   :Dict      							=				MobileBertForSequenceClassification(_lowerCAmelCase		)
          model.to(_lowerCAmelCase		)
          model.eval()
          lowerCAmelCase__   :Tuple      							=				model(_lowerCAmelCase    ,	attention_mask=_lowerCAmelCase    ,	token_type_ids=_lowerCAmelCase    ,	labels=_lowerCAmelCase		)
          self.parent.assertEqual(result.logits.shape    ,	(self.batch_size, self.num_labels)		)
      def 			snake_case_      (   self    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase		):



          '''simple docstring'''
          lowerCAmelCase__   :Optional[int]      							=				self.num_labels
          lowerCAmelCase__   :Optional[Any]      							=				MobileBertForTokenClassification(config=_lowerCAmelCase		)
          model.to(_lowerCAmelCase		)
          model.eval()
          lowerCAmelCase__   :Any      							=				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 			snake_case_      (   self    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase		):



          '''simple docstring'''
          lowerCAmelCase__   :Union[str, Any]      							=				self.num_choices
          lowerCAmelCase__   :str      							=				MobileBertForMultipleChoice(config=_lowerCAmelCase		)
          model.to(_lowerCAmelCase		)
          model.eval()
          lowerCAmelCase__   :Optional[Any]      							=				input_ids.unsqueeze(1		).expand(-1    ,	self.num_choices    ,	-1		).contiguous()
          lowerCAmelCase__   :Tuple      							=				token_type_ids.unsqueeze(1		).expand(-1    ,	self.num_choices    ,	-1		).contiguous()
          lowerCAmelCase__   :Optional[Any]      							=				input_mask.unsqueeze(1		).expand(-1    ,	self.num_choices    ,	-1		).contiguous()
          lowerCAmelCase__   :Union[str, Any]      							=				model(
              _lowerCAmelCase    ,	attention_mask=_lowerCAmelCase    ,	token_type_ids=_lowerCAmelCase    ,	labels=_lowerCAmelCase    ,	)
          self.parent.assertEqual(result.logits.shape    ,	(self.batch_size, self.num_choices)		)



      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Union[str, Any]      							=				self.prepare_config_and_inputs()
          (
              (
              lowerCAmelCase__
          )							,(
              lowerCAmelCase__
          )							,(
              lowerCAmelCase__
          )							,(
              lowerCAmelCase__
          )							,(
              lowerCAmelCase__
          )							,(
              lowerCAmelCase__
          )							,(
              lowerCAmelCase__
          )							,
          )   :Any      							=				config_and_inputs
          lowerCAmelCase__   :Optional[Any]      							=				{"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"""



      A          =    (
          (
              MobileBertModel,
              MobileBertForMaskedLM,
              MobileBertForMultipleChoice,
              MobileBertForNextSentencePrediction,
              MobileBertForPreTraining,
              MobileBertForQuestionAnswering,
              MobileBertForSequenceClassification,
              MobileBertForTokenClassification,
          )
          if is_torch_available()
          else ()
      )
      A          =    (
          {
              '''feature-extraction''': MobileBertModel,
              '''fill-mask''': MobileBertForMaskedLM,
              '''question-answering''': MobileBertForQuestionAnswering,
              '''text-classification''': MobileBertForSequenceClassification,
              '''token-classification''': MobileBertForTokenClassification,
              '''zero-shot''': MobileBertForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      A          =    True
      def 			snake_case_      (   self    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase=False		):



          '''simple docstring'''
          lowerCAmelCase__   :Dict      							=				super()._prepare_for_class(_lowerCAmelCase    ,	_lowerCAmelCase    ,	return_labels=_lowerCAmelCase		)

          if return_labels:
              if model_class in get_values(_lowerCAmelCase		):
                  lowerCAmelCase__   :Dict      							=				torch.zeros(
                      (self.model_tester.batch_size, self.model_tester.seq_length)    ,	dtype=torch.long    ,	device=_lowerCAmelCase		)
                  lowerCAmelCase__   :List[Any]      							=				torch.zeros(
                      self.model_tester.batch_size    ,	dtype=torch.long    ,	device=_lowerCAmelCase		)
          return inputs_dict
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Optional[int]      							=				MobileBertModelTester(self		)
          lowerCAmelCase__   :List[str]      							=				ConfigTester(self    ,	config_class=_lowerCAmelCase    ,	hidden_size=37		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          self.config_tester.run_common_tests()
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Optional[int]      							=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_model(*_lowerCAmelCase		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Optional[int]      							=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_masked_lm(*_lowerCAmelCase		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :int      							=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_multiple_choice(*_lowerCAmelCase		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Optional[Any]      							=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*_lowerCAmelCase		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :int      							=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_pretraining(*_lowerCAmelCase		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Tuple      							=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_question_answering(*_lowerCAmelCase		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :str      							=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_sequence_classification(*_lowerCAmelCase		)



      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :List[str]      							=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_mobilebert_for_token_classification(*_lowerCAmelCase		)





def 				snake_case__							(							UpperCAmelCase							:  Optional[Any]       ):
    return torch.tensor(
        UpperCAmelCase    ,			dtype=torch.long    ,			device=UpperCAmelCase    ,			)


_a    :   Optional[Any]        =			1e-3







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




      """simple docstring"""



      @slow
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Optional[int]      							=				MobileBertModel.from_pretrained("google/mobilebert-uncased"		).to(_lowerCAmelCase		)
          lowerCAmelCase__   :Any      							=				_long_tensor([[101, 7_110, 1_005, 1_056, 2_023, 11_333, 17_413, 1_029, 102]]		)
          with torch.no_grad():
              lowerCAmelCase__   :Tuple      							=				model(_lowerCAmelCase		)[0]
          lowerCAmelCase__   :Dict      							=				torch.Size((1, 9, 512)		)
          self.assertEqual(output.shape    ,	_lowerCAmelCase		)
          lowerCAmelCase__   :List[Any]      							=				torch.tensor(
              [
                  [
                      [-2.4736526e07, 8.2691656e04, 1.6521838e05],
                      [-5.7541704e-01, 3.9056022e00, 4.4011507e00],
                      [2.6047359e00, 1.5677652e00, -1.7324188e-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
          lowerCAmelCase__   :Dict      							=				torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE		)
          lowerCAmelCase__   :Optional[int]      							=				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


_a    :   Dict        =			logging.get_logger(__name__)

_a    :   Optional[Any]        =			{
    """caidas/swin2sr-classicalsr-x2-64""": (
        """https://huggingface.co/caidas/swin2sr-classicalsr-x2-64/resolve/main/config.json"""
    ),
}







class 	_UpperCAmelCase  (  _A		):




      """simple docstring"""



      A          =    '''swin2sr'''

      A          =    {
          '''hidden_size''': '''embed_dim''',
          '''num_attention_heads''': '''num_heads''',
          '''num_hidden_layers''': '''num_layers''',
      }
      def __init__(   self    ,	_lowerCAmelCase=64    ,	_lowerCAmelCase=1    ,	_lowerCAmelCase=3    ,	_lowerCAmelCase=180    ,	_lowerCAmelCase=[6, 6, 6, 6, 6, 6]    ,	_lowerCAmelCase=[6, 6, 6, 6, 6, 6]    ,	_lowerCAmelCase=8    ,	_lowerCAmelCase=2.0    ,	_lowerCAmelCase=True    ,	_lowerCAmelCase=0.0    ,	_lowerCAmelCase=0.0    ,	_lowerCAmelCase=0.1    ,	_lowerCAmelCase="gelu"    ,	_lowerCAmelCase=False    ,	_lowerCAmelCase=0.02    ,	_lowerCAmelCase=1e-5    ,	_lowerCAmelCase=2    ,	_lowerCAmelCase=1.0    ,	_lowerCAmelCase="1conv"    ,	_lowerCAmelCase="pixelshuffle"    ,	**_lowerCAmelCase    ,	):



          '''simple docstring'''
          super().__init__(**_lowerCAmelCase		)

          lowerCAmelCase__   :Union[str, Any]      							=				image_size
          lowerCAmelCase__   :List[Any]      							=				patch_size
          lowerCAmelCase__   :Any      							=				num_channels
          lowerCAmelCase__   :Union[str, Any]      							=				embed_dim
          lowerCAmelCase__   :Optional[int]      							=				depths
          lowerCAmelCase__   :int      							=				len(_lowerCAmelCase		)
          lowerCAmelCase__   :List[str]      							=				num_heads
          lowerCAmelCase__   :str      							=				window_size
          lowerCAmelCase__   :List[str]      							=				mlp_ratio
          lowerCAmelCase__   :List[Any]      							=				qkv_bias
          lowerCAmelCase__   :Union[str, Any]      							=				hidden_dropout_prob
          lowerCAmelCase__   :Optional[int]      							=				attention_probs_dropout_prob
          lowerCAmelCase__   :Dict      							=				drop_path_rate
          lowerCAmelCase__   :Tuple      							=				hidden_act
          lowerCAmelCase__   :Dict      							=				use_absolute_embeddings
          lowerCAmelCase__   :Tuple      							=				layer_norm_eps
          lowerCAmelCase__   :Dict      							=				initializer_range
          lowerCAmelCase__   :Optional[int]      							=				upscale
          lowerCAmelCase__   :Optional[Any]      							=				img_range
          lowerCAmelCase__   :List[str]      							=				resi_connection
          lowerCAmelCase__   :Union[str, Any]      							=				upsampler



