AI engineering and inferenceLoading progress…

Floating formats

Estimate weight memory at different precisions and explain quantization error, scales and calibration.

Number formats

Model weights are stored as numbers, and the format decides memory, range and detail. A floating-point number splits its bits into a sign, an exponent (the range) and a mantissa (the detail). FP16 uses 5 exponent and 10 mantissa bits; BF16 uses 8 and 7: the same 16 bits, but BF16 has the range of 32-bit floats and coarser detail. FP8 formats exist in more than one layout, so a bit count alone does not specify the arithmetic.

FP16
sign 1exponent 5mantissa 10
BF16
sign 1exponent 8mantissa 7
FP16BF16
exponent bits58
mantissa bits107
largest finite value65,5043.390e+38
gap between neighbours near 12⁻¹⁰ = 0.00097662⁻⁷ = 0.0078125
value near 1 (offset ×2⁻¹⁰)1.001953125
x = 1.001953125 stored as FP16: 1.001953125 stored as BF16: 1 Same width, different trade: FP16 keeps finer detail, BF16 keeps a much wider range.

A bit count alone does not define an arithmetic contract: the exponent/mantissa split, rounding and special values matter. FP8 comes in more than one layout too.

Pick a value near 1 and compare how each format stores it.

Predict first. Are FP16 and BF16 numerically identical because both use 16 bits?

Chapter context

Quantization represents values with a smaller set of numerical codes. Memory savings are easy to estimate nominally, but quality and speed depend on scales, calibration, kernels and which tensors use the smaller representation.

Chapter reference: math, worked example & code

This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.

Math and notation

Nominal packed weight memory is

M=N b/8  bytes.M=N\,b/8\;\text{bytes}.

For affine quantization, q=clip⁡(round⁡(x/s)+z)q=\operatorname{clip}(\operatorname{round}(x/s)+z) and x^=s(q−z)\hat x=s(q-z). Here s is scale, z zero point and clipping uses the chosen integer range. Metadata and mixed-precision tensors add storage.

Worked example

Seven billion weights at 16 bits occupy 14 billion bytes (14 decimal GB) nominally. At 4 bits they occupy 3.5 decimal GB, a fourfold nominal reduction. Separately, x=0.23 with s=0.1,z=0 rounds to q=2 and reconstructs 0.2, an absolute error of 0.03.

Reference code

The following example uses the Python standard library and does not call a remote service.

from math import isclose
count=7_000_000_000
assert count*16/8 == 14_000_000_000
assert count*4/8 == 3_500_000_000
q=round(.23/.1)
assert q == 2
assert isclose(abs(.23-.1*q),.03)

Python examples are provided for study and local execution; this page does not execute Python.

Common mistakes

Nominal bit packing excludes scales, padding and other state. Different 16-bit float formats have different ranges and precision. Quantization quality and kernel speed both need measurement.