Add TP8 online training support for the Qwen3-MoE model.#74
Add TP8 online training support for the Qwen3-MoE model.#74sleepcoo merged 6 commits intosgl-project:mainfrom
Conversation
ZhengHSI
commented
Jul 28, 2025
- Add TP8 online training support for the Qwen3-MoE model.Since Qwen3-MoE has num_key_value_heads < 8, the attention layer replicates each KV head across ranks instead of sharding them.
- Qwen3-235B-A22B online training is also supported
- You can use SpecForge/tests/test_qwen3_tp.py to test the correctness of TP parallelism.
There was a problem hiding this comment.
Summary of Changes
Hello @ZhengHSI, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request significantly extends the training capabilities for large language models, specifically the Qwen3-MoE and Qwen3-235B-A22B models, by introducing robust support for TP8 (Tensor Parallelism) online training. The core changes involve adapting the parallel linear layers and the model's weight loading process to intelligently handle key-value head replication when the number of available GPUs exceeds the model's native key-value head count, ensuring efficient and correct distributed training.
Highlights
- Qwen3-MoE TP8 Training: Implemented support for TP8 (Tensor Parallelism with 8 GPUs) online training for the Qwen3-MoE model. This includes specific handling for scenarios where the number of key-value heads is less than the TP size, by replicating KV heads across ranks rather than sharding them.
- Qwen3-235B-A22B Support: Added foundational support for online training of the Qwen3-235B-A22B model, including a commented-out example script in
run_qwen3_moe_eagle3_online.shdemonstrating TP8 training for this model. - Generalized Parallel Linear Layers: Enhanced the
RowParallelLinearandColumnParallelLinearmodules to accept akv_head_replicasflag. This allows these layers to dynamically adjust their sharding behavior, enabling proper handling of replicated key-value heads in attention mechanisms. - Attention Weight Sharding Logic: Updated the
Qwen3MoeForCausalLM's weight loading mechanism to correctly shard or replicate attention layer weights (q_proj,k_proj,v_proj,o_proj). This includes a new conditional path fork_projandv_projto manage KV head replication by selecting the appropriate head slice whentp_sizeexceeds the total number of KV heads.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Code Review
This pull request adds TP8 online training support for the Qwen3-MoE model. A critical bug was identified in the tensor parallelism logic that could lead to runtime errors, and a detailed suggestion for a fix has been provided. There are also a few medium-severity recommendations to improve code maintainability and usability.
| # --standalone \ | ||
| # --nproc_per_node $NUM_GPUS \ | ||
| # $ROOT_DIR/scripts/train_eagle3_online.py \ | ||
| # --target-model-path /root/model/Qwen3-235B-A22B \ |
There was a problem hiding this comment.
The model path is hardcoded. This makes the script less portable and harder to use for others who may have the model stored in a different location. It's a good practice to use an environment variable for paths like this, with a sensible default if needed.
| # --target-model-path /root/model/Qwen3-235B-A22B \ | |
| # --target-model-path ${TARGET_MODEL_PATH:-/root/model/Qwen3-235B-A22B} \ |
| def __init__( | ||
| self, | ||
| in_features, | ||
| out_features, | ||
| bias=True, | ||
| device=None, | ||
| dtype=None, | ||
| kv_head_replicas=False, | ||
| ): |
There was a problem hiding this comment.
The kv_head_replicas parameter and its associated logic (at lines 28-31) are not used for RowParallelLinear anywhere in this pull request. This layer is used for o_proj and MoE down_proj, neither of which requires this special handling for KV head replication.
To improve clarity and avoid maintaining unused code, I recommend removing this parameter and its conditional logic from RowParallelLinear.
| layer_match = key.split(".") | ||
| layer_idx = None | ||
| for i, part in enumerate(layer_match): | ||
| if part.startswith("layers") and i + 1 < len(layer_match): |
There was a problem hiding this comment.
Using part == "layers" is more precise and robust than part.startswith("layers"). While startswith works in this context, using an exact match better conveys the intent to find a specific segment of the model key and prevents potential mismatches if other keys were to begin with "layers" (e.g., "layers_config").
| if part.startswith("layers") and i + 1 < len(layer_match): | |
| if part == "layers" and i + 1 < len(layer_match): |
|
Any plan to add TP8 online training support for the Qwen3-Dense model? |
coming soon |
|
I trained for 1 epoch on ShareGPT and obtained the following result. |