Repository navigation
[Fix] cuda_graph: Wait for in-flight layer-wise cache loads before prefill graph replay - #44220
Merged
ispobock merged 7 commits intoOct 17, 2026
Merged
Conversation
alphabetc1
requested review from
Fridge003,
Ying1123,
hnyls2002,
ispobock and
merrymercy
as code owners
October 14, 2026 06:34
3 tasks done
alphabetc1
requested review from
hanming-lu,
huangtingwei9988,
hzh0425,
xiezhq-hermann and
yizhang2077
as code owners
October 14, 2026 07:29
3 tasks done
ch-wan
reviewed
Oct 15, 2026
Oasis-Git
approved these changes
Oct 15, 2026
Oasis-Git
left a comment
Collaborator
There was a problem hiding this comment.
In high level it makes sense to me. However I do not like the delay cycles. Maybe we can adjust later.
This was referenced Oct 15, 2026
Closed
Collaborator
Author
|
TODO: Investigate whether DSV4's events can be reused to capture HiCache H2D. |
ispobock
approved these changes
Oct 17, 2026
3 tasks done
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.
Motivation
HiCache L2 load-back, HiCache storage load-back (e.g.
buffer_only), and the unified-cache external linker load KV/SWA/Mamba state into the device pools layer by layer, overlapped with the prefill forward. The forward waits for each layer inside the pool accessors (get_key_buffer,mamba2_layer_index, ...) through the registeredlayer_transfer_counter.A prefill CUDA graph replay never runs those Python-level waits: the captured graph is launched as a whole. Nothing stops the graph from replaying a batch whose load is still in flight (
scheduler.pyhas carried# todo: disable cuda graph execution if hicache loading triggeredsince 2025-06). The forward then reads layers that have not landed yet and produces wrong logits silently. Computed KV for the following pages is built on the corrupted state, and can be cached and backed up under the same content-addressed keys.This is reachable on main today whenever a model replays the prefill graph for a loading batch. Inkling defaults to the Full prefill CUDA graph backend, so its HiCache path is affected. Whether it shows up depends on the race between the H2D copies and the replay: the existing Inkling HiCache bit-exact tests pass on main only because the copies usually win.
Modifications
PrefillCudaGraphRunner.execute: before a Full replay, if the KV pool's layer transfer counter has an active consumer (this batch is loading), wait until the last layer has landed. Works for both counters (HiCacheLayerDoneCounter, a stream wait; linkerLayerWiseLoadCounter, a CPU wait), since both complete layers in order.SGLANG_TEST_HICACHE_H2D_DELAY_CYCLES(default 0, off): a GPU sleep on the host-to-device stream before each layer's load, so a replay that skips the load wait reliably reads unloaded layers.Accuracy Tests
Inkling (
thinkingmachines/Inkling --revision test, deterministic inference, Full prefill graph default).New
TestUnifiedHybridHiCacheSlowLoadBitExact(the existing HiCache bit-exact class withSGLANG_TEST_HICACHE_H2D_DELAY_CYCLES=50000000):The existing
TestUnifiedHybridHiCacheBitExactpasses on both trees: without the delay the H2D copies usually win the race, which is why CI has not caught this.Earlier probe on 1x GB300 with the same kind of injected H2D delay, also covering storage load-back; the logs confirm graph replays with an in-flight load:
TestUnifiedHybridHiCacheBitExact(L2)TestUnifiedHybridBufferOnlyBitExact(L3)The same fix also restores KL 0.0 for the Mooncake direct linker on Inkling (#41343), which hits the race without any injected delay (avg KL 2.5e-3 before; 0.0 in 3/3 runs after, and 0.0 with a 0.2 s per-layer injected load delay).
Breakable check: Inkling forced to
--cuda-graph-backend-prefill breakablewith the same injected delay gives avg KL 0.0 on main and on this PR (4 replays with an in-flight load each); its state reads run at eager breaks.Out of scope, fixed in #44414: a breakable graph that captures a state-reading linear extend (the Triton KDA extend captured since #41105) skips those reads' waits. GLM-5.3-Flash on 4x GB300 with the Triton KDA extend forced and the same delay: median KL 3.5-4.1e-3 on main and on this PR vs a 1.9e-4 floor; 1.4e-4 when every replay waits or when the KDA capture is disabled. Not a regression from this PR.
Benchmark and Profiling
Host-tier (L2) hit TTFT, Inkling-Small bf16, TP4 on 4x GB300, Full prefill graph, 512 new tokens on a cached prefix, median of 5 (ms).
eageris the alternative of running a loading batch eagerly instead of replaying the graph.Cold and device-hit TTFT are unchanged.
Tests
test_prefill_cuda_graph_runner.py::TestPrefillCudaGraphRunnerLayerLoads: the replay wait blocks until the last layer of an in-flight load completes, and is a no-op without a counter. Negative control: removing the wait failstest_replay_waits_for_every_layer_of_an_inflight_load.test_prefill_cuda_graph_runner.py: 18 passed, includingtest_breakable_replay_keeps_per_layer_load_overlap(fails if the wait is not limited to Full).test_unified_radix_cache_kl_hybrid_bitexact.py::TestUnifiedHybridHiCacheSlowLoadBitExact: fails on main, passes with the fix (table above); about 70 s per class.Checklist
CI States
Latest PR Test (Base): ✅ Run #38998797437
Latest PR Test (Extra): ❌ Run #38998797308
Latest PR Test (AMD ROCm 10): ❌ Run #38998797356