pynvml#

2026-09-24

12 min read time

Applies to Linux and Windows

Advanced Micro Devices, Inc.

A pynvml (NVML) compatibility shim for ROCm.

This is NOT a port of the upstream pynvml / nvidia-ml-py source. It is a fresh, MIT-licensed re-implementation of the subset of the NVML Python surface used by HIP ports of RAPIDS projects (e.g. cuDF), backed entirely by AMD SMI via the high-level rocm.bindings.amdsmi bindings.

Goal: code that does import pynvml and calls a handful of nvml* functions keeps working on AMD GPUs without modification. Where concepts do not exist on ROCm (e.g. MIG), the corresponding calls degrade gracefully.

Implemented surface:

  • nvmlInit / nvmlInitWithFlags / nvmlShutdown

  • nvmlDeviceGetCount

  • nvmlDeviceGetHandleByIndex / nvmlDeviceGetHandleByUUID

  • nvmlDeviceIsMigDeviceHandle / nvmlDeviceGetDeviceHandleFromMigDeviceHandle

  • nvmlDeviceGetMigMode / nvmlDeviceGetMaxMigDeviceCount / nvmlDeviceGetMigDeviceHandleByIndex (MIG unsupported on ROCm)

  • nvmlDeviceGetMemoryInfo / nvmlDeviceGetName / nvmlDeviceGetUUID

  • nvmlDeviceGetTemperature / nvmlDeviceGetPowerUsage

  • nvmlDeviceGetUtilizationRates

  • nvmlDeviceGetCpuAffinity (Linux-only, like NVML)

  • nvmlDeviceGetComputeRunningProcesses (best-effort, see note)

  • NVMLError and the per-code subclasses consumers commonly catch.

NVML eventually migrates to cuda.core.system; this shim covers the legacy pynvml entry points still in use until that migration completes.

Attributes#

Exceptions#

NVMLError

Base class mirroring pynvml.NVMLError.

Classes#

c_nvmlMemory_t

NVML c_nvmlMemory_t return object (VRAM totals, in bytes).

c_nvmlUtilization_t

NVML c_nvmlUtilization_t return object (engine utilization, percent).

c_nvmlProcessInfo_t

NVML c_nvmlProcessInfo_t return object (subset used by consumers).

Functions#

nvmlInit()

Initialize NVML (maps to amdsmi_init for AMD GPUs).

nvmlInitWithFlags(flags)

Initialize NVML; flags is accepted for compatibility and ignored.

nvmlShutdown()

Shut down NVML (refcounted, maps to amdsmi_shut_down).

nvmlDeviceGetCount()

Number of AMD GPUs visible to AMD SMI.

nvmlDeviceGetHandleByIndex(index)

Return the device handle for the given ordinal.

nvmlDeviceGetHandleByUUID(uuid)

Return the device whose UUID matches uuid (str or bytes).

nvmlDeviceGetIndex(handle)

NVML ordinal of the device handle.

nvmlDeviceIsMigDeviceHandle(handle)

ROCm has no MIG; always reports False.

nvmlDeviceGetDeviceHandleFromMigDeviceHandle(handle)

No MIG on ROCm; the handle already refers to a full device.

nvmlDeviceGetMigMode(handle)

MIG mode pair (current, pending).

nvmlDeviceGetMaxMigDeviceCount(handle)

Maximum number of MIG devices; always 0 on ROCm (no MIG).

nvmlDeviceGetMigDeviceHandleByIndex(device, index)

ROCm has no MIG instances; always unsupported.

nvmlDeviceGetMemoryInfo(handle)

VRAM totals as an nvmlMemory_t-like object (bytes).

nvmlDeviceGetName(handle)

Marketing/product name as a str (NVML returns str on py3).

nvmlDeviceGetUUID(handle)

Device UUID rendered as the conventional GPU-<uuid> string.

nvmlDeviceGetTemperature(handle, sensorType)

Current temperature in Celsius for the requested sensor.

nvmlDeviceGetPowerUsage(handle)

Current board power draw in milliwatts.

nvmlDeviceGetUtilizationRates(handle)

GPU/memory engine utilization percentages.

nvmlDeviceGetCpuAffinity(handle, cpuSetSize)

CPU affinity bitmask for the device's NUMA node.

nvmlDeviceGetComputeRunningProcesses(handle)

Compute processes running on the device.

Package Contents#

pynvml.NVML_SUCCESS = 0#
pynvml.NVML_ERROR_UNINITIALIZED = 1#
pynvml.NVML_ERROR_INVALID_ARGUMENT = 2#
pynvml.NVML_ERROR_NOT_SUPPORTED = 3#
pynvml.NVML_ERROR_NOT_FOUND = 6#
pynvml.NVML_ERROR_LIBRARY_NOT_FOUND = 12#
pynvml.NVML_ERROR_FUNCTION_NOT_FOUND = 13#
pynvml.NVML_ERROR_UNKNOWN = 999#
pynvml.NVML_TEMPERATURE_GPU = 0#
pynvml.NVML_DEVICE_MIG_DISABLE = 0#
pynvml.NVML_DEVICE_MIG_ENABLE = 1#
exception pynvml.NVMLError(value=None, msg=None)#

