Repository navigation
feat(server): state-only backups in the prefix cache for Qwen and DeepSeek, AgentX bench under the official rules (92.2% cache hit rate) - #989
Conversation
davide221
left a comment
There was a problem hiding this comment.
Code review
Found 1 issue.
Copy to agent
Use the copy button in the top-right corner of the block, then paste into your agent.
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
Repository: Luce-Org/lucebox
Reviewed commit: 3c7c4e695dd67839a14f2f32902c7e6831792275
Verify the findings against the current code, run relevant checks for any fixes, and explain any findings you reject.
<file name="server/scripts/bench_agentx_cache.py">
<violation number="1" line="149" end_line="150">
report() crashes with StopIteration when the client failed and wrote no export
`run()` calls `report(out, a.warm, a.duration)` *before* checking the client's return code (line 123–125), and `readings()` obtains the export with `next((out / "client").rglob("profile_export.jsonl"))` with no default. When the aiperf client exits non-zero without producing a `profile_export.jsonl` — a bad `--url` after `check_server`, a dataset/tokenizer fetch failure, an early client crash — `rglob` yields nothing and `next()` raises an unhandled `StopIteration`, so the user gets a traceback instead of the intended message `"the client exited with {rc}: see {out}/client.log"` that the code at line 124–125 was written to produce. Give `next` a default (`None`) and raise `SystemExit` pointing at `client.log`, or check `rc` before reporting.
<suggestion>
path = next((out / "client").rglob("profile_export.jsonl"), None)
if path is None:
raise SystemExit(f"no profile_export.jsonl under {out}/client: see {out}/client.log")
records = [json.loads(line) for line in path.open() if line.strip()]
</suggestion>
</violation>
</file>
Issue overview
- report() crashes with StopIteration when the client failed and wrote no export · confirmed (
server/scripts/bench_agentx_cache.py:149-150)
Reviewed 3c7c4e69 · 1 candidate issue checked, kept at ≥70% confidence · add the code-review label to review again
| path = next((out / "client").rglob("profile_export.jsonl")) | ||
| records = [json.loads(line) for line in path.open() if line.strip()] |
There was a problem hiding this comment.
report() crashes with StopIteration when the client failed and wrote no export · confirmed
run() calls report(out, a.warm, a.duration) before checking the client's return code (line 123–125), and readings() obtains the export with next((out / "client").rglob("profile_export.jsonl")) with no default. When the aiperf client exits non-zero without producing a profile_export.jsonl — a bad --url after check_server, a dataset/tokenizer fetch failure, an early client crash — rglob yields nothing and next() raises an unhandled StopIteration, so the user gets a traceback instead of the intended message "the client exited with {rc}: see {out}/client.log" that the code at line 124–125 was written to produce. Give next a default (None) and raise SystemExit pointing at client.log, or check rc before reporting.
Checked (90%): server/scripts/bench_agentx_cache.py:123-125 calls report() before checking rc, and readings() at line 149 uses next((out/'client').rglob('profile_export.jsonl')) with no default, so a client failure that produces no export (crash, fetch failure) yields an unhandled StopIteration traceback instead of the intended 'the client exited with {rc}' message at line 125. The only residual assumption is that aiperf can exit non-zero without writing the export, which is the normal failure behavior for a crashed profiling tool.
| path = next((out / "client").rglob("profile_export.jsonl")) | |
| records = [json.loads(line) for line in path.open() if line.strip()] | |
| path = next((out / "client").rglob("profile_export.jsonl"), None) | |
| if path is None: | |
| raise SystemExit(f"no profile_export.jsonl under {out}/client: see {out}/client.log") | |
| records = [json.loads(line) for line in path.open() if line.strip()] |
…che hit rate bench A superseded turn's snapshot repeats the attention rows of the snapshot that supersedes it. Keep it as a state-only backup (recurrent state, decode seed, drafter features) and restore it with the rows of the newer snapshot, so a prompt that departs before the end of the conversation resumes near where it departs instead of at the session head. Backups thin out with distance, and the served conversation's backups are evicted after other conversations' leaves. Qwen3.5 dense single-sequence serving; other paths are unchanged. Also: request field ignore_eos, and scripts/bench_agentx_cache.py to measure the cache hit rate on replayed agent sessions. Co-Authored-By: Claude Fable 5.2 <[email protected]>
One trimming routine for both serving paths (KvCache::trim_superseded). A concurrent engine drops a checkpoint's rows through SeqEngine::drop_prefix_kv and restores it with PrefixStorePlan::restore_kv; Qwen's paged engine and the DeepSeek 4 single-sequence backend implement the split. Co-Authored-By: Claude Fable 5.2 <[email protected]>
…4 concurrent serving Co-Authored-By: Claude Fable 5.2 <[email protected]>
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
3c7c4e6 to
6a49611
Compare
The bench now runs the official harness (agentx-harness 1.0.13 from PyPI) in its agentx scenario unchanged: recorded replies, no override, the scenario's timing, cache bust and warm-up, and the official 256K corpus by default. It reports the measured phase, the harness's hit rate for a cache that never misses, and the harness's submission_valid verdict. Qwen3.8-27B, one user, 15 minutes: 92.2% against 95.2%, submission_valid true. Co-Authored-By: Claude Opus 5.5 <[email protected]>
What
The prefix cache now keeps the turns behind a conversation's newest snapshot as state-only backups instead of freeing them.
A superseded turn's snapshot repeats the per-position rows (attention K/V, compressed rows) of the snapshot that supersedes it. Only the state of its own position is its own. So the backup drops its rows and restores with the rows of the newer snapshot. For Qwen3.8-27B a backup is about 370 MB at any length, against about 4 GB for a whole snapshot at 100K tokens.
One policy, every supported model. Which turns to keep, thinning, eviction and donor choice live in the model-agnostic cache (
KvCache::trim_superseded,PrefixCache), shared by single-sequence and concurrent serving. A backend only says what a backup holds and drops and borrows rows:--max-concurrency)Contract:
ModelBackend::snapshot_state_bytes_estimate/snapshot_drop_kv/GenerateRequest::restore_kv_slot, andSeqEngine::estimate_prefix_state_bytes/drop_prefix_kv/PrefixStorePlan::restore_kv. A restore validates the donor before it touches the live cache and falls back to a cold prefill when it cannot.Also in this PR, because they are what measures it:
ignore_eos(keep generating tomax_tokens), documented inserver/docs/API.md;server/scripts/bench_agentx_cache.py, which runs the official AgentX harness (agentx-harness1.0.13) in itsagentxscenario unchanged and reports the cache hit rate, the harness's figure for a cache that never misses, and the harness'ssubmission_validverdict.Why
Before, the previous turn was kept whole only "while the budget holds it", and the cache fills any budget with idle conversations' snapshots. A prompt that changed only its last message (a retry, an edit, a second request from the same point) then fell back to the session head and recomputed about half the context. A state-only backup costs a few hundred MB instead of a whole snapshot, so the turn it serves stays in the cache.
Result
The AgentX benchmark under its own rules, run with
server/scripts/bench_agentx_cache.pyfrom this PR:python3 server/scripts/bench_agentx_cache.py run --url http://127.0.0.1:8090 \ --model qwen3.8-27b --tokenizer Qwen/Qwen3.8-27B --sessions 1 --duration 900 --out results/agentx-1The official harness (
agentx-harness1.0.13) in itsagentxscenario, unchanged: recorded replies, no--unsafe-override, the scenario's timing, cache bust and warm-up, the official 256K corpus (semianalysis_cc_traces_weka_062126_256k), 15 minutes measured (the rules' minimum). Qwen3.8-27B with its drafter,--max-ctx 262144, default cache budget, one user, one H100, this PR's head.submission_valid(the harness's verdict)One run. No state backup was restored in those 15 minutes, so the run shows where the engine stands on the benchmark, not the gain of the backups: that needs a longer run, where the cache fills up.
Checks
--paged-attention --max-concurrency 2): 2/2 equal, with a backup thinned and a restore from a backup.test_server_unit); a paged Qwen checkpoint, a Flash-Next snapshot and a DeepSeek snapshot each restore byte for byte from a backup plus a deeper donor, and refuse donors that cannot supply the rows (test_recurrent_snapshot,test_qwen4exp_prefix_cache,test_deepseek4_unit).state backup slot=N ... restores with the rows of slot=M). Against a cold server with the cache off, Flash-Next concurrent is 2/2 equal; the other modes differ there for the whole-snapshot restore too, because a cold prefill without restore points runs other prefill chunks, so that is not from this PR.test_props_memory_reportfails in our CUDA test container;test_qwen4exp_prefix_cachesegfaults on an H100 in its KVFlash part, after the snapshot tests pass.🤖 Generated with Claude Code