





'''simple docstring'''

import os
import tempfile
import unittest

from transformers import DistilBertConfig, is_torch_available
from transformers.testing_utils import require_torch, require_torch_gpu, 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 (
       DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
       DistilBertForMaskedLM,
       DistilBertForMultipleChoice,
       DistilBertForQuestionAnswering,
       DistilBertForSequenceClassification,
       DistilBertForTokenClassification,
       DistilBertModel,
   )
class     __lowerCAmelCase					(   __magic_name__			):






       """simple docstring"""





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

            '''simple docstring'''




            _UpperCamelCase       =	parent
            _UpperCamelCase       =	batch_size
            _UpperCamelCase       =	seq_length
            _UpperCamelCase       =	is_training
            _UpperCamelCase       =	use_input_mask
            _UpperCamelCase       =	use_token_type_ids
            _UpperCamelCase       =	use_labels
            _UpperCamelCase       =	vocab_size
            _UpperCamelCase       =	hidden_size
            _UpperCamelCase       =	num_hidden_layers
            _UpperCamelCase       =	num_attention_heads
            _UpperCamelCase       =	intermediate_size
            _UpperCamelCase       =	hidden_act
            _UpperCamelCase       =	hidden_dropout_prob
            _UpperCamelCase       =	attention_probs_dropout_prob
            _UpperCamelCase       =	max_position_embeddings
            _UpperCamelCase       =	type_vocab_size
            _UpperCamelCase       =	type_sequence_label_size
            _UpperCamelCase       =	initializer_range
            _UpperCamelCase       =	num_labels
            _UpperCamelCase       =	num_choices
            _UpperCamelCase       =	scope





       def snake_case__						(							self       :		Tuple					)   ->					Any:

            '''simple docstring'''




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

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

            _UpperCamelCase       =	None
            _UpperCamelCase       =	None
            _UpperCamelCase       =	None
            if self.use_labels:
                 _UpperCamelCase       =	ids_tensor([self.batch_size]	,			self.type_sequence_label_size					)
                 _UpperCamelCase       =	ids_tensor([self.batch_size, self.seq_length]	,			self.num_labels					)
                 _UpperCamelCase       =	ids_tensor([self.batch_size]	,			self.num_choices					)

            _UpperCamelCase       =	self.get_config()

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





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

            '''simple docstring'''




            return DistilBertConfig(
                vocab_size=self.vocab_size	,			dim=self.hidden_size	,			n_layers=self.num_hidden_layers	,			n_heads=self.num_attention_heads	,			hidden_dim=self.intermediate_size	,			hidden_act=self.hidden_act	,			dropout=self.hidden_dropout_prob	,			attention_dropout=self.attention_probs_dropout_prob	,			max_position_embeddings=self.max_position_embeddings	,			initializer_range=self.initializer_range	,			)





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

            '''simple docstring'''




            _UpperCamelCase       =	DistilBertModel(config=lowerCAmelCase__					)
            model.to(lowerCAmelCase__					)
            model.eval()
            _UpperCamelCase       =	model(lowerCAmelCase__	,			lowerCAmelCase__					)
            _UpperCamelCase       =	model(lowerCAmelCase__					)
            self.parent.assertEqual(result.last_hidden_state.shape	,			(self.batch_size, self.seq_length, self.hidden_size)					)





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

            '''simple docstring'''




            _UpperCamelCase       =	DistilBertForMaskedLM(config=lowerCAmelCase__					)
            model.to(lowerCAmelCase__					)
            model.eval()
            _UpperCamelCase       =	model(lowerCAmelCase__	,			attention_mask=lowerCAmelCase__	,			labels=lowerCAmelCase__					)
            self.parent.assertEqual(result.logits.shape	,			(self.batch_size, self.seq_length, self.vocab_size)					)





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

            '''simple docstring'''




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

            '''simple docstring'''




            _UpperCamelCase       =	self.num_labels
            _UpperCamelCase       =	DistilBertForSequenceClassification(lowerCAmelCase__					)
            model.to(lowerCAmelCase__					)
            model.eval()
            _UpperCamelCase       =	model(lowerCAmelCase__	,			attention_mask=lowerCAmelCase__	,			labels=lowerCAmelCase__					)
            self.parent.assertEqual(result.logits.shape	,			(self.batch_size, self.num_labels)					)





       def snake_case__						(							self       :		Dict	,			lowerCAmelCase__       :		str	,			lowerCAmelCase__       :		Any	,			lowerCAmelCase__       :		Dict	,			lowerCAmelCase__       :		Union[str, Any]	,			lowerCAmelCase__       :		int	,			lowerCAmelCase__       :		Union[str, Any]					)   ->					int:

            '''simple docstring'''




            _UpperCamelCase       =	self.num_labels
            _UpperCamelCase       =	DistilBertForTokenClassification(config=lowerCAmelCase__					)
            model.to(lowerCAmelCase__					)
            model.eval()

            _UpperCamelCase       =	model(lowerCAmelCase__	,			attention_mask=lowerCAmelCase__	,			labels=lowerCAmelCase__					)
            self.parent.assertEqual(result.logits.shape	,			(self.batch_size, self.seq_length, self.num_labels)					)





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

            '''simple docstring'''




            _UpperCamelCase       =	self.num_choices
            _UpperCamelCase       =	DistilBertForMultipleChoice(config=lowerCAmelCase__					)
            model.to(lowerCAmelCase__					)
            model.eval()
            _UpperCamelCase       =	input_ids.unsqueeze(1					).expand(-1	,			self.num_choices	,			-1					).contiguous()
            _UpperCamelCase       =	input_mask.unsqueeze(1					).expand(-1	,			self.num_choices	,			-1					).contiguous()
            _UpperCamelCase       =	model(
                lowerCAmelCase__	,			attention_mask=lowerCAmelCase__	,			labels=lowerCAmelCase__	,			)
            self.parent.assertEqual(result.logits.shape	,			(self.batch_size, self.num_choices)					)







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

            '''simple docstring'''




            _UpperCamelCase       =	self.prepare_config_and_inputs()
            ((_UpperCamelCase)   ,    (_UpperCamelCase)   ,    (_UpperCamelCase)   ,    (_UpperCamelCase)   ,    (_UpperCamelCase)   ,    (_UpperCamelCase))       =	config_and_inputs
            _UpperCamelCase       =	{'''input_ids''': input_ids, '''attention_mask''': input_mask}
            return config, inputs_dict

