15.4 FP8 and microscaling formats
Float32 weights and activations move four bytes per value, and for inference that memory traffic is most of the cost. FP8 stores each value in one byte. The microscaling (MX) formats go further: they group values into blocks that share one scale, and with FP4 elements they store four bits per value. These formats reduce memory traffic dramatically, but every conversion into them has edge cases: ties, tiny magnitudes, signed zero, infinities, NaNs, and values beyond the finite range. Get any of those wrong and the quantized model disagrees with every other implementation by one code in a way that is very hard to trace.
Concepts from other sections (6)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- Round to nearest, ties to even: IEEE 754’s default rounding, which breaks exact ties toward the code whose last kept bit is 0. Read more: 15.1 IEEE 754 anatomy, ulps, and round to nearest even
- MX block scale: a shared power of two that places a block’s largest magnitude in the element format’s top binade. Read more: 15.9 Problem set B: condition numbers, error bounds, Newton, polynomial approximation, and low precision
- Subnormal: a value with exponent field 0, no implicit leading 1, and a fixed exponent , filling the gap between zero and the smallest normal. Read more: 15.1 IEEE 754 anatomy, ulps, and round to nearest even
- ties to even: rounding a value exactly halfway between two representable numbers to the one with an even last bit. Read more: 23.2 Candle model runner (Llama + bigram, int4), Rust KV cache, sampler and PCG32 in AI Engineering
- Biased exponent: the stored exponent field , equal to the true exponent plus a bias (127 in float32, 15 in float16) so it can be stored unsigned. Read more: 15.1 IEEE 754 anatomy, ulps, and round to nearest even
- Dot product: ; it measures how much two vectors point the same way, and it is zero exactly when they are perpendicular. Read more: Chapter 10: Calculus 3
This section writes the conversions directly from the format definitions and checks the resulting bit patterns exactly. It reuses the bit layouts and power-of-two reasoning of IEEE 754 anatomy, ulps, and round to nearest even, and it assumes you have rounded values by hand in Problem set A. The key idea is that a single rounding routine, parameterized by fraction bits and bias, serves every format; the edge rules are layered on top of it, and the block scale is chosen so that it never adds a rounding of its own.
The reference implementation is lowp.py: FP8 E4M3 and E5M2 conversion (f32_to_fp8_bits, fp8_bits_to_f32, quantize_fp8, dequantize_fp8, fp8_scale), FP4 E2M1 values (f32_to_e2m1_bits, e2m1_bits_to_f32), block scales (e8m0_scale_code, e8m0_to_f32), and MX quantize and dequantize (mx_quantize, mx_dequantize). Its interface is in lowp.pyi. An optional standalone C version, Low-precision conversions in C, produces the same bit patterns and does not load this Python code.
Learning objectives
- Describe the layouts of E4M3, E5M2, E2M1, and E8M0, including their largest finite values and special codes.
- Round any value into an FP8 or FP4 format with round to nearest, ties to even, using one generic routine.
- Apply saturation, infinity, NaN, and signed-zero rules without disturbing the rounding path.
- Choose an MX block scale from the block maximum and explain why it adds no rounding.
The formats
Section titled “The formats”An IEEE-like binary format uses a sign bit, an exponent field, and a fraction field. For a normalized value with fraction width and bias , the decoded value is
Subnormals omit the implicit leading one and use the minimum exponent. E4M3 uses four exponent bits and three fraction bits, giving more precision near one than E5M2. E5M2 uses two fraction bits and therefore covers a wider range. The project defines saturation and special-value behavior explicitly, so callers do not depend on a library default. The four formats compare as follows:
| Format | Exponent bits, bias | Fraction bits | Max finite | Specials |
|---|---|---|---|---|
| E4M3 (E4M3FN) | 4, 7 | 3 | 448 (0x7E) | no infinity; 0x7F is NaN; overflow and saturate |
| E5M2 | 5, 15 | 2 | 57344 (0x7B) | 0x7C is , 0x7D to 0x7F NaN; finite overflow saturates |
| FP4 E2M1 | 2, 1 | 1 | 6 (0x7) | none: values |
| E8M0 (scale) | 8, 127 | 0 | 0xFF is NaN; no zero, no sign |
Rounding is round to nearest, ties to even. A tie at 2.5 selects 2; a tie at 3.5 selects 4. FP8 encoding and decoding use exact powers of two to map between values and codes. Microscaling formats apply one E8M0 power-of-two scale to a fixed-size block, then encode each scaled value in FP8 or E2M1.
Working it by hand
Section titled “Working it by hand”Start with a scalar tie. In E4M3, 1.0625 lies halfway between the adjacent representable values 1.0 and 1.125. Their final significand bits are even and odd respectively, so ties to even encodes 1.0 (code 0x38). A value just above the midpoint, such as 1.07, encodes 1.125 (0x39). The next midpoint, 1.1875, lies between 1.125 (odd) and 1.25 (even) and so rounds up to 0x3A: ties to even is not “ties down”.
An MX block needs one more step. First find the largest absolute element. Choose the shared power-of-two scale that moves that maximum into the element format’s top binade, then divide each value by the scale and round once into the element format. Decoding multiplies each value by the same scale.
Take the FP4 block . The largest magnitude is 5, , and FP4’s largest normal is , so and the scale is (E8M0 code ). Each element then rounds to the FP4 grid : , , is a tie between 4 (code 6, even) and 6 (code 7, odd) and goes to 4, and . The codes are [1, 11, 6, 0] (11 is code 3 with the sign bit 0x8), the scale is [127], and the decoded block is . Notice that the block maximum itself lost precision; that cost comes up again when we discuss the scale rule.
Building it step by step
Section titled “Building it step by step”The solution rests on seven observations, each of which becomes a few lines of code.
A format is a grid of quanta per binade. In the binade a format with fraction bits holds the multiples of . Below the smallest normal, , the spacing stops shrinking: subnormals are multiples of . So for a magnitude the quantum exponent is .
Rounding is then one integer rounding. is exact in float64 (a power-of-two scale), and rounding it to an integer with ties to even is exactly IEEE rounding into the format. One routine serves E4M3, E5M2, and E2M1; only differ.
The code falls out of and . The biased exponent field is (one less than the field, because already includes the implicit bit), and the code is . When rounding carries from to , the addition carries into the exponent field by itself; for subnormals the same formula gives field 0 and , or field 1 when a subnormal rounds up to the smallest normal.
# name: (fraction bits M, bias B, largest finite magnitude code)_FMT = {"e4m3": (3, 7, 0x7E), "e5m2": (2, 15, 0x7B), "fp4_e2m1": (1, 1, 0x7)}_MAX = {"e4m3": 448.0, "e5m2": 57344.0, "fp4_e2m1": 6.0}_EMAX = {"fp4_e2m1": 2, "fp8_e4m3": 8} # 6 = 1.5 * 2^2, 448 = 1.75 * 2^8
def _round_codes(a: NDArray, fmt: str) -> NDArray: """int64 magnitude codes of the finite float64 magnitudes a >= 0, rounded to nearest even, before any overflow handling (may exceed the max code).""" M, B, _ = _FMT[fmt] _, e = np.frexp(a) # a = f * 2^e with f in [0.5, 1): floor(log2 a) = e - 1 qe = np.maximum(e.astype(np.int64) - 1, 1 - B) - M # the quantum exponent n = np.rint(np.ldexp(a, -qe)).astype(np.int64) # exact scale, then ties to even code = ((qe + M + B - 1) << M) + n # carries into the exponent return np.where(n == 0, 0, code) # underflow to zero is code 0This is the whole of the rounding logic for three formats. frexp gives exactly, with no log2 rounding near powers of two; ldexp scales without rounding; numpy’s rint rounds half to even. Trace in E4M3: , so ; rounds to 10; the code is $((-3 + 3 + 7 - 1) \ll 3) + 10 = 48 + 10 = 58 = $ 0x3A. For , frexp gives , , and the where returns code 0.
The edge rules are kept separate from rounding. Saturation, infinity, NaN, and the sign are applied after the magnitude code is known, as a few wheres, which keeps the rounding path free of special cases. Decoding is exact: a code splits into exponent field and mantissa; field 0 is , otherwise , and both are exact in float32 for every code.
def _decode(mag: NDArray, fmt: str) -> NDArray: """float64 values of magnitude codes (no specials).""" M, B, _ = _FMT[fmt] mag = mag.astype(np.int64) ef, mant = mag >> M, mag & ((1 << M) - 1) sub = np.ldexp(mant.astype(np.float64), 1 - B - M) # no implicit 1 nor = np.ldexp(((1 << M) + mant).astype(np.float64), (ef - B - M).astype(np.int64)) return np.where(ef == 0, sub, nor)
def _encode_fp8(v: NDArray, fmt: str) -> NDArray: """uint8 codes of float64 values v (any shape), with the edge rules.""" _check_fp8(fmt) v = np.asarray(v, dtype=np.float64) nan, inf = np.isnan(v), np.isinf(v) a = np.where(nan | inf, 0.0, np.abs(v)) # keep specials out of the rounding code = np.minimum(_round_codes(a, fmt), _FMT[fmt][2]) # saturate finite overflow if fmt == "e4m3": code = np.where(inf, 0x7E, code) # no infinity: saturate else: code = np.where(inf, 0x7C, code) # e5m2 keeps IEEE infinity sign = np.where(np.signbit(v), 0x80, 0) # signbit, so -0.0 keeps its sign return np.where(nan, 0x7F, code | sign).astype(np.uint8)The order of operations is the design. Specials are masked to 0 before rounding, so frexp never sees them. The rounded magnitude is clamped to the largest finite code (0x7E or 0x7B), so overflow can never produce E4M3’s NaN code 0x7F or E5M2’s infinity by accident. Only then are infinities, sign, and NaN set. np.signbit is used rather than v < 0 because is false and the sign of zero must survive.
Scaling must not round twice. quantize_fp8 divides by the scale in float64 and rounds once to FP8; a float32 intermediate would round first and could land on the other side of a midpoint.
def fp8_bits_to_f32(codes: ArrayLike, fmt: str) -> NDArray: # ... fmt check; codes must lie in 0..255 ... c = c.astype(np.int64) mag = c & 0x7F v = _decode(mag, fmt) if fmt == "e4m3": v = np.where(mag == 0x7F, np.nan, v) # the only NaN magnitude else: v = np.where(mag == 0x7C, np.inf, np.where(mag > 0x7C, np.nan, v)) return np.where(c & 0x80, -v, v).astype(np.float32) # -0.0 for 0x80
def quantize_fp8(x: ArrayLike, fmt: str, scale: float) -> NDArray: _check_scale(scale) return _encode_fp8(np.asarray(x, dtype=np.float64) / scale, fmt) # one rounding
def dequantize_fp8(q: ArrayLike, fmt: str, scale: float) -> NDArray: _check_scale(scale) return (fp8_bits_to_f32(q, fmt).astype(np.float64) * scale).astype(np.float32)f32_to_fp8_bits is _encode_fp8 of the input converted to float32 first (the contract says the input is a float32), while quantize_fp8 forms in float64 and rounds once. fp8_scale(amax, fmt) is amax / fp8_max(fmt), or 1 for an all-zero tensor.
The last observation concerns MX. The MX scale is a power of two chosen from the block maximum: puts the largest element in the element format’s top binade. Because is a power of two, is exact, so the element rounding is the only rounding.
def e8m0_scale_code(amax: float, elem: str) -> int: _check_elem(elem) # ... amax must be finite and >= 0 ... a = 1.0 if amax == 0 else amax # an all-zero block gets scale 2^-emax e = math.frexp(a)[1] - 1 # floor(log2 a), exact return int(min(max(e - _EMAX[elem] + 127, 0), 254)) # 255 is NaN, never produced
def mx_quantize(x, block=32, elem="fp4_e2m1") -> tuple[NDArray, NDArray]: fmt = _check_elem(elem) v = np.asarray(x, dtype=np.float64) if not np.isfinite(v).all(): raise ValueError("mx_quantize needs finite values") blk = _blocks(v, block) # [..., n / block, block]; raises if not divisible amax = np.max(np.abs(blk), axis=-1) # the maximum, not an average scales = np.vectorize(lambda a: e8m0_scale_code(float(a), elem), otypes=[np.int64])(amax) q = np.ldexp(blk, (127 - scales)[..., None]) # x / X, exact: X is a power of two if fmt == "fp4_e2m1": codes = f32_to_e2m1_bits(q) # rounds once, saturates at 6 else: codes = _encode_fp8(q, "e4m3") return codes.reshape(v.shape), scales.astype(np.uint8)mx_dequantize reverses it: view the codes as blocks, check that the scales have one entry per block, decode the elements, and multiply by e8m0_to_f32(scales) in float64 before one rounding to float32. In the hand example, amax = 5, frexp(5) gives exponent 3, so and the code is ; ldexp(blk, 0) leaves the block unchanged, and f32_to_e2m1_bits produces [1, 11, 6, 0]. The full solution is in lowp.py.
Why this design
Section titled “Why this design”The solution works on values, not bits. Its C twin works on float32 bit fields; this file works on float64 values, where scaling by and rint are exact. That makes one generic routine correct for three formats, vectorized over any array shape, at a cost of with a few array passes.
It prefers generic rounding to a lookup table. For 256 codes, a sorted table of decoded values and a nearest search would also work, but tie handling needs care at every midpoint and the table must be rebuilt per format. The arithmetic route makes ties to even a property of rint.
It saturates rather than overflowing to infinity. E4M3 has no infinity, and for quantized weights an outlier that becomes NaN or infinity poisons every dot product it enters; clamping to the largest finite value bounds the damage. E5M2 keeps IEEE infinity for inputs that already are infinite.
And it follows the OCP MX scale rule. is what the OCP Microscaling spec defines, so codes interoperate. The rule has an accepted cost: the block maximum can land above the element format’s largest value (amax in FP4 gives , and 7 saturates to 6), and, as in the hand example, the maximum itself can lose precision. A scale that always fits the maximum would need a non-power-of-two scale or one more exponent step, losing a bit of resolution for every other element.
What can go wrong
Section titled “What can go wrong”The tests pin the scalar tie and the MX block of the hand examples, then sweep the whole code space: every 8-bit code is decoded and known values are encoded against stable expected bit patterns. Saturation and special values define behavior for overflow, infinities, and NaNs. Tiny values and signed zero protect underflow and sign preservation at zero. Encoding must invert decoding, so every finite code is stable through decode then encode. Rounding must be nearest and monotone, with ties to even, which catches incorrect midpoint handling and non-monotone encoders. Scaled quantization must round-trip and round once, so that scaling introduces no extra rounding step. Finally, FP4 values and ties, E8M0 scale codes, the MX block law, and MX shapes and errors check element codes, scale choices, block layout, and input validation.
The mistakes that those tests catch:
| Pitfall | Symptom | How the solution avoids it |
|---|---|---|
| Rounding half away from zero | Exact midpoints choose the wrong code. | np.rint rounds half to even on the scaled integer |
| Treating subnormals like normalized values | Tiny values jump to zero or the minimum normal. | qe is floored at and _decode drops the implicit 1 for field 0 |
| Producing infinity on finite overflow | Codes disagree with the saturation rule. | np.minimum(..., max_code) before the special rules |
| Applying the block scale after rounding | Quantized values differ by one code near boundaries. | divides by the scale first (exact ldexp for MX, float64 for FP8), then rounds once |
| Choosing scale from an average instead of the maximum magnitude | A large block member clips unexpectedly. | amax = np.max(np.abs(blk), axis=-1) |
| Accepting malformed block shapes | Values are silently dropped or grouped incorrectly. | _blocks raises unless the last axis is a multiple of block; mx_dequantize checks the scale shape |
Where this leads
Section titled “Where this leads”In AI Engineering, Quantization: int8, int4 groups, fp8, and the KV cache quantizes model weights for lower-bandwidth inference with these conversions, and Candle model runner (Llama + bigram, int4), Rust KV cache, sampler and PCG32 converts quantized model weights in the Rust engine. In Software Engineering, Interface migration: KV format v1 to v2 stores fp8 KV values and relies on the saturation and decoding rules defined here. In parallel, Low-precision conversions in C implements selected conversions in standalone C with the same bit-exact results.
In production, FP8 E4M3 and E5M2 are accelerator matrix formats that trade exponent range against significand precision, and the block-scaled MX formats let compact values share a power-of-two scale per block.
Further reading
Section titled “Further reading”- OCP Microscaling Formats (MX) v1.0 specification.
- Micikevicius et al., “FP8 Formats for Deep Learning” (2022).
- IEEE 754 and vendor format guides for FP8 matrix instructions.
Key terms
Section titled “Key terms”- E4M3: an 8-bit float with 4 exponent bits (bias 7) and 3 fraction bits, no infinity, largest finite value 448.
- E5M2: an 8-bit float with 5 exponent bits (bias 15) and 2 fraction bits, IEEE-style infinities and NaNs, largest finite value 57344.
- E2M1 (FP4): a 4-bit float whose only magnitudes are .
- E8M0: an 8-bit unsigned power-of-two scale with bias 127, used as the shared scale of an MX block.
- Saturation: mapping finite overflow to the largest finite code instead of infinity or NaN.
- Microscaling (MX): a block format in which a fixed number of low-precision elements share one power-of-two scale.
Summary
Section titled “Summary”- Each FP8 or FP4 format is a grid fixed by its fraction bits and bias ; the quantum exponent is .
- Dividing by is exact in float64, so
rint(ties to even) is the whole rounding step, and the code formula carries into the exponent on its own. - Special values, saturation, and the sign are applied after rounding, in a fixed order that prevents overflow from producing NaN or infinity codes by accident.
- Scaling must happen before the single rounding, in float64 for FP8 and as an exact power of two for MX.
- The MX scale follows the OCP spec, at the accepted cost that the block maximum can saturate or lose precision.
Review questions
Section titled “Review questions”-
Why does one function,
_round_codes, round correctly into E4M3, E5M2, and E2M1?Answer
Each format is determined by : the quantum in the binade of is with . Dividing by a power of two is exact in float64 and
rintrounds to the nearest integer with ties to even, which is IEEE rounding onto that grid. The code formula then carries into the exponent field when the rounded mantissa overflows, so no format needs its own branch. -
Why is the rounded code clamped to
0x7Ebefore the NaN and infinity rules are applied in E4M3?Answer
A finite value above 448 rounds to a code of
0x7For more, and0x7Fis E4M3’s NaN. Clamping first turns any finite overflow into 448, the contract’s saturation rule; the laterwheres then set infinities to0x7Eand NaN to0x7Fexplicitly, so NaN can only come from a NaN input. -
What goes wrong if
quantize_fp8convertsx / scaleto float32 before encoding?Answer
It rounds twice. The float32 rounding can move a value that is just above an FP8 midpoint onto the midpoint itself (or across it), and the second rounding then picks the even neighbour instead of the nearer one. Forming the quotient in float64 and rounding once into FP8 gives the correctly rounded code.
-
Why is the MX scale a power of two, and why chosen from the block maximum?
Answer
A power of two makes exact, so the only error is the element rounding, and E8M0 can store it in 8 bits. The maximum fixes the range: choosing from an average lets the largest element exceed the element format and saturate, which on weights is the outlier that matters most.
-
Why does
_encode_fp8usenp.signbit(v)rather thanv < 0to set the sign bit?Answer
is false, so a comparison would encode negative zero as positive zero (
0x00).np.signbitreads the sign bit itself, so becomes0x80and decodes back to .