Number Formats: FP32, FP16, BF16, FP8, INT8, INT4

Understanding floating-point and integer representations used in neural networks: IEEE 754 formats, the exponent-mantissa tradeoff, dynamic range vs precision, BFloat16 for training, FP8 for inference, and when to use each format.

Intermediate

Table of Contents

  1. Learning Objectives
  2. Notation
  3. Core Intuition
  4. IEEE 754 Floating-Point
  5. FP32: Full Precision
  6. FP16 vs BF16: The Training Tradeoff
  7. FP8: E4M3 vs E5M2
  8. Integer Formats: INT8 and INT4
  9. The Dynamic Range vs Precision Tradeoff
  10. Choosing the Right Format
  11. Common Pitfalls
  12. Summary
  13. Exercises

Learning Objectives

  1. Decode the bit layout of IEEE 754 floating-point numbers.
  2. Explain why BF16 succeeded FP16 for training (dynamic range matters more).
  3. Compare FP8 E4M3 vs E5M2 for forward vs backward passes.
  4. Calculate the precision and range of each format.
  5. Choose the appropriate format for training, fine-tuning, and inference.

Notation

  • ee — exponent bits, mm — mantissa (significand) bits
  • bias\text{bias} — exponent bias (2e112^{e-1}-1)
  • ULP — unit in last place (smallest representable difference)

Core Intuition

A number format is a tradeoff between three things: range (largest representable number), precision (smallest distinguishable difference), and storage (bits used). Training needs range (gradients can be huge or tiny); inference needs precision (weights are small, tightly distributed). Understanding this tradeoff is essential for choosing quantization strategies.

Number Formats Comparison

FP32: 0.730000FP320.7300 (err: 0.0000)FP160.7305 (err: 0.0005)INT80.7333 (err: 0.0033)INT40.7333 (err: 0.0033)
Value
0.73
FP32 (32b)FP16 (16b)INT8 (8b)INT4 (4b)
Explore: FP32 stores full precision; INT4 maps to only 16 levels. Lower bit widths compress memory but increase quantization error — especially for outlier values.

IEEE 754 Floating-Point

General form: (1)s×2Ebias×(1+M/2m)(-1)^s \times 2^{E - \text{bias}} \times (1 + M/2^m)

ComponentRole
Sign (ss)1 bit: positive/negative
Exponent (EE)ee bits: scale (powers of 2)
Mantissa (MM)mm bits: precision within scale

Key formulas:

  • Dynamic range: [2(2e12),  22e11][2^{-(2^{e-1}-2)}, \; 2^{2^{e-1}-1}]
  • Precision: 2m\sim 2^{-m} relative error (ULP at scale 1)
  • Total values: 21+e+m2^{1+e+m}

FP32: Full Precision

Layout: 1 sign + 8 exponent + 23 mantissa = 32 bits.

PropertyValue
Range±3.4×1038\pm 3.4 \times 10^{38}
Smallest normal1.2×10381.2 \times 10^{-38}
Precision7\sim 7 decimal digits
Memory per param4 bytes

Role: The "ground truth" reference. Used for master weights in mixed-precision training, loss computation, and optimizer states.


FP16 vs BF16: The Training Tradeoff

FP16 (IEEE half): 1 + 5 + 10 = 16 bits.

PropertyValue
Range±65504\pm 65504
Precision3.3\sim 3.3 decimal digits
Memory2 bytes

BF16 (Brain Float): 1 + 8 + 7 = 16 bits.

PropertyValue
Range±3.4×1038\pm 3.4 \times 10^{38} (same as FP32!)
Precision2.4\sim 2.4 decimal digits
Memory2 bytes

Why BF16 won for training:

FP16 range=65504BF16 range=3.4×1038.(1)\text{FP16 range} = 65504 \ll \text{BF16 range} = 3.4 \times 10^{38}. \tag{1}

Gradients and activations can exceed 65504 (causing FP16 overflow). BF16 matches FP32's range, avoiding overflow without loss scaling tricks.

FP16 advantage: More precision (10 vs 7 mantissa bits). Better for inference where values are bounded.


FP8: E4M3 vs E5M2

Two FP8 variants (NVIDIA Hopper/Ada):

E4M3 (1 + 4 + 3): More precision, less range.

  • Range: ±448\pm 448
  • Precision: 3 mantissa bits (8 values per exponent)
  • Best for: Forward pass (weights and activations)

E5M2 (1 + 5 + 2): More range, less precision.

  • Range: ±57344\pm 57344
  • Precision: 2 mantissa bits (4 values per exponent)
  • Best for: Backward pass (gradients can spike)

FP8 recipe (NVIDIA):

  • Forward: weights in E4M3, activations in E4M3.
  • Backward: gradients in E5M2.
  • Master weights: FP32 (as always).

Speedup: 2x over FP16 on H100 tensor cores.


Integer Formats: INT8 and INT4

INT8 (signed): Range [128,127][-128, 127]. Uniform spacing: every integer from -128 to 127.

INT8 (unsigned): Range [0,255][0, 255]. Used for activations after ReLU.

INT4 (signed): Range [8,7][-8, 7]. Only 16 distinct values.

Key difference from floating-point: Integers have UNIFORM spacing. Floats have LOG-spaced values (denser near zero). Neural network weights are roughly Gaussian → many values near zero → integers waste precision on the sparse tails.

Mitigation: Non-uniform quantization (NF4) places levels at Gaussian quantiles.


The Dynamic Range vs Precision Tradeoff

For bb total bits, every additional exponent bit doubles the range but halves the precision:

Range22e,Precision2(be1).(2)\text{Range} \propto 2^{2^e}, \quad \text{Precision} \propto 2^{-(b-e-1)}. \tag{2}

Implications:

  • Training (needs range): prefer more exponent bits (BF16, E5M2).
  • Inference (needs precision): prefer more mantissa bits (FP16, E4M3).
  • Weights (static, known range): INT8/INT4 with good scaling.
  • Activations (dynamic range): floating-point or careful calibration.

Choosing the Right Format

StageWeightsActivationsGradients
TrainingBF16/FP32BF16BF16/FP32
FP8 TrainingE4M3E4M3E5M2
Inference (quality)INT8INT8N/A
Inference (speed)INT4FP16N/A
Edge deploymentINT4/INT2INT8N/A

Common Pitfalls

Pitfall 1. Using FP16 for training without loss scaling. Values above 65504 overflow to infinity. Always use dynamic loss scaling or switch to BF16.

Pitfall 2. Assuming INT4 and FP4 are equivalent. INT4 has 16 uniformly-spaced values; FP4 (E2M1) has non-uniformly spaced values with more resolution near zero.

Pitfall 3. Comparing formats only by bit-width. BF16 and FP16 are both 16-bit but have very different characteristics. The bit allocation (exponent vs mantissa) matters as much as total bits.


Summary

  • FP32: Ground truth; 4 bytes; used for master weights and optimizer states.
  • BF16: Same range as FP32 with 2 bytes; dominant for training.
  • FP16: Higher precision than BF16 but limited range; good for inference.
  • FP8 E4M3/E5M2: 1 byte; 2x speedup on H100; precision vs range variants.
  • INT8: 1 byte; uniform spacing; standard for deployment quantization.
  • INT4: 0.5 bytes; 16 values; needs careful calibration or non-uniform formats.
  • Choose format based on: training vs inference, range needs, precision needs.

Exercises

Exercise 1. Decode the FP16 bit pattern 0 10101 0110000000 to its decimal value.

Exercise 2. Compute the largest value representable in E4M3 (FP8) and compare to FP16.

Exercise 3. For a Gaussian-distributed weight with σ=0.02\sigma=0.02: compute the quantization error (MSE) for INT8 symmetric vs NF4 with optimal range.

Exercise 4. Explain why BF16 training doesn't need loss scaling but FP16 does (give a concrete gradient magnitude example).

Exercise 5. Design a custom 6-bit format for neural network weights. How many exponent and mantissa bits would you allocate? Justify.