Skip to content

fix: TurboKV silently drops history after a second compression round - #63

Merged
solderzzc merged 1 commit into
mainfrom
fix/turbokv-compressed-offset-reset
Sep 5, 2026
Merged

solderzzc merged 1 commit into
mainfrom
fix/turbokv-compressed-offset-reset

Conversation

@solderzzc

Copy link
Copy Markdown
Member

Summary

  • compressedOffset kept accumulating across TurboKV eviction rounds (self.compressedOffset += newColdCount) even though self.keys gets completely rebuilt on every eviction into a fresh buffer holding only the hot window, re-indexed from 0. The stale offset was then used to slice that new buffer on the next eviction — the coldEnd/newColdCount arithmetic looked plausible against the new (shorter) buffer, but pointed at the wrong tokens. The skipped window was never compressed into polarKeys and was then overwritten by the next write, permanently vanishing from history with no error, crash, or size mismatch the caller would notice — until something downstream (e.g. attention math expecting the full sequence length) broke.
  • With the real defaults (turboMinActivationTokens=2048, hotWindowSize=256, step=256), this triggers on any request past roughly 2560 tokens — a second eviction round.
  • Also fixes KVCacheSimple.state's getter, which used compressedOffset > 0 as a proxy for "there is compressed history in polarKeys to decode." That broke the moment compressedOffset started being correctly reset to 0 after each eviction. polarKeys/polarValues being non-nil is already the correct, sufficient signal — they're only ever set inside the eviction block.

How this was found

Doing a lightweight, non-model-download perf/quality comparison against upstream's independently-built TurboQuant subsystem (unrelated migration question, tracked separately) — driving KVCacheSimple through several eviction rounds with synthetic data surfaced a token-count mismatch (768 tokens back out of state for 1024 fed in) before it ever got to comparing numbers.

Test plan

  • Added testTurboKVMultiRoundEvictionPreservesAllTokens (Tests/MLXLMTests/KVCacheTests.swift) — feeds four step-sized (256-token) chunks through a real KVCacheSimple with the default hot window (triggering two eviction rounds), asserts the full token count survives and per-chunk MSE matches TurboQuant's own expected ~0.03 at 3-bit.
  • Red-green verified: reverting just the KVCache.swift fix (keeping the new test) reproduces the exact original symptom — state[0].dim(2) comes back as 768 instead of 1024, then a [broadcast_shapes] Shapes (1,4,1024,128) and (1,4,768,128) cannot be broadcast crash downstream. Restoring the fix passes cleanly.
  • Full MLXLMTests suite (108 tests) passes with the fix applied — no regressions.

🤖 Generated with Claude Code

…opping history

self.compressedOffset kept accumulating across eviction rounds
(`self.compressedOffset += newColdCount`) even though self.keys gets
completely rebuilt on every eviction into a fresh buffer holding only the
hot window, indexed from 0 again. The stale offset was then used to slice
that *new* buffer on the next eviction — the coldEnd/newColdCount math
looked plausible against the new buffer's shorter length, but pointed at
the wrong tokens. The affected window was never compressed into polarKeys
and was then overwritten on the next write, permanently vanishing from
history with no error, crash, or size mismatch the caller would notice
until something downstream (e.g. attention math expecting the full
sequence length) broke.

With defaults (turboMinActivationTokens=2048, hotWindowSize=256,
step=256) this triggers on any request past ~2560 tokens — a second
eviction round.

Also fixes the `state` getter's guard, which used `compressedOffset > 0`
as a proxy for "there is compressed history in polarKeys to decode" — that
broke as soon as compressedOffset started being correctly reset to 0 after
each eviction. polarKeys/polarValues being non-nil is already the correct,
sufficient signal (they're only ever set inside the eviction block).

Added a regression test that feeds four step-sized chunks through a real
KVCacheSimple with the default hot window (triggering two eviction
rounds) and asserts the full token count and content survive. Red-green
verified: reverting the fix reproduces the exact original symptom (state
count 768 instead of 1024, then a broadcast-shape crash downstream).

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@solderzzc
solderzzc merged commit 50d35c1 into main Sep 5, 2026
7 of 8 checks passed
@solderzzc
solderzzc deleted the fix/turbokv-compressed-offset-reset branch September 5, 2026 19:59
solderzzc added a commit to SharpAI/SwiftLM that referenced this pull request Sep 5, 2026
Pulls in SharpAI/mlx-swift-lm#63: compressedOffset wasn't reset across
TurboKV eviction rounds, silently dropping whole 256-token windows from
compressed KV history on any request past ~2560 tokens with default
settings. See that PR for the full root-cause writeup and the added
regression test.

Co-authored-by: Claude Sonnet 5 <[email protected]>
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