Open
Conversation
…ayers Qwen3-Next's full attention and MTP layers use gated attention (linear_qgkv) with Q+G interleaved, unlike the linear attention layers that use linear_qkv. This adds the missing QGKV per-group layout conversion for weight sync. megatron_to_hf/qwen3_next.py: - Add _convert_qgkv_weight_to_hf() for Megatron per-group layout [Q_g0, G_g0, K_g0, V_g0, ...] to HF [Q+G interleaved, K, V] - Add linear_qgkv.weight and linear_qgkv.layer_norm_weight handlers mbridge/qwen3_next.py: - Add linear_qgkv entries to _ATTENTION_MAPPING - Add _weight_to_mcore_format for gated QGKV (HF to Megatron direction) - Fix _get_gptmodel_args to create MTP config with use_gated_attention=True model_provider.py: - Create separate mtp_config with use_gated_attention=True for MTP block spec
Contributor
|
Good job! I will help review this. |
guapisolo
reviewed
Feb 12, 2026
| v = all_v.reshape(num_kv_heads * head_dim, hidden_size).contiguous() | ||
|
|
||
| return [ | ||
| (f"{prefix}.self_attn.q_proj.weight", qg), |
guapisolo
reviewed
Feb 12, 2026
|
|
||
| weight = super()._weight_to_mcore_format(mcore_weights_name, hf_weights) | ||
| if mcore_weights_name.endswith("eh_proj.weight"): | ||
| first_half, second_half = weight.chunk(2, dim=1) |
guapisolo
reviewed
Feb 12, 2026
Guard against division by zero in MTP loss computation when num_tokens is 0, which can happen with context parallelism when one CP rank has no response tokens after label rolling.
7f120ed to
a4f2fd8
Compare
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.
Bug:
linear_qgkvnot handled in weight converterProblem
Upstream's
scripts/models/qwen3-next-80B-A3B.shenables--use-gated-attention, which causes Megatron to create parameters namedlinear_qgkv.weightinstead oflinear_qkv.weight. But the weight converters (megatron_to_hf/qwen3_next.pyandmbridge/qwen3_next.py) only handlelinear_qkv, causing a crash during weight sync.Root Cause
In Megatron's
megatron/core/transformer/attention.py(line 891):MTP layers inherit this config, so they also use
linear_qgkv.Reproduction
Test script that loads the upstream converter and calls it with
linear_qgkvparameter names (as produced by Megatron when--use-gated-attentionis set):Output (before fix)
Summary
After fix
Bug: Incorrect
eh_proj.weighthalf-swap in MTP conversionProblem
The
_convert_mtp_layerfunction inmegatron_to_hf/qwen3_next.pyand_weight_to_mcore_format/_weight_to_hf_formatinmbridge/qwen3_next.pyswap the two halves ofeh_proj.weightalong dim=1 when converting between HF and Megatron formats:This swap is incorrect — both HF and Megatron use the same input order
[embedding, hidden_state].Evidence
HF (SGLang) forward (
sglang/python/sglang/srt/models/qwen3_next_mtp.py):Megatron forward (
megatron/core/transformer/multi_token_prediction.py):Both concatenate
[embedding, hidden_state]— same order. No swap needed.Validation
Fix
Remove the half-swap from both
megatron_to_hf/qwen3_next.pyandmbridge/qwen3_next.py. Passeh_proj.weightthrough directly without modification.Results
With these two fixes, we can try MTP layer of Qwen3-Next-80B-A3B with a reasonable loss.

