IIntroduction to AI Systems Performance EngineeringTechnical textbook
Chapter 9

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.

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
Lessons

Study this chapter

  1. 9.1
    Tiled GEMM and data reuse

    Calculate reuse and arithmetic intensity for one block tile and identify its capacity limits

    Begin lesson →
  2. 9.2
    Software pipelines and asynchronous copies

    Construct a staged producer-consumer pipeline and enforce readiness and reuse dependencies

    Begin lesson →
  3. 9.3
    Matrix instructions, fragments, and layouts

    Trace operands and accumulators through a cooperative matrix-instruction contract

    Begin lesson →
  4. 9.4
    Library selection and shape families

    Build and validate a GEMM dispatch policy for a weighted shape distribution

    Begin lesson →
Chapter projects

Extended exercises

Complete these projects after the lesson-level practice tasks. Each project combines several mechanisms from the chapter.

01
Check

Tiled GEMM

Progress from naive to shared-memory blocked FP32 GEMM.

Evidence: Roofline position, correctness, and profiler comparison at several shapes.

02
Practice

Pipeline experiment

Compare one-stage and multi-stage tiled designs or library configurations.

Evidence: Resource usage and a latency-hiding explanation.

03
Challenge

CUTLASS shape study

Profile candidate kernels across a transformer-derived shape distribution.

Evidence: Weighted dispatch recommendation, not a single best number.

References

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.

  1. NVIDIA CUTLASS
  2. CUDA C++ Programming Guide
  3. Nsight Compute Profiling Guide