






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







    def __init__(			self							,		_lowerCamelCase							,		_lowerCamelCase=1_3							,		_lowerCamelCase=7							,		_lowerCamelCase=True							,		_lowerCamelCase=True							,		_lowerCamelCase=True							,		_lowerCamelCase=True							,		_lowerCamelCase=9_9							,		_lowerCamelCase=6_4							,		_lowerCamelCase=3_2							,		_lowerCamelCase=5							,		_lowerCamelCase=4							,		_lowerCamelCase=3_7							,		_lowerCamelCase="gelu"							,		_lowerCamelCase=0.1							,		_lowerCamelCase=0.1							,		_lowerCamelCase=5_1_2							,		_lowerCamelCase=1_6							,		_lowerCamelCase=2							,		_lowerCamelCase=0.0_2							,		_lowerCamelCase=3							,		_lowerCamelCase=4							,		_lowerCamelCase=None							,		):
      lowercase							    =       parent
      lowercase							    =       batch_size
      lowercase							    =       seq_length
      lowercase							    =       is_training
      lowercase							    =       use_input_mask
      lowercase							    =       use_token_type_ids
      lowercase							    =       use_labels
      lowercase							    =       vocab_size
      lowercase							    =       hidden_size
      lowercase							    =       embedding_size
      lowercase							    =       num_hidden_layers
      lowercase							    =       num_attention_heads
      lowercase							    =       intermediate_size
      lowercase							    =       hidden_act
      lowercase							    =       hidden_dropout_prob
      lowercase							    =       attention_probs_dropout_prob
      lowercase							    =       max_position_embeddings
      lowercase							    =       type_vocab_size
      lowercase							    =       type_sequence_label_size
      lowercase							    =       initializer_range
      lowercase							    =       num_labels
      lowercase							    =       num_choices
      lowercase							    =       scope







    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       ids_tensor([self.batch_size, self.seq_length]							,		self.vocab_size       )

      lowercase							    =       None
      if self.use_input_mask:
        lowercase							    =       random_attention_mask([self.batch_size, self.seq_length]       )

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

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

      lowercase							    =       self.get_config()

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







    def 			UpperCamelCase_      (			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=_lowerCamelCase							,		initializer_range=self.initializer_range							,		)







    def 			UpperCamelCase_      (			self							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase       ):
      lowercase							    =       MobileBertModel(config=_lowerCamelCase       )
      model.to(_lowerCamelCase       )
      model.eval()
      lowercase							    =       model(_lowerCamelCase							,		attention_mask=_lowerCamelCase							,		token_type_ids=_lowerCamelCase       )
      lowercase							    =       model(_lowerCamelCase							,		token_type_ids=_lowerCamelCase       )
      lowercase							    =       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 			UpperCamelCase_      (			self							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase       ):
      lowercase							    =       MobileBertForMaskedLM(config=_lowerCamelCase       )
      model.to(_lowerCamelCase       )
      model.eval()
      lowercase							    =       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 			UpperCamelCase_      (			self							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase       ):
      lowercase							    =       MobileBertForNextSentencePrediction(config=_lowerCamelCase       )
      model.to(_lowerCamelCase       )
      model.eval()
      lowercase							    =       model(
          _lowerCamelCase							,		attention_mask=_lowerCamelCase							,		token_type_ids=_lowerCamelCase							,		labels=_lowerCamelCase							,		)
      self.parent.assertEqual(result.logits.shape							,		(self.batch_size, 2)       )







    def 			UpperCamelCase_      (			self							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase       ):
      lowercase							    =       MobileBertForPreTraining(config=_lowerCamelCase       )
      model.to(_lowerCamelCase       )
      model.eval()
      lowercase							    =       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 			UpperCamelCase_      (			self							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase       ):
      lowercase							    =       MobileBertForQuestionAnswering(config=_lowerCamelCase       )
      model.to(_lowerCamelCase       )
      model.eval()
      lowercase							    =       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 			UpperCamelCase_      (			self							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase       ):
      lowercase							    =       self.num_labels
      lowercase							    =       MobileBertForSequenceClassification(_lowerCamelCase       )
      model.to(_lowerCamelCase       )
      model.eval()
      lowercase							    =       model(_lowerCamelCase							,		attention_mask=_lowerCamelCase							,		token_type_ids=_lowerCamelCase							,		labels=_lowerCamelCase       )
      self.parent.assertEqual(result.logits.shape							,		(self.batch_size, self.num_labels)       )







    def 			UpperCamelCase_      (			self							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase       ):
      lowercase							    =       self.num_labels
      lowercase							    =       MobileBertForTokenClassification(config=_lowerCamelCase       )
      model.to(_lowerCamelCase       )
      model.eval()
      lowercase							    =       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 			UpperCamelCase_      (			self							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase       ):
      lowercase							    =       self.num_choices
      lowercase							    =       MobileBertForMultipleChoice(config=_lowerCamelCase       )
      model.to(_lowerCamelCase       )
      model.eval()
      lowercase							    =       input_ids.unsqueeze(1       ).expand(-1							,		self.num_choices							,		-1       ).contiguous()
      lowercase							    =       token_type_ids.unsqueeze(1       ).expand(-1							,		self.num_choices							,		-1       ).contiguous()
      lowercase							    =       input_mask.unsqueeze(1       ).expand(-1							,		self.num_choices							,		-1       ).contiguous()
      lowercase							    =       model(
          _lowerCamelCase							,		attention_mask=_lowerCamelCase							,		token_type_ids=_lowerCamelCase							,		labels=_lowerCamelCase							,		)
      self.parent.assertEqual(result.logits.shape							,		(self.batch_size, self.num_choices)       )







    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       self.prepare_config_and_inputs()
      (
          (
          lowercase
      )    ,     (
          lowercase
      )    ,     (
          lowercase
      )    ,     (
          lowercase
      )    ,     (
          lowercase
      )    ,     (
          lowercase
      )    ,     (
          lowercase
      )    ,     
      )							    =       config_and_inputs
      lowercase							    =       {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
      return config, inputs_dict


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







    def 			UpperCamelCase_      (			self							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase=False       ):
      lowercase							    =       super()._prepare_for_class(_lowerCamelCase							,		_lowerCamelCase							,		return_labels=_lowerCamelCase       )

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







    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       MobileBertModelTester(self       )
      lowercase							    =       ConfigTester(self							,		config_class=_lowerCamelCase							,		hidden_size=3_7       )







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







    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_model(*_lowerCamelCase       )







    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_masked_lm(*_lowerCamelCase       )







    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_multiple_choice(*_lowerCamelCase       )







    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*_lowerCamelCase       )







    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_pretraining(*_lowerCamelCase       )







    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_question_answering(*_lowerCamelCase       )







    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mobilebert_for_sequence_classification(*_lowerCamelCase       )







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


