Skip to content

[Fix] Qwen3.8next: Stop retaining per-size PLE outputs in breakable prefill CUDA graphs - #44150

Merged
Qiaolin-Yu merged 4 commits into
sgl-project:mainfrom
Qiaolin-Yu:qiaolin/qwen4-bcg-bridge-mem
Oct 19, 2026
Merged

Qiaolin-Yu merged 4 commits into
sgl-project:mainfrom
Qiaolin-Yu:qiaolin/qwen4-bcg-bridge-mem

Conversation

@Qiaolin-Yu

@Qiaolin-Yu Qiaolin-Yu commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

During breakable prefill CUDA graph capture, eager_on_graph keeps a strong reference to each eager break's return value so replay can copy fresh output into it. That return value is allocated outside the graph pool, so a break that returns a newly allocated tensor keeps one buffer per captured size for the lifetime of the graphs.

_breakable_ple_forward returned a fresh [num_tokens, hc_count * hidden_size] tensor, so the PLE layer retained one such buffer for every prefill capture size. With the default prefill capture sizes up to 16384 tokens this adds up to several GiB per GPU, while only the largest size can ever be live.

Use the output-argument pattern already used by breakable_unified_attention_with_output and the DSA indexer: _add_ple_embedding allocates the output inside the captured segment, from the graph pool, and the eager break writes residual + ple(...) into it in place and returns nothing. Graph-pool memory is then reused across capture sizes, and the residual add no longer needs a second graph-pool tensor.

Memory impact

Measured on 4x B200 with a Qwen4 PLE model at TP4 using breakable prefill CUDA graphs (74 capture sizes up to 16384 tokens), identical launch configuration for both runs; only this change differs. Per GPU (all four ranks consistent):

Before After Change
Target prefill CUDA graph capture 14.59 GB 8.42 GB -6.17 GB
Device memory used after startup 167,601 MiB 161,549 MiB -5.91 GiB

The removed retention matches the PLE output size summed over all capture sizes (296,032 tokens x 20,480 B = 5.65 GiB). KV and Mamba cache sizes are unchanged because they are sized before graph capture; the freed memory is left free at startup.

Validation

  • Greedy generation (64 tokens) for prompts of 43, 395, 1931, 7051 and 16651 tokens, cache flushed before each request, with output logprobs, compared against three independent runs of the unchanged code.
    • 43, 395 and 1931 tokens: all 64 output tokens identical and logprob differences exactly 0 against every baseline run.
    • 7051 and 16651 tokens: the unchanged code already diverges between its own runs (first differing token 5-16, max logprob difference up to 0.66); this change stays within that range.
  • All applicable pre-commit hooks pass.

CI States

Latest PR Test (Base): ⏳ Run #39201979943
Latest PR Test (Extra): ❌ Run #39201979940
Latest PR Test (AMD ROCm 10): ❌ Run #39201979884

@Qiaolin-Yu Qiaolin-Yu changed the title [Qwen4] Stop retaining per-size PLE outputs in breakable prefill CUDA graphs [Qwen 3.8 next] Stop retaining per-size PLE outputs in breakable prefill CUDA graphs Oct 14, 2026
@Qiaolin-Yu
Qiaolin-Yu requested a review from YAMY1234 October 14, 2026 19:39
@YAMY1234 YAMY1234 changed the title [Qwen 3.8 next] Stop retaining per-size PLE outputs in breakable prefill CUDA graphs [Fix][Qwen 3.8 next] Stop retaining per-size PLE outputs in breakable prefill CUDA graphs Oct 15, 2026
@Qiaolin-Yu Qiaolin-Yu added run-ci CI: run the baseline test suite on this PR bypass-fail-fast CI: a failing job no longer aborts its siblings (lint still gates) labels Oct 15, 2026
@github-actions

This comment has been minimized.

@Qiaolin-Yu Qiaolin-Yu changed the title [Fix][Qwen 3.8 next] Stop retaining per-size PLE outputs in breakable prefill CUDA graphs [Fix] Qwen 3.8 next: Stop retaining per-size PLE outputs in breakable prefill CUDA graphs Oct 18, 2026
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@Qiaolin-Yu Qiaolin-Yu changed the title [Fix] Qwen 3.8 next: Stop retaining per-size PLE outputs in breakable prefill CUDA graphs [Fix] Qwen3.8next: Stop retaining per-size PLE outputs in breakable prefill CUDA graphs Oct 19, 2026
@Qiaolin-Yu
Qiaolin-Yu merged commit 9ab74b8 into sgl-project:main Oct 19, 2026
177 of 193 checks passed
@Qiaolin-Yu
Qiaolin-Yu deleted the qiaolin/qwen4-bcg-bridge-mem branch October 19, 2026 19:41
detain pushed a commit to detain/sglang that referenced this pull request Oct 20, 2026
…refill CUDA graphs (sgl-project#44150)

Upstream PR sgl-project#44150, merged upstream as 9ab74b8 (single commit, 1 file
+15/-8). Confirmed NOT an ancestor of this stack (git merge-base
--is-ancestor 9ab74b8 HEAD -> false), so it is ported here.

