

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      :







						def __init__(			self     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE=13     ,						_SCREAMING_SNAKE_CASE=7     ,						_SCREAMING_SNAKE_CASE=True     ,						_SCREAMING_SNAKE_CASE=True     ,						_SCREAMING_SNAKE_CASE=True     ,						_SCREAMING_SNAKE_CASE=True     ,						_SCREAMING_SNAKE_CASE=99     ,						_SCREAMING_SNAKE_CASE=32     ,						_SCREAMING_SNAKE_CASE=5     ,						_SCREAMING_SNAKE_CASE=4     ,						_SCREAMING_SNAKE_CASE=37     ,						_SCREAMING_SNAKE_CASE="gelu"     ,						_SCREAMING_SNAKE_CASE=0.1     ,						_SCREAMING_SNAKE_CASE=0.1     ,						_SCREAMING_SNAKE_CASE=512     ,						_SCREAMING_SNAKE_CASE=16     ,						_SCREAMING_SNAKE_CASE=2     ,						_SCREAMING_SNAKE_CASE=0.0_2     ,						_SCREAMING_SNAKE_CASE=3     ,						_SCREAMING_SNAKE_CASE=4     ,						_SCREAMING_SNAKE_CASE=None     ,						)->							List[str]:
													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        _snake_case				(			self							)->							Tuple:
													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        _snake_case				(			self							)->							Tuple:
													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        _snake_case				(			self							)->							List[str]:
													(
													    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        _snake_case				(			self     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						)->							Union[str, Any]:
													lowerCamelCase_								=ElectraModel(config=_SCREAMING_SNAKE_CASE							)
													model.to(_SCREAMING_SNAKE_CASE							)
													model.eval()
													lowerCamelCase_								=model(_SCREAMING_SNAKE_CASE     ,						attention_mask=_SCREAMING_SNAKE_CASE     ,						token_type_ids=_SCREAMING_SNAKE_CASE							)
													lowerCamelCase_								=model(_SCREAMING_SNAKE_CASE     ,						token_type_ids=_SCREAMING_SNAKE_CASE							)
													lowerCamelCase_								=model(_SCREAMING_SNAKE_CASE							)
													self.parent.assertEqual(result.last_hidden_state.shape     ,						(self.batch_size, self.seq_length, self.hidden_size)							)







						def        _snake_case				(			self     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						)->							Dict:
													lowerCamelCase_								=True
													lowerCamelCase_								=ElectraModel(_SCREAMING_SNAKE_CASE							)
													model.to(_SCREAMING_SNAKE_CASE							)
													model.eval()
													lowerCamelCase_								=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     ,						)
													lowerCamelCase_								=model(
													    _SCREAMING_SNAKE_CASE     ,						attention_mask=_SCREAMING_SNAKE_CASE     ,						token_type_ids=_SCREAMING_SNAKE_CASE     ,						encoder_hidden_states=_SCREAMING_SNAKE_CASE     ,						)
													lowerCamelCase_								=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        _snake_case				(			self     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						)->							Union[str, Any]:
													lowerCamelCase_								=ElectraForMaskedLM(config=_SCREAMING_SNAKE_CASE							)
													model.to(_SCREAMING_SNAKE_CASE							)
													model.eval()
													lowerCamelCase_								=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        _snake_case				(			self     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						)->							int:
													lowerCamelCase_								=ElectraForCausalLM(config=_SCREAMING_SNAKE_CASE							)
													model.to(_SCREAMING_SNAKE_CASE							)
													model.eval()
													lowerCamelCase_								=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        _snake_case				(			self     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						)->							Optional[Any]:
													lowerCamelCase_								=self.num_labels
													lowerCamelCase_								=ElectraForTokenClassification(config=_SCREAMING_SNAKE_CASE							)
													model.to(_SCREAMING_SNAKE_CASE							)
													model.eval()
													lowerCamelCase_								=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        _snake_case				(			self     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						)->							str:
													lowerCamelCase_								=self.num_labels
													lowerCamelCase_								=ElectraForPreTraining(config=_SCREAMING_SNAKE_CASE							)
													model.to(_SCREAMING_SNAKE_CASE							)
													model.eval()
													lowerCamelCase_								=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        _snake_case				(			self     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						)->							Any:
													lowerCamelCase_								=self.num_labels
													lowerCamelCase_								=ElectraForSequenceClassification(_SCREAMING_SNAKE_CASE							)
													model.to(_SCREAMING_SNAKE_CASE							)
													model.eval()
													lowerCamelCase_								=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        _snake_case				(			self     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						)->							Optional[Any]:
													lowerCamelCase_								=ElectraForQuestionAnswering(config=_SCREAMING_SNAKE_CASE							)
													model.to(_SCREAMING_SNAKE_CASE							)
													model.eval()
													lowerCamelCase_								=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        _snake_case				(			self     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						)->							int:
													lowerCamelCase_								=self.num_choices
													lowerCamelCase_								=ElectraForMultipleChoice(config=_SCREAMING_SNAKE_CASE							)
													model.to(_SCREAMING_SNAKE_CASE							)
													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(
													    _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        _snake_case				(			self							)->							Dict:
													lowerCamelCase_								=self.prepare_config_and_inputs()
													(
													    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 			_SCREAMING_SNAKE_CASE      (  lowerCAmelCase__ ,    lowerCAmelCase__ ,    unittest.TestCase):
						_UpperCamelCase:Optional[Any]	=		(
						    (
						        ElectraModel,
						        ElectraForPreTraining,
						        ElectraForMaskedLM,
						        ElectraForCausalLM,
						        ElectraForMultipleChoice,
						        ElectraForTokenClassification,
						        ElectraForSequenceClassification,
						        ElectraForQuestionAnswering,
						    )
						    if is_torch_available()
						    else ()
						)
						_UpperCamelCase: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 {}
						)
						_UpperCamelCase:List[Any]	=		True







						def        _snake_case				(			self     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE=False							)->							Dict:
													lowerCamelCase_								=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							):
																											lowerCamelCase_								=torch.zeros(
																											    (self.model_tester.batch_size, self.model_tester.seq_length)     ,						dtype=torch.long     ,						device=_SCREAMING_SNAKE_CASE							)
													return inputs_dict







						def        _snake_case				(			self							)->							Optional[Any]:
													lowerCamelCase_								=ElectraModelTester(self							)
													lowerCamelCase_								=ConfigTester(self     ,						config_class=_SCREAMING_SNAKE_CASE     ,						hidden_size=37							)







						def        _snake_case				(			self							)->							Optional[Any]:
													self.config_tester.run_common_tests()







						def        _snake_case				(			self							)->							Dict:
													lowerCamelCase_								=self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_electra_model(*_SCREAMING_SNAKE_CASE							)







						def        _snake_case				(			self							)->							Any:
													lowerCamelCase_								=self.model_tester.prepare_config_and_inputs_for_decoder()
													self.model_tester.create_and_check_electra_model_as_decoder(*_SCREAMING_SNAKE_CASE							)







						def        _snake_case				(			self							)->							int:
													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(*_SCREAMING_SNAKE_CASE							)







						def        _snake_case				(			self							)->							Optional[Any]:
													lowerCamelCase_								=self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_electra_for_masked_lm(*_SCREAMING_SNAKE_CASE							)







						def        _snake_case				(			self							)->							Optional[int]:
													lowerCamelCase_								=self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_electra_for_token_classification(*_SCREAMING_SNAKE_CASE							)







						def        _snake_case				(			self							)->							List[Any]:
													lowerCamelCase_								=self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_electra_for_pretraining(*_SCREAMING_SNAKE_CASE							)







						def        _snake_case				(			self							)->							Optional[int]:
													lowerCamelCase_								=self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_electra_for_sequence_classification(*_SCREAMING_SNAKE_CASE							)







						def        _snake_case				(			self							)->							List[str]:
													lowerCamelCase_								=self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_electra_for_question_answering(*_SCREAMING_SNAKE_CASE							)







						def        _snake_case				(			self							)->							Optional[int]:
													lowerCamelCase_								=self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_electra_for_multiple_choice(*_SCREAMING_SNAKE_CASE							)







						@slow
						def        _snake_case				(			self							)->							Optional[int]:
													for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																				lowerCamelCase_								=ElectraModel.from_pretrained(_SCREAMING_SNAKE_CASE							)
																				self.assertIsNotNone(_SCREAMING_SNAKE_CASE							)







						def        _snake_case				(			self							)->							Dict:
													lowerCamelCase_								=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 			_SCREAMING_SNAKE_CASE      (  unittest.TestCase):







						@slow
						def        _snake_case				(			self							)->							Optional[Any]:
													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(_SCREAMING_SNAKE_CASE     ,						attention_mask=_SCREAMING_SNAKE_CASE							)[0]
													lowerCamelCase_								=torch.Size((1, 11, 256)							)
													self.assertEqual(output.shape     ,						_SCREAMING_SNAKE_CASE							)
													lowerCamelCase_								=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]     ,						_SCREAMING_SNAKE_CASE     ,						atol=1E-4							)							)





def 						_lowerCAmelCase    (					__lowerCamelCase :  list  ):



 """simple docstring"""





 if len(__lowerCamelCase  ) <= 1:
  return data
 else:
  return [
      *quick_sort([e for e in data[1:] if e <= data[0]]  ),
      data[0],
      *quick_sort([e for e in data[1:] if e > data[0]]  ),
  ]


if __name__ == "__main__":
      import doctest

      doctest.testmod()






