Skip to content

quantize: add option to automatically choose optimal quant types to reach a file/bpw target size at lowest error - #15550

Merged
pwilkin merged 340 commits into
ggml-org:masterfrom
EAddario:quantize
Nov 23, 2026
Merged

pwilkin merged 340 commits into
ggml-org:masterfrom
EAddario:quantize

Conversation

@EAddario

@EAddario EAddario commented Aug 24, 2025 •

Copy link
Copy Markdown
Contributor

This PR adds target_bpw_type(), a function to determine an optimal per-tensor quantization mix to achieve a user-specified total file size (i.e., --target-size 1.5g) or a global bits-per-weight (bpw) target (i.e., --target-bpw 4.5678).

An evaluation of 34 models across 238 tests shows that target_bpw_type() optimization matched or exceeded the quality of standard quantization in 93% of cases, as indicated by lower KLD metrics (see "Test results" below).

The function solves a constrained optimization problem to minimize quantization error, subject to a global size budget. It estimates per-tensor error for each layer, and dynamically allocates the bit budget where is needed the most.

High level flow:

  1. Error estimation via Monte Carlo sampling: For every applicable tensor, the function computes the quantization error, dequantizing a subset of rows weighted by an importance matrix (imatrix).
  2. Pareto Frontier analysis: For each tensor, it identifies the Pareto frontier discarding types that increase size without sufficiently decreasing error.
  3. Lagrangian optimization: It uses Lagrangian relaxation to find an optimal distribution of bits across the entire model. Higher bit-rates are dynamically allocated to tensors where they provide the highest reduction in error.
  4. Resumable state: When --state-file is set, target computations are saved to a file. If the quantization is interrupted (e.g., Ctrl+C), it can resume where it left off in the next run.

