feat(batching): batch-decode RoPE-correct cache infrastructure (A1)#65
Merged
Conversation
Foundation for continuous batching. Fixes the mlx-swift 0.31.6 scalar-offset RoPE batched-decode bug (uninitialized lanes 1..B-1; upstream ml-explore/mlx-swift#441, fixed in mlx-core 0.32.0 but not yet vendored) at the cache layer — no model edits. - BatchPositionedCacheWrapper: composition over a stock KVCache that forwards every requirement verbatim but overrides ropeOffset to .batch([offset x B]) (per-row array offset), routing decode RoPE onto the correct kernel path. Proven end-to-end on gemma-4: cross-row 12-19 -> 0.0, bit-identical tokens, throughput held at 3.6x. - batchPositioned([KVCache]) -> [KVCache]?: a SAFETY GATE — returns nil if any cache isn't a plain dense KVCacheSimple/RotatingKVCache (CacheList/QuantizedKVCache would fatalError when wrapped), so A2 refuses to batch an uncoverable model rather than emit garbage. - Honest coverage: MLXLLM dense-text models reading cache?.ropeOffset (Gemma/Gemma2/Gemma3Text/Gemma4Text/Qwen2/Qwen3/Llama); the six MLXVLM scalar-offset towers are NOT covered and documented. - Regression test asserts scalar RoPE -> NaN while .batch -> 0.0; a deletion tripwire that flips to FAILURE (pointing at #441) once mlx-swift ships the fix. Not wired into any generation path yet. Reviewed (approve-with-comments; both MEDIUM findings fixed). 7 tests incl. 3 refusal tests; xcodebuild + bare swift test green, CLI builds.
|
Bugbot is not enabled for your account, so this pull request was not reviewed. Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs. |
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.
Foundation (step 1) for continuous batching (v0.6 headline). Standalone infrastructure — not wired into any generation path yet; A2 (the BatchScheduler orchestrator) consumes it next.
The problem it solves
mlx-swift 0.31.6's fused RoPE kernel corrupts batched single-token decode (
[B,H,1,D], scalar offset, B>1): lanes 1..B-1 come out as uninitialized memory/NaN. Confirmed real (upstream ml-explore/mlx#3494/#3496, fixed in mlx-core 0.32.0 which mlx-swift hasn't vendored — I filed ml-explore/mlx-swift#441). Models read their RoPE offset fromcache.ropeOffset, so a cache returning.batch([B])(per-row array offset) routes onto the correct kernel. Proven end-to-end on gemma-4: cross-row 12–19 → exactly 0.0, bit-identical tokens across a B=4 identical batch, throughput held at 3.6×.What lands
BatchPositionedCacheWrapper— composition over a stockKVCache; forwards every requirement verbatim, overridesropeOffsetto.batch. Deletable once mlx-swift ships the fix.batchPositioned(_:) -> [KVCache]?— a safety gate, not a blind wrap: returns nil if any cache isn't a plain denseKVCacheSimple/RotatingKVCache(CacheList/QuantizedKVCachewouldfatalErrorif wrapped), so A2's scheduler refuses to batch an uncoverable model rather than emit garbage.cache?.ropeOffset(Gemma/Gemma2/Gemma3Text/Gemma4Text/Qwen2/Qwen3/Llama); the six MLXVLM towers applying RoPE from the scalarcache.offset(Paligemma, Gemma3/Gemma4 VLM, Pixtral, Mistral3, LFM2VL) are not covered and documented as such..batch→ 0.0; it flips to FAILURE (with a message pointing at #441) once mlx-swift ships the fix, telling us to delete the wrapper.Review + verification
Adversarially reviewed (approve-with-comments) — the review caught two MEDIUM safety issues (a false 'model-agnostic' coverage claim, and a blind-wrap factory that would
fatalErroron hybrid/quantized caches); both fixed (honest scope + the[KVCache]?safety gate + 3 refusal tests). No force-unwrap/try!/as!. Independently verified: xcodebuild** TEST SUCCEEDED **, bareswift test244 green (0 MLX errors, MLX-gated asserts skip), CLI builds. 7 tests.