Add Multi-Token Prediction (MTP) support for Qwen3.5#45637
Closed
curnane-lab wants to merge 1 commit intohuggingface:mainfrom
Closed
Add Multi-Token Prediction (MTP) support for Qwen3.5#45637curnane-lab wants to merge 1 commit intohuggingface:mainfrom
curnane-lab wants to merge 1 commit intohuggingface:mainfrom
Conversation
Add MTP architecture and loss computation for Qwen3.5 models, enabling multi-token prediction during training for improved efficiency. Changes: - Add Qwen3_5MTPLayer and Qwen3_5MTP module classes - Add shared _compute_qwen35_mtp_loss() helper function - Add MTP support to Qwen3_5ForCausalLM (text-only model) - Add MTP support to Qwen3_5ForConditionalGeneration (VL model) - Add mtp_num_hidden_layers and mtp_loss_weight config fields - Remove mtp from _keys_to_ignore_on_load_unexpected in CausalLM - Regenerate modeling_qwen3_5.py and configuration_qwen3_5.py
Contributor
|
[For maintainers] Suggested jobs to run (before merge) run-slow: qwen3_5 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add Multi-Token Prediction (MTP) support for Qwen3.5
This PR adds Multi-Token Prediction (MTP) architecture and loss computation for Qwen3.5 models, enabling multi-token prediction during training for improved efficiency.
Changes
New classes:
Qwen3_5MTPLayer: Single MTP transformer layer with attention and MLPQwen3_5MTP: Top-level MTP module with FC fusion, layers, and normNew shared helper:
_compute_qwen35_mtp_loss(): Shared MTP loss computation function used by both CausalLM and VL models, eliminating code duplicationModified models:
Qwen3_5ForCausalLM: Added MTP initialization and loss computation in forward passQwen3_5ForConditionalGeneration: Added MTP initialization and loss computation in forward passConfiguration:
mtp_num_hidden_layers(default: 0) andmtp_loss_weight(default: 0.0) to bothQwen3_5TextConfigandQwen3_5Configmtpfrom_keys_to_ignore_on_load_unexpectedinQwen3_5ForCausalLMso MTP weights are properly loaded from checkpointsDesign decisions
Shared loss function: The
_compute_qwen35_mtp_loss()helper eliminates code duplication between the text-only and VL models. Both models delegate to this shared function with their respectiveembed_tokensandrotary_embreferences.MTP loss stays in model files: Following the pattern of other auxiliary losses in transformers (e.g., MoE router losses), MTP loss is computed within the model's forward pass rather than in a separate trainer class.
Backward compatible: With
mtp_num_hidden_layers=0(default), MTP is disabled and the models behave identically to before.Checkpoint alignment: The MTP module structure aligns with the Qwen3.5 checkpoint format:
mtp.pre_fc_norm_hidden.*mtp.pre_fc_norm_embedding.*mtp.fc.*mtp.layers.N.*mtp.norm.*Testing
Tested with Qwen3.5-MTP model checkpoints to verify weight loading and loss computation.