






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





							"""simple docstring"""





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

												'''simple docstring'''

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




							def 			lowercase		( self:  Dict     ):

												'''simple docstring'''

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

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

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

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

												a__ 							=	self.get_config()

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




							def 			lowercase		( self:  List[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 			lowercase		( self:  Union[str, Any]     ):

												'''simple docstring'''

												(
												    (
												    a__
												)							,(
												    a__
												)							,(
												    a__
												)							,(
												    a__
												)							,(
												    a__
												)							,(
												    a__
												)							,(
												    a__
												)							,(
												    a__
												)							,
												) 							=	self.prepare_config_and_inputs()

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

												'''simple docstring'''

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




							def 			lowercase		( self:  Tuple					,		__A:  str					,		__A:  int					,		__A:  Dict					,		__A:  Any					,		__A:  Tuple					,		__A:  Dict					,		__A:  Dict					,		__A:  Tuple					,		__A:  Tuple					,		):

												'''simple docstring'''

												a__ 							=	True
												a__ 							=	ElectraModel(__A     )
												model.to(__A     )
												model.eval()
												a__ 							=	model(
												    __A					,		attention_mask=__A					,		token_type_ids=__A					,		encoder_hidden_states=__A					,		encoder_attention_mask=__A					,		)
												a__ 							=	model(
												    __A					,		attention_mask=__A					,		token_type_ids=__A					,		encoder_hidden_states=__A					,		)
												a__ 							=	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 			lowercase		( self:  List[str]					,		__A:  Dict					,		__A:  Union[str, Any]					,		__A:  Any					,		__A:  List[Any]					,		__A:  str					,		__A:  Union[str, Any]					,		__A:  Any					,		__A:  List[str]					,		):

												'''simple docstring'''

												a__ 							=	ElectraForMaskedLM(config=__A     )
												model.to(__A     )
												model.eval()
												a__ 							=	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 			lowercase		( self:  List[Any]					,		__A:  List[str]					,		__A:  str					,		__A:  Dict					,		__A:  Any					,		__A:  Dict					,		__A:  Optional[Any]					,		__A:  Any					,		__A:  int					,		__A:  Optional[Any]					,		):

												'''simple docstring'''

												a__ 							=	ElectraForCausalLM(config=__A     )
												model.to(__A     )
												model.eval()
												a__ 							=	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 			lowercase		( self:  Optional[int]					,		__A:  int					,		__A:  Any					,		__A:  int					,		__A:  Any					,		__A:  List[Any]					,		__A:  Optional[int]					,		__A:  Optional[Any]					,		__A:  int					,		):

												'''simple docstring'''

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

												'''simple docstring'''

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




							def 			lowercase		( self:  Any					,		__A:  List[str]					,		__A:  int					,		__A:  Tuple					,		__A:  List[str]					,		__A:  Dict					,		__A:  Optional[int]					,		__A:  Optional[int]					,		__A:  Tuple					,		):

												'''simple docstring'''

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




							def 			lowercase		( self:  List[str]					,		__A:  Union[str, Any]					,		__A:  int					,		__A:  Any					,		__A:  List[str]					,		__A:  Optional[Any]					,		__A:  Any					,		__A:  Any					,		__A:  Dict					,		):

												'''simple docstring'''

												a__ 							=	ElectraForQuestionAnswering(config=__A     )
												model.to(__A     )
												model.eval()
												a__ 							=	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 			lowercase		( self:  List[Any]					,		__A:  Dict					,		__A:  List[Any]					,		__A:  Union[str, Any]					,		__A:  Dict					,		__A:  List[str]					,		__A:  Dict					,		__A:  str					,		__A:  List[str]					,		):

												'''simple docstring'''

												a__ 							=	self.num_choices
												a__ 							=	ElectraForMultipleChoice(config=__A     )
												model.to(__A     )
												model.eval()
												a__ 							=	input_ids.unsqueeze(1     ).expand(-1					,		self.num_choices					,		-1     ).contiguous()
												a__ 							=	token_type_ids.unsqueeze(1     ).expand(-1					,		self.num_choices					,		-1     ).contiguous()
												a__ 							=	input_mask.unsqueeze(1     ).expand(-1					,		self.num_choices					,		-1     ).contiguous()
												a__ 							=	model(
												    __A					,		attention_mask=__A					,		token_type_ids=__A					,		labels=__A					,		)
												self.parent.assertEqual(result.logits.shape					,		(self.batch_size, self.num_choices)     )





							def 			lowercase		( self:  int     ):

												'''simple docstring'''

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







@require_torch
class 							_SCREAMING_SNAKE_CASE    (	__snake_case							,			__snake_case							,			unittest.TestCase				):





							"""simple docstring"""





							_SCREAMING_SNAKE_CASE     =(
							    (
							        ElectraModel,
							        ElectraForPreTraining,
							        ElectraForMaskedLM,
							        ElectraForCausalLM,
							        ElectraForMultipleChoice,
							        ElectraForTokenClassification,
							        ElectraForSequenceClassification,
							        ElectraForQuestionAnswering,
							    )
							    if is_torch_available()
							    else ()
							)
							_SCREAMING_SNAKE_CASE     =(
							    {
							        '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 {}
							)
							_SCREAMING_SNAKE_CASE     =True




							def 			lowercase		( self:  Optional[int]					,		__A:  Optional[Any]					,		__A:  List[Any]					,		__A:  Union[str, Any]=False     ):

												'''simple docstring'''

												a__ 							=	super()._prepare_for_class(__A					,		__A					,		return_labels=__A     )

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




							def 			lowercase		( self:  int     ):

												'''simple docstring'''

												a__ 							=	ElectraModelTester(self     )
												a__ 							=	ConfigTester(self					,		config_class=__A					,		hidden_size=37     )




							def 			lowercase		( self:  Dict     ):

												'''simple docstring'''

												self.config_tester.run_common_tests()




							def 			lowercase		( self:  List[str]     ):

												'''simple docstring'''

												a__ 							=	self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_electra_model(*__A     )




							def 			lowercase		( self:  int     ):

												'''simple docstring'''

												a__ 							=	self.model_tester.prepare_config_and_inputs_for_decoder()
												self.model_tester.create_and_check_electra_model_as_decoder(*__A     )




							def 			lowercase		( self:  Tuple     ):

												'''simple docstring'''

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




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

												'''simple docstring'''

												a__ 							=	self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_electra_for_masked_lm(*__A     )




							def 			lowercase		( self:  List[Any]     ):

												'''simple docstring'''

												a__ 							=	self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_electra_for_token_classification(*__A     )




							def 			lowercase		( self:  Dict     ):

												'''simple docstring'''

												a__ 							=	self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_electra_for_pretraining(*__A     )




							def 			lowercase		( self:  List[str]     ):

												'''simple docstring'''

												a__ 							=	self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_electra_for_sequence_classification(*__A     )




							def 			lowercase		( self:  Tuple     ):

												'''simple docstring'''

												a__ 							=	self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_electra_for_question_answering(*__A     )




							def 			lowercase		( self:  str     ):

												'''simple docstring'''

												a__ 							=	self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_electra_for_multiple_choice(*__A     )




							@slow
							def 			lowercase		( self:  str     ):

												'''simple docstring'''

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





							def 			lowercase		( self:  Optional[int]     ):

												'''simple docstring'''

												a__ 							=	self.model_tester.prepare_config_and_inputs_for_decoder()
												self.model_tester.create_and_check_electra_for_causal_lm(*__A     )







@require_torch
class 							_SCREAMING_SNAKE_CASE    (	unittest.TestCase				):





							"""simple docstring"""





							@slow
							def 			lowercase		( self:  Dict     ):

												'''simple docstring'''

												a__ 							=	ElectraModel.from_pretrained('''google/electra-small-discriminator'''     )
												a__ 							=	torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]     )
												a__ 							=	torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]     )
												a__ 							=	model(__A					,		attention_mask=__A     )[0]
												a__ 							=	torch.Size((1, 11, 256)     )
												self.assertEqual(output.shape					,		__A     )
												a__ 							=	torch.tensor(
												    [[[0.4_4_7_1, 0.6_8_2_1, -0.3_2_6_5], [0.4_6_2_7, 0.5_2_5_5, -0.3_6_6_8], [0.4_5_3_2, 0.3_3_1_3, -0.4_3_4_4]]]     )

												self.assertTrue(torch.allclose(output[:, 1:4, 1:4]					,		__A					,		atol=1e-4     )     )










