Skip to content

gguf: Q2_0, mainline ggml's type 42, is a named type with its sizes and a port of dequantize_row_q2_0 - #67

Merged
midagedev merged 1 commit into
midagedev:mainfrom
juliankang4:gguf-q2-0
Oct 12, 2026
Merged

midagedev merged 1 commit into
midagedev:mainfrom
juliankang4:gguf-q2-0

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

What and why

This is part 1 of the two Q2_0 pull requests agreed on #48: the type, its block size, its type size and its dequant,
with a FAIL-first on today's open error for type 42 and a synthetic-block test against mainline's
dequantize_row_q2_0 (ik has no Q2_0). A file of Q2_0 tensors now opens instead of stopping at ggml type ggml-type-42 has no size in this build's table. Part 2, the AVX2 host dot in crates/qdot, follows after this one.
It does not close #48: the card kernels and the Qwen3.8 body work you named there stay open.

Q2_0 is 64 values in 18 bytes: an f16 scale d, then 16 bytes of 2-bit codes. A code byte holds four values, the low
pair first, and codes 0..3 decode to -1, 0, +1, +2 times d.

  • GgmlType::Q2_0 (id 42) with its name, block size 64 and type size 18. The strict open and ggml_type_info size a
    named type from GgmlType, so the variant is the whole loader change; strict_tensors and 0.3.1's
    FILE_TABLE_IDS are untouched.
  • dequant_row decodes it with a port of dequantize_row_q2_0 (llama.cpp f2cc7282c, ggml-quants.c:439). Its one
    product, an f16 times an integer of magnitude at most 2, is exact in f32.
  • No CPU matmul takes Q2_0 yet (mainline pairs it with Q8_0 activations), so activation_format refuses it by name,
    as it does BF16. CardFormat::of and the six card kernel tables give it None, so a Q2_0 tensor the plan puts on a
    card is still refused by name.
  • tools/hf-arch-check.py's mirror of from_u32 names 42.
  • UNKNOWN_IDS trades 42 for 43, the first id past mainline's table, and its doc now says what 40, 41 and 99 are in
    the two tables instead of calling them ids past both.

The test reference is mainline's own decode. Q2_0_MAINLINE is dequantize_row_q2_0 of llama.cpp f2cc7282c over
the test's four blocks at d = 1.0, printed by a short C program linked against that build's libggml-base.so
through ggml_get_type_traits(GGML_TYPE_Q2_0)->to_float (the program and its output are below). The same library
answers q2_0, 64 and 18 for GGML_TABLE's new row. The four blocks' code bytes are chosen so that every position
holds each of the four codes once across the blocks. The test then gives the four blocks four different scales a pass,
rotated through nine (1.0, subnormals, a negative, zero, negative zero and the largest finite among them). In all nine
passes mainline's output equals the d = 1.0 value times its block's d, bit for bit and signed zeros included,
which is what the test computes.

Code shape

  • No second copy.
  • Model code holds only architecture facts. No model file is touched.
  • Dead code is gone.
  • No silent failure. A short row or a row that is not whole blocks is refused by name (ShortSrc,
    UnalignedDst), and the test holds both.
  • Comments state what is true now.
  • No relaxed gate. UNKNOWN_IDS loses 42 because the enum names it now; the line carries
    PIN(2026-10-12): as the q2_K line before it does.
  • cargo fmt and cargo clippy are clean on what I touched; the lint count did not go up (41, the ratchet).

Duplication found or removed (path:line), or "none": none

What I ran

Base 697790c4f, the 0.3.1 tag plus one README commit. Main has since moved by documentation commits only (README, docs/performance.md).

FAIL-first: the new inventory case alone on the base, on x86_64 Linux:

$ cargo test -p bloomery-gguf --test inventory
test inventory_agrees_with_the_strict_reader_on_supported_types ... FAILED
strict open of a supported type: tensor "blk.0.ffn_down.weight": ggml type ggml-type-42 has no size in this build's table
test result: FAILED. 4 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out

q2_0_matches_mainline against three dequant mutants (x86_64 Linux):

