- Wed 06 August 2025
- LLM-Inference
FP8 Block-wise Quantization for MoE
FP8 stores a number in one byte, half the size of BF16, and FP8 matrix multiplication runs at twice the BF16 rate on recent GPUs. The format has a much smaller range than BF16, so a tensor is scaled into that range before the cast and scaled back afterward. Block-wise quantization gives each 128×128 block of a weight matrix its own scale. DeepSeek-V3 trained and released its weights this way, and vLLM and TensorRT-LLM can serve such checkpoints directly.
The FP8 E4M3 format
The FP8 formats paper
by Micikevicius et al. defines two encodings. E4M3 has 4 exponent bits
and 3 mantissa bits, and E5M2 has 5 and 2. The paper recommends E4M3 for
weights and activations, which need precision, and E5M2 for gradients,
which need range. PyTorch calls the E4M3 variant
torch.float8_e4m3fn. The fn suffix means
finite: the format has no infinity, and it uses the freed bit patterns
to extend the largest value to 448.
With 3 mantissa bits, the gap between neighboring E4M3 values is 1/8 of a power of two. A cast rounds 100 to 96, for example. The normal values span from 2⁻⁶ (about 0.0156) to 448, a ratio of about 28,000. Smaller values become subnormal, with fewer significant bits, and values below 2⁻¹⁰ round to zero.
The cast
A scale maps the largest magnitude in a group of values to 448. The cast below comes from the tests of DeepGEMM, DeepSeek’s FP8 GEMM library (MIT license). It computes one scale per 128×128 block:
import torch
from typing import Tuple
def ceil_div(x: int, y: int) -> int:
return (x + y - 1) // y
def per_block_cast_to_fp8(x: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
assert x.dim() == 2
m, n = x.shape
x_padded = torch.zeros((ceil_div(m, 128) * 128, ceil_div(n, 128) * 128),
dtype=x.dtype, device=x.device)
x_padded[:m, :n] = x
x_view = x_padded.view(-1, 128, x_padded.size(1) // 128, 128)
x_amax = x_view.abs().float().amax(dim=(1, 3), keepdim=True).clamp(1e-4)
x_scaled = (x_view * (448.0 / x_amax)).to(torch.float8_e4m3fn)
return (x_scaled.view_as(x_padded)[:m, :n].contiguous(),
(x_amax / 448.0).view(x_view.size(0), x_view.size(2)))The function pads the matrix with zeros to a multiple of 128 in both
dimensions. It then views the padded matrix as
(m/128, 128, n/128, 128), so that dimensions 1 and 3 index
the rows and columns inside a block. amax(dim=(1, 3)) finds
the largest magnitude in each block. The clamp at 1e-4
keeps an all-zero block, such as one made entirely of padding, from
dividing by zero.
The function returns two tensors. The first is the FP8 matrix with
the original shape. The second holds one FP32 scale per block, with
shape (ceil(m/128), ceil(n/128)). The original value is
approximately the FP8 value times the scale of its block. A 4096×4096
weight has 1,024 blocks, so its scales take 4 KB next to 16 MB of FP8
data.
Per-tensor and per-block scales
A per-tensor scale maps the largest value in the whole matrix to 448. The rest of the matrix scales down with it, and small values can fall below the normal range of E4M3. A per-block scale confines that effect to the 128×128 block that holds the large value.
The experiment below measures how much this matters. It builds a 4096×4096 BF16 matrix with entries drawn from a normal distribution with standard deviation 0.02, then places 64 outliers at a chosen multiple of that typical size. It quantizes the matrix both ways and measures the relative error of the non-outlier entries, together with the fraction of non-zero entries that the cast rounds to zero:
| Outlier size | Per-tensor error | Per-block error | Zeroed, per-tensor | Zeroed, per-block |
|---|---|---|---|---|
| 10× | 2.65% | 2.64% | 0.0% | 0.0% |
| 100× | 2.65% | 2.64% | 0.0% | 0.0% |
| 1,000× | 2.65% | 2.64% | 0.2% | 0.0% |
| 10,000× | 2.71% | 2.65% | 1.7% | 0.1% |
| 100,000× | 12.59% | 3.98% | 17.2% | 1.0% |
Up to outliers about 1,000 times the typical value, both methods give the same error of about 2.6%. That error comes from rounding to 3 mantissa bits, and a finer scale cannot reduce it. Per-tensor scaling starts to lose small values only when the outliers approach the 28,000:1 normal range of E4M3. In that regime, per-block scaling keeps the error close to the rounding floor.
Trained weight matrices rarely have outliers that large. Activations can. LLM.int8() (Dettmers et al., 2022) found a few hidden dimensions with extreme magnitudes in every layer of transformers from about 6.7B parameters up. The DeepSeek-V3 technical report cites this sensitivity to activation outliers as the reason for fine-grained scaling. It quantizes activations in 1×128 tiles, one scale per token per 128 channels, computed at run time, and weights in 128×128 blocks.
Applying the scales in the matrix multiplication
In Y = X Wᵀ, the reduction runs over the hidden
dimension in steps of 128. Within one step, every activation value
shares one tile scale and every weight value shares one block scale. The
kernel computes the FP8 partial product for the step and multiplies it
by the two scales before adding it to an FP32 accumulator. The
DeepSeek-V3 report describes this design and its second purpose.
Tensor-core accumulation of FP8 products on H800 GPUs keeps fewer bits
than FP32, so the kernel moves the partial sums to FP32 every 128
elements, which is also where the scales apply.
Block-scaled MoE checkpoints
An MoE layer has separate gate, up and down projection weights for
each expert, and each of these matrices has its own block scales. The
DeepSeek-V3 checkpoint declares the scheme in its
config.json:
"quantization_config": {
"quant_method": "fp8",
"fmt": "e4m3",
"activation_scheme": "dynamic",
"weight_block_size": [128, 128]
}dynamic means the activation scales are computed at run
time. The weight
documentation names the per-block scale tensor
weight_scale_inv and stores it in FP32 next to each FP8
weight.
Serving frameworks read this configuration and select block-scaled kernels:
- vLLM. The FP8 quantization method in
fp8.pyacceptsweight_block_size. Setting the environment variableVLLM_USE_DEEP_GEMM=1routes supported FP8 GEMMs to DeepGEMM. - TensorRT-LLM. The quantization algorithm
QuantAlgo.FP8_BLOCK_SCALESselects block scaling. Its PyTorch MoE layer, built bycreate_moe, has several backends. TheTRTLLMbackend supports only block-scaled FP8 or NVFP4 weights and falls back to theCUTLASSbackend otherwise.
The kernels differ in speed by token count, so a comparison between backends sweeps the number of tokens from decode sizes up to prefill sizes.
References
- FP8 Formats for Deep Learning (Micikevicius et al., 2022)
- LLM.int8(): 8-bit Matrix Multiplication for Transformers at Scale (Dettmers et al., 2022)
- DeepSeek-V3 Technical Report (DeepSeek-AI, 2024)
- DeepGEMM
tests/test_core.py - DeepSeek-V3 weight documentation
- vLLM FP8 quantization method
- TensorRT-LLM
create_moe
If you found this useful, please cite this post using
Senthilkumar Gopal. (Aug 2025). FP8 Block-wise Quantization for MoE. sengopal.me. https://sengopal.me/posts/fp8-block-wise-quantization-for-moe
or
@article{gopal2025fp8blockwise,
title = {FP8 Block-wise Quantization for MoE},
author = {Senthilkumar Gopal},
journal = {sengopal.me},
year = {2025},
month = {Aug},
url = {https://sengopal.me/posts/fp8-block-wise-quantization-for-moe}
}