mlx-quant-fidelity
Measure how much quality a quantization costs on Apple Silicon. mlx-quant-fidelity scores a quantized model against a higher-precision reference on the same corpus and reports the drift as numbers you can act on: KL divergence, top-token flip rate, perplexity delta. It measures both KV-cache quantization and weight quantization. No more choosing a bit-width by file size.
The CUDA/GGUF world has had this for years: llama.cpp's --kl-divergence-base, EleutherAI's lm-evaluation-harness. MLX had nothing. This is the MLX version, and it covers the KV-cache and attention angle those tools skip.
Install
pip install mlx-quant-fidelity
Apple Silicon (MLX), Python 3.11+.
Use it
mlx-quant-fidelity kv mlx-community/Llama-3.2-3B-Instruct-4bit --kv-bits 8
Prints a Markdown report. Add --format json for JSON, --format badge for a shields.io badge line, --kv-bits 4, --kv-group-size 64, --max-chunks N to bound the corpus, or --chunk-length N (up to 4096, default 512) for a longer window and a depth-resolved drift table.
from mlx_quant_fidelity import measure_kv_fidelity
report = measure_kv_fidelity("mlx-community/Llama-3.2-3B-Instruct-4bit", kv_bits=8)
print(report.kl.mean, report.flip_rate, report.verdict)
Or measure weight quantization — a quantized repo against a higher-precision reference:
mlx-quant-fidelity weights mlx-community/Llama-3.2-3B-Instruct-4bit --reference mlx-community/Llama-3.2-3B-Instruct-bf16
from mlx_quant_fidelity import measure_weight_fidelity
# measure_weight_fidelity(quantized_repo, reference_repo)
report = measure_weight_fidelity(
"mlx-community/Llama-3.2-3B-Instruct-4bit", # quantized
"mlx-community/Llama-3.2-3B-Instruct-bf16", # reference
)
print(report.kl.mean, report.flip_rate, report.verdict)
What a report looks like
# KV-fidelity: `mlx-community/Llama-3.2-3B-Instruct-4bit` @ 8-bit (group 64)
**Verdict:** good · **mode:** stress (quantize_start=0)
| metric | value |
|---|---|
| KL mean | 0.0002 nats |
| KL median | 0.0001 nats |
| KL p99 | 0.0015 nats |
| KL max | 0.1129 nats |
| flip rate | 0.0065 |
| perplexity Δ | +0.0054 (17.722 → 17.728) |
Measured on **wikitext-2-raw/test**, 51100 positions across 100 chunks of length 512 ...
Badge output
--format badge prints a single shields.io Markdown line instead of the full report:
mlx-quant-fidelity kv mlx-community/Llama-3.2-3B-Instruct-4bit --kv-bits 8 --format badge
Output:

