



"""simple docstring"""




import unittest

from transformers import ElectraConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
	import torch

	from transformers import (
	    MODEL_FOR_PRETRAINING_MAPPING,
	    ElectraForCausalLM,
	    ElectraForMaskedLM,
	    ElectraForMultipleChoice,
	    ElectraForPreTraining,
	    ElectraForQuestionAnswering,
	    ElectraForSequenceClassification,
	    ElectraForTokenClassification,
	    ElectraModel,
	)
	from transformers.models.electra.modeling_electra import ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST






class 			UpperCAmelCase_						:



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



						def 		snake_case_      (  self  :			Union[str, Any]		):
											_UpperCAmelCase    :			int              =					ids_tensor([self.batch_size, self.seq_length]  ,			self.vocab_size		)

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

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

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

											_UpperCAmelCase    :			List[str]              =					self.get_config()

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



						def 		snake_case_      (  self  :			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=__A  ,			initializer_range=self.initializer_range  ,			)



						def 		snake_case_      (  self  :			Optional[Any]		):
											(
											    _UpperCAmelCase					
											)    :			str              =					self.prepare_config_and_inputs()

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



						def 		snake_case_      (  self  :			List[str]  ,			A  :			int  ,			A  :			List[Any]  ,			A  :			Optional[int]  ,			A  :			Tuple  ,			A  :			Any  ,			A  :			List[str]  ,			A  :			List[Any]  ,			A  :			int  ,			A  :			Any  ,			):
											_UpperCAmelCase    :			Union[str, Any]              =					True
											_UpperCAmelCase    :			Optional[Any]              =					ElectraModel(__A		)
											model.to(__A		)
											model.eval()
											_UpperCAmelCase    :			Union[str, Any]              =					model(
											    __A  ,			attention_mask=__A  ,			token_type_ids=__A  ,			encoder_hidden_states=__A  ,			encoder_attention_mask=__A  ,			)
											_UpperCAmelCase    :			Dict              =					model(
											    __A  ,			attention_mask=__A  ,			token_type_ids=__A  ,			encoder_hidden_states=__A  ,			)
											_UpperCAmelCase    :			Tuple              =					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 		snake_case_      (  self  :			List[str]  ,			A  :			List[Any]  ,			A  :			str  ,			A  :			List[str]  ,			A  :			int  ,			A  :			Union[str, Any]  ,			A  :			Any  ,			A  :			Union[str, Any]  ,			A  :			Optional[Any]  ,			):
											_UpperCAmelCase    :			Optional[int]              =					ElectraForMaskedLM(config=__A		)
											model.to(__A		)
											model.eval()
											_UpperCAmelCase    :			Dict              =					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 		snake_case_      (  self  :			Optional[int]  ,			A  :			Tuple  ,			A  :			Union[str, Any]  ,			A  :			Union[str, Any]  ,			A  :			List[Any]  ,			A  :			Dict  ,			A  :			List[str]  ,			A  :			str  ,			A  :			Optional[int]  ,			A  :			List[Any]  ,			):
											_UpperCAmelCase    :			List[Any]              =					ElectraForCausalLM(config=__A		)
											model.to(__A		)
											model.eval()
											_UpperCAmelCase    :			Optional[int]              =					model(__A  ,			attention_mask=__A  ,			token_type_ids=__A  ,			labels=__A		)
											self.parent.assertEqual(result.logits.shape  ,			(self.batch_size, self.seq_length, self.vocab_size)		)



						def 		snake_case_      (  self  :			Optional[Any]  ,			A  :			Optional[int]  ,			A  :			Dict  ,			A  :			List[Any]  ,			A  :			Union[str, Any]  ,			A  :			Dict  ,			A  :			Any  ,			A  :			Dict  ,			A  :			str  ,			):
											_UpperCAmelCase    :			Union[str, Any]              =					self.num_labels
											_UpperCAmelCase    :			List[str]              =					ElectraForTokenClassification(config=__A		)
											model.to(__A		)
											model.eval()
											_UpperCAmelCase    :			Union[str, Any]              =					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 		snake_case_      (  self  :			Optional[int]  ,			A  :			Optional[int]  ,			A  :			Tuple  ,			A  :			Optional[Any]  ,			A  :			List[Any]  ,			A  :			Optional[Any]  ,			A  :			Union[str, Any]  ,			A  :			List[str]  ,			A  :			Union[str, Any]  ,			):
											_UpperCAmelCase    :			Union[str, Any]              =					self.num_labels
											_UpperCAmelCase    :			Tuple              =					ElectraForPreTraining(config=__A		)
											model.to(__A		)
											model.eval()
											_UpperCAmelCase    :			Dict              =					model(__A  ,			attention_mask=__A  ,			token_type_ids=__A  ,			labels=__A		)
											self.parent.assertEqual(result.logits.shape  ,			(self.batch_size, self.seq_length)		)



						def 		snake_case_      (  self  :			Optional[Any]  ,			A  :			Union[str, Any]  ,			A  :			Union[str, Any]  ,			A  :			Optional[int]  ,			A  :			Optional[int]  ,			A  :			Any  ,			A  :			Optional[int]  ,			A  :			str  ,			A  :			Optional[int]  ,			):
											_UpperCAmelCase    :			int              =					self.num_labels
											_UpperCAmelCase    :			Tuple              =					ElectraForSequenceClassification(__A		)
											model.to(__A		)
											model.eval()
											_UpperCAmelCase    :			int              =					model(__A  ,			attention_mask=__A  ,			token_type_ids=__A  ,			labels=__A		)
											self.parent.assertEqual(result.logits.shape  ,			(self.batch_size, self.num_labels)		)



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



						def 		snake_case_      (  self  :			Optional[int]		):
											_UpperCAmelCase    :			Optional[int]              =					self.prepare_config_and_inputs()
											(
											    _UpperCAmelCase					
											)    :			str              =					config_and_inputs
											_UpperCAmelCase    :			Dict              =					{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
											return config, inputs_dict







@require_torch
class 			UpperCAmelCase_						(       a__ ,    a__ ,    unittest.TestCase		):
						__SCREAMING_SNAKE_CASE		:     Any				  =				(
						    (
						        ElectraModel,
						        ElectraForPreTraining,
						        ElectraForMaskedLM,
						        ElectraForCausalLM,
						        ElectraForMultipleChoice,
						        ElectraForTokenClassification,
						        ElectraForSequenceClassification,
						        ElectraForQuestionAnswering,
						    )
						    if is_torch_available()
						    else ()
						)
						__SCREAMING_SNAKE_CASE		:     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 {}
						)
						__SCREAMING_SNAKE_CASE		:     List[str]				  =				True



						def 		snake_case_      (  self  :			str  ,			A  :			Any  ,			A  :			Tuple  ,			A  :			Dict=False		):
											_UpperCAmelCase    :			Optional[Any]              =					super()._prepare_for_class(__A  ,			__A  ,			return_labels=__A		)

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



						def 		snake_case_      (  self  :			List[Any]		):
											_UpperCAmelCase    :			Union[str, Any]              =					ElectraModelTester(self		)
											_UpperCAmelCase    :			Tuple              =					ConfigTester(self  ,			config_class=__A  ,			hidden_size=3_7		)



						def 		snake_case_      (  self  :			Optional[Any]		):
											self.config_tester.run_common_tests()



						def 		snake_case_      (  self  :			List[Any]		):
											_UpperCAmelCase    :			Tuple              =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_electra_model(*__A		)



						def 		snake_case_      (  self  :			Any		):
											_UpperCAmelCase    :			Optional[int]              =					self.model_tester.prepare_config_and_inputs_for_decoder()
											self.model_tester.create_and_check_electra_model_as_decoder(*__A		)



						def 		snake_case_      (  self  :			Optional[int]		):
											_UpperCAmelCase    :			Tuple              =					self.model_tester.prepare_config_and_inputs()
											for type in ["absolute", "relative_key", "relative_key_query"]:
																_UpperCAmelCase    :			str              =					type
																self.model_tester.create_and_check_electra_model(*__A		)



						def 		snake_case_      (  self  :			Tuple		):
											_UpperCAmelCase    :			Any              =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_electra_for_masked_lm(*__A		)



						def 		snake_case_      (  self  :			List[str]		):
											_UpperCAmelCase    :			Tuple              =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_electra_for_token_classification(*__A		)



						def 		snake_case_      (  self  :			Optional[Any]		):
											_UpperCAmelCase    :			str              =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_electra_for_pretraining(*__A		)



						def 		snake_case_      (  self  :			Optional[int]		):
											_UpperCAmelCase    :			Union[str, Any]              =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_electra_for_sequence_classification(*__A		)



						def 		snake_case_      (  self  :			Tuple		):
											_UpperCAmelCase    :			Optional[Any]              =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_electra_for_question_answering(*__A		)



						def 		snake_case_      (  self  :			Optional[Any]		):
											_UpperCAmelCase    :			Union[str, Any]              =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_electra_for_multiple_choice(*__A		)



						@slow
						def 		snake_case_      (  self  :			Tuple		):







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



						def 		snake_case_      (  self  :			List[Any]		):
											_UpperCAmelCase    :			int              =					self.model_tester.prepare_config_and_inputs_for_decoder()
											self.model_tester.create_and_check_electra_for_causal_lm(*__A		)







