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





 def __init__(      self	,    lowercase_	,    lowercase_=13	,    lowercase_=7	,    lowercase_=True	,    lowercase_=True	,    lowercase_=True	,    lowercase_=True	,    lowercase_=99	,    lowercase_=32	,    lowercase_=5	,    lowercase_=4	,    lowercase_=37	,    lowercase_="gelu"	,    lowercase_=0.1	,    lowercase_=0.1	,    lowercase_=5_12	,    lowercase_=16	,    lowercase_=2	,    lowercase_=0.02	,    lowercase_=3	,    lowercase_=4	,    lowercase_=None	,    ):
     snake_case_	:      Union[str, Any]            =  parent
     snake_case_	:      Any            =  batch_size
     snake_case_	:      int            =  seq_length
     snake_case_	:      Optional[Any]            =  is_training
     snake_case_	:      str            =  use_input_mask
     snake_case_	:      Dict            =  use_token_type_ids
     snake_case_	:      Tuple            =  use_labels
     snake_case_	:      Union[str, Any]            =  vocab_size
     snake_case_	:      Dict            =  hidden_size
     snake_case_	:      int            =  num_hidden_layers
     snake_case_	:      Union[str, Any]            =  num_attention_heads
     snake_case_	:      List[str]            =  intermediate_size
     snake_case_	:      Any            =  hidden_act
     snake_case_	:      Any            =  hidden_dropout_prob
     snake_case_	:      Any            =  attention_probs_dropout_prob
     snake_case_	:      List[str]            =  max_position_embeddings
     snake_case_	:      Optional[Any]            =  type_vocab_size
     snake_case_	:      Dict            =  type_sequence_label_size
     snake_case_	:      Tuple            =  initializer_range
     snake_case_	:      Optional[Any]            =  num_labels
     snake_case_	:      int            =  num_choices
     snake_case_	:      str            =  scope





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

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

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

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

     snake_case_	:      Tuple            =  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):
     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):
     (
         snake_case_     
     )	:      Optional[Any]            =  self.prepare_config_and_inputs()

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





 def     snake_case__						(      self	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    ):
     snake_case_	:      Optional[int]            =  True
     snake_case_	:      Dict            =  ElectraModel(__A)
     model.to(__A)
     model.eval()
     snake_case_	:      Optional[Any]            =  model(
         __A	,    attention_mask=__A	,    token_type_ids=__A	,    encoder_hidden_states=__A	,    encoder_attention_mask=__A	,    )
     snake_case_	:      Optional[Any]            =  model(
         __A	,    attention_mask=__A	,    token_type_ids=__A	,    encoder_hidden_states=__A	,    )
     snake_case_	:      List[Any]            =  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	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    ):
     snake_case_	:      Tuple            =  ElectraForMaskedLM(config=__A)
     model.to(__A)
     model.eval()
     snake_case_	:      Dict            =  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	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    ):
     snake_case_	:      Dict            =  ElectraForCausalLM(config=__A)
     model.to(__A)
     model.eval()
     snake_case_	:      Optional[int]            =  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	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    ):
     snake_case_	:      Optional[Any]            =  self.num_labels
     snake_case_	:      Dict            =  ElectraForTokenClassification(config=__A)
     model.to(__A)
     model.eval()
     snake_case_	:      Union[str, Any]            =  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	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    ):
     snake_case_	:      Any            =  self.num_labels
     snake_case_	:      List[str]            =  ElectraForPreTraining(config=__A)
     model.to(__A)
     model.eval()
     snake_case_	:      int            =  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	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    ):
     snake_case_	:      List[str]            =  self.num_labels
     snake_case_	:      Tuple            =  ElectraForSequenceClassification(__A)
     model.to(__A)
     model.eval()
     snake_case_	:      Dict            =  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	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    ):
     snake_case_	:      Optional[Any]            =  ElectraForQuestionAnswering(config=__A)
     model.to(__A)
     model.eval()
     snake_case_	:      str            =  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	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    ):
     snake_case_	:      str            =  self.num_choices
     snake_case_	:      List[Any]            =  ElectraForMultipleChoice(config=__A)
     model.to(__A)
     model.eval()
     snake_case_	:      str            =  input_ids.unsqueeze(1).expand(-1	,    self.num_choices	,    -1).contiguous()
     snake_case_	:      List[str]            =  token_type_ids.unsqueeze(1).expand(-1	,    self.num_choices	,    -1).contiguous()
     snake_case_	:      Dict            =  input_mask.unsqueeze(1).expand(-1	,    self.num_choices	,    -1).contiguous()
     snake_case_	:      Any            =  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):
     snake_case_	:      Any            =  self.prepare_config_and_inputs()
     (
         snake_case_     
     )	:      Union[str, Any]            =  config_and_inputs
     snake_case_	:      Optional[int]            =  {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
     return config, inputs_dict






@require_torch
class 				UpperCAmelCase_			(		snake_case__      ,  snake_case__      ,  unittest.TestCase   ):
 UpperCAmelCase_						      =  (
     (
         ElectraModel,
         ElectraForPreTraining,
         ElectraForMaskedLM,
         ElectraForCausalLM,
         ElectraForMultipleChoice,
         ElectraForTokenClassification,
         ElectraForSequenceClassification,
         ElectraForQuestionAnswering,
     )
     if is_torch_available()
     else ()
 )
 UpperCAmelCase_						      =  (
     {
         "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 {}
 )
 UpperCAmelCase_						      =  True





 def     snake_case__						(      self	,    lowercase_	,    lowercase_	,    lowercase_=False):
     snake_case_	:      List[Any]            =  super()._prepare_for_class(__A	,    __A	,    return_labels=__A)

     if return_labels:
         if model_class in get_values(__A):
             snake_case_	:      List[str]            =  torch.zeros(
                 (self.model_tester.batch_size, self.model_tester.seq_length)	,    dtype=torch.long	,    device=__A)
     return inputs_dict





 def     snake_case__						(      self):
     snake_case_	:      List[str]            =  ElectraModelTester(self)
     snake_case_	:      int            =  ConfigTester(self	,    config_class=__A	,    hidden_size=37)





 def     snake_case__						(      self):
     self.config_tester.run_common_tests()





 def     snake_case__						(      self):
     snake_case_	:      Optional[int]            =  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_electra_model(*__A)





 def     snake_case__						(      self):
     snake_case_	:      List[Any]            =  self.model_tester.prepare_config_and_inputs_for_decoder()
     self.model_tester.create_and_check_electra_model_as_decoder(*__A)





 def     snake_case__						(      self):
     snake_case_	:      Tuple            =  self.model_tester.prepare_config_and_inputs()
     for type in ["absolute", "relative_key", "relative_key_query"]:
         snake_case_	:      Union[str, Any]            =  type
         self.model_tester.create_and_check_electra_model(*__A)





 def     snake_case__						(      self):
     snake_case_	:      List[Any]            =  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_electra_for_masked_lm(*__A)





 def     snake_case__						(      self):
     snake_case_	:      Tuple            =  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_electra_for_token_classification(*__A)





 def     snake_case__						(      self):
     snake_case_	:      Optional[Any]            =  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_electra_for_pretraining(*__A)





 def     snake_case__						(      self):
     snake_case_	:      int            =  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_electra_for_sequence_classification(*__A)





 def     snake_case__						(      self):
     snake_case_	:      Tuple            =  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_electra_for_question_answering(*__A)





 def     snake_case__						(      self):
     snake_case_	:      Dict            =  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_electra_for_multiple_choice(*__A)





 @slow
 def     snake_case__						(      self):





     for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         snake_case_	:      str            =  ElectraModel.from_pretrained(__A)
         self.assertIsNotNone(__A)





 def     snake_case__						(      self):
     snake_case_	:      Optional[Any]            =  self.model_tester.prepare_config_and_inputs_for_decoder()
     self.model_tester.create_and_check_electra_for_causal_lm(*__A)






@require_torch
class 				UpperCAmelCase_			(		unittest.TestCase   ):





 @slow
 def     snake_case__						(      self):
     snake_case_	:      Union[str, Any]            =  ElectraModel.from_pretrained("google/electra-small-discriminator")
     snake_case_	:      Optional[Any]            =  torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]])
     snake_case_	:      str            =  torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
     snake_case_	:      Optional[int]            =  model(__A	,    attention_mask=__A)[0]
     snake_case_	:      Optional[Any]            =  torch.Size((1, 11, 2_56))
     self.assertEqual(output.shape	,    __A)
     snake_case_	:      int            =  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]	,    __A	,    atol=1E-4))







