GEMM (General Matrix Multiply)

Definition

GEMM — dense general matrix multiply, — is the canonical GPU workload and the compute core of transformers. QKV projections, the attention output projection, and the MLP up/down projections are all GEMMs, and across a model they are the large majority of the FLOPs. GEMM is the ideal fit for GPU hardware: embarrassingly parallel, uniform control flow, high arithmetic intensity when tiled, and a direct map onto Tensor Cores.

Key math

For , :

(each output is a length- dot product = FLOPs; = bytes/element). For a square GEMM ():

Intensity grows linearly with → big GEMMs are compute-bound, small ones (decode-time GEMV, ) are memory-bound at . On H100 BF16 (), an GEMM has → firmly compute-bound.

Why GEMM is the perfect GPU workload:

  1. Embarrassingly parallel — every output tile independent → fills thousands of ALUs, endless ready warps for latency hiding.
  2. Uniform control flow — no data-dependent branching → zero warp divergence.
  3. Maps onto Tensor Cores — GEMM is tiled MMA.
  4. High intensity when tiled compute over data → becomes compute-bound.

High-performance implementation (tiling): load tiles of from HBM into shared memory with coalesced reads, reuse each element across an output tile (raising from to ), arrange SMEM conflict-free. This recipe takes a matmul from a few % of peak to 80–90%+.

Why it matters

A transformer forward/backward pass is mechanically a long chain of GEMMs with memory-bound elementwise glue between them. Every LLM FLOP estimate (MFU, the 6ND rule) and every performance ceiling is grounded in GEMM. Making GEMM run near peak — in the lowest acceptable precision — is the single highest-leverage lever in training and prefill.

Taught in

See also