Matrix Multiplication (GEMM)
Implement hierarchical data reuse and matrix pipelines
Derive blocked matrix multiplication, multi-level tiling, software pipelines, epilogues, and the constraints of matrix engines.
Open a profiler around one layer of the 1-billion-parameter training example, which uses four 2048-token sequences on each of eight GPUs, and several rectangular blocks usually dominate the GPU timeline. The framework may label them aten::mm, aten::linear, cublasLtMatmul, or a fused projection. Each block multiplies one two-dimensional array of numbers by another. During prompt processing, one dimension can contain thousands of token positions. During single-token generation, the same dimension can be only one or a few dozen. The mathematical operation is matrix multiplication in both cases, but the GPU executes the two shapes very differently.
Engineers usually call the operation general matrix multiplication, abbreviated GEMM. A typical GEMM computes C = A × B, sometimes followed by scaling, a bias addition, an activation, or a datatype conversion. The chapter begins with the scalar dot product that produces C[row, column]. The chapter then follows the same input values from GPU high-bandwidth memory into on-chip shared memory and registers, where many output calculations can reuse them.
Nsight Compute makes the progression observable. A first kernel that loads the same row and column repeatedly reports large device-memory traffic and low arithmetic throughput. A tiled kernel reports fewer high-bandwidth-memory transactions per floating-point operation. A pipelined tensor-core kernel can overlap transfers with matrix instructions, but the profile can regress when its shared-memory or register demand leaves too few thread blocks resident. Each optimization therefore has a numeric signature and a resource cost.
The terminology follows the profiler. A central processing unit (CPU) launches each matrix multiplication (GEMM) on a graphics processing unit (GPU). Matrix data usually begins in high-bandwidth memory (HBM). Thread blocks run on streaming multiprocessors (SMs), where the single instruction, multiple threads (SIMT) execution model coordinates lanes. Throughput reports count floating-point operations (FLOPs), while the byte model counts transfers among memory levels.
The chapter derives thread-block tiles, warp tiles, instruction tiles, software pipeline stages, and output epilogues from one concrete matrix shape. The final lesson returns to the profiler timeline and explains why a library selects different kernels for a 4096 × 4096 training multiplication, a group of small mixture-of-experts multiplications, and a decode projection with only a few input rows.
Prerequisites
- Chapter 6: tiling, coalescing, shared memory, and registers
- Chapter 8: hierarchical reduction
- Matrix multiplication indices and dot products
Learning objectives
- Derive the traffic reduction from a shared-memory GEMM tile
- Trace ownership from a thread-block tile to warp and instruction tiles
- Select pipeline depth and library kernels from resource and shape constraints
Study this chapter
- 9.1Tiled GEMM and data reuseBegin lesson →
Calculate reuse and arithmetic intensity for one block tile and identify its capacity limits
- 9.2Software pipelines and asynchronous copiesBegin lesson →
Construct a staged producer-consumer pipeline and enforce readiness and reuse dependencies
- 9.3Matrix instructions, fragments, and layoutsBegin lesson →
Trace operands and accumulators through a cooperative matrix-instruction contract
- 9.4Library selection and shape familiesBegin lesson →
Build and validate a GEMM dispatch policy for a weighted shape distribution
Extended exercises
Complete these projects after the lesson-level practice tasks. Each project combines several mechanisms from the chapter.
Tiled GEMM
Progress from naive to shared-memory blocked FP32 GEMM.
Evidence: Roofline position, correctness, and profiler comparison at several shapes.
Pipeline experiment
Compare one-stage and multi-stage tiled designs or library configurations.
Evidence: Resource usage and a latency-hiding explanation.
CUTLASS shape study
Profile candidate kernels across a transformer-derived shape distribution.
Evidence: Weighted dispatch recommendation, not a single best number.
Sources and further reading
The lesson explanations synthesize primary papers and official references. Follow the originals for proofs, exact APIs, architecture details, and version-sensitive behavior.