LLM DAILYA field guide to language models

Day 14 / 3 min read

Quantization — how 16-bit weights become 4-bit weights

Quantization stores model numbers with fewer bits.

Suppose a learned weight is:

\[ w=0.13742 \]

The model may behave almost identically if that becomes 0.137 or 0.14.

Across billions of weights, removing unnecessary precision can save enormous memory and bandwidth.

A simple 4-bit example

Suppose:

\[ W=[-0.82,-0.31,0.04,0.27,0.63,0.91] \]

Use only 16 possible codes because:

\[ 2^4=16 \]

Choose scale:

\[ s=0.125 \]

Quantize:

\[ q=\mathrm{round}(w/s) \]

For \(w=0.63\):

\[ 0.63/0.125=5.04\rightarrow q=5 \]

Reconstruct:

\[ \hat w=sq=0.625 \]

Error:

\[ 0.005 \]

For \(0.27\):

\[ 0.27/0.125=2.16\rightarrow2 \]
\[ \hat w=0.25 \]

Groupwise scaling

One scale for an entire model is poor because different regions have different numeric ranges.

So weights are often quantized in groups, for example:

weights 1–128   → scale A
weights 129–256 → scale B
...

Smaller groups improve fidelity at the cost of metadata.

Why inference benefits

70B parameters:

BF16:

\[ 70B\times2\text{ bytes}\approx140GB \]

Idealized 8-bit:

\[ 70GB \]

Idealized 4-bit:

\[ 35GB \]

Decode is often bandwidth-bound, so moving 35 GB rather than 140 GB can radically change throughput.

But compute support matters

Low-bit storage is useful only if kernels/hardware can efficiently dequantize or directly multiply low-precision values.

The stack must align:

\[ \text{model}\rightarrow \text{format}\rightarrow \text{kernel}\rightarrow \text{hardware} \]

Why training is harder

If representable values near a weight are only:

\[ 0.50,\quad0.625,\quad0.75 \]

an optimizer update:

\[ 0.625\rightarrow0.6248 \]

may round straight back to 0.625 and vanish.

Hence mixed precision and quantization-aware methods.

The broad principle:

\[ \boxed{\text{use high precision only where the computation actually needs it}} \]

Quantization as noise

A useful mathematical view is:

\[ \hat W = W+\epsilon \]

where \(\epsilon\) is quantization error.

The goal is to choose scales, grouping, and representations so the network’s computation is insensitive to that error.

Some weights are much more sensitive than others, particularly outliers. Good quantization methods often devote more precision or special treatment to these difficult values.

Weight-only versus activation quantization

Weight-only quantization shrinks persistent model storage and weight bandwidth while leaving activations at higher precision.

Quantizing activations can unlock more acceleration, but activation ranges vary dynamically with inputs, making the problem harder.

KV caches can also be quantized, which is particularly attractive for long-context serving.

Why 4-bit is not literally “every number has only 16 global choices”

Practical systems use many local scales and sometimes zero-points. Each small block may have its own mapping between low-bit codes and real values.

That lets 4 bits work surprisingly well despite the apparent coarseness.

Accuracy-throughput trade-off

A lower-bit model is valuable only if quality remains acceptable.

Thus deployment is an empirical decision:

\[ \boxed{\text{memory saved + speed gained} \quad\text{vs}\quad \text{quality lost}} \]

For some models/tasks, 4-bit is excellent. For others, 8-bit or higher precision is justified.

Quantization and matrix multiplication

Suppose the original operation is:

\[ y=Wx. \]

With quantized weights we store integer-like codes \(Q\) and scale metadata \(s\), conceptually:

\[ W\approx sQ. \]

A kernel can either reconstruct values on the fly or use hardware instructions that directly operate on supported low-precision formats.

The best implementations fuse these steps so temporary full-precision copies of the weights are never written back to HBM.

Otherwise much of the bandwidth advantage would disappear.

KV-cache quantization

Long-context serving may use enormous KV caches even after weight quantization.

Quantizing cached K/V values attacks a different memory term:

\[ \text{KV bytes} \propto T\times H_{KV}\times d_{head}\times bytes/value. \]

Dropping cache precision from 16 to 8 bits roughly halves that component.

Again the engineering question is not simply “can we compress it?” but “does attention remain accurate enough after compression?”

Calibration and quantization

Quantization parameters are often chosen from representative calibration data.

The goal is to observe realistic activation or weight ranges and select scales that minimize harmful error.

Poor calibration can waste much of the available numeric range or clip important outliers.

This is a reminder that low-bit inference is not simply a file-conversion step. Good quantization is a small model-optimization problem in its own right, involving the distribution of values the network actually uses.

LLM Daily Last updated