Summary
A hands-on continuation of the GPU lecture, moving from hardware concepts to actual kernel code. It covers the places where the clean programming model leaks and hardware details decide performance, then argues for measuring before optimizing, then writes Triton kernels of increasing difficulty ending at a tiled matmul. The through-line is that the programming model is simple enough to learn quickly, but performance is governed by warps, banks, cache lines, and occupancy, none of which appear in the model itself.
Key Takeaways
- The programming model is threads, thread blocks (CTAs), and a grid of blocks; the reason blocks exist at all is shared memory, which is what lets non-elementwise operations like softmax and matmul avoid round-tripping through HBM.
- Warps are 32 threads executing in lockstep, so branching serializes both sides. Less obviously, a warp scheduler switches between resident warps at zero cost, which is how the hardware hides the long latency of an HBM read — a stalled warp is simply replaced by one that can compute.
- Occupancy is a budget calculation, not a goal. A thread may use at most 255 registers, and registers per SM are fixed, so 128 threads per block at 160 registers each allows only 3 blocks per SM, which is 12 of a possible 64 warps — 18% occupancy. Low occupancy is not automatically bad if each thread does more work, which is the idea behind thread coarsening.
- Shared memory is split into 32 banks of 4 bytes, and only one thread may hit a bank per cycle, so 32 threads reading down a column serialize into a 32-way bank conflict. This is unavoidable in matmul, where one operand must be read by rows and the other by columns; swizzling the shared-memory layout is the standard fix. Bank conflicts (shared memory) and coalescing (128-byte HBM cache lines) are separate constraints that look superficially similar.
- Blocks are scheduled onto a finite number of SMs, so launching 160 blocks on 148 SMs leaves a second wave of 12 blocks with most of the chip idle; the fix is changing block size rather than hoping the scheduler compensates.
- The recommended loop is benchmark, change, benchmark again. Benchmarking correctly means warming up first (to exclude lazy compilation), timing with CUDA events, synchronizing because GPU execution is asynchronous, and averaging over repeats. Matmul wall time is roughly flat until around 2000 dimensions, because smaller matrices cannot fill the hardware.
- Profiling shows what actually ran. A hand-written PyTorch GeLU dispatches a separate kernel per graph primitive, each reading and writing HBM; the built-in GeLU is one hand-written CUDA kernel; torch.compile fuses the naive version into a single generated Triton kernel. Kernel names are informative — a CUTLASS name encodes the architecture (SM100 is Blackwell) and the tile shape, and changing matrix dimensions selects a different kernel.
- Triton is written in Python at thread-block granularity: compute a program ID, derive offsets with tl.arange, build a mask for the ragged final block, tl.load from pointers, compute as if on tensors, and tl.store back. It compiles to PTX, which is per-thread and reveals decisions the programmer never made — including thread coarsening, where one thread was given eight elements because it was too light.
- The examples escalate in exactly the way the hardware forces: GeLU is elementwise and needs no coordination; softmax assigns one row per block so no cross-block communication is needed; row-sum introduces tiling with a per-thread accumulator once a row exceeds the block size; and matmul is the canonical case, where the naive version does O(MKN) reads for constant arithmetic intensity, loading everything into shared memory would give O(N) but does not fit, and tiling reaches arithmetic intensity on the order of the tile size. Fusing an elementwise activation before the final store is free once the kernel is already written.
Extracted Notes
- None yet.