Expand description
F8_E4M3/F8_E5M2 weight quantization.
Both are natively supported safetensors dtypes (see the pinned safetensors 0.7’s
Dtype::F8_E4M3/Dtype::F8_E5M2), so unlike INT4 (see crate::quant::pack4) no packing
convention is needed – this module only has to do the bit-level f32 <-> f8 conversion and
per-group amax-based scale computation, since the crate depends on neither the half crate
(f16/bf16 only, no f8) nor any other f8 implementation.
Encoding follows the OCP FP8 spec: E4M3 is the “FN” variant (no infinities; the single
exponent=1111/mantissa=111 bit pattern is reserved for NaN, freeing up the rest of that
exponent for finite values up to 448). E5M2 is IEEE-754-like (has infinities). Rounding is
round-to-nearest-even; out-of-range magnitudes saturate (to 448/57344 for E4M3/E5M2
respectively, or to infinity for E5M2, which has one). Subnormal outputs are flushed to
zero rather than rounded into the target format’s subnormal range – an accepted
simplification for weight quantization, where values within about one ULP of the smallest
normal (2^-9 for E4M3, 2^-16 for E5M2) are negligible relative to the scale factor
applied before conversion. Decoding handles subnormals and NaN/Inf fully, since it also needs
to correctly read back bytes this module didn’t itself produce.
Structs§
- Quantized
Fp8 - An
f32tensor quantized tof8bytes, onef32scale per group (the tensor is scaled by1 / scalebefore conversion, matching the affine schemes’ convention of storing a multiplicative dequantization scale).
Enums§
- Fp8Variant
- Which OCP FP8 encoding to target.
Functions§
- dequantize_
fp8 - Reconstructs
f32values from aQuantizedFp8. - f8_
to_ f32 - Decodes an
f8byte (variant) back tof32. Unlikef32_to_f8, this fully handles subnormals, since it may be asked to decode bytes this module didn’t itself produce. - f32_
to_ f8 - Encodes
xas anf8byte invariant. See the module docs for rounding/saturation behavior. - quantize_
fp8 - Quantizes
data(row-major, shapeshape) tovariant, scaling each group so itsamaxmaps to the format’s largest finite value.