[Feature] Add SonicMoE support - #2058
Open
sallyjunjun wants to merge 4 commits 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.
Summary
This PR adds an optional SonicMoE expert backend for XTuner MoE models.
The feature is disabled by default. Existing MoE configs still use
expert_backend="grouped_gemm"unless users explicitly opt in withexpert_backend="sonicmoe", so the original MoE flow, dispatcher logic, checkpoint layout, and default training behavior remain unchanged.Implementation
The implementation introduces SonicMoE as a backend adapter instead of changing XTuner’s parameter ownership or checkpoint format.
Main changes:
expert_backendtoMoEConfig, with default value"grouped_gemm".SonicMoEBackendConfigandSonicMoEBackend.sonic-moepackage.expert_backend="sonicmoe".[E, 2I, H] / [E, H, I].topk_idsandtopk_weights.sonicmoe_cfg.routing_mode="token_rounding".The initial integration intentionally validates and rejects unsupported combinations:
ep_sizemust be1.expert_tp_sizemust be1.dispatchermust beNone.float8_cfgmust beNone.How To Enable
Install the optional dependency:
Optional SonicMoE routing config can be passed through sonicmoe_cfg:
from xtuner.v1.module.moe_backend import SonicMoEBackendConfig model = Qwen3MoE30BA3Config( expert_backend="sonicmoe", sonicmoe_cfg=SonicMoEBackendConfig( routing_mode="general", ), )For token rounding mode:
model = Qwen3MoE30BA3Config( expert_backend="sonicmoe", sonicmoe_cfg=SonicMoEBackendConfig( routing_mode="token_rounding", ), )Token rounding has additional validation requirements: greedy router, softmax scores, no grouped router selection, norm_topk_prob=True, and router_scaling_factor=1.0.
Tests And Validation
Added unit coverage in tests/module/test_sonicmoe_backend.py for:
A standalone benchmark/profiler is also added:
tests/profiler/qwen35_sonicmoe_fsdp_benchmark.py
It compares XTuner native grouped GEMM MoE vs SonicMoE on Qwen3.5 MoE FSDP using identical model weights and identical per-rank input batches. Each measured step performs forward and backward without optimizer update, so loss comparison is directly attributable to the expert backend implementation. The profiler emits both JSON
results and a markdown report including loss deltas, throughput, per-step latency, and SonicMoE speedup ratio.
Example:
XTUNER_DETERMINISTIC=true torchrun --nproc-per-node 8 \ tests/profiler/qwen35_sonicmoe_fsdp_benchmark.py \ --model-path /path/to/Qwen3.5-35B-A3B \ --output /path/to/qwen35_sonicmoe_fsdp_benchmark.json \ --steps 10 --warmup-steps 5 --deterministic