Advantages

  1. Target arbitrary size models

    • The algorithm will produce a model (nearly) exactly of the requested file/bpw size, which is very useful for maximizing VRAM usage. In a system with 24GB VRAM and a 70B model, standard quants might produce a 16.8GB file (too small, quality left on table) or a 24.1GB file (won't fit). --target-size 23.85g will generate a 23.85 GiB file to utilize the hardware fully.
  2. Data-driven mixed precision often can improve quality at fixed size

    • Instead of using hardcoded heuristics, that may be sub‑optimal for a given architecture or size, the mix is determined by the actual error sensitivity of the specific model's weights. This often yields a better quality/size trade-off, especially in aggressive quantization scenarios (1.5 to 3.5 bpw), or for unusual architectures.
  3. Allows better like-for-like comparisons between models and families

    • Standard quantization uses hardcoded rules like: "use Q4_K_M, except bump some tensors up/down, except fall back if incompatible, except keep some tensors unquantized..." and consquently, two different models quantized at the same Q4_K_M level can end up with very different bpw (e.g. 4.75 and 4.30).

    • Model performance generally scales with size; larger models typically outperform smaller ones. A model quantized with more bits will usually perform better (exhibiting lower perplexity and better evaluation scores) than a smaller version, even when the same underlying quantization method is used. This makes performance comparisons between models not a controlled experiment, as the models being compared have different effective compression ratios.

    • --target-bpw helps to normalize experiments by forcing models to be quantized to a roughly equal overall byte budget. This standardization allows performance variations between models to be more accurately linked to underlying factors such as architectural or training differences, the effect of quantization error at the same compression level, or the decisions made by the optimizer regarding allocation.

Disadvantages

  1. Quantization process is significantly slower than standard

    • This approach can take 5x-10x longer as it quantizes a sample of most tensors into 15 different formats, dequantizes them back, computes error diffs, and selects the best size/error option that fits the target file size or global bpw budget.

    • However, the --state-file option will save the above-mentioned computations to disk so that future quantizations can be generated at normal speed. It also allows to interrupt the computation process and resume it at a later time.

  2. The optimization target is only a proxy for the model's performance quality

    • The process minimizes a per-tensor estimated error computed from sampled rows, not actual perplexity or divergence of output distributions. Since errors interact nonlinearly across layers, there are no guarantees it will select the best possible mix subject to the file/bpw size constraint.
  3. An imatrix with activations data is required for best results

    • The algorithm requires an imatrix generated by llama-imatrix commit 0504396 or newer
    • Activation and statistics data are needed to compute the error. If the imatrix file does not contain the necessary data, --target-bpw and --target-file will refuse to run.

Design considerations

The target_bpw_type() function is implemented as a container for several lambdas providing all the logic for serialization, multithreading, math/stats, and optimization. Although there are clear downsides to this approach (i.e. cognitive load, testability and maintainability), given that all structs and helper lambdas are highly specific to this exact algorithm and have no reuse value elsewhere in the library, a self-contained "God function" seemed the best option to keep modifications to existing code to a minimum.

Test results

Target BPW test results
`KLD results over the full quant range:`
model IQ1_M Q2_K Q3_K Q4_K Q5_K Q6_K Q8_0
ARWKV-R1-1B5 WIN WIN WIN WIN WIN WIN TIE
ARWKV-R1-7B WIN WIN WIN LOSS WIN WIN TIE
ERNIE-4.5-0.3B-PT WIN WIN WIN WIN WIN WIN TIE
ERNIE-4.5-21B-A3B-PT WIN WIN WIN WIN WIN WIN WIN
Falcon-H1-1.5B-Instruct WIN WIN WIN WIN WIN LOSS WIN
Falcon-H1-7B-Instruct LOSS LOSS LOSS WIN WIN LOSS TIE
gemma-3-4b-it WIN WIN WIN WIN WIN WIN TIE
gemma-3-12b-it WIN WIN WIN WIN WIN WIN TIE
gemma-4-E2B-it WIN WIN WIN WIN WIN WIN WIN
gemma-4-E4B-it WIN WIN WIN WIN WIN WIN WIN
granite-3.1-1b-a400m-instruct WIN WIN WIN WIN WIN WIN WIN
granite-3.1-3b-a800m-instruct WIN WIN WIN WIN WIN WIN WIN
granite-4.0-h-tiny WIN WIN WIN WIN WIN TIE TIE
granite-4.0-micro WIN WIN WIN WIN WIN WIN WIN
granite-4.1-3b WIN WIN WIN WIN WIN WIN WIN
granite-4.1-8b WIN WIN WIN WIN WIN WIN TIE
Huihui-MoE-1.2B-A0.6B WIN WIN WIN WIN WIN WIN TIE
Huihui-MoE-5B-A1.7B-abliterated WIN WIN WIN WIN WIN WIN WIN
Llama-3.1-8B WIN WIN WIN WIN WIN WIN TIE
Llama-3.2-1B WIN WIN WIN WIN WIN WIN TIE
mamba-1.4b-hf WIN WIN WIN WIN WIN WIN TIE
mamba-2.8b-hf LOSS LOSS LOSS LOSS LOSS LOSS LOSS
MiniCPM5-1B WIN WIN WIN WIN WIN WIN TIE
MiniCPM5-2B WIN WIN WIN WIN WIN WIN TIE
Ministral-3-3B-Instruct-2512 WIN WIN WIN WIN WIN TIE TIE
Ministral-3-8B-Instruct-2512 WIN WIN WIN WIN WIN WIN TIE
NVIDIA-Nemotron-Nano-9B-v2 WIN WIN WIN WIN WIN LOSS WIN
NVIDIA-Nemotron-Nano-12B WIN WIN WIN WIN WIN WIN WIN
Phi-4-mini-reasoning NA LOSS WIN WIN WIN WIN TIE
Phi-4-reasoning WIN WIN WIN WIN WIN WIN TIE
Qwen3-8B WIN WIN WIN WIN WIN WIN TIE
Qwen3-14B WIN WIN WIN WIN TIE WIN TIE
Qwen3.5-4B WIN WIN WIN WIN WIN TIE TIE
Qwen3.5-9B WIN WIN WIN WIN WIN LOSS TIE

238 tests: 195 (82%) WIN, 26 (11%) TIE, 16 (7%) LOSS, 1 NA

AI usage disclosure

AI was used to validate the mathematical approach and calculations, and to optimize and debug the code.

Special thanks to @AesSedai, @am17an, @compilade, @ddh0 and @ngxson for their contributions during the development of this PR.

@netrunnereve

netrunnereve commented Aug 25, 2025 •

Copy link
Copy Markdown
Contributor

This is a very interesting idea and makes me think of video compression. In video we can use a variable bitrate algorithm that allocates more bits to scenes with lots of detail and less bits for say a still image, all while targeting a preset birate.

I'm just thinking here but maybe in the future we can consider performance as well and automatically juggle error and speed with some sort of slider like what they have for video.

screenshot

@EAddario EAddario changed the title quantize: add option to automatically choose optimal quant types to reach a bpw target at lowest MSE error possible quantize: add option to automatically choose optimal quant types to reach a bpw target at lowest MSE error Aug 25, 2025
@EAddario

Copy link
Copy Markdown
Contributor Author

Sharing some like-for-like test results showing that this approach produces, in the majority of the cases, better quality models compared to naive quantisation (i.e. simply running standard llama-quantize with no further optimisations).

To reduce the duration of the tests, I have chosen two small but representative models: Llama-3.2-1B ("classic" transformer architecture) and Huihui-MoE-1.2B-A0.6B (typical Mixture or Experts).

The test protocol for each is:

  1. Generate Q8_0, Q6_K, Q5_K_M, Q5_K_S, Q4_K_M, Q4_K_S, Q3_K_L, Q3_K_M, Q3_K_S, IQ4_NL, IQ3_M, and IQ3_S naive quantisations (e.g. llama-quantize --imatrix imatrix-with-activations.gguf LLM-Model-F16.gguf Naive-Quantized-<TYPE>.gguf <type>)
  2. Determine each model bits per weight (bpw). This can be easily done by using python llama.cpp/gguf-py/gguf/scripts/gguf_dump.py --markdown BPW-Quantized-<TYPE>.gguf type
  3. Generate equivalent quant types by setting --target-bpw to the corresponding bpw values (e.g. llama-quantize --imatrix imatrix-with-activations.gguf --target-bpw <naive bpw> LLM-Model-F16.gguf BPW-Quantized-<TYPE>.gguf <type>)
  4. Calculate quality scores via llama-perplexity -m <Naive|BPW>-Quantized-<TYPE>.gguf -f calibration_dataset.txt --kl-divergence-base LLM-Model-F16.logits --kl-divergence

Llama-3.2-1B results:

Model Naive BPW Target BPW Naive PPL PPL Naive 𝜌PPL 𝜌PPL Naive KLD KLD
IQ3_M 4.2042 4.2058 11.21441 13.08066 97.46% 94.62% 0.14661 0.29047
IQ3_S 4.1177 4.1191 11.41846 14.10772 97.08% 93.22% 0.16744 0.36883
IQ4_NL 4.9535 4.9542 10.10609 9.98096 99.19% 99.41% 0.04641 0.03356
Q3_K_L 4.6913 4.6894 10.74840 10.30599 98.10% 98.83% 0.10510 0.06738
Q3_K_M 4.4215 4.4184 10.97909 10.42277 97.71% 98.65% 0.12602 0.07920
Q3_K_S 4.1033 4.1037 14.19578 12.11165 92.80% 95.92% 0.37986 0.22400
Q4_K_M 5.1779 5.1792 10.01618 9.88781 99.34% 99.54% 0.03732 0.02654
Q4_K_S 4.9704 4.9762 10.06778 9.97105 99.27% 99.42% 0.04243 0.03350
Q5_K_M 5.8499 5.8521 9.75894 9.79049 99.80% 99.73% 0.01128 0.01620
Q5_K_S 5.7273 5.7291 9.76039 9.79663 99.80% 99.70% 0.01135 0.01757
Q6_K 6.5639 6.5646 9.68812 9.68277 99.91% 99.94% 0.00495 0.00354
Q8_0 8.5013 8.486 9.65172 9.64781 99.99% 99.99% 0.00050 0.00048

Huihui-MoE-1.2B-A0.6B results:

Model Naive BPW Target BPW Naive PPL PPL Naive 𝜌PPL 𝜌PPL Naive KLD KLD
IQ3_M 3.9173 3.9204 27.44670 30.17950 92.93% 91.42% 0.53704 0.58776
IQ3_S 3.8207 3.8239 29.30734 32.94412 92.20% 90.00% 0.52148 0.70061
IQ4_NL 4.5043 4.5092 19.55229 19.71237 98.62% 98.09% 0.08709 0.13948
Q3_K_L 4.3883 4.3923 21.48216 20.62434 96.80% 98.20% 0.20565 0.12301
Q3_K_M 4.1221 5.0412 21.94908 18.87276 96.43% 99.20% 0.23232 0.04863
Q3_K_S 3.8207 3.8519 26.05622 23.81005 93.87% 95.60% 0.41128 0.30752
Q4_K_M 4.9904 5.0412 18.91957 18.87276 99.02% 99.20% 0.05888 0.04863
Q4_K_S 4.7793 4.7826 19.12118 19.25212 98.89% 99.02% 0.06898 0.06238
Q5_K_M 5.7541 5.7950 18.28129 18.31989 99.66% 99.70% 0.01778 0.01531
Q5_K_S 5.6323 5.6342 18.38359 18.37216 99.63% 99.65% 0.02013 0.01884
Q6_K 6.5655 6.5693 18.20380 18.19202 99.80% 99.81% 0.00776 0.00725
Q8_0 8.5028 8.5071 18.09292 18.08959 99.90% 99.90% 0.00094 0.00090

PPL: the smaller the better; 𝜌PPL: the higher the better; KLD: the smaller the better; In bold: best quality

Note

Although these are very encouraging results, more testing with different model architectures and sizes will be required before categorically concluding this functionality consistently yields higher quality models.

Comments, feedback and, in particular, bug reports are very much welcome

@ThiloteE

ThiloteE commented Aug 30, 2025 •

Copy link
Copy Markdown
Contributor

This has an effect on the file size of the quants too, right?

@EAddario

Copy link
Copy Markdown
Contributor Author

That's correct @ThiloteE, file size is directly and proportionally influenced by the chosen bpw

Comment thread src/llama-quant.cpp Resolved
Comment thread src/llama-quant.cpp Resolved
Comment thread src/llama-quant.cpp Resolved
Comment thread src/llama-quant.cpp Resolved
@EAddario

Copy link
Copy Markdown
Contributor Author

Latest version introduces a new algorithm to select candidates resulting in models that are much closer to the bpw target and, in the large majority of the models tested, with better quality metrics than the equivalent naive version or very close:

ERNIE-4.5-21B-A3B-PT
Model BPW PPL 𝜌PPL KLD Same Top P
ERNIE-4.5-21B-A3B-PT-Q4_K_M-naive 4.8543 6.358778 99.48% 0.020945 93.72%
ERNIE-4.5-21B-A3B-PT-Q4_K_M-mse 4.8600 6.358717 99.57% 0.017668 94.04%
ERNIE-4.5-21B-A3B-PT-Q4_K_M-fast 4.8600 6.361663 99.57% 0.017631 94.05%
ERNIE-4.5-21B-A3B-PT-Q4_K_M-precise 4.8600 6.361663 99.57% 0.017631 94.05%
ERNIE-4.5-21B-A3B-PT-IQ4_NL-naive 4.5325 6.373562 99.40% 0.025309 93.14%
ERNIE-4.5-21B-A3B-PT-IQ4_NL-mse 4.5382 6.350721 99.44% 0.023833 93.18%
ERNIE-4.5-21B-A3B-PT-IQ4_NL-fast 4.5382 6.360253 99.45% 0.023487 93.29%
ERNIE-4.5-21B-A3B-PT-IQ4_NL-precise 4.5382 6.360253 99.45% 0.023487 93.29%
Huihui-MoE-1.2B-A0.6B
Model BPW PPL 𝜌PPL KLD Same Top P
Huihui-MoE-1.2B-A0.6B-Q4_K_M-naive 4.9904 18.919566 99.02% 0.058880 87.35%
Huihui-MoE-1.2B-A0.6B-Q4_K_M-mse 4.9934 18.790209 99.19% 0.047507 88.43%
Huihui-MoE-1.2B-A0.6B-Q4_K_M-fast 4.9934 19.042245 99.11% 0.056029 87.12%
Huihui-MoE-1.2B-A0.6B-Q4_K_M-precise 4.9928 18.850755 99.20% 0.047156 88.47%
Huihui-MoE-1.2B-A0.6B-IQ4_NL-naive 4.7539 19.547184 98.62% 0.087197 84.89%
Huihui-MoE-1.2B-A0.6B-IQ4_NL-mse 4.7568 19.164889 98.99% 0.064329 86.78%
Huihui-MoE-1.2B-A0.6B-IQ4_NL-fast 4.7563 19.123570 98.88% 0.069254 86.30%
Huihui-MoE-1.2B-A0.6B-IQ4_NL-precise 4.7568 19.203988 98.87% 0.070200 86.04%
Huihui-MoE-5B-A1.7B-abliterated
Model BPW PPL 𝜌PPL KLD Same Top P
Huihui-MoE-5B-A1.7B-abliterated-Q4_K_M-naive 5.1797 14.744046 99.21% 0.031757 92.07%
Huihui-MoE-5B-A1.7B-abliterated-Q4_K_M-mse 5.1815 14.833509 99.20% 0.032450 92.01%
Huihui-MoE-5B-A1.7B-abliterated-Q4_K_M-fast 5.1812 14.879478 99.19% 0.032689 91.96%
Huihui-MoE-5B-A1.7B-abliterated-Q4_K_M-precise 5.1813 14.851407 99.20% 0.032571 92.00%
Huihui-MoE-5B-A1.7B-abliterated-IQ4_NL-naive 4.6252 14.911541 98.58% 0.065290 89.03%
Huihui-MoE-5B-A1.7B-abliterated-IQ4_NL-mse 4.6270 14.917422 98.86% 0.050793 90.09%
Huihui-MoE-5B-A1.7B-abliterated-IQ4_NL-fast 4.6270 14.787866 98.80% 0.051434 90.12%
Huihui-MoE-5B-A1.7B-abliterated-IQ4_NL-precise 4.6270 14.825255 98.79% 0.051980 90.09%
Llama-3.1-8B
Model BPW PPL 𝜌PPL KLD Same Top P
Llama-3.1-8B-Q4_K_M-naive 5.1777 6.291397 99.47% 0.024019 93.18%
Llama-3.1-8B-Q4_K_M-mse 5.1781 6.243892 99.64% 0.016981 93.88%
Llama-3.1-8B-Q4_K_M-fast 5.1781 6.243892 99.64% 0.016981 93.88%
Llama-3.1-8B-Q4_K_M-precise 5.1781 6.243892 99.64% 0.016981 93.88%
Llama-3.1-8B-IQ4_NL-naive 4.6526 6.321343 99.33% 0.030539 92.22%
Llama-3.1-8B-IQ4_NL-mse 4.6534 6.308061 99.40% 0.027789 92.35%
Llama-3.1-8B-IQ4_NL-fast 4.6534 6.308061 99.40% 0.027789 92.35%
Llama-3.1-8B-IQ4_NL-precise 4.6534 6.308061 99.40% 0.027789 92.35%
Llama-3.2-1B
Model BPW PPL 𝜌PPL KLD Same Top P
Llama-3.2-1B-Q4_K_M-naive 5.1779 10.022728 99.34% 0.037496 90.20%
Llama-3.2-1B-Q4_K_M-mse 5.1790 9.875390 99.54% 0.026770 91.43%
Llama-3.2-1B-Q4_K_M-fast 5.1790 9.864875 99.55% 0.026093 91.55%
Llama-3.2-1B-Q4_K_M-precise 5.1790 9.864875 99.55% 0.026093 91.55%
Llama-3.2-1B-IQ4_NL-naive 4.9535 10.094483 99.18% 0.046429 88.96%
Llama-3.2-1B-IQ4_NL-mse 4.9542 9.967031 99.41% 0.033563 90.52%
Llama-3.2-1B-IQ4_NL-fast 4.9542 9.961903 99.42% 0.033207 90.54%
Llama-3.2-1B-IQ4_NL-precise 4.9542 9.961903 99.42% 0.033207 90.54%
NVIDIA-Nemotron-Nano-9B-v2
Model BPW PPL 𝜌PPL KLD Same Top P
NVIDIA-Nemotron-Nano-9B-v2-Q4_K_M-naive 5.8664 7.812578 99.83% 0.008216 95.49%
NVIDIA-Nemotron-Nano-9B-v2-Q4_K_M-mse 5.8722 7.828592 99.79% 0.011882 93.62%
NVIDIA-Nemotron-Nano-9B-v2-Q4_K_M-fast 5.8722 7.830588 99.79% 0.011959 93.62%
NVIDIA-Nemotron-Nano-9B-v2-Q4_K_M-precise 5.8722 7.829907 99.79% 0.012017 93.62%
NVIDIA-Nemotron-Nano-9B-v2-IQ4_NL-naive 4.7711 7.884677 99.68% 0.016280 93.65%
NVIDIA-Nemotron-Nano-9B-v2-IQ4_NL-mse 4.7778 7.904368 99.61% 0.021356 91.92%
NVIDIA-Nemotron-Nano-9B-v2-IQ4_NL-fast 4.7772 7.905187 99.61% 0.020880 92.06%
NVIDIA-Nemotron-Nano-9B-v2-IQ4_NL-precise 4.7771 7.903981 99.61% 0.021088 92.00%
NVIDIA-Nemotron-Nano-12B-v2
Model BPW PPL 𝜌PPL KLD Same Top P
NVIDIA-Nemotron-Nano-12B-v2-Q4_K_M-naive 4.8654 6.514963 99.70% 0.012792 94.79%
NVIDIA-Nemotron-Nano-12B-v2-Q4_K_M-mse 4.8702 6.542849 99.64% 0.016636 93.85%
NVIDIA-Nemotron-Nano-12B-v2-Q4_K_M-fast 4.8705 6.536551 99.67% 0.014806 94.11%
NVIDIA-Nemotron-Nano-12B-v2-Q4_K_M-precise 4.8705 6.535902 99.68% 0.014691 94.15%
NVIDIA-Nemotron-Nano-12B-v2-IQ4_NL-naive 4.6177 6.544873 99.60% 0.018025 93.87%
NVIDIA-Nemotron-Nano-12B-v2-IQ4_NL-mse 4.7778 7.904368 99.60% 0.021356 91.92%
NVIDIA-Nemotron-Nano-12B-v2-IQ4_NL-fast 4.6228 6.520408 99.65% 0.015356 94.29%
NVIDIA-Nemotron-Nano-12B-v2-IQ4_NL-precise 4.6228 6.520723 99.65% 0.015515 94.23%
Qwen3-14B
Model BPW PPL 𝜌PPL KLD Same Top P
Qwen3-14B-Q4_K_M-naive 4.8730 8.451932 99.47% 0.016716 94.68%
Qwen3-14B-Q4_K_M-mse 4.8735 8.432478 99.52% 0.016641 94.40%
Qwen3-14B-Q4_K_M-fast 4.8735 8.432038 99.53% 0.016474 94.39%
Qwen3-14B-Q4_K_M-precise 4.8735 8.432038 99.53% 0.016474 94.39%
Qwen3-14B-IQ4_NL-naive 4.6236 8.381749 99.35% 0.022481 93.73%
Qwen3-14B-IQ4_NL-mse 4.6243 8.371418 99.42% 0.021300 93.71%
Qwen3-14B-IQ4_NL-fast 4.6243 8.364448 99.43% 0.020910 93.77%
Qwen3-14B-IQ4_NL-precise 4.6243 8.364448 99.43% 0.020910 93.77%

@EAddario EAddario changed the title quantize: add option to automatically choose optimal quant types to reach a bpw target at lowest MSE error quantize: add option to automatically choose optimal quant types to reach a bpw target at lowest error Sep 16, 2025

@CISC CISC left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems ok in general, but I have not had time to test it, will leave that to @pwilkin

Comment thread common/imatrix-loader.cpp Resolved
Comment thread common/imatrix-loader.cpp Resolved
Comment thread src/llama-quant.cpp Resolved
Comment thread tests/CMakeLists.txt Resolved
@EAddario

Copy link
Copy Markdown
Contributor Author

Checks finally finished. Failed tests are not related to the changes as far as I can see.

@EAddario
EAddario requested a review from CISC November 21, 2026 23:32
@pwilkin

pwilkin commented Nov 22, 2026

Copy link
Copy Markdown
Member

@bartowski1182 any chance you could run a quantization pass for some of the popular small models on your hardware using this PR for a range of bpws (starting from 2.50 incrementing every 0.25 up to 5) and see how the KLD / PPL is? I'm afraid it would take ages on my home box.

@EAddario

Copy link
Copy Markdown
Contributor Author

Remember to use an imatrix created post 0504396.

When quanting the same model over a range of sizes (bytes or bpw) it helps to generate the state checkpoint first. My typical workflow starts with:

llama-quantize --dry-run --target-bpw 2.345 --state-file --imatrix imatrix.gguf input-model-f16.gguf output-model.gguf

That will generate a .bpw_state file using the default name (<model name>-<hash>.bpw_state). Once the state checkpoint exists, running the same command (minus the --dry-run 🙂) will reuse the computations for any size/bpw.

More info in the README

For an apples-to-apples comparison, I suggest to also try targeting the equivalent sizes of standard types (e.g. Q2_K, Q3_K, Q4_K, etc.) using the same quant/ppl parameters (ctx, logits, ubatch, etc.)

@bartowski1182

Copy link
Copy Markdown
Contributor

Running some tests and will let you know the results :)

