Build a numerics regression harness and catch a seeded bug
CPU · your Macscript180 minrequiredGoal · numerics_check.py that compares FP32, BF16 and INT4 variants of Qwen2.5-0.5B (or GPT-2) with KL and top-k metrics, applies a justified threshold, and flags an injected quantization regression.
- 1Load Qwen/Qwen2.5-0.5B-Instruct (or gpt2) in float32 and build a prompt set of 100 varied prompts of 64-256 tokens; generate 32 greedy tokens per prompt with the FP32 model and save prompt plus continuation as the reference token sequences.
- 2Write compare(model_ref, model_test, sequences) that teacher-forces each sequence through both models and records per-position KL(p_ref || p_test), top-1 agreement and top-5 Jaccard overlap.
- 3Create the BF16 variant with model.to(torch.bfloat16) and run compare; report mean and p99 KL, top-1 agreement and top-5 overlap.
- 4Create the INT4 variant by applying your per-group(128) weight quantizer from p4-quantization-l1 to every linear layer and run compare.
- 5Measure the noise floor by comparing FP32 against FP32 with sequences processed one at a time versus in padded batches of 8; set your gate thresholds at least 3x above the measured floor and write down the reasoning.
- 6Inject a regression into the INT4 variant, for example quantizing one middle MLP layer per-tensor instead of per-group, or truncating instead of rounding in one layer; rerun compare and confirm the gate fires.
- 7Add a per-layer bisection mode that quantizes layers one at a time and reports which single layer moves mean KL the most; confirm it points at the layer you broke.
- 8Write a short results table and a paragraph on which metric caught the bug first and why.
- Deliverable
- numerics_check.py, a saved reference set, the results table for BF16, INT4 clean and INT4 seeded, and the paragraph.
- Check
- BF16 mean KL is under 0.02 nats with top-1 agreement above 95 percent; the seeded regression raises INT4 mean KL by at least 3x over clean INT4 and trips your gate; the bisection names the layer you modified.