The Bug Was Four Heads Sharing One int8 Scale
A new WebGPU engine matched its PyTorch oracle to five digits at every stage but one: the output projection was 4.9% off. Before touching a kernel we reproduced the error in numpy: 4.91%. The cause was int8 activation blocks spanning four attention heads whose magnitudes differ by 230x.
Summary
We brought up a hybrid linear-attention model (a 0.8B Qwen3.5) on a from-scratch WebGPU engine and compared it against a PyTorch oracle stage by stage. Four stages matched to five digits. The fifth, the output projection of the first linear-attention layer, was 4.9% off. We did not touch the kernel. We reproduced the number in numpy first, and the simulation said 4.91%: the engine was doing exactly what we told it to. The cause is a design choice, not a bug: activations are quantized to int8 in blocks of 512 values, and a 512-block here spans four attention heads whose output magnitudes differ by two orders of magnitude.
What matched, and what did not
| stage (layer 0, last prompt token) | cosine | relative L2 |
|---|---|---|
| input projection (q, k, v) | 1.00000 | 0.0026 |
| z projection | 0.99999 | 0.0045 |
| causal conv + silu | 1.00000 | 0.0021 |
| delta-rule readout | 1.00000 | 0.0007 |
| output projection | 0.99881 | 0.0488 |
The gating weights (the two small projections that set the state decay) matched to three decimals, so the recurrence itself was right.
Reproducing it before fixing it
The oracle saved the gated-norm output that feeds the output projection. We quantized it the way the engine does and multiplied by the same dequantized weights:
| activation quantization | relative error vs oracle |
|---|---|
| none (exact f32 matmul) | 0.0013 |
| int8, 512-value blocks (the engine) | 0.0491 |
| int8, 128-value blocks (one per head) | 0.0277 |
| int8, 64-value blocks | 0.0212 |
The engine's 4.88% against the simulation's 4.91% settled it. The per-head maximum of that vector ranges from 0.01 to 2.32, so a block covering four heads gets a scale set by the loudest head and rounds the quietest to a handful of levels.
Gotchas
The model needed no fix to be "wrong". A cosine of 0.999 at a stage looks fine and hides a 5% relative error. We only noticed because we printed both numbers.
Simulate before you debug. Twenty lines of numpy on a saved tensor ruled out indexing, stride, and layout bugs in one step. Without it we would have spent the afternoon in a shader.
The fix is not free. Per-head scales cut the simulated error to 2.8% but need a new matvec variant. We have not built it; this post is about the diagnosis.
Key takeaways
- When a stage is slightly off, reproduce the error offline from saved tensors before editing the kernel.
- Report relative L2 beside cosine. Cosine saturates near 1 and hides percent-level error.
- Quantization blocks should not span units with very different magnitudes; heads are the natural unit.
- A matching number from an independent simulation is stronger evidence than a clean-looking log.
Follow @robotnurseio on X for the short version.
Everything in these notes I also do for hire: local AI set up on hardware you own, configured on-site, then handed over with enough documentation that you do not need me afterward. If that sounds more useful than another weekend of reading forum threads, the details are at /work. No obligation from an email, and the posts stay free either way.