INT4 Quantization › Worked Example: Tracing a Weight Through INT4 Quantization
Language AI HandbookAll chapters INT4 Quantization › Worked Example: Tracing a Weight Through INT4 QuantizationOne section · Sections in this chapter6 / 11 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
To solidify your understanding of the full quantization pipeline, let's trace a single group of weights through the complete NF4 quantization and dequantization cycle. This numerical example shows exactly what happens at each step, making the abstractions concrete.
Suppose we have a small group of 8 weights (we use 8 for clarity; in practice groups are larger):
Step 1: Compute the group maximum absolute value.
The first step is to identify the scale of this group by finding the largest absolute weight value:
Step 2: Normalize the group to the range expected by NF4.
NF4 is designed for data in the range approximately . We normalize by dividing by :
Note that the largest absolute value becomes exactly 1.0 after normalization, and all other values fall within .
Step 3: Map each normalized weight to the nearest NF4 level.
We look up each normalized value in the NF4 table. Let's trace one value: the weight , which normalized to . Scanning the canonical NF4 codebook, the nearest level is approximately , so this weight gets integer index 1.
After mapping all 8 weights:
| Original | Normalized | NF4 Level | NF4 Index |
|---|---|---|---|
| 0.031 | 0.356 | ~0.3379 | 11 |
| -0.054 | -0.621 | ~-0.6962 | 1 |
| 0.012 | 0.138 | ~0.1609 | 9 |
| 0.087 | 1.000 | ~1.0000 | 15 |
| -0.023 | -0.264 | ~-0.2844 | 4 |
| 0.065 | 0.747 | ~0.7230 | 14 |
| -0.041 | -0.471 | ~-0.5251 | 2 |
| 0.019 | 0.218 | ~0.2461 | 10 |
Each NF4 index (0-15) can be stored in exactly 4 bits.
Step 4: Store the scale factor and quantized indices.
We store two things: the scale factor (in FP16) and the 8 quantized indices (4 bits each). Total storage: 16 bits for the scale plus bits for the indices, for 48 bits total. The original 8 FP16 weights used bits. We achieved a 2.67x compression for this group.
Step 5: Dequantization.
When we need to use this weight during inference, we reconstruct by looking up each index in the NF4 table and multiplying by the scale:
For example, index 11 maps to NF4 level , giving reconstructed weight . The original was 0.031, so the absolute error is approximately 0.0016, well within the local quantization step size.
This worked example reveals several important properties of the NF4 + group-wise pipeline:
- The scale factor carries the magnitude information; the NF4 index carries the shape information
- Reconstruction error is bounded by the NF4 step size times the scale factor
- The largest weight in each group (here 0.087) always maps to the extreme NF4 level (index 15), guaranteeing zero error for the maximum
- Reconstruction accuracy is best for large weights and can be relatively coarser for small weights, but those small errors have less impact on the matrix product
# Reproduce the worked example numerically
worked_weights = np.array(
[0.031, -0.054, 0.012, 0.087, -0.023, 0.065, -0.041, 0.019]
)
# Step 1: find max absolute value
s_max = np.max(np.abs(worked_weights))
# Step 2: normalize
w_norm = worked_weights / s_max
# Step 3: find nearest NF4 level for each normalized weight
nf4_arr = np.array(nf4_levels)
dists = np.abs(w_norm[:, None] - nf4_arr[None, :])
q_indices = np.argmin(dists, axis=1)
q_levels = nf4_arr[q_indices]
# Step 4: dequantize
w_recon = q_levels * s_maxWorked Example: NF4 Group Quantization
----------------------------------------------------------------------
Original Normalized NF4 Level Index Recon Error
----------------------------------------------------------------------
0.0310 0.3563 0.3379 11 0.0294 0.001601
-0.0540 -0.6207 -0.6962 1 -0.0606 0.006569
0.0120 0.1379 0.1609 9 0.0140 0.002001
0.0870 1.0000 1.0000 15 0.0870 0.000000
-0.0230 -0.2644 -0.2844 4 -0.0247 0.001746
0.0650 0.7471 0.7230 14 0.0629 0.002103
-0.0410 -0.4713 -0.5251 2 -0.0457 0.004681
0.0190 0.2184 0.2461 10 0.0214 0.002412
----------------------------------------------------------------------
Scale factor: 0.087000
Mean absolute error: 0.002639
Max absolute error: 0.006569