perfr, from Orbit · Post 32

What the GPU Was Doing

A profile of a PyTorch training loop said the main thread spent over half its time in the NVIDIA driver, waiting, and said nothing about what it was waiting for. perfr has a new switch that records the other side: every CUDA kernel, memory copy and fill, as a span from when the GPU started it to when it ended, under the thread that launched it. This post is what a capture has with it on, how it is read without changing the program, what it costs, and which programs it works for. It was written against nanoGPT and then tried on three programs it had never seen. It failed on one.

449,110kernels, copies and fills in 28 seconds of nanoGPT, each a span under the thread that launched it
83%of those 28 seconds the GPU was running one of them
56.9 msthe main thread's average wait in loss.item(), now with what it waits for drawn beneath it
1 of 3other programs it failed on when first tried: CuPy, which never loads the library the search started from

A CUDA program does not run its kernels. It hands them to the driver, which puts them on a queue, and the call returns. The GPU gets to them later. A sampling profiler sees the CPU's part of that: a short call that launches something, and then, somewhere else in the program, a wait. In nanoGPT's training loop the wait is one line, loss.item(), which the script calls every tenth step to print the loss. It needs a number that is still on the GPU, so it blocks until everything queued before it has run. Of the main thread's samples, 54% were there, in cudaStreamSynchronize, spinning.

That is a true reading and half a profile. It says the loop is waiting for the GPU. It does not say whether the GPU was busy with the forward pass or the backward pass, with matrix multiplies or with copies, or idle. Finding out meant stopping and running PyTorch's own profiler, which is a second tool with a second timeline. The switch described here puts that half in the capture.

The other half

The switch is GPU, under COLLECT, off by default; gpu_kernels in the capture request and in the MCP tool. With it on, each thread that launches GPU work gets a lane labelled GPU below it, and every kernel is a span on that lane, placed when the GPU ran it and not when the thread asked for it.

A third of a second of nanoGPT training in the perfr viewer: the main thread's forward and backward scopes, two long waits in loss.item(), and GPU lanes under MainThread and pt_autograd_0 filled with kernels
A third of a second of training. The scopes on MainThread are the CPU's side of each step, from the lines added to the script. The two GPU lanes are what the GPU ran: under MainThread the kernels of the forward pass and the optimizer, under pt_autograd_0 those of the backward pass. The long orange scopes are loss.item(), 75 and 51 ms, and the kernels keep coming under them: that is the backlog being worked off. The red line is the loss, from one perfr.value call, and the two lines at the bottom are what the script printed when each wait ended.

The picture explains the wait. The CPU's side of a step is short: on average 2.65 ms in the forward pass, 5.28 ms in loss.backward(), 0.89 ms in the optimizer and 1.09 ms fetching a batch, 9.9 ms in all. Almost none of that is the model's arithmetic. It is Python and PyTorch deciding what to launch. The GPU needs longer than 9.9 ms for what one step launches, so with every step the thread gets further ahead of it. At the tenth step the script asks for the loss and pays the difference back: 113 waits in this capture, 56.9 ms on average, 6.43 of the 28 seconds.

The end of one wait in loss.item() followed by the next training steps, with the kernels of each pass on the GPU lanes
The end of one wait, and the steps after it. During the wait on the left the two GPU lanes alternate: a block under MainThread (a forward pass), then one under pt_autograd_0 (its backward pass). After it the CPU's scopes come quicker than the blocks of kernels do, which is the thread getting ahead of the GPU again.

The two lanes never overlap, and that is not a drawing choice. A CUDA stream runs what it is given one kernel at a time, in the order it was given, and PyTorch gives everything to one stream. In the probe this was prototyped with, not one of 273,992 kernels started before the one before it had ended, and the thread that had launched the running kernel changed 1,502 times. So the two lanes are one queue, split by who filled it. PyTorch runs the backward pass on a thread of its own, which is why splitting by thread happens to split a training step into its forward and backward halves.

The spans are also rows of the statistics, by kernel name, and a thread id keeps one lane's rows. For this capture:

Under MainThreadUnder pt_autograd_0
Kernels, copies and fills248,856200,254
GPU time13.37 s9.87 s
Matrix multiplies48.9%56.9%
Elementwise kernels compiled by torch.compile33.0%28.5%
Attention9.0%11.8%
Copies and fills5.4%1.8%
Optimizer3.7%none

Together that is 23.25 s of GPU time in a 28.0 s capture: the GPU was running a kernel 83% of the time. The main thread's column is the forward pass, the optimizer and the evaluation passes the script runs every 250 steps. For this model on this card, the answer to "what was it waiting for" is matrix multiplies, about half of everything, with no single kernel to blame.

How it is read

NVIDIA ships a library for exactly this, CUPTI, which its own profilers and PyTorch's are built on. It has two interfaces, and the switch needs both, because each knows half of what a span is.

The callback interface calls a function on the thread that enters a driver call, such as cuLaunchKernel, and passes the number the driver gave that call, its correlation id. That is the only place the launching thread is known. The activity interface hands over, some time later and on a thread of CUPTI's own, a buffer of records: one per kernel that has finished, with its name, when the GPU started it, when it ended, and the same correlation id.

on MainThread, entering cuLaunchKernel
    callback   correlation 81422, thread 3879950
