Education › Inference › Stage 3: Analysing inference performance

Compute-bound, memory-bound, and profiling

Arithmetic intensity and the roofline, then measuring for real with nvidia-smi, DCGM and Nsight.

Intermediate ~35 min read Module 6 of 15

Metrics tell you a service is slow; profiling tells you why. This lesson gives you the one model that explains most GPU performance questions, the roofline, and uses it to show exactly why decode is memory-bound and prefill is compute-bound, and when batching flips decode from one to the other. Then it covers the measuring tools in order of depth: nvidia-smi for a glance, DCGM metrics for fleets, Nsight Systems for timelines, and Nsight Compute for individual kernels.

After this module you can
  • Compute arithmetic intensity and the ridge point of a GPU
  • Use the roofline to classify a workload as compute-bound or memory-bound
  • Predict how batch size changes decode from memory-bound toward compute-bound
  • Choose between nvidia-smi, DCGM, Nsight Systems and Nsight Compute for a question
  • Read GPU timeline gaps that point to CPU, synchronisation or communication stalls

Arithmetic intensity and the roofline

Every piece of GPU work does some floating-point operations (FLOPs) and moves some bytes to and from memory. Its arithmetic intensity is FLOPs per byte moved. A GPU has a peak compute rate (FLOP/s) and a peak memory bandwidth (bytes/s); their ratio is the ridge point. Work with intensity below the ridge cannot use all the compute because memory cannot feed it fast enough: it is memory-bound. Work above the ridge is compute-bound.

The roofline in numbers (an example GPU, not a specific product)
text
peak compute   = 1,000 TFLOP/s  (dense FP16 Tensor Core)
peak bandwidth = 3.3 TB/s
ridge point    = 1,000e12 / 3.3e12 ~ 300 FLOPs per byte

attainable FLOP/s = min(peak compute, intensity x peak bandwidth)

Plot attainable performance against intensity on log axes and you get a slanted line (bandwidth-limited) meeting a flat roof (compute-limited) at the ridge. Every optimisation either raises a kernel's intensity, moving it right toward the roof, or makes it run closer to the line it is already under.

Placing prefill and decode on the roofline

A forward pass costs about 2 FLOPs per parameter per token. In FP16 each parameter is 2 bytes. During decode at batch size B, the weights are read once and used for B tokens, so intensity is roughly 2B FLOPs over 2 bytes, about B FLOPs per byte. At batch 1 that is about 1, far below a ridge of around 300: the GPU uses well under 1% of its compute. At batch 64 it is about 64: much better, still memory-bound. Reading the KV cache adds bytes without many FLOPs, so long contexts push decode further toward the memory side.

Prefill of a 2,000-token prompt uses each weight for 2,000 tokens, an intensity in the thousands, well past the ridge: compute-bound. That is the roofline explanation of everything in the lifecycle lesson, and it predicts what helps: for decode, bigger batches, fewer bytes per weight (quantization) and faster memory; for prefill, more Tensor Core throughput and less redundant work (prefix caching).

Note

Quantizing weights to FP8 halves the bytes per parameter, which both halves memory traffic and doubles intensity. That is why it speeds up memory-bound decode almost proportionally, even before faster FP8 Tensor Core maths is counted.

Measuring: from a glance to a kernel

ToolAnswersCost
nvidia-smiIs the GPU busy at all? Memory used? Throttling?Free, instant
DCGM + dcgm-exporterPer-GPU utilisation, Tensor Core and memory activity over time, across a fleetAlways on, Prometheus
Nsight Systems (nsys)Timeline: where time goes across CPU threads, CUDA kernels, memory copies, NCCLMinutes, per run
Nsight Compute (ncu)Why one kernel is slow: achieved bandwidth and FLOPs versus peakSlow, per kernel

DCGM's profiling metrics are the useful upgrade over nvidia-smi's coarse utilisation: DCGM_FI_PROF_PIPE_TENSOR_ACTIVE shows how busy the Tensor Cores really are, and DCGM_FI_PROF_DRAM_ACTIVE shows how busy the memory interface is. A decode-heavy server typically shows DRAM activity high and Tensor activity low, the memory-bound signature in production. Some fields, such as SM activity, are commented out in the exporter's default counter list and must be enabled.

