Skip to content

fix: keep the hybrid prompt cache when --ctx-size is set - #207

Merged
solderzzc merged 1 commit into
SharpAI:mainfrom
CodeAndCanvas728:pr/ctx-size-hybrid-cache
Oct 5, 2026
Merged

solderzzc merged 1 commit into
SharpAI:mainfrom
CodeAndCanvas728:pr/ctx-size-hybrid-cache

Conversation

@CodeAndCanvas728

@CodeAndCanvas728 CodeAndCanvas728 commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Problem

On hybrid models (Qwen3.5/3.6), --ctx-size makes makeAttentionKVCache return a RotatingKVCache for the attention layers. hybridCacheBoundary only accepted KVCacheSimple, so it returned nil. The request then fell to the sliding-window path, whose restore misses on any MambaCache and whose save refuses recurrent state. Result: with --ctx-size, every agent turn re-prefilled the whole conversation, and nothing in the log said why.

Change

hybridCacheBoundary also accepts RotatingKVCache attention layers. This is safe because the hybrid path never trims: it restores the exact snapshot taken synchronously at the turn boundary, and a ring's state + metaState (keep, maxSize, offset, idx, wrapped) round-trip it, even once wrapped. #202 already detaches ring buffers on save/restore. The hybrid path already runs before the sliding-window one, so nothing else changes.

Tests

New in HybridPromptCacheTests: boundary accepts a rotating + Mamba cache; a wrapped ring is restored exactly after more decode on the live ring; a divergent prefix still misses. Full SwiftLMTests suite: 219 pass.

Live check

16 GB M2, Qwen3.5 MoE 4-bit (text-only via --no-vision, #206), --stream-experts --ssd-prefetch --prefill-size 2048 --ctx-size 32768, three agent turns sharing a prefix:

Turn Prompt tokens Before After
1 12,687 146.5 s 157.7 s (cold; run-to-run variance)
2 ~14.7k 166.6 s, miss 30.4 s, 12,680 reused
3 ~16.5k 189.0 s, miss 28.6 s, 14,645 reused

"Before" is the same rig and flags on main + #206. The "after" run used this branch + #206 (a local merge, not pushed); #206 is needed only because a VLM-capable checkpoint otherwise skips the cache.

🤖 Generated with Claude Code

…ers a RotatingKVCache

hybridCacheBoundary only accepted KVCacheSimple attention layers, so Qwen3.5/3.6
with --ctx-size (RotatingKVCache attention) silently lost the hybrid prompt cache
and re-prefilled every turn. The hybrid path never trims; it restores the exact
snapshot taken at the turn boundary, and a ring's state+metaState round-trip it
even after wrapping, so accepting RotatingKVCache is safe.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014J4GsqznbKNX8tcKzNRxEr
@solderzzc
solderzzc merged commit 89460d2 into SharpAI:main Oct 5, 2026
14 checks passed
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.

2 participants