Skip to content

perf: cross-request prefix-KV cache mechanism [wiring + A/B pending] - #101

Open
leyten wants to merge 1 commit into
masterfrom
perf/prefix-kv-cache
Open

perf: cross-request prefix-KV cache mechanism [wiring + A/B pending]#101
leyten wants to merge 1 commit into
masterfrom
perf/prefix-kv-cache

Conversation

@leyten

@leyten leyten commented Jul 14, 2026

Copy link
Copy Markdown
Owner

DO NOT MERGE — mechanism only, wiring + A/B pending. This adds the coordinator-side lever and proves it token-exact on the fake ring; it is NOT wired into the gateway and the multi-turn TTFT win is unmeasured.

What: in static-KV mode reset() keeps KV, so a follow-up turn on the same ring reuses the resident KV of the shared prefix. coordinate_pipe(cached_prefix=N) skips re-prefilling [0:N] and prefills only the suffix at start=N.

Why it is correct: greedy verify commits the same tokens whether [0:N] was just written or reused (given the rows hold the same prefix). run_coordinator_twoturn runs two jobs on one persistent FakeRing; the ring RAISES (KV-GAP / KV-CONTENT) if the cached turn reads an unwritten or clobbered slot — so a green test proves legitimate reuse, not a skipped assertion. Both chunked + whole-suffix pass; turn 2 first verify starts at the cache boundary.

Follow-up (ring-gated): gateway session->ring pinning + cache-validity bookkeeping (same ring served the prior turn; rows not evicted by a batched tenant), EAGLE-context reseed over the cached prefix (speed only), and the on-ring multi-turn TTFT A/B. Task #5.

In static-KV mode the stage's reset() is a logical no-op — the KV buffer keeps
what was written and reads are :total-bounded — so a follow-up turn on the same
ring can reuse the resident KV of the shared prompt prefix instead of re-prefilling
it. Add a cached_prefix arg to coordinate_pipe: skip the verify frames for [0:N],
prefill only the suffix at start=N, decode as usual. Token-exact by construction —
greedy verify commits the same tokens whether [0:N] was just written or reused,
given those rows hold the same prefix.

Proven on the fake ring: run_coordinator_twoturn drives two jobs on ONE persistent
ring (turn 1 warms KV, turn 2 reuses it). The ring's KV-GAP / KV-CONTENT asserts
raise if the cached turn reads an unwritten or clobbered slot, so the green
multi-turn test proves the cache reused only legitimately-resident, correct KV;
both chunked and whole-suffix prefill pass and turn 2's first verify starts at the
cache boundary, not 0.

Mechanism only. The gateway session->ring pinning + cache-validity bookkeeping
(same ring served the prior turn; rows not evicted) is the ring-gated follow-up —
NOT wired into serving. Multi-turn TTFT A/B pending a live ring.
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.

1 participant