diff --git a/amdsmi_cli/amdsmi_commands.py b/amdsmi_cli/amdsmi_commands.py index d376401ac9..a4583e091c 100644 --- a/amdsmi_cli/amdsmi_commands.py +++ b/amdsmi_cli/amdsmi_commands.py @@ -422,11 +422,11 @@ class AMDSMICommands(): power_limit_error = False power_cap_info = amdsmi_interface.amdsmi_get_power_cap_info(args.gpu) max_power_limit = power_cap_info['max_power_cap'] - max_power_limit = AMDSMIHelpers.convert_SI_unit(max_power_limit, AMDSMIHelpers.SI_Unit.MICRO) + max_power_limit = self.helpers.convert_SI_unit(max_power_limit, AMDSMIHelpers.SI_Unit.MICRO) min_power_limit = power_cap_info['min_power_cap'] - min_power_limit = AMDSMIHelpers.convert_SI_unit(min_power_limit, AMDSMIHelpers.SI_Unit.MICRO) + min_power_limit = self.helpers.convert_SI_unit(min_power_limit, AMDSMIHelpers.SI_Unit.MICRO) socket_power_limit = power_cap_info['power_cap'] - socket_power_limit = AMDSMIHelpers.convert_SI_unit(socket_power_limit, AMDSMIHelpers.SI_Unit.MICRO) + socket_power_limit = self.helpers.convert_SI_unit(socket_power_limit, AMDSMIHelpers.SI_Unit.MICRO) except amdsmi_exception.AmdSmiLibraryException as e: power_limit_error = True max_power_limit = "N/A" @@ -3529,11 +3529,11 @@ class AMDSMICommands(): power_cap_info = amdsmi_interface.amdsmi_get_power_cap_info(args.gpu) logging.debug(f"Power cap info for gpu {gpu_id} | {power_cap_info}") min_power_cap = power_cap_info["min_power_cap"] - min_power_cap = AMDSMIHelpers.convert_SI_unit(min_power_cap, AMDSMIHelpers.SI_Unit.MICRO) + min_power_cap = self.helpers.convert_SI_unit(min_power_cap, AMDSMIHelpers.SI_Unit.MICRO) max_power_cap = power_cap_info["max_power_cap"] - max_power_cap = AMDSMIHelpers.convert_SI_unit(max_power_cap, AMDSMIHelpers.SI_Unit.MICRO) + max_power_cap = self.helpers.convert_SI_unit(max_power_cap, AMDSMIHelpers.SI_Unit.MICRO) current_power_cap = power_cap_info["power_cap"] - current_power_cap = AMDSMIHelpers.convert_SI_unit(current_power_cap, AMDSMIHelpers.SI_Unit.MICRO) + current_power_cap = self.helpers.convert_SI_unit(current_power_cap, AMDSMIHelpers.SI_Unit.MICRO) except amdsmi_exception.AmdSmiLibraryException as e: raise ValueError(f"Unable to get power cap info from {gpu_string}") from e @@ -3541,7 +3541,7 @@ class AMDSMICommands(): self.logger.store_output(args.gpu, 'powercap', f"Power cap is already set to {args.power_cap}") elif args.power_cap >= min_power_cap and args.power_cap <= max_power_cap: try: - new_power_cap = AMDSMIHelpers.convert_SI_unit(args.power_cap, AMDSMIHelpers.SI_Unit.BASE, + new_power_cap = self.helpers.convert_SI_unit(args.power_cap, AMDSMIHelpers.SI_Unit.BASE, AMDSMIHelpers.SI_Unit.MICRO) amdsmi_interface.amdsmi_set_power_cap(args.gpu, 0, new_power_cap) except amdsmi_exception.AmdSmiLibraryException as e: @@ -3932,9 +3932,9 @@ class AMDSMICommands(): power_cap_info = amdsmi_interface.amdsmi_get_power_cap_info(args.gpu) logging.debug(f"Power cap info for gpu {gpu_id} | {power_cap_info}") default_power_cap_in_w = power_cap_info["default_power_cap"] - default_power_cap_in_w = AMDSMIHelpers.convert_SI_unit(default_power_cap_in_w, AMDSMIHelpers.SI_Unit.MICRO) + default_power_cap_in_w = self.helpers.convert_SI_unit(default_power_cap_in_w, AMDSMIHelpers.SI_Unit.MICRO) current_power_cap_in_w = power_cap_info["power_cap"] - current_power_cap_in_w = AMDSMIHelpers.convert_SI_unit(current_power_cap_in_w, AMDSMIHelpers.SI_Unit.MICRO) + current_power_cap_in_w = self.helpers.convert_SI_unit(current_power_cap_in_w, AMDSMIHelpers.SI_Unit.MICRO) except amdsmi_exception.AmdSmiLibraryException as e: raise ValueError(f"Unable to get power cap info from {gpu_id}") from e @@ -3942,7 +3942,7 @@ class AMDSMICommands(): self.logger.store_output(args.gpu, 'powercap', f"Power cap is already set to {default_power_cap_in_w}") else: try: - default_power_cap_in_uw = AMDSMIHelpers.convert_SI_unit(default_power_cap_in_w, + default_power_cap_in_uw = self.helpers.convert_SI_unit(default_power_cap_in_w, AMDSMIHelpers.SI_Unit.BASE, AMDSMIHelpers.SI_Unit.MICRO) amdsmi_interface.amdsmi_set_power_cap(args.gpu, 0, default_power_cap_in_uw) diff --git a/amdsmi_cli/amdsmi_helpers.py b/amdsmi_cli/amdsmi_helpers.py index b1e6308724..22e0c6beda 100644 --- a/amdsmi_cli/amdsmi_helpers.py +++ b/amdsmi_cli/amdsmi_helpers.py @@ -28,9 +28,7 @@ import sys import time import re -from subprocess import run -from subprocess import PIPE, STDOUT -from typing import List +from typing import List, Union from enum import Enum from typing import Set @@ -786,35 +784,24 @@ class AMDSMIHelpers(): MICRO = 0.000001 # 10^-6 NANO = 0.000000001 # 10^-9 - def convert_SI_unit(val: float, unit_in: SI_Unit, unit_out = SI_Unit.BASE) -> float: + def convert_SI_unit(self, val: Union[int, float], unit_in: SI_Unit, unit_out = SI_Unit.BASE) -> Union[int, float]: """This function will convert a value into another scientific (SI) unit. Defaults unit_out to SI_Unit.BASE - This function returns a float. params: - val: float unit to convert + val: int or float unit to convert unit_in: Requires using SI_Unit to set current value's SI unit (eg. SI_Unit.MICRO) unit_out - Requires using SI_Unit to set current value's SI unit default value is SI_Unit.BASE (eg. SI_Unit.MICRO) return: - float : converted SI unit of value requested + int or float : converted SI unit of value requested """ - return val * unit_in / unit_out - - def convert_SI_unit(val: int, unit_in: SI_Unit, unit_out=SI_Unit.BASE) -> int: - """This function will convert a value into another - scientific (SI) unit. Defaults unit_out to SI_Unit.BASE - This function returns a int. - - params: - val: int unit to convert - unit_in: Requires using SI_Unit to set current value's SI unit (eg. SI_Unit.MICRO) - unit_out - Requires using SI_Unit to set current value's SI unit - default value is SI_Unit.BASE (eg. SI_Unit.MICRO) - return: - int : converted SI unit of value requested - """ - return int(float(val) * unit_in / unit_out) + if isinstance(val, float): + return val * unit_in / unit_out + elif isinstance(val, int): + return int(float(val) * unit_in / unit_out) + else: + raise TypeError("val must be an int or float") def get_pci_device_ids(self) -> Set[str]: pci_devices_path = "/sys/bus/pci/devices" diff --git a/py-interface/amdsmi_exception.py b/py-interface/amdsmi_exception.py index ae07cdbe44..e77e9a9d88 100644 --- a/py-interface/amdsmi_exception.py +++ b/py-interface/amdsmi_exception.py @@ -107,6 +107,7 @@ class AmdSmiTimeoutException(AmdSmiLibraryException): class AmdSmiParameterException(AmdSmiException): def __init__(self, receivedValue, expectedType, msg=None): super().__init__(msg) + self.err_code = None self.actualType = type(receivedValue) self.expectedType = expectedType self.set_err_msg()