L2 Intermediate · Module 6: GEMM and memory optimization
6.7Tensor cores and mixed precision
Tensor cores multiply small matrix tiles in a single warp instruction, several times faster than the ordinary FP32 cores, as long as the inputs are in a narrower format. This chapter explains the formats (TF32, FP16, BF16), measures speed and error for each, and writes a WMMA kernel that reaches 75% of cuBLAS.
By the end you will be able to
- ✓Explain what a tensor core computes, and why it needs narrower input formats
- ✓Compare FP32, TF32, FP16 and BF16 in range and precision, and predict their errors
- ✓Explain mixed precision, with low-precision inputs and an FP32 accumulator, and why LLM inference uses it
- ✓Write a WMMA kernel, and explain why the naive version is slower than FP32
Everything so far in this module ran on the CUDA cores, which do one scalar FMA per lane per instruction. Since Volta (2017), each SM also has tensor cores: units that multiply whole matrix tiles. On Ampere, one warp-wide instruction computes for a 16 × 8 × 16 tile of FP16, which is 2,048 multiply-adds per instruction.
That is where nearly all of an LLM’s FLOPs actually run.
Narrower numbers
Tensor cores get their speed partly from narrow inputs: smaller multipliers, and more values per byte moved. The formats:
| Format | Bits | Exponent | Mantissa | Largest value | Relative precision |
|---|---|---|---|---|---|
| FP32 | 32 | 8 | 23 | 3.4 × 10³⁸ | 6 × 10⁻⁸ |
| TF32 | 19 (stored as 32) | 8 | 10 | 3.4 × 10³⁸ | 5 × 10⁻⁴ |
| FP16 | 16 | 5 | 10 | 65,504 | 5 × 10⁻⁴ |
| BF16 | 16 | 8 | 7 | 3.4 × 10³⁸ | 4 × 10⁻³ |
- TF32 keeps FP32’s range and storage but rounds the mantissa to 10 bits inside the tensor core. FP32 code gets tensor-core speed with no changes to the data.
- FP16 has the same precision but a tiny range: anything above 65,504 overflows. Training in FP16 needs loss scaling for that reason.
- BF16 keeps FP32’s range with less precision. It almost never overflows, which is why it became the default format for LLM weights.
12.1 goes further, down to FP8 and INT4.
Mixed precision
Products of FP16 or BF16 inputs are accumulated in FP32. A dot product of length 4,096 sums 4,096 terms. In FP16 the running sum would lose low bits at every step and could overflow, while FP32 keeps it accurate. “Mixed precision” means narrow inputs and a wide accumulator, and it is the standard for LLM inference: BF16 or FP16 weights and activations, with FP32 accumulation inside every GEMM.
Measured
The same 4096 × 4096 × 4096 matmul, inputs uniform in :
4096 x 4096 x 4096 matmul, inputs uniform in [-1, 1]; largest |C| = 121.2 time TFLOP/s vs FP32 max |error| cuBLAS FP32 30.18 ms 4.55 1.0x cuBLAS TF32 15.76 ms 8.72 1.9x 3.1e-02 cuBLAS FP16->FP32 8.37 ms 16.42 3.6x 3.1e-02 cuBLAS BF16->FP32 8.00 ms 17.18 3.8x 2.4e-01 cuBLAS FP16->FP16 5.30 ms 25.92 5.7x 1.0e+00 WMMA naive 96.70 ms 1.42 0.3x 3.1e-02 WMMA tiled 11.12 ms 12.36 2.7x 3.1e-02Speed. TF32 doubles FP32 with no change to the data. FP16 or BF16 with an FP32 accumulator gives 3.6–3.8×. FP16 with an FP16 accumulator is faster still, because on GeForce GPUs like this one, FP32 accumulation runs at half the tensor-core rate. (Data-center GPUs such as A100 and H100 run both at full rate.)
Error. Every error here comes from rounding, measured against the FP32 result:
- TF32 and FP16 inputs both have a 10-bit mantissa, so both give the same error, 0.03 on outputs up to 121. That’s a relative error of about .
- BF16 has 3 fewer mantissa bits, so its error is about larger: 0.24.
- An FP16 accumulator pushes the error to 1.0. Near 100, FP16 values are 0.0625 apart, and each of 4,096 additions rounds. This is why inference kernels accumulate in FP32.
These sound large, but the network was trained with the same rounding. LLM inference in BF16 matches FP32 outputs closely in practice. The errors that matter come from going further, to FP8 and INT4, which is the subject of Module 12.
Writing tensor-core code: WMMA
CUDA’s WMMA API exposes tensor cores at warp level. A fragment is a 16 × 16 tile spread across the 32 lanes’ registers, in a layout you don’t need to know:
// labs/m06/6.7/tensor_cores.cu, wmma_naive: one warp computes one 16 x 16 tile of Cwmma::fragment<wmma::matrix_a, 16, 16, 16, half, wmma::row_major> a;wmma::fragment<wmma::matrix_b, 16, 16, 16, half, wmma::row_major> b;wmma::fragment<wmma::accumulator, 16, 16, 16, float> c;wmma::fill_fragment(c, 0.0f);for (int k = 0; k < N; k += 16) { wmma::load_matrix_sync(a, A + tile_row * 16 * N + k, N); // the whole warp loads a 16 x 16 tile wmma::load_matrix_sync(b, B + k * N + tile_col * 16, N); wmma::mma_sync(c, a, b, c); // 4,096 multiply-adds, one call}wmma::store_matrix_sync(C + tile_row * 16 * N + tile_col * 16, c, N, wmma::mem_row_major);This naive version runs at 1.4 TFLOP/s, slower than plain FP32. The tensor core is idle most of the time, waiting on fragments loaded straight from global memory with no reuse. That is the 6.1 lesson again, only harsher: the faster the math, the more the memory hierarchy matters.
The tiled version applies the 6.1 and 6.2 ideas. A block of 4 warps stages 64 × 32 and 32 × 64 tiles in shared memory with 16-byte loads, padded to avoid bank conflicts (6.4), and each warp computes a 32 × 32 output from 2 × 2 fragments, reusing each loaded fragment twice. That alone gives 12.4 TFLOP/s, 75% of cuBLAS FP16 and 2.7× the best possible FP32.
Lab: tensor cores 🔴
cd labs/m06/6.7 && nvcc -O2 -arch=native -DUSE_CUBLAS tensor_cores.cu -o tensor_cores -lcublas && ./tensor_coresTensor cores need sm_70 or newer; on Colab, the T4 (sm_75) supports FP16 but not BF16 or TF32.
Sources
Check your understanding
0 / 4 answered1.Why is BF16 preferred over FP16 for LLM weights and activations?
2.What does "mixed precision" mean in a GEMM?
3.Why was BF16's error about 8× FP16's in the lab?
4.The naive WMMA kernel was slower than FP32 cuBLAS. Why?