Chapter 66
CUDA Engineering Checklist
On this page
Use this checklist as an acceptance record alongside the concrete experiments: decoder cache/visibility tests for semantics, page-pool tests for ownership, and both the MPS RMSNorm comparison and compiled Metal RMSNorm investigation for executed operator evidence. CUDA race checking, custom-kernel compilation, SM100 timelines, and multi-GPU execution still require the actual target. A check marked unmeasured is not a pass.
Before writing the kernel¶
- State input and output shapes, dtype, layout, alignment, and allowed error.
- Write the serial reference and complexity.
- Identify parallel output units and reduction dimensions.
- Estimate bytes and FLOPs.
- Choose block and warp ownership.
- Decide which values live in registers, shared memory, or HBM.
- Plan edge handling and synchronization.
Correctness checks¶
- Every global load and store is bounds-safe.
- Every block barrier is reached uniformly.
- Warp masks match active lanes.
- Shared-memory producers synchronize before consumers.
- Reduction identities initialize inactive lanes.
- Atomics use the correct type and scope.
- Random state is indexed without overlap.
- Numerical accumulation uses adequate precision.
- Empty, singleton, odd, and maximum shapes are tested.
- Results and gradients match a reference within declared tolerance.
Performance checks¶
- Global accesses are coalesced or the scatter is unavoidable.
- Shared accesses avoid serious bank conflicts.
- Tiles provide reuse before eviction.
- Register count does not cause damaging spill.
- Shared memory does not eliminate needed residency.
- Launch count and CPU submission are measured.
- Vector width matches alignment and tail policy.
- Tensor-core instructions are actually selected.
- Fusion saves more traffic than it costs in occupancy or flexibility.
- Benchmark covers the production shape distribution.
Matmul drill¶
Start with one output per thread. Add shared-memory tiling. Add register tiling. Change the inner loop to tensor-core fragments. Pipeline global-to-shared copies. Discuss split-K, persistent scheduling, epilogue fusion, and why a library may still win.
Reduction drill¶
Reduce within a warp using shuffles. Store one value per warp. Synchronize. Let the first warp reduce partials. State which lane owns the output. Extend to max, argmax, Welford state, and multi-block reduction.
Softmax drill¶
Find row max, sum shifted exponentials, normalize. Explain stable math, online combination, wide rows, recompute versus store, and fusion into attention.
LayerNorm drill¶
Compute mean and variance or Welford state, normalize, apply scale and bias. Explain accumulation dtype, row width, vectorized IO, residual fusion, and backward reductions.
Attention drill¶
Contrast naive score materialization with tiled online softmax. Derive rescaling. Cover causal masking, ragged lengths, GQA, backward recomputation, and tile selection.
Common CUDA implementation bugs¶
| Bug | Symptom | Repair |
|---|---|---|
| Early return before barrier | Deadlock or partial output | Predicate loads and stores; keep barrier uniform |
| Missing edge predicate | Illegal access at nonmultiple shapes | Zero-fill input tile and guard output |
| Lane uses undefined shuffle value | Shape-dependent corruption | Correct active mask and identity values |
| Excess register tile | Spill and sudden slowdown | Inspect register count; retune tile |
| Column access in square shared tile | Bank-conflict serialization | Pad or remap layout |
| Timing without synchronization | Impossible speed | Use events or synchronized benchmark boundary |