




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






						def __init__(   self						:						List[Any]					,   __UpperCamelCase						:						int					,   __UpperCamelCase						:						Dict=1_3					,   __UpperCamelCase						:						List[str]=7					,   __UpperCamelCase						:						List[str]=True					,   __UpperCamelCase						:						List[Any]=True					,   __UpperCamelCase						:						List[str]=True					,   __UpperCamelCase						:						str=True					,   __UpperCamelCase						:						str=9_9					,   __UpperCamelCase						:						Optional[int]=3_2					,   __UpperCamelCase						:						Any=5					,   __UpperCamelCase						:						Dict=4					,   __UpperCamelCase						:						Union[str, Any]=3_7					,   __UpperCamelCase						:						Optional[Any]="gelu"					,   __UpperCamelCase						:						Dict=0.1					,   __UpperCamelCase						:						Optional[int]=0.1					,   __UpperCamelCase						:						Union[str, Any]=5_1_2					,   __UpperCamelCase						:						str=1_6					,   __UpperCamelCase						:						Optional[int]=2					,   __UpperCamelCase						:						List[Any]=0.02					,   __UpperCamelCase						:						Optional[Any]=3					,   __UpperCamelCase						:						Union[str, Any]=4					,   __UpperCamelCase						:						str=None					,   ):



									'''simple docstring'''






									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       __lowerCAmelCase(   self						:						Union[str, Any]       ):



									'''simple docstring'''






									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       __lowerCAmelCase(   self						:						Dict       ):



									'''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						:						Optional[int]       ):



									'''simple docstring'''






									(
									    (
									    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       __lowerCAmelCase(   self						:						List[str]					,   __UpperCamelCase						:						Optional[int]					,   __UpperCamelCase						:						Union[str, Any]					,   __UpperCamelCase						:						List[str]					,   __UpperCamelCase						:						Dict					,   __UpperCamelCase						:						Dict					,   __UpperCamelCase						:						List[Any]					,   __UpperCamelCase						:						List[Any]					,   __UpperCamelCase						:						List[Any]					,   ):



									'''simple docstring'''






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






						def       __lowerCAmelCase(   self						:						Tuple					,   __UpperCamelCase						:						Optional[int]					,   __UpperCamelCase						:						Union[str, Any]					,   __UpperCamelCase						:						Union[str, Any]					,   __UpperCamelCase						:						int					,   __UpperCamelCase						:						Optional[int]					,   __UpperCamelCase						:						Tuple					,   __UpperCamelCase						:						Tuple					,   __UpperCamelCase						:						Optional[int]					,   __UpperCamelCase						:						List[Any]					,   ):



									'''simple docstring'''






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



									'''simple docstring'''






									snake_case__          =						ElectraForMaskedLM(config=a_       )
									model.to(a_       )
									model.eval()
									snake_case__          =						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						:						Any					,   __UpperCamelCase						:						Dict					,   __UpperCamelCase						:						Optional[Any]					,   __UpperCamelCase						:						List[str]					,   __UpperCamelCase						:						str					,   __UpperCamelCase						:						List[str]					,   __UpperCamelCase						:						Dict					,   __UpperCamelCase						:						Dict					,   __UpperCamelCase						:						Union[str, Any]					,   __UpperCamelCase						:						Optional[Any]					,   ):



									'''simple docstring'''






									snake_case__          =						ElectraForCausalLM(config=a_       )
									model.to(a_       )
									model.eval()
									snake_case__          =						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						:						Tuple					,   __UpperCamelCase						:						Tuple					,   __UpperCamelCase						:						Tuple					,   __UpperCamelCase						:						Optional[Any]					,   __UpperCamelCase						:						Any					,   __UpperCamelCase						:						Optional[Any]					,   __UpperCamelCase						:						Dict					,   __UpperCamelCase						:						Optional[Any]					,   __UpperCamelCase						:						Optional[Any]					,   ):



									'''simple docstring'''






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



									'''simple docstring'''






									snake_case__          =						self.num_labels
									snake_case__          =						ElectraForPreTraining(config=a_       )
									model.to(a_       )
									model.eval()
									snake_case__          =						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						:						List[Any]					,   __UpperCamelCase						:						Optional[Any]					,   __UpperCamelCase						:						Union[str, Any]					,   __UpperCamelCase						:						Dict					,   __UpperCamelCase						:						int					,   __UpperCamelCase						:						List[str]					,   __UpperCamelCase						:						str					,   __UpperCamelCase						:						List[str]					,   __UpperCamelCase						:						Union[str, Any]					,   ):



									'''simple docstring'''






									snake_case__          =						self.num_labels
									snake_case__          =						ElectraForSequenceClassification(a_       )
									model.to(a_       )
									model.eval()
									snake_case__          =						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					,   __UpperCamelCase						:						str					,   __UpperCamelCase						:						str					,   __UpperCamelCase						:						List[str]					,   __UpperCamelCase						:						Optional[int]					,   __UpperCamelCase						:						Any					,   __UpperCamelCase						:						Optional[int]					,   __UpperCamelCase						:						List[str]					,   __UpperCamelCase						:						List[Any]					,   ):



									'''simple docstring'''






									snake_case__          =						ElectraForQuestionAnswering(config=a_       )
									model.to(a_       )
									model.eval()
									snake_case__          =						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						:						List[Any]					,   __UpperCamelCase						:						List[Any]					,   __UpperCamelCase						:						Any					,   __UpperCamelCase						:						List[Any]					,   __UpperCamelCase						:						List[str]					,   __UpperCamelCase						:						Dict					,   __UpperCamelCase						:						Dict					,   __UpperCamelCase						:						List[str]					,   __UpperCamelCase						:						Union[str, Any]					,   ):



									'''simple docstring'''






									snake_case__          =						self.num_choices
									snake_case__          =						ElectraForMultipleChoice(config=a_       )
									model.to(a_       )
									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(
									    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						:						int       ):



									'''simple docstring'''






									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 __magic_name__(						SCREAMING_SNAKE_CASE__    ,		SCREAMING_SNAKE_CASE__    ,		unittest.TestCase      ):
						UpperCAmelCase_		:					Any    =  (
						    (
						        ElectraModel,
						        ElectraForPreTraining,
						        ElectraForMaskedLM,
						        ElectraForCausalLM,
						        ElectraForMultipleChoice,
						        ElectraForTokenClassification,
						        ElectraForSequenceClassification,
						        ElectraForQuestionAnswering,
						    )
						    if is_torch_available()
						    else ()
						)
						UpperCAmelCase_		:					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 {}
						)
						UpperCAmelCase_		:					Union[str, Any]    =  True






						def       __lowerCAmelCase(   self						:						Union[str, Any]					,   __UpperCamelCase						:						List[Any]					,   __UpperCamelCase						:						List[Any]					,   __UpperCamelCase						:						List[Any]=False       ):



									'''simple docstring'''






									snake_case__          =						super()._prepare_for_class(a_					,   a_					,   return_labels=a_       )

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






						def       __lowerCAmelCase(   self						:						str       ):



									'''simple docstring'''






									snake_case__          =						ElectraModelTester(self       )
									snake_case__          =						ConfigTester(self					,   config_class=a_					,   hidden_size=3_7       )






						def       __lowerCAmelCase(   self						:						Optional[Any]       ):



									'''simple docstring'''






									self.config_tester.run_common_tests()






						def       __lowerCAmelCase(   self						:						Union[str, Any]       ):



									'''simple docstring'''






									snake_case__          =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_electra_model(*a_       )






						def       __lowerCAmelCase(   self						:						Tuple       ):



									'''simple docstring'''






									snake_case__          =						self.model_tester.prepare_config_and_inputs_for_decoder()
									self.model_tester.create_and_check_electra_model_as_decoder(*a_       )






						def       __lowerCAmelCase(   self						:						List[Any]       ):



									'''simple docstring'''






									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(*a_       )






						def       __lowerCAmelCase(   self						:						List[Any]       ):



									'''simple docstring'''






									snake_case__          =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_electra_for_masked_lm(*a_       )






						def       __lowerCAmelCase(   self						:						Tuple       ):



									'''simple docstring'''






									snake_case__          =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_electra_for_token_classification(*a_       )






						def       __lowerCAmelCase(   self						:						List[Any]       ):



									'''simple docstring'''






									snake_case__          =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_electra_for_pretraining(*a_       )






						def       __lowerCAmelCase(   self						:						Optional[Any]       ):



									'''simple docstring'''






									snake_case__          =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_electra_for_sequence_classification(*a_       )






						def       __lowerCAmelCase(   self						:						Union[str, Any]       ):



									'''simple docstring'''






									snake_case__          =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_electra_for_question_answering(*a_       )






						def       __lowerCAmelCase(   self						:						List[str]       ):



									'''simple docstring'''






									snake_case__          =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_electra_for_multiple_choice(*a_       )






						@slow
						def       __lowerCAmelCase(   self						:						Dict       ):



									'''simple docstring'''






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







						def       __lowerCAmelCase(   self						:						Dict       ):



									'''simple docstring'''






									snake_case__          =						self.model_tester.prepare_config_and_inputs_for_decoder()
									self.model_tester.create_and_check_electra_for_causal_lm(*a_       )







@require_torch
class __magic_name__(						unittest.TestCase      ):






						@slow
						def       __lowerCAmelCase(   self						:						Tuple       ):



									'''simple docstring'''






									snake_case__          =						ElectraModel.from_pretrained("""google/electra-small-discriminator"""       )
									snake_case__          =						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]]       )
									snake_case__          =						torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]       )
									snake_case__          =						model(a_					,   attention_mask=a_       )[0]
									snake_case__          =						torch.Size((1, 1_1, 2_5_6)       )
									self.assertEqual(output.shape					,   a_       )
									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]					,   a_					,   atol=1E-4       )       )



'''simple docstring'''

from typing import TYPE_CHECKING

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


__lowercase:		Tuple    						=  {
    "configuration_imagegpt": ["IMAGEGPT_PRETRAINED_CONFIG_ARCHIVE_MAP", "ImageGPTConfig", "ImageGPTOnnxConfig"]
}

try:
       if not is_vision_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       __lowercase:		List[str]    						=  ["ImageGPTFeatureExtractor"]
       __lowercase:		Tuple    						=  ["ImageGPTImageProcessor"]

try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       __lowercase:		str    						=  [
           "IMAGEGPT_PRETRAINED_MODEL_ARCHIVE_LIST",
           "ImageGPTForCausalImageModeling",
           "ImageGPTForImageClassification",
           "ImageGPTModel",
           "ImageGPTPreTrainedModel",
           "load_tf_weights_in_imagegpt",
       ]


if TYPE_CHECKING:
       from .configuration_imagegpt import IMAGEGPT_PRETRAINED_CONFIG_ARCHIVE_MAP, ImageGPTConfig, ImageGPTOnnxConfig

       try:
              if not is_vision_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .feature_extraction_imagegpt import ImageGPTFeatureExtractor
              from .image_processing_imagegpt import ImageGPTImageProcessor

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_imagegpt import (
                  IMAGEGPT_PRETRAINED_MODEL_ARCHIVE_LIST,
                  ImageGPTForCausalImageModeling,
                  ImageGPTForImageClassification,
                  ImageGPTModel,
                  ImageGPTPreTrainedModel,
                  load_tf_weights_in_imagegpt,
              )

else:
       import sys

       __lowercase:		List[str]    						=  _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)