




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




				'''simple docstring'''





				def __init__(self			,  _UpperCAmelCase			,  _UpperCAmelCase=1_3			,  _UpperCAmelCase=7			,  _UpperCAmelCase=True			,  _UpperCAmelCase=True			,  _UpperCAmelCase=True			,  _UpperCAmelCase=True			,  _UpperCAmelCase=9_9			,  _UpperCAmelCase=3_2			,  _UpperCAmelCase=5			,  _UpperCAmelCase=4			,  _UpperCAmelCase=3_7			,  _UpperCAmelCase="gelu"			,  _UpperCAmelCase=0.1			,  _UpperCAmelCase=0.1			,  _UpperCAmelCase=5_1_2			,  _UpperCAmelCase=1_6			,  _UpperCAmelCase=2			,  _UpperCAmelCase=0.02			,  _UpperCAmelCase=3			,  _UpperCAmelCase=4			,  _UpperCAmelCase=None			,  )     ->	Dict:
							__UpperCamelCase				:      Optional[Any]									=       parent
							__UpperCamelCase				:      Tuple									=       batch_size
							__UpperCamelCase				:      str									=       seq_length
							__UpperCamelCase				:      Any									=       is_training
							__UpperCamelCase				:      List[str]									=       use_input_mask
							__UpperCamelCase				:      Union[str, Any]									=       use_token_type_ids
							__UpperCamelCase				:      Dict									=       use_labels
							__UpperCamelCase				:      Any									=       vocab_size
							__UpperCamelCase				:      Optional[int]									=       hidden_size
							__UpperCamelCase				:      str									=       num_hidden_layers
							__UpperCamelCase				:      Tuple									=       num_attention_heads
							__UpperCamelCase				:      Union[str, Any]									=       intermediate_size
							__UpperCamelCase				:      str									=       hidden_act
							__UpperCamelCase				:      List[Any]									=       hidden_dropout_prob
							__UpperCamelCase				:      str									=       attention_probs_dropout_prob
							__UpperCamelCase				:      Optional[Any]									=       max_position_embeddings
							__UpperCamelCase				:      Tuple									=       type_vocab_size
							__UpperCamelCase				:      Any									=       type_sequence_label_size
							__UpperCamelCase				:      str									=       initializer_range
							__UpperCamelCase				:      Optional[int]									=       num_labels
							__UpperCamelCase				:      int									=       num_choices
							__UpperCamelCase				:      Optional[Any]									=       scope
				def 	a_	(self      )     ->	List[Any]:
							__UpperCamelCase				:      Tuple									=       ids_tensor([self.batch_size, self.seq_length]			,  self.vocab_size      )

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

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

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

							__UpperCamelCase				:      Union[str, Any]									=       self.get_config()

							return (
							    config,
							    input_ids,
							    token_type_ids,
							    input_mask,
							    sequence_labels,
							    token_labels,
							    choice_labels,
							    fake_token_labels,
							)
				def 	a_	(self      )     ->	Optional[Any]:
							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 	a_	(self      )     ->	Any:
							(
							    __UpperCamelCase							
							)				:      List[str]									=       self.prepare_config_and_inputs()

							__UpperCamelCase				:      List[str]									=       True
							__UpperCamelCase				:      int									=       floats_tensor([self.batch_size, self.seq_length, self.hidden_size]      )
							__UpperCamelCase				:      Any									=       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 	a_	(self			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  )     ->	Optional[int]:
							__UpperCamelCase				:      Optional[int]									=       ElectraModel(config=_UpperCAmelCase      )
							model.to(_UpperCAmelCase      )
							model.eval()
							__UpperCamelCase				:      List[str]									=       model(_UpperCAmelCase			,  attention_mask=_UpperCAmelCase			,  token_type_ids=_UpperCAmelCase      )
							__UpperCamelCase				:      Optional[int]									=       model(_UpperCAmelCase			,  token_type_ids=_UpperCAmelCase      )
							__UpperCamelCase				:      Tuple									=       model(_UpperCAmelCase      )
							self.parent.assertEqual(result.last_hidden_state.shape			,  (self.batch_size, self.seq_length, self.hidden_size)      )
				def 	a_	(self			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  )     ->	List[Any]:
							__UpperCamelCase				:      Optional[int]									=       True
							__UpperCamelCase				:      int									=       ElectraModel(_UpperCAmelCase      )
							model.to(_UpperCAmelCase      )
							model.eval()
							__UpperCamelCase				:      Dict									=       model(
							    _UpperCAmelCase			,  attention_mask=_UpperCAmelCase			,  token_type_ids=_UpperCAmelCase			,  encoder_hidden_states=_UpperCAmelCase			,  encoder_attention_mask=_UpperCAmelCase			,  )
							__UpperCamelCase				:      Optional[int]									=       model(
							    _UpperCAmelCase			,  attention_mask=_UpperCAmelCase			,  token_type_ids=_UpperCAmelCase			,  encoder_hidden_states=_UpperCAmelCase			,  )
							__UpperCamelCase				:      Any									=       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 	a_	(self			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  )     ->	Optional[int]:
							__UpperCamelCase				:      List[Any]									=       ElectraForMaskedLM(config=_UpperCAmelCase      )
							model.to(_UpperCAmelCase      )
							model.eval()
							__UpperCamelCase				:      Any									=       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 	a_	(self			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  )     ->	Tuple:
							__UpperCamelCase				:      Dict									=       ElectraForCausalLM(config=_UpperCAmelCase      )
							model.to(_UpperCAmelCase      )
							model.eval()
							__UpperCamelCase				:      int									=       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 	a_	(self			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  )     ->	Any:
							__UpperCamelCase				:      List[str]									=       self.num_labels
							__UpperCamelCase				:      List[str]									=       ElectraForTokenClassification(config=_UpperCAmelCase      )
							model.to(_UpperCAmelCase      )
							model.eval()
							__UpperCamelCase				:      Union[str, Any]									=       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 	a_	(self			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  )     ->	Optional[int]:
							__UpperCamelCase				:      Tuple									=       self.num_labels
							__UpperCamelCase				:      Any									=       ElectraForPreTraining(config=_UpperCAmelCase      )
							model.to(_UpperCAmelCase      )
							model.eval()
							__UpperCamelCase				:      Optional[int]									=       model(_UpperCAmelCase			,  attention_mask=_UpperCAmelCase			,  token_type_ids=_UpperCAmelCase			,  labels=_UpperCAmelCase      )
							self.parent.assertEqual(result.logits.shape			,  (self.batch_size, self.seq_length)      )
				def 	a_	(self			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  )     ->	Optional[Any]:
							__UpperCamelCase				:      Union[str, Any]									=       self.num_labels
							__UpperCamelCase				:      Union[str, Any]									=       ElectraForSequenceClassification(_UpperCAmelCase      )
							model.to(_UpperCAmelCase      )
							model.eval()
							__UpperCamelCase				:      List[Any]									=       model(_UpperCAmelCase			,  attention_mask=_UpperCAmelCase			,  token_type_ids=_UpperCAmelCase			,  labels=_UpperCAmelCase      )
							self.parent.assertEqual(result.logits.shape			,  (self.batch_size, self.num_labels)      )
				def 	a_	(self			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  )     ->	int:
							__UpperCamelCase				:      Tuple									=       ElectraForQuestionAnswering(config=_UpperCAmelCase      )
							model.to(_UpperCAmelCase      )
							model.eval()
							__UpperCamelCase				:      List[Any]									=       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 	a_	(self			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase			,  )     ->	List[Any]:
							__UpperCamelCase				:      List[Any]									=       self.num_choices
							__UpperCamelCase				:      int									=       ElectraForMultipleChoice(config=_UpperCAmelCase      )
							model.to(_UpperCAmelCase      )
							model.eval()
							__UpperCamelCase				:      Optional[Any]									=       input_ids.unsqueeze(1      ).expand(-1			,  self.num_choices			,  -1      ).contiguous()
							__UpperCamelCase				:      List[Any]									=       token_type_ids.unsqueeze(1      ).expand(-1			,  self.num_choices			,  -1      ).contiguous()
							__UpperCamelCase				:      List[str]									=       input_mask.unsqueeze(1      ).expand(-1			,  self.num_choices			,  -1      ).contiguous()
							__UpperCamelCase				:      Optional[Any]									=       model(
							    _UpperCAmelCase			,  attention_mask=_UpperCAmelCase			,  token_type_ids=_UpperCAmelCase			,  labels=_UpperCAmelCase			,  )
							self.parent.assertEqual(result.logits.shape			,  (self.batch_size, self.num_choices)      )





				def 	a_	(self      )     ->	List[str]:
							__UpperCamelCase				:      List[str]									=       self.prepare_config_and_inputs()
							(
							    __UpperCamelCase							
							)				:      Any									=       config_and_inputs
							__UpperCamelCase				:      int									=       {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
							return config, inputs_dict







@require_torch
class  A		(			snake_case__      ,							snake_case__      ,							unittest.TestCase		):




				'''simple docstring'''





				A				     =    (
				    (
				        ElectraModel,
				        ElectraForPreTraining,
				        ElectraForMaskedLM,
				        ElectraForCausalLM,
				        ElectraForMultipleChoice,
				        ElectraForTokenClassification,
				        ElectraForSequenceClassification,
				        ElectraForQuestionAnswering,
				    )
				    if is_torch_available()
				    else ()
				)
				A				     =    (
				    {
				        '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				     =    True
				def 	a_	(self			,  _UpperCAmelCase			,  _UpperCAmelCase			,  _UpperCAmelCase=False      )     ->	Dict:
							__UpperCamelCase				:      Union[str, Any]									=       super()._prepare_for_class(_UpperCAmelCase			,  _UpperCAmelCase			,  return_labels=_UpperCAmelCase      )

							if return_labels:
										if model_class in get_values(_UpperCAmelCase      ):
													__UpperCamelCase				:      List[Any]									=       torch.zeros(
													    (self.model_tester.batch_size, self.model_tester.seq_length)			,  dtype=torch.long			,  device=_UpperCAmelCase      )
							return inputs_dict
				def 	a_	(self      )     ->	Optional[Any]:
							__UpperCamelCase				:      str									=       ElectraModelTester(self      )
							__UpperCamelCase				:      Optional[Any]									=       ConfigTester(self			,  config_class=_UpperCAmelCase			,  hidden_size=3_7      )
				def 	a_	(self      )     ->	str:
							self.config_tester.run_common_tests()
				def 	a_	(self      )     ->	List[Any]:
							__UpperCamelCase				:      Optional[Any]									=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_electra_model(*_UpperCAmelCase      )
				def 	a_	(self      )     ->	Union[str, Any]:
							__UpperCamelCase				:      str									=       self.model_tester.prepare_config_and_inputs_for_decoder()
							self.model_tester.create_and_check_electra_model_as_decoder(*_UpperCAmelCase      )
				def 	a_	(self      )     ->	int:
							__UpperCamelCase				:      Tuple									=       self.model_tester.prepare_config_and_inputs()
							for type in ["absolute", "relative_key", "relative_key_query"]:
										__UpperCamelCase				:      List[Any]									=       type
										self.model_tester.create_and_check_electra_model(*_UpperCAmelCase      )
				def 	a_	(self      )     ->	Tuple:
							__UpperCamelCase				:      int									=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_electra_for_masked_lm(*_UpperCAmelCase      )
				def 	a_	(self      )     ->	Tuple:
							__UpperCamelCase				:      Dict									=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_electra_for_token_classification(*_UpperCAmelCase      )
				def 	a_	(self      )     ->	Any:
							__UpperCamelCase				:      Tuple									=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_electra_for_pretraining(*_UpperCAmelCase      )
				def 	a_	(self      )     ->	List[Any]:
							__UpperCamelCase				:      Dict									=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_electra_for_sequence_classification(*_UpperCAmelCase      )
				def 	a_	(self      )     ->	str:
							__UpperCamelCase				:      str									=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_electra_for_question_answering(*_UpperCAmelCase      )
				def 	a_	(self      )     ->	Dict:
							__UpperCamelCase				:      List[Any]									=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_electra_for_multiple_choice(*_UpperCAmelCase      )
				@slow
				def 	a_	(self      )     ->	str:
							for model_name in ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										__UpperCamelCase				:      Union[str, Any]									=       ElectraModel.from_pretrained(_UpperCAmelCase      )
										self.assertIsNotNone(_UpperCAmelCase      )





				def 	a_	(self      )     ->	str:
							__UpperCamelCase				:      Dict									=       self.model_tester.prepare_config_and_inputs_for_decoder()
							self.model_tester.create_and_check_electra_for_causal_lm(*_UpperCAmelCase      )







@require_torch
class  A		(			unittest.TestCase		):




				'''simple docstring'''





				@slow
				def 	a_	(self      )     ->	Any:
							__UpperCamelCase				:      Union[str, Any]									=       ElectraModel.from_pretrained("google/electra-small-discriminator"      )
							__UpperCamelCase				:      Optional[Any]									=       torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]]      )
							__UpperCamelCase				:      List[Any]									=       torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]      )
							__UpperCamelCase				:      str									=       model(_UpperCAmelCase			,  attention_mask=_UpperCAmelCase      )[0]
							__UpperCamelCase				:      Dict									=       torch.Size((1, 1_1, 2_5_6)      )
							self.assertEqual(output.shape			,  _UpperCAmelCase      )
							__UpperCamelCase				:      Optional[int]									=       torch.tensor(
							    [[[0.4_471, 0.6_821, -0.3_265], [0.4_627, 0.5_255, -0.3_668], [0.4_532, 0.3_313, -0.4_344]]]      )

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









