Pitfalls — each of these has actually cost real time
Measurement lies
- Timing the host, not the GPU. An eager reference measured 185 ms wall vs 2.06 ms GPU-busy. Use CUDA events around the region, and check the two agree.
- Timing under ncu. It serializes and replays; the number is meaningless.
- Warm cache. Re-running on the same buffer measures L2, not HBM. Use fresh inputs per rep.
- Autotune cache keyed on a stale shape. The first call compiles and tunes; time the steady state.
- Clock throttling. Long benchmarks drift. Lock clocks or take min-of-N, not mean.
- First-call outliers. cuBLAS/SDPA pick an algorithm per shape on first sight; warm up per shape.
Correctness traps
- A fast wrong kernel is the worst outcome. Correctness gate before optimization, always.
- Guessed tolerances. Measure
E(independent correct implementation) andD(feature dropped); the gate goes between them. A gate that rejects a more precise kernel is a real and common bug. - Top-k / argmax gates. Near-ties flip on ordinary noise; two correct implementations disagree.
- Integer outputs compared as floats.
.float()is lossy above 2²⁴, so distinct large ids compare equal and a wrong kernel passes. - The more precise implementation can be further from the reference. An fp64 RoPE differed 3.6e-4 from a reference that a merely-rearranged fp32 version matched to 1.6e-5.
- fp8 rounding is coarse. e4m3 eps is 0.125 — a 1% perturbation is below one ULP, so error is set by which elements flip a code, not by the perturbation size.
tensor / python_floatis a reciprocal-multiply and flips ~0.05% of fp8 codes. Use a 0-dim tensor.
Implementation traps
- Hardcoded SM count or a grid sized from one. Query
multi_processor_count. - Persistent kernel with a grid larger than fits. The spinning blocks deadlock. Check co-residency.
- Assuming a dimension is a multiple of the tile. Ragged shapes are graded deliberately.
- Register spills masquerading as a memory problem. Check
LDL/STLin the SASS. - Padding shared memory to avoid bank conflicts — breaks 128-bit alignment for 2-byte types. Swizzle instead.
- bf16 accumulation. Accumulate in fp32; a bf16 running sum drifts past tolerance on its own.
Reading a profile wrong
Counters are hints, not verdicts. A measured example from this machine: cuBLAS's own bf16 GEMM
(nvjet_tst_256x128_64x4..., 4096³) profiles at SM 90.5%, DRAM 16.5%, occupancy 14.6%, 168
registers/thread, 136k shared-memory bank conflicts — and it is essentially the fastest thing that
runs on the device. Three of those numbers look like defects.
- Low occupancy is not a bug. A GEMM wants big register-resident accumulator tiles; that costs registers, which caps occupancy by construction. Occupancy only matters when you are latency-bound and have nothing else hiding the latency. Check the SM/DRAM throughputs first: if one is already high, occupancy is not your problem.
- High register count is not automatically a spill. 168 regs/thread is a deliberate trade. Confirm
a spill by looking for
LDL/STLin the SASS (tools/dump_ir.sh sass), not by reading the count. - Bank conflicts reported on a highly-tuned kernel are often on a path that is already overlapped. Fix them when a swizzle is missing, not because the counter is nonzero.
The order that works: establish which resource is saturated (SpeedOfLight), then look for a specific mechanism explaining the gap. Never chase a counter that merely looks unhealthy.
Process traps
- Abandoning a good optimization because it currently has a bug. The most expensive habit there is. Record why a variant was dropped: slower, or merely broken.
- Losing the last-known-good. Always keep a runnable fallback.
- Optimizing before profiling. And profiling with ncu before nsys has told you the time is in the kernel at all.
- Stopping because a percentage looks good. The leaderboard is uncapped and the roofline is an attribution formula, not a wall — a kernel that moves fewer bytes than counted can beat it.