Skip to content

fix(gguf): honour a per-module ggml type in dynamic - #3562

Merged
Qubitium merged 2 commits into
ModelCloud:mainfrom
Qubitium:fix/gguf-dynamic-format
Nov 6, 2026
Merged

Qubitium merged 2 commits into
ModelCloud:mainfrom
Qubitium:fix/gguf-dynamic-format

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator

Bug

GGUFConfig accepts a per-module width and a per-module ggml type:

GGUFConfig(bits="q4_0", dynamic={
    "model.layers.0.mlp.down_proj": {"bits": 4, "format": "q4_k"},
})

Two fields are needed because a width maps to several types — width 4 is
q4_0 / q4_1 / q4_k, width 2 is q2_0 / q2_k, width 5 is
q5_0 / q5_k_m.

create_quant_module read only the digits and let
_GGUF_DEFAULT_BITS_ALIAS_BY_WIDTH choose the type. A mixed-type checkpoint
therefore reloaded every module at the default type for its width and
discarded the requested one.

Measured on a real q3_k/q4_k mixed checkpoint: 7 of 14 modules reloaded as
Q4_0
instead of their assigned type. Silent — no warning, no error.

Fix

Fold the format override back into the structured spec before the module is
constructed, so {"bits": 4, "format": "q4_k"} builds a Q4_K module rather
than a Q4_0 one.

Mirrors the existing FP8 / bitsandbytes override branches a few lines above,
which already resolve their format field the same way.

Test

tests/test_gguf_dynamic_format.py — the cases are chosen so the requested
type is never the default for its width, which is what makes them catch the
bug:

requested width default for that width before after
q4_k 4 q4_0 Q4_0 Q4_K
q4_1 4 q4_0 Q4_0 Q4_1
q2_k 2 q2_0 Q2_0 Q2_K
q5_0 5 q5_k_m Q5_K_M Q5_0

A guard test asserts the table really is non-default, so a future change to the
default mapping cannot silently turn these into no-ops.

5 passed with the fix; 4 of 5 fail on unmodified upstream/main. Ruff clean.

`GGUFConfig` stores a per-module width and ggml type in separate fields,
because one width maps to several types: width 4 is `q4_0`/`q4_1`/`q4_k`,
width 2 is `q2_0`/`q2_k`, width 5 is `q5_0`/`q5_k_m`. `create_quant_module`
read only the digits and let `_GGUF_DEFAULT_BITS_ALIAS_BY_WIDTH` pick the
type, so a mixed-type checkpoint silently reloaded every module at the
*default* type for its width and discarded the requested one.

Fold the `format` override back into the structured spec before the module
is constructed, so `{"bits": 4, "format": "q4_k"}` builds a `Q4_K` module
rather than a `Q4_0` one.

Measured on a q3_k/q4_k mixed checkpoint: 7 of 14 modules reloaded as
`Q4_0` before this change, 0 after.
Resolve dynamic widths and formats independently to match the per-module
quantization config. Inherit the base subtype for width-only overrides
and only the base width for raw format-only overrides.

Use GGUFTorchLinear for regression tests so they run without the optional
llama-cpp-python runtime. Cover config serialization, packed state loading,
explicit formats, inherited formats, unmatched patterns, and exclusions.

Validation: 98 targeted tests passed; Ruff and git diff --check passed.
Four new regression cases fail on the previous PR head.
@Qubitium
Qubitium merged commit 7df75ee into ModelCloud:main Nov 6, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant