

import unittest

from transformers import ElectraConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
   import torch

   from transformers import (
       MODEL_FOR_PRETRAINING_MAPPING,
       ElectraForCausalLM,
       ElectraForMaskedLM,
       ElectraForMultipleChoice,
       ElectraForPreTraining,
       ElectraForQuestionAnswering,
       ElectraForSequenceClassification,
       ElectraForTokenClassification,
       ElectraModel,
   )
   from transformers.models.electra.modeling_electra import ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST



class 						lowercase_					:



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



      def 		UpperCamelCase_	(    self   :       str	)					->  Dict:
        _snake_case		=							ids_tensor([self.batch_size, self.seq_length]    ,      self.vocab_size	)

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

        _snake_case		=							None
        if self.use_token_type_ids:
          _snake_case		=							ids_tensor([self.batch_size, self.seq_length]    ,      self.type_vocab_size	)

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

        _snake_case		=							self.get_config()

        return (
            config,
            input_ids,
            token_type_ids,
            input_mask,
            sequence_labels,
            token_labels,
            choice_labels,
            fake_token_labels,
        )



      def 		UpperCamelCase_	(    self   :       Optional[Any]	)					->  List[Any]:
        return ElectraConfig(
            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    ,      is_decoder=A__    ,      initializer_range=self.initializer_range    ,      )



      def 		UpperCamelCase_	(    self   :       List[str]	)					->  Dict:
        (
            (
            _snake_case  
        ),  (
            _snake_case  
        ),  (
            _snake_case  
        ),  (
            _snake_case  
        ),  (
            _snake_case  
        ),  (
            _snake_case  
        ),  (
            _snake_case  
        ),  (
            _snake_case  
        ),    
        )		=							self.prepare_config_and_inputs()

        _snake_case		=							True
        _snake_case		=							floats_tensor([self.batch_size, self.seq_length, self.hidden_size]	)
        _snake_case		=							ids_tensor([self.batch_size, self.seq_length]    ,      vocab_size=2	)

        return (
            config,
            input_ids,
            token_type_ids,
            input_mask,
            sequence_labels,
            token_labels,
            choice_labels,
            encoder_hidden_states,
            encoder_attention_mask,
        )



      def 		UpperCamelCase_	(    self   :       List[str]    ,      A__   :       Dict    ,      A__   :       Tuple    ,      A__   :       str    ,      A__   :       Optional[Any]    ,      A__   :       int    ,      A__   :       Optional[int]    ,      A__   :       Optional[int]    ,      A__   :       str    ,      )					->  List[Any]:
        _snake_case		=							ElectraModel(config=A__	)
        model.to(A__	)
        model.eval()
        _snake_case		=							model(A__    ,      attention_mask=A__    ,      token_type_ids=A__	)
        _snake_case		=							model(A__    ,      token_type_ids=A__	)
        _snake_case		=							model(A__	)
        self.parent.assertEqual(result.last_hidden_state.shape    ,      (self.batch_size, self.seq_length, self.hidden_size)	)



      def 		UpperCamelCase_	(    self   :       List[str]    ,      A__   :       List[str]    ,      A__   :       int    ,      A__   :       Dict    ,      A__   :       str    ,      A__   :       Optional[Any]    ,      A__   :       Union[str, Any]    ,      A__   :       Optional[Any]    ,      A__   :       Union[str, Any]    ,      A__   :       Optional[int]    ,      )					->  List[Any]:
        _snake_case		=							True
        _snake_case		=							ElectraModel(A__	)
        model.to(A__	)
        model.eval()
        _snake_case		=							model(
            A__    ,      attention_mask=A__    ,      token_type_ids=A__    ,      encoder_hidden_states=A__    ,      encoder_attention_mask=A__    ,      )
        _snake_case		=							model(
            A__    ,      attention_mask=A__    ,      token_type_ids=A__    ,      encoder_hidden_states=A__    ,      )
        _snake_case		=							model(A__    ,      attention_mask=A__    ,      token_type_ids=A__	)
        self.parent.assertEqual(result.last_hidden_state.shape    ,      (self.batch_size, self.seq_length, self.hidden_size)	)



      def 		UpperCamelCase_	(    self   :       Optional[int]    ,      A__   :       Dict    ,      A__   :       str    ,      A__   :       Tuple    ,      A__   :       Tuple    ,      A__   :       Optional[Any]    ,      A__   :       int    ,      A__   :       List[Any]    ,      A__   :       Union[str, Any]    ,      )					->  Optional[Any]:
        _snake_case		=							ElectraForMaskedLM(config=A__	)
        model.to(A__	)
        model.eval()
        _snake_case		=							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 		UpperCamelCase_	(    self   :       int    ,      A__   :       Optional[int]    ,      A__   :       List[str]    ,      A__   :       Tuple    ,      A__   :       List[str]    ,      A__   :       Tuple    ,      A__   :       int    ,      A__   :       str    ,      A__   :       Any    ,      A__   :       Tuple    ,      )					->  Optional[int]:
        _snake_case		=							ElectraForCausalLM(config=A__	)
        model.to(A__	)
        model.eval()
        _snake_case		=							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 		UpperCamelCase_	(    self   :       Union[str, Any]    ,      A__   :       Optional[Any]    ,      A__   :       Tuple    ,      A__   :       Dict    ,      A__   :       Any    ,      A__   :       List[str]    ,      A__   :       int    ,      A__   :       List[str]    ,      A__   :       List[Any]    ,      )					->  Optional[int]:
        _snake_case		=							self.num_labels
        _snake_case		=							ElectraForTokenClassification(config=A__	)
        model.to(A__	)
        model.eval()
        _snake_case		=							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 		UpperCamelCase_	(    self   :       Tuple    ,      A__   :       Tuple    ,      A__   :       List[str]    ,      A__   :       List[str]    ,      A__   :       List[Any]    ,      A__   :       List[str]    ,      A__   :       Optional[Any]    ,      A__   :       Tuple    ,      A__   :       Tuple    ,      )					->  Any:
        _snake_case		=							self.num_labels
        _snake_case		=							ElectraForPreTraining(config=A__	)
        model.to(A__	)
        model.eval()
        _snake_case		=							model(A__    ,      attention_mask=A__    ,      token_type_ids=A__    ,      labels=A__	)
        self.parent.assertEqual(result.logits.shape    ,      (self.batch_size, self.seq_length)	)



      def 		UpperCamelCase_	(    self   :       str    ,      A__   :       int    ,      A__   :       Any    ,      A__   :       List[str]    ,      A__   :       Union[str, Any]    ,      A__   :       Union[str, Any]    ,      A__   :       List[str]    ,      A__   :       List[Any]    ,      A__   :       Optional[int]    ,      )					->  Tuple:
        _snake_case		=							self.num_labels
        _snake_case		=							ElectraForSequenceClassification(A__	)
        model.to(A__	)
        model.eval()
        _snake_case		=							model(A__    ,      attention_mask=A__    ,      token_type_ids=A__    ,      labels=A__	)
        self.parent.assertEqual(result.logits.shape    ,      (self.batch_size, self.num_labels)	)



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







      def 		UpperCamelCase_	(    self   :       Dict	)					->  List[str]:
        _snake_case		=							self.prepare_config_and_inputs()
        (
            (
            _snake_case  
        ),  (
            _snake_case  
        ),  (
            _snake_case  
        ),  (
            _snake_case  
        ),  (
            _snake_case  
        ),  (
            _snake_case  
        ),  (
            _snake_case  
        ),  (
            _snake_case  
        ),    
        )		=							config_and_inputs
        _snake_case		=							{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
        return config, inputs_dict



@require_torch
class 						lowercase_					(						__lowercase     ,							__lowercase     ,							unittest.TestCase  ):
      UpperCamelCase_    :	Dict		   =       (
          (
              ElectraModel,
              ElectraForPreTraining,
              ElectraForMaskedLM,
              ElectraForCausalLM,
              ElectraForMultipleChoice,
              ElectraForTokenClassification,
              ElectraForSequenceClassification,
              ElectraForQuestionAnswering,
          )
          if is_torch_available()
          else ()
      )
      UpperCamelCase_    :	Optional[int]		   =       (
          {
              "feature-extraction": ElectraModel,
              "fill-mask": ElectraForMaskedLM,
              "question-answering": ElectraForQuestionAnswering,
              "text-classification": ElectraForSequenceClassification,
              "text-generation": ElectraForCausalLM,
              "token-classification": ElectraForTokenClassification,
              "zero-shot": ElectraForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      UpperCamelCase_    :	Optional[int]		   =       True



      def 		UpperCamelCase_	(    self   :       Any    ,      A__   :       Dict    ,      A__   :       List[Any]    ,      A__   :       str=False	)					->  Dict:
        _snake_case		=							super()._prepare_for_class(A__    ,      A__    ,      return_labels=A__	)

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



      def 		UpperCamelCase_	(    self   :       List[str]	)					->  Union[str, Any]:
        _snake_case		=							ElectraModelTester(self	)
        _snake_case		=							ConfigTester(self    ,      config_class=A__    ,      hidden_size=37	)



      def 		UpperCamelCase_	(    self   :       int	)					->  Dict:
        self.config_tester.run_common_tests()



      def 		UpperCamelCase_	(    self   :       List[str]	)					->  Dict:
        _snake_case		=							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_model(*A__	)



      def 		UpperCamelCase_	(    self   :       List[str]	)					->  Any:
        _snake_case		=							self.model_tester.prepare_config_and_inputs_for_decoder()
        self.model_tester.create_and_check_electra_model_as_decoder(*A__	)



      def 		UpperCamelCase_	(    self   :       Dict	)					->  int:
        _snake_case		=							self.model_tester.prepare_config_and_inputs()
        for type in ["absolute", "relative_key", "relative_key_query"]:
          _snake_case		=							type
          self.model_tester.create_and_check_electra_model(*A__	)



      def 		UpperCamelCase_	(    self   :       List[Any]	)					->  str:
        _snake_case		=							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_for_masked_lm(*A__	)



      def 		UpperCamelCase_	(    self   :       List[str]	)					->  Any:
        _snake_case		=							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_for_token_classification(*A__	)



      def 		UpperCamelCase_	(    self   :       Tuple	)					->  Tuple:
        _snake_case		=							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_for_pretraining(*A__	)



      def 		UpperCamelCase_	(    self   :       List[Any]	)					->  Optional[int]:
        _snake_case		=							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_for_sequence_classification(*A__	)



      def 		UpperCamelCase_	(    self   :       Optional[int]	)					->  Optional[int]:
        _snake_case		=							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_for_question_answering(*A__	)



      def 		UpperCamelCase_	(    self   :       Any	)					->  Tuple:
        _snake_case		=							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_for_multiple_choice(*A__	)



      @slow
      def 		UpperCamelCase_	(    self   :       Tuple	)					->  int:
        for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          _snake_case		=							ElectraModel.from_pretrained(A__	)
          self.assertIsNotNone(A__	)







      def 		UpperCamelCase_	(    self   :       Tuple	)					->  Dict:
        _snake_case		=							self.model_tester.prepare_config_and_inputs_for_decoder()
        self.model_tester.create_and_check_electra_for_causal_lm(*A__	)



@require_torch
class 						lowercase_					(						unittest.TestCase  ):



      @slow
      def 		UpperCamelCase_	(    self   :       Union[str, Any]	)					->  str:
        _snake_case		=							ElectraModel.from_pretrained('''google/electra-small-discriminator'''	)
        _snake_case		=							torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]	)
        _snake_case		=							torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]	)
        _snake_case		=							model(A__    ,      attention_mask=A__	)[0]
        _snake_case		=							torch.Size((1, 11, 256)	)
        self.assertEqual(output.shape    ,      A__	)
        _snake_case		=							torch.tensor(
            [[[0.4471, 0.6821, -0.3265], [0.4627, 0.5255, -0.3668], [0.4532, 0.3313, -0.4344]]]	)

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




