A model that fits on your development laptop may be too large for the phone or edge device where you want to use it. Its weights take up memory before it processes a single example. Each float32 weight needs four bytes, so representing it with an 8-bit integer can reduce weight storage by a factor of four. Neural network quantisation makes that smaller representation possible, but rounding the weights also changes the model's predictions. The choice is how much of that change you can accept.
Our MNIST experiment tests how rounding changes predictions. Reducing 8 bits to 3 costs 0.4 percentage points. At 2 bits, changing the scale moves accuracy from 12.9% to 92.5%. The naive scale erases 99% of weights. We implement scale and zero-point calculations in NumPy, then round and restore the weights to trace that accuracy cost.
Let's Build It
Open this in Google Colab and run it top to bottom. It trains a small convolutional network on MNIST, then quantizes its weights to 2 bits twice, changing nothing but the rule that picks the scale. About a minute on a CPU.
import numpy as np
def quantize_dequantize(w, s, bits):
"""Round onto an integer grid of width `s`, then read back as float."""
qmax = 2 ** (bits - 1) - 1
q = np.clip(np.round(w / s), -qmax, qmax)
return s * q
def absmax_scale(w, bits):
"""One step wide enough that the largest weight is representable."""
return np.abs(w).max() / (2 ** (bits - 1) - 1)
def mse_scale(w, bits, n=80):
"""Clip somewhere, and search for the somewhere that hurts least."""
base = absmax_scale(w, bits)
alphas = np.linspace(0.2, 1.0, n)
errs = [((quantize_dequantize(w, base * a, bits) - w) ** 2).sum() for a in alphas]
return base * alphas[int(np.argmin(errs))]
Those are the whole method. Applied to a network trained to 0.988 on MNIST, at 2 bits per weight:
for name, scale_fn in (("absmax", absmax_scale), ("mse", mse_scale)):
q = quantized_copy(model, bits=2, scale_fn=scale_fn)
print(f"{name:7s} {accuracy(q, x_test, y_test):.4f}")
# absmax 0.1289
# mse 0.9254
Same network, same bit width, same rounding. The only difference is a number.

The left panel is the whole story and the right panel is the part everyone spends their time on. Between 8 bits and 3 the three rules are indistinguishable and the loss against float32 is 0.4 points at worst. At 2 bits they separate by 80.
What Just Happened?
The map, in two lines
Quantization replaces a float with an integer and a recipe for turning it back. The recipe is affine:
is the scale, the width of one integer step measured in the units of the weight. is the zero point, the integer that decodes to exactly 0.0. Together they place a ruler over the real line: sets how far apart the ticks are, sets where the ruler starts.
Setting gives symmetric quantization, where the grid is centred on zero and is representable whenever is. Fitting to the tensor's minimum gives asymmetric quantization, which costs one extra integer per tensor and matters when the values are one-sided. A ReLU output is never negative, so a symmetric grid spends half its levels on values that cannot occur. That is a real effect and it is measured further down.
At bits the symmetric grid runs from to , dropping the extra negative value so the grid stays centred. Eight bits gives 255 levels. Two bits gives three: , , .
The scale is the whole problem
Every weight has to land on one of those levels. The rounding is fixed, the clipping is fixed, and the only freedom is .
The obvious choice sets so that the largest magnitude in the tensor lands on the last integer. Nothing clips, no information is thrown away at the top, and this is what absmax_scale does. It is also what breaks the network, because the largest weight is the least typical one and it has been handed the job of setting the step size for all the others.
Watch what that means at 2 bits, where there are three levels to spend:

The bottom-left panel is a network being destroyed. The step is 0.398, the two non-zero levels sit at , and the entire weight distribution lives inside . Every weight rounds to zero except the handful that reach past half a step. That is 99% of fc1, and 99.00% of the network.
The bottom-right panel is the same layer with the step at 0.088. Both non-zero levels now sit inside the distribution, 65% of weights round to zero, and the network scores 0.925.
The search absmax never runs
mse_scale finds that step by trying clipping ratios and keeping the one with the smallest squared error. Absmax is the ratio 1.00, so the search starts where absmax stops:

The left panel shows the levels walking inward as the clip tightens. The right panel is the objective: mean squared reconstruction error against clipping ratio, falling from 0.0033 at absmax to 0.0009 at its minimum near 0.22. The curve is smooth and shallow near the bottom, which is why 80 ratios are enough and why nobody needs to be clever here.
Clipping trades a large error on a few weights against a smaller error on all the others. At 8 bits there is enough grid for that trade to be irrelevant. At 2 bits it is the difference between a model and a bag of zeros.
The Cliff, Measured
Everything from here is three seeds, a converged baseline, and the numbers as they came out.
The float32 network scores 0.9880 ± 0.0014 after eight epochs, at which point the budget curve has flattened: eight epochs gives 0.9886 and twelve gives 0.9887 on the same run. Quantizing an undertrained model would have made every comparison below a fact about training length rather than about quantization.
| bits | absmax | 99.9th percentile | MSE-optimal |
|---|---|---|---|
| 8 | 0.9879 | 0.9881 | 0.9878 |
| 6 | 0.9879 | 0.9878 | 0.9879 |
| 4 | 0.9851 | 0.9859 | 0.9873 |
| 3 | 0.9722 | 0.9736 | 0.9802 |
| 2 | 0.1289 | 0.1279 | 0.9254 |
One scale per tensor, weights only, mean over three seeds. Read down a column and the bit width buys you very little until it buys you everything. Read across the 2-bit row and one line of code is worth 80 accuracy points.
The seeds agree. mse beats absmax at 2 bits by +0.7950, +0.8157 and +0.7789, so the smallest gap on any run is 78 points. Nothing about that finding depends on a lucky initialisation.
A network of zeros
The accuracy table is a consequence, and this is the cause:

At 8 bits every rule leaves under 3% of weights on zero. At 2 bits absmax leaves 99.00%, the percentile clip leaves 96.53%, and the MSE search leaves 65.32%. A network that is 99% zeros is not a quantized network, it is a deleted one, and the 0.1289 accuracy is what remains of four biases and a few hundred surviving weights.
Note that the percentile clip barely helps here, at 96.53% against 99.00%, and its accuracy is no better. Clipping the top 0.1% of magnitudes is a sensible default at 8 bits and far too timid at 2. The rule that works is the one that optimises the thing you care about instead of guessing a threshold.
Going Deeper
The tail that sets the scale
Absmax only fails when the largest weight is unrepresentative. Whether it is depends on the layer:

Three of the four layers have no tail worth the name. Their largest weight is within 8% of their 99.9th percentile, which is what a Gaussian does. fc1 sits at 1.44, with a kurtosis of 5.45 against a Gaussian's 3.0, and it is also the layer holding 51,200 of the network's 56,592 weights.
That is the shape of the problem in real models too, and at a much larger scale. LLM.int8() found that in transformers past roughly 6.7B parameters, a small number of feature dimensions carry outliers up to 20 times larger than the rest, and that quantizing them with everything else destroys the model. The fix in that paper is to keep the outlier dimensions in float16. The fix in TurboQuant is to rotate the tensor first, so the outlier's magnitude is spread across many coordinates and no single one dominates the scale. Both are answers to the picture above.
Per-channel scales, and when they are not worth it
Rather than one scale per tensor, give each output channel its own. For a Linear weight of shape (out, in) that is one scale per row; for a Conv2d weight of shape (out, in, kh, kw) one per filter. The storage cost is one float32 per channel, which here means 122 extra scales, 488 bytes against a 13.8 KiB 2-bit model.
It is nearly free, and it is not always worth it. At 3 bits with the MSE rule, per-channel scales gain +0.0047 and +0.0061 on two seeds and lose 0.0003 on the third, taking the best 3-bit result to 0.9837 ± 0.0015. At 2 bits with absmax it does not rescue anything: 0.2609 ± 0.1244, where the standard deviation is half the mean because one seed happened to score 0.4353 and another 0.1536.
The reason is granularity meeting sample size. conv1 has 16 channels of 9 weights each, and a scale estimated from nine numbers is noise. Per-channel helps where channels have different ranges and enough weights each to measure them, which in this network means fc1 and little else. In a transformer, where every projection row has thousands of entries, the advice to use per-channel is sound. Here it is worth checking rather than assuming.
No single layer is the culprit
The usual next move is to find the fragile layer and leave it in float. That works at moderate bit widths and fails completely at 2:

The teal bars quantize one layer and leave the rest in float32. conv1 alone costs almost nothing (0.879), conv2 alone drops to 0.369 and fc1 alone to 0.397. So there are two fragile layers, not one.
The steel bars are the question that matters, and they are flat. Sparing fc1 while quantizing everything else gives 0.190. Sparing conv2 gives 0.182. Sparing any single layer leaves the network at chance, because at this bit width every layer is independently sufficient to break it. Mixed precision is a real technique and it does not apply here: the answer is not to protect a layer, it is to fix the scale.
At 4 bits the same sweep is uninformative in the other direction. Quantizing one layer costs at most 0.0014 and sparing one gains at most 0.0021, all inside the seed spread. There is no sensitivity to find when nothing is sensitive.
Activations, and how few images calibrate them
Weights are known before inference runs. Activations are not, so their range has to be estimated by pushing unlabelled data through the network and recording what comes out. That is calibration, and the usual question is how much data it needs.

Eight images. The left panel runs 8, 32, 128, 512 and 2,048 calibration images and returns 0.9881, 0.9880, 0.9880, 0.9881 and 0.9881. A range is a maximum over a large number of values per image, so a handful of images already carries tens of thousands of samples, and the estimate stops moving almost immediately.
The right panel is where the two design choices show up, at 4-bit activations where there is enough pressure to separate them. Starting from absmax with a symmetric grid at 0.9841:
- clipping at the 99.9th percentile adds +0.0015, +0.0034 and +0.0017 across the three seeds
- keeping absmax but adding a zero point adds +0.0019, +0.0046 and +0.0026
- doing both reaches 0.9882, which is the float32 baseline
Every one of those deltas is positive on every seed. The zero point is worth slightly more than the clip, which follows from the ranges: these activations are ReLU outputs, so a symmetric grid throws away half its levels before it starts, and that is a larger loss than a mild tail. The tails are mild: the three ReLU sites have maximum-to-99.9th-percentile ratios of 1.04, 1.46 and 1.26, nothing like the 20-fold outliers of a large transformer.
Putting weights and activations together, 8-bit weights with 8-bit activations gives 0.9879 and 4-bit weights with 8-bit activations gives 0.9870, both within a fifth of a point of float32. Two-bit weights with 8-bit activations gives 0.9267, matching the weight-only result: at that point the activations are not what is wrong.
The saving that is real, and the one that is not
Bytes, immediately and reliably. The network's 56,592 weights occupy 221.1 KiB in float32, 55.3 KiB at 8 bits, 27.6 KiB at 4 and 13.8 KiB at 2. Per-channel scales add 488 bytes, which is 3.4% of the 2-bit model and rounding error everywhere else.
Speed, only if someone has written the kernel. A 1024 x 800 by 800 x 64 matrix multiply in NumPy takes 330 microseconds in float32 and 41,628 microseconds in int8, which is 126 times slower. NumPy dispatches float32 to a BLAS library with vectorised, multithreaded kernels and has nothing equivalent for 8-bit integers, so it falls back to a generic loop.
This is worth stating plainly because the promise of quantization is usually delivered as "smaller and faster". The smaller is arithmetic. The faster requires an integer kernel that fuses the dequantization into the multiply, which is what llama.cpp, TensorRT, and every production inference stack exist to provide. Quantizing weights in NumPy and expecting a speedup produces a slower model, and the reason is not the quantization.
Quantization-aware training, bounded
Everything above is post-training quantization: take a finished model, round it, measure the damage. The alternative simulates the rounding during training so the weights can move somewhere that survives it.
The obstacle is that round has a derivative of zero almost everywhere, so no gradient reaches the weights. The straight-through estimator ignores this: in the forward pass it rounds, and in the backward pass it pretends the rounding was the identity and passes the incoming gradient through unchanged. The approximation is wrong and works, which Bengio, Léonard and Courville (2013) established empirically before anyone explained why.
Quantization-aware training is what you reach for when post-training quantization leaves you short, which on the evidence here means below 3 bits. Above that, on this network, there is nothing left to recover.
Where This Comes From
The affine map at the top of this post is the one specified in Jacob et al. (2018), the paper behind TensorFlow Lite's integer inference. Its contribution is less the map than the observation that a whole layer can be executed in integer arithmetic if the scales are handled correctly, with the scale factors folded into a single fixed-point multiplier applied to the accumulator. The paper states the requirement directly:
We emphasize that the inference computation is carried out entirely on integer arithmetic, with the quantization parameters being the only floating-point quantities.
Krishnamoorthi (2018) is the companion whitepaper and remains the best survey of the design space this post samples: symmetric against asymmetric, per-tensor against per-channel, post-training against quantization-aware. Its headline recommendation, that per-channel weight quantization with per-tensor activation quantization is the sensible default at 8 bits, holds up on this network in the sense that it costs nothing. The measurements above add the caveat that it is not automatically the right choice on small tensors.
The 2-bit case has its own literature, because a symmetric 2-bit grid is exactly ternary: every weight becomes , or times a scale. Li, Zhang and Liu (2016) derived an approximate threshold for that case, , under the assumption that the weights are Gaussian.
Our search knows nothing about that derivation and finds its own threshold numerically, which makes the comparison a test of the assumption rather than of the formula. On conv1 the two agree to within 0.6%. On fc2 the gap is 9%, on conv2 22%, and on fc1 the search lands 43% above the closed form. The ordering is the kurtosis ordering: the approximation is accurate on the layers that are Gaussian and drifts on the one that is not. A closed form derived from an assumption is worth exactly as much as the assumption, and checking it costs 80 evaluations of a squared error.
The outlier problem this post demonstrates at 51,200 weights is the one Dettmers et al. (2022) hit at billions, and GPTQ attacks from a different direction: rather than choosing a better scale, it quantizes weights one column at a time and updates the remaining full-precision weights to compensate for the error already committed. Both are the same admission, that rounding each weight to its nearest grid point in isolation is not the best you can do.
Interactive Tools
- KV Cache Calculator: size the other half of an inference memory budget, the activations that grow with context rather than the weights that do not.
- LLM Cost Calculator: put a price on the memory a quantized model frees up.
Related Posts
- TurboQuant: How a Random Rotation Makes LLM Quantization Near-Optimal: the answer to the outlier problem this post measures, by rotating the tensor so no single coordinate sets the scale.
- LoRA from Scratch: Why LLM Fine-Tuning Got 1000x Cheaper: QLoRA fine-tunes on top of 4-bit base weights, which is the regime this post finds costs almost nothing.
- KV Cache Memory: Sizing LLM Inference: weights are the fixed part of the memory bill and the cache is the part that grows.
- Shrinking the KV Cache: MQA, GQA, and Multi-Head Latent Attention: the architectural way to cut inference memory, rather than the numerical one.
- Convolutional Neural Networks from Scratch: Conv2d in NumPy: the layers being quantized here, built from the arithmetic up.





