


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





  """simple docstring"""




  def __init__(   self      ,  UpperCamelCase      ,  UpperCamelCase=13      ,  UpperCamelCase=7      ,  UpperCamelCase=True      ,  UpperCamelCase=True      ,  UpperCamelCase=True      ,  UpperCamelCase=True      ,  UpperCamelCase=99      ,  UpperCamelCase=32      ,  UpperCamelCase=5      ,  UpperCamelCase=4      ,  UpperCamelCase=37      ,  UpperCamelCase="gelu"      ,  UpperCamelCase=0.1      ,  UpperCamelCase=0.1      ,  UpperCamelCase=512      ,  UpperCamelCase=16      ,  UpperCamelCase=2      ,  UpperCamelCase=0.02      ,  UpperCamelCase=3      ,  UpperCamelCase=4      ,  UpperCamelCase=None      ,  ):







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






  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  ids_tensor([self.batch_size, self.seq_length]      ,  self.vocab_size					)

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

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

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

   lowerCamelCase_								=  self.get_config()

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






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






  def   snake_case		(   self					):







   """simple docstring"""
   (
       lowerCamelCase_		
   )								=  self.prepare_config_and_inputs()

   lowerCamelCase_								=  True
   lowerCamelCase_								=  floats_tensor([self.batch_size, self.seq_length, self.hidden_size]					)
   lowerCamelCase_								=  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   snake_case		(   self      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  ):







   """simple docstring"""
   lowerCamelCase_								=  ElectraModel(config=__snake_case					)
   model.to(__snake_case					)
   model.eval()
   lowerCamelCase_								=  model(__snake_case      ,  attention_mask=__snake_case      ,  token_type_ids=__snake_case					)
   lowerCamelCase_								=  model(__snake_case      ,  token_type_ids=__snake_case					)
   lowerCamelCase_								=  model(__snake_case					)
   self.parent.assertEqual(result.last_hidden_state.shape      ,  (self.batch_size, self.seq_length, self.hidden_size)					)






  def   snake_case		(   self      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  ):







   """simple docstring"""
   lowerCamelCase_								=  True
   lowerCamelCase_								=  ElectraModel(__snake_case					)
   model.to(__snake_case					)
   model.eval()
   lowerCamelCase_								=  model(
       __snake_case      ,  attention_mask=__snake_case      ,  token_type_ids=__snake_case      ,  encoder_hidden_states=__snake_case      ,  encoder_attention_mask=__snake_case      ,  )
   lowerCamelCase_								=  model(
       __snake_case      ,  attention_mask=__snake_case      ,  token_type_ids=__snake_case      ,  encoder_hidden_states=__snake_case      ,  )
   lowerCamelCase_								=  model(__snake_case      ,  attention_mask=__snake_case      ,  token_type_ids=__snake_case					)
   self.parent.assertEqual(result.last_hidden_state.shape      ,  (self.batch_size, self.seq_length, self.hidden_size)					)






  def   snake_case		(   self      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  ):







   """simple docstring"""
   lowerCamelCase_								=  ElectraForMaskedLM(config=__snake_case					)
   model.to(__snake_case					)
   model.eval()
   lowerCamelCase_								=  model(__snake_case      ,  attention_mask=__snake_case      ,  token_type_ids=__snake_case      ,  labels=__snake_case					)
   self.parent.assertEqual(result.logits.shape      ,  (self.batch_size, self.seq_length, self.vocab_size)					)






  def   snake_case		(   self      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  ):







   """simple docstring"""
   lowerCamelCase_								=  ElectraForCausalLM(config=__snake_case					)
   model.to(__snake_case					)
   model.eval()
   lowerCamelCase_								=  model(__snake_case      ,  attention_mask=__snake_case      ,  token_type_ids=__snake_case      ,  labels=__snake_case					)
   self.parent.assertEqual(result.logits.shape      ,  (self.batch_size, self.seq_length, self.vocab_size)					)






  def   snake_case		(   self      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  ):







   """simple docstring"""
   lowerCamelCase_								=  self.num_labels
   lowerCamelCase_								=  ElectraForTokenClassification(config=__snake_case					)
   model.to(__snake_case					)
   model.eval()
   lowerCamelCase_								=  model(__snake_case      ,  attention_mask=__snake_case      ,  token_type_ids=__snake_case      ,  labels=__snake_case					)
   self.parent.assertEqual(result.logits.shape      ,  (self.batch_size, self.seq_length, self.num_labels)					)






  def   snake_case		(   self      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  ):







   """simple docstring"""
   lowerCamelCase_								=  self.num_labels
   lowerCamelCase_								=  ElectraForPreTraining(config=__snake_case					)
   model.to(__snake_case					)
   model.eval()
   lowerCamelCase_								=  model(__snake_case      ,  attention_mask=__snake_case      ,  token_type_ids=__snake_case      ,  labels=__snake_case					)
   self.parent.assertEqual(result.logits.shape      ,  (self.batch_size, self.seq_length)					)






  def   snake_case		(   self      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  ):







   """simple docstring"""
   lowerCamelCase_								=  self.num_labels
   lowerCamelCase_								=  ElectraForSequenceClassification(__snake_case					)
   model.to(__snake_case					)
   model.eval()
   lowerCamelCase_								=  model(__snake_case      ,  attention_mask=__snake_case      ,  token_type_ids=__snake_case      ,  labels=__snake_case					)
   self.parent.assertEqual(result.logits.shape      ,  (self.batch_size, self.num_labels)					)






  def   snake_case		(   self      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  ):







   """simple docstring"""
   lowerCamelCase_								=  ElectraForQuestionAnswering(config=__snake_case					)
   model.to(__snake_case					)
   model.eval()
   lowerCamelCase_								=  model(
       __snake_case      ,  attention_mask=__snake_case      ,  token_type_ids=__snake_case      ,  start_positions=__snake_case      ,  end_positions=__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   snake_case		(   self      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  ):







   """simple docstring"""
   lowerCamelCase_								=  self.num_choices
   lowerCamelCase_								=  ElectraForMultipleChoice(config=__snake_case					)
   model.to(__snake_case					)
   model.eval()
   lowerCamelCase_								=  input_ids.unsqueeze(1					).expand(-1      ,  self.num_choices      ,  -1					).contiguous()
   lowerCamelCase_								=  token_type_ids.unsqueeze(1					).expand(-1      ,  self.num_choices      ,  -1					).contiguous()
   lowerCamelCase_								=  input_mask.unsqueeze(1					).expand(-1      ,  self.num_choices      ,  -1					).contiguous()
   lowerCamelCase_								=  model(
       __snake_case      ,  attention_mask=__snake_case      ,  token_type_ids=__snake_case      ,  labels=__snake_case      ,  )
   self.parent.assertEqual(result.logits.shape      ,  (self.batch_size, self.num_choices)					)







  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  self.prepare_config_and_inputs()
   (
       lowerCamelCase_		
   )								=  config_and_inputs
   lowerCamelCase_								=  {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
   return config, inputs_dict





@require_torch
class 					snake_case   (   A__      ,						A__      ,						unittest.TestCase      ):





  """simple docstring"""




  _lowerCamelCase		   =  (
      (
          ElectraModel,
          ElectraForPreTraining,
          ElectraForMaskedLM,
          ElectraForCausalLM,
          ElectraForMultipleChoice,
          ElectraForTokenClassification,
          ElectraForSequenceClassification,
          ElectraForQuestionAnswering,
      )
      if is_torch_available()
      else ()
  )
  _lowerCamelCase		   =  (
      {
          "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		   =  True






  def   snake_case		(   self      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase=False					):







   """simple docstring"""
   lowerCamelCase_								=  super()._prepare_for_class(__snake_case      ,  __snake_case      ,  return_labels=__snake_case					)

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






  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  ElectraModelTester(self					)
   lowerCamelCase_								=  ConfigTester(self      ,  config_class=__snake_case      ,  hidden_size=37					)






  def   snake_case		(   self					):







   """simple docstring"""
   self.config_tester.run_common_tests()






  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_electra_model(*__snake_case					)






  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  self.model_tester.prepare_config_and_inputs_for_decoder()
   self.model_tester.create_and_check_electra_model_as_decoder(*__snake_case					)






  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  self.model_tester.prepare_config_and_inputs()
   for type in ["absolute", "relative_key", "relative_key_query"]:
    lowerCamelCase_								=  type
    self.model_tester.create_and_check_electra_model(*__snake_case					)






  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_electra_for_masked_lm(*__snake_case					)






  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_electra_for_token_classification(*__snake_case					)






  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_electra_for_pretraining(*__snake_case					)






  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_electra_for_sequence_classification(*__snake_case					)






  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_electra_for_question_answering(*__snake_case					)






  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_electra_for_multiple_choice(*__snake_case					)






  @slow
  def   snake_case		(   self					):







   """simple docstring"""
   for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
    lowerCamelCase_								=  ElectraModel.from_pretrained(__snake_case					)
    self.assertIsNotNone(__snake_case					)







  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  self.model_tester.prepare_config_and_inputs_for_decoder()
   self.model_tester.create_and_check_electra_for_causal_lm(*__snake_case					)





@require_torch
class 					snake_case   (   unittest.TestCase      ):





  """simple docstring"""




  @slow
  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  ElectraModel.from_pretrained("google/electra-small-discriminator"					)
   lowerCamelCase_								=  torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]					)
   lowerCamelCase_								=  torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]					)
   lowerCamelCase_								=  model(__snake_case      ,  attention_mask=__snake_case					)[0]
   lowerCamelCase_								=  torch.Size((1, 11, 256)					)
   self.assertEqual(output.shape      ,  __snake_case					)
   lowerCamelCase_								=  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]      ,  __snake_case      ,  atol=1e-4					)					)