Bases: Exception

Base class mirroring pynvml.NVMLError.

Carries an integer value (an NVML_ERROR_* code) so existing code that inspects err.value continues to work. Concrete per-code subclasses are registered in _NVML_ERROR_SUBCLASSES and surfaced as module attributes (e.g. NVMLError_NotSupported), matching upstream pynvml.

value = 999#
pynvml.NVMLError_Uninitialized#
pynvml.NVMLError_InvalidArgument#
pynvml.NVMLError_NotSupported#
pynvml.NVMLError_NoPermission#
pynvml.NVMLError_NotFound#
pynvml.NVMLError_InsufficientSize#
pynvml.NVMLError_DriverNotLoaded#
pynvml.NVMLError_Timeout#
pynvml.NVMLError_GpuIsLost#
pynvml.NVMLError_LibraryNotFound#
pynvml.NVMLError_FunctionNotFound#
pynvml.NVMLError_Unknown#
class pynvml.c_nvmlMemory_t(total, free, used)#

NVML c_nvmlMemory_t return object (VRAM totals, in bytes).

The class name intentionally matches upstream pynvml (nvidia-ml-py), whose ctypes wrapper for the C nvmlMemory_t struct is likewise named c_nvmlMemory_t – so ported code that inspects the type keeps working. Returned by nvmlDeviceGetMemoryInfo.

total: int#

Total installed VRAM, in bytes.

free: int#

Free VRAM currently available, in bytes.

used: int#

VRAM currently in use, in bytes.

class pynvml.c_nvmlUtilization_t(gpu, memory)#

NVML c_nvmlUtilization_t return object (engine utilization, percent).

The class name intentionally matches upstream pynvml (nvidia-ml-py), whose ctypes wrapper for the C nvmlUtilization_t struct is likewise named c_nvmlUtilization_t – so ported code that inspects the type keeps working. Returned by nvmlDeviceGetUtilizationRates.

gpu: int#

Percent of the last sampling period the GPU/compute engine was busy.

memory: int#

Percent of the last sampling period the memory controller was busy.

class pynvml.c_nvmlProcessInfo_t(pid, usedGpuMemory)#

NVML c_nvmlProcessInfo_t return object (subset used by consumers).

The class name intentionally matches upstream pynvml (nvidia-ml-py), whose ctypes wrapper for the C nvmlProcessInfo_t struct is likewise named c_nvmlProcessInfo_t – so ported code that inspects the type keeps working. Returned by nvmlDeviceGetComputeRunningProcesses.

pid: int#

Operating-system process identifier.

usedGpuMemory: int#

GPU memory used by the process, in bytes. (Upstream NVML may report None under the Windows WDDM driver; not applicable on ROCm.)

pynvml.nvmlInit()#

Initialize NVML (maps to amdsmi_init for AMD GPUs).

Returns:

None.

Raises:
NVMLError:

if AMD SMI initialization or GPU enumeration fails.

pynvml.nvmlInitWithFlags(flags)#

Initialize NVML; flags is accepted for compatibility and ignored.

Returns:

None.

Raises:
NVMLError:

if AMD SMI initialization or GPU enumeration fails.

pynvml.nvmlShutdown()#

Shut down NVML (refcounted, maps to amdsmi_shut_down).

Returns:

None.

Raises:
NVMLError_Uninitialized:

if NVML was not successfully initialized.

pynvml.nvmlDeviceGetCount()#

Number of AMD GPUs visible to AMD SMI.

Returns:
int:

the count of AMD GPU devices.

Raises:
NVMLError_Uninitialized:

if NVML was not successfully initialized.

pynvml.nvmlDeviceGetHandleByIndex(index)#

Return the device handle for the given ordinal.

Returns:

An opaque NVML device handle (pass to the nvmlDeviceGet* queries).

Raises:
NVMLError_Uninitialized:

if NVML was not successfully initialized.

NVMLError_InvalidArgument:

if index is out of range.

pynvml.nvmlDeviceGetHandleByUUID(uuid)#

Return the device whose UUID matches uuid (str or bytes).

Returns:

An opaque NVML device handle for the matching device.

Raises:
NVMLError_Uninitialized:

if NVML was not successfully initialized.

NVMLError_NotFound:

if no device has the requested UUID.

pynvml.nvmlDeviceGetIndex(handle)#

NVML ordinal of the device handle.

Returns:
int:

the zero-based device ordinal.

Raises:
NVMLError_Uninitialized:

if NVML was not successfully initialized.

pynvml.nvmlDeviceIsMigDeviceHandle(handle)#

ROCm has no MIG; always reports False.

Returns:
bool:

always False on ROCm.

Raises:
NVMLError_Uninitialized:

if NVML was not successfully initialized.

pynvml.nvmlDeviceGetDeviceHandleFromMigDeviceHandle(handle)#

No MIG on ROCm; the handle already refers to a full device.

Returns:

The same device handle that was passed in.

Raises:
NVMLError_Uninitialized:

if NVML was not successfully initialized.

pynvml.nvmlDeviceGetMigMode(handle)#

MIG mode pair (current, pending).

ROCm has no MIG, so MIG is always disabled. Returns a 2-tuple to match NVML, whose callers typically read [0] for the current mode.

Returns:
tuple[int, int]:

(current, pending) MIG mode, both always NVML_DEVICE_MIG_DISABLE on ROCm.

Raises:
NVMLError_Uninitialized:

if NVML was not successfully initialized.

pynvml.nvmlDeviceGetMaxMigDeviceCount(handle)#

Maximum number of MIG devices; always 0 on ROCm (no MIG).

Returns:
int:

always 0 on ROCm.

Raises:
NVMLError_Uninitialized:

if NVML was not successfully initialized.

pynvml.nvmlDeviceGetMigDeviceHandleByIndex(device, index)#

ROCm has no MIG instances; always unsupported.

Raises:
NVMLError_Uninitialized:

if NVML was not successfully initialized.

NVMLError_NotSupported:

always, since ROCm has no MIG.

pynvml.nvmlDeviceGetMemoryInfo(handle)#

VRAM totals as an nvmlMemory_t-like object (bytes).

Returns:
c_nvmlMemory_t:

total/free/used VRAM, in bytes.

Raises:
NVMLError_Uninitialized:

if NVML was not successfully initialized.

NVMLError:

if the AMD SMI VRAM-usage query fails.

pynvml.nvmlDeviceGetName(handle)#

Marketing/product name as a str (NVML returns str on py3).

Returns:
str:

the device market name, falling back to the board product name.

Raises:
NVMLError_Uninitialized:

if NVML was not successfully initialized.

NVMLError:

if the AMD SMI ASIC/board queries fail.

pynvml.nvmlDeviceGetUUID(handle)#

Device UUID rendered as the conventional GPU-<uuid> string.

Returns:
str:

the device UUID, prefixed with GPU-.

Raises:
NVMLError_Uninitialized:

if NVML was not successfully initialized.

NVMLError:

if the AMD SMI UUID query fails.

pynvml.nvmlDeviceGetTemperature(handle, sensorType)#

Current temperature in Celsius for the requested sensor.

Returns:
int:

temperature in degrees Celsius (edge sensor, falling back to the hotspot/junction sensor).

Raises:
NVMLError_Uninitialized:

if NVML was not successfully initialized.

NVMLError:

if no supported temperature sensor could be read.

pynvml.nvmlDeviceGetPowerUsage(handle)#

Current board power draw in milliwatts.

Returns:
int:

board power draw in milliwatts.

Raises:
NVMLError_Uninitialized:

if NVML was not successfully initialized.

NVMLError_NotSupported:

if power telemetry is unavailable on this device.

pynvml.nvmlDeviceGetUtilizationRates(handle)#

GPU/memory engine utilization percentages.

Returns:
c_nvmlUtilization_t:

gpu and memory busy percentages (unsupported fields normalized to 0).

Raises:
NVMLError_Uninitialized:

if NVML was not successfully initialized.

NVMLError:

if the AMD SMI activity query fails.

pynvml.nvmlDeviceGetCpuAffinity(handle, cpuSetSize)#

CPU affinity bitmask for the device’s NUMA node.

Mirrors NVML’s nvmlDeviceGetCpuAffinity: returns a list of cpuSetSize 64-bit words whose bits mark the CPUs local to the GPU. Backed by amdsmi_get_cpu_affinity_with_scope at NUMA-node scope.

Like NVML, this is a Linux-only capability; on platforms (e.g. Windows) or builds where AMD SMI cannot provide it, NVMLError_NotSupported is raised so callers can fall back to a default affinity.

Returns:
list[int]:

cpuSetSize 64-bit words forming the CPU affinity bitmask.

Raises:
NVMLError_Uninitialized:

if NVML was not successfully initialized.

NVMLError_InvalidArgument:

if cpuSetSize is not positive.

NVMLError_NotSupported:

on non-Linux platforms or builds where AMD SMI cannot provide CPU affinity.

pynvml.nvmlDeviceGetComputeRunningProcesses(handle)#

Compute processes running on the device.

Drives AMD SMI’s two-call (count -> allocate -> fill) process query: amdsmi_get_gpu_process_list reports the running-process count when max_processes is 0 and list is None; a caller-sized amdsmi_proc_info_t record array (used as a sequence adapter via its indexed get_*(i) accessors) is then filled on the second call. Returns an empty list when nothing is running or the platform does not support the query.

Returns:
list of c_nvmlProcessInfo_t:

one entry per running compute process (pid and usedGpuMemory in bytes); empty if none or unsupported.

Raises:
NVMLError_Uninitialized:

if NVML was not successfully initialized.

NVMLError:

if the AMD SMI process-list query fails for a reason other than being unsupported.