Repository navigation
fix(sglang): wait for HiCache loads before discarding a prefill batch - #601
Conversation
SGLang 0.5.20 submits a prefill batch's HiCache host-to-device loads before prepare_for_extend. When a kvcached pool then misses, the capacity-miss patch unwinds the batch: it frees the requests' Mamba slots, including the copy-on-write slot a load is writing, and puts the requests back in the waiting queue, while no forward will ever wait for those loads. Another request can be handed the slot before the copy lands, and a re-admitted request can match the loaded tree nodes and read them before their data arrives. Found by reading the code; not reproduced on hardware. The unwind now synchronizes the device first when the batch carries a HiCache consumer index. Batches without HiCache loads unwind as before.
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
rishabhsinha17
left a comment
There was a problem hiding this comment.
LGTM, the hazard is real at 0.5.20 and the wait sits where it has to.
start_loading runs inside ready_to_load_host_cache before prepare_for_extend (scheduler.py:4012-4018) and submit_host_to_device enqueues every layer's copy on host_to_device_stream (l2_transfer.py:74-118), so at the unwind the copies are already queued and a device sync covers them. the mamba load-back writes into req.kv.mamba_pool_idx (unified_cache/components/mamba.py:674-679, 752-755), the slot the unwind frees, and a requeued batch gets hicache_consumer_index -1 from an empty load queue, so nothing else orders its kernels behind the copies.
the sync comes before dec_lock_ref and the mamba free, after the tp all-reduce with no collective until the next check_hicache_events, and is a no-op at -1. a cpu probe recording the sync, the dec_lock_ref and the free in order fails on main like the new test, 34 passed in the file, cpu manifest 1509 passed with the failure set identical to clean main. lint and ci-exact mypy are clean, gpu coverage stays on your side.
| # before their Mamba slots are freed and later batches read the | ||
| # loaded nodes. | ||
| import torch | ||
| torch.cuda.synchronize() |
There was a problem hiding this comment.
optional, layer_done_counter.events[batch.hicache_consumer_index].finish_event.synchronize() on the cache controller would wait for this load alone instead of the whole device (with overlap scheduling the device sync also waits for the forward in flight), a miss is rare so i am fine either way. one limit for the record, the unified cache external linker sets the same index but loads on its own thread (mooncake_direct_linker.py:177), which no device sync orders, and kvcached does not run that path.
shipiyouniao
left a comment
There was a problem hiding this comment.
LGTM for native HiCache. The loads are already queued before allocation can fail, so this wait closes the gap before unwind releases the Mamba slots and admission locks.
Wait for the batch's host-to-device loads before releasing admission locks and Mamba slots, without synchronizing unrelated device streams. Co-authored-by: 石皮幼鸟 <2960474346@qq.com>
|
Updated the unwind to wait for the batch's HiCache |
Problem
When a kvcached pool cannot allocate a prefill batch, kvcached frees the batch's Mamba slots and puts its requests back in the waiting queue (#547). By then SGLang 0.5.20 has already started the batch's HiCache loads from the host (
ready_to_load_host_cache()runs beforeprepare_for_extend), and the forward that would have waited for them never runs. So:THIS IS A PROBLEM BY READING CODE. NO REAL REPRODUCTION.
Change
For native HiCache, unwind synchronizes the batch's
layer_done_counter.events[consumer].finish_eventbefore releasing admission locks or Mamba slots. This covers its MHA/Mamba transfers and retains their forward-stream dependency, without synchronizing unrelated device streams. Batches without loads do not wait. External linker mode is outside this change's scope.Validation
a74ee23: 78 targeted tests passed, including consumer -1/0/2, event selection, and wait-before-release ordering. The load cases fail on main.