




from math import pi






def    snake_case   (				A     )				->				float:




	'''simple docstring'''


	return degree / (180 / pi)


if __name__ == "__main__":
							from doctest import testmod

							testmod()







'''simple docstring'''






import argparse
import os
from os.path import isfile

import torch

from transformers import GPTaConfig
def 				_lowerCamelCase(	UpperCamelCase__     :			List[str]     ,							UpperCamelCase__     :			Optional[Any]     ,							UpperCamelCase__     :			List[str]=0		)		->	Optional[int]:
    # Format the message.
    if name is None:
        A							:							Dict				=    None
    else:
        A							:							Optional[Any]				=    '''.''' * max(0     ,							spaces - 2		) + '''# {:''' + str(50 - spaces		) + '''s}'''
        A							:							Tuple				=    fmt.format(UpperCamelCase__		)

    # Print and recurse (if needed).
    if isinstance(UpperCamelCase__     ,							UpperCamelCase__		):
        if msg is not None:
            print(UpperCamelCase__		)
        for k in val.keys():
            recursive_print(UpperCamelCase__     ,							val[k]     ,							spaces + 2		)
    elif isinstance(UpperCamelCase__     ,							torch.Tensor		):
        print(UpperCamelCase__     ,							''':'''     ,							val.size()		)
    else:
        print(UpperCamelCase__     ,							''':'''     ,							UpperCamelCase__		)
def 				_lowerCamelCase(	UpperCamelCase__     :			Dict     ,							UpperCamelCase__     :			List[Any]     ,							UpperCamelCase__     :			Dict     ,							UpperCamelCase__     :			List[str]		)		->	Dict:
    # Permutes layout of param tensor to [num_splits * num_heads * hidden_size, :]
    # for compatibility with later versions of NVIDIA Megatron-LM.
    # The inverse operation is performed inside Megatron-LM to read checkpoints:
    # https://github.com/NVIDIA/Megatron-LM/blob/v2.4/megatron/checkpointing.py#L209
    # If param is the weight tensor of the self-attention block, the returned tensor
    # will have to be transposed one more time to be read by HuggingFace GPT2.
    A							:							int				=    param.size()
    # other versions store [num_heads * num_splits * hidden_size, :]
    A							:							List[str]				=    (num_heads, num_splits, hidden_size) + input_shape[1:]
    A							:							Optional[int]				=    param.view(*UpperCamelCase__		)
    A							:							Optional[Any]				=    param.transpose(0     ,							1		).contiguous()
    A							:							Dict				=    param.view(*UpperCamelCase__		)
    return param
def 				_lowerCamelCase(	UpperCamelCase__     :			Any     ,							UpperCamelCase__     :			Tuple		)		->	Optional[int]:
    A							:							int				=    config.n_positions
    A							:							Dict				=    config.n_layer
    A							:							List[str]				=    config.vocab_size
    A							:							Optional[Any]				=    config.n_head
    A							:							Union[str, Any]				=    config.n_embd // config.n_head

    A							:							Optional[int]				=    sd_megatron['''model.language_model.embedding.word_embeddings.weight'''][:vocab_size, :]
    A							:							Any				=    {
        '''transformer.wte.weight''': word_embeddings,
        '''transformer.wpe.weight''': sd_megatron['''model.language_model.embedding.position_embeddings.weight'''],
        '''transformer.ln_f.weight''': sd_megatron['''model.language_model.encoder.final_layernorm.weight'''],
        '''transformer.ln_f.bias''': sd_megatron['''model.language_model.encoder.final_layernorm.bias'''],
    }

    A							:							Optional[int]				=    '''model.language_model.encoder.layers.'''
    for i in range(UpperCamelCase__		):
        A							:							Dict				=    torch.tril(torch.ones((n_positions, n_positions)     ,							dtype=torch.bool		)		)
        A							:							Any				=    causal_mask.view(1     ,							1     ,							UpperCamelCase__     ,							UpperCamelCase__		)
        A							:							List[Any]				=    causal_mask
        A							:							List[str]				=    torch.tensor(-1e4     ,							dtype=torch.bfloataa		)

        A							:							Optional[int]				=    sd_megatron[F'''{pf}{i}.input_layernorm.weight''']
        A							:							Optional[int]				=    sd_megatron[F'''{pf}{i}.input_layernorm.bias''']

        A							:							Any				=    sd_megatron[F'''{pf}{i}.self_attention.query_key_value.weight''']
        A							:							List[Any]				=    fix_query_key_value_ordering(UpperCamelCase__     ,							3     ,							UpperCamelCase__     ,							UpperCamelCase__		)
        A							:							List[Any]				=    vala.transpose(0     ,							1		).contiguous()

        A							:							List[Any]				=    sd_megatron[F'''{pf}{i}.self_attention.query_key_value.bias''']
        A							:							Any				=    fix_query_key_value_ordering(UpperCamelCase__     ,							3     ,							UpperCamelCase__     ,							UpperCamelCase__		)
        A							:							List[Any]				=    vala

        A							:							Tuple				=    sd_megatron[F'''{pf}{i}.self_attention.dense.weight'''].transpose(
            0     ,							1		)
        A							:							Optional[int]				=    sd_megatron[F'''{pf}{i}.self_attention.dense.bias''']
        A							:							Any				=    sd_megatron[F'''{pf}{i}.post_attention_layernorm.weight''']
        A							:							Any				=    sd_megatron[F'''{pf}{i}.post_attention_layernorm.bias''']
        A							:							str				=    sd_megatron[F'''{pf}{i}.mlp.dense_h_to_4h.weight'''].transpose(0     ,							1		)
        A							:							List[Any]				=    sd_megatron[F'''{pf}{i}.mlp.dense_h_to_4h.bias''']
        A							:							Tuple				=    sd_megatron[F'''{pf}{i}.mlp.dense_4h_to_h.weight'''].transpose(
            0     ,							1		)
        A							:							Union[str, Any]				=    sd_megatron[F'''{pf}{i}.mlp.dense_4h_to_h.bias''']

    # For LM head, transformers' wants the matrix to weight embeddings.
    A							:							Optional[int]				=    word_embeddings

    return sd_hf
