Repository navigation
add kvScheme parameter for extensible KV cache compression - #230
Merged
Merged
Conversation
TheTom
force-pushed
the
pr/upstream-kv-scheme
branch
from
April 21, 2026 18:07
5e857be to
122300d
Compare
davidkoski
reviewed
Jun 11, 2026
Comment on lines
+1779
to
+1781
| /// Resolve a kvScheme string to (bits, groupSize) for affine quantization. | ||
| /// Returns nil for unrecognized schemes (custom schemes handle their own caches). | ||
| public func resolveAffineScheme(_ scheme: String?) -> (bits: Int, groupSize: Int)? { |
Member
There was a problem hiding this comment.
I think this is getting concatenated with the docs for the maybeQuantizeKVCache below (the docs checker is failing). Also need kvScheme added to the docs.
Member
|
When you get a chance please rebase -- this will fix the merge conflict and fix the CI issue so tests can run. Thank you! |
Adds kvScheme to GenerateParameters alongside existing kvBits. Provides a string-based scheme selector for KV cache compression strategies. Built-in: "affine4", "affine8" (equivalent to kvBits 4/8). kvScheme overrides kvBits when set. Unrecognized schemes pass through for custom KVCache implementations. Plumbing only, no new compression algorithms. Prepares the API for WHT-based and other non-affine KV compression schemes.
TheTom
force-pushed
the
pr/upstream-kv-scheme
branch
from
June 11, 2026 20:57
122300d to
4e799a4
Compare
Contributor
Author
|
Rebased onto main and pushed — the doc-comment concatenation is fixed ( |
4 tasks done
davidkoski
approved these changes
Jun 22, 2026
davidkoski
left a comment
Member
There was a problem hiding this comment.
Thank you for the contribution!
somegeekintn
added a commit
to somegeekintn/mlx-swift-lm
that referenced
this pull request
Jun 28, 2026
TheTom
added a commit
to TheTom/mlx-swift-lm
that referenced
this pull request
Jul 16, 2026
Walsh-Hadamard rotation + Lloyd-Max codebook KV compression behind the kvScheme parameter (ml-explore#230), with JIT Metal kernels: fused encode, a SIMD-parallel two-pass online-softmax flash decode (packed-K, raw-fp16-K, and 8-bit-affine-K score variants), and an inline sparse value kernel. Schemes are asymmetric because keys are far more quality-sensitive than values (softmax amplifies key error, value error averages out): the recommended tiers keep K at fp16 (turbo0v4/v3/v2) or 8-bit affine (turbo8v4/v3/v2) while V takes the compression; symmetric turbo4/3/2 are available for validated use. Boundary-layer protection converts the first and last attention layers to 8-bit affine for fragile schemes. Per-call kernel values travel in a parameter buffer rather than template arguments, so each shape compiles once. Caches stay raw fp16 through prefill, compress on the first decode step, and encode incrementally; state round-trips through prompt cache save/restore, and speculative decoding routes through the compressed path. Generation settles cache state with the token eval and drains autoreleased temporaries per step so long generations hold steady memory.
TheTom
added a commit
to TheTom/mlx-swift-lm
that referenced
this pull request
Jul 16, 2026
Walsh-Hadamard rotation + Lloyd-Max codebook KV compression behind the kvScheme parameter (ml-explore#230), with JIT Metal kernels: fused encode, a SIMD-parallel two-pass online-softmax flash decode (packed-K, raw-fp16-K, and 8-bit-affine-K score variants), and an inline sparse value kernel. Schemes are asymmetric because keys are far more quality-sensitive than values (softmax amplifies key error, value error averages out): the recommended tiers keep K at fp16 (turbo0v4/v3/v2) or 8-bit affine (turbo8v4/v3/v2) while V takes the compression; symmetric turbo4/3/2 compress both sides. Symmetric key quality rides on per-dimension key calibration: at compression time the cache derives a per-dimension scale from the post-rotation key variance of the prefill cache and folds it into the query side, leaving the score kernels untouched; the calibrated encode rotates through the codec's MLX ops so kernel and reference quantize identical values. Boundary-layer protection converts the first and last attention layers to 8-bit affine for fragile schemes. Rotating (sliding-window) cache layers stay fp16 with a one-time notice; global layers on mixed models compress normally. Per-call kernel values travel in a parameter buffer rather than template arguments, so each shape compiles once. Caches stay raw fp16 through prefill, compress on the first decode step, and encode incrementally; state round-trips through prompt cache save/restore, and speculative decoding routes through the compressed path. Generation settles cache state with the token eval and drains autoreleased temporaries per step so long generations hold steady memory.
TheTom
added a commit
to TheTom/mlx-swift-lm
that referenced
this pull request
Jul 16, 2026
Walsh-Hadamard rotation + Lloyd-Max codebook KV compression behind the kvScheme parameter (ml-explore#230), with JIT Metal kernels: fused encode, a SIMD-parallel two-pass online-softmax flash decode (packed-K, raw-fp16-K, and 8-bit-affine-K score variants), and an inline sparse value kernel. Schemes are asymmetric because keys are far more quality-sensitive than values (softmax amplifies key error, value error averages out): the recommended tiers keep K at fp16 (turbo0v4/v3/v2) or 8-bit affine (turbo8v4/v3/v2) while V takes the compression; symmetric turbo4/3/2 compress both sides. Symmetric key quality rides on per-dimension key calibration: at compression time the cache derives a per-dimension scale from the post-rotation key variance of the prefill cache and folds it into the query side, leaving the score kernels untouched; the calibrated encode rotates through the codec's MLX ops so kernel and reference quantize identical values. Boundary-layer protection converts the first and last attention layers to 8-bit affine for fragile schemes. Rotating (sliding-window) cache layers stay fp16 with a one-time notice; global layers on mixed models compress normally. Per-call kernel values travel in a parameter buffer rather than template arguments, so each shape compiles once. Caches stay raw fp16 through prefill, compress on the first decode step, and encode incrementally; state round-trips through prompt cache save/restore, and speculative decoding routes through the compressed path. Generation settles cache state with the token eval and drains autoreleased temporaries per step so long generations hold steady memory.
davidkoski
pushed a commit
that referenced
this pull request
Jul 20, 2026
* Add TurboQuant KV cache compression Walsh-Hadamard rotation + Lloyd-Max codebook KV compression behind the kvScheme parameter (#230), with JIT Metal kernels: fused encode, a SIMD-parallel two-pass online-softmax flash decode (packed-K, raw-fp16-K, and 8-bit-affine-K score variants), and an inline sparse value kernel. Schemes are asymmetric because keys are far more quality-sensitive than values (softmax amplifies key error, value error averages out): the recommended tiers keep K at fp16 (turbo0v4/v3/v2) or 8-bit affine (turbo8v4/v3/v2) while V takes the compression; symmetric turbo4/3/2 compress both sides. Symmetric key quality rides on per-dimension key calibration: at compression time the cache derives a per-dimension scale from the post-rotation key variance of the prefill cache and folds it into the query side, leaving the score kernels untouched; the calibrated encode rotates through the codec's MLX ops so kernel and reference quantize identical values. Boundary-layer protection converts the first and last attention layers to 8-bit affine for fragile schemes. Rotating (sliding-window) cache layers stay fp16 with a one-time notice; global layers on mixed models compress normally. Per-call kernel values travel in a parameter buffer rather than template arguments, so each shape compiles once. Caches stay raw fp16 through prefill, compress on the first decode step, and encode incrementally; state round-trips through prompt cache save/restore, and speculative decoding routes through the compressed path. Generation settles cache state with the token eval and drains autoreleased temporaries per step so long generations hold steady memory. * Add TurboQuant tests Codec round trips and pinned codebooks, rotation orthogonality, packing, kernel-vs-reference attention parity (GQA repeat factors 1/2/3, non-power-of-two head dims), calibrated-encode kernel exact index parity, prompt-cache round trips for all state layouts including calibration state, compressed partial trim with continued decode, speculative decoding with a turbo scheme, allocation-boundary growth, boundary-protection gating, mixed rotating/standard cache conversion, and end-to-end generation on tiny random-weight models. CI-runnable, no checkpoint downloads. * Add KV cache quantization documentation Scheme reference table, the asymmetric-first selection ladder, measured family sensitivity and calibration notes, performance guidance, and limitations.
dkgv
pushed a commit
to dkgv/mlx-swift-lm
that referenced
this pull request
Jul 30, 2026
…e#230) Adds kvScheme to GenerateParameters alongside existing kvBits. Provides a string-based scheme selector for KV cache compression strategies. Built-in: "affine4", "affine8" (equivalent to kvBits 4/8). kvScheme overrides kvBits when set. Unrecognized schemes pass through for custom KVCache implementations. Plumbing only, no new compression algorithms. Prepares the API for WHT-based and other non-affine KV compression schemes.
dkgv
pushed a commit
to dkgv/mlx-swift-lm
that referenced
this pull request
Jul 30, 2026
* Add TurboQuant KV cache compression Walsh-Hadamard rotation + Lloyd-Max codebook KV compression behind the kvScheme parameter (ml-explore#230), with JIT Metal kernels: fused encode, a SIMD-parallel two-pass online-softmax flash decode (packed-K, raw-fp16-K, and 8-bit-affine-K score variants), and an inline sparse value kernel. Schemes are asymmetric because keys are far more quality-sensitive than values (softmax amplifies key error, value error averages out): the recommended tiers keep K at fp16 (turbo0v4/v3/v2) or 8-bit affine (turbo8v4/v3/v2) while V takes the compression; symmetric turbo4/3/2 compress both sides. Symmetric key quality rides on per-dimension key calibration: at compression time the cache derives a per-dimension scale from the post-rotation key variance of the prefill cache and folds it into the query side, leaving the score kernels untouched; the calibrated encode rotates through the codec's MLX ops so kernel and reference quantize identical values. Boundary-layer protection converts the first and last attention layers to 8-bit affine for fragile schemes. Rotating (sliding-window) cache layers stay fp16 with a one-time notice; global layers on mixed models compress normally. Per-call kernel values travel in a parameter buffer rather than template arguments, so each shape compiles once. Caches stay raw fp16 through prefill, compress on the first decode step, and encode incrementally; state round-trips through prompt cache save/restore, and speculative decoding routes through the compressed path. Generation settles cache state with the token eval and drains autoreleased temporaries per step so long generations hold steady memory. * Add TurboQuant tests Codec round trips and pinned codebooks, rotation orthogonality, packing, kernel-vs-reference attention parity (GQA repeat factors 1/2/3, non-power-of-two head dims), calibrated-encode kernel exact index parity, prompt-cache round trips for all state layouts including calibration state, compressed partial trim with continued decode, speculative decoding with a turbo scheme, allocation-boundary growth, boundary-protection gating, mixed rotating/standard cache conversion, and end-to-end generation on tiny random-weight models. CI-runnable, no checkpoint downloads. * Add KV cache quantization documentation Scheme reference table, the asymmetric-first selection ladder, measured family sensitivity and calibration notes, performance guidance, and limitations.
davidtai
added a commit
to davidtai/mlx-swift-lm
that referenced
this pull request
Oct 6, 2026
…l-explore#230) Integrate Deepseek V4 fp8 scale-key cleanup and enable strict checkpoint-loading regression assertions without reducing coverage.
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.
Proposed changes
Adds
kvSchemetoGenerateParametersalongside existingkvBits. Providesa string-based scheme selector for KV cache compression strategies.
Built-in:
"affine4","affine8"(equivalent tokvBits4/8).kvSchemeoverrideskvBitswhen set. Unrecognized schemes pass throughfor custom
KVCacheimplementations.Fully backward compatible.
kvSchemedefaults tonil, existingkvBitsbehavior unchanged.
Plumbing only, no new compression algorithms. Prepares the API for
WHT-based and other non-affine KV compression schemes.
Checklist
pre-commit run --all-filesto format my code