@bartowski1182

bartowski1182 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Okay gathered some results, the TLDR is that for the midrange (Q5_K_M to IQ2_M) it is an improvement on several models

image

Also noticed that this makes the thread count be dropped when using the target flag

There was a bad case though, granite4-h-small lost on most sizes by a large margin, likely because it's a hybrid Mamba MoE? Those rows are at the bottom, Q3_K_M was a good increase, all the others were a net loss (Q4_K_M is within noise though basically)

Results

Wiki, 80 chunks, -c 512. GB = 10⁹ bytes; the PR file has the same size to ±30 ppm. Ratios carry 95 % paired-bootstrap CIs. * marks a harm cell.

model rung GB stock KLD stock PPL PR KLD PR PPL wiki ratio [CI] chat_en code_py
qwen35-4b IQ2_M / IQ3_XXS – stock refused – – –
qwen35-4b Q3_K_M 2.319 0.0906 10.285 0.0492 10.051 0.543 [0.522, 0.564] 0.600 0.471
qwen35-4b IQ4_XS 2.579 0.0344 9.856 0.0272 9.825 0.791 [0.743, 0.832] 0.975 0.818
qwen35-4b Q4_K_M 2.783 0.0252 9.855 0.0208 9.803 0.825 [0.770, 0.887] 0.977 0.809
qwen35-4b Q5_K_M 3.161 0.0100 9.774 0.0112 9.805 1.118 [1.049, 1.198]* 0.994 1.031
lfm3-1.7b IQ2_M 0.671 0.6671 33.570 0.7432 39.899 1.114 [1.039, 1.197]* 0.898 0.894
lfm3-1.7b IQ3_XXS 0.750 0.4430 27.677 0.3315 24.065 0.748 [0.712, 0.786] 0.783 0.828
lfm3-1.7b Q3_K_M 0.915 0.2637 26.196 0.1670 23.061 0.634 [0.556, 0.716] 0.685 0.670
lfm3-1.7b IQ4_XS 1.004 0.1299 23.286 0.1237 23.154 0.952 [0.893, 1.018] 0.838 0.916
lfm3-1.7b Q4_K_M 1.100 0.0960 23.479 0.0821 22.983 0.855 [0.783, 0.942] 0.857 0.743
lfm3-1.7b Q5_K_M 1.259 0.0337 21.953 0.0345 21.934 1.026 [0.973, 1.085] 0.946 1.034
llama31-8b IQ2_M 2.948 0.2631 9.900 0.2450 9.628 0.931 [0.909, 0.954] 0.921 0.945
llama31-8b IQ3_XXS 3.275 0.1498 8.808 0.1403 8.783 0.937 [0.917, 0.957] 0.831 0.929
llama31-8b Q3_K_M 4.019 0.0654 8.172 0.0560 8.005 0.856 [0.827, 0.884] 0.784 0.866
llama31-8b IQ4_XS 4.448 0.0280 7.814 0.0295 7.852 1.054 [1.030, 1.081]* 1.076* 1.003
llama31-8b Q4_K_M 4.921 0.0223 7.806 0.0179 7.731 0.800 [0.776, 0.824] 0.792 0.761
llama31-8b Q5_K_M 5.733 0.0079 7.651 0.0075 7.642 0.948 [0.911, 0.977] 1.011 0.926
lfm25-8b-a1b IQ2_M 2.843 0.7043 32.952 0.7213 37.560 1.024 [1.003, 1.047] 1.064* 1.056*
lfm25-8b-a1b IQ3_XXS 3.335 0.4837 34.993 0.4521 31.225 0.935 [0.916, 0.952] 0.911 0.972
lfm25-8b-a1b Q3_K_M 4.108 0.3137 34.442 0.2579 33.925 0.822 [0.790, 0.856] 0.783 0.798
lfm25-8b-a1b IQ4_XS 4.588 0.1655 31.029 0.1428 33.439 0.863 [0.826, 0.900] 0.825 0.851
lfm25-8b-a1b Q4_K_M 5.156 0.1336 31.813 0.1062 31.978 0.795 [0.759, 0.832] 0.775 0.780
lfm25-8b-a1b Q5_K_M 6.030 0.0726 32.462 0.0652 33.091 0.898 [0.842, 0.947] 0.914 0.855
granite4-h-small IQ2_M 10.609 0.2946 7.348 0.3965 7.905 1.346 [1.311, 1.383]* 1.381* 1.380*
granite4-h-small IQ3_XXS 12.507 0.1715 6.731 0.2730 7.220 1.592 [1.504, 1.683]* 1.619* 1.582*
granite4-h-small Q3_K_M 15.360 0.1175 6.455 0.0892 6.214 0.759 [0.723, 0.796] 0.755 0.799
granite4-h-small IQ4_XS 17.281 0.0388 5.990 0.0712 6.102 1.832 [1.752, 1.918]* 1.694* 1.914*
granite4-h-small Q4_K_M 19.477 0.0307 5.965 0.0323 5.985 1.050 [1.001, 1.105]* 0.927 0.971
granite4-h-small Q5_K_M 22.866 0.0141 5.913 0.0199 5.934 1.406 [1.333, 1.492]* 1.225* 1.341*

