server: add KV cache quantization flags (--kv-bits, --kv-group-size, --quantized-kv-start)#1353
Open
soobrosa wants to merge 3 commits into
Open
server: add KV cache quantization flags (--kv-bits, --kv-group-size, --quantized-kv-start)#1353soobrosa wants to merge 3 commits into
soobrosa wants to merge 3 commits into
Conversation
soobrosa
pushed a commit
to soobrosa/dataroom
that referenced
this pull request
Jun 7, 2026
Detects --kv-bits in mlx_lm.server (ml-explore/mlx-lm#1353); when present, serves with --kv-bits 4 --kv-group-size 64 and raises MLX_CTX_CAP 75K->85K (verified greedy-lossless + long-ctx recall). Falls back to fp16 KV + 75K cap on stock mlx-lm, so the change is safe before #1353 ships. Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
Wire KV cache quantization (already supported by generate_step) into the server, addressing ml-explore#1043. Because the batched path's BatchKVCache has no quantized variant yet, enabling --kv-bits routes requests through the single-sequence stream_generate path, which does support quantized KV. Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
Wire make_prompt_cache(max_kv_size=...) through ModelProvider so the server can bound the per-request KV cache (RotatingKVCache eviction) for standard models. Folds in the --max-kv-size flag from the closed PR ml-explore#1362 (thanks @0xSoftBoi), making this PR a superset. Note: models providing their own make_cache (some hybrid architectures) ignore max_kv_size by design. Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
290c282 to
059a29c
Compare
|
I noticed an issue with this PR, where
I believe that the Gemma 4 model architecture uses a hybrid cache strategy, ie; some layers use regular KVCache() (full attention), and some layers use RotatingKVCache() with a sliding window. |
Author
|
@sunpazed this looks better to me now |
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.
Add KV cache quantization flags to
mlx_lm.serverCloses #1043 (see also #1308).
mlx_lm.generatehas supported quantized KV cache for a while, butmlx_lm.serverdidnot expose it, so long-context serving was stuck at fp16 KV — which OOMs well before the
model's real context limit on memory-bound Apple Silicon.
What this does
Adds three CLI flags to
mlx_lm.server:--kv-bits(defaultNone→ no quantization)--kv-group-size(default64)--quantized-kv-start(default5000)These are threaded straight into the existing
stream_generatepath, reusing the samequantized-KV implementation
mlx_lm.generatealready uses.Design note (batched path)
BatchKVCachehas no quantized variant yet, so when--kv-bitsis set,_is_batchable()returns
Falseand requests are served through the single-sequence generator (which doessupport quantized KV). This trades batched throughput for correctness when quantization is
explicitly requested, and is forward-compatible with the batched-quantized work in #1322.
Validation
Tested against Qwen3.6-35B-A3B (hybrid GDN + MoE, MLX 4-bit) served with
--kv-bits 4 --kv-group-size 64:filler — past
quantized_kv_start) returned the correct answer, no crash on the hybridcache layout.
vs #1309
This PR additionally exposes
--kv-group-size/--quantized-kv-startand routes throughthe proven
stream_generatepath rather than building caches manually.