codes read high pair first:           FAILED  d 0x3c00 value 0: got 2e0, want -1e0
(q - 2) * d instead of (q - 1) * d:   FAILED  d 0x3c00 value 0: got -2e0, want -1e0
every block at the first block's d:   FAILED  d 0x0001 value 66: got 1e0, want 5.9604645e-8

With the change, x86_64 Linux:

$ cargo test -p bloomery-gguf -p bloomery-placement
gguf lib:   test result: ok. 26 passed; 0 failed
inventory:  test result: ok. 5 passed; 0 failed
placement:  test result: ok. 119 passed; 0 failed
$ just contrib-check --tier t0
DONE tier=t0 total=66 red=0 wall=741s

aarch64 macOS:

$ just mac-static upstream/main
mac-static: fmt-check rc 0 in 10 s
mac-static: lint rc 0 in 79 s
mac-static: combos rc 0 in 111 s
mac-static: check-recipes rc 0 in 583 s
mac-static: ok

The lint count is 41, the ratchet's, and the combos step ran 35 shapes with none red.

The gguf lib tests do not build on macOS (libc::RUSAGE_THREAD), so they ran on Linux only.

I could not run the box gates. just affected 697790c4 --no-box selects 145 of 150 gate recipes, since every GPU gate
bin links bloomery-gguf and the CPU engine's gates reach bloomery-placement through bloomery-model; those are yours to run. No kernel or
CPU path changes for an existing type: the new match arms are Q2_0's alone, and a Q2_0 tensor is refused before any
card or CPU kernel sees it.

The mainline reference program and its output

Linked against libggml-base.so of a llama.cpp f2cc7282c Release build (-DGGML_CUDA=ON; the program uses only
ggml-base), GCC 15.2.0, x86_64 Ubuntu 26.04:

cc -O2 -o q2_0_ref q2_0_ref.c -I$LCPP/ggml/include -L$LCPP/build/bin -lggml-base -Wl,-rpath,$LCPP/build/bin
// Mainline ggml's Q2_0 decode over the blocks bloomery's q2_0 test builds.
// Block i (0..4), code byte j (0..16) = (j | (15 - j) << 4) ^ (0x55 * i), scale d[i].
// Prints ggml's (name, blck_size, type_size) for type 42, then the to_float
// output with every block at d = 1.0 (f16 0x3c00) as integers, then for each pass s
// (block i at scale s + i of the nine, mod 9) checks that every output's bits equal
// (float)((double)L * (double)d[i]), L the d = 1.0 value.
//
// cc -O2 -o q2_0_ref q2_0_ref.c -I$LCPP/ggml/include -L$LCPP/build/bin -lggml-base -Wl,-rpath,$LCPP/build/bin
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include "ggml.h"

enum { NB = 4, QK = 64, BS = 18 };

static void blocks(uint8_t * buf, const uint16_t * d) {
    for (int i = 0; i < NB; i++) {
        uint8_t * b = buf + i * BS;
        memcpy(b, &d[i], 2);
        for (int j = 0; j < 16; j++) {
            b[2 + j] = (uint8_t) ((j | ((15 - j) << 4)) ^ (0x55 * i));
        }
    }
}

int main(void) {
    const enum ggml_type t = (enum ggml_type) 42;
    printf("type 42: name %s blck_size %lld type_size %zu\n", ggml_type_name(t),
           (long long) ggml_blck_size(t), ggml_type_size(t));
    const struct ggml_type_traits * tr = ggml_get_type_traits(t);
    uint8_t buf[NB * BS];
    float one[NB * QK], y[NB * QK];

    const uint16_t ones[NB] = {0x3c00, 0x3c00, 0x3c00, 0x3c00};
    blocks(buf, ones);
    tr->to_float(buf, one, NB * QK);
    printf("d = 0x3c00:");
    for (int p = 0; p < NB * QK; p++) {
        printf("%s%d", p % 32 == 0 ? "\n" : ", ", (int) one[p]);
        if ((float) (int) one[p] != one[p]) {
            printf("\nnot an integer at %d\n", p);
            return 1;
        }
    }
    printf("\n");

    const uint16_t scales[] = {0x3c00, 0x0001, 0x03ff, 0x0400, 0xb555, 0x0000, 0x8000, 0x7bff, 0xfbff};
    const size_t ns = sizeof scales / sizeof scales[0];
    int bad = 0;
    for (size_t s = 0; s < ns; s++) {
        uint16_t ds[NB];
        for (int i = 0; i < NB; i++) ds[i] = scales[(s + i) % ns];
        blocks(buf, ds);
        tr->to_float(buf, y, NB * QK);
        int n = 0;
        for (int p = 0; p < NB * QK; p++) {
            const float want = (float) ((double) one[p] * (double) ggml_fp16_to_fp32(ds[p / QK]));
            uint32_t a, b;
            memcpy(&a, &y[p], 4);
            memcpy(&b, &want, 4);
            n += a != b;
        }
        printf("pass %zu (d 0x%04x 0x%04x 0x%04x 0x%04x): %d of %d values differ from L * d\n", s, ds[0], ds[1], ds[2], ds[3], n, NB * QK);
        bad += n;
    }
    return bad != 0;
}

Output:

type 42: name q2_0 blck_size 64 type_size 18
d = 0x3c00:
-1, -1, 2, 2, 0, -1, 1, 2, 1, -1, 0, 2, 2, -1, -1, 2, -1, 0, 2, 1, 0, 0, 1, 1, 1, 0, 0, 1, 2, 0, -1, 1
-1, 1, 2, 0, 0, 1, 1, 0, 1, 1, 0, 0, 2, 1, -1, 0, -1, 2, 2, -1, 0, 2, 1, -1, 1, 2, 0, -1, 2, 2, -1, -1
0, 0, 1, 1, -1, 0, 2, 1, 2, 0, -1, 1, 1, 0, 0, 1, 0, -1, 1, 2, -1, -1, 2, 2, 2, -1, -1, 2, 1, -1, 0, 2
0, 2, 1, -1, -1, 2, 2, -1, 2, 2, -1, -1, 1, 2, 0, -1, 0, 1, 1, 0, -1, 1, 2, 0, 2, 1, -1, 0, 1, 1, 0, 0
1, 1, 0, 0, 2, 1, -1, 0, -1, 1, 2, 0, 0, 1, 1, 0, 1, 2, 0, -1, 2, 2, -1, -1, -1, 2, 2, -1, 0, 2, 1, -1
1, -1, 0, 2, 2, -1, -1, 2, -1, -1, 2, 2, 0, -1, 1, 2, 1, 0, 0, 1, 2, 0, -1, 1, -1, 0, 2, 1, 0, 0, 1, 1
2, 2, -1, -1, 1, 2, 0, -1, 0, 2, 1, -1, -1, 2, 2, -1, 2, 1, -1, 0, 1, 1, 0, 0, 0, 1, 1, 0, -1, 1, 2, 0
2, 0, -1, 1, 1, 0, 0, 1, 0, 0, 1, 1, -1, 0, 2, 1, 2, -1, -1, 2, 1, -1, 0, 2, 0, -1, 1, 2, -1, -1, 2, 2
pass 0 (d 0x3c00 0x0001 0x03ff 0x0400): 0 of 256 values differ from L * d
pass 1 (d 0x0001 0x03ff 0x0400 0xb555): 0 of 256 values differ from L * d
pass 2 (d 0x03ff 0x0400 0xb555 0x0000): 0 of 256 values differ from L * d
pass 3 (d 0x0400 0xb555 0x0000 0x8000): 0 of 256 values differ from L * d
pass 4 (d 0xb555 0x0000 0x8000 0x7bff): 0 of 256 values differ from L * d
pass 5 (d 0x0000 0x8000 0x7bff 0xfbff): 0 of 256 values differ from L * d
pass 6 (d 0x8000 0x7bff 0xfbff 0x3c00): 0 of 256 values differ from L * d
pass 7 (d 0x7bff 0xfbff 0x3c00 0x0001): 0 of 256 values differ from L * d
pass 8 (d 0xfbff 0x3c00 0x0001 0x03ff): 0 of 256 values differ from L * d

Hardware (if the change is for a machine)

Not for a machine. The tests ran on x86_64 Linux (AMD Ryzen 9 9950X, Ubuntu 26.04.1, kernel 7.0, glibc 2.43,
nightly-2026-08-28) and the static tier on an aarch64 Mac.