'''simple docstring'''






from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


_lowerCAmelCase				     = {'''configuration_visual_bert''': ['''VISUAL_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''VisualBertConfig''']}

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				_lowerCAmelCase				     = [
				    '''VISUAL_BERT_PRETRAINED_MODEL_ARCHIVE_LIST''',
				    '''VisualBertForMultipleChoice''',
				    '''VisualBertForPreTraining''',
				    '''VisualBertForQuestionAnswering''',
				    '''VisualBertForRegionToPhraseAlignment''',
				    '''VisualBertForVisualReasoning''',
				    '''VisualBertLayer''',
				    '''VisualBertModel''',
				    '''VisualBertPreTrainedModel''',
				]


if TYPE_CHECKING:
				from .configuration_visual_bert import VISUAL_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, VisualBertConfig

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_visual_bert import (
								    VISUAL_BERT_PRETRAINED_MODEL_ARCHIVE_LIST,
								    VisualBertForMultipleChoice,
								    VisualBertForPreTraining,
								    VisualBertForQuestionAnswering,
								    VisualBertForRegionToPhraseAlignment,
								    VisualBertForVisualReasoning,
								    VisualBertLayer,
								    VisualBertModel,
								    VisualBertPreTrainedModel,
								)


else:
				import sys

				_lowerCAmelCase				     = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)



