

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 				UpperCAmelCase_		:
							"""simple docstring"""







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


							def 				__lowercase				(			self       )    ->    str:
												_a	:    List[str]         =		ids_tensor([self.batch_size, self.seq_length]       ,							self.vocab_size       )

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

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

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

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

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


							def 				__lowercase				(			self       )    ->    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=_a       ,							initializer_range=self.initializer_range       ,							)


							def 				__lowercase				(			self       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       )    ->    Optional[Any]:
												_a	:    List[str]         =		MobileBertModel(config=_a       )
												model.to(_a       )
												model.eval()
												_a	:    List[Any]         =		model(_a       ,							attention_mask=_a       ,							token_type_ids=_a       )
												_a	:    Union[str, Any]         =		model(_a       ,							token_type_ids=_a       )
												_a	:    int         =		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 				__lowercase				(			self       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       )    ->    str:
												_a	:    Optional[int]         =		MobileBertForMaskedLM(config=_a       )
												model.to(_a       )
												model.eval()
												_a	:    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 				__lowercase				(			self       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       )    ->    Optional[Any]:
												_a	:    List[str]         =		MobileBertForNextSentencePrediction(config=_a       )
												model.to(_a       )
												model.eval()
												_a	:    Dict         =		model(
												    _a       ,							attention_mask=_a       ,							token_type_ids=_a       ,							labels=_a       ,							)
												self.parent.assertEqual(result.logits.shape       ,							(self.batch_size, 2)       )


							def 				__lowercase				(			self       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       )    ->    List[str]:
												_a	:    Dict         =		MobileBertForPreTraining(config=_a       )
												model.to(_a       )
												model.eval()
												_a	:    Tuple         =		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 				__lowercase				(			self       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       )    ->    int:
												_a	:    int         =		MobileBertForQuestionAnswering(config=_a       )
												model.to(_a       )
												model.eval()
												_a	:    Any         =		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 				__lowercase				(			self       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       )    ->    Tuple:
												_a	:    Tuple         =		self.num_labels
												_a	:    Optional[int]         =		MobileBertForSequenceClassification(_a       )
												model.to(_a       )
												model.eval()
												_a	:    int         =		model(_a       ,							attention_mask=_a       ,							token_type_ids=_a       ,							labels=_a       )
												self.parent.assertEqual(result.logits.shape       ,							(self.batch_size, self.num_labels)       )


							def 				__lowercase				(			self       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       )    ->    Optional[int]:
												_a	:    Union[str, Any]         =		self.num_labels
												_a	:    Optional[Any]         =		MobileBertForTokenClassification(config=_a       )
												model.to(_a       )
												model.eval()
												_a	:    Optional[int]         =		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 				__lowercase				(			self       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       ,							_a       )    ->    List[str]:
												_a	:    Optional[Any]         =		self.num_choices
												_a	:    str         =		MobileBertForMultipleChoice(config=_a       )
												model.to(_a       )
												model.eval()
												_a	:    Union[str, Any]         =		input_ids.unsqueeze(1       ).expand(-1       ,							self.num_choices       ,							-1       ).contiguous()
												_a	:    int         =		token_type_ids.unsqueeze(1       ).expand(-1       ,							self.num_choices       ,							-1       ).contiguous()
												_a	:    Dict         =		input_mask.unsqueeze(1       ).expand(-1       ,							self.num_choices       ,							-1       ).contiguous()
												_a	:    int         =		model(
												    _a       ,							attention_mask=_a       ,							token_type_ids=_a       ,							labels=_a       ,							)
												self.parent.assertEqual(result.logits.shape       ,							(self.batch_size, self.num_choices)       )




							def 				__lowercase				(			self       )    ->    Tuple:
												_a	:    Any         =		self.prepare_config_and_inputs()
												(
												    (
												    _a
												)      ,					(
												    _a
												)      ,					(
												    _a
												)      ,					(
												    _a
												)      ,					(
												    _a
												)      ,					(
												    _a
												)      ,					(
												    _a
												)      ,					
												)	:    Optional[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 				UpperCAmelCase_		(			__lowercase				,       __lowercase				,       unittest.TestCase   ):
							"""simple docstring"""







							UpperCAmelCase__       :						Optional[int]	    =    (
							    (
							        MobileBertModel,
							        MobileBertForMaskedLM,
							        MobileBertForMultipleChoice,
							        MobileBertForNextSentencePrediction,
							        MobileBertForPreTraining,
							        MobileBertForQuestionAnswering,
							        MobileBertForSequenceClassification,
							        MobileBertForTokenClassification,
							    )
							    if is_torch_available()
							    else ()
							)
							UpperCAmelCase__       :						Dict	    =    (
							    {
							        "feature-extraction": MobileBertModel,
							        "fill-mask": MobileBertForMaskedLM,
							        "question-answering": MobileBertForQuestionAnswering,
							        "text-classification": MobileBertForSequenceClassification,
							        "token-classification": MobileBertForTokenClassification,
							        "zero-shot": MobileBertForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							UpperCAmelCase__       :						Union[str, Any]	    =    True


							def 				__lowercase				(			self       ,							_a       ,							_a       ,							_a=False       )    ->    Optional[Any]:
												_a	:    Any         =		super()._prepare_for_class(_a       ,							_a       ,							return_labels=_a       )

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


							def 				__lowercase				(			self       )    ->    Dict:
												_a	:    int         =		MobileBertModelTester(self       )
												_a	:    Tuple         =		ConfigTester(self       ,							config_class=_a       ,							hidden_size=3_7       )


							def 				__lowercase				(			self       )    ->    int:
												self.config_tester.run_common_tests()


							def 				__lowercase				(			self       )    ->    List[str]:
												_a	:    str         =		self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_mobilebert_model(*_a       )


							def 				__lowercase				(			self       )    ->    Optional[Any]:
												_a	:    Tuple         =		self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_mobilebert_for_masked_lm(*_a       )


							def 				__lowercase				(			self       )    ->    Optional[Any]:
												_a	:    int         =		self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_mobilebert_for_multiple_choice(*_a       )


							def 				__lowercase				(			self       )    ->    Tuple:
												_a	:    Tuple         =		self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*_a       )


							def 				__lowercase				(			self       )    ->    int:
												_a	:    List[str]         =		self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_mobilebert_for_pretraining(*_a       )


							def 				__lowercase				(			self       )    ->    str:
												_a	:    List[Any]         =		self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_mobilebert_for_question_answering(*_a       )


							def 				__lowercase				(			self       )    ->    List[str]:
												_a	:    Union[str, Any]         =		self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_mobilebert_for_sequence_classification(*_a       )




							def 				__lowercase				(			self       )    ->    Dict:
												_a	:    Union[str, Any]         =		self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_mobilebert_for_token_classification(*_a       )



def    __UpperCAmelCase (	__a      :			Optional[Any]	)			->     Tuple:

					"""simple docstring"""

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


a__				=		1E-3







@require_torch
@require_sentencepiece
@require_tokenizers
class 				UpperCAmelCase_		(			unittest.TestCase   ):
							"""simple docstring"""







							@slow
							def 				__lowercase				(			self       )    ->    Dict:
												_a	:    List[Any]         =		MobileBertModel.from_pretrained('''google/mobilebert-uncased'''       ).to(_a       )
												_a	:    Optional[Any]         =		_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	:    Dict         =		model(_a       )[0]
												_a	:    List[Any]         =		torch.Size((1, 9, 5_1_2)       )
												self.assertEqual(output.shape       ,							_a       )
												_a	:    Union[str, Any]         =		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=_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
												_a	:    Optional[int]         =		torch.all((expected_slice / output[..., :3, :3]) >= 1 - TOLERANCE       )
												_a	:    str         =		torch.all((expected_slice / output[..., :3, :3]) <= 1 + TOLERANCE       )

												self.assertTrue(lower_bound and upper_bound       )






from math import ceil



def    __UpperCAmelCase (	__a      :			int = 1_001	)			->     int:

					"""simple docstring"""

					_a	:    List[Any]         =		1

					for i in range(1		,int(ceil(n / 2.0	)	)	):
										_a	:    Optional[Any]         =		2 * i + 1
										_a	:    Optional[int]         =		2 * i
										_a	:    str         =		total + 4 * odd**2 - 6 * even

					return total


if __name__ == "__main__":
			import sys

			if len(sys.argv) == 1:
						print(solution())
			else:
						try:
									a__				=		int(sys.argv[1])
									print(solution(n))
						except ValueError:
									print('''Invalid entry - please enter a number''')



