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:
- Embarrassingly parallel — every output tile independent → fills thousands of ALUs, endless ready warps for latency hiding.
- Uniform control flow — no data-dependent branching → zero warp divergence.
- Maps onto Tensor Cores — GEMM is tiled MMA.
- 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
- 01-gpu-architecture-and-simt — §9 the GEMM/LLM connection.
- 03-performance-modeling-roofline — §3 counting GEMM FLOPs/bytes, §9 GEMM vs attention.
See also
- tensor-core — the engine GEMM runs on
- arithmetic-intensity — , the compute-bound lever
- memory-coalescing — coalesced tile loads
- shared-memory-bank-conflicts — conflict-free tiles
- roofline-model — classifying a GEMM
- gpu-systems-for-llms