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

scripts/reference/gguf_iq_reference/harness.c

A C program that links llama.cpp’s ggml-quants.c, decodes fixed blocks with dequantize_row_* and prints blocks, expected floats and the constant tables.

scripts/reference/gguf_iq_reference/gguf_iq_reference.py

Builds and runs the harness and writes the two Kotlin files below. --crosscheck compares every case with gguf-py, an independent NumPy decoder.

GgufIqTables.kt (skainet-io-gguf, commonMain)

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.

GgufIqDequantReference.kt (skainet-io-gguf, commonTest)

Raw blocks and the floats llama.cpp decodes from them, generated.

GgufIqDequantReferenceTest.kt (skainet-io-gguf, commonTest)

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. fp16 is 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, ib is the group (0 to 7) and l the 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. GgufIqTables stores them in that order: entry i, element j is TABLE[i * entryBytes + j].

  • Signs: bit j of a sign byte negates element j of the sub-group (kmask_iq2xs[j] is 1 shl j). Some formats store the sign byte directly; others store a 7-bit index into ksigns_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

kmask_iq2xs

8

1

every format with sign bits

ksigns_iq2xs

128

1

IQ2_XXS, IQ2_XS, IQ3_XXS

iq2xxs_grid

256

8

IQ2_XXS

iq2xs_grid

512

8

IQ2_XS

iq2s_grid

1024

8

IQ2_S

iq3xxs_grid

256

4

IQ3_XXS

iq3s_grid

512

4

IQ3_S

iq1s_grid

2048

8, signed

IQ1_S, IQ1_M

kvalues_mxfp4

16

1, signed

MXFP4

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

e: the shared exponent (E8M0)

1

16

qs: 32 four-bit codes, two per byte

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

d: fp16 block scale

2

64

qs: 8 bytes per group of 32

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

d: fp16 block scale

2

64

qs: 32 uint16, one per sub-group

66

8

scales: one byte per group

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

d: fp16 block scale

2

32

qs[0..31]: low 8 bits of the grid index, one byte per sub-group

34

32

qs[32..63]: sign byte, one per sub-group (used directly, no ksigns_iq2xs)

66

8

qh: one byte per group, 2 high index bits per sub-group

74

8

scales: one byte per group

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

d: fp16 block scale

2

64

qs[0..63]: grid indexes, two per sub-group (each entry gives 4 values)

66

32

qs[64..95]: one uint32 per group with signs and scale

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

d: fp16 block scale

2

64

qs: low 8 bits of the grid indexes, two per sub-group

66

8

qh: one byte per group, the 9th index bit of its 8 indexes

74

32

signs: one sign byte per sub-group, used directly

106

4

scales: one byte per pair of groups

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

d: fp16 block scale

2

32

qs: low 8 bits of the grid index, one byte per sub-group

34

16

qh: one uint16 per group

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

qs: low 8 bits of the grid index, one byte per sub-group

32

16

qh: one byte per pair of sub-groups

48

8

scales: four uint16, sc[0] to sc[3]

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

random

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.

extremes

One block with all payload bits clear and one with all set.

quantized

What llama.cpp’s own quantizer writes for 512 fixed pseudo-random values.

exponent_limits

MXFP4 only: exponent bytes 0, 1, 2, 126, 127, 128, 254 and 255.

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.

Not covered

llama.cpp at this commit also defines NVFP4 (40), Q1_0 (41), Q2_0 (42) and F8_E4M3 (43). They are outside #1408, GGMLQuantizationType does not list them, and this page does not describe them.