@require_torch
class 			UpperCAmelCase_						(       unittest.TestCase		):



						@slow
						def 		snake_case_      (  self  :			int		):
											_UpperCAmelCase    :			str              =					ElectraModel.from_pretrained("google/electra-small-discriminator"		)
											_UpperCAmelCase    :			int              =					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    :			Optional[int]              =					torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]		)
											_UpperCAmelCase    :			Optional[Any]              =					model(__A  ,			attention_mask=__A		)[0]
											_UpperCAmelCase    :			Optional[Any]              =					torch.Size((1, 1_1, 2_5_6)		)
											self.assertEqual(output.shape  ,			__A		)
											_UpperCAmelCase    :			List[Any]              =					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]  ,			__A  ,			atol=1e-4		)		)





"""simple docstring"""




import argparse

import torch

from transformers import ImageGPTConfig, ImageGPTForCausalLM, load_tf_weights_in_imagegpt
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


logging.set_verbosity_info()

def 		__snake_case		(   SCREAMING_SNAKE_CASE__			:		Optional[Any]		,						SCREAMING_SNAKE_CASE__			:		Tuple		,						SCREAMING_SNAKE_CASE__			:		Any       )						->     Dict:





     '''simple docstring'''

     _UpperCAmelCase    :			Any              =					{"small": (512, 8, 24), "medium": (1_024, 8, 36), "large": (1_536, 16, 48)}
     _UpperCAmelCase       , _UpperCAmelCase       , _UpperCAmelCase    :			str              =					MODELS[model_size]  # set model hyperparameters
     _UpperCAmelCase    :			Tuple              =					ImageGPTConfig(n_embd=SCREAMING_SNAKE_CASE__		,						n_layer=SCREAMING_SNAKE_CASE__		,						n_head=SCREAMING_SNAKE_CASE__       )
     _UpperCAmelCase    :			int              =					ImageGPTForCausalLM(SCREAMING_SNAKE_CASE__       )

     # Load weights from numpy
     load_tf_weights_in_imagegpt(SCREAMING_SNAKE_CASE__		,						SCREAMING_SNAKE_CASE__		,						SCREAMING_SNAKE_CASE__       )

     # Save pytorch-model
     _UpperCAmelCase    :			Union[str, Any]              =					pytorch_dump_folder_path + "/" + WEIGHTS_NAME
     _UpperCAmelCase    :			int              =					pytorch_dump_folder_path + "/" + CONFIG_NAME
     print(f'Save PyTorch model to {pytorch_weights_dump_path}'       )
     torch.save(model.state_dict()		,						SCREAMING_SNAKE_CASE__       )
     print(f'Save configuration file to {pytorch_config_dump_path}'       )
     with open(SCREAMING_SNAKE_CASE__		,						"w"		,						encoding="utf-8"       ) as f:
          f.write(config.to_json_string()       )


if __name__ == "__main__":
 _lowerCAmelCase       :      List[str]							=							argparse.ArgumentParser()
 # Required parameters
 parser.add_argument(
     "--imagegpt_checkpoint_path",
     default=None,
     type=str,
     required=True,
     help="Path to the TensorFlow checkpoint path.",
 )
 parser.add_argument(
     "--model_size",
     default=None,
     type=str,
     required=True,
     help="Size of the model (can be either 'small', 'medium' or 'large').",
 )
 parser.add_argument(
     "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output PyTorch model."
 )
 _lowerCAmelCase       :      Optional[int]							=							parser.parse_args()
 convert_imagegpt_checkpoint_to_pytorch(
     args.imagegpt_checkpoint_path, args.model_size, args.pytorch_dump_folder_path
 )