…ale and 2-bit codes, (q - 1) * d), is a named type with its block size, its type size and a dequant_row port of mainline's dequantize_row_q2_0, so a file of Q2_0 tensors opens instead of stopping at "ggml type ggml-type-42 has no size in this build's table" (midagedev#48)

The strict open and ggml_type_info size a named type from GgmlType, so the variant is the whole
loader change; strict_tensors is untouched. A code byte holds four values, the low pair first:
value j is bits 2 * (j % 4) of qs[j / 4], and codes 0..3 decode to -1, 0, +1, +2 times d. No CPU
matmul takes Q2_0 here yet (mainline pairs it with Q8_0 activations), so activation_format refuses
it by name, as it does BF16; CardFormat::of and the six card kernel tables give it None, so a Q2_0
tensor that the plan puts on a card is still refused by name. tools/hf-arch-check.py's mirror of
from_u32 names 42.

The reference is mainline's own decode, since ik has no Q2_0. Q2_0_MAINLINE is
dequantize_row_q2_0 of llama.cpp f2cc7282c over the test's four blocks at d = 1.0, printed on an
x86_64 Ubuntu 26.04 machine by a short C program linked against that build's libggml-base.so,
through ggml_get_type_traits(GGML_TYPE_Q2_0)->to_float; the same library answers q2_0, 64 and 18
for GGML_TABLE's new row. The test then gives the four blocks four different scales a pass,
rotated through nine (1.0, subnormal, negative, zero, negative zero and largest finite among
them), and in all nine passes mainline's output equals the d = 1.0 value times its block's d bit
for bit, signed zeros included, which is what the test computes. UNKNOWN_IDS trades 42 for 43,
the first id past mainline's table, and its doc now says what 40, 41 and 99 are in the two
tables instead of calling them ids past both.

FAIL-first: with the new inventory case and 697790c's gguf source, the strict open of the Q2_0
file is red with `strict open of a supported type: tensor "blk.0.ffn_down.weight": ggml type
ggml-type-42 has no size in this build's table`, on x86_64 Linux. q2_0_matches_mainline is red
on three dequant mutants: the codes read high pair first ("d 0x3c00 value 0: got 2e0, want
-1e0"), (q - 2) * d ("got -2e0, want -1e0"), and every block decoded with the first block's
scale ("d 0x0001 value 66: got 1e0, want 5.9604645e-8").

On x86_64 Linux, cargo test -p bloomery-gguf -p bloomery-placement passes (gguf lib 26,
inventory 5, placement 119), and just contrib-check --tier t0 has 66 of 66 units green. On
aarch64 macOS, just mac-static 697790c passes (check-recipes green, lint at the ratchet's 41).
The gguf lib tests do not build on macOS, for libc::RUSAGE_THREAD.
@midagedev

Copy link
Copy Markdown
Owner

Thank you. This is part 1 as agreed on #48. Review has started. The PR touches crates/placement/src/placement.rs, and the 0.3.2 train also changes that file, so the merge may need a rebase after the train lands. I will tell you if it does.

@midagedev

Copy link
Copy Markdown
Owner

Thanks. Review found no defect. dequant_q2_0 matches dequantize_row_q2_0 at f2cc7282c: we rebuilt Q2_0_MAINLINE from that source and it is equal. The line citations are right.

This goes into the 0.3.2 train now, as a merge of your commit, and lands when the train's gates are green. You do not need to change anything. We will add two small things on main afterwards:

  1. a test that pins activation_format(Q2_0) as refused by name;
  2. a note that has_dequant(Q2_0) lets a Q2_0 token_embd table load (host decode, no kernel).

@midagedev
midagedev merged commit 754e9a8 into midagedev:main Oct 12, 2026
1 check passed
@juliankang4 juliankang4 added model The CPU engine and model files: crates/model, models, hf, gguf, tokenizer, jinja placement Placement planning: crates/placement tools Tools, recipes, CI and the toolchain: tools/, justfile, .github/, Cargo labels Oct 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

model The CPU engine and model files: crates/model, models, hf, gguf, tokenizer, jinja placement Placement planning: crates/placement tools Tools, recipes, CI and the toolchain: tools/, justfile, .github/, Cargo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

gguf: Q2_0, where the ISTA GSQ-RCO Qwen3.8 files stop today

2 participants