Quantization Basics
While optimizing DeepSeek V4.1 Flash recently, I ran into a case where FP8 GEMM on Hopper was much slower than BF16. Digging into it sent me back over the fundamentals of quantization, and this article is the write-up.
It does not cover the analysis of that DSV4.1 Flash FP8 GEMM issue. It is an introduction to the basics of quantization from an infra perspective.
1Why quantize for inference
Quantization means storing and computing numbers with fewer bits, for example replacing BF16 / FP32 with FP8, FP4, or even a single bit.
This works because large models do not need all the precision BF16 provides. One study ran more than 500,000 evaluations across the Llama-3.1 family (8B, 70B, 405B) and found that FP8 weights and activations (W8A8) cause essentially no accuracy loss. Some teams go further and train in FP8: DeepSeek V4.1 Flash runs its matmuls in FP8 during training, so the model is adapted to that precision from the start. In the technical report, controlled experiments on two validation models (16B and 230B) show the relative loss gap between FP8 and BF16 training stays within 0.25%.
More aggressive approaches push each weight down to 1–2 bits. Microsoft's BitNet restricts weights to two values, −1 and +1; the later BitNet b1.58 uses three values, −1, 0 and +1, about 1.58 bits per weight, and was used to train an open 2B-parameter model from scratch. These models are all trained in low precision from the start; compressing an already-trained BF16 model straight to 1–2 bits usually costs noticeable accuracy. This article covers only the most common step: BF16 → FP8.
At inference time, GPU memory holds the weights and the KV cache, and every decode step has to move the weights to the Tensor Cores for matrix multiplication. Quantization therefore saves three resources: memory capacity, memory bandwidth and compute. Taking BF16 → 8-bit as an example, each number takes half the bytes, and each of the three savings pays off in a different scenario:
2How numbers are stored in memory
Sign / exponent / mantissa
A floating-point number has three parts:
- Sign bit: positive or negative.
- Exponent bits: the range, i.e. how large and how small a number can be represented.
- Mantissa bits: the precision, i.e. how finely values within one order of magnitude can be distinguished.
FP8 E4M3 has only 4 exponent bits and 3 mantissa bits, so both range and precision are tight:
- The largest representable value is 448.
- The smallest normal number is 2-6 ≈ 0.0156, and the smallest subnormal is 2-9 ≈ 0.00195 (when the exponent bits are all 0, the implicit leading 1 is dropped, so the ticks keep extending toward 0).
- 8 bits give 256 encodings; minus 2 NaNs, that leaves 254 finite values, 126 of them positive.
Going from 16 bits to 8 saves capacity and bandwidth. The price is that each number keeps only 3 mantissa bits, with 254 finite values in total. The next section looks at what happens when high-precision numbers are converted into these formats.
3From high precision to low, and back again
The obvious approach: just round
The first idea for going from high to low precision is to round directly. Take FP32 → BF16: BF16 has the same 8-bit exponent as FP32, so the range is identical; only the mantissa shrinks from 23 bits to 7. Rounding directly is therefore fine for FP32 → BF16. You lose precision, but nothing goes out of range. Let's walk through it with 0.0137.
Another example: 3.14159 becomes 3.140625 in BF16, a relative error of about 0.03%. With 7 mantissa bits, BF16's maximum relative rounding error is within 2-8 ≈ 0.39%, which is usually acceptable for inference.
FP16 needs more care: it has only 5 exponent bits, a maximum of 65504, and a smallest normal number of about 6.1 × 10-5. Weights generally fall within this range and can be converted directly, but activations or intermediate results occasionally fall outside it: large values overflow to inf, small ones underflow. This is often cited as one reason large-model training favors BF16.
Why this intuition breaks down for FP8
Weights and activations in large models are mostly concentrated near 0, with very small magnitudes. Let's look at a set of illustrative small-magnitude data: 100,000 numbers drawn from a normal distribution with mean 0 and standard deviation 0.002. We already saw the error of converting directly to BF16; the trouble is converting directly to FP8.
The subnormal range marked in red is the stretch below FP8's smallest normal number, 2-6 ≈ 0.0156. Here the exponent bits are all 0 and the implicit leading 1 is gone, so the ticks can no longer get denser by halving intervals toward 0. Instead they are evenly spaced at a fixed 2-9 ≈ 0.00195. The smaller the number, the larger this spacing is relative to it, so relative precision gets worse the closer you get to 0.
The problem with direct conversion is that these numbers are packed within ±0.0085 and use almost none of FP8's ticks. If we first scale the whole set up so that the largest value lands exactly on 448 and then convert to FP8, we get to use the entire ruler. That multiplier is the scale: map the high-precision numbers onto the FP8 grid using it, and map them back when they are used. Two questions follow naturally:
- How exactly is the mapping done?
- Why FP8 rather than INT8, which is also 8 bits?
How the mapping works: what a quantized number actually stores
Once the scale is applied, the quantized number is no longer the original number. Say the largest absolute value in a group of weights is 0.05; then 0.02 in that group quantizes to 176 (0.02 ÷ (0.05 ÷ 448) ≈ 179.2, rounded to the nearest tick, 176). On its own, 176 means nothing: it is nearly 9,000 times the real value 0.02.
A quantized number records how large a value is relative to the largest value in its group. Think of the scale as the real length that one unit on the ruler represents, and the quantized number as the reading taken with that ruler. Multiply them (x′ = reading × scale) to get back to real units. So a group of quantized numbers consists of two parts:
- A set of readings, 8 bits each.
- The ruler itself, the scale: only one per group, and tiny compared with the readings, so the capacity and bandwidth savings are essentially unaffected.
The steps of quantization: following 4 numbers
Suppose a tensor contains these 4 numbers: 12, 7.25, −8.95, 1.71. Let's follow them through:
- Find the largest absolute value (absmax): here it is 12. Use it to set scale = 12 ÷ 448 ≈ 0.0268.
- Divide each number by the scale to move it onto the FP8 ruler: 12 → 448, 7.25 → 270.7, −8.95 → −334.1, 1.71 → 63.8.
- Round to the nearest tick to get the quantized numbers: FP8 can only represent those discrete tick values, so anything between ticks must be rounded to the closest one.
- 448 is itself a tick and stays unchanged.
- 270.7 and −334.1 sit in a range where adjacent ticks are 32 apart; they round to 256 and −320, a noticeable shift.
- 63.8 is very close to the tick at 64 and barely moves.
- When the values are used, multiply back by the scale to get x′: 12, 6.857, −8.571, 1.714. Compared with the originals, the errors are 0%, 5.4%, 4.2% and 0.25%.
The figure below walks through these four steps. In the last step, dashed lines project the original values onto the output ruler, so you can see the difference directly.
The error comes from the rounding step: the distance a value moves when snapped to a tick, multiplied back by the scale, is the final error. Where ticks are sparse (large magnitudes), values move more; where ticks are dense, they move less.
absmax ÷ 448 is really one way of mapping the original values onto the FP8 grid, and the simplest one: it guarantees the largest value stays in range and everything else falls proportionally. There are more sophisticated mapping strategies, all aiming to minimize quantization error. They fall roughly into two groups:
- Tune only the mapping itself. For example, deliberately pick a slightly smaller scale so a few of the largest values get clipped, in exchange for better precision on most of the rest. The scale is chosen by searching for minimum error.
- Adjust the weight values as well, aiming to minimize the error of the layer's output rather than just the error of the numbers themselves, e.g. GPTQ and AWQ (see Section 6).
For FP8, absmax is usually good enough, because its relative precision is roughly the same everywhere. Sophisticated mapping strategies matter more for formats with very few ticks, such as INT4.
Why FP8 and not INT8
INT8 quantization follows the same steps, except that scale = absmax ÷ 127 and values round to evenly spaced integer ticks (−127…127). Both are 8 bits with 256 encodings; the only difference is how the ticks are laid out. The figure below converts both grids back to original values and plots them on the same horizontal axis; the bottom row shows the values from a real weight matrix.
The two grids have opposite error patterns:
- INT8 has the same absolute error everywhere, at most half a scale step (absmax ÷ 254). For small values near 0, this error is large relative to the value itself; it can be about as large as the value, or round it straight to 0.
- FP8's absolute error grows with the value, and near absmax it is even larger than INT8's, but its relative error stays within a fairly narrow band (in the normal range, the maximum is roughly 3.3% to 6.3%).
Values in real tensors are mostly crowded near 0, so FP8 is the better fit. Over all elements of this matrix, the median relative error is 7.2% for INT8 and 2.2% for FP8, and 9.1% of the values become 0 under INT8.
Conversely, if a set of values spans a narrow range with no outliers, INT8's evenly spaced grid (7 magnitude bits) is actually finer than FP8 (3 mantissa bits). FP8's advantage is its large dynamic range; when the data spans a narrow range, it is no more accurate than INT8.
4Granularity: how big a group one scale covers
So far we have said "one scale per group of numbers". That group can be very coarse or very fine. Three choices are common:
- per-tensor: the whole tensor shares one scale. Coarsest and simplest.
- per-channel: each row (or column) of the matrix shares one scale. For activations with one scale per row, i.e. per token, this is usually called per-token.
- per-block: the matrix is cut into small blocks, one scale per block.
A real-world example is DeepSeek-V4.1-Flash: according to its public config and reference code, weights get one scale per 32×32 block, and activations one scale per 32 values.
Finer granularity isolates outliers, i.e. the occasional value in a group that is much larger than the rest. The scale is set by the largest absolute value in the group, so the larger the group, the more likely it contains an outlier that squeezes everything else toward 0. With small blocks, an outlier only hurts its own block. The figure below places the same outlier in each of the three granularities:
There are two costs:
- More scales to store. With 32×32 blocks, every 1024 FP8 weights add 1 scale: about 0.4% extra if scales are stored as FP32, about 0.1% as ue8m0 (1 byte).
- The matmul has to multiply by scales more often along the way. Per-tensor or per-row scales can be applied once after the matmul finishes accumulating. Once you cut blocks along the reduction dimension, you have to apply a scale after every block, and the kernel gets more complex. This is in fact one of the reasons behind the FP8 GEMM performance problem of DeepSeek V4.1 Flash on Hopper (Blackwell does not have this problem).
5What to quantize: W / A / KV, and the WxAy notation
The WxAy notation gives the bit width of the two inputs to the matmul (weights W, activations A); it says nothing about the output. In both schemes below, the output is BF16, and what flows between layers is BF16 as well.
- W8A8: quantize the activations to FP8 online, run an FP8×FP8 matmul with FP32 accumulation, multiply by the scales and output BF16; the next layer quantizes again at its input.
- W8A16 / W4A16: convert the weights back to BF16 and run a BF16×BF16 matmul, again with FP32 accumulation and BF16 output.
Both use FP32 accumulators; the accuracy difference comes from the activations. W8A16's error comes only from the weights, while W8A8 adds the activation quantization error on top.
In addition, W8A8 has to quantize activations on the fly at inference time, which causes two headaches:
- Overhead: every time, you have to compute absmax and the scale before quantizing, which is extra work. The gains must outweigh this overhead to actually speed things up; otherwise it can end up slower. The capacity advantage (half the weight bytes), though, is generally guaranteed.
- Difficulty: activations differ for every input, so they cannot be tuned offline at leisure the way weights can.
What each scheme saves
| What is quantized | What it saves | Where it helps |
|---|---|---|
| Weights only (W8A16 / W4A16, weight-only) | Capacity + bandwidth, no compute savings | decode (bandwidth-bound) |
| Weights + activations (W8A8) | Plus compute | Also prefill (compute-bound) |
| KV cache | Capacity + attention bandwidth; whether attention computes in FP8 depends on the kernel | Long context, high concurrency |
6What AWQ / GPTQ are, and how they relate to absmax ÷ 448
If you have looked into quantization at all, you have probably come across terms like "AWQ" and "GPTQ". Here is what they mean.
The "absmax ÷ range + round-to-nearest" recipe above is called RTN (round-to-nearest). With 8 bits there are enough ticks that RTN is usually fine; INT4 has only 16 ticks, and RTN error becomes too large.
AWQ and GPTQ are both designed mainly for 4-bit weights (W4A16). They do not introduce a new data format; what they change is which value each weight ends up with. The output looks the same as RTN: low-bit weights plus scales. Packing formats and zero-point conventions differ between implementations, and frameworks pick the matching kernel based on quant_method, but at inference time they all do W4A16 dequantization plus GEMM.
| Method | What it minimizes | How |
|---|---|---|
| RTN | ‖W − W′‖ (error of the weights themselves) | absmax ÷ range, round to nearest |
| AWQ (arXiv 2306.00978) | ‖WX − W′X‖ (error of the output; needs calibration data to obtain X) | Weight channels multiplied by large activations matter more: scale them up before quantizing to reduce their relative error |
| GPTQ (arXiv 2210.17323) | Quantize column by column; after each column, adjust the remaining unquantized weights to compensate for the error (using second-order information computed from calibration data) |
- W: original weights
- W′: weights after quantization and dequantization
- X: activations on the calibration data
For infra, all of this happens offline. At inference time, all you see is a quant_method field in the config. What AWQ / GPTQ buy you is keeping accuracy from dropping too much at 4 bits.
7Reading a quantized model's config
Take the public DeepSeek-V4.1-Flash as an example. All quantization-related fields live under quantization_config in config.json:
| Field | Meaning |
|---|---|
quant_method | Name of the quantization scheme; the framework picks kernels based on it |
activation_scheme | dynamic = activation scales computed on the fly at inference; static = fixed scales from calibration |
weight_block_size | Weight block granularity; [32, 32] = one scale per 32×32 block |
scale_fmt = ue8m0 | The scale has only 8 exponent bits and no mantissa → it can only be an integer power of 2 |
expert_dtype = fp4 | Routed expert weights use FP4 E2M1 |
The comment in the reference code (linear() in the public inference/model.py) reads:
Following the dispatch rules in linear():
- Linear layers with fp8 weights are W8A8.
- Experts with fp4 weights are W4A8.
To see exactly which layers stay in BF16, check the safetensors.
Other quantized models commonly have a few more fields: fmt (e.g. e4m3), bits / group_size (INT4-style schemes), and modules excluded from quantization (e.g. lm_head). The safetensors also contain the corresponding scale tensors (e.g. weight_scale_inv).
8How to measure quantization accuracy
As mentioned earlier, the 32x32 quantization granularity of DeepSeek V4.1 Flash hurts performance on Hopper. I tried requantizing the 32x32 quantized weights into 128x128 blocks, and performance improved substantially.
But how do you tell whether a new quantization scheme hurts accuracy? The rough approach is below; I may cover it in a separate article later. In the end, metrics such as top-1 token agreement and KL divergence fell short, the accuracy was not acceptable, and I dropped the 128x128 approach.
At the distribution level, NLL (negative log-likelihood) is the same quantity as cross-entropy in this setting, and perplexity = exp(NLL).
Also, measure accuracy for every inference configuration, including high concurrency. Don't check accuracy only with serial requests and then go straight to benchmarking performance under high concurrency.
Summary: which fields to check in a quantized model
Here is everything above condensed into one table: which field to check, which resource it saves, and where it pays off:
| Field to check in a quantized model | What it tells you | Resource saved | Where it pays off |
|---|---|---|---|
Weight bit width (quant_method / dtype) | Bytes per weight | Capacity + bandwidth | decode (bandwidth-bound) |
activation_scheme, whether activations are quantized (A8 / A16) | Whether low-precision Tensor Cores are used | Compute | prefill (compute-bound) |
| KV cache dtype | Bytes per KV entry | Capacity (+ attention bandwidth) | Long context, high concurrency |
weight_block_size, scale_fmt | Scale granularity and format | Accuracy / kernel path | Whether it is actually faster, and whether accuracy drops |