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


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

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


 def    _A	(    self  : Dict    ):
  '''simple docstring'''

  lowerCAmelCase__     :   Optional[Any]     		=	ids_tensor([self.batch_size, self.seq_length]			,							self.vocab_size    )

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

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

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

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

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


 def    _A	(    self  : List[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=a__			,							initializer_range=self.initializer_range			,							)


 def    _A	(    self  : Union[str, Any]			,							a__  : Dict			,							a__  : Any			,							a__  : int			,							a__  : Optional[Any]			,							a__  : List[Any]			,							a__  : List[Any]			,							a__  : Union[str, Any]    ):
  '''simple docstring'''

  lowerCAmelCase__     :   int     		=	MobileBertModel(config=a__    )
  model.to(a__    )
  model.eval()
  lowerCAmelCase__     :   List[Any]     		=	model(a__			,							attention_mask=a__			,							token_type_ids=a__    )
  lowerCAmelCase__     :   Optional[int]     		=	model(a__			,							token_type_ids=a__    )
  lowerCAmelCase__     :   List[str]     		=	model(a__    )

  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    _A	(    self  : Dict			,							a__  : int			,							a__  : List[Any]			,							a__  : Union[str, Any]			,							a__  : int			,							a__  : Optional[int]			,							a__  : Tuple			,							a__  : str    ):
  '''simple docstring'''

  lowerCAmelCase__     :   Optional[int]     		=	MobileBertForMaskedLM(config=a__    )
  model.to(a__    )
  model.eval()
  lowerCAmelCase__     :   Tuple     		=	model(a__			,							attention_mask=a__			,							token_type_ids=a__			,							labels=a__    )
  self.parent.assertEqual(result.logits.shape			,							(self.batch_size, self.seq_length, self.vocab_size)    )


 def    _A	(    self  : Dict			,							a__  : Tuple			,							a__  : str			,							a__  : Dict			,							a__  : List[Any]			,							a__  : List[Any]			,							a__  : Optional[int]			,							a__  : Tuple    ):
  '''simple docstring'''

  lowerCAmelCase__     :   Dict     		=	MobileBertForNextSentencePrediction(config=a__    )
  model.to(a__    )
  model.eval()
  lowerCAmelCase__     :   List[Any]     		=	model(
      a__			,							attention_mask=a__			,							token_type_ids=a__			,							labels=a__			,							)
  self.parent.assertEqual(result.logits.shape			,							(self.batch_size, 2)    )


 def    _A	(    self  : Union[str, Any]			,							a__  : List[Any]			,							a__  : Any			,							a__  : List[str]			,							a__  : Union[str, Any]			,							a__  : Optional[int]			,							a__  : Any			,							a__  : Dict    ):
  '''simple docstring'''

  lowerCAmelCase__     :   Dict     		=	MobileBertForPreTraining(config=a__    )
  model.to(a__    )
  model.eval()
  lowerCAmelCase__     :   Dict     		=	model(
      a__			,							attention_mask=a__			,							token_type_ids=a__			,							labels=a__			,							next_sentence_label=a__			,							)
  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    _A	(    self  : Union[str, Any]			,							a__  : List[str]			,							a__  : Dict			,							a__  : Dict			,							a__  : Dict			,							a__  : Any			,							a__  : List[Any]			,							a__  : Union[str, Any]    ):
  '''simple docstring'''

  lowerCAmelCase__     :   str     		=	MobileBertForQuestionAnswering(config=a__    )
  model.to(a__    )
  model.eval()
  lowerCAmelCase__     :   Optional[int]     		=	model(
      a__			,							attention_mask=a__			,							token_type_ids=a__			,							start_positions=a__			,							end_positions=a__			,							)
  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    _A	(    self  : List[str]			,							a__  : Optional[int]			,							a__  : Optional[Any]			,							a__  : Any			,							a__  : Any			,							a__  : Any			,							a__  : str			,							a__  : str    ):
  '''simple docstring'''

  lowerCAmelCase__     :   Optional[int]     		=	self.num_labels
  lowerCAmelCase__     :   Tuple     		=	MobileBertForSequenceClassification(a__    )
  model.to(a__    )
  model.eval()
  lowerCAmelCase__     :   Dict     		=	model(a__			,							attention_mask=a__			,							token_type_ids=a__			,							labels=a__    )
  self.parent.assertEqual(result.logits.shape			,							(self.batch_size, self.num_labels)    )


 def    _A	(    self  : Dict			,							a__  : List[str]			,							a__  : int			,							a__  : Any			,							a__  : List[Any]			,							a__  : int			,							a__  : Any			,							a__  : Union[str, Any]    ):
  '''simple docstring'''

  lowerCAmelCase__     :   Tuple     		=	self.num_labels
  lowerCAmelCase__     :   Dict     		=	MobileBertForTokenClassification(config=a__    )
  model.to(a__    )
  model.eval()
  lowerCAmelCase__     :   List[Any]     		=	model(a__			,							attention_mask=a__			,							token_type_ids=a__			,							labels=a__    )
  self.parent.assertEqual(result.logits.shape			,							(self.batch_size, self.seq_length, self.num_labels)    )


 def    _A	(    self  : Union[str, Any]			,							a__  : Optional[int]			,							a__  : Optional[int]			,							a__  : Tuple			,							a__  : Optional[int]			,							a__  : int			,							a__  : Optional[Any]			,							a__  : Any    ):
  '''simple docstring'''

  lowerCAmelCase__     :   List[str]     		=	self.num_choices
  lowerCAmelCase__     :   Dict     		=	MobileBertForMultipleChoice(config=a__    )
  model.to(a__    )
  model.eval()
  lowerCAmelCase__     :   Any     		=	input_ids.unsqueeze(1    ).expand(-1			,							self.num_choices			,							-1    ).contiguous()
  lowerCAmelCase__     :   str     		=	token_type_ids.unsqueeze(1    ).expand(-1			,							self.num_choices			,							-1    ).contiguous()
  lowerCAmelCase__     :   Tuple     		=	input_mask.unsqueeze(1    ).expand(-1			,							self.num_choices			,							-1    ).contiguous()
  lowerCAmelCase__     :   Dict     		=	model(
      a__			,							attention_mask=a__			,							token_type_ids=a__			,							labels=a__			,							)
  self.parent.assertEqual(result.logits.shape			,							(self.batch_size, self.num_choices)    )


 def    _A	(    self  : str    ):
  '''simple docstring'''

  lowerCAmelCase__     :   List[Any]     		=	self.prepare_config_and_inputs()
  (
      lowerCAmelCase__		
  )     :   Optional[int]     		=	config_and_inputs
  lowerCAmelCase__     :   Optional[Any]     		=	{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
  return config, inputs_dict




@require_torch
class    lowerCAmelCase       (     __A    ,  __A    ,  unittest.TestCase  ):
 A_				:				List[Any]	=			(
     (
         MobileBertModel,
         MobileBertForMaskedLM,
         MobileBertForMultipleChoice,
         MobileBertForNextSentencePrediction,
         MobileBertForPreTraining,
         MobileBertForQuestionAnswering,
         MobileBertForSequenceClassification,
         MobileBertForTokenClassification,
     )
     if is_torch_available()
     else ()
 )
 A_				:				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 {}
 )
 A_				:				int	=			True


 def    _A	(    self  : Tuple			,							a__  : List[Any]			,							a__  : str			,							a__  : str=False    ):
  '''simple docstring'''

  lowerCAmelCase__     :   Dict     		=	super()._prepare_for_class(a__			,							a__			,							return_labels=a__    )

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


 def    _A	(    self  : int    ):
  '''simple docstring'''

  lowerCAmelCase__     :   List[Any]     		=	MobileBertModelTester(self    )
  lowerCAmelCase__     :   Union[str, Any]     		=	ConfigTester(self			,							config_class=a__			,							hidden_size=37    )


 def    _A	(    self  : str    ):
  '''simple docstring'''

  self.config_tester.run_common_tests()


 def    _A	(    self  : Tuple    ):
  '''simple docstring'''

  lowerCAmelCase__     :   Optional[Any]     		=	self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_mobilebert_model(*a__    )


 def    _A	(    self  : int    ):
  '''simple docstring'''

  lowerCAmelCase__     :   Tuple     		=	self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_mobilebert_for_masked_lm(*a__    )


 def    _A	(    self  : Union[str, Any]    ):
  '''simple docstring'''

  lowerCAmelCase__     :   Optional[int]     		=	self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_mobilebert_for_multiple_choice(*a__    )


 def    _A	(    self  : int    ):
  '''simple docstring'''

  lowerCAmelCase__     :   List[Any]     		=	self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*a__    )


 def    _A	(    self  : Any    ):
  '''simple docstring'''

  lowerCAmelCase__     :   Optional[Any]     		=	self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_mobilebert_for_pretraining(*a__    )


 def    _A	(    self  : Any    ):
  '''simple docstring'''

  lowerCAmelCase__     :   List[Any]     		=	self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_mobilebert_for_question_answering(*a__    )


 def    _A	(    self  : Dict    ):
  '''simple docstring'''

  lowerCAmelCase__     :   str     		=	self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_mobilebert_for_sequence_classification(*a__    )


 def    _A	(    self  : Tuple    ):
  '''simple docstring'''

  lowerCAmelCase__     :   Any     		=	self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_mobilebert_for_token_classification(*a__    )



def        UpperCAmelCase_ (  lowerCamelCase_			):







 """simple docstring"""


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


snake_case									=    1e-3







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


 @slow
 def    _A	(    self  : Dict    ):
  '''simple docstring'''

  lowerCAmelCase__     :   Union[str, Any]     		=	MobileBertModel.from_pretrained("google/mobilebert-uncased"    ).to(a__    )
  lowerCAmelCase__     :   Tuple     		=	_long_tensor([[101, 7110, 1005, 1056, 2023, 1_1333, 1_7413, 1029, 102]]    )
  with torch.no_grad():
   lowerCAmelCase__     :   Union[str, Any]     		=	model(a__    )[0]
  lowerCAmelCase__     :   Optional[int]     		=	torch.Size((1, 9, 512)    )
  self.assertEqual(output.shape			,							a__    )
  lowerCAmelCase__     :   Any     		=	torch.tensor(
      [
          [
              [-2.4_736_526e07, 8.2_691_656e04, 1.6_521_838e05],
              [-5.7_541_704e-01, 3.9_056_022e00, 4.4_011_507e00],
              [2.6_047_359e00, 1.5_677_652e00, -1.7_324_188e-01],
          ]
      ]			,							device=a__			,							)

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

  self.assertTrue(lower_bound and upper_bound    )













