nvtx

Contents

nvtx#

2026-09-24

15 min read time

Applies to Linux and Windows

Advanced Micro Devices, Inc.

An nvtx (NVTX) compatibility shim for ROCm.

This is NOT a port of the upstream (Apache-2.0) nvtx Python package. It is a fresh, MIT-licensed re-implementation of the nvtx Python surface, backed entirely by ROCTX via the high-level roctx bindings.

Goal: code that does import nvtx and uses annotations, ranges and markers keeps working on AMD GPUs without modification. Profile with a ROCm-aware tool (e.g. rocprof-compute) instead of Nsight Systems.

Implemented surface (faithfully backed by ROCTX):

  • mark - instantaneous event (roctxMarkA).

  • push_range / pop_range - nested, per-thread ranges (roctxRangePushA / roctxRangePop).

  • start_range / end_range - process ranges that may cross threads (roctxRangeStartA / roctxRangeStop).

  • annotate - decorator and context manager around push/pop ranges.

  • Profile - automatic function annotation via sys.setprofile / threading.setprofile.

  • enabled - honors the NVTX_DISABLE environment variable and the availability of the ROCTX runtime.

Important

Limitations. ROCTX is a message-only tracing API. It has no concept of domains, colors, categories, payloads, registered strings, or counters. The following parts of the nvtx API are therefore accepted for source compatibility but do not behave as they would on NVIDIA hardware:

Accepted but silently dropped (only the message reaches ROCTX):

Accepted but complete no-ops (record nothing):

The counter/semantics surface additionally postdates NVTX release-v3 and is provided here only as forward-compatible stubs.

Compatibility mode#

To help audit whether code is portable to ROCTX, the shim can report when an unsupported feature is used or an unsupported (dropped) argument is supplied. The mode is one of:

  • "silent" (default) - accept and drop/no-op silently, preserving drop-in behavior.

  • "warn" - emit an NvtxCompatWarning and then drop/no-op.

  • "error" - raise an NvtxCompatError.

Select it via the HIP_PYTHON_NVTX_COMPAT environment variable (silent / warn / error) or at runtime with set_compat_mode / get_compat_mode. The checks fire regardless of whether tracing is enabled (see enabled), so they also flag non-portable usage in CI that runs without a ROCTX runtime.

Submodules#

Exceptions#

NvtxCompatWarning

Warning emitted when an unsupported NVTX feature/argument is used.

NvtxCompatError

Error raised when an unsupported NVTX feature/argument is used.

Classes#

annotate

Annotate code ranges using a context manager or a decorator.

Profile

Programmatically control NVTX automatic annotations.

RegisteredString

A stand-in for an NVTX registered string.

EventAttributes

A holder for event attributes.

Domain

A per-domain interface to the NVTX API.

DummyDomain

A no-op replacement for Domain used when NVTX is disabled.

Counter

Base class for NVTX counters.

Int64Counter

Counter for signed 64-bit integer samples (no-op).

Float64Counter

Counter for double-precision floating-point samples (no-op).

ExtCounter

Counter for NumPy dtype-based samples and counter groups (no-op).

DummyCounter

A no-op replacement for Counter when the domain is disabled.

CounterSemantics

Metadata describing how a counter value should be interpreted.

CounterValueType

How counter sample values relate to previous samples.

CounterInterpolation

How tools should interpolate counter values between samples.

CounterNoValueReason

Reasons for recording a counter sample without a value.

TimestampType

Timestamp domains that can be associated with batched samples.

Functions#

get_compat_mode()

Return the current compatibility mode (silent/warn/error).

set_compat_mode(mode)

Set the compatibility mode.

enabled()

Return True if NVTX annotations are enabled.

mark([message, color, domain, category, payload])

Mark an instantaneous event.

push_range([message, color, domain, category, payload])

Mark the beginning of a (nested, per-thread) code range.

pop_range([domain])

Mark the end of a code range started with push_range.

start_range([message, color, domain, category, payload])

Mark the beginning of a process range.

end_range(range_id)

Mark the end of a process range started with start_range.

get_domain([name])

Get or create a Domain for a domain name.

numpy_dtype(*args[, counter_semantics])

Construct a NumPy dtype, optionally carrying counter semantics metadata.

Package Contents#

exception nvtx.NvtxCompatWarning#

Bases: UserWarning

Warning emitted when an unsupported NVTX feature/argument is used.

Only emitted when the compatibility mode is "warn" (see set_compat_mode).

exception nvtx.NvtxCompatError#

Bases: RuntimeError

Error raised when an unsupported NVTX feature/argument is used.

Only raised when the compatibility mode is "error" (see set_compat_mode).

nvtx.get_compat_mode()#

Return the current compatibility mode (silent/warn/error).

nvtx.set_compat_mode(mode)#

Set the compatibility mode.

Args:
mode:

One of "silent", "warn" or "error".

Raises:
ValueError:

If mode is not a recognized compatibility mode.

nvtx.enabled()#

Return True if NVTX annotations are enabled.

Annotations are disabled when the NVTX_DISABLE environment variable is set (to any value) or when the ROCTX runtime is not available.

class nvtx.annotate(message=None, color=_UNSET, domain=_UNSET, category=_UNSET, payload=_UNSET)#

Annotate code ranges using a context manager or a decorator.

Args:
message:

Message for the range; as a decorator it defaults to the decorated function name, as a context manager to the empty string.

color:

Accepted for compatibility; dropped (ROCTX has no color).

domain:

Accepted for compatibility; dropped (ROCTX has no domains).

category:

Accepted for compatibility; dropped.

payload:

Accepted for compatibility; dropped.

Examples:

Using a decorator (message defaults to "func"):

@nvtx.annotate(color="red", domain="my_domain")
def func():
    ...

Using a context manager:

with nvtx.annotate("my_code_range", color="blue"):
    ...
init_args#
message = None#
nvtx.mark(message=None, color=_UNSET, domain=_UNSET, category=_UNSET, payload=_UNSET)#

Mark an instantaneous event.

Args:
message:

A message associated with the event.

color:

Accepted for compatibility; dropped (supplying it triggers the compatibility mode).

domain:

Accepted for compatibility; dropped (supplying it triggers the compatibility mode).

category:

Accepted for compatibility; dropped (supplying it triggers the compatibility mode).

payload:

Accepted for compatibility; dropped (supplying it triggers the compatibility mode).

nvtx.push_range(message=None, color=_UNSET, domain=_UNSET, category=_UNSET, payload=_UNSET)#

Mark the beginning of a (nested, per-thread) code range.

Args:
message:

A message associated with the annotated code range.

color:

Accepted for compatibility; dropped (supplying it triggers the compatibility mode).

domain:

Accepted for compatibility; dropped (supplying it triggers the compatibility mode).

category:

Accepted for compatibility; dropped (supplying it triggers the compatibility mode).

payload:

Accepted for compatibility; dropped (supplying it triggers the compatibility mode).

Note:

When applicable, prefer annotate.

nvtx.pop_range(domain=_UNSET)#

Mark the end of a code range started with push_range.

Args:
domain:

Accepted for compatibility; dropped (supplying it triggers the compatibility mode).

nvtx.start_range(message=None, color=_UNSET, domain=_UNSET, category=_UNSET, payload=_UNSET)#

Mark the beginning of a process range.

Args:
message:

A message associated with the range.

color:

Accepted for compatibility; dropped (supplying it triggers the compatibility mode).

domain:

Accepted for compatibility; dropped (supplying it triggers the compatibility mode).

category:

Accepted for compatibility; dropped (supplying it triggers the compatibility mode).

payload:

Accepted for compatibility; dropped (supplying it triggers the compatibility mode).

Returns:

A (range_id, domain_handle) tuple that must be passed to end_range. domain_handle is always 0 in this shim.

nvtx.end_range(range_id)#

Mark the end of a process range started with start_range.

Args:
range_id:

The tuple (or bare id) returned by start_range.

class nvtx.Profile(linenos=True, annotate_cfuncs=True)#

Programmatically control NVTX automatic annotations.

Wraps every Python (and, optionally, C-extension) function call in a ROCTX range using sys.setprofile / threading.setprofile.

Args:
linenos:

Include file and line number information in annotations.

annotate_cfuncs:

Also annotate C-extension and builtin functions.

Examples:
>>> import nvtx, time
>>> pr = nvtx.Profile()
>>> pr.enable()
>>> time.sleep(1)  # captured by nvtx
>>> pr.disable()
>>> time.sleep(1)  # not captured
linenos = True#
annotate_cfuncs = True#
enable()#

Start annotating function calls automatically.

disable()#

Stop annotating function calls automatically.

class nvtx.RegisteredString(domain=None, string=None)#

A stand-in for an NVTX registered string.

ROCTX has no string-registration API, so this simply wraps the raw string; the message is re-sent as a plain string on every event.

domain = None#
string = None#
class nvtx.EventAttributes(domain=None, message=None, color=None, category=None, payload=None)#

A holder for event attributes.

Only message reaches ROCTX; color, category and payload are stored for compatibility but dropped.

domain = None#
message = None#
color = None#
category = None#
payload = None#
class nvtx.Domain(name=None)#

A per-domain interface to the NVTX API.

Important

