Comptime-Specialized .safetensors Quantization in Zig (0.17).
gpt-oss-20b (openai/gpt-oss-20b, 12.8 GiB, MXFP4 experts) on Ryzen 5 7600X:
| out dtype | 1t | 6t | 12t | file GB/s (6t) |
|---|---|---|---|---|
| nvfp4 | 6490 ms | 1090 ms | 880 ms | 12.6 |
| mxfp4 | 850 ms | 250 ms | 240 ms | 55.0 |
| i4 | 3910 ms | 660 ms | 580 ms | 20.9 |
| f8e4m3 | 3380 ms | 580 ms | 530 ms | 23.7 |
| f16 | 1410 ms | 280 ms | 270 ms | 49.1 |
| bf16 | 3120 ms | 560 ms | 520 ms | 24.6 |
| f32 | 1140 ms | 240 ms | 320 ms | 57.3 |
File GB/s divides total shard size by wall time. For mxfp4 output, MXFP4 experts pass through and BF16 tensors are quantized.
zig build -Doptimize=ReleaseFast -Dcpu=native
./zig-out/bin/quant ./models/gpt-oss-20b/model*.safetensors f32 8Recognized tensor layouts (src/safetensors.zig, HEADERS):
| scheme | weight | scale | global scale |
|---|---|---|---|
| NVFP4 (1) | U8 | F8_E4M3 | F32 |
| NVFP4 (2) | U8 | F8_E4M3 | F32 (inv) |
| MXFP4 (3) | U8 | U8 (E8M0) | none |
| INT4 W4A16 (2) | I32 | BF16 | none |
| FP8 (1)(2) | F8_E4M3 | F32 (scalar) | none |
| plain | F32 / F16 / BF16 / F8_E4M3 | none | none |
main
parseJson single threaded, every safetensors shard
sort by weights per layer, Desc
spawn N workers
worker
incremental atomic counter as shared idx into sorted layers
out -> `doNotOptimizeAway()`
quant block
unpack lo/hi shuffle
decode in_t -> f32
scaleIn in_t -> f32 lookup translate
translate f32 RTN | std-affine
scaleOut f32 -> out scales scaled output only
encode f32 -> out_t
pack lo/hi shuffle
The quantization pipeline is modular through types.Dtype.RECIPE and types.Scheme.RECIPE which holds type recipies. All threads reuse their buffer, kept via doNotOptimizeAway.
zig build testquant: nvfp4,quant: mxfp4: all 16 E2M1 x 2 scales, bit-exact for f32 / f16 / bf16 (-0.0included)safetensors: parseJson: scale and global scale merged into the weight layer.
Setup: Ryzen 5 7600X (Zen 4, 6C/12T), DDR5, Linux 6.18, boost on, EPP balance_performance, zig 0.17. Files warm in page cache. Minimum of three runs across all shards.
hf download openai/gpt-oss-20b --include "model-*.safetensors" --local-dir models/gpt-oss-20b
./bench/wallmodel.sh models/gpt-oss-20b/model-*.safetensors./bench/target.shgpt-oss-20b walltime
| target | ISA | gpt f32 | gpt f16 | gpt bf16 | kernel nvfp4 to f32 ns/w |
|---|---|---|---|---|---|
| x86_64_v1 | SSE2 | 6060 ms | 100590 ms | 15800 ms | 0.3946 |
| x86_64_v2 | SSE4.2 | 6110 ms | 101380 ms | 13350 ms | 0.3861 |
| x86_64_v3 | AVX2+F16C | 1870 ms | 2290 ms | 4940 ms | 0.0850 |
| x86_64_v4 | AVX-512 | 1540 ms | 1690 ms | 3520 ms | 0.0691 |
| native | znver4 | 1160 ms | 1430 ms | 3130 ms | 0.0494 |
gpt: min of 3 pinned 1t runs (10 s budget). Kernel: median of 3 x 10 s trials.
./bench/mca.sh # summary, all pairs, all targets
./bench/mca.sh --full # one row per (pair, target)cycles per weights (llvm-mca)
| target | ISA | float+scaled | integer+scaled | float | native |
|---|---|---|---|---|---|
| x86_64_v1 | SSE2 | 3.590* | 6.084* | 2.567* | 1.673* |
| x86_64_v2 | SSE4.2 | 2.904* | 3.053* | 2.087* | 1.682* |
| x86_64_v3 | AVX2+F16C | 1.584* | 1.496 | 1.206 | 0.475 |
| x86_64_v4 | AVX-512 | 1.164* | 1.053 | 0.910 | 0.367 |
| native | znver4 | 0.938* | 0.619 | 0.876 | 0.377 |
geomean per class (out Dtype.Kind, +scaled if out scale). *: libcall as 1 inst or trip count not parsed.
mxfp4: same as input scheme, experts pass through.
./bench/matrix.sh L1: 3 x 1 s trials per conversion (~2.5 min). Diagonal: in == out, weights and scales copied.
Scaling:
- Reading from userspace cap at 63 GB/s, reached with 3 threads. Then copies start competing for DRAM bus (not on gpt-oss because quantized weight freq is too high)
- Each layer is one work item, the program sort them desc. A gpt-oss shard has 20 quantized ones. One 253 MiB tensor takes 85 ms on one thread, against 213 ms of ideal work per thread at 6 threads
_copy_to_itertakes 16% of cycles at 1 thread
Caveat: out_buf is reused, so output never reaches DRAM. A real f32 consumer writes 8 bytes per byte of quantized input, so it hits the write-bandwidth limit before compute does.
1 thread, kernels only (no file read, parse or orchestration). Median ns/weight.
| size | cache | llama.cpp nvfp4 | HEAD nvfp4 | ratio | llama.cpp mxfp4 | HEAD mxfp4 | ratio |
|---|---|---|---|---|---|---|---|
| 18 KiB | L1 | 0.2902 | 0.0475 | 6.11x | 0.2677 | 0.0480 | 5.58x |
| 584 KiB | L2 | 0.2936 | 0.0531 | 5.53x | 0.2685 | 0.0462 | 5.81x |
| 18 MiB | L3 | 0.3162 | 0.0524 | 6.03x | 0.2673 | 0.0463 | 5.77x |
| 292 MiB | DRAM | 0.3169 | 0.1452 | 2.18x | 0.2676 | 0.1428 | 1.87x |
| size | cache | llama.cpp nvfp4 | HEAD nvfp4 | ratio | llama.cpp mxfp4 | HEAD mxfp4 | ratio |
|---|---|---|---|---|---|---|---|
| 18 KiB | L1 | 0.8990 | 0.4144 | 2.17x | 1.4204 | 0.3940 | 3.61x |
| 584 KiB | L2 | 0.9017 | 0.4079 | 2.21x | 1.4237 | 0.3949 | 3.61x |
| 18 MiB | L3 | 0.9024 | 0.4068 | 2.22x | 1.4238 | 0.3981 | 3.58x |
| 292 MiB | DRAM | 0.9028 | 0.4084 | 2.21x | 1.4231 | 0.3985 | 3.57x |
Sizes use the nvfp4 input and output footprint. llama.cpp quantization uses reference kernels; its harness validates a roundtrip without comparing output bit-for-bit against quant.
./bench/build.sh
./bench/out/bench_llama nvfp4 && ./bench/out/bench_quant nvfp4
./bench/out/bench_llama mxfp4 && ./bench/out/bench_quant mxfp4
./bench/out/bench_llama_quant nvfp4 && ./bench/out/bench_quant f32 nvfp4
./bench/out/bench_llama_quant mxfp4 && ./bench/out/bench_quant f32 mxfp4llama.cpp kernels from
tests/test-quantize-perf.cpp, masterded522f8304554317e7ff8f88698dbbbca5425aa(2026-11-13). Pinned, 3 trials x 10 s, TSC-timed, with 2 s gaps for multiple sizes and 10 s gaps for a single size.
- swap
vcvtps2phforvcvtneps2bf16with same generic kernels - more layouts