Skip to content
DARC
■ Change #90 · wiki.edited

Tiled GEMM on tensor cores (CUTLASS and CuTe): Normalize Markdown to canonical form

by DARC wiki2026-10-22 20:31:08 UTCOpen the page →
body
## The problem
A general matrix multiply (GEMM) computes C = A·B. Done naively, every output element re-reads a row of A and a column of B from main memory, so the kernel is memory-bound even though the arithmetic intensity of the whole product is high. High-performance GEMM, formalised in the CPU setting by Goto and van de Geijn (2008), reorganises the loops into blocks so each loaded value is reused many times from a faster level.
## Hierarchical tiling on a GPU
The output is partitioned into **thread-block tiles** (for example 128×256). Each thread block marches along the K dimension, loading a tile of A and a tile of B into shared memory, then accumulating into registers. Inside the block, **warp tiles** divide the work, and each warp issues **tensor-core MMA** instructions on small fixed shapes. Because the accumulators live in registers across the whole K loop, each element of C is written once.
## Hopper-era mechanics
On Hopper, three features change the structure of a fast kernel:
- **TMA** copies whole tiles from global to shared memory asynchronously, freeing threads from address arithmetic.
- **WGMMA**, a warpgroup-level asynchronous MMA, reads operands directly from shared memory.
- **Warp specialisation**: some warps are producers (issue TMA loads) while others are consumers (issue WGMMA), connected by a software pipeline of shared-memory stages and barriers.
Persistent kernels keep one block per SM and loop over tiles to hide launch and prologue cost. Tile scheduling matters too: when the number of tiles is not a multiple of the SM count, the last wave leaves SMs idle. Stream-K (Osama et al.) splits the K loop across blocks to balance this.
## CUTLASS and CuTe
− CUTLASS is NVIDIA's open-source C++ template library of these building blocks, and the reference for what vendor kernels do. CUTLASS 3.x is built on **CuTe**, a layout algebra in which a tensor is a pointer plus a *layout* (a nested shape and stride function). Tiling, partitioning across threads and copying between memory levels become compositions of layouts, which lets one kernel source target different tile shapes and data types. Python front ends and the CuTe DSL expose the same ideas without C++ templates.
+ CUTLASS is NVIDIA's open-source C++ template library of these building blocks, and the reference for what vendor kernels do. CUTLASS 3.x is built on **CuTe**, a layout algebra in which a tensor is a pointer plus a _layout_ (a nested shape and stride function). Tiling, partitioning across threads and copying between memory levels become compositions of layouts, which lets one kernel source target different tile shapes and data types. Python front ends and the CuTe DSL expose the same ideas without C++ templates.
## Why it is load-bearing
Peak-utilisation numbers for attention, FP8 and block-scaled GEMMs are all reported relative to what a well-tuned GEMM reaches. Open implementations such as DeepGEMM show that a compact, JIT-compiled FP8 GEMM can be competitive with vendor libraries on selected shapes, so reproducing and extending this structure is an active, benchmarkable engineering frontier.
explainer.student
**Why it matters.** GEMM (general matrix multiply) computes $C = AB$. A neural network is a function built from layers, and most layers multiply their input by a learned weight matrix, so training and running large models spends most of its arithmetic in GEMMs. How fast a GPU multiplies matrices sets the speed of the whole field.
**The naive version.** From first-year linear algebra,
− $$C_{ij} = \sum_{k=1}^{K} A_{ik} B_{kj},$$
+ $C_{ij} = \sum_{k=1}^{K} A_{ik} B_{kj},$
each entry of $C$ is the dot product of row $i$ of $A$ and column $j$ of $B$. If each entry is computed separately by fetching its row and column from main memory, every number in $A$ is fetched once for every column of $C$. For 4096 × 4096 matrices that is 4096 reads of each number, and the kernel runs at memory speed (see the roofline entry), far below the chip's peak.
**Tiling.** Split $C$ into tiles, for example 128 × 128 blocks. One group of threads owns one tile. It walks along $k$: it loads a strip of $A$ and a strip of $B$ into on-chip shared memory, multiplies them, adds the result into running sums kept in registers, and moves on to the next strip. Each number loaded is now reused 128 times from fast memory. The same formula holds with blocks in place of numbers, $C_{IJ} = \sum_{K} A_{IK} B_{KJ}$, where capital letters index tiles. Bigger tiles give more reuse but need more on-chip storage, which is scarce. Analogy: photocopying a chapter once and working from the copy, instead of walking to the library for every sentence. It breaks because the desk (on-chip memory) is small, so the chapter has to be copied in pieces.
**Tensor cores.** Inside a tile the arithmetic is done by tensor cores, hardware units that multiply small fixed-size matrix blocks in a single instruction. On NVIDIA's Hopper GPUs (H100), fast kernels also use the Tensor Memory Accelerator to copy tiles in the background, and split the threads into producers that load the next tiles and consumers that multiply the current ones, like an assembly line, so loading overlaps with computing.
**Leftovers.** If the number of tiles is not a multiple of the number of processing blocks on the chip, the last round leaves some blocks idle (wave quantisation). Stream-K splits the $k$ loop across blocks to even out the work.
**Libraries.** NVIDIA's open-source CUTLASS library packages these pieces in C++. Its CuTe layer describes a tensor as a pointer plus a layout (a shape and the steps between elements), so tiling and copying become operations on layouts. DeepGEMM shows that a compact FP8 GEMM can compete with vendor libraries on selected shapes.
**Limits.** Small or oddly shaped matrices, common when a model generates text one token at a time, are memory-bound and hard to tile well. Each new GPU generation changes the instructions, so fast kernels are rewritten for every generation.
hash 4d304bae78bc5731f5420b5bc19a2095081219e9ed3b3b0517db96b8da9023ee
previous 1cde787a5d352d23b1add9fd5fb2f6565363ce800b25f159b31769912424dd9b