'''simple docstring'''

import argparse
import json
import re
import string
import sys
from collections import Counter





def __snake_case (				UpperCAmelCase_						:						str		):

 def remove_articles(UpperCAmelCase_						:						Optional[Any]		):
  return re.sub(r"\b(a|an|the)\b" ,    " " ,    UpperCAmelCase_		)

 def white_space_fix(UpperCAmelCase_						:						List[str]		):
  return " ".join(text.split()		)

 def remove_punc(UpperCAmelCase_						:						Any		):
  lowerCamelCase_								=  set(string.punctuation		)
  return "".join(ch for ch in text if ch not in exclude		)

 def lower(UpperCAmelCase_						:						List[str]		):
  return text.lower()

 return white_space_fix(remove_articles(remove_punc(lower(UpperCAmelCase_		)		)		)		)





def __snake_case (				UpperCAmelCase_						:						Tuple ,    UpperCAmelCase_						:						int		):
 lowerCamelCase_								=  normalize_answer(UpperCAmelCase_		).split()
 lowerCamelCase_								=  normalize_answer(UpperCAmelCase_		).split()
 lowerCamelCase_								=  Counter(UpperCAmelCase_		) & Counter(UpperCAmelCase_		)
 lowerCamelCase_								=  sum(common.values()		)
 if num_same == 0:
  return 0
 lowerCamelCase_								=  1.0 * num_same / len(UpperCAmelCase_		)
 lowerCamelCase_								=  1.0 * num_same / len(UpperCAmelCase_		)
 lowerCamelCase_								=  (2 * precision * recall) / (precision + recall)
 return fa





