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







				'''simple docstring'''





				def __init__(						self					:						Dict       ,		lowercase__					:						Dict       ,		lowercase__					:						Union[str, Any]=13       ,		lowercase__					:						Optional[int]=7       ,		lowercase__					:						Tuple=True       ,		lowercase__					:						Optional[Any]=True       ,		lowercase__					:						str=True       ,		lowercase__					:						Tuple=True       ,		lowercase__					:						Dict=99       ,		lowercase__					:						Dict=64       ,		lowercase__					:						Tuple=32       ,		lowercase__					:						Optional[int]=5       ,		lowercase__					:						Union[str, Any]=4       ,		lowercase__					:						List[str]=37       ,		lowercase__					:						int="gelu"       ,		lowercase__					:						str=0.1       ,		lowercase__					:						Dict=0.1       ,		lowercase__					:						int=512       ,		lowercase__					:						Optional[int]=16       ,		lowercase__					:						List[str]=2       ,		lowercase__					:						Union[str, Any]=0.0_2       ,		lowercase__					:						Tuple=3       ,		lowercase__					:						Tuple=4       ,		lowercase__					:						Union[str, Any]=None       ,		)							->str:


										'''simple docstring'''




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





				def  snake_case__				(						self					:						Tuple					)							->int:


										'''simple docstring'''




										_UpperCamelCase			:			List[Any]   			=						ids_tensor([self.batch_size, self.seq_length]       ,		self.vocab_size					)

										_UpperCamelCase			:			int   			=						None
										if self.use_input_mask:
																_UpperCamelCase			:			Dict   			=						random_attention_mask([self.batch_size, self.seq_length]					)

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

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

										_UpperCamelCase			:			Union[str, Any]   			=						self.get_config()

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





				def  snake_case__				(						self					:						str					)							->Tuple:


										'''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  snake_case__				(						self					:						str       ,		lowercase__					:						Optional[int]       ,		lowercase__					:						int       ,		lowercase__					:						List[Any]       ,		lowercase__					:						Optional[Any]       ,		lowercase__					:						str       ,		lowercase__					:						List[str]       ,		lowercase__					:						List[Any]					)							->Optional[int]:


										'''simple docstring'''




										_UpperCamelCase			:			Dict   			=						MobileBertModel(config=lowercase__					)
										model.to(lowercase__					)
										model.eval()
										_UpperCamelCase			:			str   			=						model(lowercase__       ,		attention_mask=lowercase__       ,		token_type_ids=lowercase__					)
										_UpperCamelCase			:			Tuple   			=						model(lowercase__       ,		token_type_ids=lowercase__					)
										_UpperCamelCase			:			List[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  snake_case__				(						self					:						Dict       ,		lowercase__					:						List[Any]       ,		lowercase__					:						Optional[Any]       ,		lowercase__					:						Tuple       ,		lowercase__					:						List[str]       ,		lowercase__					:						Any       ,		lowercase__					:						Union[str, Any]       ,		lowercase__					:						Union[str, Any]					)							->int:


										'''simple docstring'''




										_UpperCamelCase			:			List[Any]   			=						MobileBertForMaskedLM(config=lowercase__					)
										model.to(lowercase__					)
										model.eval()
										_UpperCamelCase			:			Tuple   			=						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  snake_case__				(						self					:						Optional[int]       ,		lowercase__					:						Dict       ,		lowercase__					:						List[Any]       ,		lowercase__					:						int       ,		lowercase__					:						Tuple       ,		lowercase__					:						Optional[Any]       ,		lowercase__					:						Any       ,		lowercase__					:						Any					)							->Any:


										'''simple docstring'''




										_UpperCamelCase			:			int   			=						MobileBertForNextSentencePrediction(config=lowercase__					)
										model.to(lowercase__					)
										model.eval()
										_UpperCamelCase			:			Tuple   			=						model(
										    lowercase__       ,		attention_mask=lowercase__       ,		token_type_ids=lowercase__       ,		labels=lowercase__       ,		)
										self.parent.assertEqual(result.logits.shape       ,		(self.batch_size, 2)					)





				def  snake_case__				(						self					:						Any       ,		lowercase__					:						str       ,		lowercase__					:						Dict       ,		lowercase__					:						int       ,		lowercase__					:						int       ,		lowercase__					:						Optional[int]       ,		lowercase__					:						Tuple       ,		lowercase__					:						List[Any]					)							->Union[str, Any]:


										'''simple docstring'''




										_UpperCamelCase			:			Dict   			=						MobileBertForPreTraining(config=lowercase__					)
										model.to(lowercase__					)
										model.eval()
										_UpperCamelCase			:			int   			=						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  snake_case__				(						self					:						str       ,		lowercase__					:						Optional[Any]       ,		lowercase__					:						Dict       ,		lowercase__					:						Dict       ,		lowercase__					:						Dict       ,		lowercase__					:						List[str]       ,		lowercase__					:						Tuple       ,		lowercase__					:						Tuple					)							->Dict:


										'''simple docstring'''




										_UpperCamelCase			:			Optional[int]   			=						MobileBertForQuestionAnswering(config=lowercase__					)
										model.to(lowercase__					)
										model.eval()
										_UpperCamelCase			:			Optional[Any]   			=						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  snake_case__				(						self					:						Tuple       ,		lowercase__					:						Union[str, Any]       ,		lowercase__					:						str       ,		lowercase__					:						List[str]       ,		lowercase__					:						Any       ,		lowercase__					:						Tuple       ,		lowercase__					:						Any       ,		lowercase__					:						Optional[Any]					)							->Union[str, Any]:


										'''simple docstring'''




										_UpperCamelCase			:			Dict   			=						self.num_labels
										_UpperCamelCase			:			str   			=						MobileBertForSequenceClassification(lowercase__					)
										model.to(lowercase__					)
										model.eval()
										_UpperCamelCase			:			Tuple   			=						model(lowercase__       ,		attention_mask=lowercase__       ,		token_type_ids=lowercase__       ,		labels=lowercase__					)
										self.parent.assertEqual(result.logits.shape       ,		(self.batch_size, self.num_labels)					)





				def  snake_case__				(						self					:						Any       ,		lowercase__					:						Dict       ,		lowercase__					:						List[Any]       ,		lowercase__					:						Any       ,		lowercase__					:						int       ,		lowercase__					:						str       ,		lowercase__					:						int       ,		lowercase__					:						Optional[int]					)							->Dict:


										'''simple docstring'''




										_UpperCamelCase			:			str   			=						self.num_labels
										_UpperCamelCase			:			Optional[int]   			=						MobileBertForTokenClassification(config=lowercase__					)
										model.to(lowercase__					)
										model.eval()
										_UpperCamelCase			:			Tuple   			=						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  snake_case__				(						self					:						List[Any]       ,		lowercase__					:						int       ,		lowercase__					:						int       ,		lowercase__					:						Optional[Any]       ,		lowercase__					:						List[str]       ,		lowercase__					:						Union[str, Any]       ,		lowercase__					:						Union[str, Any]       ,		lowercase__					:						Dict					)							->Tuple:


										'''simple docstring'''




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







				def  snake_case__				(						self					:						List[str]					)							->List[str]:


										'''simple docstring'''




										_UpperCamelCase			:			Dict   			=						self.prepare_config_and_inputs()
										(
										    (
										    _UpperCamelCase
										)			,       (
										    _UpperCamelCase
										)			,       (
										    _UpperCamelCase
										)			,       (
										    _UpperCamelCase
										)			,       (
										    _UpperCamelCase
										)			,       (
										    _UpperCamelCase
										)			,       (
										    _UpperCamelCase
										)			,       
										)			:			int   			=						config_and_inputs
										_UpperCamelCase			:			Tuple   			=						{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
										return config, inputs_dict





@require_torch
class 		SCREAMING_SNAKE_CASE     (					SCREAMING_SNAKE_CASE   , SCREAMING_SNAKE_CASE   , unittest.TestCase	):







				'''simple docstring'''
				UpperCAmelCase__							=							(
				    (
				        MobileBertModel,
				        MobileBertForMaskedLM,
				        MobileBertForMultipleChoice,
				        MobileBertForNextSentencePrediction,
				        MobileBertForPreTraining,
				        MobileBertForQuestionAnswering,
				        MobileBertForSequenceClassification,
				        MobileBertForTokenClassification,
				    )
				    if is_torch_available()
				    else ()
				)
				UpperCAmelCase__							=							(
				    {
				        '''feature-extraction''': MobileBertModel,
				        '''fill-mask''': MobileBertForMaskedLM,
				        '''question-answering''': MobileBertForQuestionAnswering,
				        '''text-classification''': MobileBertForSequenceClassification,
				        '''token-classification''': MobileBertForTokenClassification,
				        '''zero-shot''': MobileBertForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)
				UpperCAmelCase__							=							True





				def  snake_case__				(						self					:						Optional[int]       ,		lowercase__					:						Dict       ,		lowercase__					:						int       ,		lowercase__					:						Any=False					)							->Any:


										'''simple docstring'''




										_UpperCamelCase			:			Optional[Any]   			=						super()._prepare_for_class(lowercase__       ,		lowercase__       ,		return_labels=lowercase__					)

										if return_labels:
																if model_class in get_values(lowercase__					):
																						_UpperCamelCase			:			Union[str, Any]   			=						torch.zeros(
																						    (self.model_tester.batch_size, self.model_tester.seq_length)       ,		dtype=torch.long       ,		device=lowercase__					)
																						_UpperCamelCase			:			Optional[Any]   			=						torch.zeros(
																						    self.model_tester.batch_size       ,		dtype=torch.long       ,		device=lowercase__					)
										return inputs_dict





				def  snake_case__				(						self					:						Dict					)							->Dict:


										'''simple docstring'''




										_UpperCamelCase			:			Union[str, Any]   			=						MobileBertModelTester(self					)
										_UpperCamelCase			:			Optional[int]   			=						ConfigTester(self       ,		config_class=lowercase__       ,		hidden_size=37					)





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


										'''simple docstring'''




										self.config_tester.run_common_tests()





				def  snake_case__				(						self					:						Tuple					)							->List[str]:


										'''simple docstring'''




										_UpperCamelCase			:			Tuple   			=						self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_model(*lowercase__					)





				def  snake_case__				(						self					:						Optional[Any]					)							->List[Any]:


										'''simple docstring'''




										_UpperCamelCase			:			Union[str, Any]   			=						self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_masked_lm(*lowercase__					)





				def  snake_case__				(						self					:						str					)							->str:


										'''simple docstring'''




										_UpperCamelCase			:			Dict   			=						self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_multiple_choice(*lowercase__					)





				def  snake_case__				(						self					:						Tuple					)							->List[str]:


										'''simple docstring'''




										_UpperCamelCase			:			List[str]   			=						self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_next_sequence_prediction(*lowercase__					)





				def  snake_case__				(						self					:						Tuple					)							->Union[str, Any]:


										'''simple docstring'''




										_UpperCamelCase			:			int   			=						self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_pretraining(*lowercase__					)





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


										'''simple docstring'''




										_UpperCamelCase			:			List[Any]   			=						self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_question_answering(*lowercase__					)





				def  snake_case__				(						self					:						str					)							->List[str]:


										'''simple docstring'''




										_UpperCamelCase			:			int   			=						self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_sequence_classification(*lowercase__					)







				def  snake_case__				(						self					:						Tuple					)							->List[str]:


										'''simple docstring'''




										_UpperCamelCase			:			Optional[int]   			=						self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_mobilebert_for_token_classification(*lowercase__					)





def 						__A							(     UpperCAmelCase					)     ->			Dict:




						'''simple docstring'''
						return torch.tensor(
						    UpperCAmelCase     ,dtype=torch.long     ,device=UpperCAmelCase     ,)


lowerCAmelCase_	:		List[Any]			=     1E-3






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







				'''simple docstring'''





				@slow
				def  snake_case__				(						self					:						Dict					)							->Optional[Any]:


										'''simple docstring'''




										_UpperCamelCase			:			str   			=						MobileBertModel.from_pretrained("google/mobilebert-uncased"					).to(lowercase__					)
										_UpperCamelCase			:			Optional[Any]   			=						_long_tensor([[101, 7_110, 1_005, 1_056, 2_023, 11_333, 17_413, 1_029, 102]]					)
										with torch.no_grad():
																_UpperCamelCase			:			List[Any]   			=						model(lowercase__					)[0]
										_UpperCamelCase			:			List[str]   			=						torch.Size((1, 9, 512)					)
										self.assertEqual(output.shape       ,		lowercase__					)
										_UpperCamelCase			:			List[Any]   			=						torch.tensor(
										    [
										        [
										            [-2.4736526e07, 8.2691656e04, 1.6521838e05],
										            [-5.7541704e-01, 3.9056022e00, 4.4011507e00],
										            [2.6047359e00, 1.5677652e00, -1.7324188e-01],
										        ]
										    ]       ,		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			:			Optional[int]   			=						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'''





