Repository navigation
Conversation
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>
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
Notes