Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions docs/npu.md
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,23 @@ is otherwise served a chat prompt with no BOS: measured on ZR1-1.5B, the forward
| Q4_1 | scale `d`, zero `m`, codes unchanged | bf16 rounding of scale and zero |
| Q4_K | per 32-column sub-block: scale `d*sc`, zero `-dmin*mn`, codes unchanged (a Q4_K super-block is one tile row's 256 columns) | bf16 rounding of scale and zero |
| Q5_K, Q6_K, Q8_0, F16, BF16, F32 | dequantized, then min/max q4_1 per 32 columns | lossy |
| the same, named by `--dtype8` | dequantized, then symmetric int8 per 32 columns (a Q8 chunk: `d*code`, bf16 `d`, codes at `column*32 + row`) | int8 rounding (rel rms ~0.005) |

`pack --dtype8 REGEX` (a Q4NX-name regex, e.g. `'v_proj|down_proj|lm_head'`) stores the
matching tensors as Q8 chunks instead of crushing them to q4_1, and leaves any tensor whose
source was already exact at 4 bits alone (a Q4_K_M mixes types layer by layer). It exists
because q4_1 spends 4 of every 32-value group's bits on a min and has 16 levels: a tensor
that was Q6_K or Q8_0 in the source is both worse and *bigger* after the crush. On
`Zyphra/ZR1-1.5B` Q4_K_M the pattern names 57 tensors and 29 of them are Q6_K (14 `v_proj`,
14 `down_proj`, `lm_head`); the other 28 are Q4_K and stay q4_1. Those 29 as Q8 move the
container's wikitext-2 KLD against BF16 from **0.204038 to 0.129436** (PPL 85.4513 ->
79.7284) for **+180 MiB** (1,431,806,280 -> 1,620,611,528 B), which is better than the
Q4_K_M arm it was packed from (0.138010) but still 1,546 MiB against that arm's 1,066. The
Q8 chunk decodes in `npu/q4nx.cpp` and `npu/forward/src/q4nx_dequant.c`, and
`tools/q4nx_to_gguf.py` writes it back as Q8_0 (the same f16 `d` + int8 `code` blocks), so
a `--dtype8` container is still one model on both sides. The **dx route cannot run it
today**: `q4nx::as_q4_1()` throws for Q8 (8-bit codes do not fit q4_1), so a container
carrying Q8 tensors is for a consumer that reads the chunk kinds, not the narrowed path.

`tools/gguf_to_q4nx.py verify <model.gguf> <out dir>` decodes every tensor it wrote and
compares it with the GGUF, dequantized. For the exact types, every weight must lie
Expand Down
115 changes: 111 additions & 4 deletions tests/gguf_to_q4nx_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@

Q = gguf.GGMLQuantizationType
GOLDEN_Q4_1 = "9f58d4201c4844abc937204c2b7f8d364ff44bc574b0bb87292928c54447d2c8"
# one real Q8 tile (Qwen3.5-4B lm_head tile 0), hashed by the same test as npu/q4nx.cpp: this is
# the layout check that a self-inverse round trip cannot make, because our encoder and decoder
# could agree with each other and still disagree with the container a device serves.
GOLDEN_Q8 = "09ad9e7a61cad612ffcb9cc2a1d7dfd27b6813a6d184329404045d4691c5fdf2"


def golden_tile():
Expand All @@ -55,6 +59,22 @@ def golden_tile():
return ok and same


def golden_tile8():
"""The committed real Q8 tile: decoded by our decoder, and re-encoded by our encoder."""
tile = np.fromfile(os.path.join(HERE, "golden", "q4nx", "q8.bin"), dtype=np.uint8)
assert tile.size == G.Q8, f"q8.bin is {tile.size} B"
w = G.untile_q8(tile.reshape(1, G.Q8), 32, 256).astype(np.float32)
ok = hashlib.sha256(w.tobytes()).hexdigest() == GOLDEN_Q8
print(f"{'ok ' if ok else 'FAIL'} golden q8 tile decodes to the verified SHA-256")
# its parts, back through the writer (scale plane is [g, r]; tiles_q8 wants [r, g])
sc = G.from_bf16(tile[:512].view(np.uint16)).reshape(8, 32).T
codes = np.clip(np.rint(w.reshape(32, 8, 32) / np.where(sc == 0, 1, sc)[..., None]), -127, 127)
again = G.tiles_q8(codes.reshape(32, 256).astype(np.int8), sc)
same = again.tobytes() == tile.tobytes()
print(f"{'ok ' if same else 'FAIL'} golden q8 tile re-encodes to the same bytes")
return ok and same


