
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   :



		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_=512							, A_=16							, A_=2							, A_=0.02							, A_=3							, A_=4							, A_=None							, )    ->   Union[str, Any]:
				"""simple docstring"""
				UpperCamelCase            =	parent
				UpperCamelCase            =	batch_size
				UpperCamelCase            =	seq_length
				UpperCamelCase            =	is_training
				UpperCamelCase            =	use_input_mask
				UpperCamelCase            =	use_token_type_ids
				UpperCamelCase            =	use_labels
				UpperCamelCase            =	vocab_size
				UpperCamelCase            =	hidden_size
				UpperCamelCase            =	num_hidden_layers
				UpperCamelCase            =	num_attention_heads
				UpperCamelCase            =	intermediate_size
				UpperCamelCase            =	hidden_act
				UpperCamelCase            =	hidden_dropout_prob
				UpperCamelCase            =	attention_probs_dropout_prob
				UpperCamelCase            =	max_position_embeddings
				UpperCamelCase            =	type_vocab_size
				UpperCamelCase            =	type_sequence_label_size
				UpperCamelCase            =	initializer_range
				UpperCamelCase            =	num_labels
				UpperCamelCase            =	num_choices
				UpperCamelCase            =	scope



		def __UpperCamelCase					(					self			)    ->   Optional[Any]:
				"""simple docstring"""
				UpperCamelCase            =	ids_tensor([self.batch_size, self.seq_length]							, self.vocab_size			)

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

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

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

				UpperCamelCase            =	self.get_config()

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



		def __UpperCamelCase					(					self			)    ->   int:
				"""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=SCREAMING_SNAKE_CASE_							, initializer_range=self.initializer_range							, )



		def __UpperCamelCase					(					self			)    ->   Tuple:
				"""simple docstring"""
				(
				    (
				    UpperCamelCase				
				)      ,    (
				    UpperCamelCase				
				)      ,    (
				    UpperCamelCase				
				)      ,    (
				    UpperCamelCase				
				)      ,    (
				    UpperCamelCase				
				)      ,    (
				    UpperCamelCase				
				)      ,    (
				    UpperCamelCase				
				)      ,    (
				    UpperCamelCase				
				)      ,    				
				)            =	self.prepare_config_and_inputs()

				UpperCamelCase            =	True
				UpperCamelCase            =	floats_tensor([self.batch_size, self.seq_length, self.hidden_size]			)
				UpperCamelCase            =	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							, A_							, A_							, A_							, A_							, A_							, A_							, A_							, A_							, )    ->   str:
				"""simple docstring"""
				UpperCamelCase            =	ElectraModel(config=SCREAMING_SNAKE_CASE_			)
				model.to(SCREAMING_SNAKE_CASE_			)
				model.eval()
				UpperCamelCase            =	model(SCREAMING_SNAKE_CASE_							, attention_mask=SCREAMING_SNAKE_CASE_							, token_type_ids=SCREAMING_SNAKE_CASE_			)
				UpperCamelCase            =	model(SCREAMING_SNAKE_CASE_							, token_type_ids=SCREAMING_SNAKE_CASE_			)
				UpperCamelCase            =	model(SCREAMING_SNAKE_CASE_			)
				self.parent.assertEqual(result.last_hidden_state.shape							, (self.batch_size, self.seq_length, self.hidden_size)			)



		def __UpperCamelCase					(					self							, A_							, A_							, A_							, A_							, A_							, A_							, A_							, A_							, A_							, )    ->   Optional[Any]:
				"""simple docstring"""
				UpperCamelCase            =	True
				UpperCamelCase            =	ElectraModel(SCREAMING_SNAKE_CASE_			)
				model.to(SCREAMING_SNAKE_CASE_			)
				model.eval()
				UpperCamelCase            =	model(
				    SCREAMING_SNAKE_CASE_							, attention_mask=SCREAMING_SNAKE_CASE_							, token_type_ids=SCREAMING_SNAKE_CASE_							, encoder_hidden_states=SCREAMING_SNAKE_CASE_							, encoder_attention_mask=SCREAMING_SNAKE_CASE_							, )
				UpperCamelCase            =	model(
				    SCREAMING_SNAKE_CASE_							, attention_mask=SCREAMING_SNAKE_CASE_							, token_type_ids=SCREAMING_SNAKE_CASE_							, encoder_hidden_states=SCREAMING_SNAKE_CASE_							, )
				UpperCamelCase            =	model(SCREAMING_SNAKE_CASE_							, attention_mask=SCREAMING_SNAKE_CASE_							, token_type_ids=SCREAMING_SNAKE_CASE_			)
				self.parent.assertEqual(result.last_hidden_state.shape							, (self.batch_size, self.seq_length, self.hidden_size)			)



		def __UpperCamelCase					(					self							, A_							, A_							, A_							, A_							, A_							, A_							, A_							, A_							, )    ->   Optional[int]:
				"""simple docstring"""
				UpperCamelCase            =	ElectraForMaskedLM(config=SCREAMING_SNAKE_CASE_			)
				model.to(SCREAMING_SNAKE_CASE_			)
				model.eval()
				UpperCamelCase            =	model(SCREAMING_SNAKE_CASE_							, attention_mask=SCREAMING_SNAKE_CASE_							, token_type_ids=SCREAMING_SNAKE_CASE_							, labels=SCREAMING_SNAKE_CASE_			)
				self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.seq_length, self.vocab_size)			)



		def __UpperCamelCase					(					self							, A_							, A_							, A_							, A_							, A_							, A_							, A_							, A_							, A_							, )    ->   Union[str, Any]:
				"""simple docstring"""
				UpperCamelCase            =	ElectraForCausalLM(config=SCREAMING_SNAKE_CASE_			)
				model.to(SCREAMING_SNAKE_CASE_			)
				model.eval()
				UpperCamelCase            =	model(SCREAMING_SNAKE_CASE_							, attention_mask=SCREAMING_SNAKE_CASE_							, token_type_ids=SCREAMING_SNAKE_CASE_							, labels=SCREAMING_SNAKE_CASE_			)
				self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.seq_length, self.vocab_size)			)



		def __UpperCamelCase					(					self							, A_							, A_							, A_							, A_							, A_							, A_							, A_							, A_							, )    ->   int:
				"""simple docstring"""
				UpperCamelCase            =	self.num_labels
				UpperCamelCase            =	ElectraForTokenClassification(config=SCREAMING_SNAKE_CASE_			)
				model.to(SCREAMING_SNAKE_CASE_			)
				model.eval()
				UpperCamelCase            =	model(SCREAMING_SNAKE_CASE_							, attention_mask=SCREAMING_SNAKE_CASE_							, token_type_ids=SCREAMING_SNAKE_CASE_							, labels=SCREAMING_SNAKE_CASE_			)
				self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.seq_length, self.num_labels)			)



		def __UpperCamelCase					(					self							, A_							, A_							, A_							, A_							, A_							, A_							, A_							, A_							, )    ->   Optional[int]:
				"""simple docstring"""
				UpperCamelCase            =	self.num_labels
				UpperCamelCase            =	ElectraForPreTraining(config=SCREAMING_SNAKE_CASE_			)
				model.to(SCREAMING_SNAKE_CASE_			)
				model.eval()
				UpperCamelCase            =	model(SCREAMING_SNAKE_CASE_							, attention_mask=SCREAMING_SNAKE_CASE_							, token_type_ids=SCREAMING_SNAKE_CASE_							, labels=SCREAMING_SNAKE_CASE_			)
				self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.seq_length)			)



		def __UpperCamelCase					(					self							, A_							, A_							, A_							, A_							, A_							, A_							, A_							, A_							, )    ->   Union[str, Any]:
				"""simple docstring"""
				UpperCamelCase            =	self.num_labels
				UpperCamelCase            =	ElectraForSequenceClassification(SCREAMING_SNAKE_CASE_			)
				model.to(SCREAMING_SNAKE_CASE_			)
				model.eval()
				UpperCamelCase            =	model(SCREAMING_SNAKE_CASE_							, attention_mask=SCREAMING_SNAKE_CASE_							, token_type_ids=SCREAMING_SNAKE_CASE_							, labels=SCREAMING_SNAKE_CASE_			)
				self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.num_labels)			)



		def __UpperCamelCase					(					self							, A_							, A_							, A_							, A_							, A_							, A_							, A_							, A_							, )    ->   Tuple:
				"""simple docstring"""
				UpperCamelCase            =	ElectraForQuestionAnswering(config=SCREAMING_SNAKE_CASE_			)
				model.to(SCREAMING_SNAKE_CASE_			)
				model.eval()
				UpperCamelCase            =	model(
				    SCREAMING_SNAKE_CASE_							, attention_mask=SCREAMING_SNAKE_CASE_							, token_type_ids=SCREAMING_SNAKE_CASE_							, start_positions=SCREAMING_SNAKE_CASE_							, end_positions=SCREAMING_SNAKE_CASE_							, )
				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							, A_							, A_							, A_							, A_							, A_							, A_							, A_							, A_							, )    ->   List[Any]:
				"""simple docstring"""
				UpperCamelCase            =	self.num_choices
				UpperCamelCase            =	ElectraForMultipleChoice(config=SCREAMING_SNAKE_CASE_			)
				model.to(SCREAMING_SNAKE_CASE_			)
				model.eval()
				UpperCamelCase            =	input_ids.unsqueeze(1			).expand(-1							, self.num_choices							, -1			).contiguous()
				UpperCamelCase            =	token_type_ids.unsqueeze(1			).expand(-1							, self.num_choices							, -1			).contiguous()
				UpperCamelCase            =	input_mask.unsqueeze(1			).expand(-1							, self.num_choices							, -1			).contiguous()
				UpperCamelCase            =	model(
				    SCREAMING_SNAKE_CASE_							, attention_mask=SCREAMING_SNAKE_CASE_							, token_type_ids=SCREAMING_SNAKE_CASE_							, labels=SCREAMING_SNAKE_CASE_							, )
				self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.num_choices)			)



		def __UpperCamelCase					(					self			)    ->   Dict:
				"""simple docstring"""
				UpperCamelCase            =	self.prepare_config_and_inputs()
				(
				    (
				    UpperCamelCase				
				)      ,    (
				    UpperCamelCase				
				)      ,    (
				    UpperCamelCase				
				)      ,    (
				    UpperCamelCase				
				)      ,    (
				    UpperCamelCase				
				)      ,    (
				    UpperCamelCase				
				)      ,    (
				    UpperCamelCase				
				)      ,    (
				    UpperCamelCase				
				)      ,    				
				)            =	config_and_inputs
				UpperCamelCase            =	{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
				return config, inputs_dict






@require_torch
class 							lowercase   (						SCREAMING_SNAKE_CASE_    ,  SCREAMING_SNAKE_CASE_    ,  unittest.TestCase  ):
		__lowercase			:			Union[str, Any]      	=							(
		    (
		        ElectraModel,
		        ElectraForPreTraining,
		        ElectraForMaskedLM,
		        ElectraForCausalLM,
		        ElectraForMultipleChoice,
		        ElectraForTokenClassification,
		        ElectraForSequenceClassification,
		        ElectraForQuestionAnswering,
		    )
		    if is_torch_available()
		    else ()
		)
		__lowercase			:			List[str]      	=							(
		    {
		        "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 {}
		)
		__lowercase			:			Optional[Any]      	=							True



		def __UpperCamelCase					(					self							, A_							, A_							, A_=False			)    ->   Optional[Any]:
				"""simple docstring"""
				UpperCamelCase            =	super()._prepare_for_class(SCREAMING_SNAKE_CASE_							, SCREAMING_SNAKE_CASE_							, return_labels=SCREAMING_SNAKE_CASE_			)

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



		def __UpperCamelCase					(					self			)    ->   Optional[int]:
				"""simple docstring"""
				UpperCamelCase            =	ElectraModelTester(self			)
				UpperCamelCase            =	ConfigTester(self							, config_class=SCREAMING_SNAKE_CASE_							, hidden_size=37			)



		def __UpperCamelCase					(					self			)    ->   List[Any]:
				"""simple docstring"""
				self.config_tester.run_common_tests()



		def __UpperCamelCase					(					self			)    ->   Optional[Any]:
				"""simple docstring"""
				UpperCamelCase            =	self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_electra_model(*SCREAMING_SNAKE_CASE_			)



		def __UpperCamelCase					(					self			)    ->   List[Any]:
				"""simple docstring"""
				UpperCamelCase            =	self.model_tester.prepare_config_and_inputs_for_decoder()
				self.model_tester.create_and_check_electra_model_as_decoder(*SCREAMING_SNAKE_CASE_			)



		def __UpperCamelCase					(					self			)    ->   Tuple:
				"""simple docstring"""
				UpperCamelCase            =	self.model_tester.prepare_config_and_inputs()
				for type in ["absolute", "relative_key", "relative_key_query"]:
						UpperCamelCase            =	type
						self.model_tester.create_and_check_electra_model(*SCREAMING_SNAKE_CASE_			)



		def __UpperCamelCase					(					self			)    ->   Union[str, Any]:
				"""simple docstring"""
				UpperCamelCase            =	self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_electra_for_masked_lm(*SCREAMING_SNAKE_CASE_			)



		def __UpperCamelCase					(					self			)    ->   Tuple:
				"""simple docstring"""
				UpperCamelCase            =	self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_electra_for_token_classification(*SCREAMING_SNAKE_CASE_			)



		def __UpperCamelCase					(					self			)    ->   Optional[Any]:
				"""simple docstring"""
				UpperCamelCase            =	self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_electra_for_pretraining(*SCREAMING_SNAKE_CASE_			)



		def __UpperCamelCase					(					self			)    ->   Optional[Any]:
				"""simple docstring"""
				UpperCamelCase            =	self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_electra_for_sequence_classification(*SCREAMING_SNAKE_CASE_			)



		def __UpperCamelCase					(					self			)    ->   List[Any]:
				"""simple docstring"""
				UpperCamelCase            =	self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_electra_for_question_answering(*SCREAMING_SNAKE_CASE_			)



		def __UpperCamelCase					(					self			)    ->   List[str]:
				"""simple docstring"""
				UpperCamelCase            =	self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_electra_for_multiple_choice(*SCREAMING_SNAKE_CASE_			)



		@slow
		def __UpperCamelCase					(					self			)    ->   Any:
				"""simple docstring"""






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



		def __UpperCamelCase					(					self			)    ->   Optional[Any]:
				"""simple docstring"""
				UpperCamelCase            =	self.model_tester.prepare_config_and_inputs_for_decoder()
				self.model_tester.create_and_check_electra_for_causal_lm(*SCREAMING_SNAKE_CASE_			)






@require_torch
class 							lowercase   (						unittest.TestCase  ):



		@slow
		def __UpperCamelCase					(					self			)    ->   str:
				"""simple docstring"""
				UpperCamelCase            =	ElectraModel.from_pretrained('google/electra-small-discriminator'			)
				UpperCamelCase            =	torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]			)
				UpperCamelCase            =	torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]			)
				UpperCamelCase            =	model(SCREAMING_SNAKE_CASE_							, attention_mask=SCREAMING_SNAKE_CASE_			)[0]
				UpperCamelCase            =	torch.Size((1, 11, 256)			)
				self.assertEqual(output.shape							, SCREAMING_SNAKE_CASE_			)
				UpperCamelCase            =	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]							, SCREAMING_SNAKE_CASE_							, atol=1e-4			)			)


def 						A_			(				snake_case				: Union[str, Any]			,   snake_case				: Dict			)   ->     Dict:



						'''simple docstring'''

						__UpperCamelCase  	=      (boundary[1] - boundary[0]) / steps
						__UpperCamelCase  	=      boundary[0]
						__UpperCamelCase  	=      boundary[1]
						__UpperCamelCase  	=      make_points(snake_case			,   snake_case			,   snake_case			)
						__UpperCamelCase  	=      0.0
						y += (h / 3.0) * f(snake_case			)
						__UpperCamelCase  	=      2
						for i in x_i:
												y += (h / 3) * (4 - 2 * (cnt % 2)) * f(snake_case			)
												cnt += 1
						y += (h / 3.0) * f(snake_case			)
						return y




def 						A_			(				snake_case				: str			,   snake_case				: Optional[int]			,   snake_case				: Any			)   ->     Optional[int]:



						'''simple docstring'''

						__UpperCamelCase  	=      a + h
						while x < (b - h):
												yield x
												__UpperCamelCase  	=      x + h




def 						A_			(				snake_case				: Union[str, Any]			)   ->     str:  # enter your function here



						'''simple docstring'''

						__UpperCamelCase  	=      (x - 0) * (x - 0)
						return y




def 						A_			(				)   ->     Dict:



						'''simple docstring'''

						__UpperCamelCase  	=      0.0  # Lower bound of integration
						__UpperCamelCase  	=      1.0  # Upper bound of integration
						__UpperCamelCase  	=      10.0  # define number of steps or resolution
						__UpperCamelCase  	=      [a, b]  # define boundary of integration
						__UpperCamelCase  	=      method_a(snake_case			,   snake_case			)
						print(f"y = {y}"			)


if __name__ == "__main__":
				main()




