




"""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 lowerCAmelCase__	:






			'''simple docstring'''



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

							_lowerCamelCase     :			int    =    None
							if self.use_input_mask:
											_lowerCamelCase     :			Optional[Any]    =    random_attention_mask([self.batch_size, self.seq_length]  )

							_lowerCamelCase     :			Optional[Any]    =    None
							if self.use_token_type_ids:
											_lowerCamelCase     :			Union[str, Any]    =    ids_tensor([self.batch_size, self.seq_length]  ,      self.type_vocab_size  )

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

							_lowerCamelCase     :			Any    =    self.get_config()

							return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
			def 					A_    (  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=lowercase  ,      initializer_range=self.initializer_range  ,      )
			def 					A_    (  self  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ):
							_lowerCamelCase     :			Union[str, Any]    =    MobileBertModel(config=lowercase  )
							model.to(lowercase  )
							model.eval()
							_lowerCamelCase     :			Optional[int]    =    model(lowercase  ,      attention_mask=lowercase  ,      token_type_ids=lowercase  )
							_lowerCamelCase     :			Union[str, Any]    =    model(lowercase  ,      token_type_ids=lowercase  )
							_lowerCamelCase     :			List[str]    =    model(lowercase  )

							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 					A_    (  self  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ):
							_lowerCamelCase     :			Union[str, Any]    =    MobileBertForMaskedLM(config=lowercase  )
							model.to(lowercase  )
							model.eval()
							_lowerCamelCase     :			Optional[Any]    =    model(lowercase  ,      attention_mask=lowercase  ,      token_type_ids=lowercase  ,      labels=lowercase  )
							self.parent.assertEqual(result.logits.shape  ,      (self.batch_size, self.seq_length, self.vocab_size)  )
			def 					A_    (  self  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ):
							_lowerCamelCase     :			List[Any]    =    MobileBertForNextSentencePrediction(config=lowercase  )
							model.to(lowercase  )
							model.eval()
							_lowerCamelCase     :			List[str]    =    model(
							    lowercase  ,      attention_mask=lowercase  ,      token_type_ids=lowercase  ,      labels=lowercase  ,      )
							self.parent.assertEqual(result.logits.shape  ,      (self.batch_size, 2)  )
			def 					A_    (  self  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ):
							_lowerCamelCase     :			str    =    MobileBertForPreTraining(config=lowercase  )
							model.to(lowercase  )
							model.eval()
							_lowerCamelCase     :			int    =    model(
							    lowercase  ,      attention_mask=lowercase  ,      token_type_ids=lowercase  ,      labels=lowercase  ,      next_sentence_label=lowercase  ,      )
							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 					A_    (  self  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ):
							_lowerCamelCase     :			Any    =    MobileBertForQuestionAnswering(config=lowercase  )
							model.to(lowercase  )
							model.eval()
							_lowerCamelCase     :			str    =    model(
							    lowercase  ,      attention_mask=lowercase  ,      token_type_ids=lowercase  ,      start_positions=lowercase  ,      end_positions=lowercase  ,      )
							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 					A_    (  self  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ):
							_lowerCamelCase     :			Optional[int]    =    self.num_labels
							_lowerCamelCase     :			List[str]    =    MobileBertForSequenceClassification(lowercase  )
							model.to(lowercase  )
							model.eval()
							_lowerCamelCase     :			str    =    model(lowercase  ,      attention_mask=lowercase  ,      token_type_ids=lowercase  ,      labels=lowercase  )
							self.parent.assertEqual(result.logits.shape  ,      (self.batch_size, self.num_labels)  )
			def 					A_    (  self  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ):
							_lowerCamelCase     :			Dict    =    self.num_labels
							_lowerCamelCase     :			Optional[int]    =    MobileBertForTokenClassification(config=lowercase  )
							model.to(lowercase  )
							model.eval()
							_lowerCamelCase     :			Optional[Any]    =    model(lowercase  ,      attention_mask=lowercase  ,      token_type_ids=lowercase  ,      labels=lowercase  )
							self.parent.assertEqual(result.logits.shape  ,      (self.batch_size, self.seq_length, self.num_labels)  )
			def 					A_    (  self  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ):
							_lowerCamelCase     :			List[str]    =    self.num_choices
							_lowerCamelCase     :			Optional[int]    =    MobileBertForMultipleChoice(config=lowercase  )
							model.to(lowercase  )
							model.eval()
							_lowerCamelCase     :			Optional[int]    =    input_ids.unsqueeze(1  ).expand(-1  ,      self.num_choices  ,      -1  ).contiguous()
							_lowerCamelCase     :			List[Any]    =    token_type_ids.unsqueeze(1  ).expand(-1  ,      self.num_choices  ,      -1  ).contiguous()
							_lowerCamelCase     :			List[str]    =    input_mask.unsqueeze(1  ).expand(-1  ,      self.num_choices  ,      -1  ).contiguous()
							_lowerCamelCase     :			Tuple    =    model(
							    lowercase  ,      attention_mask=lowercase  ,      token_type_ids=lowercase  ,      labels=lowercase  ,      )
							self.parent.assertEqual(result.logits.shape  ,      (self.batch_size, self.num_choices)  )






			def 					A_    (  self  ):
							_lowerCamelCase     :			Tuple    =    self.prepare_config_and_inputs()
							(
							    (
							    _lowerCamelCase
							), (
							    _lowerCamelCase
							), (
							    _lowerCamelCase
							), (
							    _lowerCamelCase
							), (
							    _lowerCamelCase
							), (
							    _lowerCamelCase
							), (
							    _lowerCamelCase
							), 
							)     :			Union[str, Any]    =    config_and_inputs
							_lowerCamelCase     :			Tuple    =    {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
							return config, inputs_dict






@require_torch
class lowerCAmelCase__	(     lowercase,	lowercase,	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 					A_    (  self  ,      lowercase  ,      lowercase  ,      lowercase=False  ):
							_lowerCamelCase     :			Optional[int]    =    super()._prepare_for_class(lowercase  ,      lowercase  ,      return_labels=lowercase  )

							if return_labels:
											if model_class in get_values(lowercase  ):
															_lowerCamelCase     :			Tuple    =    torch.zeros(
															    (self.model_tester.batch_size, self.model_tester.seq_length)  ,      dtype=torch.long  ,      device=lowercase  )
															_lowerCamelCase     :			Dict    =    torch.zeros(
															    self.model_tester.batch_size  ,      dtype=torch.long  ,      device=lowercase  )
							return inputs_dict
			def 					A_    (  self  ):
							_lowerCamelCase     :			int    =    MobileBertModelTester(self  )
							_lowerCamelCase     :			Union[str, Any]    =    ConfigTester(self  ,      config_class=lowercase  ,      hidden_size=37  )
			def 					A_    (  self  ):
							self.config_tester.run_common_tests()
			def 					A_    (  self  ):
							_lowerCamelCase     :			List[str]    =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_model(*lowercase  )
			def 					A_    (  self  ):
							_lowerCamelCase     :			int    =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_masked_lm(*lowercase  )
			def 					A_    (  self  ):
							_lowerCamelCase     :			Optional[int]    =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_multiple_choice(*lowercase  )
			def 					A_    (  self  ):
							_lowerCamelCase     :			List[Any]    =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*lowercase  )
			def 					A_    (  self  ):
							_lowerCamelCase     :			Union[str, Any]    =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_pretraining(*lowercase  )
			def 					A_    (  self  ):
							_lowerCamelCase     :			Tuple    =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_question_answering(*lowercase  )
			def 					A_    (  self  ):
							_lowerCamelCase     :			List[str]    =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_sequence_classification(*lowercase  )






			def 					A_    (  self  ):
							_lowerCamelCase     :			Dict    =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mobilebert_for_token_classification(*lowercase  )







def      _snake_case			(				lowercase__		):
				return torch.tensor(
				    lowercase__							,    dtype=torch.long							,    device=lowercase__							,    )


lowercase__     						=						1E-3






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






			'''simple docstring'''



			@slow
			def 					A_    (  self  ):
							_lowerCamelCase     :			Optional[int]    =    MobileBertModel.from_pretrained('google/mobilebert-uncased'  ).to(lowercase  )
							_lowerCamelCase     :			Tuple    =    _long_tensor([[101, 7110, 1005, 1056, 2023, 11333, 17413, 1029, 102]]  )
							with torch.no_grad():
											_lowerCamelCase     :			Any    =    model(lowercase  )[0]
							_lowerCamelCase     :			Tuple    =    torch.Size((1, 9, 512)  )
							self.assertEqual(output.shape  ,      lowercase  )
							_lowerCamelCase     :			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=lowercase  ,      )

							# 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     :			Any    =    torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE  )
							_lowerCamelCase     :			str    =    torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE  )

							self.assertTrue(lower_bound and upper_bound  )


'''simple docstring'''



from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


UpperCAmelCase :       List[str]			   =     logging.get_logger(__name__)







class 						lowerCAmelCase__   (      a ,					a					):



 """simple docstring"""



 lowerCAmelCase__  					=      "maskformer-swin"

 lowerCAmelCase__  					=      {
     "num_attention_heads": "num_heads",
     "num_hidden_layers": "num_layers",
 }
 def __init__(		self					:       str					,							__SCREAMING_SNAKE_CASE					:       Tuple=224					,							__SCREAMING_SNAKE_CASE					:       str=4					,							__SCREAMING_SNAKE_CASE					:       Union[str, Any]=3					,							__SCREAMING_SNAKE_CASE					:       Optional[Any]=96					,							__SCREAMING_SNAKE_CASE					:       Optional[Any]=[2, 2, 6, 2]					,							__SCREAMING_SNAKE_CASE					:       Any=[3, 6, 12, 24]					,							__SCREAMING_SNAKE_CASE					:       Dict=7					,							__SCREAMING_SNAKE_CASE					:       Dict=4.0					,							__SCREAMING_SNAKE_CASE					:       Dict=True					,							__SCREAMING_SNAKE_CASE					:       str=0.0					,							__SCREAMING_SNAKE_CASE					:       int=0.0					,							__SCREAMING_SNAKE_CASE					:       str=0.1					,							__SCREAMING_SNAKE_CASE					:       List[Any]="gelu"					,							__SCREAMING_SNAKE_CASE					:       str=False					,							__SCREAMING_SNAKE_CASE					:       Optional[int]=0.02					,							__SCREAMING_SNAKE_CASE					:       Optional[int]=1E-5					,							__SCREAMING_SNAKE_CASE					:       Dict=None					,							__SCREAMING_SNAKE_CASE					:       Dict=None					,							**__SCREAMING_SNAKE_CASE					:       Tuple					,							)  -> Tuple:


      """simple docstring"""




      super().__init__(**__SCREAMING_SNAKE_CASE			)

      __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(__SCREAMING_SNAKE_CASE			)
      __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
      # we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel
      # this indicates the channel dimension after the last stage of the model
      __SCREAMING_SNAKE_CASE     =      int(embed_dim * 2 ** (len(__SCREAMING_SNAKE_CASE			) - 1)			)
      __SCREAMING_SNAKE_CASE     =      ["""stem"""] + [f'stage{idx}' for idx in range(1					,							len(__SCREAMING_SNAKE_CASE			) + 1			)]
      __SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE     =      get_aligned_output_features_output_indices(
          out_features=__SCREAMING_SNAKE_CASE					,							out_indices=__SCREAMING_SNAKE_CASE					,							stage_names=self.stage_names			)






