Skip to content

[Bugfix] Drain decode HiCache offloads before retraction - #40982

Closed
Jiminator wants to merge 1 commit into
sgl-project:mainfrom
Jiminator:fix/hicache-retraction-main
Closed

Jiminator wants to merge 1 commit into
sgl-project:mainfrom
Jiminator:fix/hicache-retraction-main

Conversation

@Jiminator

@Jiminator Jiminator commented Sep 23, 2026 •

Copy link
Copy Markdown
Collaborator

Consolidated into #30899, the original PR by Kevin Flansburg (@kflansburg), to keep the implementation, validation and credit together. The reviewed source tree and Kevin-authored fix commit are preserved there.


Motivation

Decode-side HiCache offload copies KV to host asynchronously. Retraction can return those device pages to the allocator before the copy finishes, allowing an independent writer to overwrite data the old offload still reads. A retract pause can also leave offload bookkeeping pending and prevent cache flush while paused.

Based on @kflansburg's #30899, adapted to current main.

Modifications

Reuse check_offload_progress() before retract_all() in pause/retract and before retract_decode() in the memory-pressure branch. Add two deterministic regression cases that observe copy completion when KV is freed.

Normal and overlap decode already drain before memory-pressure retraction. The PP loop selects the next batch before its drain, and abort paths release with offload_kv=False, without the synchronizing CPU backup. The second call protects that ordering. These are the current decode retraction entrypoints; future decode release paths must preserve it too. The ordinary asynchronous offload path is unchanged.

Accuracy Tests

Validated on main 2909f84e with H200, Torch 2.13.0+cu130 and sglang-kernel 0.4.7:

  • Negative control: exactly the two new ordering assertions fail on the unmodified base. Candidate: 87 tests plus 32 subtests pass across four relevant unit suites; direct CI-style execution passes all 23 tests.
  • Real CUDA copy/reuse controls exercise both scheduler entrypoints. The base persists the next owner's values; the candidate preserves the original values. Single-GPU controls were repeated, and a two-rank standalone control verifies the real TP CPU-group collective, identical per-rank releases and acknowledgement state, and correct persisted pages.
  • Qwen3-0.6B (c1899de2), TP1 prefill/decode: paired pause, delayed pause, resume, flush and abort checks pass with exact output-token equality. Each variant also completes four concurrent requests through 85 forced retractions, matching the uninterrupted reference. Delayed paused flush changes from HTTP 400 on base to HTTP 200 on the candidate.

The CUDA control uses an independent writer stream to model forward/RDMA reuse; no corruption from natural PD traffic is claimed. PP decode was not run end to end; its ordering is covered by the memory-pressure entrypoint control and source inspection. Full TP2 serving did not fit the available container host-memory budget. Its coverage is the standalone two-rank control above, not a trained TP2 serving run.

Speed Tests and Profiling

Three alternating launches per variant, telemetry off, 16 requests at concurrency 4, 210 input / 256 output tokens. Each launch has one warmup repeat and two measured repeats. Qwen3-0.6B BF16, overlap on, Triton attention, CUDA graphs disabled, 4096 device KV tokens, decode HiCache ratio 1 and file storage; prefill uses its ordinary cache.

Base Candidate
Mean output tokens/s 275.5 276.6
Range across measured repeats 273.2–276.7 272.6–280.3

All requests completed and no benchmark retracted. The ranges overlap; this is a regression check for this workload, not a speedup claim. Injected-delay timings are excluded from performance results.

Checklist

  • Changed-file and all-files pre-commit checks pass.
  • Focused failing-before/passing-after regression coverage is included.
  • No user-facing API or configuration change requiring documentation.
  • Correctness and common-path measurements are reported with their scope.
  • Code follows the repository's contribution and style guidance.

CI States

Latest PR Test (Base): ❌ Run #35929520979
Latest PR Test (Extra): ❌ Run #35929520712
Latest PR Test (AMD ROCm 10): ❌ Run #35929521042

Decode-side HiCache offload copies KV to host asynchronously. Retraction
could return those device pages to the allocator while a copy still read
them: pause/retract runs before the decode loop's offload drain, and so
does memory-pressure retraction in the PP loop. A retract pause also
left the offload acks pending, so the paused engine never became idle
for flush_cache.

Run the offload manager's existing check_offload_progress() before
retract_all() in pause_generation and before retract_decode() in
update_running_batch, and add a regression case for each entrypoint.

Based on sgl-project#30899 by Kevin Flansburg.
Jiminator added a commit to kflansburg/sglang that referenced this pull request Sep 24, 2026
Bring this branch to the implementation that was reviewed and tested
separately in sgl-project#40982. The resulting tree is exactly that tested
commit's tree: the offload manager's check_offload_progress() now runs
before retract_all() in pause_generation and before retract_decode()
in update_running_batch, with a regression case for each entrypoint.

The earlier callback-based changes on this branch are superseded by
that tree. Their commits stay in the history; nothing is rewritten.

Original fix and PR by Kevin Flansburg.
@Jiminator Jiminator closed this Sep 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants