NVIDIA Tools Extension (NVTX) Python Binding

12.9.79 · active · verified Sat Mar 28

NVTX (NVIDIA Tools Extension SDK) is a C-based API with Python wrappers for annotating application code with events, ranges, and resources. These annotations provide contextual information for NVIDIA developer tools like Nsight Systems and Nsight Compute, enabling visual profiling and performance analysis of CPU and GPU activities in Python applications. The `nvidia-nvtx-cu12` package provides bindings specifically for CUDA 12.x environments. It is actively maintained with frequent updates, often tied to CUDA toolkit releases.

Warnings

Install

Imports

Quickstart

This example demonstrates how to use `nvtx.annotate` as a decorator for functions and as a context manager for code blocks, and `nvtx.mark` for instantaneous events. The annotated code itself does not directly produce a visible output, but generates profiling data that can be captured and visualized by NVIDIA Nsight Systems.

import time
import nvtx

@nvtx.annotate("my_outer_function", color="blue")
def my_function_to_profile():
    time.sleep(0.05) # Simulate some work
    with nvtx.annotate("inner_loop_work", color="red"):
        for i in range(2):
            time.sleep(0.02) # More work
            nvtx.mark(f"Iteration {i} complete", color="green")

if __name__ == "__main__":
    print("Running annotated code...")
    my_function_to_profile()
    print("Code finished. To profile this, save as e.g., 'demo.py' and run:\nnsys profile python demo.py")
    print("Then open the generated .qdrep file in NVIDIA Nsight Systems for visualization.")

view raw JSON →