File size: 4,598 Bytes
1ab6c33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Profiling: nsys first, ncu second

They answer different questions. Reaching for ncu too early is the standard mistake.

| tool | question it answers |
|---|---|
| **nsys** | Is the time even *inside* a kernel? Launch gaps, dispatch overhead, H2D/D2H stalls, missing overlap, stream serialization. |
| **ncu** | Given that the time is in kernel K, which resource is saturated and why. |

A real case from this repo: an eager reference measured **185 ms wall against 2.06 ms GPU-busy**. nsys
shows that in one screen. ncu would have reported a perfectly healthy kernel and told you nothing.

## Permissions — this WILL bite first

```
==ERROR== ERR_NVGPUCTRPERM - The user does not have permission to access NVIDIA GPU Performance Counters
```

GPU performance counters are privileged. Either:
- run the container with `--cap-add SYS_ADMIN` (verified working), or
- have the host set `NVreg_RestrictProfilingToAdminUsers=0` in the nvidia kernel module options, which
  fixes it for everyone with no per-container flag.

`ncu --version` succeeding proves nothing — it does not touch counters. Test with a real metric.

## nsys

```bash
nsys profile -o /tmp/prof --force-overwrite true --stats=true \
     --trace=cuda,nvtx,osrt python3 my_bench.py
nsys stats --report cuda_gpu_kern_sum /tmp/prof.nsys-rep     # per-kernel totals
nsys stats --report cuda_gpu_trace   /tmp/prof.nsys-rep      # the timeline
```

What to look for:
- **Gaps between kernels** — dispatch-bound. Fix with CUDA Graphs, fewer launches, or fusion.
- **GPU idle while CPU is busy** — python/host overhead, not a kernel problem.
- **Kernels not overlapping** that should — stream/dependency issue.
- **Sum of kernel time << wall time** — the kernel is not your problem yet.

Annotate regions with `torch.cuda.nvtx.range_push/pop` so the timeline is readable.

## ncu

Target one kernel; do not `--set full` by default (it is slow enough to become its own problem).

```bash
ncu --target-processes all \
    --kernel-name regex:my_kernel --launch-skip 5 --launch-count 3 \
    --section SpeedOfLight --section MemoryWorkloadAnalysis \
    --section WarpStateStats --section Occupancy \
    -o /tmp/rep python3 my_bench.py
ncu -i /tmp/rep.ncu-rep --page details
```

Add `-lineinfo` at compile time (nvcc `-lineinfo`; Triton emits it) to get **per-source-line** stall
attribution — the single most useful ncu feature and the one most often left off.

### metric → conclusion

| observation | metric | conclusion |
|---|---|---|
| SM high, DRAM low | `sm__throughput.avg.pct_of_peak_sustained_elapsed` | compute-bound → tensor cores, better instruction mix, less redundant work |
| DRAM high, SM low | `gpu__dram_throughput.avg.pct_of_peak_sustained_elapsed` | bandwidth-bound → cut traffic, fuse passes, improve reuse |
| both low | — | latency-bound → read stall reasons below |
| ↳ memory latency | `smsp__pcsamp_warps_issue_stalled_long_scoreboard` | more loads in flight: `cp.async`/TMA, deeper pipeline, unroll |
| ↳ sync | `smsp__pcsamp_warps_issue_stalled_barrier` | fewer `__syncthreads`, warp specialization |
| ↳ smem/SFU pressure | `smsp__pcsamp_warps_issue_stalled_mio_throttle` | fewer smem ops, cut transcendentals |
| ↳ dependency chain | `smsp__pcsamp_warps_issue_stalled_wait` | more ILP, unroll, independent accumulators |
| uncoalesced access | `l1tex__t_sectors_per_request.avg` | fix layout / access pattern (ideal is 4 sectors per 32-thread request for 32-bit) |
| bank conflicts | `l1tex__data_bank_conflicts_pipe_lsu_mem_shared` | swizzle the shared-memory layout |
| low occupancy | `sm__warps_active.avg.pct_of_peak_sustained_active` | find the limiter: registers, smem, or block size |
| register spills | `launch__registers_per_thread` + SASS `LDL/STL` | cut live values, smaller tile |
| tensor cores idle | `sm__pipe_tensor_op_hmma_cycles_active…` | the MMA you think you issued did not issue |

`tools/profile.sh` collects this set and prints the diagnosis rather than the report.

### ncu gotchas

- **Never time under ncu.** It serializes and replays kernels; wall time under the profiler is
  meaningless.
- **Kernel replay breaks stateful kernels** — persistent kernels, cross-launch atomics, anything with
  side effects. Use `--replay-mode application` (replays the whole process) or range replay.
- Autotuned frameworks compile many variants; use `--launch-skip` to get past warmup so you profile
  the steady-state kernel, not a first-call outlier.
- `--clock-control none` if you are comparing against un-profiled timings; by default ncu locks clocks.