Skip to content

llama-quant : fail early on missing imatrix, refactor type selection, code cleanup - #19770

Merged
ggerganov merged 13 commits into
ggml-org:masterfrom
ddh0:llama-quant-refactor-2
Mar 10, 2026
Merged

llama-quant : fail early on missing imatrix, refactor type selection, code cleanup#19770
ggerganov merged 13 commits into
ggml-org:masterfrom
ddh0:llama-quant-refactor-2

Conversation

@ddh0

@ddh0 ddh0 commented Feb 20, 2026

Copy link
Copy Markdown
Contributor

Currently, if a quantization requires an importance matrix and one isn't provided, the program doesn't discover this until it reaches the offending tensor during the main quantization loop. Depending on model size and target type, this can mean wasting minutes to hours before the process aborts with a partial GGUF.

This PR adds a preliminary metadata pass over all tensors that determines target types upfront, enabling early validation. The quantization logic in llama-quant.cpp is refactored for clarity and correctness.


Fail early for missing required imatrix

A preliminary pass now computes each tensor's target quantization type before the main loop begins. If an importance matrix is required but missing, quantization fails immediately with an error identifying the offending tensor and its target type:

Screenshot 2026-02-26 at 1 05 57 AM

The old ftype-based imatrix guard in quantize.cpp is removed in favor of this per-tensor check. tensor_requires_imatrix (renamed from tensor_type_requires_imatrix) now uses a switch on dst_type and correctly exempts per_layer_token_embd.weight in addition to token_embd.weight.

Refactoring

Extracted functions to reduce the size of llama_model_quantize_impl and make the logic reusable across the preliminary and main passes, and to improve clarity and maintainability:

  • tensor_allows_quantization: consolidates all "should we quantize this tensor?" checks (norm tensors, RWKV weights, conv1d, positional embeddings, etc.) previously inlined in the main loop
  • tensor_category + tensor_get_category: replaces repeated name.find(...) calls with a single categorization pass; used by type selection and the attention-v check (category_is_attn_v)
  • llama_tensor_get_type / llama_tensor_get_type_impl: splits type resolution into a wrapper (manual overrides, token_embedding/output overrides, fallbacks) and the core mixture/architecture logic
  • tensor_type_fallback: extracted from the inline incompatible-shape handling block; now also handles the rare case where the fallback type itself is incompatible (falls back to F16 with a warning)
  • llama_ftype_get_default_type: the ftype -> ggml_type switch, extracted and organized by category
  • tensor_name_match_token_embd / tensor_name_match_output_weight: small helpers used across multiple call sites

