INT4 Quantization › Worked Example: Tracing a Weight Through INT4 Quantization

Michael BrenndoerferJanuary 12, 20264 min read
Language AI HandbookAll chapters
Part 42 of 67Inference Optimization
Chapter 319INT4 Quantization › Worked Example: Tracing a Weight Through INT4 Quantization
Reading section 6 of 11 · Worked Example: Tracing a Weight Through INT4 Quantization · 4 minSection 6 of 11 · 4 min

Book contents

Section 6 of 11 · 4 min read

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):

w=[0.031,−0.054,0.012,0.087,−0.023,0.065,−0.041,0.019]\mathbf{w} = [0.031, -0.054, 0.012, 0.087, -0.023, 0.065, -0.041, 0.019]

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:

smax⁡=max⁡(∣w∣)=0.087s_{\max} = \max(|\mathbf{w}|) = 0.087

Step 2: Normalize the group to the range expected by NF4.

NF4 is designed for data in the range approximately [−1,1][-1, 1]. We normalize by dividing by smax⁡s_{\max}:

wnorm=wsmax⁡=[0.356,−0.621,0.138,1.000,−0.264,0.747,−0.471,0.218]\mathbf{w}_{\text{norm}} = \frac{\mathbf{w}}{s_{\max}} = [0.356, -0.621, 0.138, 1.000, -0.264, 0.747, -0.471, 0.218]

Note that the largest absolute value becomes exactly 1.0 after normalization, and all other values fall within [−1,1][-1, 1].

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 −0.054-0.054, which normalized to −0.621-0.621. Scanning the canonical NF4 codebook, the nearest level is approximately −0.6962-0.6962, so this weight gets integer index 1.

After mapping all 8 weights:

OriginalNormalizedNF4 LevelNF4 Index
0.0310.356~0.337911
-0.054-0.621~-0.69621
0.0120.138~0.16099
0.0871.000~1.000015
-0.023-0.264~-0.28444
0.0650.747~0.723014
-0.041-0.471~-0.52512
0.0190.218~0.246110

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 smax⁡=0.087s_{\max} = 0.087 (in FP16) and the 8 quantized indices [11,1,9,15,4,14,2,10][11, 1, 9, 15, 4, 14, 2, 10] (4 bits each). Total storage: 16 bits for the scale plus 8×4=328 \times 4 = 32 bits for the indices, for 48 bits total. The original 8 FP16 weights used 8×16=1288 \times 16 = 128 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:

w^i=NF4[qi]×smax⁡\hat{w}_i = \text{NF4}[q_i] \times s_{\max}

For example, index 11 maps to NF4 level ≈0.3379\approx 0.3379, giving reconstructed weight 0.3379×0.087≈0.02940.3379 \times 0.087 \approx 0.0294. 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
In[36]:
Code
# 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_max
Out[37]:
Console
Worked 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