Quantize weights and KV cache in vLLM-TorchTPU¶
Use this guide when a model does not fit, when the KV cache runs out under load, or when decode is too slow at the bandwidth floor, and you want to serve lower-precision weights or KV on TPU. vLLM-TorchTPU repacks quantized weights once at load time, so the Pallas matmul, attention and fused MoE kernels run on the MXU's low-precision paths with no layout work per step. Chapter 18 shows what fp8 weights and fp8 KV do to the decode floor and to the number of sequences that fit; chapter 23 is how to prove the quantized model is still the model.
Prerequisites¶
- A working server for the unquantized or checkpoint-native model: Serve a first model with vLLM-TorchTPU.
- Reference outputs and an accuracy baseline from a build you trust (step 5 says how).
Steps¶
1. Find out what the checkpoint already is¶
Open the checkpoint's config.json. If it contains a quantization_config section (compressed-tensors, fp8, mxfp4 and so on), vLLM detects the format and you pass no --quantization. The flag is for overriding a format or for quantizing a bf16 checkpoint while it loads.
Check: you know whether the server needs --quantization at all. Most *-FP8 checkpoints in the recipes pass --quantization fp8 explicitly, which matches what they carry.
2. Choose the method¶
--quantization |
Weights | Activations | Layers | Use it for |
|---|---|---|---|---|
(auto) / fp8 |
float8_e4m3fn, per tensor, per channel or 2D blocks |
float8_e4m3fn or bf16 |
Linear, fused MoE, attention KV | Qwen3.5-*-FP8, Qwen3-Coder-*-FP8 |
compressed-tensors |
fp8 (W8A8), int4 (W4A8, W4A16), mxfp4 | fp8 or bf16 | Linear, fused MoE | Checkpoints with quant_method == "compressed-tensors"; selected automatically |
mxfp4 |
fp4 (e2m1) with e8m0 block scales, block 32 |
bf16 / fp8 | Linear, fused MoE | openai/gpt-oss-120b, openai/gpt-oss-20b, MXFP4 MoE checkpoints |
nvfp4 |
fp4 (e2m1) with fp8 block scales |
bf16 / fp8 | Linear, fused MoE | Qwen3-30B-A3B-NVFP4, Qwen3-Coder-480B-A35B-NVFP4 |
awq |
int4, packed | bf16 | Linear | Weight-only 4-bit AWQ checkpoints |
gptq |
int4 / int8 | bf16 | Linear | Weight-only GPTQ checkpoints |
deepseek_v4_fp8 |
float8_e4m3fn, optional fp4 MoE requantization |
float8_e4m3fn |
Linear, MLA, fused MoE | DeepSeek-V4 |
online_fp8 |
a bf16 checkpoint quantized to float8_e4m3fn while loading |
float8_e4m3fn |
Linear, fused MoE | Serving a bf16 checkpoint with fp8 weights |
Check: the method's weight dtype is the one your checkpoint holds, or online_fp8 for a bf16 checkpoint you want in fp8.
3. Serve fp8 weights with an fp8 KV cache¶
The vLLM-TorchTPU quantization guide's headline combination halves both the weight and the KV cache footprint and, on v6e and v7x, doubles MXU matrix throughput:
vllm serve Qwen/Qwen3-Coder-30B-A3B-Instruct-FP8 \
--tensor-parallel-size 8 \
--enable-expert-parallel \
--quantization fp8 \
--kv-cache-dtype fp8 \
--max-model-len 16384 \
--gpu-memory-utilization 0.95
--kv-cache-dtype fp8 works with any weight format; the v6e recipes pass it always, and the DeepSeek-V4 recipes write it fp8_e4m3.
Check: Application startup complete, then compare the KV capacity with the bf16 run: with the same --gpu-memory-utilization, the fp8 cache holds about twice the tokens. Chapter 18's kv_capacity() in code/ch18_vllm/launch_profiles.py gives the expected number.
4. Requantize at load time when the checkpoint's layout does not suit the MXU¶
For checkpoints whose block size or weight dtype is not a good fit for MXU tiling, environment variables requantize weights while they load:
| Variable | Default | Effect |
|---|---|---|
ENABLE_QUANTIZED_MATMUL_KERNEL |
0 |
The dedicated Pallas quantized matmul kernel for linear layers |
REQUANTIZE_WEIGHT_DTYPE |
"float8_e4m3fn" |
Target dtype for linear weights |
REQUANTIZE_BLOCK_SIZE |
None |
Target subchannel block size for linear weights |
TPU_FP8_LINEAR_W8A16_MAX_ROWS |
0 |
Opt-in: fp8 linear steps whose padded bucket has at most this many rows keep bf16 activations against fp8 weights (W8A16); speeds up small decode steps on v7x; per-channel fp8 path only, so the blockwise kernel and REQUANTIZE_WEIGHT_DTYPE=int8 are unaffected |
MOE_REQUANTIZE_WEIGHT_DTYPE |
"float8_e4m3fn" |
Target dtype for MoE expert weights; fp4 with MOE_FUSED_EP_ENABLE_W4A8=1 for W4A8 MoE |
MOE_REQUANTIZE_BLOCK_SIZE |
None |
Target block size for MoE requantization, for example 512 |
The quantization guide's W4A8 MoE example:
export USE_MOE_FUSED_EP_KERNEL=1
export MOE_FUSED_EP_ENABLE_W4A8=1
export MOE_REQUANTIZE_WEIGHT_DTYPE=fp4
export MOE_REQUANTIZE_BLOCK_SIZE=512
vllm serve deepseek-ai/DeepSeek-V4-Pro-0813 \
--tensor-parallel-size 1 \
--data-parallel-size 16 \
--enable-expert-parallel \
--quantization deepseek_v4_fp8 \
--kv-cache-dtype fp8_e4m3
This command needs 16 devices, a v7x-16 slice; the full recipe is in vLLM-TorchTPU recipes for TPU v7x.
Check: the variables are exported in the server's own environment (on a Ray multi-host cluster, listed in VLLM_RAY_EXTRA_ENV_VARS_TO_COPY).
5. Prove the quantized model is still the model¶
A quantization change changes what you serve. Record greedy outputs and an accuracy score on the trusted build, then compare the quantized one:
python code/ch18_vllm/client.py record --model YOUR_MODEL --out ref.json # trusted build
python code/ch18_vllm/client.py parity --model YOUR_MODEL --reference ref.json # quantized build
Exact greedy agreement is a strong pass; a divergence at a near tie can be legitimate between two numerics, so use the logprob gate with a tie rule from chapter 23 and the benchmarking harness's lm_eval accuracy gate (Benchmark vLLM-TorchTPU). Then measure throughput on the same machine with the same flags.
Check: parity passes or every divergence is a tie, the accuracy is within the gate's tolerance of the baseline, and only then the throughput gain counts.
Troubleshooting¶
| Symptom | Cause | Fix |
|---|---|---|
| Requests preempted under long contexts on v6e | bf16 KV on 32 GB chips | --kv-cache-dtype fp8 |
TPU_FP8_LINEAR_W8A16_MAX_ROWS changes nothing |
Blockwise fp8 kernel path or REQUANTIZE_WEIGHT_DTYPE=int8, which it does not affect; or the default 0 |
Per-channel fp8 weights; set a row count that covers your decode buckets |
| W4A8 MoE not used | MOE_REQUANTIZE_WEIGHT_DTYPE=fp4 without MOE_FUSED_EP_ENABLE_W4A8=1 (or the reverse) |
Set both, with USE_MOE_FUSED_EP_KERNEL=1 as in the example |
| Requantization variables ignored on some hosts | Ray actors receive only VLLM_ variables by default |
VLLM_RAY_EXTRA_ENV_VARS_TO_COPY |
| Outputs changed after quantizing | Quantization changes the model you serve | Gate on parity and evaluation (step 5) before it takes traffic |