The PLE add runs inside an @eager_on_graph(True) piece of the breakable
piecewise prefill graph. Returning a tensor from that piece makes the graph
machinery retain a distinct output buffer per captured shape/size, which is
exactly the per-forward transient pressure this stack has been chasing for the
Qwen3.8-Flash-Next prefill path (entries 210/211 pack-OOM hardening, the
chunked-prefill KV-source work): every registered prefill size kept its own
PLE output alive for the lifetime of the graph.

Fix, applied as upstream wrote it:
  * _breakable_ple_forward(ple, hidden_states, batch, output) now fills the
    caller-supplied buffer: torch.add(hidden_states, ple(...), out=output), and
    the idle-dummy path does output.copy_(hidden_states) instead of returning
    torch.zeros_like(hidden_states). Semantics preserved: the old idle path
    returned zeros to a caller that added them to residual, so residual passed
    through unchanged; the new form produces the same residual with no
    allocation.
  * _add_ple_embedding allocates output = torch.empty_like(residual) OUTSIDE
    the eager piece, so the buffer belongs to the caller's (replay-stable)
    scope rather than being minted inside the piece per size.

METHOD: git cherry-pick -x 9ab74b8 applied CLEAN with zero conflicts even
though qwen4_exp.py is this stack's most-diverged file (+981/-82 vs upstream)
-- both pre-image regions happen to be byte-identical to ours. Verified the
landed patch content is byte-identical to the upstream commit (diff of the
+/- line streams is empty), and grep-proved there are no other callers of
_breakable_ple_forward or _add_ple_embedding anywhere in python/ or test/
(only the definition and this one call site), so the new 4-arg signature has no
other user to update.

FORK SURVIVAL around the change (all re-read after landing, all intact):
  * host-staged PLE: _BreakablePLEBatch (:244) still wraps the full _PLEBatch,
    prepare_ple_rows (:2789) still re-runs on every eager break, and the
    prepare_rows(...) call just above _breakable_ple_prefetch (:269 comment)
    keeps the staging buffer fed -- the eager break re-runs it, so the new
    in-place add sees the same rows as before;
  * query_start_loc / packed n-gram threading (_PLEBatch.query_start_loc,
    use_packed_ngram at :257/:265/:317/:450, use_packed_ple_ngram call sites
    :2896/:2904) untouched;
  * SM120 graph-prefetch buffers (_graph_prefetch_buffers :1540,
    prepare_cuda_graph_prefetch_buffer :1800, _consume_prefetched_embeddings)
    untouched -- they cache prefetched EMBEDDINGS per size, a different buffer
    family from the PLE output the PR fixes, so no double-buffering or
    aliasing is introduced by writing into `output`;
  * backing-file reuse (entry 167: make_ple_file_prefetcher :106/:1345) and
    ple_warned_downcast (entry 207: :3271/:3322) untouched.

VERIFIED per-file with CUDA_VISIBLE_DEVICES=9 -- every pinned baseline
reproduces exactly: test_qwen4_exp_ple_staging.py 37 passed (the one "ERROR"
token in the log is the test's own intentional host-staging error-path log
line, not a pytest error; rc=0),
test_qwen4_exp_ple_packed_short_conv.py 2P/1S,
test_qwen4_loader_lifetime.py 3P/4sub,
test/registered/unit/model_executor/test_prefill_cuda_graph_runner_helpers.py
22P/14sub. Import smoke on sglang.srt.models.qwen4_exp OK. ruff repo ruleset
(F401,F821,F823,UP037) + ruff format clean on the touched file.

GPU-window debt: measure the drop in retained prefill-graph bytes at the prod
PLE geometry and confirm packed-vs-padded PLE outputs stay bit-equal through a
replayed breakable graph (the in-place torch.add(..., out=) is a new buffer
identity for the graph's output).

(cherry picked from commit 9ab74b8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bypass-fail-fast CI: a failing job no longer aborts its siblings (lint still gates) run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants