Back to paths

Precision & Memory — Why AI Uses "Weird" Number Formats

bf16, fp16, fp4 — not arbitrary. A number's byte width is a throughput lever — halving the bytes a value occupies doubles arithmetic intensity and keeps the matrix unit fed. The range-vs-precision trade, why fp16 overflows where bf16 holds, and how byte width moves a workload across the roofline, for infra engineers with no calculus.

beginner~11 min#math-infra#numerics#performance
100Math for the Metal (arithmetic & shapes)Lesson 3 of 4Show

Prerequisites: What Is a Tensor? — A Multidimensional Array


The one-line summary

Every infra engineer meets bf16, fp16, fp4 and assumes the ML team is being fashionable. They're not — this is the most systems-y decision in the whole stack. A number's byte width is a throughput lever. Halve the bytes a value occupies and you halve the traffic it puts on the memory bus, which doubles its and keeps the expensive matrix unit fed instead of starved. The price is range and precision — and getting that wrong makes a training run silently blow up. This is the first number you reason about when you size memory or pick a config.

A float is range plus precision

A floating-point number splits its bits into two jobs:

  • the exponent sets the — how big or how small the value can get before it overflows to inf or underflows to 0;
  • the sets the precision — how finely it can tell nearby values apart.

bf16: range over precision

spends 4 bytes: an 8-bit exponent (huge range) and a 23-bit mantissa (fine precision). keeps the full 8-bit exponent — the same reach as fp32 — and throws away mantissa bits to fit in 2 bytes. The other 16-bit float, , makes the opposite bet: more mantissa, but only a 5-bit exponent, so it overflows to inf past ~65,504.

Why training picks range: the values in a model span a huge dynamic range — tiny gradients (the per-step correction signals) near 1e-7 up to activations (the intermediate values flowing through the model) near 1e5 — and only an 8-bit exponent covers both. The coarser bf16 precision averages out over millions of updates (rounding is random noise that cancels); lost range does not — one overflow to inf poisons everything downstream, and the run diverges.

Drag a value and watch fp16 break where bf16 holds, and how little fp4 can represent at all:

Predict before you drag: a loss scale pushes an activation to 70,000. What does fp16 hold, and what does bf16 hold?

Simulation: number-format-explorer

Why fewer bytes = a faster chip

Smaller isn't just cheaper to store — it's the lever that keeps the chip busy. The matrix unit can do far more math per second than can deliver bytes, so the real question is = FLOPs (floating-point math ops) ÷ bytes moved. Halve the bytes per number and you roughly double the intensity for the same math — pushing the workload from memory-bound (the idling, waiting on the bus) toward compute-bound (the matrix unit saturated, which is what you paid for).

That's the : below the ridge you're starved on bandwidth, above it you're saturating compute. Lower precision is one of the main levers that gets you over it. Same value slider, now read off how byte width moves the workload across the ridge:

Simulation: arithmetic-intensity-calculator

fp4 and where this is going

The trend only goes one way: fewer bytes. (the OCP E2M1 variant — 1 sign, 2 exponent, 1 mantissa) is arriving on the newest accelerators and runs at ~¼ bf16's bytes, roughly doubling throughput. The catch the sim makes obvious: with one mantissa bit and a ~0.5–6 range, fp4 rounds or overflows almost everything raw — so in practice values are scaled into range first. It's a throughput bet you take where you can afford the resolution loss (inference, parts of training).

One detail that ties it together: the matrix unit multiplies in low precision but accumulates partial sums in fp32 — a matmul builds each output by adding up a long string of products, and keeping that running sum in fp32 means the rounding doesn't pile up. You get the bandwidth win on the inputs without the running sum drifting.

Why this matters

Every memory and throughput decision you'll make starts here. Picking the precision sets your HBM footprint (bf16 halves a checkpoint vs fp32) and your effective bandwidth (fewer bytes = more intensity). Reading a roofline tells you whether to reach for lower precision or a bigger batch. And when a run dies in NaNs, "did someone push it into fp16 and overflow?" is a real first question. The engineer who treats number formats as ML trivia guesses; the one who sees them as a byte-width-vs-range trade can size and debug the system.

Where's the dial? Precision is usually the ML team's training-config flag — you'll see bf16/fp16/fp8 in the run config rather than flip it yourself — and arithmetic intensity isn't on a standard Grafana board; you read it off the roofline and the profiler's MFU, not a scraped metric. Your job isn't to set the dial — it's to recognize the trade, size the memory it implies, and spot the failure mode (the inf/NaN overflow) when it shows up.

Put it together

Steps
  1. Break fp16, keep bf16

    In the format explorer, push the value to a big activation (1e5). Which formats overflow, and which holds — and say in one sentence why, from the exponent bits.

  2. Move the workload across the ridge

    In the intensity calculator, switch the precision from fp32 to bf16 and read what happens to arithmetic intensity and to the memory-bound vs compute-bound verdict.

  3. Place fp4

    Explain why fp4 needs values scaled into range before it's usable, and what you're buying in return — in bytes and throughput.

Review

Flashcards1 / 4

A float = range + precision. Which bits do which?

Check yourself

Quick exam1 / 3 · 0 correct

Which 16-bit float overflows to infinity past ~65,504, and why?

Your notes on this lesson