Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Megatron-LM权重转hf格式 #52

Open
Yang-QW opened this issue Dec 2, 2023 · 4 comments
Open

Megatron-LM权重转hf格式 #52

Yang-QW opened this issue Dec 2, 2023 · 4 comments

Comments

@Yang-QW
Copy link

Yang-QW commented Dec 2, 2023

请问用原始的Megatron-LM训练的llama2可以使用megatron_to_hf.sh脚本转换吗?目前遇到了一点问题,有些参数的格式不一样导致脚本运行报错,自己按理解改了一下,改完可以进行转换了,但转换后的hf模型输出有问题。

下面是我改动的地方,感觉前两处改动应该没有问题,参数名都比较接近,最后一个改动不知道对不对,参数的shape能对上,改完后输出全重复一个token

image
image
image

请问上面的改动有没有问题?能否提供一个支持Megatron-LM的转换脚本?

@li-yi-dong
Copy link
Collaborator

两边QKV projection view 的方式不同,tensor 的size 相同但里面的layout 可能不同。
最后一层linear 你取的参数应该是word_emb 的,正确的可以参考:https://github.com/NVIDIA/Megatron-LM/blob/main/megatron/model/gpt_model.py#L91C10-L91C10
最新的megatron-lm 里提供了从hf 转到他支持的ckpt 格式,可以参考这个转回去:https://github.com/NVIDIA/Megatron-LM/blob/main/tools/checkpoint/loader_llama2_hf.py

@PKU-Fgx
Copy link

PKU-Fgx commented Mar 14, 2024

请问这个问题解决了吗,我也遇到了同样的情况

@Yang-QW
Copy link
Author

Yang-QW commented Mar 14, 2024

请问这个问题解决了吗,我也遇到了同样的情况
image
image
image
我改了代码里上面几个地方,试了一下转换前后输出一致。你可以试一下,太久没看了,不一定好用

@PKU-Fgx
Copy link

PKU-Fgx commented Mar 15, 2024

我训的时候没有加 untie-embeddings-and-output-weights 参数,没有 output_layer 这一层,但是我按照 alibaba/Pai-Megatron-Patch 这个仓库的 convert_to_hf 方式,改了一下 QKV Tensor 的 Layerout 就正常了
Anyway,还是十分感谢 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants