add device utilizationRate
This commit is contained in:
guanlongfeng
2023-06-20 06:38:13 +00:00
committed by 关龙锋
parent 5ee10a085f
commit 00342e1a36
8 changed files with 87 additions and 4 deletions
+18 -2
View File
@@ -14,6 +14,7 @@
import contextlib
import collections
import time
import torch
import torch_npu
@@ -198,6 +199,21 @@ class TorchNPUApiTestCase(TestCase):
def test_npu_get_aclnn_version(self):
res = torch_npu.npu.aclnn.version()
self.assertEqual(res, None)
def test_npu_get_utilization(self):
res = torch.npu.utilization()
self.assertEqual(res, 0)
def test_npu_get_utilization_runing(self):
for i in range(200):
tensora = torch.randn(64, 100, 64, 64, dtype=torch.float, device="npu:0")
torch.sum(tensora)
util_sum = 0
for i in range(200):
res = torch.npu.utilization()
util_sum = util_sum + res
time.sleep(0.2)
self.assertNotEqual(util_sum, 0)
if __name__ == "__main__":
run_tests()
run_tests()
+20
View File
@@ -28,6 +28,8 @@ extern "C" {
#define ACL_CONTINUE_ON_FAILURE 0x00000000u
#define ACL_STOP_ON_FAILURE 0x00000001u
constexpr int32_t DEVICE_UTILIZATION_NOT_SUPPORT = -1;
typedef enum aclrtRunMode {
ACL_DEVICE,
ACL_HOST,
@@ -105,6 +107,13 @@ typedef enum aclrtFloatOverflowMode {
ACL_RT_OVERFLOW_MODE_UNDEF,
} aclrtFloatOverflowMode;
typedef struct aclrtUtilizationInfo {
int32_t cube;
int32_t vector;
int32_t aicpu;
int32_t memory;
} aclrtUtilizationInfo;
typedef struct tagRtGroupInfo aclrtGroupInfo;
typedef struct rtExceptionInfo aclrtExceptionInfo;
@@ -1160,6 +1169,17 @@ ACL_FUNC_VISIBILITY aclError aclrtSetStreamOverflowSwitch(aclrtStream stream, ui
*/
ACL_FUNC_VISIBILITY aclError aclrtGetStreamOverflowSwitch(aclrtStream stream, uint32_t *flag);
/**
* @ingroup AscendCL
* @brief Query the comprehensive usage rate of device
* @param deviceId [IN] the need query's deviceId
* @param utilizationInfo [IN] the need query's device unit switch
* @param utilizationInfo [OUT] the usage rate of device
*
* @retval ACL_SUCCESS The function is successfully executed.
* @retval OtherValues Failure
*/
ACL_FUNC_VISIBILITY aclError aclrtGetDeviceUtilizationRate(int32_t deviceId, aclrtUtilizationInfo *utilizationInfo);
#ifdef __cplusplus
}
#endif
+1
View File
@@ -90,4 +90,5 @@ const char *aclGetTensorDescName(aclTensorDesc *desc) {return NULL;}
aclError aclSetTensorPlaceMent(aclTensorDesc *desc, aclMemType type) {return 0;};
aclError aclrtSetDeviceSatMode(aclrtFloatOverflowMode mode) {return 0;};
aclError aclrtGetDeviceUtilizationRate(int32_t deviceId, aclrtUtilizationInfo *utilizationInfo) {return 0;};
}
@@ -40,6 +40,7 @@ LOAD_FUNCTION(aclrtSetStreamOverflowSwitch)
LOAD_FUNCTION(aclrtGetStreamOverflowSwitch)
LOAD_FUNCTION(aclrtSynchronizeStreamWithTimeout)
LOAD_FUNCTION(aclrtDestroyStreamForce)
LOAD_FUNCTION(aclrtGetDeviceUtilizationRate)
aclprofStepInfoPtr init_stepinfo(){
typedef aclprofStepInfoPtr(*npdInitFunc)();
@@ -343,5 +344,15 @@ aclError AclrtDestroyStreamForce(aclrtStream stream) {
return aclrtDestroyStream(stream);
}
aclError AclrtGetDeviceUtilizationRate(int32_t deviceId, aclrtUtilizationInfo *utilizationInfo) {
typedef aclError (*AclrtGetDeviceUtilizationRate)(int32_t, aclrtUtilizationInfo*);
static AclrtGetDeviceUtilizationRate func = nullptr;
if (func == nullptr) {
func = (AclrtGetDeviceUtilizationRate)GET_FUNC(aclrtGetDeviceUtilizationRate);
}
TORCH_CHECK(func, "Failed to find function ", "aclrtGetDeviceUtilizationRate");
return func(deviceId, utilizationInfo);
}
} // namespace acl
} // namespace c10
@@ -112,5 +112,7 @@ aclError AclrtSynchronizeStreamWithTimeout(aclrtStream stream);
aclError AclrtDestroyStreamForce(aclrtStream stream);
aclError AclrtGetDeviceUtilizationRate(int32_t deviceId, aclrtUtilizationInfo *utilizationInfo);
} // namespace acl
} // namespace c10_npu
+25
View File
@@ -175,6 +175,30 @@ PyObject* THNPModule_getDeviceCount_wrap(PyObject* self, PyObject* noargs) {
END_HANDLE_TH_ERRORS
}
PyObject* THNPModule_getDeviceUtilizationRate_wrap(PyObject* self, PyObject* device_index) {
HANDLE_TH_ERRORS
THPUtils_assert(THPUtils_checkLong(device_index), "invalid argument to getDeviceUtilizationRate");
int32_t device = THPUtils_unpackUInt32(device_index);
aclrtUtilizationInfo util_info;
util_info.cube = 0;
util_info.vector = 0;
NPU_CHECK_ERROR(c10_npu::acl::AclrtGetDeviceUtilizationRate(device, &util_info));
int32_t cube = util_info.cube;
int32_t vector = util_info.vector;
int32_t util_rate = 0;
// 如果vector和cube谁支持,就返回谁的使用率,如果都支持计算(vector*1+cube*1)/2
if (cube == DEVICE_UTILIZATION_NOT_SUPPORT && vector != DEVICE_UTILIZATION_NOT_SUPPORT) {
util_rate = vector;
} else if (cube != DEVICE_UTILIZATION_NOT_SUPPORT && vector == DEVICE_UTILIZATION_NOT_SUPPORT) {
util_rate = cube;
} else if (cube != DEVICE_UTILIZATION_NOT_SUPPORT && vector != DEVICE_UTILIZATION_NOT_SUPPORT) {
util_rate = (cube + vector) / 2;
}
THPUtils_assert(util_rate <=100 && util_rate >= 0, "invalid result to util_rate");
return PyLong_FromLong(util_rate);
END_HANDLE_TH_ERRORS
}
PyObject * THNPModule_getCurrentStream_wrap(
PyObject * /* unused */, PyObject *device_index) {
HANDLE_TH_ERRORS
@@ -740,6 +764,7 @@ static struct PyMethodDef THNPModule_methods[] = {
{"_npu_setDevice", (PyCFunction)THNPModule_setDevice_wrap, METH_O, nullptr},
{"_npu_getDevice", (PyCFunction)THNPModule_getDevice_wrap, METH_NOARGS, nullptr},
{"_npu_getDeviceCount", (PyCFunction)THNPModule_getDeviceCount_wrap, METH_NOARGS, nullptr},
{"_npu_getDeviceUtilizationRate", (PyCFunction)THNPModule_getDeviceUtilizationRate_wrap, METH_O, nullptr},
{"_npu_getCurrentStream", (PyCFunction)THNPModule_getCurrentStream_wrap, METH_O, nullptr},
{"_npu_getDefaultStream", (PyCFunction)THNPModule_getDefaultStream_wrap, METH_O, nullptr},
{"_npu_setStream", (PyCFunction)THNPModule_setStream_wrap, METH_O, nullptr},
+2 -2
View File
@@ -18,7 +18,7 @@ __all__ = [
"native_device", "npu_device", "is_initialized", "_lazy_call", "_lazy_init", "init", "set_dump",
"synchronize", "device_count", "set_device", "current_device", "get_device_name",
"get_device_properties", "get_device_capability", "_get_device_index", "is_available", "device", "device_of",
"stream", "current_stream", "default_stream", "init_dump",
"stream", "current_stream", "default_stream", "init_dump", "utilization",
"finalize_dump", "set_dump", "manual_seed", "manual_seed_all",
"seed", "seed_all", "initial_seed", "_free_mutex", "caching_allocator_alloc",
"caching_allocator_delete", "set_per_process_memory_fraction", "empty_cache", "memory_stats",
@@ -44,7 +44,7 @@ from .device import __npu_device__ as npu_device
from .utils import (is_initialized, _lazy_call, _lazy_init, init, set_dump,
synchronize, device_count, set_device, current_device, get_device_name,
get_device_properties, get_device_capability, _get_device_index, is_available, device, device_of,
stream, current_stream, default_stream, init_dump,
stream, current_stream, default_stream, init_dump, utilization,
finalize_dump, set_dump, _in_bad_fork, get_npu_overflow_flag,
clear_npu_overflow_flag)
from .random import (manual_seed, manual_seed_all, seed, seed_all, initial_seed,
+8
View File
@@ -182,6 +182,14 @@ def get_device_capability(device=None):
warnings.warn("torch_npu.npu.get_device_capability isn't implemented!")
return None
def utilization(device=None):
r"""Query the comprehensive utilization rate of device
"""
device_id = _get_device_index(device, optional=True)
if device_id < 0 or device_id >= device_count():
raise AssertionError("Invalid device id")
torch_npu.npu._lazy_init()
return torch_npu._C._npu_getDeviceUtilizationRate(device_id)
def _get_device_index(device, optional=False):
r"""Gets the device index from :attr:`device`, which can be a torch.device