Skip to content

feat(#33): experimental Quest block-sparse decode attention behind serve --quest-top-k - #281

Merged
goodmai merged 1 commit into
mainfrom
feat/33-quest
Nov 4, 2026
Merged

goodmai merged 1 commit into
mainfrom
feat/33-quest

Conversation

@goodmai

@goodmai goodmai commented Nov 4, 2026

Copy link
Copy Markdown
Owner

Part of #33 (Quest). The rest of #33 shipped in #276. The ≥ 2× attention target moves to #280.

What

basalt serve --quest-top-k N (also in PerformanceBenchmark). It is experimental.

  • Decode steps. Each full-attention layer attends to 128 sink tokens, the last 256 or more tokens, and the N best 64-token blocks between them.
  • Block choice. Blocks are picked per KV head by Σ_d max(q·max, q·min) over per-block key bounds, summed over the GQA group.
  • Prefill stays dense.
  • Where it applies. QuestKVCache replaces the KVCacheSimple layers only. Hybrid models keep their recurrent layers.
  • Refused models. At load, a one-token probe checks that the model attends through KVCacheAttentionProtocol.updateAndAttend. Gemma 4 reads the whole cache through cache.update, so it is refused with a warning and stays dense.
  • Not combined with --kv-bits, --kv-zones, --kv-head-split or prompt-cache namespaces.

Found while measuring: the block bounds have to be evaluated with the cache (innerState). Without that, a 30k prefill keeps every intermediate key buffer alive and runs out of Metal memory.

Measured

Apple M4 Pro, 24 GB.

  • Passkey: commit 15ea195 for dense, then the Quest modes rerun at b6b159d. b6b159d is the tree of this PR.
  • Speed: b6b159d. Gemma rows are from 15ea195; the refused path is unchanged since then.
  • Logs: logs/quest-passkey.log, logs/quest-passkey-2.log, logs/quest-passkey-gemma.log.

Passkey

Greedy, corpus37, 5 depths (0.1, 0.3, 0.5, 0.7, 0.9).

Model Length dense quest-16 quest-32
Qwen3-4B-4bit 16k 5/5 5/5 5/5
Qwen3-4B-4bit 32k 5/5 5/5 5/5
Ornith-1.5-9B-MLX-4bit (hybrid) 16k 4/5 5/5 4/5
Ornith-1.5-9B-MLX-4bit (hybrid) 32k 5/5 2/5 4/5
gemma-4-12B-it-qat-4bit 16k, depth 0.5 1/1 — 1/1 (refused, ran dense)
  • Ornith's misses are near misses: 21194 for 21196, 28582 for 28596. Dense also misses once (21147 for 21148).
  • On the hybrid, quest-32 is 8/10 against dense 9/10, so the passkey criterion is met on Qwen3-4B but not quite on Ornith.
  • 32-token blocks did worse on Ornith at 32k (quest-16 0/5, quest-32 4/5), so blocks stay at 64.

Decode speed

Release build, ~30k-token prompt (120,000 characters of corpus37), 256 tokens, T=0.7, seed 7.

Model dense tok/s quest-16 quest-32
Qwen3-4B-4bit 31.2 37.4 (1.20×) 34.0 (1.09×)
Ornith-1.5-9B-MLX-4bit 37.8 40.5 (1.07×) 39.6 (1.05×)
gemma-4-26B-A4B-it-qat-4bit 22.48 22.48 (refused) 22.50 (refused)

Checks

  • swift build -Xswiftc -warnings-as-errors (debug and release) passes.
  • swift test --parallel: 1243 tests, all pass.
  • check_no_new_comments.py: no comments added.
  • TSan on QuestKVCacheTests reports only MLX-internal allocator and scheduler races, not code in this PR.
  • Unit tests cover:
    • a decode step stays dense while the middle has no more than top-k blocks;
    • the block that holds the needle is picked and the output matches dense;
    • multi-token steps stay dense, the bounds are evaluated with the cache, and trimming drops stale bounds;
    • Qwen3 and Qwen3.5 are supported and Gemma 4 is refused;
    • a tiny Qwen3 matches dense decoding.

🤖 Generated with Claude Code

https://claude.ai/code/session_012cicgudpVmSKaQgaioocQ1

…rve --quest-top-k

On a decode step, a full-attention layer attends to 128 sink tokens, the
last 256 or more, and the top-k 64-token blocks between them, picked per KV
head from per-block key maxima and minima. Prefill stays dense. Models whose
attention reads the whole cache through cache.update (Gemma 4) are refused
at load with a warning and stay dense.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012cicgudpVmSKaQgaioocQ1
@goodmai
goodmai merged commit 289ea74 into main Nov 4, 2026
4 checks passed
@goodmai

goodmai commented Nov 4, 2026

Copy link
Copy Markdown
Owner Author

Passkey added after the merge on the MoE models, at commit c8abc66 (same tree as 289ea74). Log: logs/quest-passkey-moe.log. The Gemma run was stopped early at 9 of 12 cases.

Model Length dense quest-16 quest-32
LFM2.5-8B-A1B-MLX-4bit (hybrid MoE) 16k, 5 depths 5/5 4/5 5/5
LFM2.5-8B-A1B-MLX-4bit (hybrid MoE) 32k, 5 depths 4/5 3/5 4/5
gemma-4-26B-A4B-it-qat-4bit (MoE) 16k, depths 0.1/0.5/0.9 3/3 — 3/3 (refused, ran dense)
gemma-4-26B-A4B-it-qat-4bit (MoE) 32k, depths 0.1/0.5 2/2 — 1/1 (refused, ran dense)
  • At 32k, depth 0.1, LFM2.5 misses in all three modes: it answers with words instead of the key.
  • As on Ornith, quest-32 matches dense on the hybrid and quest-16 loses one or two cases per length.

🤖 Generated with Claude Code

https://claude.ai/code/session_012cicgudpVmSKaQgaioocQ1

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