

import argparse

import torch

from transformers import GPTaConfig, GPTaModel, load_tf_weights_in_gpta
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


logging.set_verbosity_info()






def        snake_case__		(    lowerCamelCase_     ,				lowerCamelCase_     ,				lowerCamelCase_		):
							# Construct model
							if gpta_config_file == "":
														A      :					Optional[int]      		=					GPTaConfig()
							else:
														A      :					Optional[int]      		=					GPTaConfig.from_json_file(lowerCamelCase_		)
							A      :					Tuple      		=					GPTaModel(lowerCamelCase_		)

							# Load weights from numpy
							load_tf_weights_in_gpta(lowerCamelCase_     ,				lowerCamelCase_     ,				lowerCamelCase_		)

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


if __name__ == "__main__":
		lowercase			:				Optional[int]   =     argparse.ArgumentParser()
		# Required parameters
		parser.add_argument(
		    "--gpt2_checkpoint_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path."
		)
		parser.add_argument(
		    "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output PyTorch model."
		)
		parser.add_argument(
		    "--gpt2_config_file",
		    default="",
		    type=str,
		    help=(
		        "An optional config json file corresponding to the pre-trained OpenAI model. \n"
		        "This specifies the model architecture."
		    ),
		)
		lowercase			:				List[Any]   =     parser.parse_args()
		convert_gpta_checkpoint_to_pytorch(args.gpta_checkpoint_path, args.gpta_config_file, args.pytorch_dump_folder_path)









from __future__ import annotations

import unittest

from transformers import FunnelConfig, is_tf_available
from transformers.testing_utils import require_tf

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
		import tensorflow as tf

		from transformers import (
		    TFFunnelBaseModel,
		    TFFunnelForMaskedLM,
		    TFFunnelForMultipleChoice,
		    TFFunnelForPreTraining,
		    TFFunnelForQuestionAnswering,
		    TFFunnelForSequenceClassification,
		    TFFunnelForTokenClassification,
		    TFFunnelModel,
		)







class 					__lowercase  :





							"""simple docstring"""

							def __init__(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase=13    ,						__UpperCAmelCase=7    ,						__UpperCAmelCase=True    ,						__UpperCAmelCase=True    ,						__UpperCAmelCase=True    ,						__UpperCAmelCase=True    ,						__UpperCAmelCase=99    ,						__UpperCAmelCase=[1, 1, 2]    ,						__UpperCAmelCase=1    ,						__UpperCAmelCase=32    ,						__UpperCAmelCase=4    ,						__UpperCAmelCase=8    ,						__UpperCAmelCase=37    ,						__UpperCAmelCase="gelu_new"    ,						__UpperCAmelCase=0.1    ,						__UpperCAmelCase=0.1    ,						__UpperCAmelCase=0.0    ,						__UpperCAmelCase=5_12    ,						__UpperCAmelCase=3    ,						__UpperCAmelCase=0.0_2    ,						__UpperCAmelCase=3    ,						__UpperCAmelCase=4    ,						__UpperCAmelCase=None    ,						__UpperCAmelCase=False    ,						)		->       Tuple:
														A      :					Optional[int]      		=					parent
														A      :					int      		=					batch_size
														A      :					Union[str, Any]      		=					seq_length
														A      :					Optional[Any]      		=					is_training
														A      :					int      		=					use_input_mask
														A      :					List[Any]      		=					use_token_type_ids
														A      :					Optional[int]      		=					use_labels
														A      :					List[Any]      		=					vocab_size
														A      :					int      		=					block_sizes
														A      :					Optional[int]      		=					num_decoder_layers
														A      :					int      		=					d_model
														A      :					Dict      		=					n_head
														A      :					Tuple      		=					d_head
														A      :					int      		=					d_inner
														A      :					Tuple      		=					hidden_act
														A      :					Tuple      		=					hidden_dropout
														A      :					Any      		=					attention_dropout
														A      :					Any      		=					activation_dropout
														A      :					List[Any]      		=					max_position_embeddings
														A      :					int      		=					type_vocab_size
														A      :					Dict      		=					2
														A      :					List[str]      		=					num_labels
														A      :					Optional[int]      		=					num_choices
														A      :					int      		=					scope
														A      :					int      		=					initializer_std

														# Used in the tests to check the size of the first attention layer
														A      :					Optional[int]      		=					n_head
														# Used in the tests to check the size of the first hidden state
														A      :					List[Any]      		=					self.d_model
														# Used in the tests to check the number of output hidden states/attentions
														A      :					Dict      		=					sum(self.block_sizes						) + (0 if base else self.num_decoder_layers)
														# FunnelModel adds two hidden layers: input embeddings and the sum of the upsampled encoder hidden state with
														# the last hidden state of the first block (which is the first hidden state of the decoder).
														if not base:
																					A      :					Union[str, Any]      		=					self.num_hidden_layers + 2




							def       snake_case			(					self						)		->       List[Any]:
														A      :					int      		=					ids_tensor([self.batch_size, self.seq_length]    ,						self.vocab_size						)

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

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

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

														A      :					List[str]      		=					FunnelConfig(
														    vocab_size=self.vocab_size    ,						block_sizes=self.block_sizes    ,						num_decoder_layers=self.num_decoder_layers    ,						d_model=self.d_model    ,						n_head=self.n_head    ,						d_head=self.d_head    ,						d_inner=self.d_inner    ,						hidden_act=self.hidden_act    ,						hidden_dropout=self.hidden_dropout    ,						attention_dropout=self.attention_dropout    ,						activation_dropout=self.activation_dropout    ,						max_position_embeddings=self.max_position_embeddings    ,						type_vocab_size=self.type_vocab_size    ,						initializer_std=self.initializer_std    ,						)

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




							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						)		->       Optional[int]:
														A      :					str      		=					TFFunnelModel(config=__UpperCAmelCase						)
														A      :					str      		=					{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
														A      :					Tuple      		=					model(__UpperCAmelCase						)

														A      :					str      		=					[input_ids, input_mask]
														A      :					Optional[Any]      		=					model(__UpperCAmelCase						)

														A      :					List[str]      		=					model(__UpperCAmelCase						)
														self.parent.assertEqual(result.last_hidden_state.shape    ,						(self.batch_size, self.seq_length, self.d_model)						)

														A      :					str      		=					False
														A      :					List[str]      		=					TFFunnelModel(config=__UpperCAmelCase						)
														A      :					str      		=					model(__UpperCAmelCase						)
														self.parent.assertEqual(result.last_hidden_state.shape    ,						(self.batch_size, self.seq_length, self.d_model)						)

														A      :					Union[str, Any]      		=					False
														A      :					Tuple      		=					TFFunnelModel(config=__UpperCAmelCase						)
														A      :					Any      		=					model(__UpperCAmelCase						)
														self.parent.assertEqual(result.last_hidden_state.shape    ,						(self.batch_size, self.seq_length, self.d_model)						)




							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						)		->       Union[str, Any]:
														A      :					Dict      		=					TFFunnelBaseModel(config=__UpperCAmelCase						)
														A      :					Optional[int]      		=					{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
														A      :					List[str]      		=					model(__UpperCAmelCase						)

														A      :					Union[str, Any]      		=					[input_ids, input_mask]
														A      :					Optional[Any]      		=					model(__UpperCAmelCase						)

														A      :					Union[str, Any]      		=					model(__UpperCAmelCase						)
														self.parent.assertEqual(result.last_hidden_state.shape    ,						(self.batch_size, 2, self.d_model)						)

														A      :					List[Any]      		=					False
														A      :					Union[str, Any]      		=					TFFunnelBaseModel(config=__UpperCAmelCase						)
														A      :					Union[str, Any]      		=					model(__UpperCAmelCase						)
														self.parent.assertEqual(result.last_hidden_state.shape    ,						(self.batch_size, 3, self.d_model)						)

														A      :					int      		=					False
														A      :					Tuple      		=					TFFunnelBaseModel(config=__UpperCAmelCase						)
														A      :					Tuple      		=					model(__UpperCAmelCase						)
														self.parent.assertEqual(result.last_hidden_state.shape    ,						(self.batch_size, 2, self.d_model)						)




							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						)		->       List[Any]:
														A      :					List[str]      		=					TFFunnelForPreTraining(config=__UpperCAmelCase						)
														A      :					Any      		=					{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
														A      :					Optional[Any]      		=					model(__UpperCAmelCase						)
														self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.seq_length)						)




							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						)		->       List[Any]:
														A      :					str      		=					TFFunnelForMaskedLM(config=__UpperCAmelCase						)
														A      :					int      		=					{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
														A      :					str      		=					model(__UpperCAmelCase						)
														self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.seq_length, self.vocab_size)						)




							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						)		->       Union[str, Any]:
														A      :					Tuple      		=					self.num_labels
														A      :					List[Any]      		=					TFFunnelForSequenceClassification(config=__UpperCAmelCase						)
														A      :					List[str]      		=					{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
														A      :					Any      		=					model(__UpperCAmelCase						)
														self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.num_labels)						)




							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						)		->       Dict:
														A      :					Union[str, Any]      		=					self.num_choices
														A      :					Tuple      		=					TFFunnelForMultipleChoice(config=__UpperCAmelCase						)
														A      :					Tuple      		=					tf.tile(tf.expand_dims(__UpperCAmelCase    ,						1						)    ,						(1, self.num_choices, 1)						)
														A      :					List[Any]      		=					tf.tile(tf.expand_dims(__UpperCAmelCase    ,						1						)    ,						(1, self.num_choices, 1)						)
														A      :					Any      		=					tf.tile(tf.expand_dims(__UpperCAmelCase    ,						1						)    ,						(1, self.num_choices, 1)						)
														A      :					str      		=					{
														    '''input_ids''': multiple_choice_inputs_ids,
														    '''attention_mask''': multiple_choice_input_mask,
														    '''token_type_ids''': multiple_choice_token_type_ids,
														}
														A      :					Any      		=					model(__UpperCAmelCase						)
														self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.num_choices)						)




							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						)		->       str:
														A      :					Optional[Any]      		=					self.num_labels
														A      :					List[str]      		=					TFFunnelForTokenClassification(config=__UpperCAmelCase						)
														A      :					int      		=					{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
														A      :					Optional[Any]      		=					model(__UpperCAmelCase						)
														self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.seq_length, self.num_labels)						)




							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						)		->       Any:
														A      :					Dict      		=					TFFunnelForQuestionAnswering(config=__UpperCAmelCase						)
														A      :					str      		=					{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
														A      :					List[Any]      		=					model(__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       snake_case			(					self						)		->       str:
														A      :					Union[str, Any]      		=					self.prepare_config_and_inputs()
														(
														    (
														    A
														)		,		(
														    A
														)		,		(
														    A
														)		,		(
														    A
														)		,		(
														    A
														)		,		(
														    A
														)		,		(
														    A
														)		,		
														)      :					Optional[Any]      		=					config_and_inputs
														A      :					Union[str, Any]      		=					{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
														return config, inputs_dict







@require_tf
class 					__lowercase  (						_SCREAMING_SNAKE_CASE		,       _SCREAMING_SNAKE_CASE		,       unittest.TestCase				):





							"""simple docstring"""

							UpperCAmelCase_						:		Any								=	(
							    (
							        TFFunnelModel,
							        TFFunnelForMaskedLM,
							        TFFunnelForPreTraining,
							        TFFunnelForQuestionAnswering,
							        TFFunnelForTokenClassification,
							    )
							    if is_tf_available()
							    else ()
							)
							UpperCAmelCase_						:		Optional[Any]								=	(
							    {
							        '''feature-extraction''': (TFFunnelBaseModel, TFFunnelModel),
							        '''fill-mask''': TFFunnelForMaskedLM,
							        '''question-answering''': TFFunnelForQuestionAnswering,
							        '''text-classification''': TFFunnelForSequenceClassification,
							        '''token-classification''': TFFunnelForTokenClassification,
							        '''zero-shot''': TFFunnelForSequenceClassification,
							    }
							    if is_tf_available()
							    else {}
							)
							UpperCAmelCase_						:		Optional[Any]								=	False
							UpperCAmelCase_						:		Optional[Any]								=	False




							def       snake_case			(					self						)		->       str:
														A      :					Dict      		=					TFFunnelModelTester(self						)
														A      :					int      		=					ConfigTester(self    ,						config_class=__UpperCAmelCase						)




							def       snake_case			(					self						)		->       Tuple:
														self.config_tester.run_common_tests()




							def       snake_case			(					self						)		->       List[str]:
														A      :					Tuple      		=					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*__UpperCAmelCase						)




							def       snake_case			(					self						)		->       Union[str, Any]:
														A      :					Optional[Any]      		=					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_pretraining(*__UpperCAmelCase						)




							def       snake_case			(					self						)		->       Optional[Any]:
														A      :					List[Any]      		=					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_masked_lm(*__UpperCAmelCase						)




							def       snake_case			(					self						)		->       Optional[Any]:
														A      :					List[Any]      		=					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_token_classification(*__UpperCAmelCase						)






							def       snake_case			(					self						)		->       Optional[int]:
														A      :					List[str]      		=					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_question_answering(*__UpperCAmelCase						)







@require_tf
class 					__lowercase  (						_SCREAMING_SNAKE_CASE		,       unittest.TestCase				):





							"""simple docstring"""

							UpperCAmelCase_						:		Any								=	(
							    (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else ()
							)
							UpperCAmelCase_						:		str								=	False
							UpperCAmelCase_						:		Union[str, Any]								=	False




							def       snake_case			(					self						)		->       int:
														A      :					Optional[int]      		=					TFFunnelModelTester(self    ,						base=__UpperCAmelCase						)
														A      :					Optional[Any]      		=					ConfigTester(self    ,						config_class=__UpperCAmelCase						)




							def       snake_case			(					self						)		->       str:
														self.config_tester.run_common_tests()




							def       snake_case			(					self						)		->       Tuple:
														A      :					Optional[Any]      		=					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_base_model(*__UpperCAmelCase						)




							def       snake_case			(					self						)		->       int:
														A      :					Union[str, Any]      		=					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_sequence_classification(*__UpperCAmelCase						)






							def       snake_case			(					self						)		->       Tuple:
														A      :					List[str]      		=					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_multiple_choice(*__UpperCAmelCase						)






