Skip to content

core: backend-neutral EXL3 trellis reference decoder with an oracle fixture - #496

Merged
ashhart merged 1 commit into
ashhart:zig-flashnextfrom
akol1:exl3-format-zig
Oct 10, 2026
Merged

ashhart merged 1 commit into
ashhart:zig-flashnextfrom
akol1:exl3-format-zig

Conversation

@akol1

@akol1 akol1 commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Summary

A backend-neutral reference decoder for the EXL3 trellis format at
zig/src/core/exl3_format.zig: the circular bitstream (E(p) ends with wrapping
16-bit windows, alternating KA/KA+1 for the mul1 half widths), the 3inst/mcg/mul1
codebooks with their single fp16 rounding, tile placement, W_q unpack, su/sv sign
expansion, and the Hadamard-rotated dequantize. Written one bit at a time so it
reads exactly like docs/recipes/exl3.md; the fast per-backend tile decoders
(CUDA fragments today, Metal simdgroup mma per #494) are the follow-up this
module gates and the correctness oracle they are checked against.

The fixture

zig/tests/fixtures/exl3_oracle.bin (371,478 bytes) carries:

  • 27 codebook x width records (11 widths, half steps mul1-only) of random trellis
    tiles with the numpy decoder's exact 16-bit states — 162 tiles, 41,472 state
    comparisons, all bit-exact
  • 384 codebook spot values (every 512th state, fp16 bit patterns exact)
  • one complete 128x128 Hadamard block of a real group from
    UnstableLlama/Qwen3.5-2B-exl3-8.00bpw (mcg codebook, 8.00 bpw,
    layers.3.self_attn.q_proj, the 2B geometry the native Metal engine pins):
    trellis words, suh/svh scales, the expected W_q (bit-exact) and the float64
    dequantized block

Tests

zig test zig/src/core/exl3_format.zig from the repository root (the working
directory every zig build test step runs from):

  1. the reference decoder reproduces the numpy oracle on every codebook and width
  2. a real mcg 8.00 bpw block unpacks and dequantizes to the oracle's values —
    W_q bit-exact; the dequantize compares within a few ULP because the Hadamard
    sums are exact f64 products whose order BLAS and this loop may choose
    differently (the tolerance is 1e-9 * (1 + max|want|), measured headroom is
    far below it)

Both pass on Linux x86-64 with Zig 0.17.0 (the pinned .zig-version); the
module is pure std and touches no Metal or CUDA code.

Relationship to #494 and #490

This is the first slice of #494: the oracle a Metal trellis decoder must pass
tile by tile, landed where both backends can consume it. #490's HIP draft ships
its own EXL3 machinery inside the HIP backend; this module is the shared,
backend-neutral piece that the CUDA and Metal readers can both verify against.
The doc pointer in docs/recipes/exl3.md says where it lives and how to run it.

Receipt

  • Environment: zig-flashnext @ 1.0.2 lineage (f8fe17d), Zig 0.17.0, Linux
    x86-64, no GPU involved.
  • Exactness: the fixture comparisons above; no engine arithmetic touched.
  • Platforms: none affected (new standalone module + fixture + a doc paragraph).
  • One operational find from building the fixture: plain HTTP range requests
    against xet-backed Hugging Face repositories return wrong bytes outside the
    first chunk (the reassembled chunks do not map linearly to file offsets), so
    EXL3 fixture pulls must go through huggingface_hub's downloader. Worth a
    line in whichever doc covers checkpoint fixtures.

…ixture

The format reader the CUDA and Metal trellis decoders are checked against:
the circular bitstream (E(p) ends, wrapping 16-bit windows), the 3inst/mcg/mul1
codebooks with their single fp16 rounding, tile placement, W_q unpack, su/sv
sign expansion and the Hadamard-rotated dequantize. Written one bit at a time
so it reads like docs/recipes/exl3.md; the fast per-backend kernels are the
follow-up this module gates.

The fixture (zig/tests/fixtures/exl3_oracle.bin) carries 27 codebook x width
records of random tiles with the numpy decoder's exact states, codebook spot
values, and one complete 128x128 Hadamard block of a real mcg @ 8.00 bpw group
(Qwen3.5-2B geometry, self_attn.q_proj) with its scales and expected W_q /
float64 dequantize. The tests reproduce all of it: states and W_q bit-exactly,
dequantize within a few ULP (the Hadamard sums are exact products in f64; BLAS
and this loop may order them differently). Run from the repository root:
zig test zig/src/core/exl3_format.zig
@ashhart

ashhart commented Oct 10, 2026

Copy link
Copy Markdown
Owner

Thank you, @akol1. A one-bit-at-a-time EXL3 reference with a fixture tied to the numpy decoder is a good oracle to have. This is merged into main (85aa565), with one fix of ours (5daebf6): unpack() used tiles_k * 16 as the row stride where it needs tiles_n * 16. The fixture's 128 by 128 block hid it, but any layer with different input and output sizes decoded rows into the wrong places, so a rectangular test now covers it, and the multi-line comments are one line each. 23e988d names the fixture's source block in the EXL3 recipe. Nothing in the engine loads EXL3 yet, so this is the oracle for the readers in #494.

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.

2 participants