



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       :



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



						"""simple docstring"""






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



			def        _lowerCamelCase			(		self  :    List[Any])  ->       Dict:



						"""simple docstring"""






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

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

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

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

						_UpperCAmelCase									=  self.get_config()

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



			def        _lowerCamelCase			(		self  :    int)  ->       int:



						"""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        _lowerCamelCase			(		self  :    Any)  ->       Optional[int]:



						"""simple docstring"""






						(
						    (
						    _UpperCAmelCase
						)			,	(
						    _UpperCAmelCase
						)			,	(
						    _UpperCAmelCase
						)			,	(
						    _UpperCAmelCase
						)			,	(
						    _UpperCAmelCase
						)			,	(
						    _UpperCAmelCase
						)			,	(
						    _UpperCAmelCase
						)			,	(
						    _UpperCAmelCase
						)			,	
						)									=  self.prepare_config_and_inputs()

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



						"""simple docstring"""






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



			def        _lowerCamelCase			(		self  :    Optional[Any]					,						A  :    List[str]					,						A  :    str					,						A  :    Optional[int]					,						A  :    Tuple					,						A  :    Optional[Any]					,						A  :    int					,						A  :    Optional[int]					,						A  :    int					,						A  :    Tuple					,						)  ->       Any:



						"""simple docstring"""






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



						"""simple docstring"""






						_UpperCAmelCase									=  ElectraForMaskedLM(config=A)
						model.to(A)
						model.eval()
						_UpperCAmelCase									=  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        _lowerCamelCase			(		self  :    int					,						A  :    Optional[Any]					,						A  :    Union[str, Any]					,						A  :    List[Any]					,						A  :    List[str]					,						A  :    int					,						A  :    int					,						A  :    int					,						A  :    int					,						A  :    Tuple					,						)  ->       Dict:



						"""simple docstring"""






						_UpperCAmelCase									=  ElectraForCausalLM(config=A)
						model.to(A)
						model.eval()
						_UpperCAmelCase									=  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        _lowerCamelCase			(		self  :    Dict					,						A  :    str					,						A  :    int					,						A  :    Any					,						A  :    List[Any]					,						A  :    Optional[int]					,						A  :    int					,						A  :    List[Any]					,						A  :    List[str]					,						)  ->       str:



						"""simple docstring"""






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



						"""simple docstring"""






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



			def        _lowerCamelCase			(		self  :    Optional[int]					,						A  :    Optional[int]					,						A  :    Optional[Any]					,						A  :    str					,						A  :    Dict					,						A  :    Any					,						A  :    Tuple					,						A  :    Optional[Any]					,						A  :    Union[str, Any]					,						)  ->       List[str]:



						"""simple docstring"""






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



			def        _lowerCamelCase			(		self  :    Tuple					,						A  :    Dict					,						A  :    Optional[int]					,						A  :    Optional[int]					,						A  :    List[str]					,						A  :    Optional[int]					,						A  :    Optional[Any]					,						A  :    int					,						A  :    List[str]					,						)  ->       Any:



						"""simple docstring"""






						_UpperCAmelCase									=  ElectraForQuestionAnswering(config=A)
						model.to(A)
						model.eval()
						_UpperCAmelCase									=  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        _lowerCamelCase			(		self  :    str					,						A  :    Dict					,						A  :    List[Any]					,						A  :    Tuple					,						A  :    Tuple					,						A  :    Optional[int]					,						A  :    Optional[int]					,						A  :    int					,						A  :    Optional[Any]					,						)  ->       List[str]:



						"""simple docstring"""






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



			def        _lowerCamelCase			(		self  :    Union[str, Any])  ->       Any:



						"""simple docstring"""






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







@require_torch
class      __lowerCAmelCase       (     A							,		A							,		unittest.TestCase		):
			UpperCamelCase     =	(
			    (
			        ElectraModel,
			        ElectraForPreTraining,
			        ElectraForMaskedLM,
			        ElectraForCausalLM,
			        ElectraForMultipleChoice,
			        ElectraForTokenClassification,
			        ElectraForSequenceClassification,
			        ElectraForQuestionAnswering,
			    )
			    if is_torch_available()
			    else ()
			)
			UpperCamelCase     =	(
			    {
			        '''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     =	True



			def        _lowerCamelCase			(		self  :    Any					,						A  :    List[Any]					,						A  :    Union[str, Any]					,						A  :    Any=False)  ->       List[str]:



						"""simple docstring"""






						_UpperCAmelCase									=  super()._prepare_for_class(A					,						A					,						return_labels=A)

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



			def        _lowerCamelCase			(		self  :    List[Any])  ->       str:



						"""simple docstring"""






						_UpperCAmelCase									=  ElectraModelTester(self)
						_UpperCAmelCase									=  ConfigTester(self					,						config_class=A					,						hidden_size=37)



			def        _lowerCamelCase			(		self  :    int)  ->       Union[str, Any]:



						"""simple docstring"""






						self.config_tester.run_common_tests()



			def        _lowerCamelCase			(		self  :    Union[str, Any])  ->       str:



						"""simple docstring"""






						_UpperCAmelCase									=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_electra_model(*A)



			def        _lowerCamelCase			(		self  :    Union[str, Any])  ->       List[Any]:



						"""simple docstring"""






						_UpperCAmelCase									=  self.model_tester.prepare_config_and_inputs_for_decoder()
						self.model_tester.create_and_check_electra_model_as_decoder(*A)



			def        _lowerCamelCase			(		self  :    Tuple)  ->       List[str]:



						"""simple docstring"""






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



			def        _lowerCamelCase			(		self  :    str)  ->       Optional[int]:



						"""simple docstring"""






						_UpperCAmelCase									=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_electra_for_masked_lm(*A)



			def        _lowerCamelCase			(		self  :    Union[str, Any])  ->       Dict:



						"""simple docstring"""






						_UpperCAmelCase									=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_electra_for_token_classification(*A)



			def        _lowerCamelCase			(		self  :    Tuple)  ->       Dict:



						"""simple docstring"""






						_UpperCAmelCase									=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_electra_for_pretraining(*A)



			def        _lowerCamelCase			(		self  :    List[str])  ->       int:



						"""simple docstring"""






						_UpperCAmelCase									=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_electra_for_sequence_classification(*A)



			def        _lowerCamelCase			(		self  :    Union[str, Any])  ->       List[Any]:



						"""simple docstring"""






						_UpperCAmelCase									=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_electra_for_question_answering(*A)



			def        _lowerCamelCase			(		self  :    List[str])  ->       Optional[int]:



						"""simple docstring"""






						_UpperCAmelCase									=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_electra_for_multiple_choice(*A)



			@slow
			def        _lowerCamelCase			(		self  :    Optional[Any])  ->       Union[str, Any]:



						"""simple docstring"""






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



			def        _lowerCamelCase			(		self  :    str)  ->       Union[str, Any]:



						"""simple docstring"""






						_UpperCAmelCase									=  self.model_tester.prepare_config_and_inputs_for_decoder()
						self.model_tester.create_and_check_electra_for_causal_lm(*A)







@require_torch
class      __lowerCAmelCase       (     unittest.TestCase		):



			@slow
			def        _lowerCamelCase			(		self  :    Any)  ->       int:



						"""simple docstring"""






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





from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_torch_available,
)


UpperCAmelCase__  =			{
    "configuration_align": [
        "ALIGN_PRETRAINED_CONFIG_ARCHIVE_MAP",
        "AlignConfig",
        "AlignTextConfig",
        "AlignVisionConfig",
    ],
    "processing_align": ["AlignProcessor"],
}

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							UpperCAmelCase__  =			[
							    "ALIGN_PRETRAINED_MODEL_ARCHIVE_LIST",
							    "AlignModel",
							    "AlignPreTrainedModel",
							    "AlignTextModel",
							    "AlignVisionModel",
							]

if TYPE_CHECKING:
							from .configuration_align import (
							    ALIGN_PRETRAINED_CONFIG_ARCHIVE_MAP,
							    AlignConfig,
							    AlignTextConfig,
							    AlignVisionConfig,
							)
							from .processing_align import AlignProcessor

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_align import (
														    ALIGN_PRETRAINED_MODEL_ARCHIVE_LIST,
														    AlignModel,
														    AlignPreTrainedModel,
														    AlignTextModel,
														    AlignVisionModel,
														)

else:
							import sys

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