(stock refused was my fault because of the imatrix used, I'll probably re-run that specifically just for the extra info but figured should post in the interim)

@pwilkin

pwilkin commented Nov 23, 2026

Copy link
Copy Markdown
Member

All right. I say we merge it and then we can work on fine-tuning the specific cases in follow-up PRs, let's get this out of the way :)

@pwilkin
pwilkin merged commit 30ec35a into ggml-org:master Nov 23, 2026
28 of 35 checks passed
@EverchangerL

Copy link
Copy Markdown

something is wrong here with MSVC

F:\LLM\Apps\llama.cpp>cmake --build build --config Release --target llama-server llama-imatrix llama-quantize llama-perplexity -j6
MSBuild 18.12.3-1.26559.12+8df097b6f for .NET Framework

  ggml-base.vcxproj -> F:\LLM\Apps\llama.cpp\build\bin\Release\ggml-base.dll
  llama-common-base.vcxproj -> F:\LLM\Apps\llama.cpp\build\common\Release\llama-common-base.lib
  vendor-hash.vcxproj -> F:\LLM\Apps\llama.cpp\build\vendor\hash\Release\vendor-hash.lib
  cpp-httplib.vcxproj -> F:\LLM\Apps\llama.cpp\build\vendor\cpp-httplib\Release\cpp-httplib.lib
  1>Provisioning UI assets
  -- UI: HF stamp matches 'ggml-org/llama-ui|b1', skipping HF fetch
  ggml-cpu.vcxproj -> F:\LLM\Apps\llama.cpp\build\bin\Release\ggml-cpu.dll
  -- UI: assets unchanged, skipping embedding
  ggml-cuda.vcxproj -> F:\LLM\Apps\llama.cpp\build\bin\Release\ggml-cuda.dll
  llama-ui.vcxproj -> F:\LLM\Apps\llama.cpp\build\ui\apps\chat\Release\llama-ui.lib
  ggml.vcxproj -> F:\LLM\Apps\llama.cpp\build\bin\Release\ggml.dll
C:\Program Files\Microsoft Visual Studio\18\Community\MSBuild\Microsoft\VC\v180\Microsoft.CppBuild.targe
ts(1150,5): warning MSB8027: Two or more files with the name of llama.cpp will produce outputs to the sa
me location. This can lead to an incorrect build result.  The files involved are F:\LLM\Apps\llama.cpp\s
rc\llama.cpp, F:\LLM\Apps\llama.cpp\src\models\llama.cpp. [F:\LLM\Apps\llama.cpp\build\src\llama.vcxproj
]
  llama-quant.cpp
F:\LLM\Apps\llama.cpp\src\llama-quant.cpp(1674,33): error C2131: expression did not evaluate to a consta
nt [F:\LLM\Apps\llama.cpp\build\src\llama.vcxproj]
      F:\LLM\Apps\llama.cpp\src\llama-quant.cpp(1674,35):
      failure was caused by reading a value that is unknown during constant evaluation

no idea if it's related to this PR

@ServeurpersoCom

Copy link
Copy Markdown
Contributor

something is wrong here with MSVC

F:\LLM\Apps\llama.cpp>cmake --build build --config Release --target llama-server llama-imatrix llama-quantize llama-perplexity -j6
MSBuild 18.12.3-1.26559.12+8df097b6f for .NET Framework

  ggml-base.vcxproj -> F:\LLM\Apps\llama.cpp\build\bin\Release\ggml-base.dll
  llama-common-base.vcxproj -> F:\LLM\Apps\llama.cpp\build\common\Release\llama-common-base.lib
  vendor-hash.vcxproj -> F:\LLM\Apps\llama.cpp\build\vendor\hash\Release\vendor-hash.lib
  cpp-httplib.vcxproj -> F:\LLM\Apps\llama.cpp\build\vendor\cpp-httplib\Release\cpp-httplib.lib
  1>Provisioning UI assets
  -- UI: HF stamp matches 'ggml-org/llama-ui|b1', skipping HF fetch
  ggml-cpu.vcxproj -> F:\LLM\Apps\llama.cpp\build\bin\Release\ggml-cpu.dll
  -- UI: assets unchanged, skipping embedding
  ggml-cuda.vcxproj -> F:\LLM\Apps\llama.cpp\build\bin\Release\ggml-cuda.dll
  llama-ui.vcxproj -> F:\LLM\Apps\llama.cpp\build\ui\apps\chat\Release\llama-ui.lib
  ggml.vcxproj -> F:\LLM\Apps\llama.cpp\build\bin\Release\ggml.dll
C:\Program Files\Microsoft Visual Studio\18\Community\MSBuild\Microsoft\VC\v180\Microsoft.CppBuild.targe
ts(1150,5): warning MSB8027: Two or more files with the name of llama.cpp will produce outputs to the sa
me location. This can lead to an incorrect build result.  The files involved are F:\LLM\Apps\llama.cpp\s
rc\llama.cpp, F:\LLM\Apps\llama.cpp\src\models\llama.cpp. [F:\LLM\Apps\llama.cpp\build\src\llama.vcxproj
]
  llama-quant.cpp
F:\LLM\Apps\llama.cpp\src\llama-quant.cpp(1674,33): error C2131: expression did not evaluate to a consta
nt [F:\LLM\Apps\llama.cpp\build\src\llama.vcxproj]
      F:\LLM\Apps\llama.cpp\src\llama-quant.cpp(1674,35):
      failure was caused by reading a value that is unknown during constant evaluation

no idea if it's related to this PR

Please try :

diff --git a/src/llama-quant.cpp b/src/llama-quant.cpp
index c66f060f62..fe5ed33662 100644
--- a/src/llama-quant.cpp
+++ b/src/llama-quant.cpp
@@ -1671,7 +1671,7 @@ static std::unordered_map<std::string, ggml_type> target_bpw_type(
         constexpr int64_t target_rank = 25;
         constexpr int64_t k = 20;   // 20th singular value for condition number estimation
         constexpr float eps_norm = 1e-8f;
-        constexpr float eps_eig = EPSILON;
+        const float eps_eig = EPSILON;
         constexpr int jacobi_sweeps = 20;
         if (total_rows_sampled < 1 || n_per_row < 1) { return INFINITE; }

@ntowle

ntowle commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

@ServeurpersoCom Your patch fixed the build for me on MSVC 14.52.

I see there's also #32902

@EverchangerL

Copy link
Copy Markdown

@ServeurpersoCom Thank you, but I have already tried PR #32902 - it works fine on my end.
The two patches are different, though. I hope the PR one is fine (it takes me a long time to compile it on Windows).

@EAddario

Copy link
Copy Markdown
Contributor Author

Just seen this. Whilst both approaches work, PR #32902 would be safer due to the lambda captures.

@EugeoSynthesisThirtyTwo

Copy link
Copy Markdown
Contributor

For me, llama.cpp doesn't compile anymore since this commit.

changing in llama-quant.cpp
constexpr float eps_eig = EPSILON;
with
const float eps_eig = static_cast<float>(EPSILON);
fixed it.

@EAddario

Copy link
Copy Markdown
Contributor Author

Not even with ServeurpersoCom patch or after applying #32902?
What OS and compiler are you using?

@EverchangerL

Copy link
Copy Markdown

Sorry for ping, @EAddario
I tried this PR today and got conflicting results with Gemma4-26B-A4B

Basic step-by-step preparations before quantisation

Step 1: get gguf's
bf16 safetensors --> bf16 gguf (with --fuse-gate-up-exps --fuse-qkv)
bf16 gguf --> Q8_0 gguf for imatrix only

Step 2: get imatrix.gguf
Gemma4-26B-A4B-Q8_0.gguf --> imatrix.gguf
Final estimate: PPL = 97.5719 +/- 1.64554

Step 3: get the reference Q4_K_M quant from bf16 gguf with imatrix
Gemma4-26B-A4B-Q4_K_M.gguf
llama_model_quantize_impl: quant size = 16058.14 MiB (5.3384 BPW)
Final estimate: PPL = 65.4088 +/- 0.99542

This is with the --target-size 15.7g - looks good
Gemma4-26B-A4B-15.7g.gguf
llama_model_quantize_impl: quant size = 16061.63 MiB (5.3396 BPW)
Final estimate: PPL = 56.1822 +/- 0.84984

And this is with the --target-bpw 5.34
Gemma4-26B-A4B-5.34bpw.gguf
llama_model_quantize_impl: quant size = 16062.78 MiB (5.3400 BPW)
Final estimate: PPL = 58.6881 +/- 0.90024

Filesize is bigger, BPW is higher, but PPL is lower - is this expected?
Should I prefer --target-size in the future?

Cinderella-Man added a commit to Cinderella-Man/miner-llama that referenced this pull request Nov 24, 2026
Upstream's newest tag, 191 commits past b12483, merged on pascal-p106-v5 af00bb1 (the 14 landed branches).
26 files conflicted. Each kept both sides' intent; the list with reasons is the public data folder
docs/data/2026-11-24_c912_upstream_b12674/conflicts.tsv. The ones that change code:

- ggml.h, ggml.c, ggml-rpc.h: upstream's GGML_OP_SLEEP and GGML_OP_SIGMOID_BACK go after the fork's three ops after
  GLU, so the fork's op numbers stay; GGML_OP_COUNT 105, RPC patch version 6 (fork 4 + upstream 2).
- ggml-rpc.cpp: upstream's single validation step (ggml-org#32651) already rejects unknown ops, so the fork's own check goes;
  the client's FREE_BUFFER that forgets dropped graphs (C929) sends with upstream's typed call<>.
- mmid.cu: upstream's helper with duplicate expert ids (ggml-org#32505) dumps its store in pieces; the legacy tile layout
  (GGML_CUDA_MMQ_MOE_LEGACY) reads the expert's column count from n_dumped.
- mmq.cuh: the kernel takes kq_fp32 (C861) before upstream's force_tiling (GCN only); the force_tiling launch passes
  kq_fp32 through.
- ggml-cuda.cu: ggml-org#32239 (no matrix-vector fusion on any Pascal card) merged without a conflict; the fork keeps the
  fusion on compute 6.1 as v5 measured it per file, and GGML_CUDA_MMID_GLU_UNFUSE=2 gives upstream's rule (C893).
- llama-graph.cpp: upstream's automatic "<name>.scale" lookup (ggml-org#32669) replaces the explicit scale arguments, the cold
  tier's included (its scales carry their tensors' names); the down projection's rescale is upstream's use_amax rule
  (ggml-org#32677) and the fork's LLAMA_NO_MOE_DOWN_RESCALE condition; route-once's histogram before upstream's random routing
  (ggml-org#29539); the device-built KQ mask (LLAMA_GPU_KQ_MASK) moves into upstream's build_attn_iswa_impl (ggml-org#32429) and
  replaces only the default mask.
- llama-memory-hybrid-idx.cpp: ggml-org#32519 scores only the ubatch's sequences' k-pools; with the k-pool tables on the devices
  (LLAMA_QSA_KPOOL_DEV) the list keeps every sequence's pools, as the tables hold them (get_n_kpool); the pads avoid
  every pool's rep in both layouts (LLAMA_QSA_PAD_LINEAR's bound kept).
- llama-quant.cpp: N2P and N3P's fallbacks go into upstream's fallback_type_for (ggml-org#15550), with the row width; Q4_1_64
  in upstream's moved llama_ftype_get_default_type.
- glm5-next.cpp, qwen4exp.cpp: the fused lightning indexer is always on (ggml-org#32650): the in-place row variant
  (LLAMA_QSA_INDEXER_ROWS) without the removed switch.
- flint.cpp, qwen4exp.cpp, gemma4.cpp, test-llama-archs.cpp, test-backend-ops.cpp, server-context.cpp: the fork's
  code with upstream's signature changes, model-name helper, decision groups and label outputs (ggml-org#32553), gemma4's
  multi-sequence prune (ggml-org#32342) without device-built masks.

Carried upstream fixes now merged upstream: ggml-org#32434 (6026911: upstream's version taken, the fork's change kept is only
--ctx-checkpoints-share's copy and get_reuse's two trim arguments) and ggml-org#31913 (2b2da8a: upstream's fill, the same
change).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@EAddario

Copy link
Copy Markdown
Contributor Author

No worries at all @EverchangerL, always happy to help, plus I have a vested interest in people finding the functionality useful 😁

I'll start by saying Gemma 4's history is cursed (AI Mode will give you more gory details). It's a great compact model, but it's cursed nevertheless.

With that out of the way, on to your question: the differences you're observing are most likely due to rounding differences and possibly one or more types that sit at the decision boundary whereby just a few bytes difference tip the balance to use one, say IQ4_XS, instead of another (e.g. Q4_K_S).

When BPW is used, the internal optimiser will convert to bytes possibly introducing some minor rounding errors. During tests, and working with non-cursed models, I've found the end result to be virtually identical. However, if you're strictly looking for the most faithful outcome, file size is the way to go.

@LostRuins

Copy link
Copy Markdown
Collaborator

Can we not use identifiers named INFINITE or EPSILON?

For example INFINITE is a preprocessor macro on windows

#define INFINITE 0xFFFFFFFF

it's just a lot cleaner to not use the same names.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation examples ggml changes relating to the ggml tensor library for machine learning testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.