Parity with PyTorch as the test
The test runs four fixed inputs through the TypeScript code character by character and compares the logits at every position, plus all attention maps, with PyTorch. The difference is 5.7e-6 for fp32 weights and 6.7e-6 for fp16, against a 1e-3 limit.
A test that can fail
Two negative controls break the model on purpose: they drop the 1/√d scaling or let a query read one character past the causal mask. Both exceed the limit, so the test really guards the math.
No ML library in the browser
No ONNX and no TensorFlow.js: just arrays of numbers and loops. That keeps the page light and lets every step of the model be shown on screen.