CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-amdsmi

Python library for monitoring and managing AMD GPUs and CPUs with programmatic hardware metrics access

Overview
Eval results
Files

library-init.mddocs/reference/

Library Initialization and Lifecycle

Functions for initializing and shutting down the AMDSMI library. The library must be initialized before using any other functions and should be shut down when finished.

Capabilities

Initialize Library

Initialize the AMDSMI library with specified device type flags.

def amdsmi_init(flag: AmdSmiInitFlags = AmdSmiInitFlags.INIT_AMD_GPUS) -> None:
    """
    Initialize the AMDSMI library.

    This function must be called before using any other AMDSMI functions. It initializes
    the library and discovers available devices based on the provided flags.

    Parameters:
    - flag (AmdSmiInitFlags, optional): Initialization flags specifying which device types
      to initialize. Default is INIT_AMD_GPUS.
      Valid values:
        - INIT_ALL_PROCESSORS: Initialize all processor types
        - INIT_AMD_CPUS: Initialize AMD CPUs only
        - INIT_AMD_GPUS: Initialize AMD GPUs only (default)
        - INIT_AMD_APUS: Initialize AMD APUs only
        - INIT_NON_AMD_CPUS: Initialize non-AMD CPUs
        - INIT_NON_AMD_GPUS: Initialize non-AMD GPUs

    Returns:
    - None

    Raises:
    - AmdSmiParameterException: If flag is not an AmdSmiInitFlags value
    - AmdSmiLibraryException: On initialization failure

    Example:
    ```python
    import amdsmi

    # Initialize for GPU monitoring
    amdsmi.amdsmi_init(amdsmi.AmdSmiInitFlags.INIT_AMD_GPUS)

    # Or initialize for both GPUs and CPUs
    amdsmi.amdsmi_init(amdsmi.AmdSmiInitFlags.INIT_ALL_PROCESSORS)
    ```
    """

Shut Down Library

Shut down the AMDSMI library and release all resources.

def amdsmi_shut_down() -> None:
    """
    Shut down the AMDSMI library and release resources.

    This function should be called when finished using the library to properly clean up
    and release resources. After calling this function, amdsmi_init() must be called
    again before using any other AMDSMI functions.

    Returns:
    - None

    Raises:
    - AmdSmiLibraryException: On shutdown failure

    Example:
    ```python
    import amdsmi

    amdsmi.amdsmi_init()
    try:
        # Use AMDSMI functions
        devices = amdsmi.amdsmi_get_processor_handles()
        # ...
    finally:
        # Always shut down
        amdsmi.amdsmi_shut_down()
    ```
    """

Usage Pattern

The recommended usage pattern ensures proper cleanup:

import amdsmi

# Initialize library
amdsmi.amdsmi_init(amdsmi.AmdSmiInitFlags.INIT_AMD_GPUS)

try:
    # Perform monitoring operations
    devices = amdsmi.amdsmi_get_processor_handles()

    for device in devices:
        activity = amdsmi.amdsmi_get_gpu_activity(device)
        print(f"GPU Activity: {activity}")

finally:
    # Always clean up, even if errors occur
    amdsmi.amdsmi_shut_down()

Or using context manager pattern (requires custom implementation):

import amdsmi

# Custom context manager
class AmdSmiContext:
    def __init__(self, flags=amdsmi.AmdSmiInitFlags.INIT_AMD_GPUS):
        self.flags = flags

    def __enter__(self):
        amdsmi.amdsmi_init(self.flags)
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        amdsmi.amdsmi_shut_down()

# Usage
with AmdSmiContext():
    devices = amdsmi.amdsmi_get_processor_handles()
    # ... use devices ...

Initialization Flags

class AmdSmiInitFlags(IntEnum):
    """
    Library initialization flags specifying which device types to initialize.
    """
    INIT_ALL_PROCESSORS = ...  # Initialize all processor types
    INIT_AMD_CPUS = ...         # Initialize AMD CPUs only (requires ESMI)
    INIT_AMD_GPUS = ...         # Initialize AMD GPUs only
    INIT_AMD_APUS = ...         # Initialize AMD APUs only
    INIT_NON_AMD_CPUS = ...     # Initialize non-AMD CPUs
    INIT_NON_AMD_GPUS = ...     # Initialize non-AMD GPUs

Notes

  • The library must be initialized before calling any other AMDSMI functions
  • Only one initialization is needed per process
  • CPU monitoring features require the ESMI library to be installed
  • Initialization discovers all available devices of the specified types
  • Always call amdsmi_shut_down() to properly release resources
  • The library can be re-initialized after shutdown if needed

Install with Tessl CLI

npx tessl i tessl/pypi-amdsmi

docs

index.md

tile.json