'''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    :


  """simple docstring"""

  def __init__(		self      :       List[str]   ,				lowercase_      :       List[Any]   ,				lowercase_      :       int=13   ,				lowercase_      :       Union[str, Any]=7   ,				lowercase_      :       Tuple=True   ,				lowercase_      :       List[str]=True   ,				lowercase_      :       List[str]=True   ,				lowercase_      :       Dict=True   ,				lowercase_      :       Dict=99   ,				lowercase_      :       Dict=64   ,				lowercase_      :       Union[str, Any]=32   ,				lowercase_      :       Optional[Any]=5   ,				lowercase_      :       Union[str, Any]=4   ,				lowercase_      :       Any=37   ,				lowercase_      :       List[Any]="gelu"   ,				lowercase_      :       Optional[int]=0.1   ,				lowercase_      :       Optional[int]=0.1   ,				lowercase_      :       str=512   ,				lowercase_      :       int=16   ,				lowercase_      :       Optional[Any]=2   ,				lowercase_      :       int=0.0_2   ,				lowercase_      :       str=3   ,				lowercase_      :       Any=4   ,				lowercase_      :       Union[str, Any]=None   ,				):







    '''simple docstring'''


    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 		lowerCamelCase__  (		self      :       Tuple      ):







    '''simple docstring'''


    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 		lowerCamelCase__  (		self      :       Tuple      ):







    '''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=lowercase_   ,				initializer_range=self.initializer_range   ,				)




  def 		lowerCamelCase__  (		self      :       Union[str, Any]   ,				lowercase_      :       str   ,				lowercase_      :       str   ,				lowercase_      :       Optional[Any]   ,				lowercase_      :       Union[str, Any]   ,				lowercase_      :       List[Any]   ,				lowercase_      :       Tuple   ,				lowercase_      :       str      ):







    '''simple docstring'''


    lowercase_     =       MobileBertModel(config=lowercase_      )
    model.to(lowercase_      )
    model.eval()
    lowercase_     =       model(lowercase_   ,				attention_mask=lowercase_   ,				token_type_ids=lowercase_      )
    lowercase_     =       model(lowercase_   ,				token_type_ids=lowercase_      )
    lowercase_     =       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 		lowerCamelCase__  (		self      :       Union[str, Any]   ,				lowercase_      :       Any   ,				lowercase_      :       List[str]   ,				lowercase_      :       Union[str, Any]   ,				lowercase_      :       Tuple   ,				lowercase_      :       Optional[int]   ,				lowercase_      :       List[Any]   ,				lowercase_      :       str      ):







    '''simple docstring'''


    lowercase_     =       MobileBertForMaskedLM(config=lowercase_      )
    model.to(lowercase_      )
    model.eval()
    lowercase_     =       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 		lowerCamelCase__  (		self      :       Tuple   ,				lowercase_      :       Any   ,				lowercase_      :       Optional[Any]   ,				lowercase_      :       List[str]   ,				lowercase_      :       int   ,				lowercase_      :       str   ,				lowercase_      :       Any   ,				lowercase_      :       List[Any]      ):







    '''simple docstring'''


    lowercase_     =       MobileBertForNextSentencePrediction(config=lowercase_      )
    model.to(lowercase_      )
    model.eval()
    lowercase_     =       model(
        lowercase_   ,				attention_mask=lowercase_   ,				token_type_ids=lowercase_   ,				labels=lowercase_   ,				)
    self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, 2)      )




  def 		lowerCamelCase__  (		self      :       Any   ,				lowercase_      :       List[str]   ,				lowercase_      :       Any   ,				lowercase_      :       Tuple   ,				lowercase_      :       str   ,				lowercase_      :       Any   ,				lowercase_      :       Tuple   ,				lowercase_      :       int      ):







    '''simple docstring'''


    lowercase_     =       MobileBertForPreTraining(config=lowercase_      )
    model.to(lowercase_      )
    model.eval()
    lowercase_     =       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 		lowerCamelCase__  (		self      :       Union[str, Any]   ,				lowercase_      :       Optional[int]   ,				lowercase_      :       Union[str, Any]   ,				lowercase_      :       Dict   ,				lowercase_      :       int   ,				lowercase_      :       Union[str, Any]   ,				lowercase_      :       Any   ,				lowercase_      :       str      ):







    '''simple docstring'''


    lowercase_     =       MobileBertForQuestionAnswering(config=lowercase_      )
    model.to(lowercase_      )
    model.eval()
    lowercase_     =       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 		lowerCamelCase__  (		self      :       List[Any]   ,				lowercase_      :       Any   ,				lowercase_      :       Optional[int]   ,				lowercase_      :       Tuple   ,				lowercase_      :       Union[str, Any]   ,				lowercase_      :       List[str]   ,				lowercase_      :       List[str]   ,				lowercase_      :       int      ):







    '''simple docstring'''


    lowercase_     =       self.num_labels
    lowercase_     =       MobileBertForSequenceClassification(lowercase_      )
    model.to(lowercase_      )
    model.eval()
    lowercase_     =       model(lowercase_   ,				attention_mask=lowercase_   ,				token_type_ids=lowercase_   ,				labels=lowercase_      )
    self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.num_labels)      )




  def 		lowerCamelCase__  (		self      :       List[str]   ,				lowercase_      :       Dict   ,				lowercase_      :       Any   ,				lowercase_      :       Union[str, Any]   ,				lowercase_      :       List[Any]   ,				lowercase_      :       Dict   ,				lowercase_      :       Any   ,				lowercase_      :       Optional[Any]      ):







    '''simple docstring'''


    lowercase_     =       self.num_labels
    lowercase_     =       MobileBertForTokenClassification(config=lowercase_      )
    model.to(lowercase_      )
    model.eval()
    lowercase_     =       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 		lowerCamelCase__  (		self      :       int   ,				lowercase_      :       Optional[int]   ,				lowercase_      :       Any   ,				lowercase_      :       int   ,				lowercase_      :       Tuple   ,				lowercase_      :       List[Any]   ,				lowercase_      :       Optional[Any]   ,				lowercase_      :       List[str]      ):







    '''simple docstring'''


    lowercase_     =       self.num_choices
    lowercase_     =       MobileBertForMultipleChoice(config=lowercase_      )
    model.to(lowercase_      )
    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(
        lowercase_   ,				attention_mask=lowercase_   ,				token_type_ids=lowercase_   ,				labels=lowercase_   ,				)
    self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.num_choices)      )





  def 		lowerCamelCase__  (		self      :       Optional[int]      ):







    '''simple docstring'''


    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      ):


  """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 		lowerCamelCase__  (		self      :       Union[str, Any]   ,				lowercase_      :       Optional[Any]   ,				lowercase_      :       str   ,				lowercase_      :       Any=False      ):







    '''simple docstring'''


    lowercase_     =       super()._prepare_for_class(lowercase_   ,				lowercase_   ,				return_labels=lowercase_      )

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




  def 		lowerCamelCase__  (		self      :       List[str]      ):







    '''simple docstring'''


    lowercase_     =       MobileBertModelTester(self      )
    lowercase_     =       ConfigTester(self   ,				config_class=lowercase_   ,				hidden_size=37      )




  def 		lowerCamelCase__  (		self      :       Tuple      ):







    '''simple docstring'''


    self.config_tester.run_common_tests()




  def 		lowerCamelCase__  (		self      :       List[Any]      ):







    '''simple docstring'''


    lowercase_     =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_mobilebert_model(*lowercase_      )




  def 		lowerCamelCase__  (		self      :       Union[str, Any]      ):







    '''simple docstring'''


    lowercase_     =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_mobilebert_for_masked_lm(*lowercase_      )




  def 		lowerCamelCase__  (		self      :       Dict      ):







    '''simple docstring'''


    lowercase_     =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_mobilebert_for_multiple_choice(*lowercase_      )




  def 		lowerCamelCase__  (		self      :       str      ):







    '''simple docstring'''


    lowercase_     =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*lowercase_      )




  def 		lowerCamelCase__  (		self      :       List[Any]      ):







    '''simple docstring'''


    lowercase_     =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_mobilebert_for_pretraining(*lowercase_      )




  def 		lowerCamelCase__  (		self      :       Union[str, Any]      ):







    '''simple docstring'''


    lowercase_     =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_mobilebert_for_question_answering(*lowercase_      )




  def 		lowerCamelCase__  (		self      :       List[Any]      ):







    '''simple docstring'''


    lowercase_     =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_mobilebert_for_sequence_classification(*lowercase_      )





  def 		lowerCamelCase__  (		self      :       Any      ):







    '''simple docstring'''


    lowercase_     =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_mobilebert_for_token_classification(*lowercase_      )






def   A_      (       SCREAMING_SNAKE_CASE_						)			->str:
  return torch.tensor(
      SCREAMING_SNAKE_CASE_ ,	dtype=torch.long ,	device=SCREAMING_SNAKE_CASE_ ,	)


__snake_case						  =		1e-3





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


  """simple docstring"""

  @slow
  def 		lowerCamelCase__  (		self      :       str      ):







    '''simple docstring'''


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


from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


__snake_case						  =		logging.get_logger(__name__)

__snake_case						  =		{
    """camembert-base""": """https://huggingface.co/camembert-base/resolve/main/config.json""",
    """umberto-commoncrawl-cased-v1""": (
        """https://huggingface.co/Musixmatch/umberto-commoncrawl-cased-v1/resolve/main/config.json"""
    ),
    """umberto-wikipedia-uncased-v1""": (
        """https://huggingface.co/Musixmatch/umberto-wikipedia-uncased-v1/resolve/main/config.json"""
    ),
}





