


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 					SCREAMING_SNAKE_CASE__    :
      def __init__(			self							:			List[str]	,				SCREAMING_SNAKE_CASE__							:			Any	,				SCREAMING_SNAKE_CASE__							:			str=1_3	,				SCREAMING_SNAKE_CASE__							:			Union[str, Any]=7	,				SCREAMING_SNAKE_CASE__							:			Union[str, Any]=True	,				SCREAMING_SNAKE_CASE__							:			Tuple=True	,				SCREAMING_SNAKE_CASE__							:			Union[str, Any]=True	,				SCREAMING_SNAKE_CASE__							:			Union[str, Any]=True	,				SCREAMING_SNAKE_CASE__							:			List[Any]=9_9	,				SCREAMING_SNAKE_CASE__							:			Optional[int]=6_4	,				SCREAMING_SNAKE_CASE__							:			Union[str, Any]=3_2	,				SCREAMING_SNAKE_CASE__							:			Union[str, Any]=5	,				SCREAMING_SNAKE_CASE__							:			Optional[Any]=4	,				SCREAMING_SNAKE_CASE__							:			Any=3_7	,				SCREAMING_SNAKE_CASE__							:			Union[str, Any]="gelu"	,				SCREAMING_SNAKE_CASE__							:			Any=0.1	,				SCREAMING_SNAKE_CASE__							:			int=0.1	,				SCREAMING_SNAKE_CASE__							:			List[Any]=5_1_2	,				SCREAMING_SNAKE_CASE__							:			Dict=1_6	,				SCREAMING_SNAKE_CASE__							:			List[Any]=2	,				SCREAMING_SNAKE_CASE__							:			Tuple=0.02	,				SCREAMING_SNAKE_CASE__							:			List[str]=3	,				SCREAMING_SNAKE_CASE__							:			List[str]=4	,				SCREAMING_SNAKE_CASE__							:			Tuple=None	,				)						->     List[str]:
             a_   :						str				=    parent
             a_   :						str				=    batch_size
             a_   :						Tuple				=    seq_length
             a_   :						Tuple				=    is_training
             a_   :						int				=    use_input_mask
             a_   :						Optional[int]				=    use_token_type_ids
             a_   :						int				=    use_labels
             a_   :						int				=    vocab_size
             a_   :						Optional[int]				=    hidden_size
             a_   :						List[str]				=    embedding_size
             a_   :						Tuple				=    num_hidden_layers
             a_   :						List[Any]				=    num_attention_heads
             a_   :						int				=    intermediate_size
             a_   :						int				=    hidden_act
             a_   :						Optional[Any]				=    hidden_dropout_prob
             a_   :						Union[str, Any]				=    attention_probs_dropout_prob
             a_   :						int				=    max_position_embeddings
             a_   :						Optional[Any]				=    type_vocab_size
             a_   :						Dict				=    type_sequence_label_size
             a_   :						Union[str, Any]				=    initializer_range
             a_   :						Dict				=    num_labels
             a_   :						Union[str, Any]				=    num_choices
             a_   :						Optional[Any]				=    scope
      def 			SCREAMING_SNAKE_CASE				(			self							:			Dict	)						->     Dict:
             a_   :						int				=    ids_tensor([self.batch_size, self.seq_length]	,				self.vocab_size	)

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

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

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

             a_   :						str				=    self.get_config()

             return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
      def 			SCREAMING_SNAKE_CASE				(			self							:			Tuple	)						->     List[Any]:
             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 			SCREAMING_SNAKE_CASE				(			self							:			Optional[Any]	,				SCREAMING_SNAKE_CASE__							:			Any	,				SCREAMING_SNAKE_CASE__							:			Optional[int]	,				SCREAMING_SNAKE_CASE__							:			Dict	,				SCREAMING_SNAKE_CASE__							:			List[str]	,				SCREAMING_SNAKE_CASE__							:			List[Any]	,				SCREAMING_SNAKE_CASE__							:			Optional[Any]	,				SCREAMING_SNAKE_CASE__							:			Dict	)						->     Any:
             a_   :						str				=    MobileBertModel(config=_lowerCamelCase	)
             model.to(_lowerCamelCase	)
             model.eval()
             a_   :						Any				=    model(_lowerCamelCase	,				attention_mask=_lowerCamelCase	,				token_type_ids=_lowerCamelCase	)
             a_   :						Dict				=    model(_lowerCamelCase	,				token_type_ids=_lowerCamelCase	)
             a_   :						List[Any]				=    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 			SCREAMING_SNAKE_CASE				(			self							:			str	,				SCREAMING_SNAKE_CASE__							:			List[str]	,				SCREAMING_SNAKE_CASE__							:			int	,				SCREAMING_SNAKE_CASE__							:			Any	,				SCREAMING_SNAKE_CASE__							:			Tuple	,				SCREAMING_SNAKE_CASE__							:			Dict	,				SCREAMING_SNAKE_CASE__							:			str	,				SCREAMING_SNAKE_CASE__							:			Optional[int]	)						->     Tuple:
             a_   :						Optional[Any]				=    MobileBertForMaskedLM(config=_lowerCamelCase	)
             model.to(_lowerCamelCase	)
             model.eval()
             a_   :						Tuple				=    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 			SCREAMING_SNAKE_CASE				(			self							:			int	,				SCREAMING_SNAKE_CASE__							:			str	,				SCREAMING_SNAKE_CASE__							:			Optional[int]	,				SCREAMING_SNAKE_CASE__							:			Optional[Any]	,				SCREAMING_SNAKE_CASE__							:			Any	,				SCREAMING_SNAKE_CASE__							:			List[str]	,				SCREAMING_SNAKE_CASE__							:			Any	,				SCREAMING_SNAKE_CASE__							:			Tuple	)						->     List[Any]:
             a_   :						Tuple				=    MobileBertForNextSentencePrediction(config=_lowerCamelCase	)
             model.to(_lowerCamelCase	)
             model.eval()
             a_   :						List[str]				=    model(
                 _lowerCamelCase	,				attention_mask=_lowerCamelCase	,				token_type_ids=_lowerCamelCase	,				labels=_lowerCamelCase	,				)
             self.parent.assertEqual(result.logits.shape	,				(self.batch_size, 2)	)
      def 			SCREAMING_SNAKE_CASE				(			self							:			Dict	,				SCREAMING_SNAKE_CASE__							:			Any	,				SCREAMING_SNAKE_CASE__							:			List[Any]	,				SCREAMING_SNAKE_CASE__							:			Any	,				SCREAMING_SNAKE_CASE__							:			List[Any]	,				SCREAMING_SNAKE_CASE__							:			Tuple	,				SCREAMING_SNAKE_CASE__							:			int	,				SCREAMING_SNAKE_CASE__							:			int	)						->     str:
             a_   :						List[str]				=    MobileBertForPreTraining(config=_lowerCamelCase	)
             model.to(_lowerCamelCase	)
             model.eval()
             a_   :						int				=    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 			SCREAMING_SNAKE_CASE				(			self							:			Tuple	,				SCREAMING_SNAKE_CASE__							:			Optional[int]	,				SCREAMING_SNAKE_CASE__							:			Tuple	,				SCREAMING_SNAKE_CASE__							:			Union[str, Any]	,				SCREAMING_SNAKE_CASE__							:			int	,				SCREAMING_SNAKE_CASE__							:			Union[str, Any]	,				SCREAMING_SNAKE_CASE__							:			int	,				SCREAMING_SNAKE_CASE__							:			Any	)						->     str:
             a_   :						Union[str, Any]				=    MobileBertForQuestionAnswering(config=_lowerCamelCase	)
             model.to(_lowerCamelCase	)
             model.eval()
             a_   :						Dict				=    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 			SCREAMING_SNAKE_CASE				(			self							:			List[Any]	,				SCREAMING_SNAKE_CASE__							:			List[str]	,				SCREAMING_SNAKE_CASE__							:			Optional[int]	,				SCREAMING_SNAKE_CASE__							:			Dict	,				SCREAMING_SNAKE_CASE__							:			Tuple	,				SCREAMING_SNAKE_CASE__							:			Tuple	,				SCREAMING_SNAKE_CASE__							:			str	,				SCREAMING_SNAKE_CASE__							:			Optional[Any]	)						->     List[Any]:
             a_   :						Any				=    self.num_labels
             a_   :						Optional[Any]				=    MobileBertForSequenceClassification(_lowerCamelCase	)
             model.to(_lowerCamelCase	)
             model.eval()
             a_   :						int				=    model(_lowerCamelCase	,				attention_mask=_lowerCamelCase	,				token_type_ids=_lowerCamelCase	,				labels=_lowerCamelCase	)
             self.parent.assertEqual(result.logits.shape	,				(self.batch_size, self.num_labels)	)
      def 			SCREAMING_SNAKE_CASE				(			self							:			List[Any]	,				SCREAMING_SNAKE_CASE__							:			int	,				SCREAMING_SNAKE_CASE__							:			List[str]	,				SCREAMING_SNAKE_CASE__							:			Optional[Any]	,				SCREAMING_SNAKE_CASE__							:			str	,				SCREAMING_SNAKE_CASE__							:			Tuple	,				SCREAMING_SNAKE_CASE__							:			List[str]	,				SCREAMING_SNAKE_CASE__							:			int	)						->     Optional[int]:
             a_   :						Dict				=    self.num_labels
             a_   :						Any				=    MobileBertForTokenClassification(config=_lowerCamelCase	)
             model.to(_lowerCamelCase	)
             model.eval()
             a_   :						str				=    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 			SCREAMING_SNAKE_CASE				(			self							:			Dict	,				SCREAMING_SNAKE_CASE__							:			Optional[int]	,				SCREAMING_SNAKE_CASE__							:			Optional[int]	,				SCREAMING_SNAKE_CASE__							:			Any	,				SCREAMING_SNAKE_CASE__							:			List[Any]	,				SCREAMING_SNAKE_CASE__							:			Tuple	,				SCREAMING_SNAKE_CASE__							:			List[str]	,				SCREAMING_SNAKE_CASE__							:			str	)						->     Union[str, Any]:
             a_   :						Dict				=    self.num_choices
             a_   :						str				=    MobileBertForMultipleChoice(config=_lowerCamelCase	)
             model.to(_lowerCamelCase	)
             model.eval()
             a_   :						Optional[Any]				=    input_ids.unsqueeze(1	).expand(-1	,				self.num_choices	,				-1	).contiguous()
             a_   :						Any				=    token_type_ids.unsqueeze(1	).expand(-1	,				self.num_choices	,				-1	).contiguous()
             a_   :						int				=    input_mask.unsqueeze(1	).expand(-1	,				self.num_choices	,				-1	).contiguous()
             a_   :						int				=    model(
                 _lowerCamelCase	,				attention_mask=_lowerCamelCase	,				token_type_ids=_lowerCamelCase	,				labels=_lowerCamelCase	,				)
             self.parent.assertEqual(result.logits.shape	,				(self.batch_size, self.num_choices)	)

      def 			SCREAMING_SNAKE_CASE				(			self							:			List[str]	)						->     List[Any]:
             a_   :						Union[str, Any]				=    self.prepare_config_and_inputs()
             (
                 (
                 a_ 
             )				,						(
                 a_ 
             )				,						(
                 a_ 
             )				,						(
                 a_ 
             )				,						(
                 a_ 
             )				,						(
                 a_ 
             )				,						(
                 a_ 
             )				,						 
             )   :						Union[str, Any]				=    config_and_inputs
             a_   :						List[str]				=    {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
             return config, inputs_dict
@require_torch
class 					SCREAMING_SNAKE_CASE__    (							__lowerCAmelCase		,       __lowerCAmelCase		,       unittest.TestCase     ):
      snake_case__							:	Union[str, Any]	  =		(
          (
              MobileBertModel,
              MobileBertForMaskedLM,
              MobileBertForMultipleChoice,
              MobileBertForNextSentencePrediction,
              MobileBertForPreTraining,
              MobileBertForQuestionAnswering,
              MobileBertForSequenceClassification,
              MobileBertForTokenClassification,
          )
          if is_torch_available()
          else ()
      )
      snake_case__							:	Optional[int]	  =		(
          {
              '''feature-extraction''': MobileBertModel,
              '''fill-mask''': MobileBertForMaskedLM,
              '''question-answering''': MobileBertForQuestionAnswering,
              '''text-classification''': MobileBertForSequenceClassification,
              '''token-classification''': MobileBertForTokenClassification,
              '''zero-shot''': MobileBertForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      snake_case__							:	Dict	  =		True
      def 			SCREAMING_SNAKE_CASE				(			self							:			Dict	,				SCREAMING_SNAKE_CASE__							:			str	,				SCREAMING_SNAKE_CASE__							:			List[Any]	,				SCREAMING_SNAKE_CASE__							:			List[Any]=False	)						->     str:
             a_   :						Optional[Any]				=    super()._prepare_for_class(_lowerCamelCase	,				_lowerCamelCase	,				return_labels=_lowerCamelCase	)

             if return_labels:
                    if model_class in get_values(_lowerCamelCase	):
                           a_   :						Union[str, Any]				=    torch.zeros(
                               (self.model_tester.batch_size, self.model_tester.seq_length)	,				dtype=torch.long	,				device=_lowerCamelCase	)
                           a_   :						Union[str, Any]				=    torch.zeros(
                               self.model_tester.batch_size	,				dtype=torch.long	,				device=_lowerCamelCase	)
             return inputs_dict
      def 			SCREAMING_SNAKE_CASE				(			self							:			Any	)						->     Optional[int]:
             a_   :						Dict				=    MobileBertModelTester(self	)
             a_   :						Any				=    ConfigTester(self	,				config_class=_lowerCamelCase	,				hidden_size=3_7	)
      def 			SCREAMING_SNAKE_CASE				(			self							:			Union[str, Any]	)						->     int:
             self.config_tester.run_common_tests()
      def 			SCREAMING_SNAKE_CASE				(			self							:			Dict	)						->     List[str]:
             a_   :						Dict				=    self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mobilebert_model(*_lowerCamelCase	)
      def 			SCREAMING_SNAKE_CASE				(			self							:			Any	)						->     Optional[Any]:
             a_   :						str				=    self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mobilebert_for_masked_lm(*_lowerCamelCase	)
      def 			SCREAMING_SNAKE_CASE				(			self							:			str	)						->     int:
             a_   :						str				=    self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mobilebert_for_multiple_choice(*_lowerCamelCase	)
      def 			SCREAMING_SNAKE_CASE				(			self							:			str	)						->     Dict:
             a_   :						str				=    self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*_lowerCamelCase	)
      def 			SCREAMING_SNAKE_CASE				(			self							:			Dict	)						->     Union[str, Any]:
             a_   :						Any				=    self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mobilebert_for_pretraining(*_lowerCamelCase	)
      def 			SCREAMING_SNAKE_CASE				(			self							:			Tuple	)						->     Any:
             a_   :						int				=    self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mobilebert_for_question_answering(*_lowerCamelCase	)
      def 			SCREAMING_SNAKE_CASE				(			self							:			Dict	)						->     int:
             a_   :						Any				=    self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mobilebert_for_sequence_classification(*_lowerCamelCase	)

      def 			SCREAMING_SNAKE_CASE				(			self							:			Any	)						->     Tuple:
             a_   :						List[str]				=    self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_mobilebert_for_token_classification(*_lowerCamelCase	)







def 			SCREAMING_SNAKE_CASE_		(     __A					:     List[Any]							)      ->						Any:

       """simple docstring"""

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


UpperCAmelCase_ :					int       =			1e-3
@require_torch
@require_sentencepiece
@require_tokenizers
class 					SCREAMING_SNAKE_CASE__    (							unittest.TestCase     ):
      @slow
      def 			SCREAMING_SNAKE_CASE				(			self							:			Union[str, Any]	)						->     Union[str, Any]:
             a_   :						Optional[Any]				=    MobileBertModel.from_pretrained('google/mobilebert-uncased'	).to(_lowerCamelCase	)
             a_   :						int				=    _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():
                    a_   :						int				=    model(_lowerCamelCase	)[0]
             a_   :						int				=    torch.Size((1, 9, 5_1_2)	)
             self.assertEqual(output.shape	,				_lowerCamelCase	)
             a_   :						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=_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
             a_   :						Tuple				=    torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE	)
             a_   :						Optional[int]				=    torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE	)

             self.assertTrue(lower_bound and upper_bound	)



"""simple docstring"""


def      lowerCamelCase_					(   __lowerCAmelCase							)				->   int:




						'''simple docstring'''



						if not isinstance(__lowerCAmelCase  ,       __lowerCAmelCase							):
												raise TypeError("only integers accepted as input"							)
						else:
												lowerCamelCase__   =str(abs(__lowerCAmelCase							)							)
												lowerCamelCase__   =[list(__lowerCAmelCase							) for char in range(len(__lowerCAmelCase							)							)]
												for index in range(len(__lowerCAmelCase							)							):
																		num_transpositions[index].pop(__lowerCAmelCase							)
												return max(
												    int("".join(list(__lowerCAmelCase							)							)							) for transposition in num_transpositions							)


if __name__ == "__main__":
	__import__('doctest').testmod()


