[QUARK-493] Fix Qwen3 MXFP4 MoE weight loading with TP 4/8#309
Open
[QUARK-493] Fix Qwen3 MXFP4 MoE weight loading with TP 4/8#309
Conversation
valarLip
previously approved these changes
Mar 12, 2026
When loading MXFP4-quantized Qwen3-235B MoE weights with tensor parallelism (TP=4 or TP=8), the _load_w13 and _load_w2 methods crashed with: RuntimeError: start (1536) + length (256) exceeds dimension size (1536) Root cause: The .narrow() calls used expert_data.shape (which is padded to MXFP4 block alignment) to compute the per-TP-rank shard offset and size. For MXFP4, expert_data is rounded up to the next block boundary, making shard_size larger than the actual loaded weight dimension. Multiplying this padded shard_size by tp_rank produced an out-of-bounds start index. Fix: Compute loaded_shard_size from the loaded_weight tensor's actual dimension (loaded_weight.shape[shard_dim] // tp_size) instead of the padded expert_data dimension. Use loaded_shard_size for the .narrow() on loaded_weight. When copying into expert_data, narrow expert_data to loaded_shard_size if it exceeds the loaded shard, ensuring only the valid (unpadded) region is written. Applied to both _load_w13 (gate/up projection) and _load_w2 (down projection) code paths.
dd61e5b to
1928a31
Compare
Contributor
Author
|
Looks like the same fix was merged into main before I merged this PR. I will then keep the unit tests if that's ok |
Contributor
hi, thiago, you can click the re-run button to restart the test. It appears the CI failure is not due to this PR. |
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.
When loading MXFP4-quantized Qwen3-235B MoE weights with tensor parallelism (TP=4 or TP=8), the _load_w13 and _load_w2 methods crashed with:
RuntimeError: start (1536) + length (256) exceeds dimension size (1536)
Root cause: The .narrow() calls used expert_data.shape (which is padded to MXFP4 block alignment) to compute the per-TP-rank shard offset and size. For MXFP4, expert_data is rounded up to the next block boundary, making shard_size larger than the actual loaded weight dimension. Multiplying this padded shard_size by tp_rank produced an out-of-bounds start index.
Fix: Compute loaded_shard_size from the loaded_weight tensor's actual dimension (loaded_weight.shape[shard_dim] // tp_size) instead of the padded expert_data dimension. Use loaded_shard_size for the .narrow() on loaded_weight. When copying into expert_data, narrow expert_data to loaded_shard_size if it exceeds the loaded shard, ensuring only the valid (unpadded) region is written.
Applied to both _load_w13 (gate/up projection) and _load_w2 (down projection) code paths.
Motivation
Technical Details
Test Plan
Test Result
Server
lm-eval
Submission Checklist