
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     lowercase							:
					def __init__(		self			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__=13			,						SCREAMING_SNAKE_CASE__=7			,						SCREAMING_SNAKE_CASE__=True			,						SCREAMING_SNAKE_CASE__=True			,						SCREAMING_SNAKE_CASE__=True			,						SCREAMING_SNAKE_CASE__=True			,						SCREAMING_SNAKE_CASE__=99			,						SCREAMING_SNAKE_CASE__=64			,						SCREAMING_SNAKE_CASE__=32			,						SCREAMING_SNAKE_CASE__=5			,						SCREAMING_SNAKE_CASE__=4			,						SCREAMING_SNAKE_CASE__=37			,						SCREAMING_SNAKE_CASE__="gelu"			,						SCREAMING_SNAKE_CASE__=0.1			,						SCREAMING_SNAKE_CASE__=0.1			,						SCREAMING_SNAKE_CASE__=512			,						SCREAMING_SNAKE_CASE__=16			,						SCREAMING_SNAKE_CASE__=2			,						SCREAMING_SNAKE_CASE__=0.02			,						SCREAMING_SNAKE_CASE__=3			,						SCREAMING_SNAKE_CASE__=4			,						SCREAMING_SNAKE_CASE__=None			,						):
								"""simple docstring"""







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







								lowerCAmelCase__			:     List[str]					   =      ids_tensor([self.batch_size, self.seq_length]			,						self.vocab_size     )

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

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

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

								lowerCAmelCase__			:     List[str]					   =      self.get_config()

								return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
					def lowercase_      (		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=__lowerCAmelCase			,						initializer_range=self.initializer_range			,						)
					def lowercase_      (		self			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__     ):
								"""simple docstring"""







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







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







								lowerCAmelCase__			:     str					   =      MobileBertForNextSentencePrediction(config=__lowerCAmelCase     )
								model.to(__lowerCAmelCase     )
								model.eval()
								lowerCAmelCase__			:     int					   =      model(
								    __lowerCAmelCase			,						attention_mask=__lowerCAmelCase			,						token_type_ids=__lowerCAmelCase			,						labels=__lowerCAmelCase			,						)
								self.parent.assertEqual(result.logits.shape			,						(self.batch_size, 2)     )
					def lowercase_      (		self			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__     ):
								"""simple docstring"""







								lowerCAmelCase__			:     List[str]					   =      MobileBertForPreTraining(config=__lowerCAmelCase     )
								model.to(__lowerCAmelCase     )
								model.eval()
								lowerCAmelCase__			:     List[Any]					   =      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 lowercase_      (		self			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__     ):
								"""simple docstring"""







								lowerCAmelCase__			:     int					   =      MobileBertForQuestionAnswering(config=__lowerCAmelCase     )
								model.to(__lowerCAmelCase     )
								model.eval()
								lowerCAmelCase__			:     int					   =      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 lowercase_      (		self			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__     ):
								"""simple docstring"""







								lowerCAmelCase__			:     Optional[Any]					   =      self.num_labels
								lowerCAmelCase__			:     List[str]					   =      MobileBertForSequenceClassification(__lowerCAmelCase     )
								model.to(__lowerCAmelCase     )
								model.eval()
								lowerCAmelCase__			:     Any					   =      model(__lowerCAmelCase			,						attention_mask=__lowerCAmelCase			,						token_type_ids=__lowerCAmelCase			,						labels=__lowerCAmelCase     )
								self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.num_labels)     )
					def lowercase_      (		self			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__     ):
								"""simple docstring"""







								lowerCAmelCase__			:     Tuple					   =      self.num_labels
								lowerCAmelCase__			:     int					   =      MobileBertForTokenClassification(config=__lowerCAmelCase     )
								model.to(__lowerCAmelCase     )
								model.eval()
								lowerCAmelCase__			:     Optional[Any]					   =      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 lowercase_      (		self			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__     ):
								"""simple docstring"""







								lowerCAmelCase__			:     int					   =      self.num_choices
								lowerCAmelCase__			:     Any					   =      MobileBertForMultipleChoice(config=__lowerCAmelCase     )
								model.to(__lowerCAmelCase     )
								model.eval()
								lowerCAmelCase__			:     Optional[Any]					   =      input_ids.unsqueeze(1     ).expand(-1			,						self.num_choices			,						-1     ).contiguous()
								lowerCAmelCase__			:     Union[str, Any]					   =      token_type_ids.unsqueeze(1     ).expand(-1			,						self.num_choices			,						-1     ).contiguous()
								lowerCAmelCase__			:     Union[str, Any]					   =      input_mask.unsqueeze(1     ).expand(-1			,						self.num_choices			,						-1     ).contiguous()
								lowerCAmelCase__			:     Tuple					   =      model(
								    __lowerCAmelCase			,						attention_mask=__lowerCAmelCase			,						token_type_ids=__lowerCAmelCase			,						labels=__lowerCAmelCase			,						)
								self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.num_choices)     )
					def lowercase_      (		self     ):
								"""simple docstring"""







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





