

'''simple docstring'''

from __future__ import annotations

import unittest

from transformers import DebertaVaConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
 import tensorflow as tf

 from transformers import (
     TFDebertaVaForMaskedLM,
     TFDebertaVaForQuestionAnswering,
     TFDebertaVaForSequenceClassification,
     TFDebertaVaForTokenClassification,
     TFDebertaVaModel,
 )




class 	A_			:






     '''simple docstring'''



     def __init__(		self			:					Union[str, Any]					,      lowercase_			:					Dict					,      lowercase_			:					Optional[Any]=13					,      lowercase_			:					Union[str, Any]=7					,      lowercase_			:					Tuple=True					,      lowercase_			:					List[str]=True					,      lowercase_			:					Optional[Any]=True					,      lowercase_			:					str=True					,      lowercase_			:					Dict=99					,      lowercase_			:					Tuple=32					,      lowercase_			:					Optional[Any]=2					,      lowercase_			:					Union[str, Any]=4					,      lowercase_			:					Any=37					,      lowercase_			:					Tuple="gelu"					,      lowercase_			:					Any=0.1					,      lowercase_			:					Optional[int]=0.1					,      lowercase_			:					Optional[int]=512					,      lowercase_			:					Dict=16					,      lowercase_			:					List[Any]=2					,      lowercase_			:					Optional[Any]=0.02					,      lowercase_			:					Dict=False					,      lowercase_			:					Optional[Any]=True					,      lowercase_			:					Union[str, Any]="None"					,      lowercase_			:					int=3					,      lowercase_			:					Any=4					,      lowercase_			:					str=None					,      )						->     Optional[int]:
      UpperCAmelCase	:     List[str]						 =			parent
      UpperCAmelCase	:     Union[str, Any]						 =			batch_size
      UpperCAmelCase	:     List[Any]						 =			seq_length
      UpperCAmelCase	:     Optional[int]						 =			is_training
      UpperCAmelCase	:     int						 =			use_input_mask
      UpperCAmelCase	:     List[str]						 =			use_token_type_ids
      UpperCAmelCase	:     Tuple						 =			use_labels
      UpperCAmelCase	:     Optional[int]						 =			vocab_size
      UpperCAmelCase	:     List[Any]						 =			hidden_size
      UpperCAmelCase	:     Dict						 =			num_hidden_layers
      UpperCAmelCase	:     Dict						 =			num_attention_heads
      UpperCAmelCase	:     Any						 =			intermediate_size
      UpperCAmelCase	:     Dict						 =			hidden_act
      UpperCAmelCase	:     List[str]						 =			hidden_dropout_prob
      UpperCAmelCase	:     List[Any]						 =			attention_probs_dropout_prob
      UpperCAmelCase	:     Dict						 =			max_position_embeddings
      UpperCAmelCase	:     Any						 =			type_vocab_size
      UpperCAmelCase	:     Any						 =			type_sequence_label_size
      UpperCAmelCase	:     Tuple						 =			initializer_range
      UpperCAmelCase	:     List[Any]						 =			num_labels
      UpperCAmelCase	:     Tuple						 =			num_choices
      UpperCAmelCase	:     int						 =			relative_attention
      UpperCAmelCase	:     List[Any]						 =			position_biased_input
      UpperCAmelCase	:     Dict						 =			pos_att_type
      UpperCAmelCase	:     Union[str, Any]						 =			scope


     def        UpperCAmelCase_	(		self			:					Optional[Any]	)						->     Union[str, Any]:
      UpperCAmelCase	:     Tuple						 =			ids_tensor([self.batch_size, self.seq_length]					,      self.vocab_size	)

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

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

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

      UpperCAmelCase	:     Tuple						 =			DebertaVaConfig(
          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					,      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					,      relative_attention=self.relative_attention					,      position_biased_input=self.position_biased_input					,      initializer_range=self.initializer_range					,      return_dict=lowercase_					,      )

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


     def        UpperCAmelCase_	(		self			:					List[str]					,      lowercase_			:					Any					,      lowercase_			:					int					,      lowercase_			:					Dict					,      lowercase_			:					Union[str, Any]					,      lowercase_			:					Optional[int]					,      lowercase_			:					Any					,      lowercase_			:					str	)						->     Tuple:
      UpperCAmelCase	:     int						 =			TFDebertaVaModel(config=lowercase_	)
      UpperCAmelCase	:     Optional[int]						 =			{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}

      UpperCAmelCase	:     List[str]						 =			[input_ids, input_mask]
      UpperCAmelCase	:     List[Any]						 =			model(lowercase_	)

      UpperCAmelCase	:     Union[str, Any]						 =			model(lowercase_	)

      self.parent.assertEqual(result.last_hidden_state.shape					,      (self.batch_size, self.seq_length, self.hidden_size)	)


     def        UpperCAmelCase_	(		self			:					Dict					,      lowercase_			:					List[Any]					,      lowercase_			:					Union[str, Any]					,      lowercase_			:					Optional[int]					,      lowercase_			:					List[Any]					,      lowercase_			:					Dict					,      lowercase_			:					Any					,      lowercase_			:					Optional[int]	)						->     str:
      UpperCAmelCase	:     Union[str, Any]						 =			TFDebertaVaForMaskedLM(config=lowercase_	)
      UpperCAmelCase	:     Optional[Any]						 =			{
          'input_ids': input_ids,
          'attention_mask': input_mask,
          'token_type_ids': token_type_ids,
      }
      UpperCAmelCase	:     int						 =			model(lowercase_	)
      self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.seq_length, self.vocab_size)	)


     def        UpperCAmelCase_	(		self			:					List[Any]					,      lowercase_			:					Any					,      lowercase_			:					str					,      lowercase_			:					List[Any]					,      lowercase_			:					int					,      lowercase_			:					Union[str, Any]					,      lowercase_			:					str					,      lowercase_			:					Union[str, Any]	)						->     Optional[Any]:
      UpperCAmelCase	:     Tuple						 =			self.num_labels
      UpperCAmelCase	:     Dict						 =			TFDebertaVaForSequenceClassification(config=lowercase_	)
      UpperCAmelCase	:     List[str]						 =			{
          'input_ids': input_ids,
          'attention_mask': input_mask,
          'token_type_ids': token_type_ids,
      }

      UpperCAmelCase	:     str						 =			model(lowercase_	)
      self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.num_labels)	)


     def        UpperCAmelCase_	(		self			:					str					,      lowercase_			:					List[Any]					,      lowercase_			:					List[str]					,      lowercase_			:					List[str]					,      lowercase_			:					Any					,      lowercase_			:					int					,      lowercase_			:					int					,      lowercase_			:					List[Any]	)						->     Tuple:
      UpperCAmelCase	:     Union[str, Any]						 =			self.num_labels
      UpperCAmelCase	:     Optional[int]						 =			TFDebertaVaForTokenClassification(config=lowercase_	)
      UpperCAmelCase	:     Optional[int]						 =			{
          'input_ids': input_ids,
          'attention_mask': input_mask,
          'token_type_ids': token_type_ids,
      }
      UpperCAmelCase	:     Dict						 =			model(lowercase_	)
      self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.seq_length, self.num_labels)	)


     def        UpperCAmelCase_	(		self			:					Any					,      lowercase_			:					Union[str, Any]					,      lowercase_			:					Union[str, Any]					,      lowercase_			:					Tuple					,      lowercase_			:					List[str]					,      lowercase_			:					int					,      lowercase_			:					Dict					,      lowercase_			:					Any	)						->     int:
      UpperCAmelCase	:     Dict						 =			TFDebertaVaForQuestionAnswering(config=lowercase_	)
      UpperCAmelCase	:     str						 =			{
          'input_ids': input_ids,
          'attention_mask': input_mask,
          'token_type_ids': token_type_ids,
      }

      UpperCAmelCase	:     Union[str, Any]						 =			model(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        UpperCAmelCase_	(		self			:					int	)						->     List[Any]:
      UpperCAmelCase	:     Tuple						 =			self.prepare_config_and_inputs()
      (
          (
          UpperCAmelCase
      ) ,			(
          UpperCAmelCase
      ) ,			(
          UpperCAmelCase
      ) ,			(
          UpperCAmelCase
      ) ,			(
          UpperCAmelCase
      ) ,			(
          UpperCAmelCase
      ) ,			(
          UpperCAmelCase
      ) ,			
      )	:     Optional[Any]						 =			config_and_inputs
      UpperCAmelCase	:     Any						 =			{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
      return config, inputs_dict




@require_tf
class 	A_			(    _snake_case	,       _snake_case	,       unittest.TestCase							):






     '''simple docstring'''



     UpperCAmelCase_						:     str  		=  (
         (
             TFDebertaVaModel,
             TFDebertaVaForMaskedLM,
             TFDebertaVaForQuestionAnswering,
             TFDebertaVaForSequenceClassification,
             TFDebertaVaForTokenClassification,
         )
         if is_tf_available()
         else ()
     )
     UpperCAmelCase_						:     Dict  		=  (
         {
             """feature-extraction""": TFDebertaVaModel,
             """fill-mask""": TFDebertaVaForMaskedLM,
             """question-answering""": TFDebertaVaForQuestionAnswering,
             """text-classification""": TFDebertaVaForSequenceClassification,
             """token-classification""": TFDebertaVaForTokenClassification,
             """zero-shot""": TFDebertaVaForSequenceClassification,
         }
         if is_tf_available()
         else {}
     )

     UpperCAmelCase_						:     List[str]  		=  False
     UpperCAmelCase_						:     str  		=  False


     def        UpperCAmelCase_	(		self			:					Dict	)						->     int:
      UpperCAmelCase	:     List[str]						 =			TFDebertaVaModelTester(self	)
      UpperCAmelCase	:     Tuple						 =			ConfigTester(self					,      config_class=lowercase_					,      hidden_size=37	)


     def        UpperCAmelCase_	(		self			:					Optional[Any]	)						->     int:
      self.config_tester.run_common_tests()


     def        UpperCAmelCase_	(		self			:					Optional[Any]	)						->     List[str]:
      UpperCAmelCase	:     Union[str, Any]						 =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*lowercase_	)


     def        UpperCAmelCase_	(		self			:					List[str]	)						->     Optional[Any]:
      UpperCAmelCase	:     List[Any]						 =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_masked_lm(*lowercase_	)


     def        UpperCAmelCase_	(		self			:					Any	)						->     Dict:
      UpperCAmelCase	:     Union[str, Any]						 =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_question_answering(*lowercase_	)


     def        UpperCAmelCase_	(		self			:					Optional[int]	)						->     int:
      UpperCAmelCase	:     int						 =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_sequence_classification(*lowercase_	)


     def        UpperCAmelCase_	(		self			:					Tuple	)						->     Optional[int]:
      UpperCAmelCase	:     int						 =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_token_classification(*lowercase_	)




     @slow
     def        UpperCAmelCase_	(		self			:					str	)						->     Optional[int]:
      UpperCAmelCase	:     Any						 =			TFDebertaVaModel.from_pretrained('kamalkraj/deberta-v2-xlarge'	)
      self.assertIsNotNone(lowercase_	)




@require_tf
class 	A_			(    unittest.TestCase							):






     '''simple docstring'''



     @unittest.skip(reason='Model not available yet'	)
     def        UpperCAmelCase_	(		self			:					str	)						->     Optional[Any]:
      pass




     @slow
     def        UpperCAmelCase_	(		self			:					Optional[Any]	)						->     str:
      UpperCAmelCase	:     int						 =			TFDebertaVaModel.from_pretrained('kamalkraj/deberta-v2-xlarge'	)
      UpperCAmelCase	:     Optional[Any]						 =			tf.constant([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]]	)
      UpperCAmelCase	:     str						 =			tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]	)
      UpperCAmelCase	:     Union[str, Any]						 =			model(lowercase_					,      attention_mask=lowercase_	)[0]

      UpperCAmelCase	:     Optional[int]						 =			tf.constant(
          [[[0.2356, 0.1948, 0.0369], [-0.1063, 0.3586, -0.5152], [-0.6399, -0.0259, -0.2525]]]	)
      tf.debugging.assert_near(output[:, 1:4, 1:4]					,      lowercase_					,      atol=1E-4	)