Profile a short benchmark with Nsight Systems, then one kernel with Nsight Compute
bash
# timeline of CPU threads, CUDA kernels, memory copies and NCCL calls
nsys profile -o decode_run --trace=cuda,nvtx,osrt python bench.py
nsys stats decode_run.nsys-rep

# detailed counters for the first 5 matmul-like kernels
ncu --kernel-name regex:gemm --launch-count 5 --set full -o gemm python bench.py

Reading a timeline

  • Gaps between kernels on the GPU row with CPU activity at the same moment: the CPU is too slow to launch work (scheduling, tokenization, Python overhead). Engines use CUDA graphs to cut launch overhead for exactly this reason.
  • Long memory copies between host and device in the steady state: something is being moved over PCIe each step that should stay on the GPU.
  • NCCL kernels taking a large share of each step in a multi-GPU run: communication is the bottleneck, covered in the parallelism stage.
  • One kernel dominating: profile it with Nsight Compute and compare achieved memory bandwidth with the GPU's peak. Near peak means it is already as fast as the hardware allows at that intensity.
Tip

Profile short, representative runs. A few seconds of steady-state decode at your real batch size tells you more than an hour of traces, and Nsight output for long runs gets unwieldy fast.

Hands-on practice

Find the roofline in your own benchmark

  1. Look up your GPU's dense FP16 Tensor Core TFLOPS and memory bandwidth and compute its ridge point.
  2. Compute the decode arithmetic intensity at batch 1, 8, 32 and 128 using the B FLOPs per byte rule, and mark which side of the ridge each falls on.
  3. Run a decode benchmark at those batch sizes (vLLM's offline LLM class or vllm bench serve at matching concurrency) and record tokens per second.
  4. Plot tokens per second against batch size; note where gains start to flatten.
  5. Run nsys profile on a few seconds of steady-state decode and look for gaps between kernels and the share of time in matmul kernels.
  6. If dcgm-exporter is available, watch DRAM_ACTIVE and PIPE_TENSOR_ACTIVE during the run and relate them to your roofline prediction.
Cheat sheet

Compute-bound, memory-bound, and profiling — at a glance

Main things to focus on

  • Intensity = FLOPs / bytes; ridge = peak FLOP/s / peak bytes/s.
  • Below the ridge: memory-bound. Above: compute-bound.
  • Decode intensity is about the batch size; prefill intensity is about the prompt length.
  • Quantization halves bytes and doubles intensity: big win for decode.
  • nvidia-smi for a glance, DCGM for fleets, nsys for timelines, ncu for kernels.
  • Timeline gaps usually mean the CPU or synchronisation, not the GPU.

Formulas

intensity = FLOPs / bytes_movedArithmetic intensity
ridge = peak_FLOPs / peak_bandwidthWhere memory-bound becomes compute-bound
attainable = min(peak_FLOPs, intensity x bandwidth)The roofline
decode intensity ~ B (FP16)About batch size FLOPs per byte
prefill intensity ~ prompt tokensLarge, so compute-bound

Nsight

nsys profile -o NAME --trace=cuda,nvtx,osrt CMDCapture a timeline
nsys stats NAME.nsys-repSummaries of kernels, copies, API calls
ncu --set full --launch-count N -o NAME CMDDetailed per-kernel counters
ncu --kernel-name regex:PATTERNProfile only matching kernels
nvidia-smi dmon -s pucmQuick rolling power, utilisation, clocks, memory

DCGM metrics

DCGM_FI_PROF_PIPE_TENSOR_ACTIVEShare of cycles Tensor Cores are busy
DCGM_FI_PROF_DRAM_ACTIVEShare of cycles memory interface is busy
DCGM_FI_DEV_FB_USEDVRAM used (MiB)
DCGM_FI_DEV_GPU_UTILCoarse utilisation, like nvidia-smi
DCGM_FI_DEV_NVLINK_BANDWIDTH_TOTALNVLink throughput

Common pitfalls

  • Judging efficiency from nvidia-smi utilisation alone.
  • Expecting more TFLOPS to speed up batch-1 decode.
  • Profiling cold-start or warm-up instead of steady state.
  • Hour-long Nsight captures nobody can open, instead of short representative runs.
  • Ignoring KV-cache reads, which make long-context decode even more memory-bound.
Quiz

Check your understanding

5 questions · 4 to pass · answers are explained as you go. Your best score is saved on this device only.

Progress and quiz scores are saved in this browser only. Back up or restore on the hub.

Was this lesson useful? Tell me what to improve →