Skip to content

feat(batched-kv): turbo storage variant for batched decode - #203

Draft
TheTom wants to merge 2 commits into
ekryski:alphafrom
TheTom:codex/batched-turbo-kv-storage
Draft

TheTom wants to merge 2 commits into
ekryski:alphafrom
TheTom:codex/batched-turbo-kv-storage

Conversation

@TheTom

@TheTom TheTom commented May 13, 2026

Copy link
Copy Markdown

Summary

  • add a TurboQuant-backed storage variant for batched KV caches
  • wire batched cache allocation through Qwen3/Qwen3.5/Qwen3Next paths
  • add BatchedTurboKVCache and parseTurboScheme regression coverage

Notes

  • draft PR split from the alpha integration stack
  • based on fresh ekryski:alpha

TheTom and others added 2 commits May 12, 2026 21:27
Closes a silent-bypass bug: vllm-swift's `--additional-config kv_scheme=
turbo*` flag was dropped on the floor whenever `BatchedKVCache` was the
active cache (the Qwen3 dense + Qwen3.5/3.6 + Qwen3Next hybrid paths).
The cache only knew how to store raw fp16/bf16 K/V, so the user's turbo
scheme silently turned into raw fp16. Buddy's v0.5.1 alpha report —
Qwen3.6-35B-A3B + turbo4v2 + "Say hello" producing `Hello.` then
`.2.2.2.2...` for thousands of tokens until cap — was a symptom of that
bypass interacting with the v0.5.1 max_tokens-bump and 4-bit weights.

`BatchedKVCache` now has an internal `.raw` / `.turbo` storage variant.
The turbo path:
  * Allocates packed-byte K/V (or raw-K + packed V when keyBits == 0)
    plus per-token L2 norms, sized [maxBatch, kvHeads, maxSeq, packedW]
  * Encodes new tokens via the existing batch-parallel `fusedEncode`
    kernels — flatten [B, kvH, D] → [B*kvH, D], encode, reshape back
  * Runs decode-time attention via a new `attention(queries:scale:mask:)`
    method that mirrors `TurboQuantKVCache`'s dequant-first SDPA: pre-
    rotates queries (with scale folded into rotation), bulk-dequants
    via existing `bulkDequantRotated` kernel into rotated codec space,
    runs MLXFast SDPA in rotated space (preserved under orthogonal
    rotation: (RQ)·(RK)ᵀ = QKᵀ), applies inverse rotation to output

Migrated 3 callers (Qwen3 / Qwen3.5 / Qwen3Next `fullyBatchedForward`)
to call `cache.attention(...)` instead of inline slicing of
`cache.keys`/`cache.values` + `MLXFast.scaledDotProductAttention`. The
default raw-mode `attention()` impl is bit-identical to the inline
pattern those models had, so existing batched-hybrid + Qwen35 lockstep
tests stay green (20/20 pass) — pure refactor first, additive turbo
storage second.

`BatchedHybridLLM.newBatchedHybridCache` gains optional
`turboKeyBits`/`turboValueBits` parameters; Qwen35 and Qwen3Next thread
them into the per-layer attention cache factory. A protocol-level
default-impl shim preserves the pre-turbo two-arg signature so existing
callers (and tests) keep working unchanged.

Tests:
  * 20/20 BatchedHybridCache + Qwen35 lockstep + fullyBatchedDecode
    slot-equivalence tests pass on the raw refactor
  * 7 new BatchedTurboKVCache tests cover construction, raw/turbo flag
    reporting, raw-key-mode (V-only compression), update slot routing,
    and dequant-first attention shape + non-NaN output through the real
    Metal kernel chain
  * Total new coverage exercises both the (kb=4, vb=4) and the
    (kb=0, vb=4) raw-K codepaths

Note for vllm-swift: Qwen3 dense path's prefill→batched K/V copy in
Bridge.swift still drops kvScheme — TODO v0.5.4 needs a bulk-encode
pass over the prefilled fp16 K/V into the turbo cache. Hybrid path
(Qwen3.5/3.6 + Qwen3Next) gets the full fix in v0.5.3.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The parser is the single source of truth for "turboNvM" → (keyBits,
valueBits) — every CLI / config flag funnels through here, so an off-
by-one in the parse rule would silently flip K and V across the whole
stack. Buddy's v0.5.1 alpha repro was a downstream symptom of a
DIFFERENT bug (BatchedKVCache silently dropping the parsed scheme),
but the parser itself is the chokepoint that "turboNvM should mean
keyBits=N, valueBits=M" must hold for. Now locked down.

9 tests cover the symmetric (turbo3/turbo4/turbo8), asymmetric
(turbo4v2/turbo8v4/turbo3v2/turbo4v3), raw-key (turbo0v4), and
asymmetric-equals-symmetric (turbo4 == turbo4v4 downstream) cases.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@ekryski ekryski added the feature New feature (auto-applied from feat: prefix) label May 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature (auto-applied from feat: prefix)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants