Repository navigation
Description
Problem
Every speculative round ends by rolling the KV cache back to the accepted prefix, and every rollback re-slices the entire live window:
// KVCache::trim
self.keys = Some(ffi::slice(keys, &[0, 0, 0, 0], &[k_shape[0], k_shape[1], live_len_after, k_shape[3]]));
self.values = Some(ffi::slice(values, ...));slice produces a new array. So discarding two rejected draft positions allocates and writes a fresh copy of K and V over the whole cache, and the cost of that grows with the conversation while the work it undoes stays constant at a few tokens.
The same shape applies to gather_positions, added for draft-tree rollback (#1204): it selects along the sequence axis with take, which is also a full rewrite.
Scale
Not yet measured, and the estimate below is arithmetic rather than a benchmark, which is the first thing this issue should fix.
For an 8-KV-head, 128-head-dim f16 cache the live window is 2 * heads * len * dim * 2 bytes across K and V, so about 4 MB at 1k tokens, 34 MB at 8k, and 134 MB at 32k. Read plus write at a few hundred GB/s puts a 32k rollback in the sub-millisecond range against a round that is 25 to 40 ms on the pairings measured for #1204 and #1185. Small, but it is paid every round, it is pure waste, and it is the term that grows when everything else in the round does not.
Why it is worth fixing rather than accepting
The rollback discards a bounded number of tail positions. Nothing about that requires touching the prefix, which is the overwhelming majority of the buffer and is bit-identical before and after.
Two directions, neither explored:
- Track a logical length instead of rewriting. The cache already separates
offsetfromlive_startfor head trims, precisely so a head trim moves an index rather than data. A tail trim could do the same: leave the buffer, lower the length, and let the next append overwrite the abandoned slots. Turbo and paged modes would need their own treatment, and the fetch paths that slice[0..offset]would have to keep agreeing with it. - Scatter into the surviving slots. For a tree, the accepted path has to be compacted rather than truncated, so a logical length is not enough; writing the few survivors into their new slots touches
O(accepted)instead ofO(context).
Acceptance criteria
- Rollback cost measured against context length, so the size of the problem is a number rather than an estimate. A sweep at 1k, 8k and 32k on one pairing is enough.
- Tail rollback stops rewriting the prefix, for at least the dense
Fp16andInt8modes. - Turbo and paged modes either get the same treatment or keep the current path with the reason recorded, rather than being left to fall through silently.
- Byte-identical decode output before and after, verified by diffing generated text at temperature 0, not by trusting a unit test on lengths.
-
gather_positionsgets the same treatment or an explicit note that a compaction cannot avoid the rewrite.
Found while wiring #1204, where the same cost appears in tree rollback. It is not specific to trees: the linear speculative path pays it today on every round of every speculative decode.
Activity
feat(cache): compact a scattered accept set within the verify block only
feat(speculative): draft and verify a token tree, behind a flag, defa…
perf(cache): make the dense KV tail trim a logical rewind instead of …
feat(speculative): draft and verify a token tree, behind a flag, defa…
perf(cache): make the dense KV tail trim a logical rewind instead of …