'''simple docstring'''
import importlib.metadata
import sys

from transformers.testing_utils import TestCasePlus
from transformers.utils.versions import require_version, require_version_core


a_     =  importlib.metadata.version("numpy")
a_     =  ".".join([str(x) for x in sys.version_info[:3]])






class 				UpperCAmelCase_			(		snake_case__   ):





 def     snake_case__						(      self):
     # lt + different version strings
     require_version_core("numpy<1000.4.5")
     require_version_core("numpy<1000.4")
     require_version_core("numpy<1000")

     # le
     require_version_core("numpy<=1000.4.5")
     require_version_core(F'numpy<={numpy_ver}')

     # eq
     require_version_core(F'numpy=={numpy_ver}')

     # ne
     require_version_core("numpy!=1000.4.5")

     # ge
     require_version_core("numpy>=1.0")
     require_version_core("numpy>=1.0.0")
     require_version_core(F'numpy>={numpy_ver}')

     # gt
     require_version_core("numpy>1.0.0")

     # mix
     require_version_core("numpy>1.0.0,<1000")

     # requirement w/o version
     require_version_core("numpy")

     # unmet requirements due to version conflict
     for req in ["numpy==1.0.0", "numpy>=1000.0.0", F'numpy<{numpy_ver}']:
         try:
             require_version_core(lowercase_)
         except ImportError as e:
             self.assertIn(F'{req} is required'	,    str(lowercase_))
             self.assertIn("but found"	,    str(lowercase_))

        # unmet requirements due to missing module
     for req in ["numpipypie>1", "numpipypie2"]:
         try:
             require_version_core(lowercase_)
         except importlib.metadata.PackageNotFoundError as e:
             self.assertIn(F'The \'{req}\' distribution was not found and is required by this application'	,    str(lowercase_))
             self.assertIn("Try: pip install transformers -U"	,    str(lowercase_))

        # bogus requirements formats:
        # 1. whole thing
     for req in ["numpy??1.0.0", "numpy1.0.0"]:
         try:
             require_version_core(lowercase_)
         except ValueError as e:
             self.assertIn("requirement needs to be in the pip package format"	,    str(lowercase_))
        # 2. only operators
     for req in ["numpy=1.0.0", "numpy == 1.00", "numpy<>1.0.0", "numpy><1.00", "numpy>>1.0.0"]:
         try:
             require_version_core(lowercase_)
         except ValueError as e:
             self.assertIn("need one of "	,    str(lowercase_))





 def     snake_case__						(      self):
     # matching requirement
     require_version("python>=3.6.0")

     # not matching requirements
     for req in ["python>9.9.9", "python<3.0.0"]:
         try:
             require_version_core(lowercase_)
         except ImportError as e:
             self.assertIn(F'{req} is required'	,    str(lowercase_))
             self.assertIn(F'but found python=={python_ver}'	,    str(lowercase_))




