


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 					__lowercase					:



		"""simple docstring"""

		def __init__(   self		,			A		,			A=13		,			A=7		,			A=True		,			A=True		,			A=True		,			A=True		,			A=99		,			A=32		,			A=5		,			A=4		,			A=37		,			A="gelu"		,			A=0.1		,			A=0.1		,			A=5_12		,			A=16		,			A=2		,			A=0.02		,			A=3		,			A=4		,			A=None		,			)     ->   str:







					'''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 				__A      (   self  )     ->   str:







					'''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 				__A      (   self  )     ->   Optional[Any]:







					'''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=A		,			initializer_range=self.initializer_range		,			)







		def 				__A      (   self  )     ->   Any:







					'''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 				__A      (   self		,			A		,			A		,			A		,			A		,			A		,			A		,			A		,			A		,			)     ->   Optional[int]:







					'''simple docstring'''







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







		def 				__A      (   self		,			A		,			A		,			A		,			A		,			A		,			A		,			A		,			A		,			A		,			)     ->   Dict:







					'''simple docstring'''







					lowerCamelCase								=     True
					lowerCamelCase								=     ElectraModel(A  )
					model.to(A  )
					model.eval()
					lowerCamelCase								=     model(
					    A		,			attention_mask=A		,			token_type_ids=A		,			encoder_hidden_states=A		,			encoder_attention_mask=A		,			)
					lowerCamelCase								=     model(
					    A		,			attention_mask=A		,			token_type_ids=A		,			encoder_hidden_states=A		,			)
					lowerCamelCase								=     model(A		,			attention_mask=A		,			token_type_ids=A  )
					self.parent.assertEqual(result.last_hidden_state.shape		,			(self.batch_size, self.seq_length, self.hidden_size)  )







		def 				__A      (   self		,			A		,			A		,			A		,			A		,			A		,			A		,			A		,			A		,			)     ->   Dict:







					'''simple docstring'''







					lowerCamelCase								=     ElectraForMaskedLM(config=A  )
					model.to(A  )
					model.eval()
					lowerCamelCase								=     model(A		,			attention_mask=A		,			token_type_ids=A		,			labels=A  )
					self.parent.assertEqual(result.logits.shape		,			(self.batch_size, self.seq_length, self.vocab_size)  )







		def 				__A      (   self		,			A		,			A		,			A		,			A		,			A		,			A		,			A		,			A		,			A		,			)     ->   List[str]:







					'''simple docstring'''







					lowerCamelCase								=     ElectraForCausalLM(config=A  )
					model.to(A  )
					model.eval()
					lowerCamelCase								=     model(A		,			attention_mask=A		,			token_type_ids=A		,			labels=A  )
					self.parent.assertEqual(result.logits.shape		,			(self.batch_size, self.seq_length, self.vocab_size)  )







		def 				__A      (   self		,			A		,			A		,			A		,			A		,			A		,			A		,			A		,			A		,			)     ->   Tuple:







					'''simple docstring'''







					lowerCamelCase								=     self.num_labels
					lowerCamelCase								=     ElectraForTokenClassification(config=A  )
					model.to(A  )
					model.eval()
					lowerCamelCase								=     model(A		,			attention_mask=A		,			token_type_ids=A		,			labels=A  )
					self.parent.assertEqual(result.logits.shape		,			(self.batch_size, self.seq_length, self.num_labels)  )







		def 				__A      (   self		,			A		,			A		,			A		,			A		,			A		,			A		,			A		,			A		,			)     ->   List[Any]:







					'''simple docstring'''







					lowerCamelCase								=     self.num_labels
					lowerCamelCase								=     ElectraForPreTraining(config=A  )
					model.to(A  )
					model.eval()
					lowerCamelCase								=     model(A		,			attention_mask=A		,			token_type_ids=A		,			labels=A  )
					self.parent.assertEqual(result.logits.shape		,			(self.batch_size, self.seq_length)  )







		def 				__A      (   self		,			A		,			A		,			A		,			A		,			A		,			A		,			A		,			A		,			)     ->   List[str]:







					'''simple docstring'''







					lowerCamelCase								=     self.num_labels
					lowerCamelCase								=     ElectraForSequenceClassification(A  )
					model.to(A  )
					model.eval()
					lowerCamelCase								=     model(A		,			attention_mask=A		,			token_type_ids=A		,			labels=A  )
					self.parent.assertEqual(result.logits.shape		,			(self.batch_size, self.num_labels)  )







		def 				__A      (   self		,			A		,			A		,			A		,			A		,			A		,			A		,			A		,			A		,			)     ->   Any:







					'''simple docstring'''







					lowerCamelCase								=     ElectraForQuestionAnswering(config=A  )
					model.to(A  )
					model.eval()
					lowerCamelCase								=     model(
					    A		,			attention_mask=A		,			token_type_ids=A		,			start_positions=A		,			end_positions=A		,			)
					self.parent.assertEqual(result.start_logits.shape		,			(self.batch_size, self.seq_length)  )
					self.parent.assertEqual(result.end_logits.shape		,			(self.batch_size, self.seq_length)  )







		def 				__A      (   self		,			A		,			A		,			A		,			A		,			A		,			A		,			A		,			A		,			)     ->   str:







					'''simple docstring'''







					lowerCamelCase								=     self.num_choices
					lowerCamelCase								=     ElectraForMultipleChoice(config=A  )
					model.to(A  )
					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(
					    A		,			attention_mask=A		,			token_type_ids=A		,			labels=A		,			)
					self.parent.assertEqual(result.logits.shape		,			(self.batch_size, self.num_choices)  )







		def 				__A      (   self  )     ->   List[Any]:







					'''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 					__lowercase					(       a_ ,       a_ ,       unittest.TestCase     ):



		"""simple docstring"""

		UpperCamelCase    :				Dict 					=		(
		    (
		        ElectraModel,
		        ElectraForPreTraining,
		        ElectraForMaskedLM,
		        ElectraForCausalLM,
		        ElectraForMultipleChoice,
		        ElectraForTokenClassification,
		        ElectraForSequenceClassification,
		        ElectraForQuestionAnswering,
		    )
		    if is_torch_available()
		    else ()
		)
		UpperCamelCase    :				Optional[int] 					=		(
		    {
		        "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    :				Dict 					=		True







		def 				__A      (   self		,			A		,			A		,			A=False  )     ->   str:







					'''simple docstring'''







					lowerCamelCase								=     super()._prepare_for_class(A		,			A		,			return_labels=A  )

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







		def 				__A      (   self  )     ->   str:







					'''simple docstring'''







					lowerCamelCase								=     ElectraModelTester(self  )
					lowerCamelCase								=     ConfigTester(self		,			config_class=A		,			hidden_size=37  )







		def 				__A      (   self  )     ->   List[str]:







					'''simple docstring'''







					self.config_tester.run_common_tests()







		def 				__A      (   self  )     ->   int:







					'''simple docstring'''







					lowerCamelCase								=     self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_electra_model(*A  )







		def 				__A      (   self  )     ->   int:







					'''simple docstring'''







					lowerCamelCase								=     self.model_tester.prepare_config_and_inputs_for_decoder()
					self.model_tester.create_and_check_electra_model_as_decoder(*A  )







		def 				__A      (   self  )     ->   Tuple:







					'''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(*A  )







		def 				__A      (   self  )     ->   int:







					'''simple docstring'''







					lowerCamelCase								=     self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_electra_for_masked_lm(*A  )







		def 				__A      (   self  )     ->   Union[str, Any]:







					'''simple docstring'''







					lowerCamelCase								=     self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_electra_for_token_classification(*A  )







		def 				__A      (   self  )     ->   str:







					'''simple docstring'''







					lowerCamelCase								=     self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_electra_for_pretraining(*A  )







		def 				__A      (   self  )     ->   Dict:







					'''simple docstring'''







					lowerCamelCase								=     self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_electra_for_sequence_classification(*A  )







		def 				__A      (   self  )     ->   int:







					'''simple docstring'''







					lowerCamelCase								=     self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_electra_for_question_answering(*A  )







		def 				__A      (   self  )     ->   Union[str, Any]:







					'''simple docstring'''







					lowerCamelCase								=     self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_electra_for_multiple_choice(*A  )







		@slow
		def 				__A      (   self  )     ->   int:







					'''simple docstring'''







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







		def 				__A      (   self  )     ->   str:







					'''simple docstring'''







					lowerCamelCase								=     self.model_tester.prepare_config_and_inputs_for_decoder()
					self.model_tester.create_and_check_electra_for_causal_lm(*A  )







@require_torch
class 					__lowercase					(       unittest.TestCase     ):



		"""simple docstring"""

		@slow
		def 				__A      (   self  )     ->   int:







					'''simple docstring'''







					lowerCamelCase								=     ElectraModel.from_pretrained("""google/electra-small-discriminator"""  )
					lowerCamelCase								=     torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]  )
					lowerCamelCase								=     torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]  )
					lowerCamelCase								=     model(A		,			attention_mask=A  )[0]
					lowerCamelCase								=     torch.Size((1, 11, 2_56)  )
					self.assertEqual(output.shape		,			A  )
					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]		,			A		,			atol=1e-4  )  )





import warnings

from .generation import FlaxGenerationMixin







class 					__lowercase					(       a_     ):



		"""simple docstring"""

		warnings.warn(
		    "Importing `FlaxGenerationMixin` from `src/transformers/generation_flax_utils.py` is deprecated and will "
		    "be removed in Transformers v5. Import as `from transformers import FlaxGenerationMixin` instead." ,       a_ ,       )

