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





	def __init__(					self			:       Union[str, Any]      ,							__magic_name__			:       str      ,							__magic_name__			:       List[Any]=13      ,							__magic_name__			:       List[Any]=7      ,							__magic_name__			:       Any=True      ,							__magic_name__			:       Any=True      ,							__magic_name__			:       Tuple=True      ,							__magic_name__			:       List[Any]=True      ,							__magic_name__			:       Union[str, Any]=99      ,							__magic_name__			:       Any=32      ,							__magic_name__			:       int=5      ,							__magic_name__			:       Tuple=4      ,							__magic_name__			:       Optional[int]=37      ,							__magic_name__			:       List[Any]="gelu"      ,							__magic_name__			:       Tuple=0.1      ,							__magic_name__			:       int=0.1      ,							__magic_name__			:       Tuple=512      ,							__magic_name__			:       Union[str, Any]=16      ,							__magic_name__			:       Optional[int]=2      ,							__magic_name__			:       Tuple=0.02      ,							__magic_name__			:       Tuple=3      ,							__magic_name__			:       str=4      ,							__magic_name__			:       List[Any]=None      ,							):


				"""simple docstring"""

				lowerCAmelCase__								=							parent
				lowerCAmelCase__								=							batch_size
				lowerCAmelCase__								=							seq_length
				lowerCAmelCase__								=							is_training
				lowerCAmelCase__								=							use_input_mask
				lowerCAmelCase__								=							use_token_type_ids
				lowerCAmelCase__								=							use_labels
				lowerCAmelCase__								=							vocab_size
				lowerCAmelCase__								=							hidden_size
				lowerCAmelCase__								=							num_hidden_layers
				lowerCAmelCase__								=							num_attention_heads
				lowerCAmelCase__								=							intermediate_size
				lowerCAmelCase__								=							hidden_act
				lowerCAmelCase__								=							hidden_dropout_prob
				lowerCAmelCase__								=							attention_probs_dropout_prob
				lowerCAmelCase__								=							max_position_embeddings
				lowerCAmelCase__								=							type_vocab_size
				lowerCAmelCase__								=							type_sequence_label_size
				lowerCAmelCase__								=							initializer_range
				lowerCAmelCase__								=							num_labels
				lowerCAmelCase__								=							num_choices
				lowerCAmelCase__								=							scope





	def 							__SCREAMING_SNAKE_CASE  (					self			:       Tuple							):


				"""simple docstring"""

				lowerCAmelCase__								=							ids_tensor([self.batch_size, self.seq_length]      ,							self.vocab_size							)

				lowerCAmelCase__								=							None
				if self.use_input_mask:
							lowerCAmelCase__								=							random_attention_mask([self.batch_size, self.seq_length]							)

				lowerCAmelCase__								=							None
				if self.use_token_type_ids:
							lowerCAmelCase__								=							ids_tensor([self.batch_size, self.seq_length]      ,							self.type_vocab_size							)

				lowerCAmelCase__								=							None
				lowerCAmelCase__								=							None
				lowerCAmelCase__								=							None
				if self.use_labels:
							lowerCAmelCase__								=							ids_tensor([self.batch_size]      ,							self.type_sequence_label_size							)
							lowerCAmelCase__								=							ids_tensor([self.batch_size, self.seq_length]      ,							self.num_labels							)
							lowerCAmelCase__								=							ids_tensor([self.batch_size]      ,							self.num_choices							)
							lowerCAmelCase__								=							ids_tensor([self.batch_size, self.seq_length]      ,							1							)

				lowerCAmelCase__								=							self.get_config()

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





	def 							__SCREAMING_SNAKE_CASE  (					self			:       Dict							):


				"""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 							__SCREAMING_SNAKE_CASE  (					self			:       Optional[int]							):


				"""simple docstring"""

				(
				    (
				    lowerCAmelCase__
				)	,(
				    lowerCAmelCase__
				)	,(
				    lowerCAmelCase__
				)	,(
				    lowerCAmelCase__
				)	,(
				    lowerCAmelCase__
				)	,(
				    lowerCAmelCase__
				)	,(
				    lowerCAmelCase__
				)	,(
				    lowerCAmelCase__
				)	,
				)								=							self.prepare_config_and_inputs()

				lowerCAmelCase__								=							True
				lowerCAmelCase__								=							floats_tensor([self.batch_size, self.seq_length, self.hidden_size]							)
				lowerCAmelCase__								=							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 							__SCREAMING_SNAKE_CASE  (					self			:       Optional[Any]      ,							__magic_name__			:       Union[str, Any]      ,							__magic_name__			:       Tuple      ,							__magic_name__			:       int      ,							__magic_name__			:       Optional[int]      ,							__magic_name__			:       Union[str, Any]      ,							__magic_name__			:       Union[str, Any]      ,							__magic_name__			:       List[Any]      ,							__magic_name__			:       str      ,							):


				"""simple docstring"""

				lowerCAmelCase__								=							ElectraModel(config=__magic_name__							)
				model.to(__magic_name__							)
				model.eval()
				lowerCAmelCase__								=							model(__magic_name__      ,							attention_mask=__magic_name__      ,							token_type_ids=__magic_name__							)
				lowerCAmelCase__								=							model(__magic_name__      ,							token_type_ids=__magic_name__							)
				lowerCAmelCase__								=							model(__magic_name__							)
				self.parent.assertEqual(result.last_hidden_state.shape      ,							(self.batch_size, self.seq_length, self.hidden_size)							)





	def 							__SCREAMING_SNAKE_CASE  (					self			:       Tuple      ,							__magic_name__			:       List[str]      ,							__magic_name__			:       Optional[Any]      ,							__magic_name__			:       Optional[int]      ,							__magic_name__			:       int      ,							__magic_name__			:       int      ,							__magic_name__			:       Union[str, Any]      ,							__magic_name__			:       Any      ,							__magic_name__			:       List[Any]      ,							__magic_name__			:       Union[str, Any]      ,							):


				"""simple docstring"""

				lowerCAmelCase__								=							True
				lowerCAmelCase__								=							ElectraModel(__magic_name__							)
				model.to(__magic_name__							)
				model.eval()
				lowerCAmelCase__								=							model(
				    __magic_name__      ,							attention_mask=__magic_name__      ,							token_type_ids=__magic_name__      ,							encoder_hidden_states=__magic_name__      ,							encoder_attention_mask=__magic_name__      ,							)
				lowerCAmelCase__								=							model(
				    __magic_name__      ,							attention_mask=__magic_name__      ,							token_type_ids=__magic_name__      ,							encoder_hidden_states=__magic_name__      ,							)
				lowerCAmelCase__								=							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 							__SCREAMING_SNAKE_CASE  (					self			:       int      ,							__magic_name__			:       Tuple      ,							__magic_name__			:       Dict      ,							__magic_name__			:       List[Any]      ,							__magic_name__			:       Optional[Any]      ,							__magic_name__			:       int      ,							__magic_name__			:       Any      ,							__magic_name__			:       Any      ,							__magic_name__			:       List[str]      ,							):


				"""simple docstring"""

				lowerCAmelCase__								=							ElectraForMaskedLM(config=__magic_name__							)
				model.to(__magic_name__							)
				model.eval()
				lowerCAmelCase__								=							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 							__SCREAMING_SNAKE_CASE  (					self			:       Tuple      ,							__magic_name__			:       Tuple      ,							__magic_name__			:       Any      ,							__magic_name__			:       str      ,							__magic_name__			:       Any      ,							__magic_name__			:       Tuple      ,							__magic_name__			:       int      ,							__magic_name__			:       List[Any]      ,							__magic_name__			:       List[Any]      ,							__magic_name__			:       Optional[Any]      ,							):


				"""simple docstring"""

				lowerCAmelCase__								=							ElectraForCausalLM(config=__magic_name__							)
				model.to(__magic_name__							)
				model.eval()
				lowerCAmelCase__								=							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 							__SCREAMING_SNAKE_CASE  (					self			:       List[Any]      ,							__magic_name__			:       int      ,							__magic_name__			:       Any      ,							__magic_name__			:       Optional[Any]      ,							__magic_name__			:       int      ,							__magic_name__			:       List[Any]      ,							__magic_name__			:       Optional[int]      ,							__magic_name__			:       Union[str, Any]      ,							__magic_name__			:       Dict      ,							):


				"""simple docstring"""

				lowerCAmelCase__								=							self.num_labels
				lowerCAmelCase__								=							ElectraForTokenClassification(config=__magic_name__							)
				model.to(__magic_name__							)
				model.eval()
				lowerCAmelCase__								=							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 							__SCREAMING_SNAKE_CASE  (					self			:       Optional[Any]      ,							__magic_name__			:       Tuple      ,							__magic_name__			:       Union[str, Any]      ,							__magic_name__			:       List[str]      ,							__magic_name__			:       List[str]      ,							__magic_name__			:       Optional[int]      ,							__magic_name__			:       Optional[Any]      ,							__magic_name__			:       List[str]      ,							__magic_name__			:       Optional[int]      ,							):


				"""simple docstring"""

				lowerCAmelCase__								=							self.num_labels
				lowerCAmelCase__								=							ElectraForPreTraining(config=__magic_name__							)
				model.to(__magic_name__							)
				model.eval()
				lowerCAmelCase__								=							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 							__SCREAMING_SNAKE_CASE  (					self			:       List[Any]      ,							__magic_name__			:       int      ,							__magic_name__			:       Optional[Any]      ,							__magic_name__			:       int      ,							__magic_name__			:       List[str]      ,							__magic_name__			:       Union[str, Any]      ,							__magic_name__			:       List[Any]      ,							__magic_name__			:       Union[str, Any]      ,							__magic_name__			:       str      ,							):


				"""simple docstring"""

				lowerCAmelCase__								=							self.num_labels
				lowerCAmelCase__								=							ElectraForSequenceClassification(__magic_name__							)
				model.to(__magic_name__							)
				model.eval()
				lowerCAmelCase__								=							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 							__SCREAMING_SNAKE_CASE  (					self			:       Dict      ,							__magic_name__			:       Tuple      ,							__magic_name__			:       Union[str, Any]      ,							__magic_name__			:       Union[str, Any]      ,							__magic_name__			:       Dict      ,							__magic_name__			:       List[Any]      ,							__magic_name__			:       Union[str, Any]      ,							__magic_name__			:       Dict      ,							__magic_name__			:       int      ,							):


				"""simple docstring"""

				lowerCAmelCase__								=							ElectraForQuestionAnswering(config=__magic_name__							)
				model.to(__magic_name__							)
				model.eval()
				lowerCAmelCase__								=							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 							__SCREAMING_SNAKE_CASE  (					self			:       List[Any]      ,							__magic_name__			:       List[str]      ,							__magic_name__			:       Optional[Any]      ,							__magic_name__			:       Optional[Any]      ,							__magic_name__			:       int      ,							__magic_name__			:       List[str]      ,							__magic_name__			:       Union[str, Any]      ,							__magic_name__			:       Union[str, Any]      ,							__magic_name__			:       int      ,							):


				"""simple docstring"""

				lowerCAmelCase__								=							self.num_choices
				lowerCAmelCase__								=							ElectraForMultipleChoice(config=__magic_name__							)
				model.to(__magic_name__							)
				model.eval()
				lowerCAmelCase__								=							input_ids.unsqueeze(1							).expand(-1      ,							self.num_choices      ,							-1							).contiguous()
				lowerCAmelCase__								=							token_type_ids.unsqueeze(1							).expand(-1      ,							self.num_choices      ,							-1							).contiguous()
				lowerCAmelCase__								=							input_mask.unsqueeze(1							).expand(-1      ,							self.num_choices      ,							-1							).contiguous()
				lowerCAmelCase__								=							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 							__SCREAMING_SNAKE_CASE  (					self			:       Dict							):


				"""simple docstring"""

				lowerCAmelCase__								=							self.prepare_config_and_inputs()
				(
				    (
				    lowerCAmelCase__
				)	,(
				    lowerCAmelCase__
				)	,(
				    lowerCAmelCase__
				)	,(
				    lowerCAmelCase__
				)	,(
				    lowerCAmelCase__
				)	,(
				    lowerCAmelCase__
				)	,(
				    lowerCAmelCase__
				)	,(
				    lowerCAmelCase__
				)	,
				)								=							config_and_inputs
				lowerCAmelCase__								=							{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
				return config, inputs_dict






@require_torch
class    A     (	SCREAMING_SNAKE_CASE__  ,      SCREAMING_SNAKE_CASE__  ,      unittest.TestCase    ):
	snake_case__      :Dict        =     (
	    (
	        ElectraModel,
	        ElectraForPreTraining,
	        ElectraForMaskedLM,
	        ElectraForCausalLM,
	        ElectraForMultipleChoice,
	        ElectraForTokenClassification,
	        ElectraForSequenceClassification,
	        ElectraForQuestionAnswering,
	    )
	    if is_torch_available()
	    else ()
	)
	snake_case__      :Optional[Any]        =     (
	    {
	        '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 {}
	)
	snake_case__      :Union[str, Any]        =     True





	def 							__SCREAMING_SNAKE_CASE  (					self			:       int      ,							__magic_name__			:       Tuple      ,							__magic_name__			:       str      ,							__magic_name__			:       List[str]=False							):


				"""simple docstring"""

				lowerCAmelCase__								=							super()._prepare_for_class(__magic_name__      ,							__magic_name__      ,							return_labels=__magic_name__							)

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





	def 							__SCREAMING_SNAKE_CASE  (					self			:       Any							):


				"""simple docstring"""

				lowerCAmelCase__								=							ElectraModelTester(self							)
				lowerCAmelCase__								=							ConfigTester(self      ,							config_class=__magic_name__      ,							hidden_size=37							)





	def 							__SCREAMING_SNAKE_CASE  (					self			:       Optional[int]							):


				"""simple docstring"""

				self.config_tester.run_common_tests()





	def 							__SCREAMING_SNAKE_CASE  (					self			:       int							):


				"""simple docstring"""

				lowerCAmelCase__								=							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_electra_model(*__magic_name__							)





	def 							__SCREAMING_SNAKE_CASE  (					self			:       List[Any]							):


				"""simple docstring"""

				lowerCAmelCase__								=							self.model_tester.prepare_config_and_inputs_for_decoder()
				self.model_tester.create_and_check_electra_model_as_decoder(*__magic_name__							)





	def 							__SCREAMING_SNAKE_CASE  (					self			:       List[Any]							):


				"""simple docstring"""

				lowerCAmelCase__								=							self.model_tester.prepare_config_and_inputs()
				for type in ["absolute", "relative_key", "relative_key_query"]:
							lowerCAmelCase__								=							type
							self.model_tester.create_and_check_electra_model(*__magic_name__							)





	def 							__SCREAMING_SNAKE_CASE  (					self			:       int							):


				"""simple docstring"""

				lowerCAmelCase__								=							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_electra_for_masked_lm(*__magic_name__							)





	def 							__SCREAMING_SNAKE_CASE  (					self			:       List[str]							):


				"""simple docstring"""

				lowerCAmelCase__								=							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_electra_for_token_classification(*__magic_name__							)





	def 							__SCREAMING_SNAKE_CASE  (					self			:       Union[str, Any]							):


				"""simple docstring"""

				lowerCAmelCase__								=							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_electra_for_pretraining(*__magic_name__							)





	def 							__SCREAMING_SNAKE_CASE  (					self			:       List[str]							):


				"""simple docstring"""

				lowerCAmelCase__								=							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_electra_for_sequence_classification(*__magic_name__							)





	def 							__SCREAMING_SNAKE_CASE  (					self			:       Optional[Any]							):


				"""simple docstring"""

				lowerCAmelCase__								=							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_electra_for_question_answering(*__magic_name__							)





	def 							__SCREAMING_SNAKE_CASE  (					self			:       List[Any]							):


				"""simple docstring"""

				lowerCAmelCase__								=							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_electra_for_multiple_choice(*__magic_name__							)





	@slow
	def 							__SCREAMING_SNAKE_CASE  (					self			:       Dict							):


				"""simple docstring"""

				for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							lowerCAmelCase__								=							ElectraModel.from_pretrained(__magic_name__							)
							self.assertIsNotNone(__magic_name__							)







	def 							__SCREAMING_SNAKE_CASE  (					self			:       Tuple							):


				"""simple docstring"""

				lowerCAmelCase__								=							self.model_tester.prepare_config_and_inputs_for_decoder()
				self.model_tester.create_and_check_electra_for_causal_lm(*__magic_name__							)






@require_torch
class    A     (	unittest.TestCase    ):





	@slow
	def 							__SCREAMING_SNAKE_CASE  (					self			:       Optional[int]							):


				"""simple docstring"""

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







'''simple docstring'''

from ..utils import DummyObject, requires_backends






class    A     (	metaclass=SCREAMING_SNAKE_CASE__    ):
	snake_case__      :Optional[Any]        =     ['torch', 'transformers', 'k_diffusion']





	def __init__(					self			:       Optional[Any]      ,							*__magic_name__			:       Optional[int]      ,							**__magic_name__			:       str							):


				"""simple docstring"""

				requires_backends(self      ,							["torch", "transformers", "k_diffusion"]							)





	@classmethod
	def 							__SCREAMING_SNAKE_CASE  (					cls			:       str      ,							*__magic_name__			:       Union[str, Any]      ,							**__magic_name__			:       int							):


				"""simple docstring"""

				requires_backends(cls      ,							["torch", "transformers", "k_diffusion"]							)







	@classmethod
	def 							__SCREAMING_SNAKE_CASE  (					cls			:       int      ,							*__magic_name__			:       Dict      ,							**__magic_name__			:       str							):


				"""simple docstring"""

				requires_backends(cls      ,							["torch", "transformers", "k_diffusion"]							)





