
import inspect
import unittest

from transformers import MobileViTConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch

							from transformers import MobileViTForImageClassification, MobileViTForSemanticSegmentation, MobileViTModel
							from transformers.models.mobilevit.modeling_mobilevit import MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
							from PIL import Image

							from transformers import MobileViTImageProcessor







class 				lowercase		(					__lowercase		):




							def  A__      (				self):
												lowercase							=						self.config_class(**self.inputs_dict)
												self.parent.assertTrue(hasattr(snake_case_   ,'''hidden_sizes'''))
												self.parent.assertTrue(hasattr(snake_case_   ,'''neck_hidden_sizes'''))
												self.parent.assertTrue(hasattr(snake_case_   ,'''num_attention_heads'''))







class 				lowercase		:




							def __init__(				self   ,A__   ,A__=1_3   ,A__=3_2   ,A__=2   ,A__=3   ,A__=6_4_0   ,A__=4   ,A__="silu"   ,A__=3   ,A__=3_2   ,A__=0.1   ,A__=0.1   ,A__=0.1   ,A__=0.02   ,A__=True   ,A__=True   ,A__=1_0   ,A__=None   ,):
												lowercase							=						parent
												lowercase							=						batch_size
												lowercase							=						image_size
												lowercase							=						patch_size
												lowercase							=						num_channels
												lowercase							=						last_hidden_size
												lowercase							=						num_attention_heads
												lowercase							=						hidden_act
												lowercase							=						conv_kernel_size
												lowercase							=						output_stride
												lowercase							=						hidden_dropout_prob
												lowercase							=						attention_probs_dropout_prob
												lowercase							=						classifier_dropout_prob
												lowercase							=						use_labels
												lowercase							=						is_training
												lowercase							=						num_labels
												lowercase							=						initializer_range
												lowercase							=						scope




							def  A__      (				self):
												lowercase							=						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

												lowercase							=						None
												lowercase							=						None
												if self.use_labels:
																	lowercase							=						ids_tensor([self.batch_size]   ,self.num_labels)
																	lowercase							=						ids_tensor([self.batch_size, self.image_size, self.image_size]   ,self.num_labels)

												lowercase							=						self.get_config()

												return config, pixel_values, labels, pixel_labels




							def  A__      (				self):
												return MobileViTConfig(
												    image_size=self.image_size   ,patch_size=self.patch_size   ,num_channels=self.num_channels   ,num_attention_heads=self.num_attention_heads   ,hidden_act=self.hidden_act   ,conv_kernel_size=self.conv_kernel_size   ,output_stride=self.output_stride   ,hidden_dropout_prob=self.hidden_dropout_prob   ,attention_probs_dropout_prob=self.attention_probs_dropout_prob   ,classifier_dropout_prob=self.classifier_dropout_prob   ,initializer_range=self.initializer_range   ,)




							def  A__      (				self   ,A__   ,A__   ,A__   ,A__):
												lowercase							=						MobileViTModel(config=snake_case_)
												model.to(snake_case_)
												model.eval()
												lowercase							=						model(snake_case_)
												self.parent.assertEqual(
												    result.last_hidden_state.shape   ,(
												        self.batch_size,
												        self.last_hidden_size,
												        self.image_size // self.output_stride,
												        self.image_size // self.output_stride,
												    )   ,)




							def  A__      (				self   ,A__   ,A__   ,A__   ,A__):
												lowercase							=						self.num_labels
												lowercase							=						MobileViTForImageClassification(snake_case_)
												model.to(snake_case_)
												model.eval()
												lowercase							=						model(snake_case_   ,labels=snake_case_)
												self.parent.assertEqual(result.logits.shape   ,(self.batch_size, self.num_labels))




							def  A__      (				self   ,A__   ,A__   ,A__   ,A__):
												lowercase							=						self.num_labels
												lowercase							=						MobileViTForSemanticSegmentation(snake_case_)
												model.to(snake_case_)
												model.eval()
												lowercase							=						model(snake_case_)
												self.parent.assertEqual(
												    result.logits.shape   ,(
												        self.batch_size,
												        self.num_labels,
												        self.image_size // self.output_stride,
												        self.image_size // self.output_stride,
												    )   ,)
												lowercase							=						model(snake_case_   ,labels=snake_case_)
												self.parent.assertEqual(
												    result.logits.shape   ,(
												        self.batch_size,
												        self.num_labels,
												        self.image_size // self.output_stride,
												        self.image_size // self.output_stride,
												    )   ,)





							def  A__      (				self):
												lowercase							=						self.prepare_config_and_inputs()
												lowercase							=						config_and_inputs
												lowercase							=						{'''pixel_values''': pixel_values}
												return config, inputs_dict