def 						_SCREAMING_SNAKE_CASE (    __snake_case			:  Any    ):





  '''simple docstring'''





  return torch.tensor(
      __snake_case		,					dtype=torch.long		,					device=__snake_case		,					)


_UpperCamelCase		:  Dict						=		1e-3



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







    @slow
    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       MobileBertModel.from_pretrained('google/mobilebert-uncased'       ).to(_lowerCamelCase       )
      lowercase							    =       _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():
        lowercase							    =       model(_lowerCamelCase       )[0]
      lowercase							    =       torch.Size((1, 9, 5_1_2)       )
      self.assertEqual(output.shape							,		_lowerCamelCase       )
      lowercase							    =       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
      lowercase							    =       torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE       )
      lowercase							    =       torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE       )

      self.assertTrue(lower_bound and upper_bound       )










"""simple docstring"""



_UpperCamelCase		:  List[str]						=		{
    'meter': 'm',
    'kilometer': 'km',
    'megametre': 'Mm',
    'gigametre': 'Gm',
    'terametre': 'Tm',
    'petametre': 'Pm',
    'exametre': 'Em',
    'zettametre': 'Zm',
    'yottametre': 'Ym',
}
# Exponent of the factor(meter)
_UpperCamelCase		:  str						=		{
    'm': 0,
    'km': 3,
    'Mm': 6,
    'Gm': 9,
    'Tm': 1_2,
    'Pm': 1_5,
    'Em': 1_8,
    'Zm': 2_1,
    'Ym': 2_4,
}
def 						_SCREAMING_SNAKE_CASE (    __snake_case			:  float		,					__snake_case			:  str		,					__snake_case			:  str    ):





  '''simple docstring'''





  lowercase							    =       from_type.lower().strip('s'    )
  lowercase							    =       to_type.lower().strip('s'    )

  lowercase							    =       UNIT_SYMBOL.get(__snake_case		,					__snake_case    )
  lowercase							    =       UNIT_SYMBOL.get(__snake_case		,					__snake_case    )

  if from_sanitized not in METRIC_CONVERSION:
    lowercase							    =       (
        f'Invalid \'from_type\' value: {from_type!r}.\n'
        f'Conversion abbreviations are: {", ".join(__snake_case    )}'
    )
    raise ValueError(__snake_case    )
  if to_sanitized not in METRIC_CONVERSION:
    lowercase							    =       (
        f'Invalid \'to_type\' value: {to_type!r}.\n'
        f'Conversion abbreviations are: {", ".join(__snake_case    )}'
    )
    raise ValueError(__snake_case    )
  lowercase							    =       METRIC_CONVERSION[from_sanitized]
  lowercase							    =       METRIC_CONVERSION[to_sanitized]
  lowercase							    =       1

  if from_exponent > to_exponent:
    lowercase							    =       from_exponent - to_exponent
  else:
    lowercase							    =       -(to_exponent - from_exponent)

  return value * pow(10		,					__snake_case    )


if __name__ == "__main__":
    from doctest import testmod

    testmod()