on CUPTI's thread, about 25 ms later
    activity   correlation 81422, name, start, end
the agent
    a span on the GPU lane of thread 3879950

The callback runs inside the driver call of a thread that is trying to launch 16,000 things a second, so it does one thing: it stores the thread id in a fixed table of 65,536 atomic slots, indexed by the correlation id. No lock, no allocation. When the record arrives the agent looks the id up, and if the slot still holds it, the span goes under that thread. A record with no launch on file, because the work was queued before the capture started, is counted and left out.

Three details made it fit a profiler that attaches to a running program:

CUPTI flushes its buffers ten times a second, so on a live capture the kernels appear about a tenth of a second behind the samples.

Names that fit in a ring

A kernel is named by its symbol, demangled when it is a C++ one and cut at 120 bytes. Compiled PyTorch gives readable ones, such as triton_poi_fused__unsafe_view_gelu_5. The libraries give long ones: cutlass::Kernel2<cutlass_80_tensorop_bf16_s16816gemm_relu_bf16_64x64_32x6_nt_align8> is a matrix multiply.

Those long names broke the first version. Spans travel from the agent to the service through a ring of 64-byte records, with 32 bytes of name in the record and the rest in records that follow. A flush of a few thousand kernels with 80-byte names is several records each, all at once, and the ring lapped: the first capture lost 2,395 records. The fix is to send a name as text once and as a number afterwards. The agent numbers each name itself, sends the text with its number the first time and again every 2,048 uses, and sends a 9-byte token in between, which fits in one record. The same capture then lost none.

What it costs

nanoGPT, two runs each waySwitch offSwitch on
Training step16.3 and 17.9 ms17.2 and 17.7 ms
Evaluation pass2.11 and 2.12 s2.17 and 2.16 s

The times are the script's own, taken after a GPU synchronise. On a training step, two runs each way cannot tell the switch from the noise. On an evaluation pass it is about 2%. Two runs is not a benchmark, and the honest reading is "small", not a number.

Is it only PyTorch

By construction, no. CUPTI sits under the frameworks, at the driver, so the switch sees whatever reaches an NVIDIA GPU through CUDA and does not know which library sent it. But "by construction" is a claim about a design. The agent had only ever been run against one program, so it was run against three it was not written for.

ProgramWhat it exercises20-second capture
PyTorch inference of a small convolutional netEager mode, no torch.compile, no autograd, cuDNN39,917 spans under the main thread, GPU busy 95%; cuDNN's kernels by name
PyTorch, two threads with a CUDA stream eachKernels that really do run at once34,201 spans, half under each thread; 23.05 s of GPU time in 20 s
CuPy, no PyTorch in the processAnother framework, its own compiled kernels, the driver called directlyFirst run: nothing. After a fix: 721,468 spans, none dropped

The inference run needed nothing. With no autograd thread there is one lane, and the names are cuDNN's and PyTorch's own (cudnn::bn_fw_inf_1C11_kernel_NCHW<float, float, true, 1>, max_pool_forward_nchw<float, int>). The two-stream run is the case nanoGPT could not show: 23 seconds of GPU time in a 20-second capture means the two lanes overlap, and each thread's kernels are under the thread that launched them.

The CuPy run said GPU kernels are not recorded: CUPTI (libcupti.so) was not found beside the program's CUDA runtime, with the library sitting in the same directory as everything else CuPy had loaded. The agent looked for CUPTI beside libcudart, the CUDA runtime, because that is where PyTorch has it. CuPy never maps the runtime. It calls the driver itself and loads cuBLAS and the kernel compiler. The search now starts from any CUDA library in the process's maps, and the same run gave 36,000 spans a second, including the script's own ElementwiseKernel under the name the script gave it. That is the third commit of the pull request, and it would not exist if the answer to this section had been written from the design.

So, what carries over and what does not:

What it does not see

A kernel is one span; nothing inside it is recorded. The program must have initialised CUDA before the capture starts, and the status line says so when it has not. CUPTI takes one subscriber per process, so a program whose own profiler is recording, PyTorch's included, cannot be read at the same time; the status line says that too. Child processes are separate targets. Not yet tried: CUDA 12, a CUDA toolkit install as opposed to pip wheels, more than one GPU, and CUDA graphs.

One thing the picture invites and the data does not yet answer: which scope a kernel belongs to. A span is drawn when the GPU ran it, about 25 ms after its launch at the median in this loop and 80 ms at the 99th percentile, so the kernels under a forward scope on the screen are mostly not that scope's kernels. The launch time is known at the callback. Carrying it to the span, so that a kernel can be traced back to the scope that was open when it was launched, is the obvious next step.

The short version. perfr's new GPU switch records every CUDA kernel, copy and fill as a span under the thread that launched it, at the time the GPU ran it, with nothing changed in the program. It reads NVIDIA's CUPTI from inside the target: a callback on each launch gives the thread, an activity record gives the name and the times, and a correlation id joins them. On nanoGPT that is 16,000 spans a second with no measurable change to a training step, and it shows the 57 ms wait in loss.item() as the GPU working off ten steps of backlog, half of it matrix multiplies. It is not specific to PyTorch or to training: it worked unchanged on eager inference and on two concurrent streams, and on CuPy after one fix to where it looks for CUPTI. It is specific to CUDA on Linux.