class       _a    (		__a      ):


  """simple docstring"""

  A_    							=						'''camembert'''




  def __init__(		self      :       Union[str, Any]   ,				lowercase_      :       Union[str, Any]=30_522   ,				lowercase_      :       Optional[Any]=768   ,				lowercase_      :       Tuple=12   ,				lowercase_      :       Dict=12   ,				lowercase_      :       Tuple=3_072   ,				lowercase_      :       Optional[Any]="gelu"   ,				lowercase_      :       Optional[int]=0.1   ,				lowercase_      :       Optional[int]=0.1   ,				lowercase_      :       List[Any]=512   ,				lowercase_      :       Optional[int]=2   ,				lowercase_      :       str=0.0_2   ,				lowercase_      :       int=1e-12   ,				lowercase_      :       str=1   ,				lowercase_      :       List[str]=0   ,				lowercase_      :       int=2   ,				lowercase_      :       Union[str, Any]="absolute"   ,				lowercase_      :       Optional[Any]=True   ,				lowercase_      :       List[Any]=None   ,				**lowercase_      :       int   ,				):







    '''simple docstring'''


    super().__init__(pad_token_id=lowercase_   ,				bos_token_id=lowercase_   ,				eos_token_id=lowercase_   ,				**lowercase_      )

    lowercase_     =       vocab_size
    lowercase_     =       hidden_size
    lowercase_     =       num_hidden_layers
    lowercase_     =       num_attention_heads
    lowercase_     =       hidden_act
    lowercase_     =       intermediate_size
    lowercase_     =       hidden_dropout_prob
    lowercase_     =       attention_probs_dropout_prob
    lowercase_     =       max_position_embeddings
    lowercase_     =       type_vocab_size
    lowercase_     =       initializer_range
    lowercase_     =       layer_norm_eps
    lowercase_     =       position_embedding_type
    lowercase_     =       use_cache
    lowercase_     =       classifier_dropout






class       _a    (		__a      ):


  """simple docstring"""

  @property
  def 		lowerCamelCase__  (		self      :       Dict      ):







    '''simple docstring'''


    if self.task == "multiple-choice":
      lowercase_     =       {0: """batch""", 1: """choice""", 2: """sequence"""}
    else:
      lowercase_     =       {0: """batch""", 1: """sequence"""}
    return OrderedDict(
        [
            ("""input_ids""", dynamic_axis),
            ("""attention_mask""", dynamic_axis),
        ]      )