def 				_lowerCamelCase(	UpperCamelCase__     :			Union[str, Any]     ,							UpperCamelCase__     :			Optional[Any]		)		->	Union[str, Any]:
    A							:							List[str]				=    64_000
    A							:							str				=    config_megatron['''encoder_seq_length''']
    A							:							Union[str, Any]				=    config_megatron['''hidden_size''']
    A							:							Tuple				=    config_megatron['''num_layers''']
    A							:							List[str]				=    config_megatron['''num_attention_heads''']
    A							:							List[Any]				=    config_megatron['''ffn_hidden_size''']
    A							:							List[str]				=    '''gelu'''
    A							:							int				=    0.1
    A							:							Union[str, Any]				=    0.1
    A							:							Optional[int]				=    0.1
    A							:							Optional[int]				=    config_megatron['''layernorm_epsilon''']  # 1e-5
    A							:							Tuple				=    config_megatron['''init_method_std''']  # 0.02
    A							:							Any				=    config_megatron['''apply_query_key_layer_scaling''']  # True
    A							:							Tuple				=    True
    A							:							str				=    True

    # This identifies the 6.7B (7B) model which uses a different tokenizer
    if config_megatron["hidden_size"] == 4_096:
        A							:							Tuple				=    1  # <|endoftext|>
        A							:							Any				=    1  # <|endoftext|>
        A							:							List[str]				=    0  # <unk>
    else:
        A							:							Any				=    2  # <s>
        A							:							str				=    3  # <|endoftext|>
        A							:							Tuple				=    0  # <pad>

    return config_hf
def 				_lowerCamelCase(	UpperCamelCase__     :			List[Any]		)		->	Tuple:
    print(UpperCamelCase__		)

    A							:							Tuple				=    args.checkpoint_path
    A							:							Dict				=    args.save_path
    if isfile(UpperCamelCase__		):
        raise FileNotFoundError(F'''ERROR! could not find file {checkpoint_path}'''		)

    # Load the model.
    A							:							str				=    torch.load(UpperCamelCase__     ,							map_location='''cpu'''		)

    # Load the config.
    A							:							List[str]				=    checkpoint['''hyper_parameters''']['''cfg''']
    A							:							Tuple				=    GPTaConfig()
    A							:							int				=    copy_config(config_hf=UpperCamelCase__     ,							config_megatron=UpperCamelCase__		)
    A							:							Any				=    ['''GPT2LMHeadModel''']

    A							:							Optional[int]				=    checkpoint['''state_dict''']

    # Convert.
    print('''Converting'''		)
    A							:							Any				=    convert_megatron_checkpoint(UpperCamelCase__     ,							UpperCamelCase__		)

    # Print the structure of converted state dict.
    if args.print_checkpoint_structure:
        recursive_print(UpperCamelCase__     ,							UpperCamelCase__		)

    A							:							Union[str, Any]				=    '''GPTSw3Tokenizer'''

    # Store the config to file.
    print('''Saving config'''		)
    config_hf.save_pretrained(UpperCamelCase__		)

    # Store the state_dict to file.
    A							:							int				=    os.path.join(UpperCamelCase__     ,							'''pytorch_model.bin'''		)
    print(F'''Saving checkpoint to "{output_checkpoint_file}"'''		)
    torch.save(UpperCamelCase__     ,							UpperCamelCase__		)


if __name__ == "__main__":
     snake_case_     			=						argparse.ArgumentParser()
     parser.add_argument(
         """--checkpoint_path""",
         type=str,
         required=True,
         help="""e.g. megatron_gpt--val_loss=2.42-step=38000-consumed_samples=54720000""",
     )
     parser.add_argument("""--save_path""", type=str, required=True, help="""e.g. /home/user/gpt-sw3/hf""")
     parser.add_argument("""--print-checkpoint-structure""", action="""store_true""")
     snake_case_     			=						parser.parse_args()
     main(_args)
