pynvml#
2026-09-24
12 min read time
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/nvmlShutdownnvmlDeviceGetCountnvmlDeviceGetHandleByIndex/nvmlDeviceGetHandleByUUIDnvmlDeviceIsMigDeviceHandle/nvmlDeviceGetDeviceHandleFromMigDeviceHandlenvmlDeviceGetMigMode/nvmlDeviceGetMaxMigDeviceCount/nvmlDeviceGetMigDeviceHandleByIndex(MIG unsupported on ROCm)nvmlDeviceGetMemoryInfo/nvmlDeviceGetName/nvmlDeviceGetUUIDnvmlDeviceGetTemperature/nvmlDeviceGetPowerUsagenvmlDeviceGetUtilizationRatesnvmlDeviceGetCpuAffinity(Linux-only, like NVML)nvmlDeviceGetComputeRunningProcesses(best-effort, see note)NVMLErrorand 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#
Base class mirroring |
Classes#
NVML |
|
NVML |
|
NVML |
Functions#
|
Initialize NVML (maps to |
|
Initialize NVML; |
Shut down NVML (refcounted, maps to |
|
Number of AMD GPUs visible to AMD SMI. |
|
|
Return the device handle for the given ordinal. |
Return the device whose UUID matches |
|
|
NVML ordinal of the device handle. |
|
ROCm has no MIG; always reports |
No MIG on ROCm; the handle already refers to a full device. |
|
|
MIG mode pair |
|
Maximum number of MIG devices; always |
|
ROCm has no MIG instances; always unsupported. |
|
VRAM totals as an |
|
Marketing/product name as a |
|
Device UUID rendered as the conventional |
|
Current temperature in Celsius for the requested sensor. |
|
Current board power draw in milliwatts. |
|
GPU/memory engine utilization percentages. |
|
CPU affinity bitmask for the device's NUMA node. |
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:
ExceptionBase class mirroring
pynvml.NVMLError.Carries an integer
value(anNVML_ERROR_*code) so existing code that inspectserr.valuecontinues to work. Concrete per-code subclasses are registered in_NVML_ERROR_SUBCLASSESand 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_treturn object (VRAM totals, in bytes).The class name intentionally matches upstream
pynvml(nvidia-ml-py), whose ctypes wrapper for the CnvmlMemory_tstruct is likewise namedc_nvmlMemory_t– so ported code that inspects the type keeps working. Returned bynvmlDeviceGetMemoryInfo.
- class pynvml.c_nvmlUtilization_t(gpu, memory)#
NVML
c_nvmlUtilization_treturn object (engine utilization, percent).The class name intentionally matches upstream
pynvml(nvidia-ml-py), whose ctypes wrapper for the CnvmlUtilization_tstruct is likewise namedc_nvmlUtilization_t– so ported code that inspects the type keeps working. Returned bynvmlDeviceGetUtilizationRates.
- class pynvml.c_nvmlProcessInfo_t(pid, usedGpuMemory)#
NVML
c_nvmlProcessInfo_treturn object (subset used by consumers).The class name intentionally matches upstream
pynvml(nvidia-ml-py), whose ctypes wrapper for the CnvmlProcessInfo_tstruct is likewise namedc_nvmlProcessInfo_t– so ported code that inspects the type keeps working. Returned bynvmlDeviceGetComputeRunningProcesses.
- pynvml.nvmlInit()#
Initialize NVML (maps to
amdsmi_initfor AMD GPUs).- Returns:
None.- Raises:
NVMLError:if AMD SMI initialization or GPU enumeration fails.
- pynvml.nvmlInitWithFlags(flags)#
Initialize NVML;
flagsis 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
indexis out of range.
- pynvml.nvmlDeviceGetHandleByUUID(uuid)#
Return the device whose UUID matches
uuid(strorbytes).- 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
Falseon 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 alwaysNVML_DEVICE_MIG_DISABLEon ROCm.
- Raises:
NVMLError_Uninitialized:if NVML was not successfully initialized.
- pynvml.nvmlDeviceGetMaxMigDeviceCount(handle)#
Maximum number of MIG devices; always
0on ROCm (no MIG).- Returns:
int:always
0on 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 returnsstron 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:gpuandmemorybusy percentages (unsupported fields normalized to0).
- 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 ofcpuSetSize64-bit words whose bits mark the CPUs local to the GPU. Backed byamdsmi_get_cpu_affinity_with_scopeat 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_NotSupportedis raised so callers can fall back to a default affinity.- Returns:
list[int]:cpuSetSize64-bit words forming the CPU affinity bitmask.
- Raises:
NVMLError_Uninitialized:if NVML was not successfully initialized.
NVMLError_InvalidArgument:if
cpuSetSizeis 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_listreports the running-process count whenmax_processesis 0 andlistisNone; a caller-sizedamdsmi_proc_info_trecord array (used as a sequence adapter via its indexedget_*(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:
listofc_nvmlProcessInfo_t:one entry per running compute process (
pidandusedGpuMemoryin 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.