ROCTX has no domain concept. The name is accepted but ignored, and every method routes to the same global ROCTX calls, so events from different Domain instances are not isolated from one another.

name = None#
handle = 0#
push_range(attributes=None, **kwargs)#

Mark the beginning of a code range (see push_range).

pop_range()#

Mark the end of a code range (see pop_range).

mark(attributes=None, **kwargs)#

Mark an instantaneous event (see mark).

start_range(attributes=None, **kwargs)#

Mark the beginning of a process range (see start_range).

end_range(range_id)#

Mark the end of a process range (see end_range).

get_category_id(name)#

Return a synthetic category id (not honored by ROCTX).

get_registered_string(string)#

Return a RegisteredString wrapper (no C registration).

get_event_attributes(message=None, color=None, category=None, payload=None)#

Create an EventAttributes object.

set_event_attributes(attributes, *, message=_DONT_SET, color=_DONT_SET, category=_DONT_SET, payload=_DONT_SET)#

Set attributes on an existing EventAttributes object.

get_timestamp()#

Return a monotonic timestamp (for API compatibility only).

get_counter(name, dtype, *, description=None, scope=None, semantics=None, time_domain=None)#

Return a no-op counter (ROCTX has no counter API).

class nvtx.DummyDomain#

A no-op replacement for Domain used when NVTX is disabled.

handle = 0#
name = None#
push_range(attributes=None, **kwargs)#
pop_range()#
mark(attributes=None, **kwargs)#
start_range(attributes=None, **kwargs)#
end_range(range_id)#
get_category_id(name)#
get_registered_string(string)#
get_event_attributes(message=None, color=None, category=None, payload=None)#
set_event_attributes(attributes, **kwargs)#
get_timestamp()#
get_counter(name, dtype, **kwargs)#
nvtx.get_domain(name=None)#

Get or create a Domain for a domain name.

Returns a DummyDomain when NVTX is disabled.

class nvtx.Counter(domain=None, name=None, dtype=None)#

Base class for NVTX counters.

Important

ROCTX has no counter API, so all counter operations in this shim are no-ops that record nothing.

domain = None#
name = None#
dtype = None#
sample(value)#

Record one counter sample (no-op).

sample_no_value(reason)#

Record a missing counter sample (no-op).

batch_submit(values, timestamps)#

Record a batch of counter samples (no-op).

class nvtx.Int64Counter(domain=None, name=None)#

Bases: Counter

Counter for signed 64-bit integer samples (no-op).

class nvtx.Float64Counter(domain=None, name=None)#

Bases: Counter

Counter for double-precision floating-point samples (no-op).

class nvtx.ExtCounter(domain=None, name=None, dtype=None)#

Bases: Counter

Counter for NumPy dtype-based samples and counter groups (no-op).

class nvtx.DummyCounter(domain=None, name=None, dtype=None)#

Bases: Counter

A no-op replacement for Counter when the domain is disabled.

class nvtx.CounterSemantics(unit=None, value_type=None, interpolation=None, min=None, max=None, unit_scale_numerator=1, unit_scale_denominator=1)#

Metadata describing how a counter value should be interpreted.

Stored for API compatibility only; it has no runtime effect on ROCTX.

unit = None#
value_type#
interpolation#
min = None#
max = None#
unit_scale_numerator = 1#
unit_scale_denominator = 1#
class nvtx.CounterValueType#

Bases: enum.IntEnum

How counter sample values relate to previous samples.

ABSOLUTE = 0#
DELTA = 1#
DELTA_SINCE_START = 2#
class nvtx.CounterInterpolation#

Bases: enum.IntEnum

How tools should interpolate counter values between samples.

POINT = 0#
SINCE_LAST = 1#
UNTIL_NEXT = 2#
LINEAR = 3#
class nvtx.CounterNoValueReason#

Bases: enum.IntEnum

Reasons for recording a counter sample without a value.

ZERO = 0#
UNCHANGED = 1#
UNAVAILABLE = 2#
class nvtx.TimestampType#

Bases: enum.IntEnum

Timestamp domains that can be associated with batched samples.

NONE = 0#
TOOL_PROVIDED = 1#
CPU_TSC = 2#
CPU_CLOCK_GETTIME_REALTIME = 3#
CPU_CLOCK_GETTIME_MONOTONIC = 4#
GPU_GLOBALTIMER = 5#
nvtx.numpy_dtype(*args, counter_semantics=None, **kwargs)#

Construct a NumPy dtype, optionally carrying counter semantics metadata.

Accepts the same arguments as numpy.dtype. The counter_semantics metadata is attached but has no runtime effect on ROCTX.

Raises:
RuntimeError:

If NumPy is not installed.