Tensor cores: WMMA, MMA and why fast GEMMs need big tiles
What a tensor core does in one instruction, how to drive it from CUDA with the WMMA API, why a naive tensor-core kernel is still memory-bound (the balance point jumps by about 8 times), and how each GPU generation changed the interface, from mma.sync to Hopper's warpgroup wgmma and Blackwell's tensor memory. Ends with when to reach for CUTLASS, Triton or a library instead.
By Sandip Maity, Applied AI Engineer
Your register-blocked FP32 matmul tops out near the T4's 8.1 TFLOP/s FP32 peak. The same chip can do about 65 TFLOP/s in FP16, but only on tensor cores, units built to do one thing: multiply small matrices. Every serious inference kernel (GEMMs, attention, MoE) runs its heavy maths on them. This lesson shows how to use them directly, and why using them well is so much harder than using them at all.
#What a tensor core does
An ordinary CUDA core does one fused multiply-add on one pair of numbers per clock. A tensor core does a whole small matrix multiply-accumulate, , on tiles like 16 by 8 by 16, every few clocks. From CUDA, you never address one tensor core directly. A warp issues a matrix instruction, and the hardware spreads the tile across the registers of its 32 threads.
| T4 (Turing) | A100 (Ampere) | H100 SXM (Hopper) | |
|---|---|---|---|
| FP32 on CUDA cores | about 8.1 TFLOP/s | about 19.5 TFLOP/s | about 67 TFLOP/s |
| FP16 or BF16 on tensor cores (dense) | about 65 (FP16 only) | about 312 | about 989 |
| Memory bandwidth | 320 GB/s | about 2,000 GB/s | 3,350 GB/s |
| Tensor-core balance point | about 200 FLOPs/byte | about 156 | about 295 |
Figures are from vendor spec sheets for the common variants of each GPU. The last row is the important one. Tensor cores multiply the compute ceiling without adding bandwidth, so the arithmetic intensity a kernel needs to be compute-bound jumps. On a T4 it goes from about 25 FLOPs per byte for FP32 to about 200 for FP16 tensor cores.
#WMMA: the portable API
The WMMA (warp matrix multiply-accumulate) API, in <mma.h>, has worked since Volta. You declare fragments (tiles held in registers across the warp), load them, multiply, and store:
#include <mma.h>
#include <cuda_fp16.h>
using namespace nvcuda;
// A: M x K, B: K x N, both row-major FP16. C: M x N FP32. M, N and K must be multiples of 16.
// Each warp computes one 16 x 16 tile of C.
__global__ void wmma_gemm(const half* A, const half* B, float* C, int M, int N, int K) {
int warp = (blockIdx.x * blockDim.x + threadIdx.x) / 32;
int tiles_n = N / 16;
int tm = warp / tiles_n, tn = warp % tiles_n;
if (tm * 16 >= M) return; // the whole warp leaves together
wmma::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> acc;
wmma::fill_fragment(acc, 0.f);
for (int k = 0; k < K; k += 16) {
wmma::load_matrix_sync(a, A + (size_t)tm * 16 * K + k, K); // last arg: leading dimension
wmma::load_matrix_sync(b, B + (size_t)k * N + tn * 16, N);
wmma::mma_sync(acc, a, b, acc);
}
wmma::store_matrix_sync(C + (size_t)tm * 16 * N + tn * 16, acc, N, wmma::mem_row_major);
}
// launch: int warps = (M / 16) * (N / 16); wmma_gemm<<<(warps * 32 + 127) / 128, 128>>>(A, B, C, M, N, K);!nvcc -O3 -arch=sm_75 -o wmma wmma.cu && ./wmmaThe rules:
- Every lane of the warp must call every WMMA function, with the same arguments. That's why the early
returnis safe here:warpis the same for all 32 threads. - The fragment layout is opaque. You can't portably say which thread holds which element. You can loop over
acc.x[i]for elementwise work that's the same for every element (scaling, say), but not for work that depends on the position. - Shapes are fixed. For FP16, the common shape is 16 by 16 by 16; a few others exist (32 by 8 by 16, 8 by 32 by 16).
- Inputs are FP16 here, accumulators FP32. That's the standard mixed-precision recipe. On a T4 the tensor cores support FP16, INT8 and INT4 inputs; BF16 and TF32 arrived with Ampere, and FP8 with Hopper.
Check it against PyTorch:
import torch, triton
a = torch.randn(4096, 4096, device="cuda", dtype=torch.half)
b = torch.randn(4096, 4096, device="cuda", dtype=torch.half)
ms = triton.testing.do_bench(lambda: a @ b)
print(f"cuBLAS FP16: {2 * 4096**3 / (ms / 1e3) / 1e12:.1f} TFLOP/s")Pro content
Keep reading with Inference Academy Pro
Unlock the rest of this lesson, including 1 animated diagram, plus every advanced lesson, hands-on lab, the larger playground models and completion certificates.
Still to come in this lesson
- Why the simple kernel is slow
- The generations, briefly
- So who writes these kernels?
- What you can now do
- Common mistakes
- Exercise
- Going deeper
You will be able to answer
- On a T4, the FP16 tensor-core peak is about 65 TFLOP/s and bandwidth is 320 GB/s. What is the balance point?
- Which threads take part in one wmma::mma_sync call?
- The simple WMMA kernel loads A and B tiles straight from global memory. Why is it far below the tensor-core peak?
- What changed with Hopper's wgmma instruction?
- When should you write your own tensor-core GEMM rather than use cuBLAS, CUTLASS or Triton?