← Master Index
Vol. 06 Module 6.3 Lecture

INT8

GPU Computing (added)

How This Lesson Fits the Module

FP16 and BF16 are still floating-point formats. INT8 stores weights and activations as 8-bit integers with a separate scale (and sometimes zero point) mapping real values into 256 discrete levels. That is roughly 4× smaller than FP32 and fast on tensor cores INT8 paths—the dominant precision for mobile, edge, and high-QPS server inference.

INT8 is rarely used for full training from scratch; it shines after a model is trained in FP32/BF16. The capstone Quantization lecture wires INT8 into end-to-end deployment pipelines.

Learning Objectives

By the end of this lesson, students should be able to:

  • Explain symmetric and asymmetric INT8 quantization with scale and zero point.
  • Distinguish weight-only vs activation quantization.
  • Describe post-training quantization (PTQ) vs quantization-aware training (QAT).
  • Run a basic INT8 dynamic quantization example in PyTorch.
  • Anticipate accuracy loss and how calibration data mitigates it.

From Float to INT8

Quantization maps a floating value x to integer q:

Symmetric: q = round(x / scale), dequantize as x ≈ q × scale.

Asymmetric: q = round(x / scale + zero_point), allowing the zero of the float range to map to a non-zero integer—useful for skewed activation distributions.

FormatBitsValuesTypical Use
FP3232ContinuousTraining master weights
FP16 / BF1616ContinuousMixed precision training
INT88−128…127 (signed)Inference weights & activations
Compression A 7B-parameter model: ~28 GB FP32 → ~7 GB INT8 weights alone. Combined with kernel fusion, latency drops on tensor core INT8 GEMMs.

Weight-Only vs Full INT8

Weight-Only INT8

  • Weights quantized; activations stay FP16/FP32
  • Easier PTQ, smaller accuracy hit
  • Common in LLM serving (W8A16)

Weight + Activation INT8 (W8A8)

  • Maximum speed and memory win
  • Needs calibration or QAT
  • Standard in CV edge deployment

PyTorch: Dynamic INT8 Quantization

Dynamic quantization converts weights to INT8 at load time; activations quantized on the fly during inference. Great first step for LSTM/Linear models on CPU; GPU INT8 paths vary by backend.

import torch import torch.nn as nn class TinyNet(nn.Module): def __init__(self): super().__init__() self.fc1 = nn.Linear(784, 256) self.fc2 = nn.Linear(256, 10) def forward(self, x): return self.fc2(torch.relu(self.fc1(x))) model = TinyNet() model.eval() quantized = torch.quantization.quantize_dynamic( model, {nn.Linear}, dtype=torch.qint8 ) x = torch.randn(1, 784) with torch.inference_mode(): y_fp32 = model(x) y_int8 = quantized(x) print("Max diff:", (y_fp32 - y_int8).abs().max().item())

Calibration and Accuracy

Static INT8 chooses scale / zero_point by running representative batches (calibration set) and recording activation min/max or histograms (entropy, percentile methods). Skipping calibration—or using mismatched data—causes clipping and accuracy collapse.

Critical Mistake — Quantizing Without Eval

Always measure accuracy, latency, and memory on a held-out set after INT8 conversion. A 0.5% metric drop may be acceptable for 3× speedup; a 5% drop usually is not.

Misconception — INT8 Means 8× Speed Always

Speedups depend on kernel support, memory bandwidth, and whether activations are quantized. Unfused CPU paths may show modest gains; GPU tensor core INT8 GEMMs show the largest wins.

Knowledge Check

  1. Short Answer: How many values can signed INT8 represent? Answer: 256 (−128 to 127).
  2. Short Answer: What is a quantization scale? Answer: Float factor mapping integer q back to approximate real value x ≈ q × scale.
  3. True/False: INT8 is standard for training LLMs from scratch. Answer: False—inference/deployment focus; training uses FP16/BF16/FP32.
  4. Multiple Choice: Dynamic quantization quantizes weights: (a) at train time, (b) at inference with on-the-fly activations, (c) never. Answer: (b) for activations; weights at convert time.
  5. Short Answer: What is a zero point? Answer: Integer offset in asymmetric quantization aligning float zero to an int value.
  6. Short Answer: What is calibration? Answer: Running sample data to choose scales for activations (static quant).
  7. Short Answer: What does W8A16 mean? Answer: 8-bit weights, 16-bit activations.
  8. True/False: INT8 always halves model accuracy. Answer: False—well-calibrated INT8 often loses <1% on vision tasks.
  9. Multiple Choice: Larger compression vs FP32: (a) FP16, (b) BF16, (c) INT8. Answer: (c).
  10. Short Answer: PyTorch function for quick linear INT8? Answer: torch.quantization.quantize_dynamic.

Key Takeaways

  • INT8 maps floats to 256 levels via scale (and optionally zero point).
  • Primary win: smaller models and faster inference, especially W8A8 on tensor cores.
  • Use calibration or QAT to control accuracy loss; always re-evaluate metrics.
  • Weight-only INT8 is the gentlest entry; full INT8 maximizes performance.
  • Next: Quantization — the full deployment workflow capstone.
Trainer’s Guide

Hands-on idea: Quantize a small MLP dynamically; plot accuracy vs latency on CPU. Discuss when GPU INT8 backends (TensorRT, torch.compile) are needed.

Discussion prompt: Why do LLM servers often use weight-only INT4/INT8 while activations stay FP16?

What’s Next INT8 is one piece of the puzzle. The Quantization capstone unifies PTQ, QAT, and production export.