Repository navigation
The prefix hash memo answers one prefill at a time: its lock holds across the reuse - #182
AgentSparko wants to merge 2 commits into
Conversation
…emo (fails: they can)
0xBakeer
left a comment
There was a problem hiding this comment.
Thanks, the fix is right. One lock around the whole read, extend and store, no path takes another lock or calls back into the memo, and for a single request the hashes are identical to main's. The new test fails on main and passes here.
One change needed: tests/test_adapter_guards.py::test_goldens_hold fails with this PR. engine/cache.py is in the whole-tree half of the 27B's v2 fingerprint, so any edit to it moves GOLDEN_V2[1]. On current main (4eb1d7a) plus this PR the value is 6f4e500565b1d8b57b6e3f53bfc41491f70672ae2f9c403f04b134aaa378bd5d. Please rebase on main, set GOLDEN_V2[1] to it, and add a short comment above the tuple saying what moved it, like the entries there. Recompute it if main moves before you push.
The gate job is red only because there is no gate report; that is a maintainer label, nothing for you to do.
A test that failed before the fix and passes after is included.
High: the prefix hash memo was shared by concurrent prefills without a lock
engine/cache.pykeeps one module-global_PrefixHashMemoand every slot's prefillcalls it. Two handler threads interleaved between reading the memo's keys and reading
its hashes: the first prefill sliced a hash list that was no longer its prefix — a
too-short list (IndexError mid-prefill) or another prompt's hashes stored under these
keys (silent cache misses).
_PrefixHashMemo.__call__now holds athreading.Lockacross the reuse. The hot path pays one uncontended acquire before a call that then
runs a whole prefill; under contention the worst case is the memo's own full hash pass
(~20 ms at 190k tokens), far under one prefill's GPU turn. A per-slot memo was the
alternative; the lock keeps the reuse across a conversation's turns exactly as it was.
Tests
tests/test_hash_memo_race.py— a second prefill landing between the first's keysread and hashes read cannot tear the first's hash list (before: the first returned 2
entries for a 1,050-token prompt); a burst of eight concurrent prefills each gets
prefix_hashesof its own keys, every time.Regression run (CPU, engine venv)
tests/test_cache.py46 passed.