"""simple docstring"""







from __future__ import annotations

from collections.abc import Iterator
from typing import Any







class 							_SCREAMING_SNAKE_CASE    :





							"""simple docstring"""





							def __init__( self:  Optional[int]					,		__A:  Any     ):

												'''simple docstring'''

												a__ 							=	data
												a__ 							=	None





							def __str__( self:  str     ):

												'''simple docstring'''

												return F'{self.data}'







class 							_SCREAMING_SNAKE_CASE    :





							"""simple docstring"""





							def __init__( self:  int     ):

												'''simple docstring'''

												a__ 							=	None
												a__ 							=	None




							def __iter__( self:  Tuple     ):

												'''simple docstring'''

												a__ 							=	self.front
												while node:
																	yield node.data
																	a__ 							=	node.next




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

												'''simple docstring'''

												return len(tuple(iter(self     )     )     )




							def __str__( self:  Any     ):

												'''simple docstring'''

												return " <- ".join(str(__A     ) for item in self     )




							def 			lowercase		( self:  Any     ):

												'''simple docstring'''

												return len(self     ) == 0




							def 			lowercase		( self:  Tuple					,		__A:  Any     ):

												'''simple docstring'''

												a__ 							=	Node(__A     )
												if self.is_empty():
																	a__ 							=	a__ 							=	node
												else:
																	assert isinstance(self.rear					,		__A     )
																	a__ 							=	node
																	a__ 							=	node




							def 			lowercase		( self:  List[Any]     ):

												'''simple docstring'''

												if self.is_empty():
																	raise IndexError('''dequeue from empty queue'''     )
												assert isinstance(self.front					,		__A     )
												a__ 							=	self.front
												a__ 							=	self.front.next
												if self.front is None:
																	a__ 							=	None
												return node.data





							def 			lowercase		( self:  Dict     ):

												'''simple docstring'''

												a__ 							=	a__ 							=	None


if __name__ == "__main__":
	from doctest import testmod

	testmod()


