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

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







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					List[Any]			     =    ids_tensor([self.batch_size, self.seq_length]				,   self.vocab_size  )

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

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

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

     UpperCAmelCase_					:					int			     =    self.get_config()

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







  def        UpperCamelCase__					(		self  ):

     """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        UpperCamelCase__					(		self				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_  ):

     """simple docstring"""
     UpperCAmelCase_					:					Optional[int]			     =    MobileBertModel(config=lowercase_  )
     model.to(lowercase_  )
     model.eval()
     UpperCAmelCase_					:					Optional[int]			     =    model(lowercase_				,   attention_mask=lowercase_				,   token_type_ids=lowercase_  )
     UpperCAmelCase_					:					Dict			     =    model(lowercase_				,   token_type_ids=lowercase_  )
     UpperCAmelCase_					:					Union[str, Any]			     =    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        UpperCamelCase__					(		self				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_  ):

     """simple docstring"""
     UpperCAmelCase_					:					Optional[Any]			     =    MobileBertForMaskedLM(config=lowercase_  )
     model.to(lowercase_  )
     model.eval()
     UpperCAmelCase_					:					str			     =    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        UpperCamelCase__					(		self				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_  ):

     """simple docstring"""
     UpperCAmelCase_					:					Any			     =    MobileBertForNextSentencePrediction(config=lowercase_  )
     model.to(lowercase_  )
     model.eval()
     UpperCAmelCase_					:					Optional[Any]			     =    model(
         lowercase_				,   attention_mask=lowercase_				,   token_type_ids=lowercase_				,   labels=lowercase_				,   )
     self.parent.assertEqual(result.logits.shape				,   (self.batch_size, 2)  )







  def        UpperCamelCase__					(		self				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_  ):

     """simple docstring"""
     UpperCAmelCase_					:					Optional[int]			     =    MobileBertForPreTraining(config=lowercase_  )
     model.to(lowercase_  )
     model.eval()
     UpperCAmelCase_					:					Tuple			     =    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        UpperCamelCase__					(		self				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_  ):

     """simple docstring"""
     UpperCAmelCase_					:					Optional[int]			     =    MobileBertForQuestionAnswering(config=lowercase_  )
     model.to(lowercase_  )
     model.eval()
     UpperCAmelCase_					:					List[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        UpperCamelCase__					(		self				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_  ):

     """simple docstring"""
     UpperCAmelCase_					:					int			     =    self.num_labels
     UpperCAmelCase_					:					List[str]			     =    MobileBertForSequenceClassification(lowercase_  )
     model.to(lowercase_  )
     model.eval()
     UpperCAmelCase_					:					Dict			     =    model(lowercase_				,   attention_mask=lowercase_				,   token_type_ids=lowercase_				,   labels=lowercase_  )
     self.parent.assertEqual(result.logits.shape				,   (self.batch_size, self.num_labels)  )







  def        UpperCamelCase__					(		self				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_  ):

     """simple docstring"""
     UpperCAmelCase_					:					str			     =    self.num_labels
     UpperCAmelCase_					:					Tuple			     =    MobileBertForTokenClassification(config=lowercase_  )
     model.to(lowercase_  )
     model.eval()
     UpperCAmelCase_					:					Union[str, 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        UpperCamelCase__					(		self				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_				,   lowercase_  ):

     """simple docstring"""
     UpperCAmelCase_					:					str			     =    self.num_choices
     UpperCAmelCase_					:					Dict			     =    MobileBertForMultipleChoice(config=lowercase_  )
     model.to(lowercase_  )
     model.eval()
     UpperCAmelCase_					:					Dict			     =    input_ids.unsqueeze(1  ).expand(-1				,   self.num_choices				,   -1  ).contiguous()
     UpperCAmelCase_					:					Dict			     =    token_type_ids.unsqueeze(1  ).expand(-1				,   self.num_choices				,   -1  ).contiguous()
     UpperCAmelCase_					:					int			     =    input_mask.unsqueeze(1  ).expand(-1				,   self.num_choices				,   -1  ).contiguous()
     UpperCAmelCase_					:					str			     =    model(
         lowercase_				,   attention_mask=lowercase_				,   token_type_ids=lowercase_				,   labels=lowercase_				,   )
     self.parent.assertEqual(result.logits.shape				,   (self.batch_size, self.num_choices)  )







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					int			     =    self.prepare_config_and_inputs()
     (
         (
         UpperCAmelCase_
     )     ,   (
         UpperCAmelCase_
     )     ,   (
         UpperCAmelCase_
     )     ,   (
         UpperCAmelCase_
     )     ,   (
         UpperCAmelCase_
     )     ,   (
         UpperCAmelCase_
     )     ,   (
         UpperCAmelCase_
     )     ,   
     )					:					List[Any]			     =    config_and_inputs
     UpperCAmelCase_					:					Dict			     =    {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
     return config, inputs_dict




@require_torch
class A_	(lowercase__				,lowercase__				,unittest.TestCase      ):




  '''simple docstring'''
  SCREAMING_SNAKE_CASE__			:						Optional[Any]       =		(
      (
          MobileBertModel,
          MobileBertForMaskedLM,
          MobileBertForMultipleChoice,
          MobileBertForNextSentencePrediction,
          MobileBertForPreTraining,
          MobileBertForQuestionAnswering,
          MobileBertForSequenceClassification,
          MobileBertForTokenClassification,
      )
      if is_torch_available()
      else ()
  )
  SCREAMING_SNAKE_CASE__			:						Optional[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__			:						Any       =		True







  def        UpperCamelCase__					(		self				,   lowercase_				,   lowercase_				,   lowercase_=False  ):

     """simple docstring"""
     UpperCAmelCase_					:					str			     =    super()._prepare_for_class(lowercase_				,   lowercase_				,   return_labels=lowercase_  )

     if return_labels:
        if model_class in get_values(lowercase_  ):
           UpperCAmelCase_					:					Optional[Any]			     =    torch.zeros(
               (self.model_tester.batch_size, self.model_tester.seq_length)				,   dtype=torch.long				,   device=lowercase_  )
           UpperCAmelCase_					:					Dict			     =    torch.zeros(
               self.model_tester.batch_size				,   dtype=torch.long				,   device=lowercase_  )
     return inputs_dict







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					str			     =    MobileBertModelTester(self  )
     UpperCAmelCase_					:					Tuple			     =    ConfigTester(self				,   config_class=lowercase_				,   hidden_size=37  )







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     self.config_tester.run_common_tests()







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					Dict			     =    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mobilebert_model(*lowercase_  )







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					List[Any]			     =    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mobilebert_for_masked_lm(*lowercase_  )







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					Optional[Any]			     =    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mobilebert_for_multiple_choice(*lowercase_  )







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					Dict			     =    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*lowercase_  )







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					str			     =    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mobilebert_for_pretraining(*lowercase_  )







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					Union[str, Any]			     =    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mobilebert_for_question_answering(*lowercase_  )







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					Optional[int]			     =    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mobilebert_for_sequence_classification(*lowercase_  )







  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					List[Any]			     =    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mobilebert_for_token_classification(*lowercase_  )







def 	__a     (		__lowerCamelCase  ):
   return torch.tensor(
       __lowerCamelCase,		dtype=torch.long,		device=__lowerCamelCase,		)


_a		       =							1e-3


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




  '''simple docstring'''







  @slow
  def        UpperCamelCase__					(		self  ):

     """simple docstring"""
     UpperCAmelCase_					:					int			     =    MobileBertModel.from_pretrained("google/mobilebert-uncased"  ).to(lowercase_  )
     UpperCAmelCase_					:					int			     =    _long_tensor([[101, 7110, 1005, 1056, 2023, 1_1333, 1_7413, 1029, 102]]  )
     with torch.no_grad():
        UpperCAmelCase_					:					Union[str, Any]			     =    model(lowercase_  )[0]
     UpperCAmelCase_					:					int			     =    torch.Size((1, 9, 512)  )
     self.assertEqual(output.shape				,   lowercase_  )
     UpperCAmelCase_					:					List[str]			     =    torch.tensor(
         [
             [
                 [-2.4_7_3_6_5_2_6E0_7, 8.2_6_9_1_6_5_6E0_4, 1.6_5_2_1_8_3_8E0_5],
                 [-5.7_5_4_1_7_0_4E-0_1, 3.9_0_5_6_0_2_2E0_0, 4.4_0_1_1_5_0_7E0_0],
                 [2.6_0_4_7_3_5_9E0_0, 1.5_6_7_7_6_5_2E0_0, -1.7_3_2_4_1_8_8E-0_1],
             ]
         ]				,   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
     UpperCAmelCase_					:					Tuple			     =    torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE  )
     UpperCAmelCase_					:					str			     =    torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE  )

     self.assertTrue(lower_bound and upper_bound  )



"""simple docstring"""






def SCREAMING_SNAKE_CASE__			(   __UpperCAmelCase )  ->  int:

   if not grid or not grid[0]:
      raise TypeError('''The grid does not contain the appropriate information''' )

   for cell_n in range(1       ,    len(grid[0] ) ):
      grid[0][cell_n] += grid[0][cell_n - 1]
   lowercase__:   Tuple					=      grid[0]

   for row_n in range(1       ,    len(__UpperCAmelCase ) ):
      lowercase__:   Tuple					=      grid[row_n]
      lowercase__:   Dict					=      fill_row(__UpperCAmelCase       ,    __UpperCAmelCase )
      lowercase__:   Union[str, Any]					=      grid[row_n]

   return grid[-1][-1]



def SCREAMING_SNAKE_CASE__			(   __UpperCAmelCase       ,    __UpperCAmelCase )  ->  list:
   current_row[0] += row_above[0]
   for cell_n in range(1       ,    len(__UpperCAmelCase ) ):
      current_row[cell_n] += min(current_row[cell_n - 1]       ,    row_above[cell_n] )

   return current_row


if __name__ == "__main__":
       import doctest

       doctest.testmod()
