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 _lowerCamelCase  (		snake_case     , snake_case     , snake_case  ):







							if gpta_config_file == "":
														_lowerCAmelCase							=							GPTaConfig()
							else:
														_lowerCAmelCase							=							GPTaConfig.from_json_file(SCREAMING_SNAKE_CASE_  )
							_lowerCAmelCase							=							GPTaModel(SCREAMING_SNAKE_CASE_  )

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

							# Save pytorch-model
							_lowerCAmelCase							=							pytorch_dump_folder_path + '/' + WEIGHTS_NAME
							_lowerCAmelCase							=							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__":
				_lowercase:	Optional[Any]       			=		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:	Dict       			=		parser.parse_args()
				convert_gpta_checkpoint_to_pytorch(args.gpta_checkpoint_path, args.gpta_config_file, args.pytorch_dump_folder_path)













from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCAmelCase_           =	logging.get_logger(__name__)

UpperCAmelCase_           =	{
    "tiiuae/falcon-40b": "https://huggingface.co/tiiuae/falcon-40b/resolve/main/config.json",
    "tiiuae/falcon-7b": "https://huggingface.co/tiiuae/falcon-7b/resolve/main/config.json",
}




class 			__UpperCamelCase    (							A__    ):
     __A	:  Dict							  =   """falcon"""
     __A	:  Any							  =   ["""past_key_values"""]





     def __init__(     self				,							_UpperCamelCase=65024				,							_UpperCamelCase=4544				,							_UpperCamelCase=32				,							_UpperCamelCase=71				,							_UpperCamelCase=1e-5				,							_UpperCamelCase=0.02				,							_UpperCamelCase=True				,							_UpperCamelCase=0.0				,							_UpperCamelCase=0.0				,							_UpperCamelCase=None				,							_UpperCamelCase=False				,							_UpperCamelCase=False				,							_UpperCamelCase=True				,							_UpperCamelCase=True				,							_UpperCamelCase=False				,							_UpperCamelCase=11				,							_UpperCamelCase=11				,							**_UpperCamelCase				,							):
        _UpperCAmelCase           =   vocab_size
        # Backward compatibility with n_embed kwarg
        _UpperCAmelCase           =   kwargs.pop('''n_embed'''				,							_UpperCamelCase					)
        _UpperCAmelCase           =   hidden_size if n_embed is None else n_embed
        _UpperCAmelCase           =   num_hidden_layers
        _UpperCAmelCase           =   num_attention_heads
        _UpperCAmelCase           =   layer_norm_epsilon
        _UpperCAmelCase           =   initializer_range
        _UpperCAmelCase           =   use_cache
        _UpperCAmelCase           =   hidden_dropout
        _UpperCAmelCase           =   attention_dropout

        _UpperCAmelCase           =   bos_token_id
        _UpperCAmelCase           =   eos_token_id
        _UpperCAmelCase           =   num_attention_heads if num_kv_heads is None else num_kv_heads
        _UpperCAmelCase           =   alibi
        _UpperCAmelCase           =   new_decoder_architecture
        _UpperCAmelCase           =   multi_query  # Ignored when new_decoder_architecture is True
        _UpperCAmelCase           =   parallel_attn
        _UpperCAmelCase           =   bias

        super().__init__(bos_token_id=_UpperCamelCase				,							eos_token_id=_UpperCamelCase				,							**_UpperCamelCase					)





     @property
     def 		UpperCamelCase(     self					):
        return self.hidden_size // self.num_attention_heads





     @property
     def 		UpperCamelCase(     self					):
        return not self.alibi