import random




class 		SCREAMING_SNAKE_CASE     :







				'''simple docstring'''





				@staticmethod
				def  snake_case__				(						lowercase__					:						str					)							->tuple[list[int], list[int]]:


										'''simple docstring'''




										_UpperCamelCase			:			List[str]   			=						[ord(lowercase__					) for i in text]
										_UpperCamelCase			:			Dict   			=						[]
										_UpperCamelCase			:			Optional[int]   			=						[]
										for i in plain:
																_UpperCamelCase			:			Tuple   			=						random.randint(1       ,		300					)
																_UpperCamelCase			:			Any   			=						(i + k) * k
																cipher.append(lowercase__					)
																key.append(lowercase__					)
										return cipher, key







				@staticmethod
				def  snake_case__				(						lowercase__					:						list[int]       ,		lowercase__					:						list[int]					)							->str:


										'''simple docstring'''




										_UpperCamelCase			:			int   			=						[]
										for i in range(len(lowercase__					)					):
																_UpperCamelCase			:			List[Any]   			=						int((cipher[i] - (key[i]) ** 2) / key[i]					)
																plain.append(chr(lowercase__					)					)
										return "".join(lowercase__					)


if __name__ == "__main__":
			lowerCAmelCase_	,	lowerCAmelCase_	:		Optional[int]			=     Onepad().encrypt("""Hello""")
			print(c, k)
			print(Onepad().decrypt(c, k))


