


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






		'''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							,			):





			'''simple docstring'''

			__snake_case      :		int         =							parent
			__snake_case      :		Optional[int]         =							batch_size
			__snake_case      :		List[str]         =							seq_length
			__snake_case      :		List[str]         =							is_training
			__snake_case      :		Optional[int]         =							use_input_mask
			__snake_case      :		Any         =							use_token_type_ids
			__snake_case      :		List[Any]         =							use_labels
			__snake_case      :		Tuple         =							vocab_size
			__snake_case      :		int         =							hidden_size
			__snake_case      :		Optional[Any]         =							num_hidden_layers
			__snake_case      :		Any         =							num_attention_heads
			__snake_case      :		Dict         =							intermediate_size
			__snake_case      :		int         =							hidden_act
			__snake_case      :		Tuple         =							hidden_dropout_prob
			__snake_case      :		Union[str, Any]         =							attention_probs_dropout_prob
			__snake_case      :		Optional[Any]         =							max_position_embeddings
			__snake_case      :		List[Any]         =							type_vocab_size
			__snake_case      :		str         =							type_sequence_label_size
			__snake_case      :		Optional[int]         =							initializer_range
			__snake_case      :		Optional[int]         =							num_labels
			__snake_case      :		List[str]         =							num_choices
			__snake_case      :		Optional[int]         =							scope



		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			__snake_case      :		Optional[Any]         =							ids_tensor([self.batch_size, self.seq_length]							,			self.vocab_size   )

			__snake_case      :		str         =							None
			if self.use_input_mask:
				__snake_case      :		int         =							random_attention_mask([self.batch_size, self.seq_length]   )

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

			__snake_case      :		Dict         =							None
			__snake_case      :		List[Any]         =							None
			__snake_case      :		Union[str, Any]         =							None
			if self.use_labels:
				__snake_case      :		int         =							ids_tensor([self.batch_size]							,			self.type_sequence_label_size   )
				__snake_case      :		List[Any]         =							ids_tensor([self.batch_size, self.seq_length]							,			self.num_labels   )
				__snake_case      :		int         =							ids_tensor([self.batch_size]							,			self.num_choices   )
				__snake_case      :		int         =							ids_tensor([self.batch_size, self.seq_length]							,			1   )

			__snake_case      :		Any         =							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   ):





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





			'''simple docstring'''

			(
			    (
			    __snake_case
			)      ,				(
			    __snake_case
			)      ,				(
			    __snake_case
			)      ,				(
			    __snake_case
			)      ,				(
			    __snake_case
			)      ,				(
			    __snake_case
			)      ,				(
			    __snake_case
			)      ,				(
			    __snake_case
			)      ,				
			)      :		Optional[Any]         =							self.prepare_config_and_inputs()

			__snake_case      :		Tuple         =							True
			__snake_case      :		List[Any]         =							floats_tensor([self.batch_size, self.seq_length, self.hidden_size]   )
			__snake_case      :		Optional[int]         =							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							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_							,			):





			'''simple docstring'''

			__snake_case      :		Optional[int]         =							ElectraModel(config=a_   )
			model.to(a_   )
			model.eval()
			__snake_case      :		str         =							model(a_							,			attention_mask=a_							,			token_type_ids=a_   )
			__snake_case      :		Optional[int]         =							model(a_							,			token_type_ids=a_   )
			__snake_case      :		Tuple         =							model(a_   )
			self.parent.assertEqual(result.last_hidden_state.shape							,			(self.batch_size, self.seq_length, self.hidden_size)   )



		def 							SCREAMING_SNAKE_CASE    (self							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_							,			):





			'''simple docstring'''

			__snake_case      :		int         =							True
			__snake_case      :		Union[str, Any]         =							ElectraModel(a_   )
			model.to(a_   )
			model.eval()
			__snake_case      :		Union[str, Any]         =							model(
			    a_							,			attention_mask=a_							,			token_type_ids=a_							,			encoder_hidden_states=a_							,			encoder_attention_mask=a_							,			)
			__snake_case      :		List[Any]         =							model(
			    a_							,			attention_mask=a_							,			token_type_ids=a_							,			encoder_hidden_states=a_							,			)
			__snake_case      :		Optional[Any]         =							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 							SCREAMING_SNAKE_CASE    (self							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_							,			):





			'''simple docstring'''

			__snake_case      :		List[Any]         =							ElectraForMaskedLM(config=a_   )
			model.to(a_   )
			model.eval()
			__snake_case      :		Tuple         =							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 							SCREAMING_SNAKE_CASE    (self							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_							,			):





			'''simple docstring'''

			__snake_case      :		List[str]         =							ElectraForCausalLM(config=a_   )
			model.to(a_   )
			model.eval()
			__snake_case      :		List[str]         =							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 							SCREAMING_SNAKE_CASE    (self							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_							,			):





			'''simple docstring'''

			__snake_case      :		Any         =							self.num_labels
			__snake_case      :		List[str]         =							ElectraForTokenClassification(config=a_   )
			model.to(a_   )
			model.eval()
			__snake_case      :		Optional[int]         =							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 							SCREAMING_SNAKE_CASE    (self							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_							,			):





			'''simple docstring'''

			__snake_case      :		str         =							self.num_labels
			__snake_case      :		List[Any]         =							ElectraForPreTraining(config=a_   )
			model.to(a_   )
			model.eval()
			__snake_case      :		Dict         =							model(a_							,			attention_mask=a_							,			token_type_ids=a_							,			labels=a_   )
			self.parent.assertEqual(result.logits.shape							,			(self.batch_size, self.seq_length)   )



		def 							SCREAMING_SNAKE_CASE    (self							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_							,			):





			'''simple docstring'''

			__snake_case      :		List[str]         =							self.num_labels
			__snake_case      :		Dict         =							ElectraForSequenceClassification(a_   )
			model.to(a_   )
			model.eval()
			__snake_case      :		int         =							model(a_							,			attention_mask=a_							,			token_type_ids=a_							,			labels=a_   )
			self.parent.assertEqual(result.logits.shape							,			(self.batch_size, self.num_labels)   )



		def 							SCREAMING_SNAKE_CASE    (self							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_							,			):





			'''simple docstring'''

			__snake_case      :		Union[str, Any]         =							ElectraForQuestionAnswering(config=a_   )
			model.to(a_   )
			model.eval()
			__snake_case      :		Optional[int]         =							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 							SCREAMING_SNAKE_CASE    (self							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_							,			):





			'''simple docstring'''

			__snake_case      :		Optional[Any]         =							self.num_choices
			__snake_case      :		int         =							ElectraForMultipleChoice(config=a_   )
			model.to(a_   )
			model.eval()
			__snake_case      :		Tuple         =							input_ids.unsqueeze(1   ).expand(-1							,			self.num_choices							,			-1   ).contiguous()
			__snake_case      :		Any         =							token_type_ids.unsqueeze(1   ).expand(-1							,			self.num_choices							,			-1   ).contiguous()
			__snake_case      :		Optional[int]         =							input_mask.unsqueeze(1   ).expand(-1							,			self.num_choices							,			-1   ).contiguous()
			__snake_case      :		int         =							model(
			    a_							,			attention_mask=a_							,			token_type_ids=a_							,			labels=a_							,			)
			self.parent.assertEqual(result.logits.shape							,			(self.batch_size, self.num_choices)   )



		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			__snake_case      :		Optional[int]         =							self.prepare_config_and_inputs()
			(
			    (
			    __snake_case
			)      ,				(
			    __snake_case
			)      ,				(
			    __snake_case
			)      ,				(
			    __snake_case
			)      ,				(
			    __snake_case
			)      ,				(
			    __snake_case
			)      ,				(
			    __snake_case
			)      ,				(
			    __snake_case
			)      ,				
			)      :		List[str]         =							config_and_inputs
			__snake_case      :		int         =							{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
			return config, inputs_dict



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






		'''simple docstring'''

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



		def 							SCREAMING_SNAKE_CASE    (self							,			a_							,			a_							,			a_=False   ):





			'''simple docstring'''

			__snake_case      :		List[str]         =							super()._prepare_for_class(a_							,			a_							,			return_labels=a_   )

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



		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			__snake_case      :		int         =							ElectraModelTester(self   )
			__snake_case      :		Optional[Any]         =							ConfigTester(self							,			config_class=a_							,			hidden_size=37   )



		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			self.config_tester.run_common_tests()



		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			__snake_case      :		List[Any]         =							self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_electra_model(*a_   )



		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			__snake_case      :		Tuple         =							self.model_tester.prepare_config_and_inputs_for_decoder()
			self.model_tester.create_and_check_electra_model_as_decoder(*a_   )



		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			__snake_case      :		Dict         =							self.model_tester.prepare_config_and_inputs()
			for type in ["absolute", "relative_key", "relative_key_query"]:
				__snake_case      :		Union[str, Any]         =							type
				self.model_tester.create_and_check_electra_model(*a_   )



		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			__snake_case      :		Optional[Any]         =							self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_electra_for_masked_lm(*a_   )



		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			__snake_case      :		Union[str, Any]         =							self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_electra_for_token_classification(*a_   )



		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			__snake_case      :		List[Any]         =							self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_electra_for_pretraining(*a_   )



		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			__snake_case      :		Optional[int]         =							self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_electra_for_sequence_classification(*a_   )



		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			__snake_case      :		str         =							self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_electra_for_question_answering(*a_   )



		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			__snake_case      :		Dict         =							self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_electra_for_multiple_choice(*a_   )



		@slow
		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
				__snake_case      :		Any         =							ElectraModel.from_pretrained(a_   )
				self.assertIsNotNone(a_   )



		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			__snake_case      :		Any         =							self.model_tester.prepare_config_and_inputs_for_decoder()
			self.model_tester.create_and_check_electra_for_causal_lm(*a_   )



@require_torch
class      _UpperCAmelCase					(				unittest.TestCase							):






		'''simple docstring'''

		@slow
		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			__snake_case      :		str         =							ElectraModel.from_pretrained('''google/electra-small-discriminator'''   )
			__snake_case      :		Optional[int]         =							torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]   )
			__snake_case      :		Optional[Any]         =							torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]   )
			__snake_case      :		int         =							model(a_							,			attention_mask=a_   )[0]
			__snake_case      :		str         =							torch.Size((1, 11, 2_56)   )
			self.assertEqual(output.shape							,			a_   )
			__snake_case      :		Any         =							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   )   )













"""simple docstring"""

import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device


if is_torch_available():
			import torch

			from transformers import CamembertModel

@require_torch
@require_sentencepiece
@require_tokenizers
class     A_				(   unittest.TestCase	):







				@slow
				def      _snake_case       (				self				:    Tuple						)					->       Dict:
										__magic_name__      =  CamembertModel.from_pretrained("camembert-base"						)
										model.to(__lowerCamelCase						)

										__magic_name__      =  torch.tensor(
										    [[5, 1_2_1, 1_1, 6_6_0, 1_6, 7_3_0, 2_5_5_4_3, 1_1_0, 8_3, 6]]    ,     device=__lowerCamelCase    ,     dtype=torch.long    ,     )  # J'aime le camembert !
										with torch.no_grad():
																__magic_name__      =  model(__lowerCamelCase						)["last_hidden_state"]
										__magic_name__      =  torch.Size((1, 1_0, 7_6_8)						)
										self.assertEqual(output.shape    ,     __lowerCamelCase						)
										# compare the actual values for a slice.
										__magic_name__      =  torch.tensor(
										    [[[-0.0254, 0.0235, 0.1027], [0.0606, -0.1811, -0.0418], [-0.1561, -0.1127, 0.2687]]]    ,     device=__lowerCamelCase    ,     dtype=torch.float    ,     )
										# camembert = torch.hub.load('pytorch/fairseq', 'camembert.v0')
										# camembert.eval()
										# expected_slice = roberta.model.forward(input_ids)[0][:, :3, :3].detach()

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



