Add KV cache quantization to mlx_lm.server (#1043)#1476
Open
katlun-lgtm wants to merge 1 commit into
Open
Conversation
…ize/--quantized-kv-start) Wires the existing KV-cache quantization (already supported by generate/ stream_generate) into mlx_lm.server, closing ml-explore#1043. Quantized KV caches do not implement merge(), so they cannot participate in continuous batching; when --kv-bits is set, requests fall back to sequential generation, which applies the quantization.
Author
|
Verified end-to-end on M3 Max: |
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 to
mlx_lm.server(closes #1043)What
Exposes the KV-cache quantization already supported by
generate/stream_generateas server CLI flags:
--kv-bits(defaultNone→ off)--kv-group-size(default64)--quantized-kv-start(default0)When set, they are threaded into the server's sequential
stream_generatepath.Why the batched path is handled by fallback, not plumbing
The server uses continuous batching (
BatchGenerator) whenever a request_is_batchable— which requires mergeable caches(
all(hasattr(c, "merge") ...)).QuantizedKVCacheintentionally does notimplement
merge(), so a quantized cache cannot participate in continuousbatching without new batch-membership support for quantized caches (a larger,
separate feature).
Rather than silently ignore
--kv-bitsin the default (batched) path, this PRmakes
_is_batchablereturnFalsewhen--kv-bitsis set, so those requestsuse sequential generation, which applies the quantization. This keeps the
existing mergeable-cache invariant intact and makes the flag behave correctly.
Trade-off worth a maintainer call: with
--kv-bits, requests no longer batch.If you'd prefer, I can instead scope a follow-up that teaches the batched path
to carry quantized caches. Happy to go either way.
Changes
mlx_lm/server.py: 3 argparse flags; pass tostream_generate;_is_batchablereturns
Falsewhenkv_bitsis set.Test plan
Verified on Apple Silicon (M3 Max, mlx 0.31.2, mlx-lm 0.31.3) with
mlx-community/Qwen2.5-0.5B-Instruct-4bit:--kv-bitsflags parse with correct defaults (None/64/0).stream_generate(..., kv_bits=4)produces aQuantizedKVCacheand coherentoutput;
kv_bits=Nonestays a plainKVCache(regression check).mlx_lm.server --kv-bits 4(recommendedbefore merge; core generation path is exercised above).
Notes
Mirrors the existing
generate.pyflag names/help text verbatim for consistency.