Quantize GPT-2 weights by hand and measure the damage
CPU · your Macnotebook120 minrequiredGoal · Implement INT8 and INT4 quantization at three granularities, apply it to every GPT-2 linear layer, and plot perplexity versus bits.
- 1Load gpt2 and a 200-sample slice of wikitext-2; compute baseline perplexity in FP32.
- 2Write quantize(w, bits, granularity) returning int tensor, scales and zero points for per-tensor, per-channel and per-group(128).
- 3Write dequantize and report mean squared error and max error for the first MLP weight at each setting.
- 4Replace every nn.Linear weight with its dequantized version and recompute perplexity for INT8 and INT4 at each granularity.
- 5Plot perplexity versus (bits, granularity) and mark the FP32 baseline.
- 6Try a simple clipping threshold (e.g. 99.99th percentile) instead of min/max for INT4 and record the change.
- Deliverable
- A notebook with the quantizer, the error table and the perplexity plot.
- Check
- INT8 per-channel perplexity is within 1 percent of FP32; INT4 per-tensor is visibly worse than INT4 per-group(128).