Repository navigation
fix: TurboKV silently drops history after a second compression round - #63
Merged
Merged
Conversation
…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]>
3 tasks done
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]>
7 of 9 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
compressedOffsetkept accumulating across TurboKV eviction rounds (self.compressedOffset += newColdCount) even thoughself.keysgets 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 — thecoldEnd/newColdCountarithmetic looked plausible against the new (shorter) buffer, but pointed at the wrong tokens. The skipped window was never compressed intopolarKeysand 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.turboMinActivationTokens=2048,hotWindowSize=256,step=256), this triggers on any request past roughly 2560 tokens — a second eviction round.KVCacheSimple.state's getter, which usedcompressedOffset > 0as a proxy for "there is compressed history inpolarKeysto decode." That broke the momentcompressedOffsetstarted being correctly reset to 0 after each eviction.polarKeys/polarValuesbeing 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
KVCacheSimplethrough several eviction rounds with synthetic data surfaced a token-count mismatch (768 tokens back out ofstatefor 1024 fed in) before it ever got to comparing numbers.Test plan
testTurboKVMultiRoundEvictionPreservesAllTokens(Tests/MLXLMTests/KVCacheTests.swift) — feeds four step-sized (256-token) chunks through a realKVCacheSimplewith 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.KVCache.swiftfix (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 broadcastcrash downstream. Restoring the fix passes cleanly.MLXLMTestssuite (108 tests) passes with the fix applied — no regressions.🤖 Generated with Claude Code