-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[bugfix] fix mtp save #7267
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
[bugfix] fix mtp save #7267
Conversation
Summary of ChangesHello @Jintao-Huang, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request implements a crucial bug fix within the Megatron-LM (MTP) model saving process. The change refines how the output layer weights are handled during model conversion, ensuring that these weights are only explicitly assigned when the model configuration specifies that embedding and output weights are untied. This adjustment prevents incorrect state management and ensures the integrity of saved models under various configurations. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request aims to fix an issue with saving MTP (Multi-Token Prediction) models by conditionally saving the output layer weights only when they are not tied to the embedding weights. The change itself is correct and aligns with how tied weights are handled elsewhere in the codebase.
However, I've identified a more significant underlying issue. The logic for saving these global weights (embedding.word_embeddings.weight and output_layer.weight) is located within a per-layer conversion function (_convert_mtp_layer). This causes the weights to be saved with an incorrect, layer-specific prefix, which will likely lead to corrupted model checkpoints. I've left a comment with a recommendation to refactor this by moving the logic outside of the per-layer loop. Addressing this is crucial for ensuring the correctness of saved MTP models.
swift/megatron/model/gpt_bridge.py
Outdated
| if not to_mcore and not self.args.hf_model_type.startswith('qwen3_next'): | ||
| self._set_state_dict(lm_model, 'embedding.word_embeddings.weight', hf_state_dict, 'embed_tokens.weight', | ||
| to_mcore) | ||
| self._set_state_dict(lm_model, 'output_layer.weight', hf_state_dict, 'shared_head.head.weight', to_mcore) | ||
| if self.args.untie_embeddings_and_output_weights: | ||
| self._set_state_dict(lm_model, 'output_layer.weight', hf_state_dict, 'shared_head.head.weight', to_mcore) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block of code appears to be misplaced. The _convert_mtp_layer method is executed within a loop for each MTP layer, and it applies a layer-specific prefix to the keys in hf_state_dict.
The weights for embedding.word_embeddings and output_layer are global to the language model, not per-layer. Saving them here results in incorrect keys in the state dictionary, such as model.layers.{layer_idx}.embed_tokens.weight, which will likely cause issues when loading the model.
This logic should be moved out of the _convert_mtp_layer method and the associated loop in _convert. These global weights should be handled only once, with the correct prefix.
While your change to conditionally save the output_layer.weight is a correct fix for tied weights, the fundamental issue of saving global weights in a per-layer function needs to be addressed.
No description provided.