






"""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 						a_ :







      '''simple docstring'''

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

       '''simple docstring'''



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




      def      a__				(self   ):

       '''simple docstring'''



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

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

       lowerCamelCase__	:     Union[str, Any]								=		None
       if self.use_token_type_ids:
        lowerCamelCase__	:     Optional[Any]								=		ids_tensor([self.batch_size, self.seq_length],	self.type_vocab_size   )

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

       lowerCamelCase__	:     Any								=		self.get_config()

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




      def      a__				(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=lowerCamelCase_,	initializer_range=self.initializer_range,	)




      def      a__				(self   ):

       '''simple docstring'''



       (
           (
           lowerCamelCase__
       )							,	(
           lowerCamelCase__
       )							,	(
           lowerCamelCase__
       )							,	(
           lowerCamelCase__
       )							,	(
           lowerCamelCase__
       )							,	(
           lowerCamelCase__
       )							,	(
           lowerCamelCase__
       )							,	(
           lowerCamelCase__
       )							,	
       )	:     List[Any]								=		self.prepare_config_and_inputs()

       lowerCamelCase__	:     Optional[Any]								=		True
       lowerCamelCase__	:     List[str]								=		floats_tensor([self.batch_size, self.seq_length, self.hidden_size]   )
       lowerCamelCase__	:     int								=		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      a__				(self,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	):

       '''simple docstring'''



       lowerCamelCase__	:     List[Any]								=		ElectraModel(config=lowerCamelCase_   )
       model.to(lowerCamelCase_   )
       model.eval()
       lowerCamelCase__	:     int								=		model(lowerCamelCase_,	attention_mask=lowerCamelCase_,	token_type_ids=lowerCamelCase_   )
       lowerCamelCase__	:     Optional[int]								=		model(lowerCamelCase_,	token_type_ids=lowerCamelCase_   )
       lowerCamelCase__	:     int								=		model(lowerCamelCase_   )
       self.parent.assertEqual(result.last_hidden_state.shape,	(self.batch_size, self.seq_length, self.hidden_size)   )




      def      a__				(self,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	):

       '''simple docstring'''



       lowerCamelCase__	:     Optional[Any]								=		True
       lowerCamelCase__	:     Dict								=		ElectraModel(lowerCamelCase_   )
       model.to(lowerCamelCase_   )
       model.eval()
       lowerCamelCase__	:     int								=		model(
           lowerCamelCase_,	attention_mask=lowerCamelCase_,	token_type_ids=lowerCamelCase_,	encoder_hidden_states=lowerCamelCase_,	encoder_attention_mask=lowerCamelCase_,	)
       lowerCamelCase__	:     Tuple								=		model(
           lowerCamelCase_,	attention_mask=lowerCamelCase_,	token_type_ids=lowerCamelCase_,	encoder_hidden_states=lowerCamelCase_,	)
       lowerCamelCase__	:     Tuple								=		model(lowerCamelCase_,	attention_mask=lowerCamelCase_,	token_type_ids=lowerCamelCase_   )
       self.parent.assertEqual(result.last_hidden_state.shape,	(self.batch_size, self.seq_length, self.hidden_size)   )




      def      a__				(self,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	):

       '''simple docstring'''



       lowerCamelCase__	:     List[str]								=		ElectraForMaskedLM(config=lowerCamelCase_   )
       model.to(lowerCamelCase_   )
       model.eval()
       lowerCamelCase__	:     Tuple								=		model(lowerCamelCase_,	attention_mask=lowerCamelCase_,	token_type_ids=lowerCamelCase_,	labels=lowerCamelCase_   )
       self.parent.assertEqual(result.logits.shape,	(self.batch_size, self.seq_length, self.vocab_size)   )




      def      a__				(self,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	):

       '''simple docstring'''



       lowerCamelCase__	:     str								=		ElectraForCausalLM(config=lowerCamelCase_   )
       model.to(lowerCamelCase_   )
       model.eval()
       lowerCamelCase__	:     Optional[int]								=		model(lowerCamelCase_,	attention_mask=lowerCamelCase_,	token_type_ids=lowerCamelCase_,	labels=lowerCamelCase_   )
       self.parent.assertEqual(result.logits.shape,	(self.batch_size, self.seq_length, self.vocab_size)   )




      def      a__				(self,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	):

       '''simple docstring'''



       lowerCamelCase__	:     int								=		self.num_labels
       lowerCamelCase__	:     Optional[int]								=		ElectraForTokenClassification(config=lowerCamelCase_   )
       model.to(lowerCamelCase_   )
       model.eval()
       lowerCamelCase__	:     Optional[int]								=		model(lowerCamelCase_,	attention_mask=lowerCamelCase_,	token_type_ids=lowerCamelCase_,	labels=lowerCamelCase_   )
       self.parent.assertEqual(result.logits.shape,	(self.batch_size, self.seq_length, self.num_labels)   )




      def      a__				(self,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	):

       '''simple docstring'''



       lowerCamelCase__	:     Any								=		self.num_labels
       lowerCamelCase__	:     List[Any]								=		ElectraForPreTraining(config=lowerCamelCase_   )
       model.to(lowerCamelCase_   )
       model.eval()
       lowerCamelCase__	:     int								=		model(lowerCamelCase_,	attention_mask=lowerCamelCase_,	token_type_ids=lowerCamelCase_,	labels=lowerCamelCase_   )
       self.parent.assertEqual(result.logits.shape,	(self.batch_size, self.seq_length)   )




      def      a__				(self,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	):

       '''simple docstring'''



       lowerCamelCase__	:     List[str]								=		self.num_labels
       lowerCamelCase__	:     List[Any]								=		ElectraForSequenceClassification(lowerCamelCase_   )
       model.to(lowerCamelCase_   )
       model.eval()
       lowerCamelCase__	:     Any								=		model(lowerCamelCase_,	attention_mask=lowerCamelCase_,	token_type_ids=lowerCamelCase_,	labels=lowerCamelCase_   )
       self.parent.assertEqual(result.logits.shape,	(self.batch_size, self.num_labels)   )




      def      a__				(self,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	):

       '''simple docstring'''



       lowerCamelCase__	:     Tuple								=		ElectraForQuestionAnswering(config=lowerCamelCase_   )
       model.to(lowerCamelCase_   )
       model.eval()
       lowerCamelCase__	:     str								=		model(
           lowerCamelCase_,	attention_mask=lowerCamelCase_,	token_type_ids=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      a__				(self,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_,	):

       '''simple docstring'''



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





      def      a__				(self   ):

       '''simple docstring'''



       lowerCamelCase__	:     int								=		self.prepare_config_and_inputs()
       (
           (
           lowerCamelCase__
       )							,	(
           lowerCamelCase__
       )							,	(
           lowerCamelCase__
       )							,	(
           lowerCamelCase__
       )							,	(
           lowerCamelCase__
       )							,	(
           lowerCamelCase__
       )							,	(
           lowerCamelCase__
       )							,	(
           lowerCamelCase__
       )							,	
       )	:     Optional[int]								=		config_and_inputs
       lowerCamelCase__	:     Union[str, Any]								=		{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
       return config, inputs_dict



@require_torch
class 						a_ (      snake_case_    ,		snake_case_    ,		unittest.TestCase					):







      '''simple docstring'''

      lowerCamelCase__			:							Optional[Any]					=      (
          (
              ElectraModel,
              ElectraForPreTraining,
              ElectraForMaskedLM,
              ElectraForCausalLM,
              ElectraForMultipleChoice,
              ElectraForTokenClassification,
              ElectraForSequenceClassification,
              ElectraForQuestionAnswering,
          )
          if is_torch_available()
          else ()
      )
      lowerCamelCase__			:							List[str]					=      (
          {
              '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      a__				(self,	lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_=False   ):

       '''simple docstring'''



       lowerCamelCase__	:     List[Any]								=		super()._prepare_for_class(lowerCamelCase_,	lowerCamelCase_,	return_labels=lowerCamelCase_   )

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




      def      a__				(self   ):

       '''simple docstring'''



       lowerCamelCase__	:     Dict								=		ElectraModelTester(self   )
       lowerCamelCase__	:     List[Any]								=		ConfigTester(self,	config_class=lowerCamelCase_,	hidden_size=3_7   )




      def      a__				(self   ):

       '''simple docstring'''



       self.config_tester.run_common_tests()




      def      a__				(self   ):

       '''simple docstring'''



       lowerCamelCase__	:     Any								=		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_model(*lowerCamelCase_   )




      def      a__				(self   ):

       '''simple docstring'''



       lowerCamelCase__	:     str								=		self.model_tester.prepare_config_and_inputs_for_decoder()
       self.model_tester.create_and_check_electra_model_as_decoder(*lowerCamelCase_   )




      def      a__				(self   ):

       '''simple docstring'''



       lowerCamelCase__	:     Dict								=		self.model_tester.prepare_config_and_inputs()
       for type in ["absolute", "relative_key", "relative_key_query"]:
        lowerCamelCase__	:     str								=		type
        self.model_tester.create_and_check_electra_model(*lowerCamelCase_   )




      def      a__				(self   ):

       '''simple docstring'''



       lowerCamelCase__	:     Dict								=		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_for_masked_lm(*lowerCamelCase_   )




      def      a__				(self   ):

       '''simple docstring'''



       lowerCamelCase__	:     Optional[Any]								=		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_for_token_classification(*lowerCamelCase_   )




      def      a__				(self   ):

       '''simple docstring'''



       lowerCamelCase__	:     Optional[int]								=		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_for_pretraining(*lowerCamelCase_   )




      def      a__				(self   ):

       '''simple docstring'''



       lowerCamelCase__	:     Dict								=		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_for_sequence_classification(*lowerCamelCase_   )




      def      a__				(self   ):

       '''simple docstring'''



       lowerCamelCase__	:     Optional[Any]								=		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_for_question_answering(*lowerCamelCase_   )




      def      a__				(self   ):

       '''simple docstring'''



       lowerCamelCase__	:     str								=		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_electra_for_multiple_choice(*lowerCamelCase_   )




      @slow
      def      a__				(self   ):

       '''simple docstring'''



       for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        lowerCamelCase__	:     Optional[int]								=		ElectraModel.from_pretrained(lowerCamelCase_   )
        self.assertIsNotNone(lowerCamelCase_   )





      def      a__				(self   ):

       '''simple docstring'''



       lowerCamelCase__	:     List[str]								=		self.model_tester.prepare_config_and_inputs_for_decoder()
       self.model_tester.create_and_check_electra_for_causal_lm(*lowerCamelCase_   )



@require_torch
class 						a_ (      unittest.TestCase					):







      '''simple docstring'''

      @slow
      def      a__				(self   ):

       '''simple docstring'''



       lowerCamelCase__	:     Any								=		ElectraModel.from_pretrained('google/electra-small-discriminator'   )
       lowerCamelCase__	:     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]]   )
       lowerCamelCase__	:     Dict								=		torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]   )
       lowerCamelCase__	:     Dict								=		model(lowerCamelCase_,	attention_mask=lowerCamelCase_   )[0]
       lowerCamelCase__	:     int								=		torch.Size((1, 1_1, 2_5_6)   )
       self.assertEqual(output.shape,	lowerCamelCase_   )
       lowerCamelCase__	:     List[Any]								=		torch.tensor(
           [[[0.4_471, 0.6_821, -0.3_265], [0.4_627, 0.5_255, -0.3_668], [0.4_532, 0.3_313, -0.4_344]]]   )

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









"""simple docstring"""
from transformers import HfArgumentParser, PyTorchBenchmark, PyTorchBenchmarkArguments






def 			lowerCamelCase_    (		):
 lowerCamelCase__	:     Tuple								=		HfArgumentParser(_lowerCamelCase							)
 try:
  lowerCamelCase__	:     Dict								=		parser.parse_args_into_dataclasses()[0]
 except ValueError as e:
  lowerCamelCase__	:     Optional[int]								=		'Arg --no_{0} is no longer used, please use --no-{0} instead.'
  lowerCamelCase__	:     Optional[Any]								=		' '.join(str(_lowerCamelCase							).split(' '							)[:-1]							)
  lowerCamelCase__	:     int								=		''
  lowerCamelCase__	:     Optional[int]								=		eval(str(_lowerCamelCase							).split(' '							)[-1]							)
  lowerCamelCase__	:     Tuple								=		[]
  for arg in depreciated_args:
   # arg[2:] removes '--'
   if arg[2:] in PyTorchBenchmarkArguments.deprecated_args:
    # arg[5:] removes '--no_'
    full_error_msg += arg_error_msg.format(arg[5:]							)
   else:
    wrong_args.append(_lowerCamelCase							)
  if len(_lowerCamelCase							) > 0:
   lowerCamelCase__	:     Optional[int]								=		full_error_msg + begin_error_msg + str(_lowerCamelCase							)
  raise ValueError(_lowerCamelCase							)

 lowerCamelCase__	:     Any								=		PyTorchBenchmark(args=_lowerCamelCase							)
 benchmark.run()


if __name__ == "__main__":
      main()

