Skip to content

Commit

Permalink
Modify the inline function throwOnFail to a macro THROW_ON_FAIL_FOR_B…
Browse files Browse the repository at this point in the history
…ACKEND
  • Loading branch information
ShaojieZhuIntel committed Oct 10, 2024
1 parent 3c7e35d commit 8557980
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 182 deletions.
46 changes: 14 additions & 32 deletions src/plugins/intel_npu/src/backend/include/zero_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace intel_npu {

namespace zeroUtils {

#define THROW_ON_FAIL(step, result, graph_ddi_table_ext) \
#define THROW_ON_FAIL_FOR_GRAPH_EXT(step, result, graph_ddi_table_ext) \
OPENVINO_THROW("L0 ", \
step, \
" result: ", \
Expand All @@ -31,18 +31,17 @@ namespace zeroUtils {
" . ", \
intel_npu::zeroUtils::getLatestBuildError(graph_ddi_table_ext));

static inline void throwOnFail(const std::string& step, const ze_result_t result) {
if (ZE_RESULT_SUCCESS != result) {
OPENVINO_THROW("L0 ",
step,
" result: ",
ze_result_to_string(result),
", code 0x",
std::hex,
uint64_t(result),
" - ",
ze_result_to_description(result));
}
#define THROW_ON_FAIL_FOR_BACKEND(step, result) \
if (ZE_RESULT_SUCCESS != result) { \
OPENVINO_THROW("L0 ", \
step, \
" result: ", \
ze_result_to_string(result), \
", code 0x", \
std::hex, \
uint64_t(result), \
" - ", \
ze_result_to_description(result)); \
}

static inline void throwOnFail(const std::string& step, const ze_result_t result, const std::string& hintOnError) {
Expand Down Expand Up @@ -230,31 +229,14 @@ static inline uint32_t findGroupOrdinal(
return 0;
}

#define NotSupportLogHandle(T) \
(std::is_same<T, ze_graph_dditable_ext_1_2_t>::value || std::is_same<T, ze_graph_dditable_ext_1_3_t>::value)

static inline std::string getLatestBuildError(ze_graph_dditable_ext_curr_t& _graph_ddi_table_ext) {
Logger _logger("LevelZeroUtils", Logger::global().level());
_logger.debug("getLatestBuildError start");

uint32_t graphDdiExtVersion = _graph_ddi_table_ext.version();
bool ifNotSupportLogHandle = true;
switch (graphDdiExtVersion) {
case ZE_GRAPH_EXT_VERSION_1_3:
ifNotSupportLogHandle = NotSupportLogHandle(ze_graph_dditable_ext_1_3_t);
break;
case ZE_GRAPH_EXT_VERSION_1_4:
ifNotSupportLogHandle = NotSupportLogHandle(ze_graph_dditable_ext_1_4_t);
break;
case ZE_GRAPH_EXT_VERSION_1_5:
ifNotSupportLogHandle = NotSupportLogHandle(ze_graph_dditable_ext_1_5_t);
break;
case ZE_GRAPH_EXT_VERSION_1_6:
ifNotSupportLogHandle = NotSupportLogHandle(ze_graph_dditable_ext_1_6_t);
break;
default:
ifNotSupportLogHandle = NotSupportLogHandle(ze_graph_dditable_ext_1_2_t);
break;
if (graphDdiExtVersion >= ZE_GRAPH_EXT_VERSION_1_4) {
ifNotSupportLogHandle = false;
}

if (ifNotSupportLogHandle) {
Expand Down
30 changes: 17 additions & 13 deletions src/plugins/intel_npu/src/backend/src/zero_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ ZeroDevice::ZeroDevice(const std::shared_ptr<ZeroInitStructsHolder>& initStructs
log("ZeroDevice", Logger::global().level()) {
log.debug("ZeroDevice::ZeroDevice init");
device_properties.stype = ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES;
zeroUtils::throwOnFail("zeDeviceGetProperties",
zeDeviceGetProperties(_initStructs->getDevice(), &device_properties));
THROW_ON_FAIL_FOR_BACKEND("zeDeviceGetProperties",
zeDeviceGetProperties(_initStructs->getDevice(), &device_properties));

// Query PCI information
// Older drivers do not have this implementend. Linux driver returns NOT_IMPLEMENTED, while windows driver returns
Expand Down Expand Up @@ -62,7 +62,7 @@ ZeroDevice::ZeroDevice(const std::shared_ptr<ZeroInitStructsHolder>& initStructs
std::vector<ze_command_queue_group_properties_t> command_group_properties;
uint32_t command_queue_group_count = 0;
// Discover all command queue groups
zeroUtils::throwOnFail(
THROW_ON_FAIL_FOR_BACKEND(
"zeDeviceGetCommandQueueGroupProperties",
zeDeviceGetCommandQueueGroupProperties(_initStructs->getDevice(), &command_queue_group_count, nullptr));

Expand All @@ -74,10 +74,10 @@ ZeroDevice::ZeroDevice(const std::shared_ptr<ZeroInitStructsHolder>& initStructs
prop.pNext = nullptr;
}

zeroUtils::throwOnFail("zeDeviceGetCommandQueueGroupProperties",
zeDeviceGetCommandQueueGroupProperties(_initStructs->getDevice(),
&command_queue_group_count,
command_group_properties.data()));
THROW_ON_FAIL_FOR_BACKEND("zeDeviceGetCommandQueueGroupProperties",
zeDeviceGetCommandQueueGroupProperties(_initStructs->getDevice(),
&command_queue_group_count,
command_group_properties.data()));

// Find the corresponding command queue group.
log.debug("ZeroDevice::ZeroDevice - findGroupOrdinal");
Expand Down Expand Up @@ -138,17 +138,21 @@ uint32_t ZeroDevice::getMaxNumSlices() const {

uint64_t ZeroDevice::getAllocMemSize() const {
ze_graph_memory_query_t query{};
zeroUtils::throwOnFail(
"pfnQueryContextMemory",
_graph_ddi_table_ext.pfnQueryContextMemory(_initStructs->getContext(), ZE_GRAPH_QUERY_MEMORY_DDR, &query));
ze_result_t result =
_graph_ddi_table_ext.pfnQueryContextMemory(_initStructs->getContext(), ZE_GRAPH_QUERY_MEMORY_DDR, &query);
if (ZE_RESULT_SUCCESS != result) {
THROW_ON_FAIL_FOR_GRAPH_EXT("pfnQueryContextMemory", result, _graph_ddi_table_ext);
}
return query.allocated;
}

uint64_t ZeroDevice::getTotalMemSize() const {
ze_graph_memory_query_t query{};
zeroUtils::throwOnFail(
"pfnQueryContextMemory",
_graph_ddi_table_ext.pfnQueryContextMemory(_initStructs->getContext(), ZE_GRAPH_QUERY_MEMORY_DDR, &query));
ze_result_t result =
_graph_ddi_table_ext.pfnQueryContextMemory(_initStructs->getContext(), ZE_GRAPH_QUERY_MEMORY_DDR, &query);
if (ZE_RESULT_SUCCESS != result) {
THROW_ON_FAIL_FOR_GRAPH_EXT("pfnQueryContextMemory", result, _graph_ddi_table_ext);
}
return query.total;
}

Expand Down
18 changes: 13 additions & 5 deletions src/plugins/intel_npu/src/backend/src/zero_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ ZeroExecutor::ZeroExecutor(const std::shared_ptr<const ZeroInitStructsHolder>& i
ze_result_t result =
_graph_ddi_table_ext.pfnCreate(_initStructs->getContext(), _initStructs->getDevice(), &desc, &_graph);
if (ZE_RESULT_SUCCESS != result) {
THROW_ON_FAIL("pfnCreate", result, _graph_ddi_table_ext);
THROW_ON_FAIL_FOR_GRAPH_EXT("pfnCreate", result, _graph_ddi_table_ext);
}

} else {
Expand All @@ -66,7 +66,10 @@ ZeroExecutor::ZeroExecutor(const std::shared_ptr<const ZeroInitStructsHolder>& i
ze_graph_properties_t props{};
props.stype = ZE_STRUCTURE_TYPE_GRAPH_PROPERTIES;

zeroUtils::throwOnFail("pfnGetProperties", _graph_ddi_table_ext.pfnGetProperties(_graph, &props));
ze_result_t result = _graph_ddi_table_ext.pfnGetProperties(_graph, &props);
if (ZE_RESULT_SUCCESS != result) {
THROW_ON_FAIL_FOR_GRAPH_EXT("pfnGetProperties", result, _graph_ddi_table_ext);
}
auto targetDriverExtVersion = _graph_ddi_table_ext.version();
if (targetDriverExtVersion <= ZE_GRAPH_EXT_VERSION_1_1) {
OPENVINO_THROW("Incompatibility between the NPU plugin and driver! The driver version is too old, please "
Expand All @@ -78,8 +81,10 @@ ZeroExecutor::ZeroExecutor(const std::shared_ptr<const ZeroInitStructsHolder>& i
for (uint32_t index = 0; index < props.numGraphArgs; ++index) {
ze_graph_argument_properties_3_t arg3{};
arg3.stype = ZE_STRUCTURE_TYPE_GRAPH_ARGUMENT_PROPERTIES;
zeroUtils::throwOnFail("pfnGetArgumentProperties3",
_graph_ddi_table_ext.pfnGetArgumentProperties3(_graph, index, &arg3));
ze_result_t result = _graph_ddi_table_ext.pfnGetArgumentProperties3(_graph, index, &arg3);
if (ZE_RESULT_SUCCESS != result) {
THROW_ON_FAIL_FOR_GRAPH_EXT("pfnGetArgumentProperties3", result, _graph_ddi_table_ext);
}

if (arg3.type == ZE_GRAPH_ARGUMENT_TYPE_INPUT) {
_input_descriptors.push_back(ArgumentDescriptor{arg3, index});
Expand Down Expand Up @@ -164,7 +169,10 @@ void ZeroExecutor::setWorkloadType(const ov::WorkloadType workloadType) const {
}

void ZeroExecutor::setArgumentValue(uint32_t argi_, const void* argv_) const {
zeroUtils::throwOnFail("zeGraphSetArgumentValue", _graph_ddi_table_ext.pfnSetArgumentValue(_graph, argi_, argv_));
ze_result_t result = _graph_ddi_table_ext.pfnSetArgumentValue(_graph, argi_, argv_);
if (ZE_RESULT_SUCCESS != result) {
THROW_ON_FAIL_FOR_GRAPH_EXT("zeGraphSetArgumentValue", result, _graph_ddi_table_ext);
}
}

void ZeroExecutor::mutexLock() const {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/intel_npu/src/backend/src/zero_infer_request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ ZeroInferRequest::ZeroInferRequest(const std::shared_ptr<ZeroInitStructsHolder>&
}

_properties.stype = ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES;
zeroUtils::throwOnFail("zeDeviceGetProperties",
zeDeviceGetProperties(_executor->getInitStructs()->getDevice(), &_properties));
THROW_ON_FAIL_FOR_BACKEND("zeDeviceGetProperties",
zeDeviceGetProperties(_executor->getInitStructs()->getDevice(), &_properties));

_outputAllocator = std::make_shared<const zeroMemory::HostMemAllocator>(_initStructs);
_inputAllocator =
Expand Down
32 changes: 16 additions & 16 deletions src/plugins/intel_npu/src/backend/src/zero_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ static std::tuple<uint32_t, std::string> queryDriverExtensionVersion(
ZeroInitStructsHolder::ZeroInitStructsHolder() : log("NPUZeroInitStructsHolder", Logger::global().level()) {
OV_ITT_SCOPED_TASK(itt::domains::LevelZeroBackend, "ZeroInitStructsHolder::ZeroInitStructsHolder");
log.debug("ZeroInitStructsHolder - performing zeInit on VPU only");
zeroUtils::throwOnFail("zeInit", zeInit(ZE_INIT_FLAG_VPU_ONLY));
THROW_ON_FAIL_FOR_BACKEND("zeInit", zeInit(ZE_INIT_FLAG_VPU_ONLY));

uint32_t drivers = 0;
zeroUtils::throwOnFail("zeDriverGet", zeDriverGet(&drivers, nullptr));
THROW_ON_FAIL_FOR_BACKEND("zeDriverGet", zeDriverGet(&drivers, nullptr));

std::vector<ze_driver_handle_t> all_drivers(drivers);
zeroUtils::throwOnFail("zeDriverGet", zeDriverGet(&drivers, all_drivers.data()));
THROW_ON_FAIL_FOR_BACKEND("zeDriverGet", zeDriverGet(&drivers, all_drivers.data()));

// Get our target driver
driver_properties.stype = ZE_STRUCTURE_TYPE_DRIVER_PROPERTIES;
Expand All @@ -79,7 +79,7 @@ ZeroInitStructsHolder::ZeroInitStructsHolder() : log("NPUZeroInitStructsHolder",

// Check L0 API version
ze_api_version_t ze_drv_api_version = {};
zeroUtils::throwOnFail("zeDriverGetApiVersion", zeDriverGetApiVersion(driver_handle, &ze_drv_api_version));
THROW_ON_FAIL_FOR_BACKEND("zeDriverGetApiVersion", zeDriverGetApiVersion(driver_handle, &ze_drv_api_version));

if (ZE_MAJOR_VERSION(ZE_API_VERSION_CURRENT) != ZE_MAJOR_VERSION(ze_drv_api_version)) {
OPENVINO_THROW("Incompatibility between NPU plugin and driver! ",
Expand All @@ -97,13 +97,13 @@ ZeroInitStructsHolder::ZeroInitStructsHolder() : log("NPUZeroInitStructsHolder",
}

uint32_t count = 0;
zeroUtils::throwOnFail("zeDriverGetExtensionProperties",
zeDriverGetExtensionProperties(driver_handle, &count, nullptr));
THROW_ON_FAIL_FOR_BACKEND("zeDriverGetExtensionProperties",
zeDriverGetExtensionProperties(driver_handle, &count, nullptr));

std::vector<ze_driver_extension_properties_t> extProps;
extProps.resize(count);
zeroUtils::throwOnFail("zeDriverGetExtensionProperties",
zeDriverGetExtensionProperties(driver_handle, &count, extProps.data()));
THROW_ON_FAIL_FOR_BACKEND("zeDriverGetExtensionProperties",
zeDriverGetExtensionProperties(driver_handle, &count, extProps.data()));

// Query our graph extension version
std::string graph_ext_name;
Expand Down Expand Up @@ -148,7 +148,7 @@ ZeroInitStructsHolder::ZeroInitStructsHolder() : log("NPUZeroInitStructsHolder",
// Load our command queue extension
ze_command_queue_npu_dditable_ext_last_t* _command_queue_npu_dditable_ext = nullptr;
if (command_queue_ext_version) {
zeroUtils::throwOnFail(
THROW_ON_FAIL_FOR_BACKEND(
"zeDriverGetExtensionFunctionAddress " + command_queue_ext_name,
zeDriverGetExtensionFunctionAddress(driver_handle,
command_queue_ext_name.c_str(),
Expand All @@ -161,10 +161,10 @@ ZeroInitStructsHolder::ZeroInitStructsHolder() : log("NPUZeroInitStructsHolder",

// Load our graph extension
ze_graph_dditable_ext_last_t* graph_ddi_table_ext = nullptr;
zeroUtils::throwOnFail("zeDriverGetExtensionFunctionAddress",
zeDriverGetExtensionFunctionAddress(driver_handle,
graph_ext_name.c_str(),
reinterpret_cast<void**>(&graph_ddi_table_ext)));
THROW_ON_FAIL_FOR_BACKEND("zeDriverGetExtensionFunctionAddress",
zeDriverGetExtensionFunctionAddress(driver_handle,
graph_ext_name.c_str(),
reinterpret_cast<void**>(&graph_ddi_table_ext)));
graph_dditable_ext_decorator =
std::make_unique<ze_graph_dditable_ext_decorator>(graph_ddi_table_ext, graph_ext_version);

Expand All @@ -189,7 +189,7 @@ ZeroInitStructsHolder::ZeroInitStructsHolder() : log("NPUZeroInitStructsHolder",

// Load our profiling extension
ze_graph_profiling_dditable_ext_t* _graph_profiling_ddi_table_ext = nullptr;
zeroUtils::throwOnFail(
THROW_ON_FAIL_FOR_BACKEND(
"zeDriverGetExtensionFunctionAddress",
zeDriverGetExtensionFunctionAddress(driver_handle,
"ZE_extension_profiling_data",
Expand All @@ -200,11 +200,11 @@ ZeroInitStructsHolder::ZeroInitStructsHolder() : log("NPUZeroInitStructsHolder",

uint32_t device_count = 1;
// Get our target device
zeroUtils::throwOnFail("zeDeviceGet", zeDeviceGet(driver_handle, &device_count, &device_handle));
THROW_ON_FAIL_FOR_BACKEND("zeDeviceGet", zeDeviceGet(driver_handle, &device_count, &device_handle));

// Create context - share between the compiler and the backend
ze_context_desc_t context_desc = {ZE_STRUCTURE_TYPE_CONTEXT_DESC, 0, 0};
zeroUtils::throwOnFail("zeContextCreate", zeContextCreate(driver_handle, &context_desc, &context));
THROW_ON_FAIL_FOR_BACKEND("zeContextCreate", zeContextCreate(driver_handle, &context_desc, &context));
log.debug("ZeroInitStructsHolder initialize complete");
}

Expand Down
35 changes: 18 additions & 17 deletions src/plugins/intel_npu/src/backend/src/zero_profiling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ ProfilingPool::~ProfilingPool() {
}

void ProfilingQuery::create(const ze_graph_profiling_pool_handle_t& profiling_pool) {
zeroUtils::throwOnFail("pfnProfilingQueryCreate",
_graph_profiling_ddi_table_ext.pfnProfilingQueryCreate(profiling_pool, _index, &_handle));
THROW_ON_FAIL_FOR_BACKEND("pfnProfilingQueryCreate",
_graph_profiling_ddi_table_ext.pfnProfilingQueryCreate(profiling_pool, _index, &_handle));
}

LayerStatistics ProfilingQuery::getLayerStatistics() const {
Expand All @@ -66,7 +66,7 @@ void ProfilingQuery::queryGetData(const ze_graph_profiling_type_t profilingType,
uint32_t* pSize,
uint8_t* pData) const {
if (_handle && pSize) {
zeroUtils::throwOnFail(
THROW_ON_FAIL_FOR_BACKEND(
"pfnProfilingQueryGetData",
_graph_profiling_ddi_table_ext.pfnProfilingQueryGetData(_handle, profilingType, pSize, pData));
}
Expand All @@ -92,7 +92,7 @@ template std::vector<uint8_t> ProfilingQuery::getData<uint8_t>() const;

void ProfilingQuery::getProfilingProperties(ze_device_profiling_data_properties_t* properties) const {
if (_handle && properties) {
zeroUtils::throwOnFail(
THROW_ON_FAIL_FOR_BACKEND(
"getProfilingProperties",
_graph_profiling_ddi_table_ext.pfnDeviceGetProfilingDataProperties(_device_handle, properties));
}
Expand Down Expand Up @@ -187,23 +187,24 @@ NpuInferProfiling::NpuInferProfiling(ze_context_handle_t context,
_logger("InferProfiling", loglevel) {
/// Fetch and store the device timer resolution
_dev_properties.stype = ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES_1_2;
zeroUtils::throwOnFail("zeDeviceGetProperties", zeDeviceGetProperties(_device_handle, &_dev_properties));
THROW_ON_FAIL_FOR_BACKEND("zeDeviceGetProperties", zeDeviceGetProperties(_device_handle, &_dev_properties));
/// Request mem allocations
ze_host_mem_alloc_desc_t desc = {ZE_STRUCTURE_TYPE_HOST_MEM_ALLOC_DESC,
nullptr,
ZE_HOST_MEM_ALLOC_FLAG_BIAS_CACHED};
zeroUtils::throwOnFail("zeMemAllocHost",
zeMemAllocHost(_context,
&desc,
sizeof(uint64_t),
64,
&npu_ts_infer_start)); // align to 64 bytes to match npu l2 cache line size
zeroUtils::throwOnFail("zeMemAllocHost",
zeMemAllocHost(_context,
&desc,
sizeof(uint64_t),
64,
&npu_ts_infer_end)); // alight to 64 bytes to match npu l2 cache line size
THROW_ON_FAIL_FOR_BACKEND(
"zeMemAllocHost",
zeMemAllocHost(_context,
&desc,
sizeof(uint64_t),
64,
&npu_ts_infer_start)); // align to 64 bytes to match npu l2 cache line size
THROW_ON_FAIL_FOR_BACKEND("zeMemAllocHost",
zeMemAllocHost(_context,
&desc,
sizeof(uint64_t),
64,
&npu_ts_infer_end)); // alight to 64 bytes to match npu l2 cache line size
}

void NpuInferProfiling::sampleNpuTimestamps() {
Expand Down
Loading

0 comments on commit 8557980

Please sign in to comment.