



"""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:							Tuple						,					snake_case:							Union[str, Any]						,					snake_case:							Union[str, Any]=13						,					snake_case:							Optional[int]=7						,					snake_case:							Tuple=True						,					snake_case:							Optional[int]=True						,					snake_case:							Optional[Any]=True						,					snake_case:							Tuple=True						,					snake_case:							List[Any]=99						,					snake_case:							Union[str, Any]=32						,					snake_case:							List[Any]=5						,					snake_case:							Dict=4						,					snake_case:							Union[str, Any]=37						,					snake_case:							Optional[Any]="gelu"						,					snake_case:							List[str]=0.1						,					snake_case:							str=0.1						,					snake_case:							List[Any]=512						,					snake_case:							List[Any]=16						,					snake_case:							Any=2						,					snake_case:							List[Any]=0.0_2						,					snake_case:							List[Any]=3						,					snake_case:							Any=4						,					snake_case:							int=None						,					)       ->     List[Any]:
											snake_case_			:Any						=      parent
											snake_case_			:Tuple						=      batch_size
											snake_case_			:str						=      seq_length
											snake_case_			:Optional[int]						=      is_training
											snake_case_			:List[str]						=      use_input_mask
											snake_case_			:int						=      use_token_type_ids
											snake_case_			:Tuple						=      use_labels
											snake_case_			:int						=      vocab_size
											snake_case_			:List[Any]						=      hidden_size
											snake_case_			:Dict						=      num_hidden_layers
											snake_case_			:Optional[Any]						=      num_attention_heads
											snake_case_			:Optional[Any]						=      intermediate_size
											snake_case_			:List[str]						=      hidden_act
											snake_case_			:str						=      hidden_dropout_prob
											snake_case_			:Any						=      attention_probs_dropout_prob
											snake_case_			:Dict						=      max_position_embeddings
											snake_case_			:Optional[Any]						=      type_vocab_size
											snake_case_			:List[Any]						=      type_sequence_label_size
											snake_case_			:Any						=      initializer_range
											snake_case_			:str						=      num_labels
											snake_case_			:Union[str, Any]						=      num_choices
											snake_case_			:Union[str, Any]						=      scope






					def        lowerCAmelCase_    (	self:							Union[str, Any]						)       ->     Optional[Any]:
											snake_case_			:List[str]						=      ids_tensor([self.batch_size, self.seq_length]						,					self.vocab_size						)

											snake_case_			:List[str]						=      None
											if self.use_input_mask:
																	snake_case_			:List[Any]						=      random_attention_mask([self.batch_size, self.seq_length]						)

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

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

											snake_case_			:Optional[Any]						=      self.get_config()

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






					def        lowerCAmelCase_    (	self:							Any						)       ->     Dict:
											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=snake_case						,					initializer_range=self.initializer_range						,					)






					def        lowerCAmelCase_    (	self:							List[Any]						)       ->     Optional[int]:
											(
											    (
											    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_			:List[str]						=      True
											snake_case_			:Dict						=      floats_tensor([self.batch_size, self.seq_length, self.hidden_size]						)
											snake_case_			:Tuple						=      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        lowerCAmelCase_    (	self:							Tuple						,					snake_case:							Union[str, Any]						,					snake_case:							List[str]						,					snake_case:							int						,					snake_case:							Optional[Any]						,					snake_case:							str						,					snake_case:							Dict						,					snake_case:							List[str]						,					snake_case:							Optional[Any]						,					)       ->     List[Any]:
											snake_case_			:str						=      ElectraModel(config=snake_case						)
											model.to(snake_case						)
											model.eval()
											snake_case_			:Optional[Any]						=      model(snake_case						,					attention_mask=snake_case						,					token_type_ids=snake_case						)
											snake_case_			:Optional[int]						=      model(snake_case						,					token_type_ids=snake_case						)
											snake_case_			:List[str]						=      model(snake_case						)
											self.parent.assertEqual(result.last_hidden_state.shape						,					(self.batch_size, self.seq_length, self.hidden_size)						)






					def        lowerCAmelCase_    (	self:							Tuple						,					snake_case:							List[Any]						,					snake_case:							List[str]						,					snake_case:							Optional[Any]						,					snake_case:							List[str]						,					snake_case:							Dict						,					snake_case:							Tuple						,					snake_case:							Optional[int]						,					snake_case:							Optional[Any]						,					snake_case:							Union[str, Any]						,					)       ->     int:
											snake_case_			:Optional[Any]						=      True
											snake_case_			:List[Any]						=      ElectraModel(snake_case						)
											model.to(snake_case						)
											model.eval()
											snake_case_			:Tuple						=      model(
											    snake_case						,					attention_mask=snake_case						,					token_type_ids=snake_case						,					encoder_hidden_states=snake_case						,					encoder_attention_mask=snake_case						,					)
											snake_case_			:Tuple						=      model(
											    snake_case						,					attention_mask=snake_case						,					token_type_ids=snake_case						,					encoder_hidden_states=snake_case						,					)
											snake_case_			:int						=      model(snake_case						,					attention_mask=snake_case						,					token_type_ids=snake_case						)
											self.parent.assertEqual(result.last_hidden_state.shape						,					(self.batch_size, self.seq_length, self.hidden_size)						)






					def        lowerCAmelCase_    (	self:							Tuple						,					snake_case:							int						,					snake_case:							Tuple						,					snake_case:							Union[str, Any]						,					snake_case:							List[Any]						,					snake_case:							List[str]						,					snake_case:							Dict						,					snake_case:							Optional[int]						,					snake_case:							int						,					)       ->     Optional[Any]:
											snake_case_			:List[str]						=      ElectraForMaskedLM(config=snake_case						)
											model.to(snake_case						)
											model.eval()
											snake_case_			:int						=      model(snake_case						,					attention_mask=snake_case						,					token_type_ids=snake_case						,					labels=snake_case						)
											self.parent.assertEqual(result.logits.shape						,					(self.batch_size, self.seq_length, self.vocab_size)						)






					def        lowerCAmelCase_    (	self:							Any						,					snake_case:							Tuple						,					snake_case:							Optional[int]						,					snake_case:							Optional[int]						,					snake_case:							List[Any]						,					snake_case:							int						,					snake_case:							List[str]						,					snake_case:							Union[str, Any]						,					snake_case:							List[Any]						,					snake_case:							int						,					)       ->     Tuple:
											snake_case_			:List[Any]						=      ElectraForCausalLM(config=snake_case						)
											model.to(snake_case						)
											model.eval()
											snake_case_			:Any						=      model(snake_case						,					attention_mask=snake_case						,					token_type_ids=snake_case						,					labels=snake_case						)
											self.parent.assertEqual(result.logits.shape						,					(self.batch_size, self.seq_length, self.vocab_size)						)






					def        lowerCAmelCase_    (	self:							Optional[int]						,					snake_case:							str						,					snake_case:							Union[str, Any]						,					snake_case:							str						,					snake_case:							Optional[Any]						,					snake_case:							List[str]						,					snake_case:							int						,					snake_case:							Any						,					snake_case:							str						,					)       ->     str:
											snake_case_			:List[Any]						=      self.num_labels
											snake_case_			:List[str]						=      ElectraForTokenClassification(config=snake_case						)
											model.to(snake_case						)
											model.eval()
											snake_case_			:str						=      model(snake_case						,					attention_mask=snake_case						,					token_type_ids=snake_case						,					labels=snake_case						)
											self.parent.assertEqual(result.logits.shape						,					(self.batch_size, self.seq_length, self.num_labels)						)






					def        lowerCAmelCase_    (	self:							Any						,					snake_case:							Optional[Any]						,					snake_case:							Optional[Any]						,					snake_case:							int						,					snake_case:							Union[str, Any]						,					snake_case:							Optional[int]						,					snake_case:							int						,					snake_case:							Optional[int]						,					snake_case:							int						,					)       ->     List[str]:
											snake_case_			:Optional[int]						=      self.num_labels
											snake_case_			:Tuple						=      ElectraForPreTraining(config=snake_case						)
											model.to(snake_case						)
											model.eval()
											snake_case_			:Dict						=      model(snake_case						,					attention_mask=snake_case						,					token_type_ids=snake_case						,					labels=snake_case						)
											self.parent.assertEqual(result.logits.shape						,					(self.batch_size, self.seq_length)						)






					def        lowerCAmelCase_    (	self:							Tuple						,					snake_case:							List[str]						,					snake_case:							Tuple						,					snake_case:							List[str]						,					snake_case:							Dict						,					snake_case:							int						,					snake_case:							Optional[int]						,					snake_case:							str						,					snake_case:							Union[str, Any]						,					)       ->     Any:
											snake_case_			:Optional[Any]						=      self.num_labels
											snake_case_			:Union[str, Any]						=      ElectraForSequenceClassification(snake_case						)
											model.to(snake_case						)
											model.eval()
											snake_case_			:str						=      model(snake_case						,					attention_mask=snake_case						,					token_type_ids=snake_case						,					labels=snake_case						)
											self.parent.assertEqual(result.logits.shape						,					(self.batch_size, self.num_labels)						)






					def        lowerCAmelCase_    (	self:							int						,					snake_case:							Optional[Any]						,					snake_case:							Optional[int]						,					snake_case:							Dict						,					snake_case:							Tuple						,					snake_case:							Optional[Any]						,					snake_case:							Optional[int]						,					snake_case:							Tuple						,					snake_case:							Optional[int]						,					)       ->     Optional[Any]:
											snake_case_			:Optional[Any]						=      ElectraForQuestionAnswering(config=snake_case						)
											model.to(snake_case						)
											model.eval()
											snake_case_			:Optional[int]						=      model(
											    snake_case						,					attention_mask=snake_case						,					token_type_ids=snake_case						,					start_positions=snake_case						,					end_positions=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        lowerCAmelCase_    (	self:							Union[str, Any]						,					snake_case:							Union[str, Any]						,					snake_case:							str						,					snake_case:							Any						,					snake_case:							Tuple						,					snake_case:							str						,					snake_case:							Optional[int]						,					snake_case:							Union[str, Any]						,					snake_case:							int						,					)       ->     int:
											snake_case_			:str						=      self.num_choices
											snake_case_			:Tuple						=      ElectraForMultipleChoice(config=snake_case						)
											model.to(snake_case						)
											model.eval()
											snake_case_			:List[str]						=      input_ids.unsqueeze(1						).expand(-1						,					self.num_choices						,					-1						).contiguous()
											snake_case_			:Union[str, Any]						=      token_type_ids.unsqueeze(1						).expand(-1						,					self.num_choices						,					-1						).contiguous()
											snake_case_			:Any						=      input_mask.unsqueeze(1						).expand(-1						,					self.num_choices						,					-1						).contiguous()
											snake_case_			:Dict						=      model(
											    snake_case						,					attention_mask=snake_case						,					token_type_ids=snake_case						,					labels=snake_case						,					)
											self.parent.assertEqual(result.logits.shape						,					(self.batch_size, self.num_choices)						)






					def        lowerCAmelCase_    (	self:							Tuple						)       ->     Any:
											snake_case_			:Optional[Any]						=      self.prepare_config_and_inputs()
											(
											    (
											    snake_case_
											),	(
											    snake_case_
											),	(
											    snake_case_
											),	(
											    snake_case_
											),	(
											    snake_case_
											),	(
											    snake_case_
											),	(
											    snake_case_
											),	(
											    snake_case_
											),	
											)			:List[Any]						=      config_and_inputs
											snake_case_			:Optional[int]						=      {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
											return config, inputs_dict

@require_torch
class     lowerCamelCase  (      _lowerCAmelCase   ,						_lowerCAmelCase   ,						unittest.TestCase    ):






					'''simple docstring'''





					_A		:      Any						=					(
					    (
					        ElectraModel,
					        ElectraForPreTraining,
					        ElectraForMaskedLM,
					        ElectraForCausalLM,
					        ElectraForMultipleChoice,
					        ElectraForTokenClassification,
					        ElectraForSequenceClassification,
					        ElectraForQuestionAnswering,
					    )
					    if is_torch_available()
					    else ()
					)
					_A		:      List[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 {}
					)
					_A		:      Any						=					True






					def        lowerCAmelCase_    (	self:							str						,					snake_case:							Any						,					snake_case:							int						,					snake_case:							str=False						)       ->     Optional[int]:
											snake_case_			:Tuple						=      super()._prepare_for_class(snake_case						,					snake_case						,					return_labels=snake_case						)

											if return_labels:
																	if model_class in get_values(snake_case						):
																							snake_case_			:List[str]						=      torch.zeros(
																							    (self.model_tester.batch_size, self.model_tester.seq_length)						,					dtype=torch.long						,					device=snake_case						)
											return inputs_dict






					def        lowerCAmelCase_    (	self:							Union[str, Any]						)       ->     List[Any]:
											snake_case_			:Any						=      ElectraModelTester(self						)
											snake_case_			:Tuple						=      ConfigTester(self						,					config_class=snake_case						,					hidden_size=37						)






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






					def        lowerCAmelCase_    (	self:							Tuple						)       ->     Any:
											snake_case_			:List[Any]						=      self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_electra_model(*snake_case						)






					def        lowerCAmelCase_    (	self:							Tuple						)       ->     Union[str, Any]:
											snake_case_			:str						=      self.model_tester.prepare_config_and_inputs_for_decoder()
											self.model_tester.create_and_check_electra_model_as_decoder(*snake_case						)






					def        lowerCAmelCase_    (	self:							Tuple						)       ->     List[str]:
											snake_case_			:Union[str, Any]						=      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(*snake_case						)






					def        lowerCAmelCase_    (	self:							str						)       ->     int:
											snake_case_			:Any						=      self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_electra_for_masked_lm(*snake_case						)






					def        lowerCAmelCase_    (	self:							Tuple						)       ->     Union[str, Any]:
											snake_case_			:Optional[int]						=      self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_electra_for_token_classification(*snake_case						)






					def        lowerCAmelCase_    (	self:							List[str]						)       ->     int:
											snake_case_			:int						=      self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_electra_for_pretraining(*snake_case						)






					def        lowerCAmelCase_    (	self:							Tuple						)       ->     Dict:
											snake_case_			:Dict						=      self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_electra_for_sequence_classification(*snake_case						)






					def        lowerCAmelCase_    (	self:							Any						)       ->     Union[str, Any]:
											snake_case_			:List[str]						=      self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_electra_for_question_answering(*snake_case						)






					def        lowerCAmelCase_    (	self:							Union[str, Any]						)       ->     str:
											snake_case_			:Any						=      self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_electra_for_multiple_choice(*snake_case						)






					@slow
					def        lowerCAmelCase_    (	self:							List[str]						)       ->     List[Any]:
											for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	snake_case_			:str						=      ElectraModel.from_pretrained(snake_case						)
																	self.assertIsNotNone(snake_case						)






					def        lowerCAmelCase_    (	self:							Dict						)       ->     Optional[int]:
											snake_case_			:int						=      self.model_tester.prepare_config_and_inputs_for_decoder()
											self.model_tester.create_and_check_electra_for_causal_lm(*snake_case						)

@require_torch
class     lowerCamelCase  (      unittest.TestCase    ):






					'''simple docstring'''





					@slow
					def        lowerCAmelCase_    (	self:							int						)       ->     Optional[int]:
											snake_case_			:List[str]						=      ElectraModel.from_pretrained("""google/electra-small-discriminator"""						)
											snake_case_			:int						=      torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]						)
											snake_case_			:int						=      torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]						)
											snake_case_			:List[str]						=      model(snake_case						,					attention_mask=snake_case						)[0]
											snake_case_			:str						=      torch.Size((1, 11, 256)						)
											self.assertEqual(output.shape						,					snake_case						)
											snake_case_			:Union[str, Any]						=      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]						,					snake_case						,					atol=1E-4						)						)





"""simple docstring"""
from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_torch_available


__a     =    {
    "configuration_xlm_prophetnet": ["XLM_PROPHETNET_PRETRAINED_CONFIG_ARCHIVE_MAP", "XLMProphetNetConfig"],
}

try:
						if not is_sentencepiece_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						__a     =    ["XLMProphetNetTokenizer"]

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						__a     =    [
						    "XLM_PROPHETNET_PRETRAINED_MODEL_ARCHIVE_LIST",
						    "XLMProphetNetDecoder",
						    "XLMProphetNetEncoder",
						    "XLMProphetNetForCausalLM",
						    "XLMProphetNetForConditionalGeneration",
						    "XLMProphetNetModel",
						    "XLMProphetNetPreTrainedModel",
						]


if TYPE_CHECKING:
						from .configuration_xlm_prophetnet import XLM_PROPHETNET_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMProphetNetConfig

						try:
												if not is_sentencepiece_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .tokenization_xlm_prophetnet import XLMProphetNetTokenizer

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_xlm_prophetnet import (
												    XLM_PROPHETNET_PRETRAINED_MODEL_ARCHIVE_LIST,
												    XLMProphetNetDecoder,
												    XLMProphetNetEncoder,
												    XLMProphetNetForCausalLM,
												    XLMProphetNetForConditionalGeneration,
												    XLMProphetNetModel,
												    XLMProphetNetPreTrainedModel,
												)

else:
						import sys

						__a     =    _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
