

'''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        lowercase_	:

  '''simple docstring'''





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


    """simple docstring"""
    UpperCAmelCase													=	parent
    UpperCAmelCase													=	batch_size
    UpperCAmelCase													=	seq_length
    UpperCAmelCase													=	is_training
    UpperCAmelCase													=	use_input_mask
    UpperCAmelCase													=	use_token_type_ids
    UpperCAmelCase													=	use_labels
    UpperCAmelCase													=	vocab_size
    UpperCAmelCase													=	hidden_size
    UpperCAmelCase													=	num_hidden_layers
    UpperCAmelCase													=	num_attention_heads
    UpperCAmelCase													=	intermediate_size
    UpperCAmelCase													=	hidden_act
    UpperCAmelCase													=	hidden_dropout_prob
    UpperCAmelCase													=	attention_probs_dropout_prob
    UpperCAmelCase													=	max_position_embeddings
    UpperCAmelCase													=	type_vocab_size
    UpperCAmelCase													=	type_sequence_label_size
    UpperCAmelCase													=	initializer_range
    UpperCAmelCase													=	num_labels
    UpperCAmelCase													=	num_choices
    UpperCAmelCase													=	scope
  def 						snake_case_			(     self     )							->				int:


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

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

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

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

    UpperCAmelCase													=	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     )							->				Any:


    """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=a_   , initializer_range=self.initializer_range   , )
  def 						snake_case_			(     self     )							->				List[str]:


    """simple docstring"""
    (
        (
        UpperCAmelCase
    )							,      (
        UpperCAmelCase
    )							,      (
        UpperCAmelCase
    )							,      (
        UpperCAmelCase
    )							,      (
        UpperCAmelCase
    )							,      (
        UpperCAmelCase
    )							,      (
        UpperCAmelCase
    )							,      (
        UpperCAmelCase
    )							,      
    )													=	self.prepare_config_and_inputs()

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


    """simple docstring"""
    UpperCAmelCase													=	ElectraModel(config=a_     )
    model.to(a_     )
    model.eval()
    UpperCAmelCase													=	model(a_   , attention_mask=a_   , token_type_ids=a_     )
    UpperCAmelCase													=	model(a_   , token_type_ids=a_     )
    UpperCAmelCase													=	model(a_     )
    self.parent.assertEqual(result.last_hidden_state.shape   , (self.batch_size, self.seq_length, self.hidden_size)     )
  def 						snake_case_			(     self   , a_   , a_   , a_   , a_   , a_   , a_   , a_   , a_   , a_   , )							->				Union[str, Any]:


    """simple docstring"""
    UpperCAmelCase													=	True
    UpperCAmelCase													=	ElectraModel(a_     )
    model.to(a_     )
    model.eval()
    UpperCAmelCase													=	model(
        a_   , attention_mask=a_   , token_type_ids=a_   , encoder_hidden_states=a_   , encoder_attention_mask=a_   , )
    UpperCAmelCase													=	model(
        a_   , attention_mask=a_   , token_type_ids=a_   , encoder_hidden_states=a_   , )
    UpperCAmelCase													=	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 						snake_case_			(     self   , a_   , a_   , a_   , a_   , a_   , a_   , a_   , a_   , )							->				Dict:


    """simple docstring"""
    UpperCAmelCase													=	ElectraForMaskedLM(config=a_     )
    model.to(a_     )
    model.eval()
    UpperCAmelCase													=	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 						snake_case_			(     self   , a_   , a_   , a_   , a_   , a_   , a_   , a_   , a_   , a_   , )							->				Optional[int]:


    """simple docstring"""
    UpperCAmelCase													=	ElectraForCausalLM(config=a_     )
    model.to(a_     )
    model.eval()
    UpperCAmelCase													=	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 						snake_case_			(     self   , a_   , a_   , a_   , a_   , a_   , a_   , a_   , a_   , )							->				str:


    """simple docstring"""
    UpperCAmelCase													=	self.num_labels
    UpperCAmelCase													=	ElectraForTokenClassification(config=a_     )
    model.to(a_     )
    model.eval()
    UpperCAmelCase													=	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 						snake_case_			(     self   , a_   , a_   , a_   , a_   , a_   , a_   , a_   , a_   , )							->				Union[str, Any]:


    """simple docstring"""
    UpperCAmelCase													=	self.num_labels
    UpperCAmelCase													=	ElectraForPreTraining(config=a_     )
    model.to(a_     )
    model.eval()
    UpperCAmelCase													=	model(a_   , attention_mask=a_   , token_type_ids=a_   , labels=a_     )
    self.parent.assertEqual(result.logits.shape   , (self.batch_size, self.seq_length)     )
  def 						snake_case_			(     self   , a_   , a_   , a_   , a_   , a_   , a_   , a_   , a_   , )							->				Any:


    """simple docstring"""
    UpperCAmelCase													=	self.num_labels
    UpperCAmelCase													=	ElectraForSequenceClassification(a_     )
    model.to(a_     )
    model.eval()
    UpperCAmelCase													=	model(a_   , attention_mask=a_   , token_type_ids=a_   , labels=a_     )
    self.parent.assertEqual(result.logits.shape   , (self.batch_size, self.num_labels)     )
  def 						snake_case_			(     self   , a_   , a_   , a_   , a_   , a_   , a_   , a_   , a_   , )							->				Dict:


    """simple docstring"""
    UpperCAmelCase													=	ElectraForQuestionAnswering(config=a_     )
    model.to(a_     )
    model.eval()
    UpperCAmelCase													=	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 						snake_case_			(     self   , a_   , a_   , a_   , a_   , a_   , a_   , a_   , a_   , )							->				str:


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





  def 						snake_case_			(     self     )							->				Dict:


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




@require_torch
class        lowercase_	(		a ,       a ,       unittest.TestCase ):

  '''simple docstring'''





  __lowerCAmelCase					: int      =   (
      (
          ElectraModel,
          ElectraForPreTraining,
          ElectraForMaskedLM,
          ElectraForCausalLM,
          ElectraForMultipleChoice,
          ElectraForTokenClassification,
          ElectraForSequenceClassification,
          ElectraForQuestionAnswering,
      )
      if is_torch_available()
      else ()
  )
  __lowerCAmelCase					: Optional[Any]      =   (
      {
          "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					: int      =   True
  def 						snake_case_			(     self   , a_   , a_   , a_=False     )							->				int:


    """simple docstring"""
    UpperCAmelCase													=	super()._prepare_for_class(a_   , a_   , return_labels=a_     )

    if return_labels:
      if model_class in get_values(a_     ):
        UpperCAmelCase													=	torch.zeros(
            (self.model_tester.batch_size, self.model_tester.seq_length)   , dtype=torch.long   , device=a_     )
    return inputs_dict
  def 						snake_case_			(     self     )							->				Tuple:


    """simple docstring"""
    UpperCAmelCase													=	ElectraModelTester(self     )
    UpperCAmelCase													=	ConfigTester(self   , config_class=a_   , hidden_size=3_7     )
  def 						snake_case_			(     self     )							->				Any:


    """simple docstring"""
    self.config_tester.run_common_tests()
  def 						snake_case_			(     self     )							->				int:


    """simple docstring"""
    UpperCAmelCase													=	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_electra_model(*a_     )
  def 						snake_case_			(     self     )							->				int:


    """simple docstring"""
    UpperCAmelCase													=	self.model_tester.prepare_config_and_inputs_for_decoder()
    self.model_tester.create_and_check_electra_model_as_decoder(*a_     )
  def 						snake_case_			(     self     )							->				List[Any]:


    """simple docstring"""
    UpperCAmelCase													=	self.model_tester.prepare_config_and_inputs()
    for type in ["absolute", "relative_key", "relative_key_query"]:
      UpperCAmelCase													=	type
      self.model_tester.create_and_check_electra_model(*a_     )
  def 						snake_case_			(     self     )							->				Optional[Any]:


    """simple docstring"""
    UpperCAmelCase													=	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_electra_for_masked_lm(*a_     )
  def 						snake_case_			(     self     )							->				int:


    """simple docstring"""
    UpperCAmelCase													=	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_electra_for_token_classification(*a_     )
  def 						snake_case_			(     self     )							->				List[str]:


    """simple docstring"""
    UpperCAmelCase													=	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_electra_for_pretraining(*a_     )
  def 						snake_case_			(     self     )							->				str:


    """simple docstring"""
    UpperCAmelCase													=	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_electra_for_sequence_classification(*a_     )
  def 						snake_case_			(     self     )							->				Optional[int]:


    """simple docstring"""
    UpperCAmelCase													=	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_electra_for_question_answering(*a_     )
  def 						snake_case_			(     self     )							->				str:


    """simple docstring"""
    UpperCAmelCase													=	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_electra_for_multiple_choice(*a_     )
  @slow
  def 						snake_case_			(     self     )							->				Any:


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





  def 						snake_case_			(     self     )							->				Dict:


    """simple docstring"""
    UpperCAmelCase													=	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 ):

  '''simple docstring'''





  @slow
  def 						snake_case_			(     self     )							->				str:


    """simple docstring"""
    UpperCAmelCase													=	ElectraModel.from_pretrained('google/electra-small-discriminator'     )
    UpperCAmelCase													=	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													=	torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]     )
    UpperCAmelCase													=	model(a_   , attention_mask=a_     )[0]
    UpperCAmelCase													=	torch.Size((1, 1_1, 2_5_6)     )
    self.assertEqual(output.shape   , a_     )
    UpperCAmelCase													=	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     )     )





'''simple docstring'''




from collections import OrderedDict
from dataclasses import fields
from typing import Any, Tuple

import numpy as np

from .import_utils import is_torch_available


def       lowerCamelCase__		(						SCREAMING_SNAKE_CASE      :   Optional[Any]						):
  if is_torch_available():
    import torch

    if isinstance(SCREAMING_SNAKE_CASE       ,   torch.Tensor						):
      return True

  return isinstance(SCREAMING_SNAKE_CASE       ,   np.ndarray						)



class        lowercase_	(		a ):

  '''simple docstring'''





  def 						snake_case_			(     self     )							->				List[str]:


    """simple docstring"""
    UpperCAmelCase													=	fields(self     )

    # Safety and consistency checks
    if not len(a_     ):
      raise ValueError(F'''{self.__class__.__name__} has no fields.'''     )

    UpperCAmelCase													=	getattr(self   , class_fields[0].name     )
    UpperCAmelCase													=	all(getattr(self   , field.name     ) is None for field in class_fields[1:]     )

    if other_fields_are_none and isinstance(a_   , a_     ):
      for key, value in first_field.items():
        UpperCAmelCase													=	value
    else:
      for field in class_fields:
        UpperCAmelCase													=	getattr(self   , field.name     )
        if v is not None:
          UpperCAmelCase													=	v
  def __delitem__(     self   , *a_   , **a_     )							->				int:


    """simple docstring"""
    raise Exception(F'''You cannot use ``__delitem__`` on a {self.__class__.__name__} instance.'''     )
  def 						snake_case_			(     self   , *a_   , **a_     )							->				Tuple:


    """simple docstring"""
    raise Exception(F'''You cannot use ``setdefault`` on a {self.__class__.__name__} instance.'''     )
  def 						snake_case_			(     self   , *a_   , **a_     )							->				int:


    """simple docstring"""
    raise Exception(F'''You cannot use ``pop`` on a {self.__class__.__name__} instance.'''     )
  def 						snake_case_			(     self   , *a_   , **a_     )							->				Dict:


    """simple docstring"""
    raise Exception(F'''You cannot use ``update`` on a {self.__class__.__name__} instance.'''     )
  def __getitem__(     self   , a_     )							->				Tuple:


    """simple docstring"""
    if isinstance(a_   , a_     ):
      UpperCAmelCase													=	dict(self.items()     )
      return inner_dict[k]
    else:
      return self.to_tuple()[k]
  def __setattr__(     self   , a_   , a_     )							->				Dict:


    """simple docstring"""
    if name in self.keys() and value is not None:
      # Don't call self.__setitem__ to avoid recursion errors
      super().__setitem__(a_   , a_     )
    super().__setattr__(a_   , a_     )
  def __setitem__(     self   , a_   , a_     )							->				str:


    """simple docstring"""
    # Will raise a KeyException if needed
    super().__setitem__(a_   , a_     )
    # Don't call self.__setattr__ to avoid recursion errors
    super().__setattr__(a_   , a_     )





  def 						snake_case_			(     self     )							->				Tuple[Any]:


    """simple docstring"""
    return tuple(self[k] for k in self.keys()     )