'''simple docstring'''



from unittest import TestCase

from datasets import Sequence, Value
from datasets.arrow_dataset import Dataset
class UpperCAmelCase_       (     __A					):




      """simple docstring"""




      def 					A__   (			self      :		Optional[int]		)	->	List[str]:


       '''simple docstring'''

       return [
           {"col_1": 3, "col_2": "a"},
           {"col_1": 2, "col_2": "b"},
           {"col_1": 1, "col_2": "c"},
           {"col_1": 0, "col_2": "d"},
       ]






      def 					A__   (			self      :		Any		)	->	List[str]:


       '''simple docstring'''

       lowercase       : List[str]						={'''col_1''': [3, 2, 1, 0], '''col_2''': ['''a''', '''b''', '''c''', '''d''']}
       return Dataset.from_dict(UpperCAmelCase		)






      def 					A__   (			self      :		Any		)	->	Tuple:


       '''simple docstring'''

       lowercase       : Tuple						=self._create_example_records()
       lowercase       : Tuple						=Dataset.from_list(UpperCAmelCase		)
       self.assertListEqual(dset.column_names  ,   ['''col_1''', '''col_2''']		)
       for i, r in enumerate(UpperCAmelCase		):
        self.assertDictEqual(UpperCAmelCase  ,   example_records[i]		)






      def 					A__   (			self      :		Dict		)	->	List[str]:


       '''simple docstring'''

       lowercase       : Optional[Any]						=self._create_example_records()
       lowercase       : Union[str, Any]						=Dataset.from_list(UpperCAmelCase		)
       lowercase       : Tuple						=Dataset.from_dict({k: [r[k] for r in example_records] for k in example_records[0]}		)
       self.assertEqual(dset.info  ,   dset_from_dict.info		)






      def 					A__   (			self      :		str		)	->	List[Any]:  # checks what happens with missing columns


       '''simple docstring'''

       lowercase       : Tuple						=[{'''col_1''': 1}, {'''col_2''': '''x'''}]
       lowercase       : Optional[Any]						=Dataset.from_list(UpperCAmelCase		)
       self.assertDictEqual(dset[0]  ,   {'''col_1''': 1}		)
       self.assertDictEqual(dset[1]  ,   {'''col_1''': None}		)  # NB: first record is used for columns






      def 					A__   (			self      :		List[Any]		)	->	int:  # checks if the type can be inferred from the second record


       '''simple docstring'''

       lowercase       : List[Any]						=[{'''col_1''': []}, {'''col_1''': [1, 2]}]
       lowercase       : Union[str, Any]						=Dataset.from_list(UpperCAmelCase		)
       self.assertEqual(dset.info.features['''col_1''']  ,   Sequence(Value('''int64'''		)		)		)






      def 					A__   (			self      :		int		)	->	str:


       '''simple docstring'''

       lowercase       : List[str]						=Dataset.from_list([]		)
       self.assertEqual(len(UpperCAmelCase		)  ,   0		)
       self.assertListEqual(dset.column_names  ,   []		)



