Skip to content

llama-quant : fix 4D tensor quantization using ggml_nrows() and name-based expert detection - #30820

Merged
ggerganov merged 2 commits into
ggml-org:masterfrom
anthony-wss:quantize-fix-4d-tensors
Oct 25, 2026
Merged

ggerganov merged 2 commits into
ggml-org:masterfrom
anthony-wss:quantize-fix-4d-tensors

Conversation

@anthony-wss

@anthony-wss anthony-wss commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Overview

Fix quantization of 4D tensors by using ggml_nrows() to count rows, and detect expert tensors by name instead of by shape.

Additional information

Fixes #29916 in 64d1bbb. The row count was computed from ne[1] and ne[2] only, ignoring ne[3]. For tensors like a.conv1d.0.weight [3, 3, 1, 128] this produces the wrong size and trips the gguf_get_tensor_size(...) == new_size assert. ggml_nrows() is now used instead. Other places in src/llama-quant.cpp that ignored ne[3] are fixed as well.

Before fix (reproduced.log)
[ 746/1411] a.conv1d.0.weight - [ 3, 3, 1, 128], type = f32, converting to f16 .. size = 0.00 MiB -> 0.00 MiB
llama-quant.cpp:1391: GGML_ASSERT(gguf_get_tensor_size(...) == new_size) failed
After fix (after-fix.log)
llama_model_quantize_impl: model size  =   941.01 MiB (16.59 BPW)
llama_model_quantize_impl: quant size  =   528.31 MiB (9.32 BPW)
llama_model_quantize_impl: WARNING: 14 of 1411 tensor(s) required fallback quantization
llama_quantize: quantize time =  3162.99 ms

On top of that, we previously used tensor->ne[2] > 1 to detect expert tensors, but that did not cover all cases and required some hard-coded exclusions for the bailingmoe3 model. Expert tensors are now identified by _exps. or _chexps. in their name. This new implementation does not break existing models, and it even caught an edge case where the attention weights for deepseek-v3.1 were wrongly treated as expert tensors when they should be quantized as Q8_0 instead. The test schema has also been updated.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES. AI was used for solution exploration and code generation. Every line of generated code has been reviewed by me.

Log files

@github-actions github-actions Bot added the testing Everything test related label Oct 19, 2026
@anthony-wss
anthony-wss force-pushed the quantize-fix-4d-tensors branch from e8efc1d to 1eebadc Compare October 21, 2026 08:54
nrows_total was ne[1]*ne[2], so for a 4D tensor only the first ne[2]
slices were quantized and the rest of the rows were dropped. Use
ggml_nrows(tensor) to count all rows, including ne[3].

Fixes ggml-org#29916
…4D tensors

The MXFP4_MOE path used ne[2] > 1 to detect MoE expert tensors. This is
wrong for non-expert tensors with 3 or more dims, e.g. MLA projections
(BailingMoE3) and 4D conv kernels. Use the tensor name instead: all
tensors used by GGML_OP_MUL_MAT_ID are named *_exps or *_chexps. This
also removes the BailingMoE3 special case.

The imatrix holds one vector of ne[0] values per 2D slice, so the
expected size is ne[0]*ne[2]*ne[3], not ne[0]*ne[2]. This matches how
the imatrix is indexed per row for 4D tensors in llama_tensor_quantize_impl.

Assisted-by: Claude Opus 5.5
@anthony-wss
anthony-wss force-pushed the quantize-fix-4d-tensors branch from 1eebadc to e9e884e Compare October 25, 2026 01:27
@CISC CISC added the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Oct 25, 2026
@ggerganov
ggerganov merged commit 5c965fd into ggml-org:master Oct 25, 2026
19 checks passed
@anthony-wss
anthony-wss deleted the quantize-fix-4d-tensors branch October 25, 2026 13:26
auRose94 pushed a commit to auRose94/llama.cpp that referenced this pull request Oct 28, 2026
…based expert detection (ggml-org#30820)

* llama-quant : fix quantizing 4D tensors

nrows_total was ne[1]*ne[2], so for a 4D tensor only the first ne[2]
slices were quantized and the rest of the rows were dropped. Use
ggml_nrows(tensor) to count all rows, including ne[3].

Fixes ggml-org#29916

* llama-quant : detect expert tensors by name and fix imatrix size for 4D tensors

The MXFP4_MOE path used ne[2] > 1 to detect MoE expert tensors. This is
wrong for non-expert tensors with 3 or more dims, e.g. MLA projections
(BailingMoE3) and 4D conv kernels. Use the tensor name instead: all
tensors used by GGML_OP_MUL_MAT_ID are named *_exps or *_chexps. This
also removes the BailingMoE3 special case.

The imatrix holds one vector of ne[0] values per 2D slice, so the
expected size is ne[0]*ne[2]*ne[3], not ne[0]*ne[2]. This matches how
the imatrix is indexed per row for 4D tensors in llama_tensor_quantize_impl.

Assisted-by: Claude Opus 5.5
edwardyoon pushed a commit to edwardyoon/focus-llama that referenced this pull request Oct 30, 2026
…based expert detection (ggml-org#30820)

* llama-quant : fix quantizing 4D tensors

nrows_total was ne[1]*ne[2], so for a 4D tensor only the first ne[2]
slices were quantized and the rest of the rows were dropped. Use
ggml_nrows(tensor) to count all rows, including ne[3].

Fixes ggml-org#29916

* llama-quant : detect expert tensors by name and fix imatrix size for 4D tensors

The MXFP4_MOE path used ne[2] > 1 to detect MoE expert tensors. This is
wrong for non-expert tensors with 3 or more dims, e.g. MLA projections
(BailingMoE3) and 4D conv kernels. Use the tensor name instead: all
tensors used by GGML_OP_MUL_MAT_ID are named *_exps or *_chexps. This
also removes the BailingMoE3 special case.

The imatrix holds one vector of ne[0] values per 2D slice, so the
expected size is ne[0]*ne[2]*ne[3], not ne[0]*ne[2]. This matches how
the imatrix is indexed per row for 4D tensors in llama_tensor_quantize_impl.

Assisted-by: Claude Opus 5.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

llama-quantize: GGML_ASSERT(size mismatch) when quantizing mmproj files containing 3D conv tensors (gemma4v audio conv1d)

3 participants