Learn AI Math

Topic 6 of 20 · 38 words

Quantization & Model Compression

Shrinking AI models to run faster and fit on smaller hardware using 8-bit and 4-bit quantization, GGUF, AWQ, GPTQ, and calibration.

✎ Napkin Diagram · Topic 06Quantization: Vacuum-Packing a Giant AI into a Carry-On Bag
PNG ↗
Quantization: Vacuum-Packing a Giant AI into a Carry-On Bag Simple cartoon showing a huge 16 GB AI model with long fussy decimals (3.14159265) squeezed by a vacuum-seal press into a compact 4 GB model with rounded numbers (3.1) that fits on a laptop. BEFORE: FULL-SIZE MODELGiant 16 GB TrunkStores every number withfussy extra decimal digits:3.14159265358979Too big for a normal laptop!Round it off!(16-bit → 4-bit)AFTER: QUANTIZED (4-BIT)Compact 4 GB Carry-On!Just keep "3.1"✓ 4× smaller in memory✓ Runs faster · nearly as smart!Like packing a puffy winter coat into a vacuum bag—same coat, quarter the space. getaimath.com
By rounding long fussy decimals to neat short numbers, quantization shrinks a model about 4× with only a small loss in smarts.

The 38 words, in learning order

Start at 1. Each word builds on the ones before it.

  1. QuantizationQuantization makes a model smaller and faster by storing its numbers with fewer bits.
  2. Post-Training Quantization (PTQ)Post-training quantization shrinks a model after it has finished training, without retraining it.
  3. Quantization-Aware Training (QAT)Quantization-aware training teaches a model during training to cope with low-precision numbers, so it loses less quality when quantized.
  4. Weight QuantizationWeight quantization stores a model's learned weights in fewer bits.
  5. Activation QuantizationActivation quantization stores the temporary numbers that flow through a model while it runs in fewer bits.
  6. Dynamic QuantizationDynamic quantization decides how to scale activations on the fly, for each input, while the model runs.
  7. Static QuantizationStatic quantization picks all the scaling settings ahead of time, using a sample of example data.
  8. Mixed PrecisionMixed precision uses different number formats for different parts of a model or task, to balance speed and accuracy.
  9. Full PrecisionFull precision usually means storing model numbers in 32-bit floating point, FP32.
  10. Half PrecisionHalf precision means storing numbers in 16 bits, like FP16 or BF16, half the size of FP32.
  11. Integer QuantizationInteger quantization converts model numbers into whole numbers, like INT8 or INT4, plus scale factors.
  12. 8-bit Quantization8-bit quantization stores each weight in 8 bits, halving the size of a 16-bit model.
  13. 4-bit Quantization4-bit quantization stores each weight in about 4 bits, shrinking a 16-bit model to roughly a quarter of its size.
  14. 2-bit Quantization2-bit quantization stores each weight in about 2 bits, making models extremely small but often noticeably worse.
  15. GGUFGGUF is a file format for storing AI models, especially quantized ones, made for llama.cpp and tools like Ollama and LM Studio.
  16. GGMLGGML is a machine learning library written in C for running models efficiently on regular computers, and also the name of its older file format.
  17. GPTQGPTQ is a method for quantizing models to 4 bits or fewer after training, while carefully keeping quality high.
  18. AWQAWQ, or Activation-aware Weight Quantization, protects the most important weights so a model stays accurate at 4 bits.
  19. EXL2EXL2 is a quantization format for the ExLlamaV2 engine that lets you pick any average bit level, like 4.65 bits per weight.
  20. bitsandbytesbitsandbytes is a popular Python library for loading and training models in 8-bit or 4-bit on NVIDIA GPUs.
  21. HQQHQQ, or Half-Quadratic Quantization, is a fast quantization method that does not need any calibration data.
  22. NF4NF4, or 4-bit NormalFloat, is a 4-bit number format designed for neural network weights, used in QLoRA.
  23. Q4_K_MQ4_K_M is a popular GGUF quantization level that uses about 4 to 5 bits per weight with smart mixing, giving a good balance of size and quality.
  24. Q5_K_MQ5_K_M is a GGUF quantization level using about 5 to 6 bits per weight, with higher quality than Q4 and a bit larger size.
  25. Q8_0Q8_0 is a GGUF quantization level that uses 8 bits per weight, giving quality almost identical to the original model.
  26. IQ QuantizationIQ quantization is a family of GGUF formats, like IQ2 and IQ3, that use clever tricks to keep models usable at very low bit levels.
  27. Importance Matrix (imatrix)An importance matrix is a record of which model weights matter most, gathered by running sample text through the model, and used to guide quantization.
  28. CalibrationCalibration in quantization means running sample data through a model to learn the typical range of its values before compressing it.
  29. DequantizationDequantization converts compressed low-bit numbers back into higher-precision numbers so they can be used in calculations.
  30. Quantization ErrorQuantization error is the difference between the original number and its compressed version.
  31. Per-Channel QuantizationPer-channel quantization gives each row or column of a weight matrix its own scale, instead of one scale for the whole matrix.
  32. Per-Tensor QuantizationPer-tensor quantization uses one single scale for an entire block of weights, called a tensor.
  33. Group-wise QuantizationGroup-wise quantization splits weights into small groups, like 32 or 128 numbers, and gives each group its own scale.
  34. Block-wise QuantizationBlock-wise quantization divides weights into fixed-size blocks, each with its own scale, much like group-wise quantization.
  35. Lossy CompressionLossy compression makes data smaller by throwing away some detail that cannot be perfectly recovered.
  36. Lossless CompressionLossless compression makes data smaller in a way that can be perfectly reversed, with no detail lost.
  37. Quantization Trade-offThe quantization trade-off is the balance between making a model smaller and faster and keeping its answers accurate.
  38. Compression RatioThe compression ratio compares a model's original size to its compressed size.

How Quantization & Model Compression fits together

Quantization is like rounding high-precision decimals to neat whole numbers on a compact scale: instead of storing every weight in 16 bits, you pack it into 8, 4, or even fewer bits. Because LLM token generation is usually bottlenecked by memory bandwidth rather than raw math speed, smaller weights mean both lower VRAM use and faster tokens per second.

Modern formats like GGUF (for local CPU/Apple Silicon) and AWQ or FP8 (for GPU servers) protect the tiny fraction of 'outlier' weights that matter most, keeping 4-bit and 8-bit models remarkably close to full-precision quality.