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.
pip install nvtxVerified import paths — ran on the pinned version, not inferred.
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.
Upgrade to `nvtx` 0.2.13 or newer. Ensure proper exception handling if using older versions.
Set `export NVTX_DISABLE=1` in your shell before running your Python script.
Use `multiprocessing.set_start_method('spawn', force=True)` or `get_context('spawn').Pool()` for multiprocessing.Prefer `nvtx.annotate()` decorators or context managers for specific code regions, or `nvtx.mark()` for events, rather than global automatic annotation if overhead is a concern.
Organize annotations with a minimal number of domains and leverage categories for further sub-grouping.
Install the package using pip: `python -m pip install nvtx` or conda: `conda install -c conda-forge nvtx`.
Ensure a CUDA-enabled PyTorch is installed and running on a system with a compatible NVIDIA GPU and drivers. If running on a CPU-only system, avoid using `torch.cuda.nvtx` specific calls and use the generic `nvtx` package for CPU-side annotations if desired.
Use `@nvtx.annotate(message='my_range')` as a decorator, `with nvtx.annotate(message='my_range'):` as a context manager, or `range_id = nvtx.start_range(message='my_range'); nvtx.end_range(range_id)` for explicit range management.
Provide a string message when calling `nvtx.mark()`, e.g., `nvtx.mark(message='My event message')`.