




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




					'''simple docstring'''






					def __init__(   self,  __UpperCamelCase,  __UpperCamelCase=13,  __UpperCamelCase=7,  __UpperCamelCase=True,  __UpperCamelCase=True,  __UpperCamelCase=True,  __UpperCamelCase=True,  __UpperCamelCase=99,  __UpperCamelCase=32,  __UpperCamelCase=5,  __UpperCamelCase=4,  __UpperCamelCase=37,  __UpperCamelCase="gelu",  __UpperCamelCase=0.1,  __UpperCamelCase=0.1,  __UpperCamelCase=512,  __UpperCamelCase=16,  __UpperCamelCase=2,  __UpperCamelCase=0.02,  __UpperCamelCase=3,  __UpperCamelCase=4,  __UpperCamelCase=None,  ):
							_snake_case     	=	parent
							_snake_case     	=	batch_size
							_snake_case     	=	seq_length
							_snake_case     	=	is_training
							_snake_case     	=	use_input_mask
							_snake_case     	=	use_token_type_ids
							_snake_case     	=	use_labels
							_snake_case     	=	vocab_size
							_snake_case     	=	hidden_size
							_snake_case     	=	num_hidden_layers
							_snake_case     	=	num_attention_heads
							_snake_case     	=	intermediate_size
							_snake_case     	=	hidden_act
							_snake_case     	=	hidden_dropout_prob
							_snake_case     	=	attention_probs_dropout_prob
							_snake_case     	=	max_position_embeddings
							_snake_case     	=	type_vocab_size
							_snake_case     	=	type_sequence_label_size
							_snake_case     	=	initializer_range
							_snake_case     	=	num_labels
							_snake_case     	=	num_choices
							_snake_case     	=	scope






					def     __SCREAMING_SNAKE_CASE     (   self			):
							_snake_case     	=	ids_tensor([self.batch_size, self.seq_length],  self.vocab_size			)

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

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

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

							_snake_case     	=	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			):
							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=__UpperCamelCase,  initializer_range=self.initializer_range,  )






					def     __SCREAMING_SNAKE_CASE     (   self			):
							(
							    (
							    _snake_case   
							)	,   (
							    _snake_case   
							)	,   (
							    _snake_case   
							)	,   (
							    _snake_case   
							)	,   (
							    _snake_case   
							)	,   (
							    _snake_case   
							)	,   (
							    _snake_case   
							)	,   (
							    _snake_case   
							)	,      
							)     	=	self.prepare_config_and_inputs()

							_snake_case     	=	True
							_snake_case     	=	floats_tensor([self.batch_size, self.seq_length, self.hidden_size]			)
							_snake_case     	=	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,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  ):
							_snake_case     	=	ElectraModel(config=__UpperCamelCase			)
							model.to(__UpperCamelCase			)
							model.eval()
							_snake_case     	=	model(__UpperCamelCase,  attention_mask=__UpperCamelCase,  token_type_ids=__UpperCamelCase			)
							_snake_case     	=	model(__UpperCamelCase,  token_type_ids=__UpperCamelCase			)
							_snake_case     	=	model(__UpperCamelCase			)
							self.parent.assertEqual(result.last_hidden_state.shape,  (self.batch_size, self.seq_length, self.hidden_size)			)






					def     __SCREAMING_SNAKE_CASE     (   self,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  ):
							_snake_case     	=	True
							_snake_case     	=	ElectraModel(__UpperCamelCase			)
							model.to(__UpperCamelCase			)
							model.eval()
							_snake_case     	=	model(
							    __UpperCamelCase,  attention_mask=__UpperCamelCase,  token_type_ids=__UpperCamelCase,  encoder_hidden_states=__UpperCamelCase,  encoder_attention_mask=__UpperCamelCase,  )
							_snake_case     	=	model(
							    __UpperCamelCase,  attention_mask=__UpperCamelCase,  token_type_ids=__UpperCamelCase,  encoder_hidden_states=__UpperCamelCase,  )
							_snake_case     	=	model(__UpperCamelCase,  attention_mask=__UpperCamelCase,  token_type_ids=__UpperCamelCase			)
							self.parent.assertEqual(result.last_hidden_state.shape,  (self.batch_size, self.seq_length, self.hidden_size)			)






					def     __SCREAMING_SNAKE_CASE     (   self,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  ):
							_snake_case     	=	ElectraForMaskedLM(config=__UpperCamelCase			)
							model.to(__UpperCamelCase			)
							model.eval()
							_snake_case     	=	model(__UpperCamelCase,  attention_mask=__UpperCamelCase,  token_type_ids=__UpperCamelCase,  labels=__UpperCamelCase			)
							self.parent.assertEqual(result.logits.shape,  (self.batch_size, self.seq_length, self.vocab_size)			)






					def     __SCREAMING_SNAKE_CASE     (   self,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  ):
							_snake_case     	=	ElectraForCausalLM(config=__UpperCamelCase			)
							model.to(__UpperCamelCase			)
							model.eval()
							_snake_case     	=	model(__UpperCamelCase,  attention_mask=__UpperCamelCase,  token_type_ids=__UpperCamelCase,  labels=__UpperCamelCase			)
							self.parent.assertEqual(result.logits.shape,  (self.batch_size, self.seq_length, self.vocab_size)			)






					def     __SCREAMING_SNAKE_CASE     (   self,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  ):
							_snake_case     	=	self.num_labels
							_snake_case     	=	ElectraForTokenClassification(config=__UpperCamelCase			)
							model.to(__UpperCamelCase			)
							model.eval()
							_snake_case     	=	model(__UpperCamelCase,  attention_mask=__UpperCamelCase,  token_type_ids=__UpperCamelCase,  labels=__UpperCamelCase			)
							self.parent.assertEqual(result.logits.shape,  (self.batch_size, self.seq_length, self.num_labels)			)






					def     __SCREAMING_SNAKE_CASE     (   self,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  ):
							_snake_case     	=	self.num_labels
							_snake_case     	=	ElectraForPreTraining(config=__UpperCamelCase			)
							model.to(__UpperCamelCase			)
							model.eval()
							_snake_case     	=	model(__UpperCamelCase,  attention_mask=__UpperCamelCase,  token_type_ids=__UpperCamelCase,  labels=__UpperCamelCase			)
							self.parent.assertEqual(result.logits.shape,  (self.batch_size, self.seq_length)			)






					def     __SCREAMING_SNAKE_CASE     (   self,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  ):
							_snake_case     	=	self.num_labels
							_snake_case     	=	ElectraForSequenceClassification(__UpperCamelCase			)
							model.to(__UpperCamelCase			)
							model.eval()
							_snake_case     	=	model(__UpperCamelCase,  attention_mask=__UpperCamelCase,  token_type_ids=__UpperCamelCase,  labels=__UpperCamelCase			)
							self.parent.assertEqual(result.logits.shape,  (self.batch_size, self.num_labels)			)






					def     __SCREAMING_SNAKE_CASE     (   self,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  ):
							_snake_case     	=	ElectraForQuestionAnswering(config=__UpperCamelCase			)
							model.to(__UpperCamelCase			)
							model.eval()
							_snake_case     	=	model(
							    __UpperCamelCase,  attention_mask=__UpperCamelCase,  token_type_ids=__UpperCamelCase,  start_positions=__UpperCamelCase,  end_positions=__UpperCamelCase,  )
							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,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  ):
							_snake_case     	=	self.num_choices
							_snake_case     	=	ElectraForMultipleChoice(config=__UpperCamelCase			)
							model.to(__UpperCamelCase			)
							model.eval()
							_snake_case     	=	input_ids.unsqueeze(1			).expand(-1,  self.num_choices,  -1			).contiguous()
							_snake_case     	=	token_type_ids.unsqueeze(1			).expand(-1,  self.num_choices,  -1			).contiguous()
							_snake_case     	=	input_mask.unsqueeze(1			).expand(-1,  self.num_choices,  -1			).contiguous()
							_snake_case     	=	model(
							    __UpperCamelCase,  attention_mask=__UpperCamelCase,  token_type_ids=__UpperCamelCase,  labels=__UpperCamelCase,  )
							self.parent.assertEqual(result.logits.shape,  (self.batch_size, self.num_choices)			)






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





