'''simple docstring'''


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      __SCREAMING_SNAKE_CASE						:







  '''simple docstring'''






  def __init__( self      ,		snake_case_      ,		snake_case_=1_3      ,		snake_case_=7      ,		snake_case_=True      ,		snake_case_=True      ,		snake_case_=True      ,		snake_case_=True      ,		snake_case_=9_9      ,		snake_case_=3_2      ,		snake_case_=5      ,		snake_case_=4      ,		snake_case_=3_7      ,		snake_case_="gelu"      ,		snake_case_=0.1      ,		snake_case_=0.1      ,		snake_case_=5_1_2      ,		snake_case_=1_6      ,		snake_case_=2      ,		snake_case_=0.02      ,		snake_case_=3      ,		snake_case_=4      ,		snake_case_=None      ,		):




       '''simple docstring'''

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


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_							:   Union[str, Any]   =      ids_tensor([self.batch_size, self.seq_length]      ,		self.vocab_size						)

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

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

       UpperCAmelCase_							:   Union[str, Any]   =      None
       UpperCAmelCase_							:   List[str]   =      None
       UpperCAmelCase_							:   Optional[int]   =      None
       if self.use_labels:
            UpperCAmelCase_							:   Optional[int]   =      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   =      ids_tensor([self.batch_size]      ,		self.num_choices						)
            UpperCAmelCase_							:   str   =      ids_tensor([self.batch_size, self.seq_length]      ,		1						)

       UpperCAmelCase_							:   Any   =      self.get_config()

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


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       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=_SCREAMING_SNAKE_CASE      ,		initializer_range=self.initializer_range      ,		)


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       (
           UpperCAmelCase_				
       )							:   List[Any]   =      self.prepare_config_and_inputs()

       UpperCAmelCase_							:   int   =      True
       UpperCAmelCase_							:   int   =      floats_tensor([self.batch_size, self.seq_length, self.hidden_size]						)
       UpperCAmelCase_							:   Dict   =      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      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		):




       '''simple docstring'''

       UpperCAmelCase_							:   str   =      ElectraModel(config=_SCREAMING_SNAKE_CASE						)
       model.to(_SCREAMING_SNAKE_CASE						)
       model.eval()
       UpperCAmelCase_							:   Any   =      model(_SCREAMING_SNAKE_CASE      ,		attention_mask=_SCREAMING_SNAKE_CASE      ,		token_type_ids=_SCREAMING_SNAKE_CASE						)
       UpperCAmelCase_							:   int   =      model(_SCREAMING_SNAKE_CASE      ,		token_type_ids=_SCREAMING_SNAKE_CASE						)
       UpperCAmelCase_							:   str   =      model(_SCREAMING_SNAKE_CASE						)
       self.parent.assertEqual(result.last_hidden_state.shape      ,		(self.batch_size, self.seq_length, self.hidden_size)						)


  def _UpperCamelCase     ( self      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		):




       '''simple docstring'''

       UpperCAmelCase_							:   int   =      True
       UpperCAmelCase_							:   Union[str, Any]   =      ElectraModel(_SCREAMING_SNAKE_CASE						)
       model.to(_SCREAMING_SNAKE_CASE						)
       model.eval()
       UpperCAmelCase_							:   Union[str, Any]   =      model(
           _SCREAMING_SNAKE_CASE      ,		attention_mask=_SCREAMING_SNAKE_CASE      ,		token_type_ids=_SCREAMING_SNAKE_CASE      ,		encoder_hidden_states=_SCREAMING_SNAKE_CASE      ,		encoder_attention_mask=_SCREAMING_SNAKE_CASE      ,		)
       UpperCAmelCase_							:   Union[str, Any]   =      model(
           _SCREAMING_SNAKE_CASE      ,		attention_mask=_SCREAMING_SNAKE_CASE      ,		token_type_ids=_SCREAMING_SNAKE_CASE      ,		encoder_hidden_states=_SCREAMING_SNAKE_CASE      ,		)
       UpperCAmelCase_							:   str   =      model(_SCREAMING_SNAKE_CASE      ,		attention_mask=_SCREAMING_SNAKE_CASE      ,		token_type_ids=_SCREAMING_SNAKE_CASE						)
       self.parent.assertEqual(result.last_hidden_state.shape      ,		(self.batch_size, self.seq_length, self.hidden_size)						)


  def _UpperCamelCase     ( self      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		):




       '''simple docstring'''

       UpperCAmelCase_							:   List[str]   =      ElectraForMaskedLM(config=_SCREAMING_SNAKE_CASE						)
       model.to(_SCREAMING_SNAKE_CASE						)
       model.eval()
       UpperCAmelCase_							:   Optional[Any]   =      model(_SCREAMING_SNAKE_CASE      ,		attention_mask=_SCREAMING_SNAKE_CASE      ,		token_type_ids=_SCREAMING_SNAKE_CASE      ,		labels=_SCREAMING_SNAKE_CASE						)
       self.parent.assertEqual(result.logits.shape      ,		(self.batch_size, self.seq_length, self.vocab_size)						)


  def _UpperCamelCase     ( self      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		):




       '''simple docstring'''

       UpperCAmelCase_							:   Union[str, Any]   =      ElectraForCausalLM(config=_SCREAMING_SNAKE_CASE						)
       model.to(_SCREAMING_SNAKE_CASE						)
       model.eval()
       UpperCAmelCase_							:   Optional[Any]   =      model(_SCREAMING_SNAKE_CASE      ,		attention_mask=_SCREAMING_SNAKE_CASE      ,		token_type_ids=_SCREAMING_SNAKE_CASE      ,		labels=_SCREAMING_SNAKE_CASE						)
       self.parent.assertEqual(result.logits.shape      ,		(self.batch_size, self.seq_length, self.vocab_size)						)


  def _UpperCamelCase     ( self      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		):




       '''simple docstring'''

       UpperCAmelCase_							:   str   =      self.num_labels
       UpperCAmelCase_							:   List[Any]   =      ElectraForTokenClassification(config=_SCREAMING_SNAKE_CASE						)
       model.to(_SCREAMING_SNAKE_CASE						)
       model.eval()
       UpperCAmelCase_							:   Optional[int]   =      model(_SCREAMING_SNAKE_CASE      ,		attention_mask=_SCREAMING_SNAKE_CASE      ,		token_type_ids=_SCREAMING_SNAKE_CASE      ,		labels=_SCREAMING_SNAKE_CASE						)
       self.parent.assertEqual(result.logits.shape      ,		(self.batch_size, self.seq_length, self.num_labels)						)


  def _UpperCamelCase     ( self      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		):




       '''simple docstring'''

       UpperCAmelCase_							:   Tuple   =      self.num_labels
       UpperCAmelCase_							:   int   =      ElectraForPreTraining(config=_SCREAMING_SNAKE_CASE						)
       model.to(_SCREAMING_SNAKE_CASE						)
       model.eval()
       UpperCAmelCase_							:   Dict   =      model(_SCREAMING_SNAKE_CASE      ,		attention_mask=_SCREAMING_SNAKE_CASE      ,		token_type_ids=_SCREAMING_SNAKE_CASE      ,		labels=_SCREAMING_SNAKE_CASE						)
       self.parent.assertEqual(result.logits.shape      ,		(self.batch_size, self.seq_length)						)


  def _UpperCamelCase     ( self      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		):




       '''simple docstring'''

       UpperCAmelCase_							:   Dict   =      self.num_labels
       UpperCAmelCase_							:   List[str]   =      ElectraForSequenceClassification(_SCREAMING_SNAKE_CASE						)
       model.to(_SCREAMING_SNAKE_CASE						)
       model.eval()
       UpperCAmelCase_							:   int   =      model(_SCREAMING_SNAKE_CASE      ,		attention_mask=_SCREAMING_SNAKE_CASE      ,		token_type_ids=_SCREAMING_SNAKE_CASE      ,		labels=_SCREAMING_SNAKE_CASE						)
       self.parent.assertEqual(result.logits.shape      ,		(self.batch_size, self.num_labels)						)


  def _UpperCamelCase     ( self      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		):




       '''simple docstring'''

       UpperCAmelCase_							:   List[str]   =      ElectraForQuestionAnswering(config=_SCREAMING_SNAKE_CASE						)
       model.to(_SCREAMING_SNAKE_CASE						)
       model.eval()
       UpperCAmelCase_							:   Optional[int]   =      model(
           _SCREAMING_SNAKE_CASE      ,		attention_mask=_SCREAMING_SNAKE_CASE      ,		token_type_ids=_SCREAMING_SNAKE_CASE      ,		start_positions=_SCREAMING_SNAKE_CASE      ,		end_positions=_SCREAMING_SNAKE_CASE      ,		)
       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      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		snake_case_      ,		):




       '''simple docstring'''

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






  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_							:   Optional[int]   =      self.prepare_config_and_inputs()
       (
           UpperCAmelCase_				
       )							:   Any   =      config_and_inputs
       UpperCAmelCase_							:   str   =      {"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'''






  lowerCamelCase_		:Dict						    =		(
      (
          ElectraModel,
          ElectraForPreTraining,
          ElectraForMaskedLM,
          ElectraForCausalLM,
          ElectraForMultipleChoice,
          ElectraForTokenClassification,
          ElectraForSequenceClassification,
          ElectraForQuestionAnswering,
      )
      if is_torch_available()
      else ()
  )
  lowerCamelCase_		:Tuple						    =		(
      {
          '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 {}
  )
  lowerCamelCase_		:Any						    =		True


  def _UpperCamelCase     ( self      ,		snake_case_      ,		snake_case_      ,		snake_case_=False						):




       '''simple docstring'''

       UpperCAmelCase_							:   int   =      super()._prepare_for_class(_SCREAMING_SNAKE_CASE      ,		_SCREAMING_SNAKE_CASE      ,		return_labels=_SCREAMING_SNAKE_CASE						)

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


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_							:   List[str]   =      ElectraModelTester(self						)
       UpperCAmelCase_							:   Union[str, Any]   =      ConfigTester(self      ,		config_class=_SCREAMING_SNAKE_CASE      ,		hidden_size=3_7						)


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       self.config_tester.run_common_tests()


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_							:   Dict   =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_model(*_SCREAMING_SNAKE_CASE						)


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_							:   Optional[int]   =      self.model_tester.prepare_config_and_inputs_for_decoder()
       self.model_tester.create_and_check_electra_model_as_decoder(*_SCREAMING_SNAKE_CASE						)


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_							:   str   =      self.model_tester.prepare_config_and_inputs()
       for type in ["absolute", "relative_key", "relative_key_query"]:
            UpperCAmelCase_							:   Union[str, Any]   =      type
            self.model_tester.create_and_check_electra_model(*_SCREAMING_SNAKE_CASE						)


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_							:   Optional[Any]   =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_for_masked_lm(*_SCREAMING_SNAKE_CASE						)


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_							:   Optional[int]   =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_for_token_classification(*_SCREAMING_SNAKE_CASE						)


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_							:   str   =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_for_pretraining(*_SCREAMING_SNAKE_CASE						)


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_							:   int   =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_for_sequence_classification(*_SCREAMING_SNAKE_CASE						)


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_							:   Optional[int]   =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_for_question_answering(*_SCREAMING_SNAKE_CASE						)


  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_							:   List[str]   =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_for_multiple_choice(*_SCREAMING_SNAKE_CASE						)


  @slow
  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            UpperCAmelCase_							:   Any   =      ElectraModel.from_pretrained(_SCREAMING_SNAKE_CASE						)
            self.assertIsNotNone(_SCREAMING_SNAKE_CASE						)






  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_							:   Optional[int]   =      self.model_tester.prepare_config_and_inputs_for_decoder()
       self.model_tester.create_and_check_electra_for_causal_lm(*_SCREAMING_SNAKE_CASE						)







@require_torch
class      __SCREAMING_SNAKE_CASE						(       unittest.TestCase       ):







  '''simple docstring'''






  @slow
  def _UpperCamelCase     ( self						):




       '''simple docstring'''

       UpperCAmelCase_							:   Optional[Any]   =      ElectraModel.from_pretrained('google/electra-small-discriminator'						)
       UpperCAmelCase_							:   Optional[Any]   =      torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]]						)
       UpperCAmelCase_							:   int   =      torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]						)
       UpperCAmelCase_							:   Any   =      model(_SCREAMING_SNAKE_CASE      ,		attention_mask=_SCREAMING_SNAKE_CASE						)[0]
       UpperCAmelCase_							:   str   =      torch.Size((1, 1_1, 2_5_6)						)
       self.assertEqual(output.shape      ,		_SCREAMING_SNAKE_CASE						)
       UpperCAmelCase_							:   Dict   =      torch.tensor(
           [[[0.44_71, 0.68_21, -0.32_65], [0.46_27, 0.52_55, -0.36_68], [0.45_32, 0.33_13, -0.43_44]]]						)

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














def    lowerCAmelCase__		(			_a				:	str     ,					_a				:	int = 13			):
		snake_case_ :							List[Any]         =				""
		for c in s:
				if "A" <= c <= "Z":
						out += chr(ord("A"			) + (ord(_a			) - ord("A"			) + n) % 26			)
				elif "a" <= c <= "z":
						out += chr(ord("a"			) + (ord(_a			) - ord("a"			) + n) % 26			)
				else:
						out += c
		return out

def    lowerCAmelCase__		(			):
		snake_case_ :							int         =				input("Enter message: "			)

		snake_case_ :							List[Any]         =				dencrypt(_a     ,					13			)
		print("Encryption:"     ,					_a			)

		snake_case_ :							int         =				dencrypt(_a     ,					13			)
		print("Decryption: "     ,					_a			)


if __name__ == "__main__":
							import doctest

							doctest.testmod()
							main()
