nvtx#
2026-09-24
15 min read time
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 viasys.setprofile/threading.setprofile.enabled- honors theNVTX_DISABLEenvironment 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):
domain/get_domain/Domain- ROCTX has no domain concept. Domain objects route to the same global ROCTX calls; the domain name has no effect and cross-domain isolation is lost. All events share a single per-thread namespace.color(andcolor_to_hex) - no color channel.category/get_category_id- no category channel.payload- no payload channel.RegisteredString/get_registered_string,EventAttributes/get_event_attributes/set_event_attributes- lightweight holders only; the message is re-sent as a plain string on every call.
Accepted but complete no-ops (record nothing):
Counter,Int64Counter,Float64Counter,ExtCounter,get_counter,sample,sample_no_value,batch_submit,get_timestamp.CounterSemanticsand the enumsCounterValueType,CounterInterpolation,CounterNoValueReason,TimestampType(defined for import compatibility only).numpy_dtype(builds a NumPy dtype but carries no runtime effect).
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 anNvtxCompatWarningand then drop/no-op."error"- raise anNvtxCompatError.
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#
Warning emitted when an unsupported NVTX feature/argument is used. |
|
Error raised when an unsupported NVTX feature/argument is used. |
Classes#
Annotate code ranges using a context manager or a decorator. |
|
Programmatically control NVTX automatic annotations. |
|
A stand-in for an NVTX registered string. |
|
A holder for event attributes. |
|
A per-domain interface to the NVTX API. |
|
A no-op replacement for |
|
Base class for NVTX counters. |
|
Counter for signed 64-bit integer samples (no-op). |
|
Counter for double-precision floating-point samples (no-op). |
|
Counter for NumPy dtype-based samples and counter groups (no-op). |
|
A no-op replacement for |
|
Metadata describing how a counter value should be interpreted. |
|
How counter sample values relate to previous samples. |
|
How tools should interpolate counter values between samples. |
|
Reasons for recording a counter sample without a value. |
|
Timestamp domains that can be associated with batched samples. |
Functions#
Return the current compatibility mode ( |
|
|
Set the compatibility mode. |
|
Return |
|
Mark an instantaneous event. |
|
Mark the beginning of a (nested, per-thread) code range. |
|
Mark the end of a code range started with |
|
Mark the beginning of a process range. |
|
Mark the end of a process range started with |
|
Get or create a |
|
Construct a NumPy dtype, optionally carrying counter semantics metadata. |
Package Contents#
- exception nvtx.NvtxCompatWarning#
Bases:
UserWarningWarning emitted when an unsupported NVTX feature/argument is used.
Only emitted when the compatibility mode is
"warn"(seeset_compat_mode).
- exception nvtx.NvtxCompatError#
Bases:
RuntimeErrorError raised when an unsupported NVTX feature/argument is used.
Only raised when the compatibility mode is
"error"(seeset_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
modeis not a recognized compatibility mode.
- nvtx.enabled()#
Return
Trueif NVTX annotations are enabled.Annotations are disabled when the
NVTX_DISABLEenvironment 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 (
messagedefaults 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 toend_range.domain_handleis always0in 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
messagereaches ROCTX;color,categoryandpayloadare 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
nameis accepted but ignored, and every method routes to the same global ROCTX calls, so events from differentDomaininstances 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).
- start_range(attributes=None, **kwargs)#
Mark the beginning of a process range (see
start_range).
- get_category_id(name)#
Return a synthetic category id (not honored by ROCTX).
- get_registered_string(string)#
Return a
RegisteredStringwrapper (no C registration).
- get_event_attributes(message=None, color=None, category=None, payload=None)#
Create an
EventAttributesobject.
- set_event_attributes(attributes, *, message=_DONT_SET, color=_DONT_SET, category=_DONT_SET, payload=_DONT_SET)#
Set attributes on an existing
EventAttributesobject.
- 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
Domainused 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
Domainfor a domain name.Returns a
DummyDomainwhen 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:
CounterCounter for signed 64-bit integer samples (no-op).
- class nvtx.Float64Counter(domain=None, name=None)#
Bases:
CounterCounter for double-precision floating-point samples (no-op).
- class nvtx.ExtCounter(domain=None, name=None, dtype=None)#
Bases:
CounterCounter for NumPy dtype-based samples and counter groups (no-op).
- class nvtx.DummyCounter(domain=None, name=None, dtype=None)#
Bases:
CounterA no-op replacement for
Counterwhen 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.IntEnumHow counter sample values relate to previous samples.
- ABSOLUTE = 0#
- DELTA = 1#
- DELTA_SINCE_START = 2#
- class nvtx.CounterInterpolation#
Bases:
enum.IntEnumHow tools should interpolate counter values between samples.
- POINT = 0#
- SINCE_LAST = 1#
- UNTIL_NEXT = 2#
- LINEAR = 3#
- class nvtx.CounterNoValueReason#
Bases:
enum.IntEnumReasons for recording a counter sample without a value.
- ZERO = 0#
- UNCHANGED = 1#
- UNAVAILABLE = 2#
- class nvtx.TimestampType#
Bases:
enum.IntEnumTimestamp 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. Thecounter_semanticsmetadata is attached but has no runtime effect on ROCTX.- Raises:
RuntimeError:If NumPy is not installed.