@require_torch
class 				lowercase		(					__lowercase       ,  __lowercase       ,  unittest.TestCase		):
							lowercase_       :       Optional[int]       					=(
							    (MobileViTModel, MobileViTForImageClassification, MobileViTForSemanticSegmentation)
							    if is_torch_available()
							    else ()
							)
							lowercase_       :       int       					=(
							    {
							        "feature-extraction": MobileViTModel,
							        "image-classification": MobileViTForImageClassification,
							        "image-segmentation": MobileViTForSemanticSegmentation,
							    }
							    if is_torch_available()
							    else {}
							)

							lowercase_       :       str       					=False
							lowercase_       :       List[str]       					=False
							lowercase_       :       str       					=False
							lowercase_       :       str       					=False




							def  A__      (				self):
												lowercase							=						MobileViTModelTester(self)
												lowercase							=						MobileViTConfigTester(self   ,config_class=snake_case_   ,has_text_modality=snake_case_)




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




							@unittest.skip(reason='''MobileViT does not use inputs_embeds''')
							def  A__      (				self):
												pass




							@unittest.skip(reason='''MobileViT does not support input and output embeddings''')
							def  A__      (				self):
												pass




							@unittest.skip(reason='''MobileViT does not output attentions''')
							def  A__      (				self):
												pass




							def  A__      (				self):
												lowercase							=						self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																	lowercase							=						model_class(snake_case_)
																	lowercase							=						inspect.signature(model.forward)
																	# signature.parameters is an OrderedDict => so arg_names order is deterministic
																	lowercase							=						[*signature.parameters.keys()]

																	lowercase							=						['''pixel_values''']
																	self.assertListEqual(arg_names[:1]   ,snake_case_)




							@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''')
							def  A__      (				self):
												pass




							def  A__      (				self):
												lowercase							=						self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*snake_case_)




							def  A__      (				self):
												def check_hidden_states_output(A__   ,A__   ,A__):
																	lowercase							=						model_class(snake_case_)
																	model.to(snake_case_)
																	model.eval()

																	with torch.no_grad():
																						lowercase							=						model(**self._prepare_for_class(snake_case_   ,snake_case_))

																	lowercase							=						outputs.hidden_states

																	lowercase							=						5
																	self.assertEqual(len(snake_case_)   ,snake_case_)

																	# MobileViT's feature maps are of shape (batch_size, num_channels, height, width)
																	# with the width and height being successively divided by 2.
																	lowercase							=						2
																	for i in range(len(snake_case_)):
																						self.assertListEqual(
																						    list(hidden_states[i].shape[-2:])   ,[self.model_tester.image_size // divisor, self.model_tester.image_size // divisor]   ,)
																						divisor *= 2

																	self.assertEqual(self.model_tester.output_stride   ,divisor // 2)

												lowercase							=						self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																	lowercase							=						True
																	check_hidden_states_output(snake_case_   ,snake_case_   ,snake_case_)

																	# check that output_hidden_states also work using config
																	del inputs_dict["output_hidden_states"]
																	lowercase							=						True

																	check_hidden_states_output(snake_case_   ,snake_case_   ,snake_case_)




							def  A__      (				self):
												lowercase							=						self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_image_classification(*snake_case_)




							def  A__      (				self):
												lowercase							=						self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_semantic_segmentation(*snake_case_)





							@slow
							def  A__      (				self):
												for model_name in MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	lowercase							=						MobileViTModel.from_pretrained(snake_case_)
																	self.assertIsNotNone(snake_case_)



def        UpperCamelCase    (							):
					'''simple docstring'''
					lowercase							=						Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
					return image







@require_torch
@require_vision
class 				lowercase		(					unittest.TestCase		):




							@cached_property
							def  A__      (				self):
												return MobileViTImageProcessor.from_pretrained('''apple/mobilevit-xx-small''') if is_vision_available() else None




							@slow
							def  A__      (				self):
												lowercase							=						MobileViTForImageClassification.from_pretrained('''apple/mobilevit-xx-small''').to(snake_case_)

												lowercase							=						self.default_image_processor
												lowercase							=						prepare_img()
												lowercase							=						image_processor(images=snake_case_   ,return_tensors='''pt''').to(snake_case_)

												# forward pass
												with torch.no_grad():
																	lowercase							=						model(**snake_case_)

												# verify the logits
												lowercase							=						torch.Size((1, 1_0_0_0))
												self.assertEqual(outputs.logits.shape   ,snake_case_)

												lowercase							=						torch.tensor([-1.9364, -1.2327, -0.4653]).to(snake_case_)

												self.assertTrue(torch.allclose(outputs.logits[0, :3]   ,snake_case_   ,atol=1E-4))




							@slow
							def  A__      (				self):
												lowercase							=						MobileViTForSemanticSegmentation.from_pretrained('''apple/deeplabv3-mobilevit-xx-small''')
												lowercase							=						model.to(snake_case_)

												lowercase							=						MobileViTImageProcessor.from_pretrained('''apple/deeplabv3-mobilevit-xx-small''')

												lowercase							=						prepare_img()
												lowercase							=						image_processor(images=snake_case_   ,return_tensors='''pt''').to(snake_case_)

												# forward pass
												with torch.no_grad():
																	lowercase							=						model(**snake_case_)
												lowercase							=						outputs.logits

												# verify the logits
												lowercase							=						torch.Size((1, 2_1, 3_2, 3_2))
												self.assertEqual(logits.shape   ,snake_case_)

												lowercase							=						torch.tensor(
												    [
												        [[6.9713, 6.9786, 7.2422], [7.2893, 7.2825, 7.4446], [7.6580, 7.8797, 7.9420]],
												        [[-10.6869, -10.3250, -10.3471], [-10.4228, -9.9868, -9.7132], [-11.0405, -11.0221, -10.7318]],
												        [[-3.3089, -2.8539, -2.6740], [-3.2706, -2.5621, -2.5108], [-3.2534, -2.6615, -2.6651]],
												    ]   ,device=snake_case_   ,)

												self.assertTrue(torch.allclose(logits[0, :3, :3, :3]   ,snake_case_   ,atol=1E-4))





							@slow
							def  A__      (				self):
												lowercase							=						MobileViTForSemanticSegmentation.from_pretrained('''apple/deeplabv3-mobilevit-xx-small''')
												lowercase							=						model.to(snake_case_)

												lowercase							=						MobileViTImageProcessor.from_pretrained('''apple/deeplabv3-mobilevit-xx-small''')

												lowercase							=						prepare_img()
												lowercase							=						image_processor(images=snake_case_   ,return_tensors='''pt''').to(snake_case_)

												# forward pass
												with torch.no_grad():
																	lowercase							=						model(**snake_case_)

												lowercase							=						outputs.logits.detach().cpu()

												lowercase							=						image_processor.post_process_semantic_segmentation(outputs=snake_case_   ,target_sizes=[(5_0, 6_0)])
												lowercase							=						torch.Size((5_0, 6_0))
												self.assertEqual(segmentation[0].shape   ,snake_case_)

												lowercase							=						image_processor.post_process_semantic_segmentation(outputs=snake_case_)
												lowercase							=						torch.Size((3_2, 3_2))
												self.assertEqual(segmentation[0].shape   ,snake_case_)






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__  :
   def __init__(self :					List[Any]					,				snake_case_ :					int					,				snake_case_ :					List[str]=1_3					,				snake_case_ :					Tuple=7					,				snake_case_ :					List[Any]=True					,				snake_case_ :					List[Any]=True					,				snake_case_ :					Dict=True					,				snake_case_ :					Optional[int]=True					,				snake_case_ :					str=9_9					,				snake_case_ :					Dict=6_4					,				snake_case_ :					Any=3_2					,				snake_case_ :					str=5					,				snake_case_ :					int=4					,				snake_case_ :					List[Any]=3_7					,				snake_case_ :					Any="gelu"					,				snake_case_ :					Dict=0.1					,				snake_case_ :					List[str]=0.1					,				snake_case_ :					str=5_1_2					,				snake_case_ :					Any=1_6					,				snake_case_ :					str=2					,				snake_case_ :					int=0.02					,				snake_case_ :					Union[str, Any]=3					,				snake_case_ :					Optional[Any]=4					,				snake_case_ :					List[Any]=None					,				):
    __a :  Any						=  parent
    __a :  Optional[int]						=  batch_size
    __a :  Any						=  seq_length
    __a :  int						=  is_training
    __a :  Optional[int]						=  use_input_mask
    __a :  List[Any]						=  use_token_type_ids
    __a :  Dict						=  use_labels
    __a :  Tuple						=  vocab_size
    __a :  str						=  hidden_size
    __a :  List[Any]						=  embedding_size
    __a :  List[Any]						=  num_hidden_layers
    __a :  str						=  num_attention_heads
    __a :  str						=  intermediate_size
    __a :  Union[str, Any]						=  hidden_act
    __a :  Optional[Any]						=  hidden_dropout_prob
    __a :  Tuple						=  attention_probs_dropout_prob
    __a :  Union[str, Any]						=  max_position_embeddings
    __a :  Any						=  type_vocab_size
    __a :  int						=  type_sequence_label_size
    __a :  int						=  initializer_range
    __a :  int						=  num_labels
    __a :  Union[str, Any]						=  num_choices
    __a :  Dict						=  scope
   def       lowerCAmelCase			(self :					str   ):
    __a :  int						=  ids_tensor([self.batch_size, self.seq_length]					,				self.vocab_size   )

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

    __a :  Optional[Any]						=  None
    if self.use_token_type_ids:
     __a :  List[Any]						=  ids_tensor([self.batch_size, self.seq_length]					,				self.type_vocab_size   )

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

    __a :  Optional[int]						=  self.get_config()

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

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







   def       lowerCAmelCase			(self :					Optional[Any]   ):
    __a :  Optional[Any]						=  self.prepare_config_and_inputs()
    (
        (
        __a
    )				,       (
        __a
    )				,       (
        __a
    )				,       (
        __a
    )				,       (
        __a
    )				,       (
        __a
    )				,       (
        __a
    )				,       
    ) :  int						=  config_and_inputs
    __a :  Union[str, Any]						=  {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
    return config, inputs_dict





@require_torch
class 		UpperCamelCase__  (       __lowercase    ,__lowercase    ,unittest.TestCase    ):
   _SCREAMING_SNAKE_CASE      :							str	       =      (
       (
           MobileBertModel,
           MobileBertForMaskedLM,
           MobileBertForMultipleChoice,
           MobileBertForNextSentencePrediction,
           MobileBertForPreTraining,
           MobileBertForQuestionAnswering,
           MobileBertForSequenceClassification,
           MobileBertForTokenClassification,
       )
       if is_torch_available()
       else ()
   )
   _SCREAMING_SNAKE_CASE      :							Any	       =      (
       {
           "feature-extraction": MobileBertModel,
           "fill-mask": MobileBertForMaskedLM,
           "question-answering": MobileBertForQuestionAnswering,
           "text-classification": MobileBertForSequenceClassification,
           "token-classification": MobileBertForTokenClassification,
           "zero-shot": MobileBertForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )
   _SCREAMING_SNAKE_CASE      :							Union[str, Any]	       =      True
   def       lowerCAmelCase			(self :					str					,				snake_case_ :					Tuple					,				snake_case_ :					List[Any]					,				snake_case_ :					Union[str, Any]=False   ):
    __a :  List[str]						=  super()._prepare_for_class(snake_case_					,				snake_case_					,				return_labels=snake_case_   )

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







   def       lowerCAmelCase			(self :					Tuple   ):
    __a :  Optional[int]						=  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_mobilebert_for_token_classification(*snake_case_   )





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


lowercase__       =1e-3



@require_torch
@require_sentencepiece
@require_tokenizers
class 		UpperCamelCase__  (       unittest.TestCase    ):
   @slow
   def       lowerCAmelCase			(self :					Any   ):
    __a :  Dict						=  MobileBertModel.from_pretrained('''google/mobilebert-uncased'''   ).to(snake_case_   )
    __a :  Tuple						=  _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 :  str						=  model(snake_case_   )[0]
    __a :  List[Any]						=  torch.Size((1, 9, 5_1_2)   )
    self.assertEqual(output.shape					,				snake_case_   )
    __a :  Union[str, 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=snake_case_					,				)

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

    self.assertTrue(lower_bound and upper_bound   )
