Files
TheRock/rocm-systems/projects/amdsmi/docs/how-to/amdsmi-py-lib.md
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

5.0 KiB

myst
myst
html_meta
description lang=en keywords
Get started with the AMD SMI Python interface. api, smi, lib, py, system, management, interface, ROCm

AMD SMI Python interface overview

The AMD SMI Python interface provides a convenient way to interact with AMD hardware through a simple and accessible API.

Refer to the [Python library API reference](../reference/amdsmi-py-api.md).

Prerequisites

Before get started, make sure your environment satisfies the following prerequisites. See the requirements section for more information.

  1. Ensure amdgpu drivers are installed properly for initialization.

  2. Export LD_LIBRARY_PATH to the amdsmi installation directory.

    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/rocm/lib:/opt/rocm/lib64:
    
  3. Install Python 3.6.8+.

Get started

``hipcc`` and other compilers will not automatically link in the ``libamd_smi``
dynamic library. To compile code that uses the AMD SMI library API, ensure the
``libamd_smi.so`` can be located by setting the ``LD_LIBRARY_PATH`` environment
variable to the directory containing ``librocm_smi64.so`` (usually
``/opt/rocm/lib``) or by passing the ``-lamd_smi`` flag to the compiler.
The following environment variables can be set to control internal cache
durations. They must be set **before** the AMDSMI library loads.

| Variable | Description | Default |
|---|---|---|
| ``AMDSMI_GPU_METRICS_CACHE_MS`` | GPU metrics cache duration (ms) | 1 ms (set to 0 to disable) |
| ``AMDSMI_ASIC_INFO_CACHE_MS`` | GPU ASIC info cache duration (ms) | 10000 ms (set to 0 to disable) |

You can set these in one of two ways:

  1. In Python (before the AMDSMI library loads):

    import os
    os.environ["AMDSMI_GPU_METRICS_CACHE_MS"] = "200"
    from amdsmi import *
    
  2. From the shell (when invoking Python):

    AMDSMI_GPU_METRICS_CACHE_MS=200 python tools/amdsmi_quick_start.py
    

To get started, the amdsmi folder should be copied and placed next to the importing script. Import it as follows:

from amdsmi import *

try:
    amdsmi_init()

    # amdsmi calls ...

except AmdSmiException as e:
    print(e)
finally:
    try:
        amdsmi_shut_down()
    except AmdSmiException as e:
        print(e)

(py_lib_fs)=

Folder structure

File name Description
__init__.py Python package initialization file
amdsmi_interface.py Amdsmi library Python interface
amdsmi_wrapper.py Python wrapper around amdsmi binary
amdsmi_exception.py Amdsmi exceptions Python file

(py_usage)=

Usage

An application using AMD SMI must call amdsmi_init() to initialize the AMI SMI library before all other calls. This call initializes the internal data structures required for subsequent AMD SMI operations. In the call, a flag can be passed to indicate if the application is interested in a specific device type.

amdsmi_shut_down() must be the last call to properly close connection to driver and make sure that any resources held by AMD SMI are released.

Refer to the [Python library API reference](../reference/amdsmi-py-api.md).

(py_exceptions)=

Exceptions

All exceptions are in amdsmi_exception.py file.

Exceptions that can be thrown by AMD SMI are:

  • AmdSmiException: base amdsmi exception class

  • AmdSmiLibraryException: derives base AmdSmiException class and represents errors that can occur in amdsmi-lib. When this exception is thrown, err_code and err_info are set. err_code is an integer that corresponds to errors that can occur in amdsmi-lib and err_info is a string that explains the error that occurred.

    try:
        num_of_GPUs = len(amdsmi_get_processor_handles())
    except amdsmi_exception.AmdSmiLibraryException as e:
        print("Unable to get processor handles, error: {} {}".format(str(e.get_error_code()), e.err_info))
    
  • AmdSmiParameterException: Derives base AmdSmiException class and represents errors related to invalid parameters passed to functions. When this exception is thrown, err_msg is set and it explains what is the actual and expected type of the parameters.

    For example:

    try:
        cpu_handles = amdsmi_get_cpu_handles()
        cpu_count = cpu_handles["cpu_count"]
        processor_handles = cpu_handles["processor_handles"]
        if cpu_count == 0:
            print("No CPU sockets on machine")
        else:
            for processor in processor_handles:
                temperature = amdsmi_get_cpu_socket_temperature(processor)
                print(temperature)
    except amdsmi_exception.AmdSmiParameterException as e:
        print("Invalid parameter error: {} {}".format(str(e.get_error_code()), e.err_msg))
    except amdsmi_exception.AmdSmiLibraryException as e:
        print("Unable to get processor handles, error: {} {}".format(str(e.get_error_code()), e.err_info))