"""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 							__snake_case:


    '''simple docstring'''





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




         '''simple docstring'''



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




         '''simple docstring'''



         __A					:					int								=		ids_tensor([self.batch_size, self.seq_length]					,       self.vocab_size	)

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

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

         __A					:					Any								=		None
         __A					:					Tuple								=		None
         __A					:					Tuple								=		None
         if self.use_labels:
              __A					:					Union[str, Any]								=		ids_tensor([self.batch_size]					,       self.type_sequence_label_size	)
              __A					:					Dict								=		ids_tensor([self.batch_size, self.seq_length]					,       self.num_labels	)
              __A					:					Union[str, Any]								=		ids_tensor([self.batch_size]					,       self.num_choices	)
              __A					:					str								=		ids_tensor([self.batch_size, self.seq_length]					,       1	)

         __A					:					Optional[int]								=		self.get_config()

         return (
             config,
             input_ids,
             token_type_ids,
             input_mask,
             sequence_labels,
             token_labels,
             choice_labels,
             fake_token_labels,
         )
    def 						_a							(  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=__lowerCamelCase					,       initializer_range=self.initializer_range					,       )
    def 						_a							(  self	):




         '''simple docstring'''



         (
             (
             __A
         )						,		(
             __A
         )						,		(
             __A
         )						,		(
             __A
         )						,		(
             __A
         )						,		(
             __A
         )						,		(
             __A
         )						,		(
             __A
         )						,		
         )					:					str								=		self.prepare_config_and_inputs()

         __A					:					Union[str, Any]								=		True
         __A					:					Tuple								=		floats_tensor([self.batch_size, self.seq_length, self.hidden_size]	)
         __A					:					List[str]								=		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 						_a							(  self					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       ):




         '''simple docstring'''



         __A					:					List[Any]								=		ElectraModel(config=__lowerCamelCase	)
         model.to(__lowerCamelCase	)
         model.eval()
         __A					:					Any								=		model(__lowerCamelCase					,       attention_mask=__lowerCamelCase					,       token_type_ids=__lowerCamelCase	)
         __A					:					str								=		model(__lowerCamelCase					,       token_type_ids=__lowerCamelCase	)
         __A					:					str								=		model(__lowerCamelCase	)
         self.parent.assertEqual(result.last_hidden_state.shape					,       (self.batch_size, self.seq_length, self.hidden_size)	)
    def 						_a							(  self					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       ):




         '''simple docstring'''



         __A					:					Union[str, Any]								=		True
         __A					:					List[str]								=		ElectraModel(__lowerCamelCase	)
         model.to(__lowerCamelCase	)
         model.eval()
         __A					:					Union[str, Any]								=		model(
             __lowerCamelCase					,       attention_mask=__lowerCamelCase					,       token_type_ids=__lowerCamelCase					,       encoder_hidden_states=__lowerCamelCase					,       encoder_attention_mask=__lowerCamelCase					,       )
         __A					:					str								=		model(
             __lowerCamelCase					,       attention_mask=__lowerCamelCase					,       token_type_ids=__lowerCamelCase					,       encoder_hidden_states=__lowerCamelCase					,       )
         __A					:					int								=		model(__lowerCamelCase					,       attention_mask=__lowerCamelCase					,       token_type_ids=__lowerCamelCase	)
         self.parent.assertEqual(result.last_hidden_state.shape					,       (self.batch_size, self.seq_length, self.hidden_size)	)
    def 						_a							(  self					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       ):




         '''simple docstring'''



         __A					:					Dict								=		ElectraForMaskedLM(config=__lowerCamelCase	)
         model.to(__lowerCamelCase	)
         model.eval()
         __A					:					List[str]								=		model(__lowerCamelCase					,       attention_mask=__lowerCamelCase					,       token_type_ids=__lowerCamelCase					,       labels=__lowerCamelCase	)
         self.parent.assertEqual(result.logits.shape					,       (self.batch_size, self.seq_length, self.vocab_size)	)
    def 						_a							(  self					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       ):




         '''simple docstring'''



         __A					:					List[str]								=		ElectraForCausalLM(config=__lowerCamelCase	)
         model.to(__lowerCamelCase	)
         model.eval()
         __A					:					Any								=		model(__lowerCamelCase					,       attention_mask=__lowerCamelCase					,       token_type_ids=__lowerCamelCase					,       labels=__lowerCamelCase	)
         self.parent.assertEqual(result.logits.shape					,       (self.batch_size, self.seq_length, self.vocab_size)	)
    def 						_a							(  self					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       ):




         '''simple docstring'''



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




         '''simple docstring'''



         __A					:					Tuple								=		self.num_labels
         __A					:					Optional[int]								=		ElectraForPreTraining(config=__lowerCamelCase	)
         model.to(__lowerCamelCase	)
         model.eval()
         __A					:					List[str]								=		model(__lowerCamelCase					,       attention_mask=__lowerCamelCase					,       token_type_ids=__lowerCamelCase					,       labels=__lowerCamelCase	)
         self.parent.assertEqual(result.logits.shape					,       (self.batch_size, self.seq_length)	)
    def 						_a							(  self					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       ):




         '''simple docstring'''



         __A					:					List[str]								=		self.num_labels
         __A					:					str								=		ElectraForSequenceClassification(__lowerCamelCase	)
         model.to(__lowerCamelCase	)
         model.eval()
         __A					:					int								=		model(__lowerCamelCase					,       attention_mask=__lowerCamelCase					,       token_type_ids=__lowerCamelCase					,       labels=__lowerCamelCase	)
         self.parent.assertEqual(result.logits.shape					,       (self.batch_size, self.num_labels)	)
    def 						_a							(  self					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       ):




         '''simple docstring'''



         __A					:					Dict								=		ElectraForQuestionAnswering(config=__lowerCamelCase	)
         model.to(__lowerCamelCase	)
         model.eval()
         __A					:					List[Any]								=		model(
             __lowerCamelCase					,       attention_mask=__lowerCamelCase					,       token_type_ids=__lowerCamelCase					,       start_positions=__lowerCamelCase					,       end_positions=__lowerCamelCase					,       )
         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 						_a							(  self					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       ):




         '''simple docstring'''



         __A					:					Union[str, Any]								=		self.num_choices
         __A					:					str								=		ElectraForMultipleChoice(config=__lowerCamelCase	)
         model.to(__lowerCamelCase	)
         model.eval()
         __A					:					Optional[Any]								=		input_ids.unsqueeze(1	).expand(-1					,       self.num_choices					,       -1	).contiguous()
         __A					:					Optional[int]								=		token_type_ids.unsqueeze(1	).expand(-1					,       self.num_choices					,       -1	).contiguous()
         __A					:					int								=		input_mask.unsqueeze(1	).expand(-1					,       self.num_choices					,       -1	).contiguous()
         __A					:					Any								=		model(
             __lowerCamelCase					,       attention_mask=__lowerCamelCase					,       token_type_ids=__lowerCamelCase					,       labels=__lowerCamelCase					,       )
         self.parent.assertEqual(result.logits.shape					,       (self.batch_size, self.num_choices)	)

    def 						_a							(  self	):




         '''simple docstring'''



         __A					:					str								=		self.prepare_config_and_inputs()
         (
             (
             __A
         )						,		(
             __A
         )						,		(
             __A
         )						,		(
             __A
         )						,		(
             __A
         )						,		(
             __A
         )						,		(
             __A
         )						,		(
             __A
         )						,		
         )					:					List[str]								=		config_and_inputs
         __A					:					Union[str, Any]								=		{'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'''





    _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 						_a							(  self					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase=False	):




         '''simple docstring'''



         __A					:					Optional[int]								=		super()._prepare_for_class(__lowerCamelCase					,       __lowerCamelCase					,       return_labels=__lowerCamelCase	)

         if return_labels:
              if model_class in get_values(__lowerCamelCase	):
                   __A					:					Tuple								=		torch.zeros(
                       (self.model_tester.batch_size, self.model_tester.seq_length)					,       dtype=torch.long					,       device=__lowerCamelCase	)
         return inputs_dict
    def 						_a							(  self	):




         '''simple docstring'''



         __A					:					Dict								=		ElectraModelTester(self	)
         __A					:					Optional[int]								=		ConfigTester(self					,       config_class=__lowerCamelCase					,       hidden_size=37	)
    def 						_a							(  self	):




         '''simple docstring'''



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




         '''simple docstring'''



         __A					:					str								=		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_electra_model(*__lowerCamelCase	)
    def 						_a							(  self	):




         '''simple docstring'''



         __A					:					Dict								=		self.model_tester.prepare_config_and_inputs_for_decoder()
         self.model_tester.create_and_check_electra_model_as_decoder(*__lowerCamelCase	)
    def 						_a							(  self	):




         '''simple docstring'''



         __A					:					List[str]								=		self.model_tester.prepare_config_and_inputs()
         for type in ["absolute", "relative_key", "relative_key_query"]:
              __A					:					List[Any]								=		type
              self.model_tester.create_and_check_electra_model(*__lowerCamelCase	)
    def 						_a							(  self	):




         '''simple docstring'''



         __A					:					Optional[Any]								=		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_electra_for_masked_lm(*__lowerCamelCase	)
    def 						_a							(  self	):




         '''simple docstring'''



         __A					:					List[Any]								=		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_electra_for_token_classification(*__lowerCamelCase	)
    def 						_a							(  self	):




         '''simple docstring'''



         __A					:					str								=		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_electra_for_pretraining(*__lowerCamelCase	)
    def 						_a							(  self	):




         '''simple docstring'''



         __A					:					int								=		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_electra_for_sequence_classification(*__lowerCamelCase	)
    def 						_a							(  self	):




         '''simple docstring'''



         __A					:					Optional[int]								=		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_electra_for_question_answering(*__lowerCamelCase	)
    def 						_a							(  self	):




         '''simple docstring'''



         __A					:					Dict								=		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_electra_for_multiple_choice(*__lowerCamelCase	)
    @slow
    def 						_a							(  self	):




         '''simple docstring'''



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

    def 						_a							(  self	):




         '''simple docstring'''



         __A					:					Union[str, Any]								=		self.model_tester.prepare_config_and_inputs_for_decoder()
         self.model_tester.create_and_check_electra_for_causal_lm(*__lowerCamelCase	)







@require_torch
class 							__snake_case(					unittest.TestCase	):


    '''simple docstring'''





    @slow
    def 						_a							(  self	):




         '''simple docstring'''



         __A					:					Optional[Any]								=		ElectraModel.from_pretrained('google/electra-small-discriminator'	)
         __A					:					int								=		torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]	)
         __A					:					int								=		torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]	)
         __A					:					Union[str, Any]								=		model(__lowerCamelCase					,       attention_mask=__lowerCamelCase	)[0]
         __A					:					Tuple								=		torch.Size((1, 11, 256)	)
         self.assertEqual(output.shape					,       __lowerCamelCase	)
         __A					:					List[str]								=		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]					,       __lowerCamelCase					,       atol=1e-4	)	)




"""simple docstring"""







from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


lowerCamelCase      : Dict       ={'''configuration_sew_d''': ['''SEW_D_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''SEWDConfig''']}

try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       lowerCamelCase      : List[str]       =[
           '''SEW_D_PRETRAINED_MODEL_ARCHIVE_LIST''',
           '''SEWDForCTC''',
           '''SEWDForSequenceClassification''',
           '''SEWDModel''',
           '''SEWDPreTrainedModel''',
       ]

if TYPE_CHECKING:
       from .configuration_sew_d import SEW_D_PRETRAINED_CONFIG_ARCHIVE_MAP, SEWDConfig

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_sew_d import (
                  SEW_D_PRETRAINED_MODEL_ARCHIVE_LIST,
                  SEWDForCTC,
                  SEWDForSequenceClassification,
                  SEWDModel,
                  SEWDPreTrainedModel,
              )


else:
       import sys

       lowerCamelCase      : str       =_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)



