Repository navigation
Fix TurboKV dropping compressed history from attention - #65
Conversation
Since #63, every TurboKV eviction in KVCacheSimple set `compressedOffset` to 0 and `offset` to the hot-window size. Two things broke once a prompt passed 2048 tokens: - AttentionUtils only prepends the decoded polar history when `compressedOffset > 0`, so attention saw only the ~256-token hot window. - `offset` restarted near 256, so RoPE positions and masks were wrong. `offset` now stays the logical token count and `compressedOffset` counts the tokens in polarKeys, as its doc comment says. Buffer indices are local (`offset - compressedOffset`). trim no longer cuts into compressed history, and toQuantized reads the full state. Qwen3.8-27B-4bit --turbo-kv, numbered-lines prompt, temperature 0: 315/500/700 lines went from "1,000"/"14"/"1,000" to correct. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Cross-review (M6 session, AI-assisted: Claude Code with the code-review skill; approved by the repo owner in conversation) Verdict: the change is correct and fixes ml-explore#175 (SharpAI/SwiftLM). One low-severity follow-up in a caller outside the diff. Fine to merge now and handle that separately. Checked
Low-severity follow-up, outside the diff:
|
| lines (tokens) | before | after |
|---|---|---|
| 315 (5,266) | 0/3: "There are 1,314 lines." | 3/3 |
| 500 (8,411) | 0/3: "There are 14 lines." | 3/3 |
| 700 (11,811) | 0/3: "There are 1,000 lines." | 3/3 |
Without --turbo-kv, both builds are 12/12. Cost with the fix: TurboKV requests are ~35% slower than without it at 11.8K tokens (97 s vs 72 s).
After a TurboKV eviction, `offset` counts all tokens, but the KVCacheSimple buffer only holds `offset - compressedOffset` of them. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks for the review. The Gemma4Text.swift:1384 point was introduced by this PR: |
Proposed changes
Since #63, once a
--turbo-kvprompt passes 2048 tokens, each eviction inKVCacheSimple.updatesetscompressedOffsetto 0 andoffsetto the hot-window size (256). Two things break:AttentionUtilsonly prepends the decoded polar history whencompressedOffset > 0, so attention sees only the last ~256 tokens.offsetrestarts near 256, so RoPE positions and masks are wrong.This PR keeps
offsetas the logical token count and makescompressedOffsetcount the tokens stored inpolarKeys, as its doc comment already says. Buffer indices become local (offset - compressedOffset).trimno longer cuts into compressed history, andtoQuantizedreads the full state.This fixes SharpAI/SwiftLM#175. The issue reproduces on both M5 and M6, so it is not chip-specific.
Test plan
testTurboKVAttentionSeesCompressedHistory: fails without the fix (offsetis 256, expected 1024), passes with itKVCacheTestspass, includingtestTurboKVMultiRoundEvictionPreservesAllTokens--vision --turbo-kv, temperature 0, numbered-lines prompt: 315 / 500 / 600 / 700 lines now answered correctly (before: "1,000" / "14" / "1,000" / "1,000")--ctx-size, 3 runs each: 315 / 500 / 700 lines go from 0/3 to 3/3. The wrong answers before the fix match the M5 exactly.Performance note
With this fix, TurboKV is correct but slower than running without it. It now attends over the full compressed history instead of only the last 256 tokens. M6 wall time per request, TurboKV on vs off: 39 vs 26 s (315 lines), 58 vs 47 s (500), 97 vs 72 s (700), about +35% at 11.8K tokens. Speeding up the history decode is left for a follow-up.
Checklist
pre-commit run --all-filesto format my code / installed pre-commit prior to committing changesAI usage
accurately describes the code changes.