

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


     def __init__(				self     :						List[str]     ,    UpperCAmelCase     :						List[Any]     ,    UpperCAmelCase     :						List[str]=1_3     ,    UpperCAmelCase     :						List[Any]=7     ,    UpperCAmelCase     :						int=True     ,    UpperCAmelCase     :						Union[str, Any]=True     ,    UpperCAmelCase     :						Tuple=True     ,    UpperCAmelCase     :						Optional[Any]=True     ,    UpperCAmelCase     :						Tuple=9_9     ,    UpperCAmelCase     :						Any=3_2     ,    UpperCAmelCase     :						Tuple=5     ,    UpperCAmelCase     :						str=4     ,    UpperCAmelCase     :						int=3_7     ,    UpperCAmelCase     :						List[str]="gelu"     ,    UpperCAmelCase     :						Dict=0.1     ,    UpperCAmelCase     :						Dict=0.1     ,    UpperCAmelCase     :						Dict=5_1_2     ,    UpperCAmelCase     :						Dict=1_6     ,    UpperCAmelCase     :						Any=2     ,    UpperCAmelCase     :						Optional[int]=0.02     ,    UpperCAmelCase     :						Dict=3     ,    UpperCAmelCase     :						List[str]=4     ,    UpperCAmelCase     :						Union[str, Any]=None     ,    )				->      Any:
      __lowerCAmelCase:  Dict        = parent
      __lowerCAmelCase:  Dict        = batch_size
      __lowerCAmelCase:  Optional[int]        = seq_length
      __lowerCAmelCase:  List[str]        = is_training
      __lowerCAmelCase:  Optional[int]        = use_input_mask
      __lowerCAmelCase:  List[Any]        = use_token_type_ids
      __lowerCAmelCase:  List[Any]        = use_labels
      __lowerCAmelCase:  int        = vocab_size
      __lowerCAmelCase:  List[Any]        = hidden_size
      __lowerCAmelCase:  str        = num_hidden_layers
      __lowerCAmelCase:  Any        = num_attention_heads
      __lowerCAmelCase:  Tuple        = intermediate_size
      __lowerCAmelCase:  Tuple        = hidden_act
      __lowerCAmelCase:  int        = hidden_dropout_prob
      __lowerCAmelCase:  Optional[Any]        = attention_probs_dropout_prob
      __lowerCAmelCase:  Dict        = max_position_embeddings
      __lowerCAmelCase:  List[Any]        = type_vocab_size
      __lowerCAmelCase:  Tuple        = type_sequence_label_size
      __lowerCAmelCase:  List[Any]        = initializer_range
      __lowerCAmelCase:  int        = num_labels
      __lowerCAmelCase:  int        = num_choices
      __lowerCAmelCase:  List[str]        = scope


     def     UpperCAmelCase    (				self     :						List[Any]   )				->      Optional[Any]:
      __lowerCAmelCase:  str        = ids_tensor([self.batch_size, self.seq_length]     ,    self.vocab_size   )

      __lowerCAmelCase:  Optional[int]        = None
      if self.use_input_mask:
       __lowerCAmelCase:  Optional[int]        = random_attention_mask([self.batch_size, self.seq_length]   )

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

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

      __lowerCAmelCase:  Tuple        = self.get_config()

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


     def     UpperCAmelCase    (				self     :						Union[str, Any]   )				->      Optional[int]:
      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=_lowercase     ,    initializer_range=self.initializer_range     ,    )


     def     UpperCAmelCase    (				self     :						Any   )				->      List[Any]:
      (
          (
          __lowerCAmelCase   
      )      ,				(
          __lowerCAmelCase   
      )      ,				(
          __lowerCAmelCase   
      )      ,				(
          __lowerCAmelCase   
      )      ,				(
          __lowerCAmelCase   
      )      ,				(
          __lowerCAmelCase   
      )      ,				(
          __lowerCAmelCase   
      )      ,				(
          __lowerCAmelCase   
      )      ,				   
      ):  List[str]        = self.prepare_config_and_inputs()

      __lowerCAmelCase:  int        = True
      __lowerCAmelCase:  Optional[Any]        = floats_tensor([self.batch_size, self.seq_length, self.hidden_size]   )
      __lowerCAmelCase:  Any        = 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     :						Optional[Any]     ,    UpperCAmelCase     :						Optional[int]     ,    UpperCAmelCase     :						Any     ,    UpperCAmelCase     :						int     ,    UpperCAmelCase     :						Tuple     ,    UpperCAmelCase     :						int     ,    UpperCAmelCase     :						Any     ,    UpperCAmelCase     :						Dict     ,    UpperCAmelCase     :						str     ,    )				->      Optional[int]:
      __lowerCAmelCase:  Optional[Any]        = ElectraModel(config=_lowercase   )
      model.to(_lowercase   )
      model.eval()
      __lowerCAmelCase:  List[Any]        = model(_lowercase     ,    attention_mask=_lowercase     ,    token_type_ids=_lowercase   )
      __lowerCAmelCase:  str        = model(_lowercase     ,    token_type_ids=_lowercase   )
      __lowerCAmelCase:  str        = model(_lowercase   )
      self.parent.assertEqual(result.last_hidden_state.shape     ,    (self.batch_size, self.seq_length, self.hidden_size)   )


     def     UpperCAmelCase    (				self     :						Any     ,    UpperCAmelCase     :						List[Any]     ,    UpperCAmelCase     :						Any     ,    UpperCAmelCase     :						int     ,    UpperCAmelCase     :						Optional[Any]     ,    UpperCAmelCase     :						str     ,    UpperCAmelCase     :						List[str]     ,    UpperCAmelCase     :						int     ,    UpperCAmelCase     :						Any     ,    UpperCAmelCase     :						Optional[int]     ,    )				->      List[Any]:
      __lowerCAmelCase:  Optional[Any]        = True
      __lowerCAmelCase:  Tuple        = ElectraModel(_lowercase   )
      model.to(_lowercase   )
      model.eval()
      __lowerCAmelCase:  Tuple        = model(
          _lowercase     ,    attention_mask=_lowercase     ,    token_type_ids=_lowercase     ,    encoder_hidden_states=_lowercase     ,    encoder_attention_mask=_lowercase     ,    )
      __lowerCAmelCase:  Any        = model(
          _lowercase     ,    attention_mask=_lowercase     ,    token_type_ids=_lowercase     ,    encoder_hidden_states=_lowercase     ,    )
      __lowerCAmelCase:  List[str]        = model(_lowercase     ,    attention_mask=_lowercase     ,    token_type_ids=_lowercase   )
      self.parent.assertEqual(result.last_hidden_state.shape     ,    (self.batch_size, self.seq_length, self.hidden_size)   )


     def     UpperCAmelCase    (				self     :						str     ,    UpperCAmelCase     :						int     ,    UpperCAmelCase     :						List[Any]     ,    UpperCAmelCase     :						List[Any]     ,    UpperCAmelCase     :						str     ,    UpperCAmelCase     :						int     ,    UpperCAmelCase     :						Union[str, Any]     ,    UpperCAmelCase     :						Dict     ,    UpperCAmelCase     :						Any     ,    )				->      List[str]:
      __lowerCAmelCase:  Optional[int]        = ElectraForMaskedLM(config=_lowercase   )
      model.to(_lowercase   )
      model.eval()
      __lowerCAmelCase:  Optional[int]        = model(_lowercase     ,    attention_mask=_lowercase     ,    token_type_ids=_lowercase     ,    labels=_lowercase   )
      self.parent.assertEqual(result.logits.shape     ,    (self.batch_size, self.seq_length, self.vocab_size)   )


     def     UpperCAmelCase    (				self     :						List[Any]     ,    UpperCAmelCase     :						List[Any]     ,    UpperCAmelCase     :						Dict     ,    UpperCAmelCase     :						List[Any]     ,    UpperCAmelCase     :						int     ,    UpperCAmelCase     :						Optional[Any]     ,    UpperCAmelCase     :						List[str]     ,    UpperCAmelCase     :						Optional[int]     ,    UpperCAmelCase     :						Union[str, Any]     ,    UpperCAmelCase     :						int     ,    )				->      List[str]:
      __lowerCAmelCase:  str        = ElectraForCausalLM(config=_lowercase   )
      model.to(_lowercase   )
      model.eval()
      __lowerCAmelCase:  Any        = model(_lowercase     ,    attention_mask=_lowercase     ,    token_type_ids=_lowercase     ,    labels=_lowercase   )
      self.parent.assertEqual(result.logits.shape     ,    (self.batch_size, self.seq_length, self.vocab_size)   )


     def     UpperCAmelCase    (				self     :						Any     ,    UpperCAmelCase     :						Dict     ,    UpperCAmelCase     :						Tuple     ,    UpperCAmelCase     :						Union[str, Any]     ,    UpperCAmelCase     :						str     ,    UpperCAmelCase     :						int     ,    UpperCAmelCase     :						List[Any]     ,    UpperCAmelCase     :						Union[str, Any]     ,    UpperCAmelCase     :						List[str]     ,    )				->      List[Any]:
      __lowerCAmelCase:  int        = self.num_labels
      __lowerCAmelCase:  Optional[int]        = ElectraForTokenClassification(config=_lowercase   )
      model.to(_lowercase   )
      model.eval()
      __lowerCAmelCase:  List[str]        = model(_lowercase     ,    attention_mask=_lowercase     ,    token_type_ids=_lowercase     ,    labels=_lowercase   )
      self.parent.assertEqual(result.logits.shape     ,    (self.batch_size, self.seq_length, self.num_labels)   )


     def     UpperCAmelCase    (				self     :						str     ,    UpperCAmelCase     :						List[Any]     ,    UpperCAmelCase     :						List[str]     ,    UpperCAmelCase     :						Tuple     ,    UpperCAmelCase     :						List[Any]     ,    UpperCAmelCase     :						Union[str, Any]     ,    UpperCAmelCase     :						int     ,    UpperCAmelCase     :						Optional[Any]     ,    UpperCAmelCase     :						List[str]     ,    )				->      str:
      __lowerCAmelCase:  Optional[int]        = self.num_labels
      __lowerCAmelCase:  Optional[int]        = ElectraForPreTraining(config=_lowercase   )
      model.to(_lowercase   )
      model.eval()
      __lowerCAmelCase:  Tuple        = model(_lowercase     ,    attention_mask=_lowercase     ,    token_type_ids=_lowercase     ,    labels=_lowercase   )
      self.parent.assertEqual(result.logits.shape     ,    (self.batch_size, self.seq_length)   )


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


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


     def     UpperCAmelCase    (				self     :						Union[str, Any]   )				->      Dict:
      __lowerCAmelCase:  List[str]        = self.prepare_config_and_inputs()
      (
          (
          __lowerCAmelCase   
      )      ,				(
          __lowerCAmelCase   
      )      ,				(
          __lowerCAmelCase   
      )      ,				(
          __lowerCAmelCase   
      )      ,				(
          __lowerCAmelCase   
      )      ,				(
          __lowerCAmelCase   
      )      ,				(
          __lowerCAmelCase   
      )      ,				(
          __lowerCAmelCase   
      )      ,				   
      ):  Optional[Any]        = config_and_inputs
      __lowerCAmelCase:  Optional[Any]        = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
      return config, inputs_dict







