LLM Quantization and Compression: A Developer Guide

How LLM quantization works and how GPTQ, AWQ, SmoothQuant, bitsandbytes, GGUF and FP8 differ, plus pruning, distillation and serving support in vLLM.

By AI Agents DirectoryLast checked
A desk with a computer and a small robot figure on it

Quantization stores a model’s numbers in fewer bits so it fits in less memory and, with the right kernels, runs faster. It is the main tool people use to fit open-weight models such as the ones in our open source LLMs overview onto a single GPU or a laptop. This guide explains what is being quantized, how the main post-training methods differ, where pruning and distillation fit, and which serving engines load which formats. Each method is described from its original paper or official documentation, linked inline.

What quantization changes

A model is mostly large matrices of weights. During inference those weights are multiplied by activations, the intermediate values that flow between layers. Quantization can target either one:

  • Weight-only (often written W4A16 or W8A16): weights are stored in 4 or 8 bits, activations stay in 16-bit. The saving is mostly memory.
  • Weights and activations (W8A8): both sides of the matrix multiply use 8-bit numbers, so the hardware can run integer or FP8 math.

The Hugging Face Transformers docs put the memory side simply: models usually load in bf16 at 2 bytes per parameter, so the weights of an 8B model take roughly 16GB. The same docs put that at roughly 8GB in 8-bit and 4-5GB in 4-bit. The rule behind those numbers is plain arithmetic (parameters x bits / 8 bytes) and covers weights only. The KV cache and activations need memory on top of that.

The llama.cpp quantize README gives measured file sizes for Llama 3.1. The 8B model drops from 32.1 GB to 4.9 GB at its Q4_K_M type, and the 70B model from 280.9 GB to 43.1 GB. (Derived: 32.1 GB for 8B parameters matches 4 bytes per weight, so the “original” column is 32-bit.)

Fewer bits cost some accuracy. llama.cpp says that loss is usually measured with perplexity or KL divergence, which is a good habit for any method: compare the quantized model against the original on your own prompts before shipping it.

Post-training methods

All of the methods below are post-training quantization: they take a finished model and convert it, with no retraining.

MethodWhat gets quantizedKey ideaSource
GPTQWeights, 3-4 bits (down to 2-bit or ternary in extreme tests)One-shot, uses approximate second-order informationarXiv 2210.17323
AWQWeights, low-bit (INT3/4 in the reference code)Uses activation statistics to find and protect important weight channelsarXiv 2306.00978
SmoothQuantWeights and activations, 8-bit (W8A8)Moves outlier difficulty from activations into weightsarXiv 2211.10438
LLM.int8()Weights and activations, 8-bit, with 16-bit outliersVector-wise quantization plus mixed-precision outlier handlingarXiv 2208.07339
NF4 (QLoRA)Weights, 4-bitA 4-bit data type suited to normally distributed weightsarXiv 2305.14314
FP8Weights and activations, 8-bit floatE4M3 and E5M2 encodingsarXiv 2209.05433

GPTQ

GPTQ is a one-shot weight quantizer: it uses approximate second-order information to convert the weights in a single pass, with no retraining. Its authors report quantizing a 175-billion-parameter GPT model in about four GPU hours at 3 or 4 bits per weight, which let them run that model on a single GPU. They also measured inference speedups over FP16 of around 3.25x on an NVIDIA A100 and 4.5x on an A6000. Transformers notes that GPTQ is the method in its list that calibrates on a dataset while the model loads.

AWQ

AWQ (Activation-aware Weight Quantization) starts from the observation that weights differ in importance, and that the activations, not the weights themselves, show which channels matter. Rather than keeping those channels in higher precision, which is awkward for hardware, AWQ scales them up before quantizing so they lose less information. The scales come from activation statistics gathered offline, without backpropagation, which the authors say helps it avoid overfitting the calibration set. The paper ships with TinyChat, a 4-bit inference framework that it reports as more than 3x faster than the Hugging Face FP16 implementation. The llm-awq repository records AWQ’s integration into Transformers and TensorRT-LLM.

SmoothQuant

Weights are fairly easy to quantize, while activations contain large outliers. SmoothQuant applies an offline, mathematically equivalent rescaling that shifts that difficulty from activations to weights, so both can be stored as INT8 for every matrix multiply. It is training-free. The paper reports up to 1.56x speedup and 2x memory reduction, and serving a 530B model within a single node.

bitsandbytes: 8-bit and 4-bit at load time

The bitsandbytes integration in Transformers quantizes weights as you load the model, with no calibration step. It offers two modes:

  • 8-bit (LLM.int8()). Most values are multiplied in 8-bit, and outlier feature dimensions are kept in a 16-bit multiply. The LLM.int8() paper reports that this halves inference memory while keeping full-precision performance on models up to 175B parameters. The llm_int8_threshold setting controls what counts as an outlier, and the docs suggest 6 as a default.
  • 4-bit (NF4 or FP4). The NF4 data type comes from the QLoRA paper. Nested (“double”) quantization quantizes the quantization constants as well and saves another 0.4 bits per parameter, according to the docs.