def q4_k_blocks(rng, N, K):
"""Random but sane Q4_K rows: small positive d, dmin; random 6-bit scales and codes."""
nb = K // 256
Expand Down Expand Up @@ -200,17 +220,26 @@ def moe_check(gguf_path, out, ok):

r = gguf.GGUFReader(gguf_path)
for name, key in ((gate, "blk.0.ffn_gate_exps.weight"),
("model.layers.0.mlp.experts.up_proj.weight", "blk.0.ffn_up_exps.weight"),
("model.layers.0.mlp.experts.down_proj.weight", "blk.0.ffn_down_exps.weight")):
t = next(t for t in r.tensors if t.name == key)
K, N = int(t.shape[0]), int(t.shape[1])
ref = gguf.quants.dequantize(t.data, t.tensor_type).astype(np.float64).reshape(4, N, K)
info = moe["tensors"][name]
bad = 0
chunk = hdr[name]["shape"][-1] # 5120, or 8704 for a --dtype8 stack
worst = 0.0
for e in range(4):
lo = base + info["offsets"][e]
got = G.untile(np.asarray(mm[lo:lo + info["bytes"]]).reshape(-1, G.TILE), N, K)
bad += int((np.abs(got - ref[e]) > 1e-2).sum())
checks.append((f"each of {name}'s 4 offsets decodes to THAT expert", bad == 0))
tiles = np.asarray(mm[lo:lo + info["bytes"]]).reshape(-1, chunk)
got = (G.untile_q8(tiles, N, K) if chunk == G.Q8 else G.untile(tiles, N, K))
# A wrong offset points at another expert (rel rms ~sqrt(2)); the right one is within
# the chunk's own quantisation error (0.08 for q4_1, 0.005 for Q8), so compare
# distributions rather than a max.
rel = float(np.sqrt(((got - ref[e]) ** 2).sum() / (ref[e] ** 2).sum()))
worst = max(worst, rel)
checks.append((f"each of {name}'s 4 offsets decodes to THAT expert "
f"({'Q8' if chunk == G.Q8 else 'q4_1'} chunk, rel rms {worst:.4f})",
worst <= 0.15))
for label, good in checks:
print(f"{'ok ' if good else 'FAIL'} {label}")
ok = ok and good
Expand All @@ -232,8 +261,50 @@ def tokenizer_flags_check(src, out, ok):
return ok


def q8_check(gguf_path, out, ok):
"""--dtype8: the named tensors are 8704-B Q8 chunks, an exactly-4-bit one is left alone, and
the Q8 tile re-encodes byte for byte from the source (so the encoder IS the decoder's inverse)."""
import json
import struct
with open(os.path.join(out, "model.q4nx"), "rb") as f:
n = struct.unpack("<Q", f.read(8))[0]
hdr = json.loads(f.read(n))
base = 8 + n
mm = np.memmap(os.path.join(out, "model.q4nx"), dtype=np.uint8, mode="r")
o_proj = "model.layers.0.self_attn.o_proj.weight" # Q8_0 in the source
up_proj = "model.layers.0.mlp.up_proj.weight" # F32 in the source
v_proj = "model.layers.0.self_attn.v_proj.weight" # Q4_K: exact already, must stay q4_1
checks = [
("--dtype8 writes 8704-B Q8 chunks",
hdr[o_proj]["shape"][-1] == G.Q8 and hdr[up_proj]["shape"][-1] == G.Q8),
("an exact Q4_K tensor named by --dtype8 stays q4_1",
hdr[v_proj]["shape"][-1] == G.TILE),
("the pattern is recorded for a round trip",
hdr["__metadata__"].get("dtype8") == "o_proj|up_proj|v_proj"),
]
r = gguf.GGUFReader(gguf_path)
for name, key in ((o_proj, "blk.0.attn_output.weight"), (up_proj, "blk.0.ffn_up.weight")):
t = next(t for t in r.tensors if t.name == key)
K, N = int(t.shape[0]), int(t.shape[1])
w = gguf.quants.dequantize(t.data, t.tensor_type).astype(np.float32).reshape(N, K)
lo, hi = hdr[name]["data_offsets"]
raw = np.asarray(mm[base + lo:base + hi])
got = G.untile_q8(raw, N, K)
# the source, quantised by the packer's own rule, must give these bytes back
codes, scale = G.quantize_q8(w)
again = G.tiles_q8(codes, scale).tobytes() == raw.tobytes()
rel = float(np.sqrt(((got - w) ** 2).sum() / (w ** 2).sum()))
checks.append((f"{key}: Q8 tile re-encodes to the same bytes", again))
checks.append((f"{key}: 8-bit error rel rms {rel:.4f} (<= 0.01)", rel <= 0.01))
for label, good in checks:
print(f"{'ok ' if good else 'FAIL'} {label}")
ok = ok and good
return ok


