# Stage 7 · Group-32 Symmetric INT4 Quantization & `unpack4xI8` *Measurements on MacBook Pro, Apple M3 Pro (18-core GPU, 18 GB), Headless Chrome 158 (WebGPU on Metal), AC power, Low Power Mode off. All quantized weights and logits verified byte-for-byte against PyTorch (`pnpm compare` + `pnpm test`).* ![Stage 7 INT4 unpack kernels, stacked decode breakdown, and Quality vs Size Pareto on M3 Pro](m3-pro/int4-pareto.png) ## Extending Stage 4 past the INT8 unpack wall Upstream [Stage 4](stage4.md) stopped at `int8` because decoding 123M scalar weights hit ALU unpack overhead (`103 GB/s` on M4 Pro). Group-32 INT4 halves weight bandwidth again (`4.5 bits/weight` effective: `4` bits payload + `fp16` group scale per 32 weights = `16/32 = 0.5` bits), provided the WGSL unpack path avoids per-weight scalar bitfields and per-weight scaling: - **Layout (`python/quantize.py`, `src/model/quantize.ts`):** Symmetric group-32 quantization into signed range `[-8, 7]` (`max_abs / 7.0`), packing `8` 4-bit weights little-endian per `u32` alongside packed `f16` group scales (`pack2x16float`). Every weight tensor's SHA-256 digest matches PyTorch in `pnpm compare`. - **`int4` vs `int4-mixed`:** - `int4` (`73.1 MB` on GPT-2): group-32 INT4 everywhere, including the `50,257`-row tied embedding/output head. - `int4-mixed` (`128.6 MB` on GPT-2): group-32 INT4 on all 48 transformer block projection matrices, keeping the `50,257`-row tied token table in `fp16`. ## `unpack4xI8` vs shift-mask in WGSL In [src/engine/kernels/matvec_split_multi_int4.wgsl](../src/engine/kernels/matvec_split_multi_int4.wgsl) (and prefill [matmul_blocked_int4.wgsl](../src/engine/kernels/matmul_blocked_int4.wgsl)): 1. Instead of scalar bitfield shifts per weight ([src/bench/matvec_int4_shift.wgsl](../src/bench/matvec_int4_shift.wgsl)), each `u32` (holding `8` signed nibbles across two `unpack4xI8` lanes) is shifted left by `4` bits, masked with `0xf0f0f0f0u`, unpacked with WGSL builtin `unpack4xI8`, and multiplied inside the 32-element group before multiplying once by the group `f16` scale (divided by `16.0` folded into group accumulation). 2. Accumulating an inner `32`-element integer/float dot product inside each group before multiplying by `group_scale` amortizes the `f16` decode across 32 weights instead of per element. Microbenchmark results on M3 Pro (`docs/m3-pro/stage7/int4-kernels-headless.json`, GPU timestamps): | Kernel | 1-row block (4 matmuls, ms) | 1-row output head (`768 → 50257`) | 8-row FFN up (`768 → 3072`) | | --- | ---: | ---: | ---: | | `split` (`fp32`) | 0.459 ms | 3.572 ms (43.2 GB/s) | 0.334 ms (56.5 GMAC/s) | | `int4-shift` (`extractBits`) | 0.256 ms | 1.840 ms (83.9 GB/s) | — | | `int4-split` (`unpack4xI8`) | **0.216 ms** | 1.224 ms (126.2 GB/s) | 0.323 ms (58.5 GMAC/s) | | `int4-split-multi` (`unpack4xI8`, tile=8) | 0.335 ms | **1.162 ms (132.9 GB/s)** | **0.110 ms (170.8 GMAC/s)** | `unpack4xI8` beats `extractBits` by **`1.19x`** across one block's warm 4 matmuls (`0.256 → 0.216 ms`, `2.13x` over `fp32` `split`) and **`1.50x`** on the large uncached `50,257`-row output head (`1.840 → 1.224 ms`), reaching **`132.9 GB/s`** effective weight throughput on `int4-split-multi` (above the M3 Pro stream read ceiling thanks to L2 reuse across reductions). Combined with `split-multi` at `8` rows, FFN up speeds up **`3.04x`** (`0.334 → 0.110 ms`, `170.8 GMAC/s`). ## End-to-end GPT-2 throughput vs quality on M3 Pro Greedy decoding across 1,000 tokens (`docs/m3-pro/stage7/kv-cache-blocked-split-fast*-1000-headless.json`) and held-out perplexity over 1,110 validation positions (`docs/m3-pro/stage7/quality-headless.json`): | Precision | Model MB | Read/token | Decode 16 / 256 / 1k (tok/s) | 16-pos GPU (Matmul / Head) | Perplexity | Top-1 vs `fp32` | PyTorch match | | --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | | `fp32` | 497.8 MB | 494.6 MB | 144.5 / 142.9 / **147.1** | 5.38 ms (4.53 / 1.26 ms) | 20.3514 | 100.0% | exact | | `fp16` | 250.7 MB | 247.6 MB | 232.9 / 237.6 / **223.0** | 3.26 ms (2.50 / 0.67 ms) | 20.3553 | 99.82% | 100.0% | | `int8` | 127.7 MB | 124.6 MB | 300.6 / 322.6 / **298.5** | 1.99 ms (1.44 / 0.41 ms) | 20.7222 | 85.59% | 100.0% | | `int8-mixed` | 166.1 MB | 162.9 MB | 284.6 / 286.3 / **263.9** | 2.40 ms (1.86 / 0.69 ms) | 20.3267 | 98.02% | 100.0% | | `int4-mixed` | **128.6 MB** | 125.5 MB | 327.5 / 291.1 / **279.2** | **2.02 ms** (1.43 / 0.60 ms) | **21.3996** | **82.52%** | **100.0%** | | `int4` | **73.1 MB** | 70.0 MB | 375.9 / 369.7 / **337.9** | **1.69 ms** (1.12 / 0.30 ms) | 293.6751 | 38.65% | **100.0%** | Why **`int4-mixed`** is the practical Pareto knee: - Pure `int4` reaches **`376 tok/s`** short / **`338 tok/s`** long (`2.30x` over `fp32`), but quantizing all `50,257` output-head rows to `[-8, 7]` adds large per-vocabulary variance and blows up perplexity to **`293.68`** (though PyTorch running those exact INT4 weights produces the identical `293.6749`). - Leaving the tied token table in `fp16` (`int4-mixed`) keeps memory virtually identical to `int8` (`128.6 MB` vs `127.7 MB`) while delivering **`279–328 tok/s`** (outperforming `int8-mixed` in both speed and `-37.5 MB` footprint) and recovering perplexity over **10x** down to **`21.40`** (`82.5%` Top-1 agreement). - Pairing `int4-mixed` with Stage 6 `split-multi` speculative decoding reaches **`1,107 tok/s`** on `code` (`3.84x`) and **`1,381 tok/s`** on `loop` (`4.73x`). Raw benchmarks: [docs/m3-pro/stage7/](m3-pro/stage7).