Skip to content

Conversation

@Jintao-Huang
Copy link
Collaborator

No description provided.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @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

  • Bugfix for MTP Model Saving: This pull request addresses a bug related to the saving mechanism for Megatron-LM (MTP) models, ensuring correct state persistence.
  • Conditional Output Layer Weight Assignment: The logic for setting the output_layer.weight during model conversion has been updated to be conditional. It will now only be explicitly set if the untie_embeddings_and_output_weights argument is true, preventing potential issues when embeddings and output weights are tied.

🧠 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 Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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.

Comment on lines 1392 to 1396
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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.

@Jintao-Huang Jintao-Huang merged commit 1a0a96d into modelscope:main Dec 31, 2025
1 of 3 checks passed
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

Successfully merging this pull request may close these issues.

2 participants