GGUF block layouts: IQ1, IQ2, IQ3 and MXFP4
This page records how llama.cpp stores and decodes the eight GGUF tensor types of
#1408: MXFP4, IQ2_XXS,
IQ2_XS, IQ2_S, IQ3_XXS, IQ3_S, IQ1_S and IQ1_M.
DequantOps decodes MXFP4, IQ2_XXS, IQ2_XS, IQ2_S, IQ3_XXS and IQ3_S. The two IQ1
types are not decoded yet; for them this page is the input for the decoders, not a description
of code that exists.
The source is ggml/src/ggml-common.h (block structs and tables) and
ggml/src/ggml-quants.c (dequantize_row_*) of llama.cpp at commit
074f1f3f730c1d17f34645d62c1859c74bedbdd8 (2026-11-26).
What is in the repository
| File | Content |
|---|---|
|
A C program that links llama.cpp’s |
|
Builds and runs the harness and writes the two Kotlin files below. |
|
The grids, the sign table and the value tables, generated. The MXFP4, IQ2 and IQ3 decoders read their tables from here; the IQ1 grid waits for its decoder. |
|
Raw blocks and the floats llama.cpp decodes from them, generated. |
|
Holds every implemented type to the reference bit for bit, and lists the types that are still pending. |
To regenerate after a llama.cpp change:
git clone --depth 1 https://github.com/ggml-org/llama.cpp /path/to/llama.cpp
python3 scripts/reference/gguf_iq_reference/gguf_iq_reference.py --llama-cpp /path/to/llama.cpp --crosscheck
Common rules
-
All multi-byte fields are little-endian.
fp16is an IEEE half-precision float. -
The K-family block holds 256 elements (
QK_K), split into 8 groups of 32 and each group into 4 sub-groups of 8. Below,ibis the group (0 to 7) andlthe sub-group (0 to 3). -
A grid entry is 8 (or 4) small values packed into one
uint64_t(uint32_t) literal. The decoder reads them as bytes, lowest byte first.GgufIqTablesstores them in that order: entryi, elementjisTABLE[i * entryBytes + j]. -
Signs: bit
jof a sign byte negates elementjof the sub-group (kmask_iq2xs[j]is1 shl j). Some formats store the sign byte directly; others store a 7-bit index intoksigns_iq2xs, whose 8th bit makes the number of minus signs even. -
The result must equal llama.cpp bit for bit. Compute in single precision and keep the order of the operations given below; do not reorder or widen to double.
Tables
| Table | Entries | Bytes per entry | Used by |
|---|---|---|---|
|
8 |
1 |
every format with sign bits |
|
128 |
1 |
|
|
256 |
8 |
|
|
512 |
8 |
|
|
1024 |
8 |
|
|
256 |
4 |
|
|
512 |
4 |
|
|
2048 |
8, signed |
|
|
16 |
1, signed |
|
iq1s_grid holds -1, 0 and 1 and must be read as signed bytes. All other grids are unsigned.
llama.cpp also has ksigns64 and iq1s_grid_gpu; they are other encodings of the same data
for SIMD and GPU code and are not needed for decoding.
MXFP4
17 bytes for 32 elements (4.25 bits per weight).
| Offset | Bytes | Field |
|---|---|---|
0 |
1 |
|
1 |
16 |
|
d = 2^(e - 128) for e >= 2: the float with bit pattern (e - 1) shl 23 d = float with bits 0x00200000 shl e for e = 0 and e = 1 (subnormal: 2^-128 and 2^-127) y[j] = kvalues_mxfp4[qs[j] and 0x0F] * d j = 0 .. 15 y[16 + j] = kvalues_mxfp4[qs[j] ushr 4] * d
The low nibbles fill the first half of the block and the high nibbles the second half; the
codes are not interleaved. kvalues_mxfp4 holds twice the E2M1 value, which is why d is
half of the E8M0 scale 2^(e - 127). With e = 255 the non-zero values overflow to infinity;
llama.cpp does not treat any exponent as NaN.
IQ2_XXS
66 bytes for 256 elements (2.0625 bits per weight).
| Offset | Bytes | Field |
|---|---|---|
0 |
2 |
|
2 |
64 |
|
Per group ib, read the 8 bytes as two uint32, a0 and a1:
index of sub-group l = byte l of a0 (into iq2xxs_grid) signs of sub-group l = ksigns_iq2xs[(a1 ushr 7*l) and 127] s = a1 ushr 28 db = d * (0.5f + s) * 0.25f y[j] = db * grid[j] * (sign bit j ? -1 : 1)
IQ2_XS
74 bytes for 256 elements (2.3125 bits per weight).
| Offset | Bytes | Field |
|---|---|---|
0 |
2 |
|
2 |
64 |
|
66 |
8 |
|
q = qs[4*ib + l] index = q and 511 (into iq2xs_grid) signs = ksigns_iq2xs[q ushr 9] s = scales[ib] and 0x0F for l = 0, 1; scales[ib] ushr 4 for l = 2, 3 db = d * (0.5f + s) * 0.25f y[j] = db * grid[j] * (sign bit j ? -1 : 1)
IQ2_S
82 bytes for 256 elements (2.5625 bits per weight).
| Offset | Bytes | Field |
|---|---|---|
0 |
2 |
|
2 |
32 |
|
34 |
32 |
|
66 |
8 |
|
74 |
8 |
|
index = qs[4*ib + l] or (((qh[ib] ushr 2*l) and 3) shl 8) (into iq2s_grid) signs = qs[32 + 4*ib + l] s, db and y[j] as in IQ2_XS
IQ3_XXS
98 bytes for 256 elements (3.0625 bits per weight).
| Offset | Bytes | Field |
|---|---|---|
0 |
2 |
|
2 |
64 |
|
66 |
32 |
|
Per group ib, with a the uint32 at 66 + 4*ib:
grid1 = iq3xxs_grid[qs[8*ib + 2*l]] elements 0 .. 3 of the sub-group grid2 = iq3xxs_grid[qs[8*ib + 2*l + 1]] elements 4 .. 7 signs = ksigns_iq2xs[(a ushr 7*l) and 127] s = a ushr 28 db = d * (0.5f + s) * 0.5f y[j] = db * grid[j] * (sign bit j ? -1 : 1)
Note the final factor: 0.5f here, 0.25f in the IQ2 formats.
IQ3_S
110 bytes for 256 elements (3.4375 bits per weight).
| Offset | Bytes | Field |
|---|---|---|
0 |
2 |
|
2 |
64 |
|
66 |
8 |
|
74 |
32 |
|
106 |
4 |
|
index1 = qs[8*ib + 2*l] or (((qh[ib] ushr 2*l) and 1) shl 8) (into iq3s_grid) index2 = qs[8*ib + 2*l + 1] or (((qh[ib] ushr 2*l + 1) and 1) shl 8) signs = signs[4*ib + l] s = scales[ib / 2] and 0x0F for even ib; scales[ib / 2] ushr 4 for odd ib db = d * (1 + 2*s) y[j] = db * grid[j] * (sign bit j ? -1 : 1)
1 + 2*s is an integer that is converted to float before the multiplication.
IQ1_S
50 bytes for 256 elements (1.5625 bits per weight).
| Offset | Bytes | Field |
|---|---|---|
0 |
2 |
|
2 |
32 |
|
34 |
16 |
|
h = qh[ib] index = qs[4*ib + l] or (((h ushr 3*l) and 7) shl 8) (into iq1s_grid, signed) s = (h ushr 12) and 7 delta = -0.125f if bit 15 of h is set, else 0.125f dl = d * (2*s + 1) y[j] = dl * (grid[j] + delta)
There are no sign bits: the grid values are signed and delta shifts the whole group.
IQ1_M
56 bytes for 256 elements (1.75 bits per weight). The block has no d field.
| Offset | Bytes | Field |
|---|---|---|
0 |
32 |
|
32 |
16 |
|
48 |
8 |
|
The fp16 block scale is spread over the top nibbles of the four uint16:
bits = (sc[0] ushr 12) or ((sc[1] ushr 8) and 0x00F0) or ((sc[2] ushr 4) and 0x0F00) or (sc[3] and 0xF000) d = fp16(bits)
The remaining 12 bits of each sc hold four 3-bit scales, two per group:
s1 = (sc[ib / 2] ushr (6*(ib % 2))) and 7 sub-groups 0 and 1 s2 = (sc[ib / 2] ushr (6*(ib % 2) + 3)) and 7 sub-groups 2 and 3 dl1 = d * (2*s1 + 1) dl2 = d * (2*s2 + 1)
qh[2*ib] serves sub-groups 0 and 1, qh[2*ib + 1] sub-groups 2 and 3. In each byte the low
nibble belongs to the first sub-group of the pair and the high nibble to the second:
index = qs[4*ib + l] or ((nibble and 7) shl 8) (into iq1s_grid, signed) delta = -0.125f if bit 3 of the nibble is set, else 0.125f y[j] = dl * (grid[j] + delta)
Reference cases
GgufIqDequantReference holds these cases for every type. The expected floats come from
dequantize_row_*, compiled without fast-math and without FMA contraction.
| Case | Content |
|---|---|
|
Four blocks of arbitrary bytes, so every index, sign and scale field takes arbitrary values. Only the block scale is replaced by a finite one. |
|
One block with all payload bits clear and one with all set. |
|
What llama.cpp’s own quantizer writes for 512 fixed pseudo-random values. |
|
|
The fixture also covers IQ4_NL and IQ4_XS, which DequantOps already decodes; both
match llama.cpp bit for bit.
No case contains a NaN. A block scale that is itself NaN or infinite is not covered: the result then depends on NaN payload propagation, which differs between platforms.