@require_torch
class 					lowerCamelCase_							(	UpperCAmelCase_,	UpperCAmelCase_,	unittest.TestCase							):




					'''simple docstring'''






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






					def     __SCREAMING_SNAKE_CASE     (   self,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase=False			):
							_snake_case     	=	super()._prepare_for_class(__UpperCamelCase,  __UpperCamelCase,  return_labels=__UpperCamelCase			)

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






					def     __SCREAMING_SNAKE_CASE     (   self			):
							_snake_case     	=	ElectraModelTester(self			)
							_snake_case     	=	ConfigTester(self,  config_class=__UpperCamelCase,  hidden_size=37			)






					def     __SCREAMING_SNAKE_CASE     (   self			):
							self.config_tester.run_common_tests()






					def     __SCREAMING_SNAKE_CASE     (   self			):
							_snake_case     	=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_electra_model(*__UpperCamelCase			)






					def     __SCREAMING_SNAKE_CASE     (   self			):
							_snake_case     	=	self.model_tester.prepare_config_and_inputs_for_decoder()
							self.model_tester.create_and_check_electra_model_as_decoder(*__UpperCamelCase			)






					def     __SCREAMING_SNAKE_CASE     (   self			):
							_snake_case     	=	self.model_tester.prepare_config_and_inputs()
							for type in ["absolute", "relative_key", "relative_key_query"]:
									_snake_case     	=	type
									self.model_tester.create_and_check_electra_model(*__UpperCamelCase			)






					def     __SCREAMING_SNAKE_CASE     (   self			):
							_snake_case     	=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_electra_for_masked_lm(*__UpperCamelCase			)






					def     __SCREAMING_SNAKE_CASE     (   self			):
							_snake_case     	=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_electra_for_token_classification(*__UpperCamelCase			)






					def     __SCREAMING_SNAKE_CASE     (   self			):
							_snake_case     	=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_electra_for_pretraining(*__UpperCamelCase			)






					def     __SCREAMING_SNAKE_CASE     (   self			):
							_snake_case     	=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_electra_for_sequence_classification(*__UpperCamelCase			)






					def     __SCREAMING_SNAKE_CASE     (   self			):
							_snake_case     	=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_electra_for_question_answering(*__UpperCamelCase			)






					def     __SCREAMING_SNAKE_CASE     (   self			):
							_snake_case     	=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_electra_for_multiple_choice(*__UpperCamelCase			)






					@slow
					def     __SCREAMING_SNAKE_CASE     (   self			):
							for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									_snake_case     	=	ElectraModel.from_pretrained(__UpperCamelCase			)
									self.assertIsNotNone(__UpperCamelCase			)






					def     __SCREAMING_SNAKE_CASE     (   self			):
							_snake_case     	=	self.model_tester.prepare_config_and_inputs_for_decoder()
							self.model_tester.create_and_check_electra_for_causal_lm(*__UpperCamelCase			)