'''simple docstring'''

import os
import random
import sys

from . import cryptomath_module as cryptomath
from . import rabin_miller

lowercase__         =      3




def 				UpperCamelCase(					UpperCAmelCase_     ):
 print('Generating primitive root of p'     )
 while True:
  UpperCAmelCase	:     Union[str, Any]						 =			random.randrange(3  ,	UpperCAmelCase_     )
  if pow(UpperCAmelCase_  ,	2  ,	UpperCAmelCase_     ) == 1:
   continue
  if pow(UpperCAmelCase_  ,	UpperCAmelCase_  ,	UpperCAmelCase_     ) == 1:
   continue
  return g




def 				UpperCamelCase(					UpperCAmelCase_     ):
 print('Generating prime p...'     )
 UpperCAmelCase	:     str						 =			rabin_miller.generate_large_prime(UpperCAmelCase_     )  # select large prime number.
 UpperCAmelCase	:     List[str]						 =			primitive_root(UpperCAmelCase_     )  # one primitive root on modulo p.
 UpperCAmelCase	:     List[Any]						 =			random.randrange(3  ,	UpperCAmelCase_     )  # private_key -> have to be greater than 2 for safety.
 UpperCAmelCase	:     List[Any]						 =			cryptomath.find_mod_inverse(pow(UpperCAmelCase_  ,	UpperCAmelCase_  ,	UpperCAmelCase_     )  ,	UpperCAmelCase_     )

 UpperCAmelCase	:     Tuple						 =			(key_size, e_a, e_a, p)
 UpperCAmelCase	:     Optional[int]						 =			(key_size, d)

 return public_key, private_key




def 				UpperCamelCase(					UpperCAmelCase_  ,	UpperCAmelCase_     ):
 if os.path.exists(F"""{name}_pubkey.txt"""     ) or os.path.exists(F"""{name}_privkey.txt"""     ):
  print('\nWARNING:'     )
  print(
      F"""\"{name}_pubkey.txt\" or \"{name}_privkey.txt\" already exists. \n"""
      'Use a different name or delete these files and re-run this program.'     )
  sys.exit()

 UpperCAmelCase ,			UpperCAmelCase	:     Dict						 =			generate_key(UpperCAmelCase_     )
 print(F"""\nWriting public key to file {name}_pubkey.txt..."""     )
 with open(F"""{name}_pubkey.txt"""  ,	'w'     ) as fo:
  fo.write(F"""{public_key[0]},{public_key[1]},{public_key[2]},{public_key[3]}"""     )

 print(F"""Writing private key to file {name}_privkey.txt..."""     )
 with open(F"""{name}_privkey.txt"""  ,	'w'     ) as fo:
  fo.write(F"""{private_key[0]},{private_key[1]}"""     )




def 				UpperCamelCase(					):
 print('Making key files...'     )
 make_key_files('elgamal'  ,	20_48     )
 print('Key files generation successful'     )


if __name__ == "__main__":
 main()