def main():
ok = golden_tile()
ok = golden_tile8() and ok
rng = np.random.default_rng(1)
tool = os.path.join(HERE, "..", "tools", "gguf_to_q4nx.py")
with tempfile.TemporaryDirectory() as d:
Expand All @@ -257,7 +328,43 @@ def main():
print(f"FAIL moe {cmd[0]} exited {r.returncode}")
ok = False
ok = moe_check(os.path.join(d, "tiny-moe.gguf"), mout, ok)
# the same MoE container with one expert stack in Q8: __metadata__.moe's offsets must
# follow the 8704-B chunk, not stay on 5120
mq8 = os.path.join(d, "moe-q8")
for cmd in (["pack", os.path.join(d, "tiny-moe.gguf"), mq8, "--dtype8", "experts.up_proj"],
["verify", "--all", os.path.join(d, "tiny-moe.gguf"), mq8]):
r = subprocess.run([sys.executable, tool, *cmd], capture_output=True, text=True)
print(r.stdout, end="")
if r.returncode:
print(r.stderr, end="")
print(f"FAIL moe-q8 {cmd[0]} exited {r.returncode}")
ok = False
ok = moe_check(os.path.join(d, "tiny-moe.gguf"), mq8, ok)
ok = tokenizer_flags_check(src, out, ok)
q8out, q8gguf = os.path.join(d, "q8"), os.path.join(d, "q8.gguf")
for cmd in (["pack", src, q8out, "--dtype8", "o_proj|up_proj|v_proj"],
["verify", "--all", src, q8out]):
r = subprocess.run([sys.executable, tool, *cmd], capture_output=True, text=True)
print(r.stdout, end="")
if r.returncode:
print(r.stderr, end="")
print(f"FAIL q8 {cmd[0]} exited {r.returncode}")
ok = False
ok = q8_check(src, q8out, ok)
# and the inverse: the Q8 container written back as a GGUF, identical weights both ways
inv = os.path.join(HERE, "..", "tools", "q4nx_to_gguf.py")
for cmd in (["write", os.path.join(q8out, "model.q4nx"), src, q8gguf],
["verify", os.path.join(q8out, "model.q4nx"), q8gguf, "--roundtrip", src]):
r = subprocess.run([sys.executable, inv, *cmd], capture_output=True, text=True)
if r.returncode:
print(r.stdout[-2000:], r.stderr[-2000:])
print(f"FAIL q4nx_to_gguf {cmd[0]} exited {r.returncode}")
ok = False
q8w = gguf.GGUFReader(q8gguf)
q8_0 = sum(t.tensor_type == Q.Q8_0 for t in q8w.tensors)
print(f"{'ok ' if q8_0 == 2 else 'FAIL'} the Q8 container writes back as 2 Q8_0 tensors "
f"(got {q8_0})")
ok = ok and q8_0 == 2
print("PASS" if ok else "FAIL")
return 0 if ok else 1

Expand Down
13 changes: 10 additions & 3 deletions tools/check_q4nx_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,17 @@ def check(path, verbose=True):
if k in kinds:
print(f" {k:5s} {CHUNKS_REV[k]:5d} B {kinds[k]:4d} tensor(s)")
if q8:
# Our own converter can say which of the two 8704-B Q8 layouts it wrote: it packs
# code (row, column) at column*32 + row, the one npu/q4nx.cpp decodes, and records
# the --dtype8 pattern that selected the tensors.
own = table.get("__metadata__", {}).get("converter") == "tools/gguf_to_q4nx.py"
how = ("packed by tools/gguf_to_q4nx.py --dtype8, so they use the layout "
"npu/q4nx.cpp decodes (code at column*32 + row)" if own else
"two code layouts share the 8704-byte chunk and the container does not say "
"which (npu/q4nx.h) - do not dequantize these with the q4_1-style decoder "
"unless the model is verified")
print(f" Q8: {len(q8)} tensor(s) ({', '.join(q8[:3])}{', ...' if len(q8) > 3 else ''}): "
"two code layouts share the 8704-byte chunk and the container does not say which "
"(npu/q4nx.h) - do not dequantize these with the q4_1-style decoder unless the "
"model is verified")
f"{how}")
if unscanned:
print(f" {unscanned} tensor(s) of an uncounted dtype (not quantized weights)")
return errors, {"kinds": kinds, "tensors": sum(kinds.values()), "q8": len(q8), "bytes": chunk_bytes}
Expand Down
Loading
Loading