Sub topic 11
Fitting larger models with quantization
This chapter assumes you can start a server with vllm serve, and that you know what gpu_memory_utilization and max_model_len do. It also assumes you know that the KV cache is the GPU memory vLLM uses to hold attention state for running requests. Here you will learn what quantization changes, how to estimate memory before you load a model, and how to choose a format.
What quantization changes
A model's weights are stored as numbers. Most open-weight checkpoints use 16-bit floats (BF16 or FP16), which take 2 bytes per parameter. Quantization stores the weights with fewer bits, such as 8 or 4, plus small scale factors that map the stored values back to a usable range.
Smaller weights help in two ways:
- The model fits on a smaller GPU, or on fewer GPUs.
- Decoding reads fewer bytes per token. Decode is usually limited by memory bandwidth, so this can raise tokens per second.
Quantization also frees memory for the KV cache. vLLM gives the KV cache whatever remains of the gpu_memory_utilization budget after weights and activations, so smaller weights mean more cache blocks and more concurrent sequences.
The cost is accuracy. Quantized weights are approximations, so output quality can drop. The drop is usually small at 8 bits and larger at 4 bits or below. Measure it on your own task.
Estimating memory before you load
Weight memory is roughly parameters times bytes per parameter. Real checkpoints are somewhat larger because of scales and any layers kept in higher precision.
def weight_gib(params_billion: float, bits: int, overhead: float = 1.1) -> float:
"""Approximate weight memory in GiB, with a rough allowance for scales."""
return params_billion * 1e9 * (bits / 8) * overhead / 2**30
for bits in (16, 8, 4):
print(f"32B model at {bits}-bit: about {weight_gib(32, bits):.0f} GiB")For a 32B model this prints about 66, 33, and 16 GiB. A 24 GiB GPU cannot hold the 16-bit version. The 4-bit version fits, but it leaves little room for the cache.
Now check the cache. Qwen2.5-32B has 64 layers, 8 KV heads, and a head dimension of 128. Per token, the cache needs 2 (keys and values) × 64 × 8 × 128 × 2 bytes (16-bit), which is 256 KiB. A single 8192-token sequence therefore needs 2 GiB of cache. With 5 GiB left over after weights, you can hold only about two such sequences at once. Quantizing the weights does not shrink this figure.
Common quantization formats
| Format | Bits | How it is made | Typical use |
|---|---|---|---|
| FP8 | 8 | Can be applied at load time, no calibration data | Recent NVIDIA GPUs with FP8 support |
| AWQ | 4 | Pre-quantized checkpoint, calibrated offline | Fitting a model on a smaller GPU |
| GPTQ | 4 or 8 | Pre-quantized checkpoint, calibrated offline | Wide availability of community checkpoints |
Hardware support varies by format, GPU generation, and vLLM version, so check the quantization page in the vLLM documentation for your setup.
Loading a pre-quantized checkpoint
AWQ and GPTQ models are published as separate checkpoints. Their config.json records the quantization method, and vLLM reads it, so you normally do not need a flag.
vllm serve Qwen/Qwen2.5-32B-Instruct-AWQ \
--max-model-len 8192 \
--gpu-memory-utilization 0.90You can state the method explicitly. This is useful as a check, because vLLM reports an error if it does not match the checkpoint.
vllm serve Qwen/Qwen2.5-32B-Instruct-AWQ \
--quantization awq \
--max-model-len 8192The offline API takes the same options:
from vllm import LLM, SamplingParams
llm = LLM(
model="Qwen/Qwen2.5-32B-Instruct-AWQ",
quantization="awq",
max_model_len=8192,
gpu_memory_utilization=0.90,
)
params = SamplingParams(temperature=0.2, max_tokens=100)
outputs = llm.chat(
[{"role": "user", "content": "Name two trade-offs of 4-bit weights."}],
params,
)
print(outputs[0].outputs[0].text)Quantizing at load time with FP8
With FP8, you can start from the original 16-bit checkpoint and let vLLM convert the weights as it loads them.
vllm serve Qwen/Qwen2.5-7B-Instruct \
--quantization fp8 \
--max-model-len 8192This needs no calibration data and no separate checkpoint. The full 16-bit weights are still downloaded and read at startup, but the GPU holds roughly half the memory afterward. The GPU must support FP8 for the speed benefit. Check your vLLM version's documentation for what it does on older hardware.
Quantizing the KV cache
Weights are only part of the budget. The --kv-cache-dtype fp8 option stores cache entries in 8 bits instead of 16. This roughly doubles how many tokens fit in the same cache memory.
vllm serve Qwen/Qwen2.5-32B-Instruct-AWQ \
--kv-cache-dtype fp8 \
--max-model-len 16384Apply it when long contexts or many concurrent users are your limit rather than weight size. It affects accuracy separately from weight quantization, so test the two changes one at a time.
Choosing an approach
As a rule of thumb:
- If the model fits at 16 bits with enough cache, keep it. You avoid any accuracy risk.
- If it does not fit, try 8-bit first, then 4-bit. Each step costs more accuracy.
- If the cache is the limit, try KV cache quantization or a shorter
max_model_len. - If the model still does not fit, add GPUs with
--tensor-parallel-size, as in the previous chapter's discussion of scaling.
Common errors and gotchas
- Out-of-memory at startup. The weights fit, but there is no room for activations and at least one full-length sequence. Lower
max_model_len, or raisegpu_memory_utilizationslightly. - "Maximum model length is larger than the KV cache can hold." This is the same problem seen from the cache side. The error message reports the largest length that fits.
- Mismatched
--quantizationvalue. Passinggptqfor an AWQ checkpoint fails. Let vLLM auto-detect, or read the model card. - Assuming 4-bit is always faster. Throughput depends on kernels and on your batch size. At large batch sizes, decode becomes compute-bound and the gain shrinks. Benchmark before you commit.
- Judging quality from one prompt. Run a task-specific evaluation set on both the 16-bit and quantized models, and compare.
Exercise
A 14B-parameter model is loaded in 4-bit with about 10% overhead. Estimate the weight memory in GiB. Then explain why quantizing the weights does not change the KV cache memory needed per token.
Show answer
Weight memory is about 14e9 × 0.5 bytes × 1.1 ≈ 7.7e9 bytes, or roughly 7.2 GiB. The KV cache stores keys and values produced during attention, and these are computed activations, not weights. Their size depends on the number of layers, KV heads, head dimension, and cache dtype. It is unaffected by the weight format unless you also set --kv-cache-dtype.
Exercise
Your 24 GiB GPU runs a 4-bit model with 16 GiB of weights. Requests with long contexts are queuing even though the model loaded. Name two changes that increase the number of concurrent sequences, without changing the model.
Show answer
Quantize the KV cache with --kv-cache-dtype fp8, which about doubles the tokens that fit. Alternatively, lower maxmodellen so each sequence reserves less cache. Raising gpumemoryutilization slightly also helps if there is headroom, though it leaves less margin against out-of-memory errors.