Skip to content

[3/3] Optimize Slime Update Weights: Load Weight in non-blocking mode#8754

Closed
hebiao064 wants to merge 1 commit intomainfrom
bhe/load_weight_non_blocking
Closed

[3/3] Optimize Slime Update Weights: Load Weight in non-blocking mode#8754
hebiao064 wants to merge 1 commit intomainfrom
bhe/load_weight_non_blocking

Conversation

@hebiao064
Copy link
Copy Markdown
Collaborator

@hebiao064 hebiao064 commented Aug 4, 2025

Motivation

This PR maps to the optimization 3
Screenshot 2025-08-03 at 11 48 58 PM

Modifications

Accuracy Test

Benchmark & Profiling

Checklist

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_w13 and _load_w2 functions within python/sglang/srt/layers/moe/fused_moe_triton/layer.py to utilize non_blocking=True when copying expert weights. This change allows the CPU to proceed with other tasks while the GPU asynchronously handles the data transfer of w13 and w2 weights, aiming to improve overall efficiency.
  • CUDA Synchronization for Data Integrity: Introduced a torch.cuda.synchronize() call in the load_weights method of python/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

  1. 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.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. The change to non-blocking copies in the shared fused_moe_triton/layer.py file is a breaking change for other models (like qwen2_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.
  2. The new synchronization call in qwen3_moe.py is 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)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

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)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

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()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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.

Suggested change
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()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to add the synchronize to all moe models, e.g. DeepSeekV3, GLM4MoE.

@hebiao064
Copy link
Copy Markdown
Collaborator Author

no improvement from my latest benchmark, hence close it

@hebiao064 hebiao064 closed this Aug 26, 2025
@zhyncs zhyncs deleted the bhe/load_weight_non_blocking branch August 26, 2025 07:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants