





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			:





			def __init__( self					,			snake_case					,			snake_case=13					,			snake_case=7					,			snake_case=True					,			snake_case=True					,			snake_case=True					,			snake_case=True					,			snake_case=99					,			snake_case=64					,			snake_case=32					,			snake_case=5					,			snake_case=4					,			snake_case=37					,			snake_case="gelu"					,			snake_case=0.1					,			snake_case=0.1					,			snake_case=512					,			snake_case=16					,			snake_case=2					,			snake_case=0.02					,			snake_case=3					,			snake_case=4					,			snake_case=None					,			)				->  List[str]:
						"""simple docstring"""





						a__	:						Dict      		=  parent
						a__	:						Union[str, Any]      		=  batch_size
						a__	:						str      		=  seq_length
						a__	:						List[Any]      		=  is_training
						a__	:						Any      		=  use_input_mask
						a__	:						str      		=  use_token_type_ids
						a__	:						Any      		=  use_labels
						a__	:						List[Any]      		=  vocab_size
						a__	:						Optional[Any]      		=  hidden_size
						a__	:						Optional[int]      		=  embedding_size
						a__	:						Dict      		=  num_hidden_layers
						a__	:						str      		=  num_attention_heads
						a__	:						Any      		=  intermediate_size
						a__	:						Optional[int]      		=  hidden_act
						a__	:						Any      		=  hidden_dropout_prob
						a__	:						List[Any]      		=  attention_probs_dropout_prob
						a__	:						Union[str, Any]      		=  max_position_embeddings
						a__	:						int      		=  type_vocab_size
						a__	:						List[Any]      		=  type_sequence_label_size
						a__	:						Union[str, Any]      		=  initializer_range
						a__	:						List[Any]      		=  num_labels
						a__	:						Dict      		=  num_choices
						a__	:						Tuple      		=  scope





			def 	_snake_case ( self      )				->  Optional[int]:
						"""simple docstring"""





						a__	:						List[str]      		=  ids_tensor([self.batch_size, self.seq_length]					,			self.vocab_size      )

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

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

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

						a__	:						Dict      		=  self.get_config()

						return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels





			def 	_snake_case ( self      )				->  Optional[Any]:
						"""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=snake_case					,			initializer_range=self.initializer_range					,			)





			def 	_snake_case ( self					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case      )				->  str:
						"""simple docstring"""





						a__	:						Dict      		=  MobileBertModel(config=snake_case      )
						model.to(snake_case      )
						model.eval()
						a__	:						Tuple      		=  model(snake_case					,			attention_mask=snake_case					,			token_type_ids=snake_case      )
						a__	:						List[Any]      		=  model(snake_case					,			token_type_ids=snake_case      )
						a__	:						Union[str, 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 	_snake_case ( self					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case      )				->  Any:
						"""simple docstring"""





						a__	:						Optional[Any]      		=  MobileBertForMaskedLM(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      )
						self.parent.assertEqual(result.logits.shape					,			(self.batch_size, self.seq_length, self.vocab_size)      )





			def 	_snake_case ( self					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case      )				->  List[str]:
						"""simple docstring"""





						a__	:						Union[str, Any]      		=  MobileBertForNextSentencePrediction(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, 2)      )





			def 	_snake_case ( self					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case      )				->  str:
						"""simple docstring"""





						a__	:						List[Any]      		=  MobileBertForPreTraining(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					,			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 	_snake_case ( self					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case      )				->  Tuple:
						"""simple docstring"""





						a__	:						int      		=  MobileBertForQuestionAnswering(config=snake_case      )
						model.to(snake_case      )
						model.eval()
						a__	:						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 	_snake_case ( self					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case      )				->  List[Any]:
						"""simple docstring"""





						a__	:						List[str]      		=  self.num_labels
						a__	:						Optional[Any]      		=  MobileBertForSequenceClassification(snake_case      )
						model.to(snake_case      )
						model.eval()
						a__	:						Dict      		=  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 	_snake_case ( self					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case      )				->  Optional[Any]:
						"""simple docstring"""





						a__	:						Optional[Any]      		=  self.num_labels
						a__	:						List[Any]      		=  MobileBertForTokenClassification(config=snake_case      )
						model.to(snake_case      )
						model.eval()
						a__	:						str      		=  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 	_snake_case ( self					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case      )				->  Dict:
						"""simple docstring"""





						a__	:						Optional[int]      		=  self.num_choices
						a__	:						Optional[Any]      		=  MobileBertForMultipleChoice(config=snake_case      )
						model.to(snake_case      )
						model.eval()
						a__	:						List[str]      		=  input_ids.unsqueeze(1      ).expand(-1					,			self.num_choices					,			-1      ).contiguous()
						a__	:						List[str]      		=  token_type_ids.unsqueeze(1      ).expand(-1					,			self.num_choices					,			-1      ).contiguous()
						a__	:						Optional[int]      		=  input_mask.unsqueeze(1      ).expand(-1					,			self.num_choices					,			-1      ).contiguous()
						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_choices)      )





			def 	_snake_case ( self      )				->  int:
						"""simple docstring"""





						a__	:						Union[str, Any]      		=  self.prepare_config_and_inputs()
						(
						    a__     
						)	:						List[str]      		=  config_and_inputs
						a__	:						Optional[Any]      		=  {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
						return config, inputs_dict







@require_torch
class   __lowerCAmelCase			(					_UpperCamelCase		,_UpperCamelCase		,unittest.TestCase  ):
			_UpperCamelCase :						Any     		=				(
			    (
			        MobileBertModel,
			        MobileBertForMaskedLM,
			        MobileBertForMultipleChoice,
			        MobileBertForNextSentencePrediction,
			        MobileBertForPreTraining,
			        MobileBertForQuestionAnswering,
			        MobileBertForSequenceClassification,
			        MobileBertForTokenClassification,
			    )
			    if is_torch_available()
			    else ()
			)
			_UpperCamelCase :						str     		=				(
			    {
			        """feature-extraction""": MobileBertModel,
			        """fill-mask""": MobileBertForMaskedLM,
			        """question-answering""": MobileBertForQuestionAnswering,
			        """text-classification""": MobileBertForSequenceClassification,
			        """token-classification""": MobileBertForTokenClassification,
			        """zero-shot""": MobileBertForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)
			_UpperCamelCase :						Optional[int]     		=				True





			def 	_snake_case ( self					,			snake_case					,			snake_case					,			snake_case=False      )				->  str:
						"""simple docstring"""





						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__	:						Union[str, Any]      		=  torch.zeros(
												    (self.model_tester.batch_size, self.model_tester.seq_length)					,			dtype=torch.long					,			device=snake_case      )
												a__	:						Any      		=  torch.zeros(
												    self.model_tester.batch_size					,			dtype=torch.long					,			device=snake_case      )
						return inputs_dict





			def 	_snake_case ( self      )				->  List[Any]:
						"""simple docstring"""





						a__	:						Union[str, Any]      		=  MobileBertModelTester(self      )
						a__	:						Optional[int]      		=  ConfigTester(self					,			config_class=snake_case					,			hidden_size=37      )





			def 	_snake_case ( self      )				->  Tuple:
						"""simple docstring"""





						self.config_tester.run_common_tests()





			def 	_snake_case ( self      )				->  Optional[int]:
						"""simple docstring"""





						a__	:						Tuple      		=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_model(*snake_case      )





			def 	_snake_case ( self      )				->  Any:
						"""simple docstring"""





						a__	:						Optional[Any]      		=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_masked_lm(*snake_case      )





			def 	_snake_case ( self      )				->  Tuple:
						"""simple docstring"""





						a__	:						str      		=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_multiple_choice(*snake_case      )





			def 	_snake_case ( self      )				->  List[Any]:
						"""simple docstring"""





						a__	:						Optional[Any]      		=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*snake_case      )





			def 	_snake_case ( self      )				->  List[Any]:
						"""simple docstring"""





						a__	:						List[Any]      		=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_pretraining(*snake_case      )





			def 	_snake_case ( self      )				->  List[str]:
						"""simple docstring"""





						a__	:						Optional[Any]      		=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_question_answering(*snake_case      )





			def 	_snake_case ( self      )				->  List[Any]:
						"""simple docstring"""





						a__	:						Optional[int]      		=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_sequence_classification(*snake_case      )





			def 	_snake_case ( self      )				->  List[str]:
						"""simple docstring"""





						a__	:						Dict      		=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mobilebert_for_token_classification(*snake_case      )







def       _A  (      lowerCamelCase   ):
			return torch.tensor(
			    lowerCamelCase							, dtype=torch.long							, device=lowerCamelCase							, )


SCREAMING_SNAKE_CASE__       :			List[Any]			      =						1e-3







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





			@slow
			def 	_snake_case ( self      )				->  Tuple:
						"""simple docstring"""





						a__	:						Optional[Any]      		=  MobileBertModel.from_pretrained("google/mobilebert-uncased"      ).to(snake_case      )
						a__	:						int      		=  _long_tensor([[101, 7_110, 1_005, 1_056, 2_023, 11_333, 17_413, 1_029, 102]]      )
						with torch.no_grad():
									a__	:						str      		=  model(snake_case      )[0]
						a__	:						List[Any]      		=  torch.Size((1, 9, 512)      )
						self.assertEqual(output.shape					,			snake_case      )
						a__	:						Optional[int]      		=  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__	:						Optional[int]      		=  torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE      )
						a__	:						Tuple      		=  torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE      )

						self.assertTrue(lower_bound and upper_bound      )








from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


SCREAMING_SNAKE_CASE__       :			Any			      =						{"""configuration_sew""": ["""SEW_PRETRAINED_CONFIG_ARCHIVE_MAP""", """SEWConfig"""]}

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  SCREAMING_SNAKE_CASE__       :			Union[str, Any]			      =						[
      """SEW_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """SEWForCTC""",
      """SEWForSequenceClassification""",
      """SEWModel""",
      """SEWPreTrainedModel""",
  ]

if TYPE_CHECKING:
  from .configuration_sew import SEW_PRETRAINED_CONFIG_ARCHIVE_MAP, SEWConfig

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_sew import (
        SEW_PRETRAINED_MODEL_ARCHIVE_LIST,
        SEWForCTC,
        SEWForSequenceClassification,
        SEWModel,
        SEWPreTrainedModel,
    )


else:
  import sys

  SCREAMING_SNAKE_CASE__       :			Optional[int]			      =						_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)

