[3/3] Optimize Slime Update Weights: Load Weight in non-blocking mode#8754
[3/3] Optimize Slime Update Weights: Load Weight in non-blocking mode#8754
Conversation
There was a problem hiding this comment.
Summary of Changes
Hello @hebiao064, 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 implements a performance optimization for the SGLang runtime by enabling non-blocking weight loading for Mixture-of-Experts (MoE) layers. The primary goal is to enhance the efficiency of weight transfers to the GPU, which can lead to reduced latency during model initialization or dynamic weight updates, by allowing the CPU and GPU to operate concurrently.
Highlights
- Non-blocking Weight Loading: Modified the
_load_w13and_load_w2functions withinpython/sglang/srt/layers/moe/fused_moe_triton/layer.pyto utilizenon_blocking=Truewhen copying expert weights. This change allows the CPU to proceed with other tasks while the GPU asynchronously handles the data transfer ofw13andw2weights, aiming to improve overall efficiency. - CUDA Synchronization for Data Integrity: Introduced a
torch.cuda.synchronize()call in theload_weightsmethod ofpython/sglang/srt/models/qwen3_moe.py. This explicit synchronization point ensures that all asynchronously loaded weights are fully transferred to the GPU and ready for use before subsequent operations, preventing potential race conditions or data inconsistencies that could arise from non-blocking transfers.
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 optimizes weight loading by using non-blocking copies, which is a good performance improvement. The changes are in fused_moe_triton/layer.py to enable asynchronous copies and in qwen3_moe.py to add the necessary synchronization.
My review identifies two main issues:
- The change to non-blocking copies in the shared
fused_moe_triton/layer.pyfile is a breaking change for other models (likeqwen2_moe) that use this layer but haven't been updated with a synchronization call. This is a critical issue that could lead to race conditions. - The new synchronization call in
qwen3_moe.pyis not conditional on CUDA being available, which will cause a crash in CPU-only environments.
I've provided suggestions to address these points.
|
|
||
| expert_data = expert_data.narrow(shard_dim, start, shard_size) | ||
| expert_data.copy_(loaded_weight) | ||
| expert_data.copy_(loaded_weight, non_blocking=True) |
There was a problem hiding this comment.
Using non_blocking=True is a good optimization for overlapping data transfers. However, this makes the copy asynchronous and requires explicit synchronization before the weights can be safely used.
I see that torch.cuda.synchronize() has been added to qwen3_moe.py, which is correct. But since this change is in a shared layer file, it will affect all models that use FusedMoE. For example, qwen2_moe.py also seems to use this layer, but its load_weights method does not have a synchronization call.
This could lead to race conditions where weights are used before they are fully copied to the device. Please ensure that all models using FusedMoE are updated to include proper synchronization after weight loading to prevent this.
|
|
||
| # w2, down_proj: Load into only logical weight of w2. | ||
| expert_data.copy_(loaded_weight) | ||
| expert_data.copy_(loaded_weight, non_blocking=True) |
There was a problem hiding this comment.
Using non_blocking=True is a good optimization for overlapping data transfers. However, this makes the copy asynchronous and requires explicit synchronization before the weights can be safely used.
I see that torch.cuda.synchronize() has been added to qwen3_moe.py, which is correct. But since this change is in a shared layer file, it will affect all models that use FusedMoE. For example, qwen2_moe.py also seems to use this layer, but its load_weights method does not have a synchronization call.
This could lead to race conditions where weights are used before they are fully copied to the device. Please ensure that all models using FusedMoE are updated to include proper synchronization after weight loading to prevent this.
| logger.warning(f"Parameter {name} not found in params_dict") | ||
|
|
||
| # Synchronize to ensure all weights are loaded since we loaded them in non-blocking mode | ||
| torch.cuda.synchronize() |
There was a problem hiding this comment.
This synchronization call is necessary for the non-blocking copies to work correctly. However, it will raise an error if the code is run in a CPU-only environment where CUDA is not available.
To prevent this, you should guard this call with a check for CUDA availability. The _is_cuda variable is already defined in this file for this purpose.
| torch.cuda.synchronize() | |
| if _is_cuda: | |
| torch.cuda.synchronize() |
| logger.warning(f"Parameter {name} not found in params_dict") | ||
|
|
||
| # Synchronize to ensure all weights are loaded since we loaded them in non-blocking mode | ||
| torch.cuda.synchronize() |
There was a problem hiding this comment.
we need to add the synchronize to all moe models, e.g. DeepSeekV3, GLM4MoE.
|
no improvement from my latest benchmark, hence close it |
Motivation
This PR maps to the optimization 3

Modifications
Accuracy Test
Benchmark & Profiling
Checklist