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:






 """simple docstring"""




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

        """simple docstring"""

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

        """simple docstring"""

        A_							:       Optional[Any]	      =				ids_tensor([self.batch_size, self.seq_length] ,       self.vocab_size      )

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

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

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

        A_							:       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      ):

        """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=__magic_name__ ,       initializer_range=self.initializer_range ,       )
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        (
            (
            A_
        )      ,							(
            A_
        )      ,							(
            A_
        )      ,							(
            A_
        )      ,							(
            A_
        )      ,							(
            A_
        )      ,							(
            A_
        )      ,							(
            A_
        )      ,							
        )							:       Any	      =				self.prepare_config_and_inputs()

        A_							:       Any	      =				True
        A_							:       Any	      =				floats_tensor([self.batch_size, self.seq_length, self.hidden_size]      )
        A_							:       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 ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       ):

        """simple docstring"""

        A_							:       Dict	      =				ElectraModel(config=__magic_name__      )
        model.to(__magic_name__      )
        model.eval()
        A_							:       Union[str, Any]	      =				model(__magic_name__ ,       attention_mask=__magic_name__ ,       token_type_ids=__magic_name__      )
        A_							:       Any	      =				model(__magic_name__ ,       token_type_ids=__magic_name__      )
        A_							:       Optional[Any]	      =				model(__magic_name__      )
        self.parent.assertEqual(result.last_hidden_state.shape ,       (self.batch_size, self.seq_length, self.hidden_size)      )
 def  UpperCAmelCase   (    self ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       ):

        """simple docstring"""

        A_							:       Any	      =				True
        A_							:       Tuple	      =				ElectraModel(__magic_name__      )
        model.to(__magic_name__      )
        model.eval()
        A_							:       Dict	      =				model(
            __magic_name__ ,       attention_mask=__magic_name__ ,       token_type_ids=__magic_name__ ,       encoder_hidden_states=__magic_name__ ,       encoder_attention_mask=__magic_name__ ,       )
        A_							:       Any	      =				model(
            __magic_name__ ,       attention_mask=__magic_name__ ,       token_type_ids=__magic_name__ ,       encoder_hidden_states=__magic_name__ ,       )
        A_							:       str	      =				model(__magic_name__ ,       attention_mask=__magic_name__ ,       token_type_ids=__magic_name__      )
        self.parent.assertEqual(result.last_hidden_state.shape ,       (self.batch_size, self.seq_length, self.hidden_size)      )
 def  UpperCAmelCase   (    self ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       ):

        """simple docstring"""

        A_							:       Optional[int]	      =				ElectraForMaskedLM(config=__magic_name__      )
        model.to(__magic_name__      )
        model.eval()
        A_							:       Optional[int]	      =				model(__magic_name__ ,       attention_mask=__magic_name__ ,       token_type_ids=__magic_name__ ,       labels=__magic_name__      )
        self.parent.assertEqual(result.logits.shape ,       (self.batch_size, self.seq_length, self.vocab_size)      )
 def  UpperCAmelCase   (    self ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       ):

        """simple docstring"""

        A_							:       str	      =				ElectraForCausalLM(config=__magic_name__      )
        model.to(__magic_name__      )
        model.eval()
        A_							:       Optional[Any]	      =				model(__magic_name__ ,       attention_mask=__magic_name__ ,       token_type_ids=__magic_name__ ,       labels=__magic_name__      )
        self.parent.assertEqual(result.logits.shape ,       (self.batch_size, self.seq_length, self.vocab_size)      )
 def  UpperCAmelCase   (    self ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       ):

        """simple docstring"""

        A_							:       Optional[int]	      =				self.num_labels
        A_							:       List[str]	      =				ElectraForTokenClassification(config=__magic_name__      )
        model.to(__magic_name__      )
        model.eval()
        A_							:       Dict	      =				model(__magic_name__ ,       attention_mask=__magic_name__ ,       token_type_ids=__magic_name__ ,       labels=__magic_name__      )
        self.parent.assertEqual(result.logits.shape ,       (self.batch_size, self.seq_length, self.num_labels)      )
 def  UpperCAmelCase   (    self ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       ):

        """simple docstring"""

        A_							:       Any	      =				self.num_labels
        A_							:       Dict	      =				ElectraForPreTraining(config=__magic_name__      )
        model.to(__magic_name__      )
        model.eval()
        A_							:       List[Any]	      =				model(__magic_name__ ,       attention_mask=__magic_name__ ,       token_type_ids=__magic_name__ ,       labels=__magic_name__      )
        self.parent.assertEqual(result.logits.shape ,       (self.batch_size, self.seq_length)      )
 def  UpperCAmelCase   (    self ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       ):

        """simple docstring"""

        A_							:       Any	      =				self.num_labels
        A_							:       Optional[int]	      =				ElectraForSequenceClassification(__magic_name__      )
        model.to(__magic_name__      )
        model.eval()
        A_							:       Tuple	      =				model(__magic_name__ ,       attention_mask=__magic_name__ ,       token_type_ids=__magic_name__ ,       labels=__magic_name__      )
        self.parent.assertEqual(result.logits.shape ,       (self.batch_size, self.num_labels)      )
 def  UpperCAmelCase   (    self ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       ):

        """simple docstring"""

        A_							:       int	      =				ElectraForQuestionAnswering(config=__magic_name__      )
        model.to(__magic_name__      )
        model.eval()
        A_							:       List[Any]	      =				model(
            __magic_name__ ,       attention_mask=__magic_name__ ,       token_type_ids=__magic_name__ ,       start_positions=__magic_name__ ,       end_positions=__magic_name__ ,       )
        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 ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       __magic_name__ ,       ):

        """simple docstring"""

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

        """simple docstring"""

        A_							:       Optional[int]	      =				self.prepare_config_and_inputs()
        (
            (
            A_
        )      ,							(
            A_
        )      ,							(
            A_
        )      ,							(
            A_
        )      ,							(
            A_
        )      ,							(
            A_
        )      ,							(
            A_
        )      ,							(
            A_
        )      ,							
        )							:       Tuple	      =				config_and_inputs
        A_							:       Tuple	      =				{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
        return config, inputs_dict






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






 """simple docstring"""




 __magic_name__          =   (
     (
         ElectraModel,
         ElectraForPreTraining,
         ElectraForMaskedLM,
         ElectraForCausalLM,
         ElectraForMultipleChoice,
         ElectraForTokenClassification,
         ElectraForSequenceClassification,
         ElectraForQuestionAnswering,
     )
     if is_torch_available()
     else ()
 )
 __magic_name__          =   (
     {
         """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 {}
 )
 __magic_name__          =   True
 def  UpperCAmelCase   (    self ,       __magic_name__ ,       __magic_name__ ,       __magic_name__=False      ):

        """simple docstring"""

        A_							:       Any	      =				super()._prepare_for_class(__magic_name__ ,       __magic_name__ ,       return_labels=__magic_name__      )

        if return_labels:
               if model_class in get_values(__magic_name__      ):
                      A_							:       List[Any]	      =				torch.zeros(
                          (self.model_tester.batch_size, self.model_tester.seq_length) ,       dtype=torch.long ,       device=__magic_name__      )
        return inputs_dict
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_							:       Any	      =				ElectraModelTester(self      )
        A_							:       Any	      =				ConfigTester(self ,       config_class=__magic_name__ ,       hidden_size=37      )
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

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

        """simple docstring"""

        A_							:       Dict	      =				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_model(*__magic_name__      )
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_							:       List[Any]	      =				self.model_tester.prepare_config_and_inputs_for_decoder()
        self.model_tester.create_and_check_electra_model_as_decoder(*__magic_name__      )
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_							:       int	      =				self.model_tester.prepare_config_and_inputs()
        for type in ["absolute", "relative_key", "relative_key_query"]:
               A_							:       Union[str, Any]	      =				type
               self.model_tester.create_and_check_electra_model(*__magic_name__      )
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_							:       Dict	      =				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_for_masked_lm(*__magic_name__      )
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_							:       List[Any]	      =				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_for_token_classification(*__magic_name__      )
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_							:       List[str]	      =				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_for_pretraining(*__magic_name__      )
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_							:       str	      =				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_for_sequence_classification(*__magic_name__      )
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_							:       Any	      =				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_for_question_answering(*__magic_name__      )
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_							:       List[Any]	      =				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_electra_for_multiple_choice(*__magic_name__      )
 @slow
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               A_							:       Any	      =				ElectraModel.from_pretrained(__magic_name__      )
               self.assertIsNotNone(__magic_name__      )
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_							:       List[Any]	      =				self.model_tester.prepare_config_and_inputs_for_decoder()
        self.model_tester.create_and_check_electra_for_causal_lm(*__magic_name__      )






@require_torch
class     __UpperCAmelCase(    unittest.TestCase				):






 """simple docstring"""




 @slow
 def  UpperCAmelCase   (    self      ):

        """simple docstring"""

        A_							:       int	      =				ElectraModel.from_pretrained('''google/electra-small-discriminator'''      )
        A_							:       Any	      =				torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]      )
        A_							:       Optional[Any]	      =				torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]      )
        A_							:       Union[str, Any]	      =				model(__magic_name__ ,       attention_mask=__magic_name__      )[0]
        A_							:       str	      =				torch.Size((1, 11, 256)      )
        self.assertEqual(output.shape ,       __magic_name__      )
        A_							:       Union[str, Any]	      =				torch.tensor(
            [[[0.44_71, 0.68_21, -0.32_65], [0.46_27, 0.52_55, -0.36_68], [0.45_32, 0.33_13, -0.43_44]]]      )

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



from transformers import ProcessorMixin






class     __UpperCAmelCase(    A__				):






 """simple docstring"""




 __magic_name__          =   """AutoFeatureExtractor"""
 __magic_name__          =   """AutoTokenizer"""


