feat(models): support TP for qwen3_5_moe - #104
Open
RuixiangMa wants to merge 1 commit into
Open
Conversation
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.
Problem
support TP for Qwen3.5 MoE model in RoadMap #79
Qwen3.5 MoE model (Qwen3.6-35B-A3B) only supported TP=1. The weight loader raised
NotImplementedError("qwen3_5_moe weight loading currently supports TP=1 only"), and all linear layers usedLinearReplicated(no sharding, no all-reduce).Implementation
Weight Sharding
Two sharding functions handle all weight types:
_shard_tp: Simple chunk along a given dimension (for o_proj, embed_tokens, lm_head, A_log, dt_bias, expert down_proj)._shard_tp_parts: Per-part column-parallel sharding with optional GQA KV head replication. Whennum_kv_heads < tp_size, KV heads are replicated usingrank * num_heads // world_size(matchingshard_tensorinloader.py).Qwen3.5's checkpoint stores pre-fused weights (qkv_proj, in_proj, gate_up_proj). Simple chunk on a fused weight gives incorrect results — e.g., chunking
[gate, up]gives rank 0 the entire gate and rank 1 the entire up, instead of each rank getting half of both. The fix shards each sub-part independently:Model Layers
Attention (
attention.py): TP-local head counts,_qkv_splitcomputed from local heads withallow_replicate=Truefor KV.o_projchanged frommake_replicatedtomake_row_parallel(row-parallel + all-reduce)..view()→.reshape()for non-contiguous tensors aftertorch.split.GDN (
gdn.py): TP-local dimensions for heads, key_dim, value_dim, conv_dim.in_projuses full sizes forLinearColParallelMerged(which handles its own sharding).conv1d,A_log,dt_biasuse TP-local sizes.out_projchanged frommake_replicated_quanttomake_row_parallel_quant. FP8 path also uses TP-local dims.MoE (
moe.py):OffloadMoELayer._maybe_all_reduceis a no-op (expert banks are full on each rank in offload mode).MoELayer(resident/fused) retains its own all-reduce.Test Results
UT
tests/models/test_qwen3_5_tp.py::test_shard_tp PASSED
tests/models/test_qwen3_5_tp.py::test_shard_tp_parts PASSED
tests/models/test_qwen3_5_tp.py::test_shard_tp_parts_replicate PASSED
E2E
Offload mode (default):
ft serve --model Qwen/Qwen3.6-35B-A3B --tp-size 1/2/4
Resident (fused) mode:
ft serve --model Qwen/Qwen3.6-35B-A3B --tp-size 1/2/4 --moe-backend fused
GPU Memory per Card
CC @jason-fxz @andy-yang-1