[Feature] Implement update_weights_from_disk for SGLang-D (Diffusion …#18306
Conversation
Summary of ChangesHello @dreamyang-liu, 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 introduces a significant enhancement to the SGLang-D diffusion engine by allowing dynamic updates of model weights from disk without requiring a server restart. This functionality is designed to streamline advanced machine learning workflows such as Reinforcement Learning and iterative fine-tuning, where frequent model adjustments are necessary. The changes ensure that models can be updated efficiently and reliably, maintaining operational continuity and providing a robust mechanism for managing model evolution. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe 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 by creating a comment using either
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 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. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. 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
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new feature to dynamically update model weights from disk for the diffusion engine, which is very useful for RL workflows and iterative fine-tuning. The implementation is comprehensive, covering an HTTP endpoint, scheduler handling, and the core logic in the GPU worker. It includes important features like atomic updates with rollback, handling of layerwise offload, and support for distributed tensors. The addition of a full suite of tests, including unit, integration, and end-to-end tests, is commendable and ensures the feature is robust.
I have found one critical issue in the logic for collecting modules to be updated, which could lead to partial updates. My review includes a suggestion to fix this. Overall, this is a great contribution.
python/sglang/multimodal_gen/test/server/test_update_weights_from_disk.py
Show resolved
Hide resolved
There was a problem hiding this comment.
One critical thing I wanna mention is that, do not copy yourself. I think a lot of code for update_weights_from_disk in LLM can be reused in Diffusion. Share these functions and do not copy these lines, which introduce quite a difficulty for maintenance.
If we can not reuse it, please also explain why. And, provide your running results of your tests, with the speed.
Thanks for the comments and suggestions @zhaochenyang20 ! I agree we don't want to reinvent the wheel, but following reasons prevent us from reusing more from LLM engine's implementation. 1. Missing
|
TestUnit TestTest with single A100 80G with model Speed Test (8 * A100): |
|
@dreamyang-liu, thanks for your detailed PR. Love to see your explanation. Could you adds that we also support update diffusion models weights in this docs: https://github.com/sgl-project/sglang/blob/main/docs/advanced_features/sglang_for_rl.md |
zhaochenyang20
left a comment
There was a problem hiding this comment.
Try to clean up the code and make it compact. thanks
python/sglang/multimodal_gen/runtime/entrypoints/http_server.py
Outdated
Show resolved
Hide resolved
python/sglang/multimodal_gen/runtime/entrypoints/http_server.py
Outdated
Show resolved
Hide resolved
python/sglang/multimodal_gen/runtime/entrypoints/http_server.py
Outdated
Show resolved
Hide resolved
python/sglang/multimodal_gen/runtime/entrypoints/http_server.py
Outdated
Show resolved
Hide resolved
python/sglang/multimodal_gen/runtime/entrypoints/http_server.py
Outdated
Show resolved
Hide resolved
@zhaochenyang20 Thanks for the suggestions! I just made some refactor and clean up, please check the revision when you get time. Thanks! |
python/sglang/multimodal_gen/runtime/entrypoints/http_server.py
Outdated
Show resolved
Hide resolved
|
@zhaochenyang20 @mickqian Thanks for the comments. I've made one more revision, please take a look when you get time and let me know if there're any other concerns. |
python/sglang/multimodal_gen/runtime/entrypoints/post_training/weights_api.py
Show resolved
Hide resolved
7e4ff9e to
5759564
Compare
python/sglang/multimodal_gen/runtime/entrypoints/post_training/weights_api.py
Outdated
Show resolved
Hide resolved
python/sglang/multimodal_gen/runtime/entrypoints/post_training/io_struct.py
Show resolved
Hide resolved
|
|
||
| @torch.compiler.disable | ||
| def update_cpu_weights(self, weight_dict: Dict[str, torch.Tensor]) -> Set[str]: | ||
| """Update consolidated CPU buffers with new weights. |
There was a problem hiding this comment.
Adds this here:
when layerwise offload (
--dit-layerwise-offload) is enabled, the diffusion offload manager replaces GPU parameters with smalltorch.empty((1,))placeholders while real weights live in consolidated pinned CPU buffers. A naiveparam.data.copy_()would fail with a shape mismatch. Instead, the updater dynamically detects active offload managers and writes new weights directly into their CPU buffers, bypassing the placeholders entirely. For any layer that happens to be prefetched on GPU at update time, the live GPU tensor is also updated so the change takes effect immediately. This requires no extra GPU memory and does not disturb the offload state.
|
After this PR is merged, I think there are two unit tests we can make up in following PRs: |
|
A further quesiton here: |
51c83bd to
666fb8b
Compare
…dreamyang-liu/sglang into feat/diffusion-update-weights-from-disk
…dreamyang-liu/sglang into feat/diffusion-update-weights-from-disk
|
We use two model pairs for testing (base model / instruct model pairs):
These model pairs share the same architecture but differ in transformer To strictly verify the correctness of the refit API, we compare the checksum in NOTE and TODO: In the refit a specific module test, we randomly select one module It should be good issue to solve for the community to adds comparison the server-disk |
…dreamyang-liu/sglang into feat/diffusion-update-weights-from-disk
|
Further profiling: |
| t = tensor.detach() | ||
| # DTensor doesn't support .numpy(); extract the local tensor. | ||
| if isinstance(t, DTensor): | ||
| t = t._local_tensor |
There was a problem hiding this comment.
Since some of the DTensors may be sharded across the devices and local_tensor is only the tensor on current device, do we need all-gather or some hash value merging logics here?
There was a problem hiding this comment.
Good question, let me check.
Motivation
Implement the
update_weights_from_diskinterface for SGLang-D (diffusion engine) to enable dynamic weight updates for RL workflows and iterative model fine-tuning without restarting the server.This feature is essential for:
Mirrors the existing LLM engine's
update_weights_from_diskfunctionality (seesglang/srt/managers/scheduler_update_weights_mixin.py).Closes #18078
Modifications
Core Implementation (
gpu_worker.py)update_weights_from_disk()method with:nn.Modulecomponents by default (transformer, vae, text_encoder, etc.)distribute_tensor()Request Handling
UpdateWeightsFromDiskReqdataclass in newio_struct.py/update_weights_from_diskHTTP endpoint inhttp_server.pyTests (
test_update_weights_from_disk.py)Accuracy Tests
N/A - This feature only reloads weights from disk without modifying model forward logic. The same weights produce identical outputs.
Tested the images generated before weight update and after the weight update are identical.
Tested with
on 8 * A100 80G machine.
Logging output example:
This log shows which modules are being updated and their corresponding weight directories, making it easy to verify the weight loading source for each module.
Example request
Benchmarking and Profiling
N/A - This is an operational API for weight management, not a performance-critical path. Weight loading time depends on model size and storage I/O.
Checklist