Skip to content

[Fix] cuda_graph: Wait for in-flight layer-wise cache loads before prefill graph replay - #44220

Merged
ispobock merged 7 commits into
sgl-project:mainfrom
alphabetc1:fix/prefill-graph-wait-layer-loads
Oct 17, 2026
Merged

ispobock merged 7 commits into
sgl-project:mainfrom
alphabetc1:fix/prefill-graph-wait-layer-loads

Conversation

@alphabetc1

@alphabetc1 alphabetc1 commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator

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 registered layer_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.py has carried # todo: disable cuda graph execution if hicache loading triggered since 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 (HiCache LayerDoneCounter, a stream wait; linker LayerWiseLoadCounter, a CPU wait), since both complete layers in order.
  • Eager and breakable prefill are unchanged and keep per-layer load/compute overlap: breakable replay runs the pool accessors at its eager breaks, so their per-layer waits still apply (except a captured state-reading linear extend, see below). A loading batch that replays a Full graph now waits for the whole load first; batches without a load are unaffected.
  • Test hook 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 with SGLANG_TEST_HICACHE_H2D_DELAY_CYCLES=50000000):

GPU main this PR
1x H200 (CI arch) avg KL 4.4e-3 (fail) 0.0
1x GB300 avg KL 1.7e-2 (fail) 0.0

The existing TestUnifiedHybridHiCacheBitExact passes 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:

Tree Injected H2D delay TestUnifiedHybridHiCacheBitExact (L2) TestUnifiedHybridBufferOnlyBitExact (L3)
main no avg KL 0.0 avg KL 0.0
main yes avg KL 3.1e-3 (fail) avg KL 6.4e-3 (fail)
this PR yes avg KL 0.0 avg KL 0.0

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 breakable with 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). eager is the alternative of running a loading batch eagerly instead of replaying the graph.

Prefix main (no wait) this PR eager
160k 113.0 115.0 195.4
320k 176.0 182.2 247.7
500k 254.9 255.0 306.7

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 fails test_replay_waits_for_every_layer_of_an_inflight_load.
  • test_prefill_cuda_graph_runner.py: 18 passed, including test_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

  • Format your code according to the Code Formatting with Pre-Commit.
  • Add unit tests.
  • Provide accuracy evaluation results.

CI States

Latest PR Test (Base): ✅ Run #38998797437
Latest PR Test (Extra): ❌ Run #38998797308
Latest PR Test (AMD ROCm 10): ❌ Run #38998797356

@Oasis-Git Oasis-Git 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.

In high level it makes sense to me. However I do not like the delay cycles. Maybe we can adjust later.

@alphabetc1 alphabetc1 added run-ci CI: run the baseline test suite on this PR run-ci-extra CI: also run the extra suite (requires run-ci) bypass-fail-fast CI: a failing job no longer aborts its siblings (lint still gates) labels Oct 16, 2026
@alphabetc1 alphabetc1 added the highest-priority CI: all three control labels, plus never batch-cancelled or stale-closed label Oct 17, 2026
@alphabetc1

Copy link
Copy Markdown
Collaborator Author

TODO: Investigate whether DSV4's events can be reused to capture HiCache H2D.

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) hicache Hierarchical Caching for SGLang highest-priority CI: all three control labels, plus never batch-cancelled or stale-closed run-ci CI: run the baseline test suite on this PR run-ci-extra CI: also run the extra suite (requires run-ci) unified-radix-cache

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants