On this page
- A kernel is a function that runs on the GPU
- One launch, thousands of threads
- The GPU in numbers
- Memory, not arithmetic, usually decides speed
- Why fusion is the biggest win
- Launch overhead: when the GPU waits for the CPU
- Reading kernel names in a profile
- Where kernels come from
- Do you need to write kernels?
- FAQ
- Go deeper
When a profiler tells you that 60% of a decode step is spent in flash_fwd_splitkv_kernel and sm90_xmma_gemm_..., it's naming kernels. Kernels are where an inference engine's time goes. You don't need to write them to serve models, but understanding them makes profiles readable and makes a lot of performance advice suddenly obvious.
This guide explains what a kernel is, how the GPU runs one, why memory matters more than maths, and the two biggest kernel-level wins in inference: fusion and removing launch overhead.
#A kernel is a function that runs on the GPU
A kernel is a function launched from the CPU that runs on the GPU, as many parallel copies at once. When you call torch.matmul or a layer in vLLM, the framework eventually launches one or more kernels: a matrix multiply, a normalisation, an attention kernel, an activation.
One decode step of a transformer launches a few hundred kernels: for each layer, a norm, the attention projections, rotary embeddings, attention itself, the output projection, residual adds and the MLP.
The CPU's job is to queue these launches in order; the GPU's job is to run them. The two work asynchronously: the CPU can queue the next kernel while the GPU is still running the current one, as long as it keeps ahead.
#One launch, thousands of threads
A kernel launch says how many copies to run, arranged as a grid of blocks of threads. Each thread works out which piece of data is its own from its block and thread index. Here's the simplest real kernel, adding two arrays in CUDA:
__global__ void add(const float* a, const float* b, float* c, int n) {
int i = blockIdx.x * blockDim.x + threadIdx.x; // my element
if (i < n) c[i] = a[i] + b[i];
}
// launch 256 threads per block, enough blocks to cover n elements
add<<<(n + 255) / 256, 256>>>(a, b, c, n);Threads per block
i = blockIdx.x × blockDim.x + threadIdx.x = 2 × 128 + 44
Grid (a few of the blocks)
Block 2: 4 warps
Warp 1: 32 lanes
blockIdx.x
2
threadIdx.x
44
warp in block
1
lane
12
SM (illustrative)
2
One thread per element, 1D launch. Blocks are handed to SMs by the hardware scheduler; the SM shown assumes simple round-robin placement on a 40-SM T4, which real hardware does not promise.
The hardware runs threads in groups of 32 called warps, and places blocks on streaming multiprocessors (SMs). The 32 threads of a warp execute the same instruction at the same time on different data. A big kernel keeps all the SMs busy; a small one leaves most idle.
#The GPU in numbers
Here's an H100 SXM, the workhorse of LLM serving, by the numbers that matter for kernels:
| Part | H100 SXM |
|---|---|
| Streaming multiprocessors | 132 |
| Threads resident per SM, at most | 2,048 (64 warps) |
| Registers per SM | 65,536 (256 KB) |
| Shared memory per SM | up to 228 KB |
| L2 cache | 50 MB |
| HBM capacity and bandwidth | 80 GB at 3.35 TB/s |
| Dense BF16 tensor-core compute | about 989 TFLOP/s |
The memory is a hierarchy: registers are fastest and tiny, shared memory is small and fast and shared within a block, L2 is shared by the whole chip, and HBM is big and comparatively slow. Good kernels load data from HBM once, then do as much work as possible on it in registers and shared memory before writing results back.
#Memory, not arithmetic, usually decides speed
That add kernel does one addition per 12 bytes it moves. A GPU can do hundreds of operations in the time it takes to move 12 bytes, so the kernel's speed is set entirely by memory bandwidth. Most kernels in LLM inference are like this: norms, activations, residual adds, rotary embeddings, sampling, and the decode-time matrix multiplies. Only large matmuls (in prefill or at big batch) and attention in prefill are limited by arithmetic (prefill vs decode).
So the questions that matter for most kernels are:
- How many bytes does it move? Fewer is faster.
- Does it read memory efficiently? Threads in a warp should read neighbouring addresses so the hardware can combine them into a few wide transactions (coalescing). Scattered reads can waste most of the bandwidth.
Stride between neighbouring threads
Threads 0 to 31
The 4 sectors spanned by the warp's addresses (lit: fetched)
sectors fetched
4
bytes fetched
128 B
useful
100.0%
Thread k loads the 4-byte float at index k × stride. Memory is fetched in 32-byte sectors (8 floats). Lit squares are sectors the warp fetches; the bar shows how much of what was fetched is used.
A quick way to judge any memory-bound kernel: divide the bytes it must move by its runtime, and compare with the GPU's bandwidth. A kernel moving 100 MB in 50 µs reaches 2 TB/s, about 60% of an H100's peak: decent. One reaching 300 GB/s has a problem worth finding.
#Why fusion is the biggest win
Each kernel reads its inputs from GPU memory and writes its outputs back. Take a residual add followed by RMSNorm as two kernels: the add writes its result to memory, and the norm reads it back (twice). Fused into one kernel, the intermediate value stays in registers: about a third less memory traffic, and one launch instead of two.
Implementation
GPU memory (HBM)
read a, read b → write c
read c (sum of squares)
read c → write y
Traffic per layer
6N
6.29 MB moved · about 1.9 µs
The intermediate tensor is written to memory and read back twice.
Residual add + RMSNorm on a hidden size of 8192 in BF16. N is one activation tensor. These operations do almost no arithmetic, so their time is the bytes they move at 3.35 TB/s.
This is why inference engines ship fused kernels (add-plus-norm, SiLU-and-multiply, dequantise-and-matmul, and attention itself, which FlashAttention fuses), and why torch.compile speeds models up: it generates fused kernels automatically (FlashAttention explained).
Common fusions in inference engines:
| Fused kernel | What it replaces |
|---|---|
| Add plus RMSNorm | A residual add kernel and a norm kernel |
| SiLU-and-multiply (SwiGLU) | An activation kernel and a multiply kernel |
| Dequantise inside the matmul | Expanding 4-bit weights to 16-bit in memory first |
| Rotary embedding inside attention or QKV | A separate rotary kernel |
| FlashAttention | Separate score, softmax and value kernels |
| Fused MoE | Separate gather, expert matmul and scatter kernels |
#Launch overhead: when the GPU waits for the CPU
Launching a kernel costs CPU time: a few microseconds in C++, more through Python and PyTorch's dispatcher. If a kernel's GPU work takes less time than its launch, the GPU sits idle between kernels.
#A worked example
Take a 1B model (such as Llama 3.2 1B, about 2.5 GB in BF16) decoding at batch 1 on an H100:
- GPU work: reading 2.5 GB at 3.35 TB/s takes about 0.74 ms per token.
- CPU work: 16 layers times roughly a dozen kernels each, plus sampling, is about 200 launches. At about 5 µs each through PyTorch, that's about 1 ms per token.
The CPU can't queue kernels as fast as the GPU finishes them, so the GPU spends more time waiting than working. Speed is set by Python, not the GPU. For a 70B model the GPU work per step is a hundred times larger, so the same overhead disappears into the noise.
The fix is CUDA graphs: record the whole sequence of launches once, then replay it with one call, cutting the CPU cost to a few microseconds per step. Serving engines capture graphs for common batch sizes for exactly this reason, which is part of why they take a while to start (streams, events and CUDA graphs).
Model
Kernels per token
388
GPU time per kernel
3.5 µs
GPU busy (eager)
29%
Graph speedup
3.47×
Launch-bound: the GPU finishes each kernel before the CPU can start the next one. Graphs, fusion or a compiled model recover the idle time.
Model: each kernel takes max(GPU time, launch cost). GPU time per kernel = weight bytes ÷ 3.35 TB/s spread over all kernels. Green is GPU work, hatched is the GPU idle while the CPU prepares the next launch. Launch costs are assumptions: measure yours.
#Reading kernel names in a profile
Profilers show kernel names, which look cryptic but follow patterns:
| Name contains | What it is |
|---|---|
gemm, xmma, cutlass |
A matrix multiply from cuBLAS or CUTLASS (sm90 means built for Hopper) |
flash_fwd, splitkv |
FlashAttention forward pass; split-KV is the decode variant |
paged_attention, flashinfer |
Paged decode attention from an engine or FlashInfer |
triton_poi_, triton_red_, triton_per_ |
Kernels generated by torch.compile (pointwise, reduction, persistent reduction) |
rms_norm, fused_add_rms_norm |
Normalisation, possibly fused with the residual add |
fused_moe |
Mixture-of-experts expert computation |
nccl, allreduce |
Multi-GPU communication |
To see them yourself, profile a few steps with Nsight Systems or the PyTorch profiler:
nsys profile -o decode --trace=cuda,nvtx python run_inference.pyOpen the result and look at the GPU timeline: large gaps between kernels mean launch overhead or CPU work; one kernel dominating means that's where to look (profiling with Nsight).
#Where kernels come from
| Source | Examples |
|---|---|
| Vendor libraries | cuBLAS and cuBLASLt for matmuls, cuDNN |
| Specialist libraries | FlashAttention, FlashInfer, CUTLASS |
| Engine-specific kernels | vLLM's and SGLang's fused MoE, quantised matmuls, norms |
| Compilers | torch.compile (generates Triton kernels), TensorRT-LLM |
| Hand-written | CUDA C++ or Triton, for fusions nobody else provides |
#Do you need to write kernels?
To serve models well: no, but you need to read profiles, and knowing what kernels do is how you read them. To work on engines, add support for a new model or format, or squeeze the last 20% out of a workload: yes, sooner or later. Start with Triton for most custom kernels, and learn enough CUDA to understand what Triton is doing for you (CUDA vs Triton).
#FAQ
Is a kernel the same as a CUDA core? No. A CUDA core is a hardware arithmetic unit; a kernel is a program that runs across many of them.
Do AMD GPUs have kernels too? Yes. The concepts are the same; AMD calls a warp a "wavefront" (64 threads on most data-centre GPUs), and kernels are written in HIP, which looks very like CUDA, or in Triton.
Why does my GPU show 100% utilisation when it's slow?
"Utilisation" in nvidia-smi means a kernel was running, not that the GPU was busy doing useful work. A memory-starved or tiny kernel still counts as 100%.
#Go deeper
The GPU Kernels for Inference course starts here and builds up to tensor-core matmuls, fused norms and a paged decode-attention kernel, all runnable on a free Colab GPU. Its first two lessons are free: what you'll build and the CUDA programming model. For how kernels fit into an engine, see the software stack from CUDA to engines.
kernelscudagpufundamentalsinference-engineering
Found this useful?
Send it to someone who is learning the same thing.
Preparing for an interview?
Next: learn it step by step, free
The CUDA programming model
Threads, blocks, warps and memory: the model every inference kernel is written against. A free lesson with step-through diagrams and a quiz.