Skip to content

Rooflines on NVIDIA GPUs

This semester, I took a course called Cloud and Machine Learning. One of the core topics covered was performance profiling, and we had a project where we used NVIDIA’s profiling tools to collect GPU metrics while training a deep neural network. In this article, I’ll review what I learned.

First, I’ll discuss roofline models, a visual way to characterize a workload as memory-bound or compute-bound. Then, I’ll walk through my project, where I used NVIDIA Nsight Compute to measure training performance and build roofline plots.

Here is the GitHub repo that contains relevant material, including a full writeup.


Here is a sample roofline model.

roofline

Let’s first look at the axes. The x-axis shows arithmetic intensity (FLOP/byte), and the y-axis shows performance (FLOP/s). Arithmetic intensity measures how much work gets done for each byte of data moved.

Given the axes, we can draw a roofline. The roofline consists of two segments, the slope and the flat line. The slope corresponds to the hardware’s peak bandwidth (GB/s). Note that (FLOP/s) / (FLOP/byte) reduces to byte/s, hence the slope. The flat line corresponds to the hardware’s peak compute. Because the roofline is determined by the hardware’s peak bandwidth and peak compute, it is unique to each hardware or machine.

Given the roofline set by the hardware specs, we can characterize a workload by plotting a single point under the roofline. Anything above the roofline is unattainable because it exceeds the hardware limits.

If the workload falls under the slope (the blue region), the workload is bandwidth-bound. This means that the hardware could do more compute, but data is not being delivered fast enough to keep the compute units busy.

If the workload falls under the flat line (the red region), the workload is compute-bound. Here, data is being moved fast enough, but the limiting factor is how quickly the hardware can perform computations.

Anything far below the roofline is usually not ideal; it means we’re failing to utilize the hardware’s full potential. This could mean that the given hardware is an overkill for the given workload.

Note that “bandwidth” and “compute” can mean different things on different systems. On a GPU, bandwidth usually means how fast data can be moved between DRAM and the GPU cores. In other settings, bandwidth could refer to something else, like network bandwidth on a distributed system.


The project was to train a deep neural network while running a GPU performance profiler, then use the collected metrics to classify the workload as memory-bound, compute-bound, or simply poorly utilizing the hardware. I’ll walk through the project step-by-step.

First, I needed a training workload. I wrote Python code to train either ResNet-18 or ResNet-34 (selected via a command-line argument) on the CIFAR-10 dataset. I chose CIFAR-10 instead of ImageNet mainly because I didn’t want to download and manage a much larger dataset locally.

Ideally, I would have profiled the entire training run, but I treated a single training step (forward + backward + optimizer) as a representative. I used NVTX to bracket exactly one training step. NVTX is NVIDIA’s API for adding named markers/ranges to our code so Nsight tools can attribute GPU activity to specific regions. To avoid cold-start effects, I ran 20 warmup epochs before profiling a single step.

The model was trained on NYU’s GCP-backed HPC cluster (Greene Burst). I ran experiments on instances with either a T4 or an L4 GPU.

During training, I used NVIDIA Nsight Compute (ncu) to collect target metrics. If the metrics are not specified, ncu tries to collect a much larger suite, which can take a long time. After the run, ncu produces an .ncu-rep report file, which can be exported and parsed as a csv.

Terminal window
ncu \
--nvtx \
--nvtx-include "train_step/" \
--target-processes all \
--metrics sm__throughput.avg.pct_of_peak_sustained_elapsed,\
dram__throughput.avg.pct_of_peak_sustained_elapsed,\
gpu__time_duration.sum,\
smsp__sass_thread_inst_executed_op_fadd_pred_on.sum,\
smsp__sass_thread_inst_executed_op_fmul_pred_on.sum,\
smsp__sass_thread_inst_executed_op_ffma_pred_on.sum,\
dram__bytes_read.sum,\
dram__bytes_write.sum \
--force-overwrite \
--export report-file-name \
python3 train.py --epochs 1 --batch-size 128 --profile-one-step --warmup-iters 20

Let’s take a step back and look at which metrics were collected. To plot a workload on a roofline chart, we need performance (FLOP/s) and arithmetic intensity (FLOP/byte).

The total number of floating-point operations (FLOPs) can be computed as F_total = F_add + F_mul + 2 × F_fused. Each fused multiply-add counts as two FLOPs. To convert this into FLOP/s, we also need the duration, which is why we collect gpu__time_duration.sum.

Next, we need the total number of bytes moved to/from DRAM, which can be computed as B_total = B_read + B_write.

I also collect average SM (compute) utilization and DRAM utilization. These aren’t required to compute FLOP/s or FLOP/byte, but they’re useful indicators of how busy the GPU and memory are.

Note that ncu reports metrics for each GPU kernel, which just means each GPU operation (e.g., a convolution, matrix multiplication, or a normalization step). So to obtain one value representative of the full training step, the per-kernel metrics have to be aggregated (e.g., sum the per-kernel FLOP/s to find the total FLOP/s).

Once we have performance (FLOP/s) and arithmetic intensity (FLOP/byte), we can plot the workload under the roofline. But before that, we first need to draw the roofline itself. As mentioned earlier, the roofline is hardware-specific because it depends on the GPU’s peak compute throughput and memory bandwidth. For example, the L4 GPU’s peak FP32 compute is about 30 TFLOP/s, and its peak memory bandwidth is about 300 GB/s (from NVIDIA’s published specs). Using those two numbers, we can draw the L4 roofline like the figure below.

a1_resnet18_bs64_L4.png

I wrote a small Python script to parse the ncu report and generate my own roofline plots. Nsight Compute also has a GUI (ncu-ui) that can display roofline charts directly, but it wasn’t working well in my setup, so it was quicker to just write my own script.

So, let’s look at the result. The roofline plot above is for training ResNet-18 on an L4. Overall, I’d characterize this as poor utilization: the model, dataset, and batch size are simply too small relative to what the GPU can handle, so the point sits far below the roofline. The project didn’t require hitting a specific utilization target, so I intentionally kept the workload lightweight. In hindsight, I wish I had chosen a more demanding setup to better stress the GPU and make the roofline analysis more interesting. The full set of results (different GPUs, models, and batch sizes) is included in my writeup.


I had a lot of fun working on this project, and I also learned a lot along the way.

First takeaway: know your hardware. Before this project, I thought the L4 was simply a “more powerful” GPU than the T4. That’s true in a sense, but it’s more nuanced.

The T4 has peak FP32 compute of around 8 TFLOP/s and peak memory bandwidth of around 300 GB/s. The L4 has peak FP32 compute of around 30 TFLOP/s, but a similar peak memory bandwidth of around 300 GB/s. So the L4 offers much more compute, but not more bandwidth.

This means the L4 really shines on compute-heavy workloads, where the extra compute (and specialized units like tensor cores) can actually be utilized. But if a workload is memory-bound, that extra compute may not help much, since performance is capped by how fast data moves. The lesson is to understand your workload’s bottleneck, and then choose hardware that actually eases that bottleneck.

Working on the project also made me appreciate tools like CUDA and Nsight Compute, which can give you real visibility into what the hardware is doing. At the same time, the world of performance profiling felt a bit overwhelming; pinpointing the true bottleneck and then optimizing for it can be a daunting task. Sometimes the issue might be subtle, like cache-line ping-ponging or false sharing, and fixing it would require one to truly understand the underlying system and the hardware.