Other changes

  • Regex patterns for --tensor-type compiled once in the quantize_state_impl constructor instead of compiled per-tensor, per-flag
  • tensor_quantization struct moved from llama-quant.cpp to the header (shared with quantize.cpp via #include instead of duplicated)
  • tensor_category enum and tensor_metadata struct added to the header
  • has_output replaced with has_tied_embeddings (clearer semantics, inverted logic, same behavior)
  • n_k_quantized counter removed (fallback count now reported against ml.n_tensors)
  • Removed dead MXFP4 sanity check code (#if 0 block)
  • Some logging cleanup

@ddh0 ddh0 changed the title quantize : refactor llama-quant.cpp quantize : refactor llama-quant.cpp (imatrix fail-early) Feb 21, 2026
@ddh0

This comment was marked as outdated.

@ddh0

This comment was marked as outdated.

@ddh0

This comment was marked as resolved.

@ddh0

This comment was marked as outdated.

@ddh0 ddh0 changed the title quantize : refactor llama-quant.cpp (imatrix fail-early) quantize : fail-early on missing imatrix; refactor + optimize Feb 26, 2026
@ddh0
ddh0 marked this pull request as ready for review February 26, 2026 05:57
@ddh0
ddh0 requested a review from ggerganov as a code owner February 26, 2026 05:57
@ddh0

ddh0 commented Feb 26, 2026

Copy link
Copy Markdown
Contributor Author

I am lucky to have had helpful feedback from @ubergarm, @AesSedai, @aldehir, @pwilkin, @compilade, and @bartowski - they all deserve a mention here. Thanks :) 🦙

Comment thread src/llama-quant.cpp Outdated
Comment thread src/llama-quant.cpp Outdated
@ddh0

This comment was marked as outdated.

@ddh0

This comment was marked as outdated.

@ddh0

ddh0 commented Feb 28, 2026

Copy link
Copy Markdown
Contributor Author

Marking back as draft until things have settled down a bit.

@ddh0
ddh0 marked this pull request as draft February 28, 2026 05:14
Comment thread src/llama-quant.cpp
@ddh0 ddh0 changed the title quantize : fail-early on missing imatrix; refactor + optimize quantize : refactor; use quantization work scheduler for faster, more efficient quantization Mar 3, 2026
@ddh0
ddh0 force-pushed the llama-quant-refactor-2 branch from e314fa3 to decff8b Compare March 4, 2026 00:22
@ddh0 ddh0 changed the title quantize : refactor; use quantization work scheduler for faster, more efficient quantization quantize : imatrix fail-early, begin code cleanup Mar 4, 2026
@ddh0 ddh0 changed the title quantize : imatrix fail-early, begin code cleanup llama-quant : fail early on missing imatrix, refactor type selection, code cleanup Mar 4, 2026
@ddh0
ddh0 marked this pull request as ready for review March 4, 2026 01:37
@ddh0

ddh0 commented Mar 4, 2026

Copy link
Copy Markdown
Contributor Author

cc @ggerganov, @CISC - sorry for the messy thread, and for the re-marking as draft; there were previously a lot more changes here, but I'm going to leave some of my more aspirational improvements for future PRs.

I would like to get your thought on the changes here, as to whether or not they are acceptable overall.

@ddh0
ddh0 requested a review from compilade March 4, 2026 01:42

@ggerganov ggerganov 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.

Overall the refactoring goals are good. Double-check that there aren't any functional changes by comparing a few quantizations before/after and we can merge.

Comment thread tools/quantize/quantize.cpp Outdated
#include "common.h"
#include "llama.h"
#include "gguf.h"
#include "../src/llama-quant.h"

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.

Is this needed?

@ddh0 ddh0 Mar 4, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, though right now it's just for the struct tensor_quantization which was previously duplicated between quantize.cpp and llama-quant.cpp.

(edit: replying to ggerganov)

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.

Including internal sources from libllama is not OK, so better to keep the duplicated structs.

Generally, passing C++ structs across the C-style API is also not OK. This API has to be refactored at some point (#12511 (comment)).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I understand, thanks. Will revert this change.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reverted in 4b7ebed.

@ggerganov
ggerganov merged commit 1dab5f5 into ggml-org:master Mar 10, 2026
15 of 75 checks passed
@ddh0
ddh0 deleted the llama-quant-refactor-2 branch March 10, 2026 06:48
ddh0 added a commit to ddh0/llama.cpp that referenced this pull request Mar 10, 2026
In ggml-org#19770, I introduced a regression in the way the
`quantize_state_impl` counter values were initialized. I was
incrementing and using `n_attention_wv` in the same loop, when it should
have been fixed by the time we're deciding tensor types in
`llama_tensor_get_type_impl` (for `use_more_bits`).

I never observed a difference in any of [my
tests](ggml-org#19770 (comment))
- it was only after @bartowski kindly pointed this out that I realized
it was incorrect. (Thanks!)
ggerganov pushed a commit that referenced this pull request Mar 10, 2026
* llama-quant : correct `n_attention_wv` usage

In #19770, I introduced a regression in the way the
`quantize_state_impl` counter values were initialized. I was
incrementing and using `n_attention_wv` in the same loop, when it should
have been fixed by the time we're deciding tensor types in
`llama_tensor_get_type_impl` (for `use_more_bits`).

I never observed a difference in any of [my
tests](#19770 (comment))
- it was only after @bartowski kindly pointed this out that I realized
it was incorrect. (Thanks!)

* simplify
Seunghhon pushed a commit to Seunghhon/llama.cpp that referenced this pull request Apr 26, 2026
… code cleanup (ggml-org#19770)

* quantize : imatrix-fail early + code cleanup

* fix manual override printing

it's in the preliminary loop now, so needs to be on its own line

* revert header changes per ggerganov

* remove old #includes

* clarify naming

rename `tensor_quantization` to `tensor_typo_option` to descirbe its
functionality

* fix per barto
Seunghhon pushed a commit to Seunghhon/llama.cpp that referenced this pull request Apr 26, 2026
* llama-quant : correct `n_attention_wv` usage

In ggml-org#19770, I introduced a regression in the way the
`quantize_state_impl` counter values were initialized. I was
incrementing and using `n_attention_wv` in the same loop, when it should
have been fixed by the time we're deciding tensor types in
`llama_tensor_get_type_impl` (for `use_more_bits`).

I never observed a difference in any of [my
tests](ggml-org#19770 (comment))
- it was only after @bartowski kindly pointed this out that I realized
it was incorrect. (Thanks!)

* simplify
my-other-github-account pushed a commit to my-other-github-account/llama.cpp that referenced this pull request May 15, 2026
… code cleanup (ggml-org#19770)

* quantize : imatrix-fail early + code cleanup

* fix manual override printing

it's in the preliminary loop now, so needs to be on its own line

* revert header changes per ggerganov

* remove old #includes

* clarify naming

rename `tensor_quantization` to `tensor_typo_option` to descirbe its
functionality

* fix per barto
my-other-github-account pushed a commit to my-other-github-account/llama.cpp that referenced this pull request May 15, 2026
* llama-quant : correct `n_attention_wv` usage

In ggml-org#19770, I introduced a regression in the way the
`quantize_state_impl` counter values were initialized. I was
incrementing and using `n_attention_wv` in the same loop, when it should
have been fixed by the time we're deciding tensor types in
`llama_tensor_get_type_impl` (for `use_more_bits`).

I never observed a difference in any of [my
tests](ggml-org#19770 (comment))
- it was only after @bartowski kindly pointed this out that I realized
it was incorrect. (Thanks!)

* simplify
fewtarius pushed a commit to fewtarius/CachyLLama that referenced this pull request May 30, 2026
… code cleanup (ggml-org#19770)

* quantize : imatrix-fail early + code cleanup

* fix manual override printing

it's in the preliminary loop now, so needs to be on its own line

* revert header changes per ggerganov

* remove old #includes

* clarify naming

rename `tensor_quantization` to `tensor_typo_option` to descirbe its
functionality

* fix per barto
fewtarius pushed a commit to fewtarius/CachyLLama that referenced this pull request May 30, 2026
* llama-quant : correct `n_attention_wv` usage

In ggml-org#19770, I introduced a regression in the way the
`quantize_state_impl` counter values were initialized. I was
incrementing and using `n_attention_wv` in the same loop, when it should
have been fixed by the time we're deciding tensor types in
`llama_tensor_get_type_impl` (for `use_more_bits`).

I never observed a difference in any of [my
tests](ggml-org#19770 (comment))
- it was only after @bartowski kindly pointed this out that I realized
it was incorrect. (Thanks!)

* simplify
fukuro-kun pushed a commit to fukuro-kun/fukuro-llama-cpp-turboquant that referenced this pull request Jul 5, 2026
… code cleanup (ggml-org#19770)

* quantize : imatrix-fail early + code cleanup

* fix manual override printing

it's in the preliminary loop now, so needs to be on its own line

* revert header changes per ggerganov

* remove old #includes

* clarify naming

rename `tensor_quantization` to `tensor_typo_option` to descirbe its
functionality

* fix per barto
fukuro-kun pushed a commit to fukuro-kun/fukuro-llama-cpp-turboquant that referenced this pull request Jul 5, 2026
* llama-quant : correct `n_attention_wv` usage

In ggml-org#19770, I introduced a regression in the way the
`quantize_state_impl` counter values were initialized. I was
incrementing and using `n_attention_wv` in the same loop, when it should
have been fixed by the time we're deciding tensor types in
`llama_tensor_get_type_impl` (for `use_more_bits`).

I never observed a difference in any of [my
tests](ggml-org#19770 (comment))
- it was only after @bartowski kindly pointed this out that I realized
it was incorrect. (Thanks!)

* simplify
MrLordCat referenced this pull request in MrLordCat/llama.cpp-rdna-lab Jul 16, 2026
… code cleanup (#19770)

* quantize : imatrix-fail early + code cleanup

* fix manual override printing

it's in the preliminary loop now, so needs to be on its own line

* revert header changes per ggerganov

* remove old #includes

* clarify naming

rename `tensor_quantization` to `tensor_typo_option` to descirbe its
functionality

* fix per barto
MrLordCat pushed a commit to MrLordCat/llama.cpp-rdna-lab that referenced this pull request Jul 16, 2026
* llama-quant : correct `n_attention_wv` usage

In #19770, I introduced a regression in the way the
`quantize_state_impl` counter values were initialized. I was
incrementing and using `n_attention_wv` in the same loop, when it should
have been fixed by the time we're deciding tensor types in
`llama_tensor_get_type_impl` (for `use_more_bits`).

I never observed a difference in any of [my
tests](ggml-org/llama.cpp#19770 (comment))
- it was only after @bartowski kindly pointed this out that I realized
it was incorrect. (Thanks!)

* simplify
The-Monk pushed a commit to The-Monk/llama.cpp that referenced this pull request Aug 5, 2026
* llama-quant : correct `n_attention_wv` usage

In #19770, I introduced a regression in the way the
`quantize_state_impl` counter values were initialized. I was
incrementing and using `n_attention_wv` in the same loop, when it should
have been fixed by the time we're deciding tensor types in
`llama_tensor_get_type_impl` (for `use_more_bits`).

I never observed a difference in any of [my
tests](ggml-org/llama.cpp#19770 (comment))
- it was only after @bartowski kindly pointed this out that I realized
it was incorrect. (Thanks!)

* simplify
zommiommy pushed a commit to zommiommy/llama.cpp that referenced this pull request Aug 18, 2026
… code cleanup (ggml-org#19770)

* quantize : imatrix-fail early + code cleanup

* fix manual override printing

it's in the preliminary loop now, so needs to be on its own line

* revert header changes per ggerganov

* remove old #includes

* clarify naming

rename `tensor_quantization` to `tensor_typo_option` to descirbe its
functionality

* fix per barto
zommiommy pushed a commit to zommiommy/llama.cpp that referenced this pull request Aug 18, 2026
* llama-quant : correct `n_attention_wv` usage

In ggml-org#19770, I introduced a regression in the way the
`quantize_state_impl` counter values were initialized. I was
incrementing and using `n_attention_wv` in the same loop, when it should
have been fixed by the time we're deciding tensor types in
`llama_tensor_get_type_impl` (for `use_more_bits`).

I never observed a difference in any of [my
tests](ggml-org#19770 (comment))
- it was only after @bartowski kindly pointed this out that I realized
it was incorrect. (Thanks!)

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants