Knowledge note

General Matrix Multiply (GEMM)

The dense multiply-accumulate kernel C = A×B whose execution rate is the standard performance reference for linear-algebra workloads on GPUs and NPUs.

Updated September 16, 2026Confidence: High

General Matrix Multiply (GEMM)

General Matrix Multiply (GEMM) is the dense multiply-accumulate kernel that computes $C = A \times B$ for matrices of arbitrary sizes, and whose throughput is the standard performance reference for dense linear algebra.

Why It Exists

Neural networks are, at their core, a long chain of matrix multiplications: attention projections, MLP weights, and even convolution (via im2col or implicit-GEMM). If the GEMM kernel is slow, the whole model is slow. Optimized GEMM kernels reach 80–90% of peak GPU throughput — the number FlashAttention-3 compares attention against — so GEMM is the roofline target that every other kernel is measured by.

How It Works

Each output element is a length-$K$ inner product:

\[C[m][n] = \sum_{k=0}^{K-1} A[m][k] \cdot B[k][n]\]

The full product is $M \times N \times K$ multiply-accumulate operations. Because that work dwarfs the data size, the practical problem is data movement, not FLOPs: operands must be fetched from global memory into on-chip storage and reused many times. GEMM implementations therefore break the computation into blocks and pick an accumulation strategy. The Spatial tutorial enumerates the main options: matrix tiling to block the loops, inner-product or outer-product accumulation, and systolic array dataflow. On hardware, tensor cores (NVIDIA), the Ascend Cube unit, and TPU systolic arrays execute GEMM as a single fused instruction with an FP32-accumulate epilogue.

Tradeoffs

  • GEMM is compute-bound only when tiles are large enough to amortize memory traffic; small matrices are memory-bound and need kernel fusion or batched dispatch.
  • Tiling must match the on-chip capacity (memory banking and SRAM size); too-large tiles spill, too-small tiles underutilize the compute units.
  • Low-precision GEMMs (FP8, FP4, INT4) multiply memory savings against accuracy; the NVFP4 format exists precisely to keep GEMM layout constraints tractable.

Common Confusions

  • GEMM vs. matmul: The same operation; GEMM emphasizes the general (non-square, non-identity) case with an optional bias/epilogue, as in cuBLAS GEMM.
  • GEMM vs. dot product: A GEMM is a matrix of inner products; a single dot product is one output element.
  • GEMM vs. convolution: Dense GEMM is a distinct kernel; convolution is often lowered to GEMM (implicit-GEMM) rather than being a GEMM itself.

Where It Appears

  • Matrix Tiling — The blocking strategy that makes GEMM compute-bound.
  • Inner Product — Element-wise accumulation view of GEMM.
  • Outer Product — Rank-1 accumulation view of GEMM.
  • Systolic Array — A spatial dataflow that executes GEMM with maximal weight reuse.
  • Memory Banking — Conflict-free parallel access to the on-chip SRAM holding GEMM tiles.