Green for good, yellow for marginal, red for bad. The badge message includes the bit width, corpus, chunk length, and mode so badges from different configurations are distinguishable. Threshold values and the color map are in docs/threshold-policy.md.
How much does KV quantization cost?
M1 Max, WikiText-2 test (100 chunks of 512 tokens), stress mode (quantize from token 0). Reproduce any row with mlx-quant-fidelity kv <model> --kv-bits <bits> --max-chunks 100; the full committed reports are under _artifacts/samples/.
| Model | KV bits | KL mean (nats) | flip rate | verdict |
|---|---|---|---|---|
| Llama-3.2-1B | 4 | 0.148 | 0.20 | bad |
| Llama-3.2-1B | 8 | 0.0004 | 0.013 | marginal |
| Llama-3.2-3B | 4 | 0.051 | 0.11 | bad |
| Llama-3.2-3B | 8 | 0.0002 | 0.007 | good |
| Qwen2.5-7B | 4 | 9.36 | 0.99 | bad |
| Qwen2.5-7B | 8 | 0.009 | 0.032 | marginal |
8-bit KV is near-lossless on all three models. 4-bit is another matter, and Qwen2.5-7B at 4-bit in stress mode falls apart: nearly every token flips. This measurement establishes a checkpoint-specific failure, not its cause. mlx-lm's default delays cache conversion until 5000 tokens, so those positions are computed while attention uses a full-precision cache. At the boundary, however, mlx-lm converts the entire stored prefix too. Run the tool first and you see the fidelity risk before deployment.
Does drift change with position depth?
--chunk-length 4096 widens the window and adds a table breaking mean and p99 KLD down by position depth within a chunk. Llama-3.2-1B at 4-bit KV, M1 Max, WikiText-2 test (12 chunks of 4096 tokens, the same ~50k-token corpus coverage as the 512-token samples above):
| positions | KL mean | KL p99 |
|---|---|---|
| 0-510 | 0.1485 | 0.9470 |
| 511-1022 | 0.1455 | 0.8659 |
| 1023-1534 | 0.1534 | 0.9329 |
| 1535-2046 | 0.1479 | 0.9568 |
| 2047-2558 | 0.1439 | 0.9048 |
| 2559-3070 | 0.1572 | 0.9835 |
| 3071-3582 | 0.1537 | 0.9757 |
| 3583-4094 | 0.1554 | 1.0237 |
On this model and corpus, drift at position 4000 looks about the same as drift at position 60 — quantization cost isn't building up across the window at these lengths. That's a narrower claim than it might sound: 4096 tokens is short next to the context lengths where other work has found KV-quantization drift growing with depth. docs/measurement-principles.md covers the measured memory cost of longer windows and why the comparison to longer-context findings elsewhere isn't apples to apples. The full report, including the 8-bit KV counterpart, is under _artifacts/samples/ (llama-3.2-1b-4bit-kv4-cl4096.md, llama-3.2-1b-4bit-kv8-cl4096.md).
How much does weight quantization cost?
Same corpus and recipe, but the comparison is now a quantized model repo against a higher-precision reference repo. Reproduce any row with mlx-quant-fidelity weights <quant> --reference <reference> --max-chunks 100; the committed reports are under _artifacts/samples/weights/.
| Model | quant | reference | KL mean (nats) | flip rate | perplexity Δ | verdict |
|---|---|---|---|---|---|---|
| Llama-3.2-1B | 4-bit | bf16 | 0.158 | 0.21 | +3.5 | marginal |
| Llama-3.2-1B | 8-bit | bf16 | 0.001 | 0.023 | −0.01 | good |
| Llama-3.2-3B | 4-bit | bf16 | 0.085 | 0.15 | +1.4 | marginal |
| Llama-3.2-3B | 8-bit | bf16 | 0.0009 | 0.021 | 0.00 | good |
| Qwen2.5-7B | 4-bit | 8-bit | 0.109 | 0.16 | +0.9 | marginal |
8-bit weights are near-lossless: about 2% of top tokens flip and perplexity barely moves. 4-bit is a real trade: 15 to 21% of top tokens flip and perplexity climbs a point or more, worst on the small 1B model. The Qwen row compares 4-bit against 8-bit rather than bf16, so its drift is relative to an already-quantized reference, not full precision; the report records that the reference is 8-bit and says so in plain text. The verdict tiers are provisional, anchored to these q8 and q4 reference points on short prose rather than to downstream task accuracy.
Unlike the KV probe, both runs use standard attention, so the drift is the deployed quantized model's weight-quant cost with no quantized-attention kernel folded in. It does still include the quantized-matmul kernel's numerics, which is exactly what you run when you load the model.
Comparing quantizations
compare ranks a set of quantizations on a memory-normalized Pareto frontier: quality (mean KL divergence) on one axis, memory cost on the other. It identifies any configuration that is both worse quality and more expensive than another option on the list — those are dominated and you would never choose them.
# rank weight quantizations against a bf16 reference
mlx-quant-fidelity compare weights q4 q6 q8 --reference fp16
# rank KV configs on a single model
mlx-quant-fidelity compare kv <model> --configs 4:32,4:64,8:64
# or auto-generate the grid from the model's config.json instead of listing configs by hand
mlx-quant-fidelity compare kv <model> --sweep --max-kv-bytes-per-token 200
Add --max-kld 0.05 to get the cheapest configuration whose mean KLD stays under a threshold, or --min-tier good to get the cheapest one that passes the good-tier verdict. --sweep builds the (bits × group-size) grid from the model's config alone, no weight download needed, and drops any combination that would crash the upstream KV cache implementation; --max-kv-bytes-per-token narrows that grid to configurations under a memory budget. Either way, skipped configurations are listed in the report rather than silently dropped. docs/ranking-principles.md explains how each axis is computed, what Pareto domination means in practice, and where the ranking has limits.
How it works
Teacher-forced scoring, not generation. For each fixed-length corpus chunk the model runs twice on the same tokens — once with a full-precision KV cache, once with a quantized one — and the two next-token distributions are compared position by position. Generation would let the runs diverge in their own inputs the moment quantization changed a sampled token, turning the measurement into trajectory drift instead of cache cost. Logits collapse to per-position scalars inside the chunk loop and are released before the next chunk, so a long corpus never holds full distributions in memory.
Two modes:
- stress (
--quantize-start 0, the default): quantize from token 0. The harsh, apples-to-apples quantizer test. - deployment (
--quantize-start N): computes the first N positions with a full-precision cache, then converts the entire stored cache and scores only the post-boundary region. This matches mlx-lm's--quantized-kv-startconversion behavior; it does not preserve a full-precision prefix in storage. docs/measurement-principles.md explains why deployment and stress drift need a matched comparison and why neither is a long-context deployment average.
A run that returns exactly zero drift raises instead of reporting a silent "perfect fidelity." That almost always means quantization never engaged, not that it was free.
The weight probe works the same way with two models instead of two caches: a quantized repo and a reference repo, scored on the same corpus tokens. A compatibility gate refuses a mismatched pair before loading, and a memory pre-flight refuses a pair too large for the device rather than risking a kernel panic.
See docs/measurement-principles.md for the zero-probability policy, the exact-zero guard, and how perplexity delta relates to mean KLD.
What the numbers don't say
- A fidelity number is corpus- and context-length-specific. WikiText-2 at temperature 0 measures short-prose distributional drift; the paper this builds on, Accuracy Is Not All You Need, shows that under-predicts task-specific and long-context degradation. Every report records the corpus and the token count so the number is never read as a bare score.
- Perplexity delta is reported for continuity with llama.cpp. It is related to but distinct from mean KLD — it scores the realized next token and can diverge from full-vocabulary drift — so it is not independent corroboration.
- The measured drift bundles the quantizer's error with the quantized-attention kernel's numerics. That is the real end-to-end cost; a quantizer-only control is on the roadmap.
Research notes
- Low-bit KV caches on MLX: what exists and what is missing — surveys mlx-lm's shipped cache, the measured 8-bit and 4-bit fidelity cost, KIVI/KVQuant-style alternatives, and the remaining MLX layout and kernel gaps.
Status
0.5.0, released on PyPI as mlx-quant-fidelity — adds depth-resolved KV drift over a configurable --chunk-length, an auto-generated compare kv --sweep, and device provenance in every report. 0.4.0 added deployment mode (--quantize-start) and a shareable fidelity badge (--format badge). 0.3.x added the compare command for memory-normalized Pareto ranking of KV-cache and weight quantizations. Downstream-task accuracy and more are on the roadmap.
License
Sister projects
Other MLX libraries for Apple Silicon:
- mlx-taef — tiny autoencoders for fast diffusion-latent previews and low-memory decode (FLUX / SD).
- mlx-teacache — TeaCache residual caching to skip redundant FLUX denoising steps.
- mlx-model-doctor — validate an MLX / Hugging Face model repo before you load it (config, tokenizer, safetensors, memory).
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mlx_quant_fidelity-0.5.0.tar.gz.
File metadata
- Download URL: mlx_quant_fidelity-0.5.0.tar.gz
- Upload date:
- Size: 866.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
22ee32219446f34f0e62c989cde6a60914a7922e512d27e17ab29a05055e765c
|
|
| MD5 |
00a4698d87d8f88ea7614bcf77167287
|
|
| BLAKE2b-256 |
e3f0da1b492f60d93d99523ed58e4f432e27cd1fc761a222fa8b7d56e4b10940
|
Provenance
The following attestation bundles were made for mlx_quant_fidelity-0.5.0.tar.gz:
Publisher:
release.yml on IonDen/mlx-quant-fidelity
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mlx_quant_fidelity-0.5.0.tar.gz -
Subject digest:
22ee32219446f34f0e62c989cde6a60914a7922e512d27e17ab29a05055e765c - Sigstore transparency entry: 2409998793
- Sigstore integration time:
-
Permalink:
IonDen/mlx-quant-fidelity@80819769a3781f37277f4ec593fba5acd28a7ea7 -
Branch / Tag:
refs/tags/v0.5.0 - Owner: https://github.com/IonDen
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@80819769a3781f37277f4ec593fba5acd28a7ea7 -
Trigger Event:
push
-
Statement type:
File details
Details for the file mlx_quant_fidelity-0.5.0-py3-none-any.whl.
File metadata
- Download URL: mlx_quant_fidelity-0.5.0-py3-none-any.whl
- Upload date:
- Size: 56.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4523f7405b7c82d7631c039bfe8b8cf8731e9d51f60deb36d6ed1bbaff954e8c
|
|
| MD5 |
d7f7fe098f5786ee2a77c1126a484080
|
|
| BLAKE2b-256 |
a11a245d88016e922e4b606ea71e4a300c5d90879aec94261f7c75810f9be55b
|
Provenance
The following attestation bundles were made for mlx_quant_fidelity-0.5.0-py3-none-any.whl:
Publisher:
release.yml on IonDen/mlx-quant-fidelity
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mlx_quant_fidelity-0.5.0-py3-none-any.whl -
Subject digest:
4523f7405b7c82d7631c039bfe8b8cf8731e9d51f60deb36d6ed1bbaff954e8c - Sigstore transparency entry: 2409998851
- Sigstore integration time:
-
Permalink:
IonDen/mlx-quant-fidelity@80819769a3781f37277f4ec593fba5acd28a7ea7 -
Branch / Tag:
refs/tags/v0.5.0 - Owner: https://github.com/IonDen
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@80819769a3781f37277f4ec593fba5acd28a7ea7 -
Trigger Event:
push
-
Statement type: