← CS Lab AI & Machine Learning / Quantization Open standalone ↗

Quantization stores a model's weights in fewer bits — float32 down to int8 or int4 — so it gets smaller and faster, for a small accuracy cost. The tool above shows the geometry: a coarser grid, fewer levels, weights snapping to the nearest one.

Where you meet it

What it does

Inference doesn't need full floating-point precision. Quantization maps each weight from a wide float range onto a small set of integers, keeping just enough resolution to preserve behaviour. You trade a controlled amount of numerical accuracy for roughly 4× less memory (fp32 → int8) and faster, cheaper math.

Fewer bits only buy speed if your hardware has the int kernels — otherwise you just shipped a smaller model that runs no faster.

How it works

A float tensor is mapped to integers with a scale and a zero-point. The scale is the step size between two integer levels; the zero-point is the integer that represents the real value 0:

scale = (max - min) / (qmax - qmin)
x_q   = round(x / scale) + zero_point      # quantize
x     = scale * (x_q - zero_point)         # dequantize (approx.)

With b bits there are 2^b levels: int8 gives 256 steps, int4 only 16. Memory is linear in bits, so int8 is ~4× smaller than fp32 and int4 ~8×. Integer matmul also runs faster on hardware that supports it. Two ways to get there:

Watch out

  • Too few bits hurts. Down to int8 quality is nearly untouched; int4 and below need care, and naive sub-4-bit quantization can break the model.
  • Outlier activations. A few large values blow up the scale and crush everything else. Fixes: per-channel / per-group scales, or mixed precision — LLM.int8() keeps outlier dimensions in fp16.
  • PTQ needs representative calibration data. Bad calibration → bad scales → silent quality loss on real inputs.
  • Hardware has to support it. Real speedups need int8/int4 kernels on your GPU/CPU; otherwise weights are just dequantized back to fp16 on the fly (smaller, not faster).

Go deeper

Quantisierung speichert die Gewichte eines Modells in weniger Bits — float32 runter auf int8 oder int4 — dadurch wird es kleiner und schneller, gegen einen kleinen Genauigkeitsverlust. Das Tool oben zeigt die Geometrie: ein gröberes Raster, weniger Stufen, Gewichte rasten auf die nächste.

Wo es vorkommt

Was es tut

Inferenz braucht keine volle Fließkomma-Präzision. Quantisierung bildet jedes Gewicht aus einem breiten Float-Bereich auf eine kleine Menge ganzer Zahlen ab — gerade genug Auflösung, um das Verhalten zu erhalten. Man tauscht einen kontrollierten Anteil numerischer Genauigkeit gegen rund 4× weniger Speicher (fp32 → int8) und schnellere, billigere Rechnung.

Weniger Bits bringen nur Speed, wenn deine Hardware die Int-Kernels hat — sonst hast du nur ein kleineres Modell, das gleich langsam läuft.

Wie es funktioniert

Ein Float-Tensor wird mit einer Skala (scale) und einem Zeropoint auf ganze Zahlen abgebildet. Die Skala ist die Schrittweite zwischen zwei Integer-Stufen; der Zeropoint ist die ganze Zahl, die den reellen Wert 0 darstellt:

scale = (max - min) / (qmax - qmin)
x_q   = round(x / scale) + zero_point      # quantisieren
x     = scale * (x_q - zero_point)         # dequantisieren (approx.)

Mit b Bits gibt es 2^b Stufen: int8 liefert 256 Schritte, int4 nur 16. Der Speicher ist linear in Bits, also ist int8 ~4× kleiner als fp32 und int4 ~8×. Integer-Matmul läuft zudem schneller auf Hardware, die es unterstützt. Zwei Wege dorthin:

Worauf achten

  • Zu wenige Bits schadet. Bis int8 bleibt die Qualität fast unberührt; int4 und darunter brauchen Sorgfalt, und naive Sub-4-bit-Quantisierung kann das Modell zerstören.
  • Ausreißer-Aktivierungen. Wenige große Werte sprengen die Skala und drücken alles andere platt. Abhilfe: per-channel- / per-group-Skalen oder mixed precision — LLM.int8() hält Ausreißer-Dimensionen in fp16.
  • PTQ braucht repräsentative Kalibrierungsdaten. Schlechte Kalibrierung → schlechte Skalen → stiller Qualitätsverlust bei echten Eingaben.
  • Die Hardware muss es unterstützen. Echte Speedups brauchen int8-/int4-Kernels auf GPU/CPU; sonst werden die Gewichte nur on-the-fly zurück nach fp16 dequantisiert (kleiner, nicht schneller).

Mehr dazu

Next up
Programming FoundationsPython OOP Programming FoundationsPython Error Handling All concepts →