






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	:





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

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


				def  __lowercase				(					self       ):




									"""simple docstring"""

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

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

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

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

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

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


				def  __lowercase				(					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  __lowercase				(					self       ):




									"""simple docstring"""

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

									__UpperCamelCase :   Optional[Any]									=True
									__UpperCamelCase :   Tuple									=floats_tensor([self.batch_size, self.seq_length, self.hidden_size]       )
									__UpperCamelCase :   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  __lowercase				(					self				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    ):




									"""simple docstring"""

									__UpperCamelCase :   Union[str, Any]									=ElectraModel(config=lowerCamelCase__       )
									model.to(lowerCamelCase__       )
									model.eval()
									__UpperCamelCase :   Union[str, Any]									=model(lowerCamelCase__				,    attention_mask=lowerCamelCase__				,    token_type_ids=lowerCamelCase__       )
									__UpperCamelCase :   Union[str, Any]									=model(lowerCamelCase__				,    token_type_ids=lowerCamelCase__       )
									__UpperCamelCase :   Optional[int]									=model(lowerCamelCase__       )
									self.parent.assertEqual(result.last_hidden_state.shape				,    (self.batch_size, self.seq_length, self.hidden_size)       )


				def  __lowercase				(					self				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    ):




									"""simple docstring"""

									__UpperCamelCase :   Dict									=True
									__UpperCamelCase :   Optional[Any]									=ElectraModel(lowerCamelCase__       )
									model.to(lowerCamelCase__       )
									model.eval()
									__UpperCamelCase :   List[Any]									=model(
									    lowerCamelCase__				,    attention_mask=lowerCamelCase__				,    token_type_ids=lowerCamelCase__				,    encoder_hidden_states=lowerCamelCase__				,    encoder_attention_mask=lowerCamelCase__				,    )
									__UpperCamelCase :   Optional[Any]									=model(
									    lowerCamelCase__				,    attention_mask=lowerCamelCase__				,    token_type_ids=lowerCamelCase__				,    encoder_hidden_states=lowerCamelCase__				,    )
									__UpperCamelCase :   Optional[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  __lowercase				(					self				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    ):




									"""simple docstring"""

									__UpperCamelCase :   str									=ElectraForMaskedLM(config=lowerCamelCase__       )
									model.to(lowerCamelCase__       )
									model.eval()
									__UpperCamelCase :   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.vocab_size)       )


				def  __lowercase				(					self				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    ):




									"""simple docstring"""

									__UpperCamelCase :   Union[str, Any]									=ElectraForCausalLM(config=lowerCamelCase__       )
									model.to(lowerCamelCase__       )
									model.eval()
									__UpperCamelCase :   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.vocab_size)       )


				def  __lowercase				(					self				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    ):




									"""simple docstring"""

									__UpperCamelCase :   Tuple									=self.num_labels
									__UpperCamelCase :   List[Any]									=ElectraForTokenClassification(config=lowerCamelCase__       )
									model.to(lowerCamelCase__       )
									model.eval()
									__UpperCamelCase :   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.num_labels)       )


				def  __lowercase				(					self				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    ):




									"""simple docstring"""

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


				def  __lowercase				(					self				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    ):




									"""simple docstring"""

									__UpperCamelCase :   Any									=self.num_labels
									__UpperCamelCase :   Union[str, Any]									=ElectraForSequenceClassification(lowerCamelCase__       )
									model.to(lowerCamelCase__       )
									model.eval()
									__UpperCamelCase :   Tuple									=model(lowerCamelCase__				,    attention_mask=lowerCamelCase__				,    token_type_ids=lowerCamelCase__				,    labels=lowerCamelCase__       )
									self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.num_labels)       )


				def  __lowercase				(					self				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    ):




									"""simple docstring"""

									__UpperCamelCase :   int									=ElectraForQuestionAnswering(config=lowerCamelCase__       )
									model.to(lowerCamelCase__       )
									model.eval()
									__UpperCamelCase :   int									=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  __lowercase				(					self				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    ):




									"""simple docstring"""

									__UpperCamelCase :   Optional[int]									=self.num_choices
									__UpperCamelCase :   Optional[int]									=ElectraForMultipleChoice(config=lowerCamelCase__       )
									model.to(lowerCamelCase__       )
									model.eval()
									__UpperCamelCase :   int									=input_ids.unsqueeze(1       ).expand(-1				,    self.num_choices				,    -1       ).contiguous()
									__UpperCamelCase :   Optional[Any]									=token_type_ids.unsqueeze(1       ).expand(-1				,    self.num_choices				,    -1       ).contiguous()
									__UpperCamelCase :   str									=input_mask.unsqueeze(1       ).expand(-1				,    self.num_choices				,    -1       ).contiguous()
									__UpperCamelCase :   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  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase :   Dict									=self.prepare_config_and_inputs()
									(
									    (
									    __UpperCamelCase
									)						,							(
									    __UpperCamelCase
									)						,							(
									    __UpperCamelCase
									)						,							(
									    __UpperCamelCase
									)						,							(
									    __UpperCamelCase
									)						,							(
									    __UpperCamelCase
									)						,							(
									    __UpperCamelCase
									)						,							(
									    __UpperCamelCase
									)						,							
									) :   Optional[Any]									=config_and_inputs
									__UpperCamelCase :   Dict									={'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
									return config, inputs_dict




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





				"""simple docstring"""







				UpperCamelCase__					: Optional[int]     					=(
				    (
				        ElectraModel,
				        ElectraForPreTraining,
				        ElectraForMaskedLM,
				        ElectraForCausalLM,
				        ElectraForMultipleChoice,
				        ElectraForTokenClassification,
				        ElectraForSequenceClassification,
				        ElectraForQuestionAnswering,
				    )
				    if is_torch_available()
				    else ()
				)
				UpperCamelCase__					: Tuple     					=(
				    {
				        """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__					: Union[str, Any]     					=True


				def  __lowercase				(					self				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__=False       ):




									"""simple docstring"""

									__UpperCamelCase :   List[Any]									=super()._prepare_for_class(lowerCamelCase__				,    lowerCamelCase__				,    return_labels=lowerCamelCase__       )

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


				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase :   Union[str, Any]									=ElectraModelTester(self       )
									__UpperCamelCase :   Any									=ConfigTester(self				,    config_class=lowerCamelCase__				,    hidden_size=37       )


				def  __lowercase				(					self       ):




									"""simple docstring"""

									self.config_tester.run_common_tests()


				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase :   Union[str, Any]									=self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_electra_model(*lowerCamelCase__       )


				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase :   Optional[Any]									=self.model_tester.prepare_config_and_inputs_for_decoder()
									self.model_tester.create_and_check_electra_model_as_decoder(*lowerCamelCase__       )


				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase :   List[str]									=self.model_tester.prepare_config_and_inputs()
									for type in ["absolute", "relative_key", "relative_key_query"]:
														__UpperCamelCase :   Tuple									=type
														self.model_tester.create_and_check_electra_model(*lowerCamelCase__       )


				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase :   Any									=self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_electra_for_masked_lm(*lowerCamelCase__       )


				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase :   int									=self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_electra_for_token_classification(*lowerCamelCase__       )


				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase :   str									=self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_electra_for_pretraining(*lowerCamelCase__       )


				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase :   str									=self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_electra_for_sequence_classification(*lowerCamelCase__       )


				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase :   Optional[Any]									=self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_electra_for_question_answering(*lowerCamelCase__       )


				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase :   List[str]									=self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_electra_for_multiple_choice(*lowerCamelCase__       )


				@slow
				def  __lowercase				(					self       ):




									"""simple docstring"""

									for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														__UpperCamelCase :   Optional[int]									=ElectraModel.from_pretrained(lowerCamelCase__       )
														self.assertIsNotNone(lowerCamelCase__       )







				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase :   List[str]									=self.model_tester.prepare_config_and_inputs_for_decoder()
									self.model_tester.create_and_check_electra_for_causal_lm(*lowerCamelCase__       )




@require_torch
class 							__A	(     unittest.TestCase					):





				"""simple docstring"""







				@slow
				def  __lowercase				(					self       ):




									"""simple docstring"""

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









from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


A_			:Union[str, Any]	    =		{'''configuration_nat''': ['''NAT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''NatConfig''']}


try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							A_			:Optional[Any]	    =		[
							    '''NAT_PRETRAINED_MODEL_ARCHIVE_LIST''',
							    '''NatForImageClassification''',
							    '''NatModel''',
							    '''NatPreTrainedModel''',
							    '''NatBackbone''',
							]

if TYPE_CHECKING:
							from .configuration_nat import NAT_PRETRAINED_CONFIG_ARCHIVE_MAP, NatConfig

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_nat import (
														    NAT_PRETRAINED_MODEL_ARCHIVE_LIST,
														    NatBackbone,
														    NatForImageClassification,
														    NatModel,
														    NatPreTrainedModel,
														)

else:
							import sys

							A_			:Tuple	    =		_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)

