Skip to content

[Fix]: Inline H2D during CUDA graph capture to avoid stream isolation in Offloader - #29166

Merged
Fridge003 merged 2 commits into
sgl-project:mainfrom
Dovis01:fix-offload-cg
Jul 1, 2026
Merged

Fridge003 merged 2 commits into
sgl-project:mainfrom
Dovis01:fix-offload-cg

Conversation

@Dovis01

@Dovis01 Dovis01 commented Jun 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

CPU weight offload prefetches parameters on alt_stream and synchronizes via CUDA events. During decode CUDA graph capture, waiting on events recorded on alt_stream (or prefetching there) creates a dependency on uncaptured work and aborts with cudaErrorStreamCaptureIsolation.

When the current stream is capturing, materialize weights inline on the capture stream in start_onload and wait_and_get_device_tensors instead of using cross-stream prefetch/wait. Non-capture inference keeps the existing alt_stream overlap path unchanged.

Root Cause

The CPU offloader pipeline is:

  1. At the end of layer N forward: start_onload() prefetches layer N + prefetch_step weights on alt_stream and records a CUDA event.
  2. At the start of layer N + prefetch_step forward: wait_and_get_device_tensors() waits on that event, then runs functional_call with the GPU weights.

CUDA graph capture only records work on the capture stream. The original code always used alt_stream for prefetch and always called _load_event.wait() on the capture stream. This violates stream-capture isolation when:

  • prefetch is issued on alt_stream during capture, or
  • a prefetch/event was created during warmup before capture starts, and capture later tries to wait on that out-of-capture event.

Changes

python/sglang/srt/utils/offloader.py

  • _ModuleOffloader.start_onload(): If the current stream is capturing a CUDA graph, load weights inline on the capture stream and skip alt_stream prefetch/event recording. Otherwise, keep the existing async prefetch on alt_stream.

  • _ModuleOffloader.wait_and_get_device_tensors(): If capturing, avoid waiting on _load_event; re-materialize weights inline when a stale pre-capture prefetch exists. Otherwise, wait on _load_event as before.


CI States

Latest PR Test (Base): ❌ Run #28215099371
Latest PR Test (Extra): ❌ Run #28215099338

…isolation

CPU weight offload prefetches parameters on alt_stream and synchronizes via CUDA events. During decode CUDA graph capture, waiting on events recorded on alt_stream (or prefetching there) creates a dependency on uncaptured work and aborts with cudaErrorStreamCaptureIsolation.

When the current stream is capturing, materialize weights inline on the capture stream in start_onload and wait_and_get_device_tensors instead of using cross-stream prefetch/wait. Non-capture inference keeps the existing alt_stream overlap path unchanged.

Signed-off-by: Shijin Zhang <[email protected]>

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

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.

Code Review

This pull request updates the offloader utility in python/sglang/srt/utils/offloader.py to support CUDA stream capturing. Specifically, it checks if the current stream is capturing during start_onload and wait_and_get_device_tensors to bypass asynchronous stream synchronization and instead load tensors synchronously. There are no review comments to address, and the changes appear correct.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@JustinTong0323

Copy link
Copy Markdown
Collaborator

/tag-and-rerun-ci

@github-actions github-actions Bot added the run-ci CI: run the baseline test suite on this PR label Jun 26, 2026

@JustinTong0323 JustinTong0323 left a comment

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.

e2e on 4×GB300 with GLM-5.2-FP8 (--offload-group-size 4 --offload-mode cpu). Reproduced the decode graph cudaErrorStreamCaptureIsolation at wait_and_get_device_tensors (the self._load_event.wait() call); this fix resolves it and GSM8K is clean (100% stop rate, correct answers). Two caveats inline.

Comment thread python/sglang/srt/utils/offloader.py Resolved
@JustinTong0323

Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

4 similar comments
@JustinTong0323

Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@JustinTong0323

Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@JustinTong0323

Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@JustinTong0323

Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@Fridge003
Fridge003 merged commit 5b76f55 into sgl-project:main Jul 1, 2026
773 of 859 checks passed
vstone-w pushed a commit to ClownBin/sglang that referenced this pull request Jul 2, 2026
Chronostasys pushed a commit to MindLab-Research/sglang that referenced this pull request Aug 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants