Skip to content

fix(sglang): retry prefill batches that the kvcached pools cannot back - #554

Merged
RixinLiu merged 7 commits into
ovg-project:mainfrom
rishabhsinha17:fix/sglang-hybrid-capacity-miss
Oct 18, 2026
Merged

RixinLiu merged 7 commits into
ovg-project:mainfrom
rishabhsinha17:fix/sglang-hybrid-capacity-miss

Conversation

@rishabhsinha17

@rishabhsinha17 rishabhsinha17 commented Oct 9, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Part of #419 item 3. Hybrid models such as Qwen3.5-4B run two kvcached pools in one SGLang process, the attention tokens and the Mamba slots, and each pool reports its free count from the whole device's free memory. SGLang admits a prefill batch against the token count and then allocates Mamba slots and tokens. Any miss after admission is fatal by native design, because native pools are static and the budget cannot be wrong. With kvcached the budget is a snapshot that the sibling pool, or another process (#467), consumes first, and the scheduler exits with the two errors in #547. This PR adds the fallback, the second of the two parts #547 lists. Either pool missing during batch preparation releases the rows and slots the batch received and returns its requests to the waiting queue, SGLang's own retry path. Under TP the ranks that share the waiting queue agree on the verdict before any of them runs the batch. Its shared budget is the first part and a separate PR. This fallback matters with or without it, because the cross-process case stays.

Root cause

Admission reads only the attention allocator. PrefillAdder.rem_total_tokens is available_size() of the token allocator plus the evictable tree (schedule_policy.py#L720), and alloc_req_slots reads the Mamba count only after the batch exists (allocation.py#L301-L322).

Past admission the path is fail-loud. None from a token allocation raises RuntimeError (allocation.py#L160-L169, #L229-L238), and HybridReqToTokenPool.alloc asserts on a None Mamba slot (memory_pool.py#L1445) and ping-pong pair (#L1597) after it has already bound request rows and earlier slots. No handler exists in the scheduler loop. run_scheduler_process logs the traceback and signals the parent (scheduler.py#L5859-L5862).

For decode there is a gate, but it compares a snapshot. check_decode_mem asks the allocator (allocator/base.py#L79-L93) and retracts on False. Then prepare_for_decode advances the penalizer state before it allocates (schedule_batch.py#L3453-L3464), and a miss there cannot run the preparation again.

Under TP each rank has a kvcached pool of its own (interfaces.py builds the manager with world_size=1), while SGLang hands every rank of the queue group the same requests (request_receiver.py#L170-L213) and expects them to build the same batch. A miss on one rank after admission is a rank-local event, and a rank that retries alone leaves its peers in the forward with a batch it does not have.

vLLM has one pool and a native miss channel. #453 and #511 turn its exhaustion into allocate_slots returning None, and the scheduler preempts and retries.

Changes

Allocator level. The elastic token and paged allocators, ElasticMambaPool and ElasticMambaSlotAllocator keep returning None on a miss (the #545 contract) except inside a batch allocation window, where they raise KVCachePoolExhausted, the type #453 introduced. The SWA composite's own pre-check None gets the same typed raise inside the window. Such a window exists because every None inside alloc_for_extend is fatal natively, while alloc_group_begin and the lazy ping-pong slot rely on the None outside it. Only a capacity shortfall makes the manager return None, and its contract violations and native fatal errors raise on their own, which keeps the translation as narrow as the vLLM one.

Inside the window the elastic token allocators also journal every token block they hand out, as (manager, block ids). Rolling back returns those ids, and no others, to the manager that gave them, newest first, before anything has bound them. That journal is what makes a partial allocation safe to retry: the SWA composite allocates from its full-attention child first and its SWA child second (swa.py#L219-L232), and a miss in the second child used to leave the first child's blocks with nobody to return them. SGLang writes the full-to-SWA mapping only after both children succeed, so no mapping entry exists at that point.

ScheduleBatchCapacityMissPatch wraps schedule_batch.alloc_for_extend in the window. On the typed miss it returns the journaled blocks, frees the request rows, the Mamba slot and the ping-pong slots that the call handed to requests which held none at entry, and raises SGLangPrefillCapacityMiss with the batch. Rows and slots that a request already owned at entry, the chunked continuation's, are left alone.

TP. After the local attempt, and before the exception or the result leaves the wrapper, every rank of the queue group reduces a miss flag: one MAX all-reduce over the attention TP CPU group and, with context parallelism, one over the attention CP CPU group, the two groups SGLang itself broadcasts the queue along (attn_cp_tp_broadcast_pyobj), and get_attn_tp_group is the TP group itself when DP attention is off. Whatever the local outcome, the reduce runs once per alloc_for_extend call. Every rank then issues the same number of collectives, and nothing waits on a peer that took another branch. A rank whose own allocation succeeded while a peer missed returns its journaled blocks and the rows and slots it handed out, restores the KV lengths the call wrote, and raises the same signal with a peer-miss cause. Every rank's scheduler wrapper then runs the same unwind, the same requeue and the same cap. Agreement sits after alloc_for_extend because prepare_for_extend mutates request state only after that call returns (schedule_batch.py#L2677-L2770). So a peer that has to let go of a batch has none of that to undo. At tp_size == 1, or without a parallel state, the local verdict stands and no collective runs, so the single-GPU path does what round 1 did.

SchedulerCapacityMissPatch wraps Scheduler._get_new_batch_prefill_raw. On that miss it releases the admission lock with its receipt and the match-time Mamba state, the same revert the scheduler applies to a candidate it did not add (scheduler.py#L3951-L3966), restores the chunked request parked with nothing new to cache, returns the requests to the head of the waiting queue in order, clears batch_is_full and returns no batch. Its next prefill round admits one request fewer than the batch that missed, through the schedule bag's scoped override of prefill_max_requests, and a round that fits lifts the cap, which shrinks the retry instead of repeating the same batch. One request that cannot fit waits for the running batch or the other process to free memory, as on vLLM. Any other exception still exits the scheduler.

Decode. Here the elastic allocators define check_decode_capacity (0.5.19+): evict as native, then reserve the step's pages through KVCacheManager.try_to_reserve. That reservation verdict goes through the same reduce, once per call, so one short rank retracts everywhere. Whichever rank reserved keeps its pages, and the retract loop's next gate re-reserves the same set. Failing the reservation takes the native retract path, and the allocation that follows draws the reserved pages, which cannot run out inside the process.

Coverage under the parallel modes. Covered: a post-admission miss on any subset of the ranks sharing a waiting queue, with or without DP attention (each DP rank's attention TP group decides for its own queue, and the native DP-wide MLP sync runs after it as before), and context parallelism by construction, not by a GPU run. Not covered: pipeline parallelism. Each PP stage reduces within its own TP group, and the stages attempt different microbatches, so a miss on one stage retries on that stage alone, as a stage-local exhaustion would natively. Admission itself can also read different available_size() on different ranks before any allocation, since the count is per-GPU physical memory. It belongs to the shared-budget part and the separate PR.

Both scheduler-side patches read the 0.5.20 request and batch shapes and register for >=0.5.20. Out of scope and still fail-loud as native: the match-time COW slot in MambaRadixCache._alloc_mamba_slot (mamba_radix_cache.py#L1061-L1068), which asserts on its own under the same exhaustion and needs an SGLang-side fallback, mixed-chunk decode preparation (scheduler.py#L4049-L4051), which runs without the gate, and allocations outside alloc_for_extend, such as the speculative draft's and the PD decode side's alloc_extend_swa_tail, which keep the None contract.

Validation

CPU only, mock-based, in the existing test style: 29 tests in tests/test_sglang_capacity_miss.py.

Against main at debcb09 the file does not import, and its unpatched-pipeline controls reproduce the issue's two errors, AssertionError "Not enough space for mamba ping pong idx" and RuntimeError "Out of memory", with the requests gone from the queue and their rows and slots still bound. Patched, both become a retry: free lists byte-equal to before the round in both pools, locks released with their receipts, queue order preserved, cap applied on the next round and lifted after a round that fits.

Round 2 adds the two review probes as regressions, each run against the round 1 head 57b07f5 before the fix. test_tp_peer_miss_unwinds_every_rank runs two fake ranks on two threads over a rendezvous that replaces the gloo all-reduce, with only rank 1's token pool missing. At 57b07f5 rank 0 ends with has_batch=True, queued=[], holds_row=[True, True], cap=None and rank 1 with the opposite; now both end identical, requeued with cap 1, free lists back to before on both, one collective with flags [0, 1], and the same capped batch on both once rank 1's pressure lifts. test_tp_peer_miss_unwinds_mamba_slots_too does the same with the hybrid pools, and test_decode_gate_agrees_across_ranks pins the decode verdict, [True, False] before and [False, False] after, with the reserving rank keeping its pages. test_swa_partial_allocation_is_returned_before_retry runs the composite's alloc shape over two elastic children with the SWA child missing after the pre-check: at 57b07f5 the full child's free count falls 16, 12, 8 on three retries; now it stays 16 on every retry, the mapping is never written, and the request runs once the child is back. test_window_journals_token_blocks_and_returns_them and test_paged_window_journals_the_new_pages_only pin that the window records the ids the manager returned, including only the new pages of a paged alloc_extend, and that a rollback returns them newest first.

Full CPU manifest 1299 passed, failure set identical to main at 43ed583 on the same machine (the same 12 environment cases). check_test_classification valid, cpu=63. ruff, isort, codespell clean. CI-exact mypy 1.11.1 without torch, 3.10 to 3.13, clean on kvcached and tests.

No GPU run on my side yet. One two-GPU TP=2 run on SGLang 0.5.20 remains the check, and the runbook for it is ready: Qwen3.5-4B, symmetric pressure as in the issue's reproduction, then an asymmetric miss forced on one rank only by a process pinning memory on one GPU mid-serve, with the pass criteria being no hang, the same miss line and cap on both ranks in the same iteration, and the queued requests completing after the pressure lifts.

Refs #547

Signed-off-by: Rishabh Sinha [email protected]

A hybrid model runs two kvcached pools in one SGLang process, and each
pool reports its free count from the whole device's free memory, so a
batch admitted against the token count can still miss on the Mamba
slots or on the tokens. SGLang treats a miss after admission as fatal,
and the scheduler exits.

Inside the batch allocation window the elastic allocators now raise
KVCachePoolExhausted instead of returning None. The alloc_for_extend
wrapper releases the rows and slots the batch received and raises a
batch-carrying miss. At the scheduler the wrapper releases the admission lock
and the match-time Mamba state, returns the requests to the head of the
waiting queue, caps the next round at one request fewer, and returns no
batch. For decode the elastic allocators take over the 0.5.19+ gate
and reserve the step's pages there, and a shortfall retracts instead
of failing in alloc.

Fixes ovg-project#547

Signed-off-by: Rishabh Sinha <[email protected]>

@shipiyouniao shipiyouniao 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.

The 23 existing capacity-miss tests pass. Two additional CPU fault-injection probes expose incomplete recovery: an asymmetric TP allocation miss produces different batch decisions, and a partial SWA allocation loses the successful child's token blocks on each retry. Details inline. These probes use the patched wrappers; the SWA probe also executes the unchanged v0.5.20 composite alloc method. They are not GPU/NCCL validation.

After these are fixed, please validate the recovery path on SGLang with real GPU pressure, including an asymmetric TP miss. Also, please use Refs #547 rather than Fixes #547 while the shared-budget part and the explicitly excluded exhaustion paths remain outstanding.

Comment thread kvcached/integration/sglang/patches.py Resolved
Comment thread kvcached/integration/sglang/patches.py Resolved
…artial allocations

Each SGLang TP rank drives its own kvcached pool, so a prefill batch
can miss on one rank and fit on its peers, and the peers would carry
the batch into the forward while the missing rank retries alone. The
alloc_for_extend wrapper now follows every local attempt with one MAX
all-reduce of a miss flag over the attention TP and CP CPU groups,
once per attempt and never conditional on allocator state, so a miss
anywhere unwinds every rank the same way and the requeue and the cap
are identical. The decode gate reduces its reservation verdict the
same way, so one short rank retracts everywhere.

The batch window journals the token blocks the elastic allocators
hand out, and a miss returns them to the manager that gave them: a
peer's miss returns this rank's whole allocation, and a miss in the
SWA composite's second child returns the first child's blocks instead
of leaking them on every retry.

Refs ovg-project#547

Signed-off-by: Rishabh Sinha <[email protected]>
…nt order

The rendezvous recorded the two ranks' miss flags in thread arrival
order, so the assertion on them depended on which thread deposited
first. CPU pytest 3.11 saw [1, 0] against the expected [0, 1]. The
fake now records the flags sorted.

Signed-off-by: Rishabh Sinha <[email protected]>
@rishabhsinha17

Copy link
Copy Markdown
Collaborator Author

round 2 pushed, head fb39167. both probes are regressions now, red at 57b07f5 and green at the head, 29 tests in the file. for tp the verdict is one MAX all-reduce per alloc_for_extend call and per decode gate over the attention tp and cp cpu groups, the groups sglang broadcasts the queue along. it runs whatever the local outcome and the ranks stay in step, and a peer that fit returns everything its own call handed out and requeues with the same cap. for swa the window journals token blocks, and the composite's partial allocation goes back through it. body updated with the coverage statement, dp attention covered, pp not, admission reading per-rank counts stays with the shared-budget pr, plus Refs #547. full cpu manifest 1299 passed with the failure set identical to main, mypy 3.10 to 3.13 clean. a two gpu tp run with an asymmetric miss is still pending on my side, runbook written for a 2x L40S box, logs follow once it has run.

@RixinLiu

Copy link
Copy Markdown
Collaborator

/ci run

@github-actions

Copy link
Copy Markdown

GPU CI (nightly) on b6fa209, requested by @RixinLiu: run

@github-actions

Copy link
Copy Markdown

Nightly correctness · 2026-10-13 · PASS

Run · kvcached b6fa209 (merged into main at 80450dc)
2x NVIDIA L4 (23034 MiB, driver 580.178.04); the SGLang and vLLM cases run at the same time, one GPU each · lmsysorg/sglang:v0.5.20, vllm/vllm-openai:v0.30.0

All 22 cases passed.

Setup of every case

On request in a pull request (gpu-pr.yml) on 2x NVIDIA L4 (23034 MiB, driver 580.178.04); the SGLang and vLLM cases run at the same time, one GPU each. kvcached is installed from the pull request merged into main into each engine's official image; each case starts one server in it.

engine image versions
SGLang lmsysorg/sglang:v0.5.20 (sha256:06e4f2ed21af…) sglang 0.5.20, kvcached 0.1.6, torch 2.13.0+cu130, CUDA 13.0
vLLM vllm/vllm-openai:v0.30.0 (sha256:8a69ffad015f…) vllm 0.30.0, kvcached 0.1.6, torch 2.13.0+cu130, CUDA 13.0
  • Every SGLang server: python3 -m sglang.launch_server --model-path MODEL --served-model-name e2e --host 127.0.0.1 --port 19000 --random-seed 42 --enable-cache-report, followed by the case's flags.
    • kvcached cases add ENABLE_KVCACHED=true KVCACHED_AUTOPATCH=1 and a per-server KVCACHED_IPC_NAME; KVCACHED_CONTIGUOUS_LAYOUT follows the KV layout.
    • native cases add ENABLE_KVCACHED=false KVCACHED_AUTOPATCH=0.
  • Every vLLM server: VLLM_USE_V2_MODEL_RUNNER=1 VLLM_SERVER_DEV_MODE=1 vllm serve MODEL --served-model-name e2e --host 127.0.0.1 --port 18000 --seed 42 --enable-prompt-tokens-details --enable-prefix-caching --async-scheduling --compilation-config.backend=eager, followed by the case's flags.
    • kvcached cases add ENABLE_KVCACHED=true KVCACHED_AUTOPATCH=1 and a per-server KVCACHED_IPC_NAME; KVCACHED_CONTIGUOUS_LAYOUT follows the KV layout.
    • native cases add ENABLE_KVCACHED=false KVCACHED_AUTOPATCH=0.

Cases

Each kvcached case is compared with the native case of the same engine and model.

case model KV cache TP KV dtype case flags
sglang-qwen05b-kv_c1 Qwen/Qwen2.5-0.5B-Instruct @ 7ae5576 kvcached, contiguous 1 model dtype --dtype bfloat16 --context-length 8192 --max-running-requests 32 --mem-fraction-static 0.8
sglang-qwen05b-kv_c0 Qwen/Qwen2.5-0.5B-Instruct @ 7ae5576 kvcached, non-contiguous 1 model dtype --dtype bfloat16 --context-length 8192 --max-running-requests 32 --mem-fraction-static 0.8
sglang-qwen05b-native Qwen/Qwen2.5-0.5B-Instruct @ 7ae5576 native 1 model dtype --dtype bfloat16 --context-length 8192 --max-running-requests 32 --mem-fraction-static 0.8
sglang-qwen35_4b-kv_c1 Qwen/Qwen3.5-4B @ 851bf6e kvcached, contiguous 1 model dtype --dtype bfloat16 --context-length 8192 --max-running-requests 16 --mem-fraction-static 0.85
sglang-qwen35_4b-kv_c0 Qwen/Qwen3.5-4B @ 851bf6e kvcached, non-contiguous 1 model dtype --dtype bfloat16 --context-length 8192 --max-running-requests 16 --mem-fraction-static 0.85
sglang-qwen35_4b-native Qwen/Qwen3.5-4B @ 851bf6e native 1 model dtype --dtype bfloat16 --context-length 8192 --max-running-requests 16 --mem-fraction-static 0.85
sglang-gemma4e2b-kv_c1 google/gemma-4-E2B-it @ 3e22461 kvcached, contiguous 1 model dtype --dtype bfloat16 --context-length 8192 --max-running-requests 16 --mem-fraction-static 0.85
sglang-gemma4e2b-kv_c0 google/gemma-4-E2B-it @ 3e22461 kvcached, non-contiguous 1 model dtype --dtype bfloat16 --context-length 8192 --max-running-requests 16 --mem-fraction-static 0.85
sglang-gemma4e2b-native google/gemma-4-E2B-it @ 3e22461 native 1 model dtype --dtype bfloat16 --context-length 8192 --max-running-requests 16 --mem-fraction-static 0.85
vllm-qwen05b-kv_c1 Qwen/Qwen2.5-0.5B-Instruct @ 7ae5576 kvcached, contiguous 1 model dtype --dtype bfloat16 --max-model-len 8192 --max-num-seqs 32 --gpu-memory-utilization 0.8
vllm-qwen05b-kv_c0 Qwen/Qwen2.5-0.5B-Instruct @ 7ae5576 kvcached, non-contiguous 1 model dtype --dtype bfloat16 --max-model-len 8192 --max-num-seqs 32 --gpu-memory-utilization 0.8
vllm-qwen05b-native Qwen/Qwen2.5-0.5B-Instruct @ 7ae5576 native 1 model dtype --dtype bfloat16 --max-model-len 8192 --max-num-seqs 32 --gpu-memory-utilization 0.8
vllm-qwen35_4b-kv_c1 Qwen/Qwen3.5-4B @ 851bf6e kvcached, contiguous 1 model dtype --dtype bfloat16 --max-model-len 8192 --max-num-seqs 16 --gpu-memory-utilization 0.9
vllm-qwen35_4b-kv_c0 Qwen/Qwen3.5-4B @ 851bf6e kvcached, non-contiguous 1 model dtype --dtype bfloat16 --max-model-len 8192 --max-num-seqs 16 --gpu-memory-utilization 0.9
vllm-qwen35_4b-native Qwen/Qwen3.5-4B @ 851bf6e native 1 model dtype --dtype bfloat16 --max-model-len 8192 --max-num-seqs 16 --gpu-memory-utilization 0.9 --block-size 1024
vllm-gemma4e2b-kv_c1 google/gemma-4-E2B-it @ 3e22461 kvcached, contiguous 1 model dtype --dtype bfloat16 --max-model-len 8192 --max-num-seqs 16 --gpu-memory-utilization 0.9
vllm-gemma4e2b-kv_c0 google/gemma-4-E2B-it @ 3e22461 kvcached, non-contiguous 1 model dtype --dtype bfloat16 --max-model-len 8192 --max-num-seqs 16 --gpu-memory-utilization 0.9
vllm-gemma4e2b-native google/gemma-4-E2B-it @ 3e22461 native 1 model dtype --dtype bfloat16 --max-model-len 8192 --max-num-seqs 16 --gpu-memory-utilization 0.9

Each case:

  • gen_seq: 9 prompts (6 short, and 300, 900 and 1800 words of filler text), greedy, 64 output tokens, one request at a time
  • gen_conc: the same 9 prompts, all at once
  • the prefix cache is reset
  • apc_0, apc_1, apc_2: 4 questions about one shared 1300-word document, greedy, 32 output tokens, one request at a time
  • apc_conc: the same 4 questions, all at once
  • the server is stopped with SIGINT to its process group (E2E_STOP_SIGNAL), SIGKILL after 90 s
check passes when
server_ready the server answers /health within the ready timeout
<phase>_ok every request of the phase returns HTTP 200 with output tokens
prefix_cache_reset the prefix-cache reset endpoint succeeds
server_alive_after_load the server process group is still running after the load
clean_exit_on_signal the server exits within 90 s of the stop signal
no_error_signatures no traceback, CUDA error or patch failure in the log before the stop signal
kvcached_active kvcached applied every required patch and sized the KV cache
kvcached_inactive the native server's log has no [kvcached] line
no_gpu_process_left no process is left on the GPUs of the run
<phase>_tokens_equal gen_seq and apc_0..2 give the same greedy tokens as native
apc_<n>_cached_tokens_equal every request reports the same cached_tokens as native

Elastic tests

case GPU server A server B
elastic-sglang-qwen05b+sglang-qwen35_4b 1 SGLang, Qwen/Qwen2.5-0.5B-Instruct @ 7ae5576, kvcached, contiguous, --dtype bfloat16 --context-length 8192 --mem-fraction-static 0.8 --max-running-requests 128 SGLang, Qwen/Qwen3.5-4B @ 851bf6e, kvcached, contiguous, --dtype bfloat16 --context-length 8192 --mem-fraction-static 0.85 --max-running-requests 128
elastic-vllm-qwen05b+vllm-qwen35_4b 0 vLLM, Qwen/Qwen2.5-0.5B-Instruct @ 7ae5576, kvcached, contiguous, --dtype bfloat16 --max-model-len 8192 --gpu-memory-utilization 0.8 --max-num-seqs 128 vLLM, Qwen/Qwen3.5-4B @ 851bf6e, kvcached, contiguous, --dtype bfloat16 --max-model-len 8192 --gpu-memory-utilization 0.9 --max-num-seqs 128
  • both servers use the contiguous KV layout and a batch limit of 128; A starts first, then B, on the same GPU
  • every load request is a distinct 3000-word filler prompt with 1024 output tokens and ignore_eos
  • calibration: 4 such requests per server measure the KV memory one request holds
  • each server's load is sized to need 70% of the GPU memory free after calibration, at most 128 requests
  • phases: (1) A gets its load; (2) A's prefix cache is reset; (3) B gets its load and its prefix cache is reset; (4) both get their loads at the same time
  • 3 greedy probe prompts of 32 tokens on each server before and after
check passes when
<server>_ready the server answers /health within the ready timeout
a_requests_ok, b_requests_ok every request of the server's own load succeeds
a_filled_memory A's KV usage grows by more than half of the free memory
a_released_memory after its prefix cache is reset, A keeps less than half of what it grew
b_reused_a_memory B's KV usage grows past what was free at A's peak
contended_requests_ok every request succeeds while both loads run
<server>_probe_unchanged the probes give the same tokens before and after
<server>_alive the server is still running at the end
<server>_<shutdown check> the shutdown and cleanup checks of each server

Limit tests

case server
limit-sglang-qwen05b SGLang, Qwen/Qwen2.5-0.5B-Instruct @ 7ae5576, kvcached, contiguous, --dtype bfloat16 --context-length 8192 --max-running-requests 32 --mem-fraction-static 0.8
limit-vllm-qwen05b vLLM, Qwen/Qwen2.5-0.5B-Instruct @ 7ae5576, kvcached, contiguous, --dtype bfloat16 --max-model-len 8192 --max-num-seqs 32 --gpu-memory-utilization 0.8
  • one kvcached server, contiguous KV layout
  • fill: 24 distinct 1500-word filler prompts with 1 output token, 8 at a time
  • kvctl limit lowers the server's limit to its idle usage plus a quarter of what the fill added
  • load under the lower limit: 100 distinct 150-word prompts with 16 output tokens, 8 at a time, then 4 distinct 1200-word prompts with 16 output tokens, one at a time
  • the limit is restored and the fill is repeated with new prompts
  • 3 greedy probe prompts of 32 tokens before and after
check passes when
<server>_ready the server answers /health within the ready timeout
limit_set kvctl limit succeeds, when lowering and when restoring the limit
limit_cache_filled every fill request succeeds and the KV usage grows
limit_released_idle within 30 s of lowering the limit, with no requests, the KV usage is within it
limit_requests_ok every request under the lower limit succeeds
limit_held_under_load the KV usage stays within the lower limit under that load
limit_raised_regrows after the limit is restored, the KV usage grows past the lower one
<server>_probe_unchanged the probes give the same tokens before and after
<server>_alive the server is still running at the end
<server>_<shutdown check> the shutdown and cleanup checks of the server

Logs: artifact, kept 30 days: serve.log and cmd.txt per case, summary.json with every request's tokens, setup.json, run.log, GPU state at the end.

@RixinLiu

Copy link
Copy Markdown
Collaborator

The ci result shows this PR fixes the problem on single GPU. But the multi-gpu problem still needs @rishabhsinha17 's effort

@rishabhsinha17

Copy link
Copy Markdown
Collaborator Author

opened #577 with a weekly ci case for the tp path: qwen3.5-4b at tp2 across both l4s, plus a second 4b pinned to gpu 1 that gets its load while the tp server holds its own, so rank 1 runs short and rank 0 does not. three checks read the miss lines of both ranks in serve.log. meant for /ci run weekly against this pr once that one merges. the pod runbook stays the fallback if the runner is busy.

@rishabhsinha17

Copy link
Copy Markdown
Collaborator Author

the shared-budget half is up as #578, stacked on this one. once per scheduler iteration the ranks of a queue group MIN-reduce their pools' free counts and admit and evict from the smallest, so the batches and the radix trees match and the miss flags line up after. the weekly case in #577 is the real test.

@shipiyouniao shipiyouniao 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.

Rechecked b6fa209: 30 existing capacity/autopatch tests pass. The findings below use CPU reproductions and SGLang 0.5.20 code inspection; I have not run real TP/PP GPU recovery.

Also confirmed the admission/eviction gap already tracked in #578: budgets 16/6 admit [a,b]/[a] despite both allocations succeeding; Mamba eviction differs by four slots and decode eviction by two tokens. The MAX reduction at patches.py:2609 happens after those decisions. These three CPU regressions fail here and pass on #578, confirming the existing dependency.

Comment thread kvcached/integration/sglang/patches.py Resolved
Comment thread kvcached/integration/sglang/patches.py Resolved
@shipiyouniao

Copy link
Copy Markdown
Collaborator

@rishabhsinha17 I checked #554/#578 against #487 and prepared a local combined candidate. I'd like to keep physical accounting and reservation ownership in #487, with #554 handling rollback/requeue and #578 providing the agreed scheduling budget.

The separate admission.py guard in #487 overlaps with that recovery path. It can return before #554's collective and reads the Mamba manager outside #578's budget view, so I don't think we should stack both paths unchanged. These are code-level concerns, not reproduced TP failures.

In the local combination, I removed the separate guard registration without changing your production implementation. 121 targeted CPU tests pass, including constructed allocation-denial and rollback cases. We still need the normal SGLang 0.5.20 elastic-serving scenario and real TP validation before retiring the guard. The cleanup must land together with, or after, the recovery path; it isn't safe to merge independently. Does this split work for you?

The retry and the decode gate agree a miss within a pipeline stage only,
so a stage that recovers a miss alone leaves the other stages waiting on
its sends. With more than one pipeline stage, keep SGLang's own
behaviour: the prefill wrappers pass through and the decode gate is the
native one.

The decode gate reserved the step's blocks with alloc(), which keeps a
request's blocks on one page, and the paged allocator's alloc_packed()
then took those reserved blocks first, so decode stopped filling the
small holes: with pages of 3, 5 and 40 free blocks, a gated
alloc_packed(7) took all seven from the 40. try_to_reserve() can now
place blocks as alloc_packed() would, and the paged gate asks for that.
@RixinLiu

Copy link
Copy Markdown
Collaborator

/ci run weekly

@github-actions

Copy link
Copy Markdown

GPU CI (weekly) on 9702a79, requested by @RixinLiu: run

@RixinLiu

Copy link
Copy Markdown
Collaborator

i fixed changes requested by @shipiyouniao. After the ci run weekly succeed, i will merge this one, then #487

@github-actions

Copy link
Copy Markdown

Weekly correctness · 2026-10-18 · PASS

Run · kvcached 9702a79 (merged into main at d5de5d7)
2x NVIDIA L4 (23034 MiB, driver 580.178.04) · vllm/vllm-openai:v0.30.0, lmsysorg/sglang:v0.5.20

All 26 cases passed.

These cases pass, though some prompts' greedy tokens differ from native: fewer than half of each phase's prompts, which is most likely numerical. Greedy decoding flips between near-tied tokens, and the engines also do this between their own runs.

case check prompts that differ
vllm-qwen35_9b-kv_c1-tp2 gen_seq_tokens_equal 1 of 9 prompts differ: prompt 8: {'index': 28, 'a': 11173, 'b': 1599}
vllm-qwen35_9b-kv_c1-tp2 apc_0_tokens_equal 1 of 4 prompts differ: prompt 2: {'index': 16, 'a': 6165, 'b': 10597}
vllm-qwen35_9b-kv_c1-tp2 apc_1_tokens_equal 1 of 4 prompts differ: prompt 2: {'index': 16, 'a': 6165, 'b': 10597}
vllm-qwen35_9b-kv_c1-tp2 apc_2_tokens_equal 1 of 4 prompts differ: prompt 2: {'index': 16, 'a': 6165, 'b': 10597}
Setup of every case

On request in a pull request (gpu-pr.yml) on 2x NVIDIA L4 (23034 MiB, driver 580.178.04). kvcached is installed from the pull request merged into main into each engine's official image; each case starts one server in it.

engine image versions
vLLM vllm/vllm-openai:v0.30.0 (sha256:8a69ffad015f…) vllm 0.30.0, kvcached 0.1.6, torch 2.13.0+cu130, CUDA 13.0
SGLang lmsysorg/sglang:v0.5.20 (sha256:06e4f2ed21af…) sglang 0.5.20, kvcached 0.1.6, torch 2.13.0+cu130, CUDA 13.0
  • Every vLLM server: HF_HUB_OFFLINE=1 VLLM_USE_V2_MODEL_RUNNER=1 VLLM_SERVER_DEV_MODE=1 vllm serve MODEL --served-model-name e2e --host 127.0.0.1 --port 18000 --seed 42 --enable-prompt-tokens-details --enable-prefix-caching --async-scheduling --compilation-config.backend=eager, followed by the case's flags.
    • kvcached cases add ENABLE_KVCACHED=true KVCACHED_AUTOPATCH=1 and a per-server KVCACHED_IPC_NAME; KVCACHED_CONTIGUOUS_LAYOUT follows the KV layout.
    • native cases add ENABLE_KVCACHED=false KVCACHED_AUTOPATCH=0.
  • Every SGLang server: HF_HUB_OFFLINE=1 python3 -m sglang.launch_server --model-path MODEL --served-model-name e2e --host 127.0.0.1 --port 18000 --random-seed 42 --enable-cache-report, followed by the case's flags.
    • kvcached cases add ENABLE_KVCACHED=true KVCACHED_AUTOPATCH=1 and a per-server KVCACHED_IPC_NAME; KVCACHED_CONTIGUOUS_LAYOUT follows the KV layout.
    • native cases add ENABLE_KVCACHED=false KVCACHED_AUTOPATCH=0.

Cases

Each kvcached case is compared with the native case of the same engine and model.

case model KV cache TP KV dtype case flags
vllm-qwen05b-kv_c1-tp2 Qwen/Qwen2.5-0.5B-Instruct @ 7ae5576 kvcached, contiguous 2 model dtype --dtype bfloat16 --max-model-len 8192 --max-num-seqs 32 --gpu-memory-utilization 0.8
vllm-qwen05b-kv_c0-tp2 Qwen/Qwen2.5-0.5B-Instruct @ 7ae5576 kvcached, non-contiguous 2 model dtype --dtype bfloat16 --max-model-len 8192 --max-num-seqs 32 --gpu-memory-utilization 0.8
vllm-qwen05b-native-tp2 Qwen/Qwen2.5-0.5B-Instruct @ 7ae5576 native 2 model dtype --dtype bfloat16 --max-model-len 8192 --max-num-seqs 32 --gpu-memory-utilization 0.8
vllm-dsv2lite-kv_c1-tp2 deepseek-ai/DeepSeek-V2-Lite-Chat @ 8586474 kvcached, contiguous 2 model dtype --dtype bfloat16 --max-model-len 8192 --max-num-seqs 16 --gpu-memory-utilization 0.9 --trust-remote-code
vllm-dsv2lite-kv_c0-tp2 deepseek-ai/DeepSeek-V2-Lite-Chat @ 8586474 kvcached, non-contiguous 2 model dtype --dtype bfloat16 --max-model-len 8192 --max-num-seqs 16 --gpu-memory-utilization 0.9 --trust-remote-code
vllm-dsv2lite-native-tp2 deepseek-ai/DeepSeek-V2-Lite-Chat @ 8586474 native 2 model dtype --dtype bfloat16 --max-model-len 8192 --max-num-seqs 16 --gpu-memory-utilization 0.9 --trust-remote-code
vllm-gemma4_12b-kv_c1-tp2 google/gemma-4-12B-it @ 707f0a3 kvcached, contiguous 2 model dtype --dtype bfloat16 --max-model-len 8192 --max-num-seqs 16 --gpu-memory-utilization 0.9
vllm-gemma4_12b-kv_c0-tp2 google/gemma-4-12B-it @ 707f0a3 kvcached, non-contiguous 2 model dtype --dtype bfloat16 --max-model-len 8192 --max-num-seqs 16 --gpu-memory-utilization 0.9
vllm-gemma4_12b-native-tp2 google/gemma-4-12B-it @ 707f0a3 native 2 model dtype --dtype bfloat16 --max-model-len 8192 --max-num-seqs 16 --gpu-memory-utilization 0.9
vllm-qwen35_9b-kv_c1-tp2 Qwen/Qwen3.5-9B @ c202236 kvcached, contiguous 2 model dtype --dtype bfloat16 --max-model-len 8192 --max-num-seqs 16 --gpu-memory-utilization 0.9
vllm-qwen35_9b-kv_c0-tp2 Qwen/Qwen3.5-9B @ c202236 kvcached, non-contiguous 2 model dtype --dtype bfloat16 --max-model-len 8192 --max-num-seqs 16 --gpu-memory-utilization 0.9
vllm-qwen35_9b-native-tp2 Qwen/Qwen3.5-9B @ c202236 native 2 model dtype --dtype bfloat16 --max-model-len 8192 --max-num-seqs 16 --gpu-memory-utilization 0.9 --block-size 1024
sglang-qwen05b-kv_c1-tp2 Qwen/Qwen2.5-0.5B-Instruct @ 7ae5576 kvcached, contiguous 2 model dtype --dtype bfloat16 --context-length 8192 --max-running-requests 32 --mem-fraction-static 0.8
sglang-qwen05b-kv_c0-tp2 Qwen/Qwen2.5-0.5B-Instruct @ 7ae5576 kvcached, non-contiguous 2 model dtype --dtype bfloat16 --context-length 8192 --max-running-requests 32 --mem-fraction-static 0.8
sglang-qwen05b-native-tp2 Qwen/Qwen2.5-0.5B-Instruct @ 7ae5576 native 2 model dtype --dtype bfloat16 --context-length 8192 --max-running-requests 32 --mem-fraction-static 0.8
sglang-dsv2lite-kv_c1-tp2 deepseek-ai/DeepSeek-V2-Lite-Chat @ 8586474 kvcached, contiguous 2 model dtype --dtype bfloat16 --context-length 8192 --max-running-requests 16 --mem-fraction-static 0.85 --trust-remote-code
sglang-dsv2lite-kv_c0-tp2 deepseek-ai/DeepSeek-V2-Lite-Chat @ 8586474 kvcached, non-contiguous 2 model dtype --dtype bfloat16 --context-length 8192 --max-running-requests 16 --mem-fraction-static 0.85 --trust-remote-code
sglang-dsv2lite-native-tp2 deepseek-ai/DeepSeek-V2-Lite-Chat @ 8586474 native 2 model dtype --dtype bfloat16 --context-length 8192 --max-running-requests 16 --mem-fraction-static 0.85 --trust-remote-code
sglang-qwen35_9b-kv_c1-tp2 Qwen/Qwen3.5-9B @ c202236 kvcached, contiguous 2 model dtype --dtype bfloat16 --context-length 8192 --max-running-requests 16 --mem-fraction-static 0.85
sglang-qwen35_9b-kv_c0-tp2 Qwen/Qwen3.5-9B @ c202236 kvcached, non-contiguous 2 model dtype --dtype bfloat16 --context-length 8192 --max-running-requests 16 --mem-fraction-static 0.85
sglang-qwen35_9b-native-tp2 Qwen/Qwen3.5-9B @ c202236 native 2 model dtype --dtype bfloat16 --context-length 8192 --max-running-requests 16 --mem-fraction-static 0.85
vllm-qwen05b-kv_c1-fp8kv Qwen/Qwen2.5-0.5B-Instruct @ 7ae5576 kvcached, contiguous 1 fp8 --dtype bfloat16 --max-model-len 8192 --max-num-seqs 32 --gpu-memory-utilization 0.8
vllm-qwen05b-native-fp8kv Qwen/Qwen2.5-0.5B-Instruct @ 7ae5576 native 1 fp8 --dtype bfloat16 --max-model-len 8192 --max-num-seqs 32 --gpu-memory-utilization 0.8
sglang-qwen05b-kv_c1-fp8kv Qwen/Qwen2.5-0.5B-Instruct @ 7ae5576 kvcached, contiguous 1 fp8_e4m3 --dtype bfloat16 --context-length 8192 --max-running-requests 32 --mem-fraction-static 0.8
sglang-qwen05b-native-fp8kv Qwen/Qwen2.5-0.5B-Instruct @ 7ae5576 native 1 fp8_e4m3 --dtype bfloat16 --context-length 8192 --max-running-requests 32 --mem-fraction-static 0.8

Each case:

  • gen_seq: 9 prompts (6 short, and 300, 900 and 1800 words of filler text), greedy, 64 output tokens, one request at a time
  • gen_conc: the same 9 prompts, all at once
  • the prefix cache is reset
  • apc_0, apc_1, apc_2: 4 questions about one shared 1300-word document, greedy, 32 output tokens, one request at a time
  • apc_conc: the same 4 questions, all at once
  • the server is stopped with SIGINT to its process group (E2E_STOP_SIGNAL), SIGKILL after 90 s
check passes when
server_ready the server answers /health within the ready timeout
<phase>_ok every request of the phase returns HTTP 200 with output tokens
prefix_cache_reset the prefix-cache reset endpoint succeeds
server_alive_after_load the server process group is still running after the load
clean_exit_on_signal the server exits within 90 s of the stop signal
no_error_signatures no traceback, CUDA error or patch failure in the log before the stop signal
kvcached_active kvcached applied every required patch and sized the KV cache
kvcached_inactive the native server's log has no [kvcached] line
no_gpu_process_left no process is left on the GPUs of the run
<phase>_tokens_equal in gen_seq and in each of apc_0..2, more than half of the prompts give the same greedy tokens as native
apc_<n>_cached_tokens_equal every request reports the same cached_tokens as native

Elastic tests

case GPU server A server B
elastic-vllm-qwen05b+sglang-qwen35_4b 0 vLLM, Qwen/Qwen2.5-0.5B-Instruct @ 7ae5576, kvcached, contiguous, --dtype bfloat16 --max-model-len 8192 --gpu-memory-utilization 0.8 --max-num-seqs 128 SGLang, Qwen/Qwen3.5-4B @ 851bf6e, kvcached, contiguous, --dtype bfloat16 --context-length 8192 --mem-fraction-static 0.85 --max-running-requests 128
  • both servers use the contiguous KV layout and a batch limit of 128; A starts first, then B, on the same GPU
  • every load request is a distinct 3000-word filler prompt with 1024 output tokens and ignore_eos
  • calibration: 4 such requests per server measure the KV memory one request holds
  • each server's load is sized to need 70% of the GPU memory free after calibration, at most 128 requests
  • phases: (1) A gets its load; (2) A's prefix cache is reset; (3) B gets its load and its prefix cache is reset; (4) both get their loads at the same time
  • 3 greedy probe prompts of 32 tokens on each server before and after
check passes when
<server>_ready the server answers /health within the ready timeout
a_requests_ok, b_requests_ok every request of the server's own load succeeds
a_filled_memory A's KV usage grows by more than half of the free memory
a_released_memory after its prefix cache is reset, A keeps less than half of what it grew
b_reused_a_memory B's KV usage grows past what was free at A's peak
contended_requests_ok every request succeeds while both loads run
<server>_probe_unchanged the probes give the same tokens before and after
<server>_alive the server is still running at the end
<server>_<shutdown check> the shutdown and cleanup checks of each server

Logs: artifact, kept 30 days: serve.log and cmd.txt per case, summary.json with every request's tokens, setup.json, run.log, GPU state at the end.

@RixinLiu

Copy link
Copy Markdown
Collaborator

The weekly correctness result above looks like indeterministic of inference engine, as this pr modifies the sglang end, not vllm end. And last weekly correctness result is ok. This result means this pr fixed the problem on TP scenario in SGLang, but the test which will be added in #577 should still fail until #578 got merged.

@shipiyouniao shipiyouniao 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.

LGTM, both review points are addressed. Thanks for the fixes!

@RixinLiu
RixinLiu merged commit a2af415 into ovg-project:main Oct 18, 2026
10 checks passed
RixinLiu added a commit to shipiyouniao/kvcached that referenced this pull request Oct 18, 2026
…dles misses

ovg-project#554 returns a prefill batch that the pools cannot back to the waiting
queue, so the separate admission guard is no longer needed; both wrapped
Scheduler._get_new_batch_prefill_raw. Also drop the A/B benchmark
script.
@rishabhsinha17

Copy link
Copy Markdown
Collaborator Author

the split works for me. i rebased #578 on main at 1f6517e, one commit over #554 and #487. on the ledger it adds one agreed forecast per iteration. every rank reads each manager's available_size(), the count #487 clamps to the per-GPU ledger, one MIN reduce over the queue groups makes the shortest forecast the budget of all, and the allocators serve it plus the round's own moves. nothing to unregister on the guard, c511e8d on the #487 branch dropped admission.py and its tests before the merge, so main has one recovery path. a pipeline skips the round, like the miss patches. 40 tests in the capacity-miss file, 6 red without the round, cpu manifest 1471 passed, failure set as main, mypy 3.10 to 3.13 clean. the weekly case in #577 is the acceptance test once its rework is in.

rishabhsinha17 added a commit to rishabhsinha17/kvcached that referenced this pull request Oct 26, 2026
The round hooks the same methods from 0.5.13 on: the three scheduler entries
it wraps, the attention TP and CP groups it reduces over, and the elastic
SWA and Mamba slot allocators it reads, which first exist at 0.5.13 (the
allocator split). Users of 0.5.13 to 0.5.19 hit the same TP admission
desync as 0.5.20, so the gate moves from 0.5.20 to 0.5.13. The two miss
patches of ovg-project#554 keep 0.5.20, where check_decode_capacity and the prefill
unwind they rely on exist, so below 0.5.20 the ranks admit the same batch
and a physical miss stays fatal, as native.

A boundary test pins the range: at 0.5.13 two ranks admit the same batch
from one MIN reduce and no MAX reduce follows, at 0.5.12 nothing is wrapped
and each rank admits from its own count. The 0.5.19 test now expects the
round in and the miss patches out.

Signed-off-by: Rishabh Sinha <[email protected]>
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.

3 participants