Build your own INT8/INT4 quantizer for GPT-2 with a correctness gate
CPU · your Mac~8 hQuantize GPT-2 small and medium to INT8 (per-channel) and INT4 (group-wise, round-to-nearest and GPTQ-lite), load the quantized weights into your Phase-1 engine or transformers, and put every configuration through a numerics gate you also build: KL and top-k agreement against fp32 goldens plus perplexity on a fixed text slice. It proves the phase because you can show, with your own numbers, exactly how much accuracy each byte saved costs and the gate that would stop a bad quantization from shipping.
Scope
- A quant package with absmax INT8 per-channel, INT4 group-128 with zero points and nibble packing, and a GPTQ-lite pass using a Hessian from 128 calibration sequences.
- A loader that swaps quantized weights into the model at load time (dequantize to fp32 for the matmul) and verifies every weight tensor was handled.
- Golden generation from transformers GPT-2 fp32 on 20 prompts and a gate with per-tier thresholds for KL, top-1 agreement, top-10 overlap and greedy divergence position.
- Perplexity evaluation on a fixed 2,048-token WikiText-2 slice, held out from the calibration data.
- A bench that reports weight bytes, compression ratio and batch-1 decode tokens per second for fp32, INT8 and both INT4 variants.
- One CLI command that runs everything and writes results.csv plus a README table.
Out of scope
- Real low-bit matmul kernels; dequantizing to fp32 before the matmul is fine on CPU.
- Activation quantization beyond an optional W8A8 demonstration.
- Models other than gpt2 and gpt2-medium.
What to measure
- Weight compression
- How · packed bytes including scales and zero points versus fp32 weight bytes, from the loader's accounting
- Target · at least 3.8x for INT8 and at least 7x for INT4 group-128
- Perplexity delta
- How · your perplexity on the held-out slice minus the fp32 figure, fp32 cross-checked against transformers within 0.01
- Target · INT8 at most 0.2; INT4 group-128 GPTQ-lite at most 1.5 and lower than round-to-nearest INT4
- Gate metrics
- How · mean KL over the full vocab and top-1 agreement against fp32 goldens on 20 prompts
- Target · INT8 KL below 0.01 and top-1 at least 0.97; INT4 values reported with the tier they pass
- Decode speed
- How · batch-1 tokens per second for 128 tokens after a 32-token prompt, median of 5
- Target · report; explain the direction of the change versus fp32 on CPU
- Gate sensitivity
- How · inject one seeded bug (for example quantizing along the wrong axis) and run the gate
- Target · exit code 1 on every injected bug
Pay attention to
- GPT-2's Conv1D stores weights transposed relative to nn.Linear; per-channel must mean per output channel after accounting for that.
- Asymmetric INT4 needs the zero point clamped into the code range; off-by-one here silently clips the largest weights.
- Hessian damping and the order of column updates decide whether GPTQ-lite helps or hurts; print the per-layer reconstruction error.
- Calibration text must not overlap the perplexity slice; reviewers check for this leak.
- Compare logits before tokens; a greedy match on short prompts hides large distribution shifts.
- Say whether the embedding and the tied unembedding were quantized, and show both choices if you tried them.
Acceptance (all must pass)
- ☐pytest passes, including the gate-sensitivity test with at least one injected bug.
- ☐One command produces results.csv with a row per configuration and the README table is generated from it.
- ☐INT8 meets the compression, perplexity and gate targets; INT4 GPTQ-lite beats INT4 round-to-nearest on perplexity.
- ☐The fp32 perplexity matches transformers within 0.01, proving the evaluator.
- ☐The README explains in one paragraph which configuration you would ship and why.