BugZoo: KV-cache contracts #
LLM inference-engine bug reports include cache-shift bugs, RoPE/position mismatches, shape mistakes, and resource/configuration errors. TorchLean does not verify a full serving engine, paged attention allocator, or multi-GPU scheduler. The useful first step is still precise: represent the cache update as a typed tensor operation and prove the append invariant we rely on.
Reference:
- Liu et al., "A First Look at Bugs in LLM Inference Engines", 2025.
This file proves the cache append boundary. A stronger future theorem should connect cached decode to full-sequence attention:
$$ \operatorname{decodeWithCache}(\mathit{prefix},\mathit{newToken}) =\operatorname{fullAttention}(\mathit{prefix}\mathbin{+\!\!+}[\mathit{newToken}]). $$
under the same mask, RoPE/position encoding, and numeric semantics.
A key/value cache with an explicit sequence length and head dimension.
- keys : TorchLean.Tensor α [seqLen, headDim]
Cached key vectors, indexed by time.
- values : TorchLean.Tensor α [seqLen, headDim]
Cached value vectors, indexed by time.
Instances For
View one token vector as a length-one sequence.
Instances For
Reading the only position of a singleton sequence gives the token back.
Append one token vector to a sequence cache along the time axis.
Instances For
The appended token lands at index seqLen, that is, at the end.
This is the fact an off-by-one in a KV cache would break: writing the new token over the last cached position instead of after it. The type already forces the length to grow by one, and this lemma pins down where the new entry goes.
Append both key and value vectors to the KV cache.
Instances For
The newly appended key is exactly the final key in the updated cache.
The newly appended value is exactly the final value in the updated cache.