ML Systems & Infrastructure

CUDA kernel optimization

A GPU runs thousands of threads in lockstep groups, so getting peak performance is less about clever math than about feeding the machine: keeping every lane busy, every memory transaction full, and the fast on-chip memories warm. Hand-optimizing a CUDA kernel means restructuring the computation until the hardware's many constraints line up, often rewriting the same arithmetic several ways until the profiler stops complaining.

The core levers are occupancy (enough resident warps to hide memory and instruction latency, traded against per-thread register and shared-memory budgets), memory coalescing (adjacent threads touching adjacent addresses so a warp's loads collapse into a few wide transactions), shared-memory tiling with bank-conflict avoidance, and minimizing warp divergence. On recent architectures, peak matmul throughput also demands staging operands through tensor cores with the right tile shapes and asynchronous copy pipelines. The work is profiler-driven: identify whether you are latency-, bandwidth-, or issue-bound, then attack that specific bottleneck.

Higher occupancy is not always the goal — a register-heavy kernel at low occupancy can beat a high-occupancy one if it keeps more data on chip and issues more independent work per thread. The discipline is empirical, and because micro-architectural details shift every hardware generation, fast kernels are retuned rather than ported blindly.

Occupancy is a means, not an end: the metric to maximize is delivered FLOP/s or bytes/s, and sometimes that means deliberately running fewer warps.

Also called
CUDA 核心調優kernel tuning