Qwen3-0.6B inference (12 KB), bare-metal bootable OS (22 KB), and from-scratch Mini-GPT trainer with live Braille TUI (32 KB) in 100% pure x86-64 NASM assembly. Zero C, zero libc, zero dependencies.
verify_train_np.py — numpy-only gradient verification
An independent, PyTorch-free replay of 03-minigpt-train's forward/backward
pass, used to cross-check the trainer's gradients, optimizer trajectory and
final weights.
Why this exists
scripts/verify_train_ref.py (upstream) already does exactly this, and does it
well — it uses PyTorch autograd as the reference implementation and checks
loss parity, per-tensor gradients, a 10-step AdamW/LR/clip trajectory, and
multi-thread determinism.
That harness has one hard requirement: PyTorch must be installed.
On a machine where python3 -c "import torch" fails, the upstream harness
cannot run at all:
$ python3 -c "import torch" && echo TORCH_OK || echo TORCH_MISSING
TORCH_MISSING
This script removes that requirement. It needs only numpy, which
scripts/export_weights.py already depends on, so it runs on any host that can
build the trainer. It is not a replacement for verify_train_ref.py — it is
a second, independent opinion produced by a different route:
verify_train_ref.py |
verify_train_np.py |
|
|---|---|---|
| reference implementation | PyTorch autograd | hand-written numpy backprop |
| dependencies | numpy + torch | numpy only |
| default steps | 10 | 10 |
| grad gate | cosine > 0.99999, max_abs < 1e-4 |
rel_rms < 1e-5, cosine > 0.9999 |
| indicators | max_abs, cosine | max_abs, rel_rms, rel_l2, cosine |
| multi-thread determinism | yes (-t 1/8/16) |
no |
The two harnesses share no code and no reference framework. If both agree with the assembly dump, the probability that the agreement is an artefact of one particular reference implementation drops sharply.
Usage
Self-contained: builds the dump itself by invoking the trainer.
python3 scripts/verify-train-np/verify_train_np.py \
--bin build/asm-gpt-train \
--data data/tinyshakespeare.txt
Verify an already-produced dump directory (no trainer invocation):
python3 scripts/verify-train-np/verify_train_np.py --dump-dir /tmp/mydump
All trainer options mirror verify_train_ref.py (--layers --dim --heads --hidden --batch --seq --steps --lr --wd --grad-clip --warmup --seed -t),
with the same defaults, so results are directly comparable.
Exit code is 0 on PASS, 1 on FAIL, 2 on environment error.
Measured results
Same configuration as the upstream verification table
(2 layers / dim 64 / 4 heads / hidden 128 / batch 2 / seq 16 / 10 steps / lr 1e-3 / wd 0.1 / clip 1.0 / warmup 3 / seed 42), trainer rc=0:
| indicator | upstream verify_train_ref.py (PyTorch) |
this script (numpy) |
|---|---|---|
| step-0 loss abs diff | 4.768e-07 | 1.524e-07 |
gradient worst max_abs |
3.353e-08 | 6.278e-08 |
gradient worst cosine |
1.0000000 | 1.0000000000 |
LR trajectory max_abs |
0.0 | 1.940e-11 |
final weights max_abs (10 steps) |
6.153e-06 | 5.867e-06 |
Gates:
L1_grad_rel_rms_lt_1e-5 PASS
L1_grad_cosine_gt_0.9999 PASS
L1_gnorm_rel_lt_1e-5 PASS
L2_loss_max_abs_lt_1e-3 PASS
L2_lr_max_rel_lt_1e-6 PASS
L3_weight_rel_rms_lt_1e-5 PASS
L3_weight_cosine_gt_0.9999 PASS
overall: PASS
The two routes agree to the same order of magnitude on every indicator, from two mutually independent implementations of the backward pass.
Note on the LR column
Upstream reports 0.0 where this script reports 1.940e-11. The difference is
confined to the cosine-decay schedule: the LR is a pure scalar function of the
step index, evaluated with math.cos here. 1.94e-11 is float64 round-off in
that evaluation, not a trajectory divergence — the loss column, which depends on
the LR being applied correctly, agrees to 1.5e-07.
Why three layers instead of one
The layers are not three views of one signal; each catches a different class of defect:
- L1 (gradients, pre-clip/pre-AdamW) — catches a wrong backward pass for any single tensor. This is the strongest single check, since it covers all 21 gradient tensors individually.
- L2 (loss + LR trajectory) — catches a wrong schedule or an off-by-one step (e.g. replaying the loss after AdamW instead of before).
- L3 (final weights after 10 steps) — catches slow systematic drift that a single step would not expose.
They are not interchangeable: L2 passing does not imply L1 is correct. Loss is a scalar; a weight error of 1e-5 can leave the loss change below 1e-6.
Negative control
A verifier that cannot fail is worthless. _negative_test.py takes a dump
directory, copies it to a temp dir, adds 1e-2 to the first 64 float32 values of
step0_grads.bin, and re-runs the harness (the original dump is never touched):
$ python3 scripts/verify-train-np/_negative_test.py /tmp/mydump # any --dump-debug output
Output on this machine:
corrupted : <tmp>/dump/step0_grads.bin (+1e-2 on first 64 float32)
rc : 1
gates PASS : 4
gates FAIL : 3
L1_grad_rel_rms_lt_1e-5 FAIL
L1_grad_cosine_gt_0.9999 FAIL
L1_gnorm_rel_lt_1e-5 FAIL
NEGATIVE CONTROL: PASS (harness rejects bad data)
All three L1 gates trip; all four L2/L3 gates still pass, because only the step-0 gradient block was touched. That is the layered design behaving as intended — the tamper is localised to the layer that observes it.
Limitations
- No multi-thread determinism check (upstream already covers it).
- The numpy reference intentionally mirrors the trainer's
float32arithmetic; it is a correctness reference, not a speed reference. - A dump produced with
--dump-debugreuses one fixed batch for all steps (sampling is skipped), otherwise the replay would not be deterministic.