@require_torch
class 				A_					(						__lowerCamelCase				,      __lowerCamelCase				,      unittest.TestCase   ):
     _lowercase     :	Tuple        =      (
         (
             ElectraModel,
             ElectraForPreTraining,
             ElectraForMaskedLM,
             ElectraForCausalLM,
             ElectraForMultipleChoice,
             ElectraForTokenClassification,
             ElectraForSequenceClassification,
             ElectraForQuestionAnswering,
         )
         if is_torch_available()
         else ()
     )
     _lowercase     :	Dict        =      (
         {
             '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 {}
     )
     _lowercase     :	List[str]        =      True


     def     UpperCAmelCase    (				self     :						List[Any]     ,    UpperCAmelCase     :						List[str]     ,    UpperCAmelCase     :						Optional[int]     ,    UpperCAmelCase     :						Optional[int]=False   )				->      Optional[Any]:
      __lowerCAmelCase:  Tuple        = super()._prepare_for_class(_lowercase     ,    _lowercase     ,    return_labels=_lowercase   )

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


     def     UpperCAmelCase    (				self     :						List[str]   )				->      str:
      __lowerCAmelCase:  int        = ElectraModelTester(self   )
      __lowerCAmelCase:  Tuple        = ConfigTester(self     ,    config_class=_lowercase     ,    hidden_size=3_7   )


     def     UpperCAmelCase    (				self     :						Tuple   )				->      str:
      self.config_tester.run_common_tests()


     def     UpperCAmelCase    (				self     :						str   )				->      Any:
      __lowerCAmelCase:  Optional[Any]        = self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_electra_model(*_lowercase   )


     def     UpperCAmelCase    (				self     :						int   )				->      str:
      __lowerCAmelCase:  Dict        = self.model_tester.prepare_config_and_inputs_for_decoder()
      self.model_tester.create_and_check_electra_model_as_decoder(*_lowercase   )


     def     UpperCAmelCase    (				self     :						Tuple   )				->      Dict:
      __lowerCAmelCase:  Dict        = self.model_tester.prepare_config_and_inputs()
      for type in ["absolute", "relative_key", "relative_key_query"]:
       __lowerCAmelCase:  List[str]        = type
       self.model_tester.create_and_check_electra_model(*_lowercase   )


     def     UpperCAmelCase    (				self     :						Optional[Any]   )				->      str:
      __lowerCAmelCase:  List[str]        = self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_electra_for_masked_lm(*_lowercase   )


     def     UpperCAmelCase    (				self     :						List[Any]   )				->      Any:
      __lowerCAmelCase:  List[str]        = self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_electra_for_token_classification(*_lowercase   )


     def     UpperCAmelCase    (				self     :						Dict   )				->      Tuple:
      __lowerCAmelCase:  Dict        = self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_electra_for_pretraining(*_lowercase   )


     def     UpperCAmelCase    (				self     :						Optional[Any]   )				->      List[Any]:
      __lowerCAmelCase:  Dict        = self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_electra_for_sequence_classification(*_lowercase   )


     def     UpperCAmelCase    (				self     :						Optional[Any]   )				->      Dict:
      __lowerCAmelCase:  int        = self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_electra_for_question_answering(*_lowercase   )


     def     UpperCAmelCase    (				self     :						List[str]   )				->      Dict:
      __lowerCAmelCase:  Any        = self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_electra_for_multiple_choice(*_lowercase   )


     @slow
     def     UpperCAmelCase    (				self     :						List[str]   )				->      Optional[Any]:
      for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       __lowerCAmelCase:  Dict        = ElectraModel.from_pretrained(_lowercase   )
       self.assertIsNotNone(_lowercase   )


     def     UpperCAmelCase    (				self     :						str   )				->      Any:
      __lowerCAmelCase:  Any        = self.model_tester.prepare_config_and_inputs_for_decoder()
      self.model_tester.create_and_check_electra_for_causal_lm(*_lowercase   )







@require_torch
class 				A_					(						unittest.TestCase   ):


     @slow
     def     UpperCAmelCase    (				self     :						str   )				->      List[str]:
      __lowerCAmelCase:  List[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:  str        = torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]   )
      __lowerCAmelCase:  int        = model(_lowercase     ,    attention_mask=_lowercase   )[0]
      __lowerCAmelCase:  str        = torch.Size((1, 1_1, 2_5_6)   )
      self.assertEqual(output.shape     ,    _lowercase   )
      __lowerCAmelCase:  Any        = 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]     ,    _lowercase     ,    atol=1E-4   )   )






"""simple docstring"""







from ...utils import (
    OptionalDependencyNotAvailable,
    is_torch_available,
    is_transformers_available,
    is_transformers_version,
)


try:
						if not (is_transformers_available() and is_torch_available() and is_transformers_version(""">=""", """4.27.0""")):
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						from ...utils.dummy_torch_and_transformers_objects import (
						    AudioLDMPipeline,
						)
else:
						from .pipeline_audioldm import AudioLDMPipeline