@require_torch
class     __lowerCAmelCase					(   __magic_name__						,						__magic_name__						,						unittest.TestCase			):






       """simple docstring"""
       _snake_case     :					int						    =   (
           (
               DistilBertModel,
               DistilBertForMaskedLM,
               DistilBertForMultipleChoice,
               DistilBertForQuestionAnswering,
               DistilBertForSequenceClassification,
               DistilBertForTokenClassification,
           )
           if is_torch_available()
           else None
       )
       _snake_case     :					Optional[int]						    =   (
           {
               'feature-extraction': DistilBertModel,
               'fill-mask': DistilBertForMaskedLM,
               'question-answering': DistilBertForQuestionAnswering,
               'text-classification': DistilBertForSequenceClassification,
               'token-classification': DistilBertForTokenClassification,
               'zero-shot': DistilBertForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )
       _snake_case     :					Optional[int]						    =   True
       _snake_case     :					Tuple						    =   True
       _snake_case     :					Optional[Any]						    =   True
       _snake_case     :					int						    =   True





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

            '''simple docstring'''




            _UpperCamelCase       =	DistilBertModelTester(self					)
            _UpperCamelCase       =	ConfigTester(self	,			config_class=lowerCAmelCase__	,			dim=37					)





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

            '''simple docstring'''




            self.config_tester.run_common_tests()





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

            '''simple docstring'''




            _UpperCamelCase       =	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_distilbert_model(*lowerCAmelCase__					)





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

            '''simple docstring'''




            _UpperCamelCase       =	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_distilbert_for_masked_lm(*lowerCAmelCase__					)





       def snake_case__						(							self       :		Dict					)   ->					Dict:

            '''simple docstring'''




            _UpperCamelCase       =	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_distilbert_for_question_answering(*lowerCAmelCase__					)





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

            '''simple docstring'''




            _UpperCamelCase       =	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_distilbert_for_sequence_classification(*lowerCAmelCase__					)





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

            '''simple docstring'''




            _UpperCamelCase       =	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_distilbert_for_token_classification(*lowerCAmelCase__					)





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

            '''simple docstring'''




            _UpperCamelCase       =	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_distilbert_for_multiple_choice(*lowerCAmelCase__					)





       @slow
       def snake_case__						(							self       :		Tuple					)   ->					Dict:

            '''simple docstring'''




            for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 _UpperCamelCase       =	DistilBertModel.from_pretrained(lowerCAmelCase__					)
                 self.assertIsNotNone(lowerCAmelCase__					)







       @slow
       @require_torch_gpu
       def snake_case__						(							self       :		Union[str, Any]					)   ->					str:

            '''simple docstring'''




            _UpperCamelCase   ,    _UpperCamelCase       =	self.model_tester.prepare_config_and_inputs_for_common()
            for model_class in self.all_model_classes:
                 # BertForMultipleChoice behaves incorrectly in JIT environments.
                 if model_class == DistilBertForMultipleChoice:
                      return

                 _UpperCamelCase       =	True
                 _UpperCamelCase       =	model_class(config=lowerCAmelCase__					)

                 _UpperCamelCase       =	self._prepare_for_class(lowerCAmelCase__	,			lowerCAmelCase__					)
                 _UpperCamelCase       =	torch.jit.trace(
                     lowerCAmelCase__	,			(inputs_dict['''input_ids'''].to('''cpu'''					), inputs_dict['''attention_mask'''].to('''cpu'''					))					)

                 with tempfile.TemporaryDirectory() as tmp:
                      torch.jit.save(lowerCAmelCase__	,			os.path.join(lowerCAmelCase__	,			'''traced_model.pt'''					)					)
                      _UpperCamelCase       =	torch.jit.load(os.path.join(lowerCAmelCase__	,			'''traced_model.pt'''					)	,			map_location=lowerCAmelCase__					)
                      loaded(inputs_dict['''input_ids'''].to(lowerCAmelCase__					)	,			inputs_dict['''attention_mask'''].to(lowerCAmelCase__					)					)

@require_torch
class     __lowerCAmelCase					(   unittest.TestCase			):






       """simple docstring"""





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

            '''simple docstring'''




            _UpperCamelCase       =	DistilBertModel.from_pretrained('''distilbert-base-uncased'''					)
            _UpperCamelCase       =	torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]					)
            _UpperCamelCase       =	torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]					)
            with torch.no_grad():
                 _UpperCamelCase       =	model(lowerCAmelCase__	,			attention_mask=lowerCAmelCase__					)[0]
            _UpperCamelCase       =	torch.Size((1, 11, 768)					)
            self.assertEqual(output.shape	,			lowerCAmelCase__					)
            _UpperCamelCase       =	torch.tensor(
                [[[-0.1639, 0.3299, 0.1648], [-0.1746, 0.3289, 0.1710], [-0.1884, 0.3357, 0.1810]]]					)

            self.assertTrue(torch.allclose(output[:, 1:4, 1:4]	,			lowerCAmelCase__	,			atol=1e-4					)					)










'''simple docstring'''

import random
class     __lowerCAmelCase					:






       """simple docstring"""





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

            '''simple docstring'''




            _UpperCamelCase       =	[ord(lowerCAmelCase__					) for i in text]
            _UpperCamelCase       =	[]
            _UpperCamelCase       =	[]
            for i in plain:
                 _UpperCamelCase       =	random.randint(1	,			300					)
                 _UpperCamelCase       =	(i + k) * k
                 cipher.append(lowerCAmelCase__					)
                 key.append(lowerCAmelCase__					)
            return cipher, key







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

            '''simple docstring'''




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


if __name__ == "__main__":
   lowercase__	,       lowercase__						:      List[str]	=    Onepad().encrypt('Hello')
   print(c, k)
   print(Onepad().decrypt(c, k))



