Repository navigation
[Fix] Qwen3.8next: Stop retaining per-size PLE outputs in breakable prefill CUDA graphs - #44150
Merged
Qiaolin-Yu merged 4 commits intoOct 19, 2026
Merged
Conversation
YAMY1234
approved these changes
Oct 15, 2026
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
During breakable prefill CUDA graph capture,
eager_on_graphkeeps 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_forwardreturned 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_outputand the DSA indexer:_add_ple_embeddingallocates the output inside the captured segment, from the graph pool, and the eager break writesresidual + 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):
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
CI States
Latest PR Test (Base): ⏳ Run #39201979943
Latest PR Test (Extra): ❌ Run #39201979940
Latest PR Test (AMD ROCm 10): ❌ Run #39201979884