import random
import sys

from . import cryptomath_module as cryptomath

__A    	=			(
    r''' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`'''
    r'''abcdefghijklmnopqrstuvwxyz{|}~'''
)



def    snake_case_(_UpperCamelCase				, _UpperCamelCase				, _UpperCamelCase  )							->			None:



  """simple docstring"""






  if mode == "encrypt":
    if key_a == 1:
      sys.exit(
          '''The affine cipher becomes weak when key '''
          '''A is set to 1. Choose different key'''  )
    if key_b == 0:
      sys.exit(
          '''The affine cipher becomes weak when key '''
          '''B is set to 0. Choose different key'''  )
  if key_a < 0 or key_b < 0 or key_b > len(_UpperCamelCase  ) - 1:
    sys.exit(
        '''Key A must be greater than 0 and key B must '''
        F"""be between 0 and {len(_UpperCamelCase  ) - 1}."""  )
  if cryptomath.gcd(_UpperCamelCase				, len(_UpperCamelCase  )  ) != 1:
    sys.exit(
        F"""Key A {key_a} and the symbol set size {len(_UpperCamelCase  )} """
        '''are not relatively prime. Choose a different key.'''  )



def    snake_case_(_UpperCamelCase				, _UpperCamelCase  )							->			str:



  """simple docstring"""






  _snake_case,  _snake_case		=							divmod(_UpperCamelCase				, len(_UpperCamelCase  )  )
  check_keys(_UpperCamelCase				, _UpperCamelCase				, '''encrypt'''  )
  _snake_case		=							''''''
  for symbol in message:
    if symbol in SYMBOLS:
      _snake_case		=							SYMBOLS.find(_UpperCamelCase  )
      cipher_text += SYMBOLS[(sym_index * key_a + key_b) % len(_UpperCamelCase  )]
    else:
      cipher_text += symbol
  return cipher_text



def    snake_case_(_UpperCamelCase				, _UpperCamelCase  )							->			str:



  """simple docstring"""






  _snake_case,  _snake_case		=							divmod(_UpperCamelCase				, len(_UpperCamelCase  )  )
  check_keys(_UpperCamelCase				, _UpperCamelCase				, '''decrypt'''  )
  _snake_case		=							''''''
  _snake_case		=							cryptomath.find_mod_inverse(_UpperCamelCase				, len(_UpperCamelCase  )  )
  for symbol in message:
    if symbol in SYMBOLS:
      _snake_case		=							SYMBOLS.find(_UpperCamelCase  )
      plain_text += SYMBOLS[
          (sym_index - key_b) * mod_inverse_of_key_a % len(_UpperCamelCase  )
      ]
    else:
      plain_text += symbol
  return plain_text



def    snake_case_()							->			int:



  """simple docstring"""






  while True:
    _snake_case		=							random.randint(2				, len(_UpperCamelCase  )  )
    _snake_case		=							random.randint(2				, len(_UpperCamelCase  )  )
    if cryptomath.gcd(_UpperCamelCase				, len(_UpperCamelCase  )  ) == 1 and key_b % len(_UpperCamelCase  ) != 0:
      return key_b * len(_UpperCamelCase  ) + key_b



def    snake_case_()							->			None:



  """simple docstring"""






  _snake_case		=							input('''Enter message: '''  ).strip()
  _snake_case		=							int(input('''Enter key [2000 - 9000]: '''  ).strip()  )
  _snake_case		=							input('''Encrypt/Decrypt [E/D]: '''  ).strip().lower()

  if mode.startswith('''e'''  ):
    _snake_case		=							'''encrypt'''
    _snake_case		=							encrypt_message(_UpperCamelCase				, _UpperCamelCase  )
  elif mode.startswith('''d'''  ):
    _snake_case		=							'''decrypt'''
    _snake_case		=							decrypt_message(_UpperCamelCase				, _UpperCamelCase  )
  print(F"""\n{mode.title()}ed text: \n{translated}"""  )


if __name__ == "__main__":
 import doctest

 doctest.testmod()
 # main()

