Profiling kernels with Nsight Systems and Nsight Compute
A hands-on workflow for finding out why a kernel or a model step is slow: Nsight Systems for the whole timeline (gaps, launches, copies, NVTX ranges), Nsight Compute for one kernel (speed of light, memory chart, occupancy, warp stall reasons, roofline), the PyTorch profiler when you can't use either, and how to turn each finding into a specific fix.
By Sandip Maity, Applied AI Engineer
Every lesson so far has given you a prediction and asked you to measure. When the measurement falls short, you need to know why, and guessing is slow. This lesson is a workflow for profiling: which tool to open, what to look at first, and how to turn what you see into a fix. The profiling overview in the Inference Engineering course introduced the tools; here you use them on the kernels you wrote.
#Two tools, two questions
| Question | Tool | What it shows |
|---|---|---|
| Where does the time go across a whole step or program? | Nsight Systems (nsys) |
A timeline of CPU threads, CUDA API calls, kernels, copies and your own named ranges |
| Why is this one kernel slow? | Nsight Compute (ncu) |
Detailed hardware metrics for single kernels: throughput, memory traffic, occupancy, stall reasons |
Always start with Nsight Systems. There's no point tuning a kernel that takes 5% of the step when the GPU is idle for 40% of it.
A note on access: Nsight Compute reads GPU performance counters, which many shared cloud GPUs block (you'll see an error containing ERR_NVGPUCTRPERM). Colab may block it too. If so, use a rented VM where you control the driver, or set the driver option that allows non-admin profiling on your own machine. Nsight Systems' basic timeline usually works everywhere.
#Nsight Systems: the timeline
!nsys profile -o step --trace=cuda,nvtx,osrt --force-overwrite true python decode_step.py
!nsys stats step.nsys-rep --report cuda_gpu_kern_sumThe stats report lists kernels by total time, which is often enough to find the top offenders. For the full picture, open the .nsys-rep file in the Nsight Systems GUI on your own machine.
#Add NVTX ranges
Kernel names alone don't tell you which layer you're in. Wrap the parts of your code in named ranges:
import torch
with torch.cuda.nvtx.range("decode_step"):
for i, layer in enumerate(model.layers):
with torch.cuda.nvtx.range(f"layer_{i}"):
with torch.cuda.nvtx.range("attention"):
h = layer.attn(x)
with torch.cuda.nvtx.range("mlp"):
x = layer.mlp(h)The ranges appear on the timeline above the kernels they launched. Keep them coarse; thousands of tiny ranges add CPU overhead of their own.
#What to look for
Here are simulated decode steps on a profiler timeline. Step through the four patterns it shows; each has a typical cause and fix:
GPU busy
96%
Diagnosis
memory-bound
Healthy, memory-bound decode
Large kernels (matmuls reading weights) back to back, almost no gaps. Next step: check achieved bandwidth in Nsight Compute; if it is near peak, only fewer bytes (quantisation) or more batching helps.
Two decode steps on the GPU row of a profiler timeline. Coloured blocks are kernels; gaps are idle GPU time.
| Pattern on the timeline | Likely cause | First thing to try |
|---|---|---|
| Gaps between many short kernels | CPU launch overhead | CUDA graphs, torch.compile, fewer, fused kernels |
| A long gap before a kernel, with a sync call on the CPU row | A hidden synchronisation (.item(), .cpu(), a print) |
Remove it, or move it outside the hot loop |
| Memcpy blocks between kernels | Host-device copies in the step | Keep data on the GPU, or overlap with streams and pinned memory |
| One kernel taking most of the step | That kernel | Open it in Nsight Compute |
| GPU busy, but slow overall | The kernels themselves | Nsight Compute on the top three kernels |
Pro content
Keep reading with Inference Academy Pro
Unlock the rest of this lesson, including 1 animated diagram, plus every advanced lesson, hands-on lab, the larger playground models and completion certificates.
Still to come in this lesson
- Nsight Compute: one kernel at a time
- Profiling the kernels from this course
- When you can't use Nsight: the PyTorch profiler
- A profiling workflow you can repeat
- What you can now do
- Common mistakes
- Exercise
- Going deeper
You will be able to answer
- You want to know why a decode step has gaps between kernels. Which tool do you start with?
- Nsight Compute shows a kernel at 92% memory throughput and 15% compute throughput. What does that mean?
- Both memory and compute throughput are low, around 20%. What is the most likely class of problem?
- Why does Nsight Compute replay each kernel several times?
- What do NVTX ranges add to an Nsight Systems profile?
Articles that build on this lesson