NVIDIA Nsight Python Tutorial: How to Profile GPU Kernels with Pandas DataFrames
Overview
In this tutorial, we work with Nsight Python, NVIDIA's open-source Python interface to its kernel profilers. Rather than running a script under the Nsight Compute command line and digging through a report afterward, we mark the code we care about with nsight.annotate, decorate a benchmark function with nsight.analyze.kernel, and get back a pandas DataFrame of hardware metrics per annotated region, per configuration, and per run.
Since version 1.0, the library attaches Nsight Compute to the running Python process, making it usable in Jupyter and Colab. The upcoming 1.1 release adds an experimental CUPTI backend, explicit tool management, and custom information collectors.
All experiments in this tutorial run on a free Colab T4. We cover the following steps:
- Install a current Nsight Compute alongside the one Colab ships
- Profile a first kernel
- Decide how many runs a measurement needs
- Measure what cache and clock control do to the same kernel
- Sweep matrix sizes in fp32 and fp16 with a derived TFLOP/s metric
- Use hardware counters to distinguish compute-bound from memory-bound work
- Measure kernel fusion in bytes moved
- Record clocks and temperature per run
- Compare the CUPTI backend with Nsight Compute
- Keep the raw report for deeper analysis
Setup and Installation
import os
import sys
import glob
import json
import time
import shutil
import textwrap
import traceback
import subprocess
RESULTS = {}
def banner(title):
print("\n" + "=" * 78)
print(title)
print("=" * 78)
Profiling a First Kernel
Mark the region of interest with nsight.annotate, then wrap a benchmark function with nsight.analyze.kernel to collect hardware metrics automatically.
Choosing Measurement Runs
Determine how many runs a measurement requires before trusting the collected metrics.
Cache and Clock Control
Measure how cache settings and clock control affect the same kernel to isolate performance factors.
Matrix Size Sweeps in fp32 and fp16
Sweep matrix sizes in both fp32 and fp16 precision, adding a derived TFLOP/s metric to normalize performance across configurations.
Compute-Bound vs. Memory-Bound Work
Use hardware counters to classify workloads as compute-bound or memory-bound.
Measuring Kernel Fusion
Quantify the benefit of kernel fusion by measuring bytes moved.
Recording Clocks and Temperature
Capture clocks and temperature per run to understand thermal and frequency effects.
CUPTI Backend vs. Nsight Compute
Compare the experimental CUPTI backend with Nsight Compute for compatibility and overhead.
Retaining Raw Reports
Keep the raw report for deeper analysis beyond the summarized DataFrame output.
via MarkTechPost