@require_torch
class     lowercase							(	__UpperCamelCase						,		__UpperCamelCase						,		unittest.TestCase       ):
					__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 lowercase_      (		self			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__=False     ):
								"""simple docstring"""







								lowerCAmelCase__			:     int					   =      super()._prepare_for_class(__lowerCAmelCase			,						__lowerCAmelCase			,						return_labels=__lowerCAmelCase     )

								if return_labels:
											if model_class in get_values(__lowerCAmelCase     ):
														lowerCAmelCase__			:     Union[str, Any]					   =      torch.zeros(
														    (self.model_tester.batch_size, self.model_tester.seq_length)			,						dtype=torch.long			,						device=__lowerCAmelCase     )
														lowerCAmelCase__			:     List[str]					   =      torch.zeros(
														    self.model_tester.batch_size			,						dtype=torch.long			,						device=__lowerCAmelCase     )
								return inputs_dict
					def lowercase_      (		self     ):
								"""simple docstring"""







								lowerCAmelCase__			:     Tuple					   =      MobileBertModelTester(self     )
								lowerCAmelCase__			:     int					   =      ConfigTester(self			,						config_class=__lowerCAmelCase			,						hidden_size=37     )
					def lowercase_      (		self     ):
								"""simple docstring"""







								self.config_tester.run_common_tests()
					def lowercase_      (		self     ):
								"""simple docstring"""







								lowerCAmelCase__			:     List[str]					   =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_model(*__lowerCAmelCase     )
					def lowercase_      (		self     ):
								"""simple docstring"""







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







								lowerCAmelCase__			:     str					   =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_multiple_choice(*__lowerCAmelCase     )
					def lowercase_      (		self     ):
								"""simple docstring"""







								lowerCAmelCase__			:     int					   =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*__lowerCAmelCase     )
					def lowercase_      (		self     ):
								"""simple docstring"""







								lowerCAmelCase__			:     List[str]					   =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_pretraining(*__lowerCAmelCase     )
					def lowercase_      (		self     ):
								"""simple docstring"""







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







								lowerCAmelCase__			:     Optional[Any]					   =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_sequence_classification(*__lowerCAmelCase     )
					def lowercase_      (		self     ):
								"""simple docstring"""







								lowerCAmelCase__			:     Optional[Any]					   =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mobilebert_for_token_classification(*__lowerCAmelCase     )





def   _a  ( __UpperCamelCase							:	Tuple     ):
			return torch.tensor(
			    __a						,dtype=torch.long						,device=__a						,)


A__							: Any     =       1E-3





@require_torch
@require_sentencepiece
@require_tokenizers
class     lowercase							(	unittest.TestCase       ):
					@slow
					def lowercase_      (		self     ):
								"""simple docstring"""







								lowerCAmelCase__			:     Union[str, Any]					   =      MobileBertModel.from_pretrained('''google/mobilebert-uncased'''     ).to(__lowerCAmelCase     )
								lowerCAmelCase__			:     List[Any]					   =      _long_tensor([[101, 7110, 1005, 1056, 2023, 11333, 17413, 1029, 102]]     )
								with torch.no_grad():
											lowerCAmelCase__			:     Union[str, Any]					   =      model(__lowerCAmelCase     )[0]
								lowerCAmelCase__			:     Union[str, Any]					   =      torch.Size((1, 9, 512)     )
								self.assertEqual(output.shape			,						__lowerCAmelCase     )
								lowerCAmelCase__			:     str					   =      torch.tensor(
								    [
								        [
								            [-2.473_6526E07, 8.269_1656E04, 1.652_1838E05],
								            [-5.754_1704E-01, 3.905_6022E00, 4.401_1507E00],
								            [2.604_7359E00, 1.567_7652E00, -1.732_4188E-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
								lowerCAmelCase__			:     Optional[int]					   =      torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE     )
								lowerCAmelCase__			:     List[Any]					   =      torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE     )

								self.assertTrue(lower_bound and upper_bound     )










def   __lowerCamelCase   (				__a  :int       ,	__a  :int       )						->							Optional[Any]:






		"""simple docstring"""

		if b == 0:
				return 1
		if (b % 2) == 0:
				return actual_power(__a       ,	int(b / 2       )       ) * actual_power(__a       ,	int(b / 2       )       )
		else:
				return a * actual_power(__a       ,	int(b / 2       )       ) * actual_power(__a       ,	int(b / 2       )       )







def   __lowerCamelCase   (				__a  :int       ,	__a  :int       )						->							float:






		"""simple docstring"""

		if b < 0:
				return 1 / actual_power(__a       ,	__a       )
		return actual_power(__a       ,	__a       )


if __name__ == "__main__":
					print(power(-2, -3))



