NVTX Python Bindings

0.2.15 · active · verified Sun Apr 12

NVTX (NVIDIA Tools Extension Library) is a cross-platform API for annotating source code to provide contextual information to developer tools like NVIDIA Nsight Systems. The `nvtx` Python library provides native Python wrappers for a subset of the NVTX C API, enabling Python developers to mark events and define code ranges for profiling and visualization of CPU and GPU activities. The current Python package version is 0.2.15, with active development tied to the broader NVTX v3.x.x core library releases.

Warnings

Install

Imports

Quickstart

This example demonstrates how to use `nvtx.annotate` as both a decorator for a function and a context manager for a code block. To observe these annotations, you typically run the Python script using NVIDIA Nsight Systems' command-line interface (`nsys profile`) and then visualize the generated `.qdrep` file in the Nsight Systems GUI.

import time
import nvtx
import os

# Define a function to be annotated
@nvtx.annotate(color="blue")
def my_function():
    for i in range(os.environ.get('NVTX_ITERATIONS', 2)):
        with nvtx.annotate(f"my_loop_iteration_{i}", color="red"):
            time.sleep(0.1)

if __name__ == "__main__":
    print("Running annotated code...")
    my_function()
    print("Code execution complete. Profile with NVIDIA Nsight Systems.")
    # To profile, run from your terminal:
    # nsys profile -t nvtx python your_script_name.py
    # Then open the generated .qdrep file in Nsight Systems GUI.

view raw JSON →