def __snake_case (				UpperCAmelCase_						:						Tuple ,    UpperCAmelCase_						:						List[Any]		):
 return normalize_answer(UpperCAmelCase_		) == normalize_answer(UpperCAmelCase_		)





def __snake_case (				UpperCAmelCase_						:						Any ,    UpperCAmelCase_						:						Dict ,    UpperCAmelCase_						:						Union[str, Any]		):
 lowerCamelCase_								=  []
 for ground_truth in ground_truths:
  lowerCamelCase_								=  metric_fn(UpperCAmelCase_ ,    UpperCAmelCase_		)
  scores_for_ground_truths.append(UpperCAmelCase_		)
 return max(UpperCAmelCase_		)





def __snake_case (				UpperCAmelCase_						:						Optional[int] ,    UpperCAmelCase_						:						Dict		):
 lowerCamelCase_								=  lowerCamelCase_								=  lowerCamelCase_								=  0
 lowerCamelCase_								=  []
 for passage in dataset:
  for qa in passage["qas"]:
   total += 1
   if qa["id"] not in predictions:
    lowerCamelCase_								=  F'''Unanswered question {qa["id"]} will receive score 0.'''
    print(UpperCAmelCase_ ,    file=sys.stderr		)
    continue

   lowerCamelCase_								=  [x["text"] for x in qa["answers"]]
   lowerCamelCase_								=  predictions[qa["id"]]

   lowerCamelCase_								=  metric_max_over_ground_truths(UpperCAmelCase_ ,    UpperCAmelCase_ ,    UpperCAmelCase_		)
   if int(_exact_match		) == 1:
    correct_ids.append(qa["id"]		)
   exact_match += _exact_match

   fa += metric_max_over_ground_truths(UpperCAmelCase_ ,    UpperCAmelCase_ ,    UpperCAmelCase_		)

 lowerCamelCase_								=  exact_match / total
 lowerCamelCase_								=  fa / total

 return {"exact_match": exact_match, "f1": fa}, correct_ids


if __name__ == "__main__":
     a_  :    Optional[int]      =			"""1.0"""
     a_  :    Dict      =			argparse.ArgumentParser("""Official evaluation script for ReCoRD v1.0.""")
     parser.add_argument("""data_file""", help="""The dataset file in JSON format.""")
     parser.add_argument("""pred_file""", help="""The model prediction file in JSON format.""")
     parser.add_argument("""--output_correct_ids""", action="""store_true""", help="""Output the correctly answered query IDs.""")
     a_  :    Optional[int]      =			parser.parse_args()

     with open(args.data_file) as data_file:
          a_  :    Any      =			json.load(data_file)
          if dataset_json["version"] != expected_version:
               print(
                   f'''Evaluation expects v-{expected_version}, but got dataset with v-{dataset_json['version']}''',
                   file=sys.stderr,
               )
          a_  :    Any      =			dataset_json["""data"""]

     with open(args.pred_file) as pred_file:
          a_  :    List[str]      =			json.load(pred_file)

     a_     ,       a_  :    int      =			evaluate(dataset, predictions)

     if args.output_correct_ids:
          print(f'''Output {len(correct_ids)} correctly answered question IDs.''')
          with open("""correct_ids.json""", """w""") as f:
               json.dump(correct_ids, f)