@require_torch
class 					lowerCamelCase_							(	unittest.TestCase							):




					'''simple docstring'''






					@slow
					def     __SCREAMING_SNAKE_CASE     (   self			):
							_snake_case     	=	ElectraModel.from_pretrained("google/electra-small-discriminator"			)
							_snake_case     	=	torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]			)
							_snake_case     	=	torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]			)
							_snake_case     	=	model(__UpperCamelCase,  attention_mask=__UpperCamelCase			)[0]
							_snake_case     	=	torch.Size((1, 11, 256)			)
							self.assertEqual(output.shape,  __UpperCamelCase			)
							_snake_case     	=	torch.tensor(
							    [[[0.44_71, 0.68_21, -0.32_65], [0.46_27, 0.52_55, -0.36_68], [0.45_32, 0.33_13, -0.43_44]]]			)

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











'''simple docstring'''







import os
import tempfile
import unittest
from typing import List

from transformers.models.esm.tokenization_esm import VOCAB_FILES_NAMES, EsmTokenizer
from transformers.testing_utils import require_tokenizers
from transformers.tokenization_utils import PreTrainedTokenizer
from transformers.tokenization_utils_base import PreTrainedTokenizerBase





@require_tokenizers
class 					lowerCamelCase_							(	unittest.TestCase							):




					'''simple docstring'''






					lowercase__   =			EsmTokenizer






					def     __SCREAMING_SNAKE_CASE     (   self			):
							super().setUp()
							_snake_case     	=	tempfile.mkdtemp()
							# fmt: off
							_snake_case     	=	["<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>"]  # noqa: E501
							# fmt: on
							_snake_case     	=	os.path.join(self.tmpdirname,  VOCAB_FILES_NAMES["vocab_file"]			)
							with open(self.vocab_file,  "w",  encoding="utf-8"			) as vocab_writer:
									vocab_writer.write("".join([x + "\n" for x in vocab_tokens]			)			)






					def     __SCREAMING_SNAKE_CASE     (   self,  **__UpperCamelCase			):
							return [self.get_tokenizer(**__UpperCamelCase			)]






					def     __SCREAMING_SNAKE_CASE     (   self,  **__UpperCamelCase			):
							return self.tokenizer_class.from_pretrained(self.tmpdirname,  **__UpperCamelCase			)






					def     __SCREAMING_SNAKE_CASE     (   self			):
							_snake_case     	=	self.tokenizer_class(self.vocab_file			)

							_snake_case     	=	tokenizer.tokenize("LAGVS"			)
							self.assertListEqual(__UpperCamelCase,  ["L", "A", "G", "V", "S"]			)
							self.assertListEqual(tokenizer.convert_tokens_to_ids(__UpperCamelCase			),  [4, 5, 6, 7, 8]			)






					def     __SCREAMING_SNAKE_CASE     (   self			):
							_snake_case     	=	self.tokenizer_class(self.vocab_file			)

							_snake_case     	=	"LAGVS"
							self.assertListEqual(tokenizer.encode(__UpperCamelCase			),  [0, 4, 5, 6, 7, 8, 2]			)






					def     __SCREAMING_SNAKE_CASE     (   self			):
							_snake_case     	=	self.tokenizer_class(self.vocab_file			)

							_snake_case     	=	["LAGVS", "WCB"]
							_snake_case     	=	tokenizer(__UpperCamelCase,  padding=__UpperCamelCase			)["input_ids"]

							self.assertListEqual(__UpperCamelCase,  [[0, 4, 5, 6, 7, 8, 2], [0, 22, 23, 25, 2]]			)






					def     __SCREAMING_SNAKE_CASE     (   self			):
							_snake_case     	=	self.tokenizer_class(self.vocab_file			)

							_snake_case     	=	["LAGVS", "WCB"]
							_snake_case     	=	tokenizer(__UpperCamelCase,  padding=__UpperCamelCase			)["input_ids"]

							self.assertListEqual(__UpperCamelCase,  [[0, 4, 5, 6, 7, 8, 2], [0, 22, 23, 25, 2, 1, 1]]			)






					def     __SCREAMING_SNAKE_CASE     (   self			):
							_snake_case     	=	self.get_tokenizers(fast=__UpperCamelCase			)
							for tokenizer in tokenizers:
									with self.subTest(f'''{tokenizer.__class__.__name__}'''			):
											_snake_case     	=	"<unk>"
											_snake_case     	=	"<mask>"

											_snake_case     	=	tokenizer.tokenize(__UpperCamelCase			)
											_snake_case     	=	tokenizer.tokenize(__UpperCamelCase			)

											self.assertEqual(len(__UpperCamelCase			),  1			)
											self.assertEqual(len(__UpperCamelCase			),  1			)
											self.assertEqual(token_a[0],  __UpperCamelCase			)
											self.assertEqual(token_a[0],  __UpperCamelCase			)