Here is the 4-bit example from the Transformers quantization overview:

import torch
from transformers import AutoModelForCausalLM, BitsAndBytesConfig

quantization_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_compute_dtype=torch.bfloat16,
)
model = AutoModelForCausalLM.from_pretrained(
    "meta-llama/Llama-3.1-8B-Instruct",
    quantization_config=quantization_config,
    device_map="auto",
)

Setting the compute dtype to bf16 speeds up computation compared with the float32 default. Add bnb_4bit_quant_type="nf4" if you plan to train on top of the 4-bit model. For inference the docs say the quant type makes little difference. The docs list support for NVIDIA GPUs on CUDA 11.8 to 13.0, Intel XPU, Intel Gaudi and CPU.

GGUF and llama.cpp quant types

GGUF is the single-file format used by llama.cpp and other GGML-based tools. Unlike safetensors, the Hugging Face GGUF docs note that it holds metadata alongside the tensors. Quantizing takes two steps: convert the original model to GGUF, then run llama-quantize:

./build/bin/llama-quantize gemma-4-E2B-it-bf16.gguf gemma-4-E2B-it-Q4_K_M.gguf Q4_K_M

The type names encode the scheme. Legacy types such as Q4_0 and Q8_0 use round-to-nearest over blocks of 32 weights. K-quants (Q2_K to Q6_K) group blocks into super-blocks with their own scales; the Hub docs list Q4_K at 4.5 bits per weight and Q6_K at 6.5625. I-quants (IQ1_S to IQ4_NL) lean on an importance matrix and reach down to about 1.56 bits per weight. The --imatrix flag supplies that matrix to reduce error, and the README warns that requantizing an already quantized file can cut quality badly. Start from a 16-bit or 32-bit source.

One catch: Transformers can open GGUF files, but it dequantizes most of them to full precision on load, so the memory saving only applies when you run them in llama.cpp or another GGML runtime.

FP8

FP8 is an 8-bit floating-point format defined in two encodings: E4M3 (4 exponent bits, 3 mantissa bits) and E5M2 (5 exponent, 2 mantissa). The 2022 paper that proposed it matched 16-bit training quality on models up to 175B parameters. It also tried FP8 post-training quantization on language models trained in 16-bit that had resisted INT8 fixed-point quantization. Serving engines tie FP8 support to newer GPUs (see the next section).

Pruning and distillation

Quantization is not the only way to shrink a model.

  • Pruning deletes weights. SparseGPT showed that GPT-family models can be pruned to at least half sparsity in one shot without retraining, and that it supports 2:4 and 4:8 semi-structured patterns. Its authors say it combines with weight quantization.
  • Distillation trains a smaller student model to match a larger teacher’s outputs. Hinton, Vinyals and Dean’s 2015 paper framed it as compressing an ensemble’s knowledge into one easier-to-deploy model. Unlike the methods above, the student has to be trained.

Serving support

Both engines below can load checkpoints that were quantized ahead of time with a separate tool. The LLM infrastructure category lists related tools.

vLLM. The vLLM quantization docs point to LLM Compressor for producing FP8, INT8 and INT4 checkpoints. vLLM’s hardware table shows:

Format in vLLMVoltaTuringAmpereAdaHopper
AWQNoYesYesYesYes
GPTQYesYesYesYesYes
bitsandbytesYesYesYesYesYes
GGUFYesYesYesYesYes
llm-compressor FP8 (W8A8)NoNoNoYesYes
llm-compressor INT8 (W8A8)NoYesYesYesYes

The docs say the chart changes as vLLM evolves, so check the current page before choosing hardware.

TensorRT-LLM. TensorRT-LLM recipes include NVFP4, FP8 (per tensor, block scaling and rowwise), W4A16 and W4A8 for both AWQ and GPTQ, and FP8 or NVFP4 KV cache. Its quantization docs say the default PyTorch backend supports FP4 and FP8 on Blackwell and Hopper GPUs, and that it runs checkpoints produced by NVIDIA Model Optimizer directly:

llm = LLM(model='nvidia/Llama-3.1-8B-Instruct-FP8')

Transformers. The library integrates more than 20 methods through from_pretrained(). Some, including AWQ and GGUF, can only be loaded from a checkpoint made with their own tools.

Picking a method

  • Quick experiments or QLoRA fine-tuning on a GPU: bitsandbytes 4-bit with NF4.
  • Production GPU serving with weight-only 4-bit: AWQ or GPTQ checkpoints in vLLM or TensorRT-LLM.
  • Hopper, Ada or Blackwell GPUs and throughput-bound workloads: FP8 W8A8.
  • Local use with llama.cpp or other GGML tools: GGUF, trying a higher-bit K-quant if quality drops.

Whatever you pick, measure accuracy on your own task. Every paper above reports results on its own benchmarks and models, and those results do not transfer automatically.

#Quantization#LLM inference#GPTQ#AWQ#GGUF
AI

Written by AI Agents Directory

Editorial team

Facts in this guide are checked against official documentation and repositories, which are linked in the article.