Skip to content

Commit

Permalink
[NPU] Add pfn build log to pfn-related function (openvinotoolkit#26772)
Browse files Browse the repository at this point in the history
There are many OPENVINO_THROW, OPENVINO_ASSERT inside
https://github.com/openvinotoolkit/openvino/blob/master/src/plugins/intel_npu/src/compiler/src/zero_compiler_in_driver.cpp,
call getLatestBuildError to show error info when pfn-related function is
called and remove exception handle fuction that would never be called.
Use a macro THROW_ON_FAIL_FOR_LEVELZERO_EXT to simplify exception
handling code.
There are two functions zeroUtils::throwOnFail() in zero_utils.hpp, but
only the two-parameters function zeroUtils::throwOnFail() is actually
used. And zeroUtils::throwOnFail() will use its code location instead of
the real location where issue happen, so use a macro
THROW_ON_FAIL_FOR_LEVELZERO instead of the function
zeroUtils::throwOnFail() with two parameters.
Function zeroUtils::throwOnFail() with three parameters is preserved.

 - [EISW-139871](https://jira.devtools.intel.com/browse/EISW-139871)
  • Loading branch information
ShaojieZhuIntel authored and DanLiu2Intel committed Nov 5, 2024
1 parent 2ceba1e commit f74635f
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 349 deletions.
28 changes: 15 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_LEVELZERO("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_LEVELZERO(
"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_LEVELZERO("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,19 @@ 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);
THROW_ON_FAIL_FOR_LEVELZERO_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);
THROW_ON_FAIL_FOR_LEVELZERO_EXT("pfnQueryContextMemory", result, _graph_ddi_table_ext);

return query.total;
}

Expand Down
20 changes: 12 additions & 8 deletions src/plugins/intel_npu/src/backend/src/zero_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ ZeroExecutor::ZeroExecutor(const std::shared_ptr<const ZeroInitStructsHolder>& i
_networkDesc->compiledNetwork.size(),
_networkDesc->compiledNetwork.data(),
nullptr};

zeroUtils::throwOnFail(
"pfnCreate",
_graph_ddi_table_ext.pfnCreate(_initStructs->getContext(), _initStructs->getDevice(), &desc, &_graph));
ze_result_t result =
_graph_ddi_table_ext.pfnCreate(_initStructs->getContext(), _initStructs->getDevice(), &desc, &_graph);
THROW_ON_FAIL_FOR_LEVELZERO_EXT("pfnCreate", result, _graph_ddi_table_ext);

} else {
_logger.debug("reuse graph handle created from compiler");
Expand All @@ -65,7 +64,9 @@ 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);
THROW_ON_FAIL_FOR_LEVELZERO_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 @@ -77,8 +78,8 @@ 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);
THROW_ON_FAIL_FOR_LEVELZERO_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 @@ -163,7 +164,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_LEVELZERO_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_LEVELZERO("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_LEVELZERO("zeInit", zeInit(ZE_INIT_FLAG_VPU_ONLY));

uint32_t drivers = 0;
zeroUtils::throwOnFail("zeDriverGet", zeDriverGet(&drivers, nullptr));
THROW_ON_FAIL_FOR_LEVELZERO("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_LEVELZERO("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_LEVELZERO("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_LEVELZERO("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_LEVELZERO("zeDriverGetExtensionProperties",
zeDriverGetExtensionProperties(driver_handle, &count, extProps.data()));

// Query our graph extension version
std::string graph_ext_name;
Expand Down Expand Up @@ -149,7 +149,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_LEVELZERO(
"zeDriverGetExtensionFunctionAddress " + command_queue_ext_name,
zeDriverGetExtensionFunctionAddress(driver_handle,
command_queue_ext_name.c_str(),
Expand All @@ -162,10 +162,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_LEVELZERO("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 @@ -190,7 +190,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_LEVELZERO(
"zeDriverGetExtensionFunctionAddress",
zeDriverGetExtensionFunctionAddress(driver_handle,
"ZE_extension_profiling_data",
Expand All @@ -201,11 +201,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_LEVELZERO("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_LEVELZERO("zeContextCreate", zeContextCreate(driver_handle, &context_desc, &context));
log.debug("ZeroInitStructsHolder initialize complete");
}

Expand Down
37 changes: 20 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,9 @@ 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_LEVELZERO(
"pfnProfilingQueryCreate",
_graph_profiling_ddi_table_ext.pfnProfilingQueryCreate(profiling_pool, _index, &_handle));
}

LayerStatistics ProfilingQuery::getLayerStatistics() const {
Expand All @@ -66,7 +67,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_LEVELZERO(
"pfnProfilingQueryGetData",
_graph_profiling_ddi_table_ext.pfnProfilingQueryGetData(_handle, profilingType, pSize, pData));
}
Expand All @@ -92,7 +93,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_LEVELZERO(
"getProfilingProperties",
_graph_profiling_ddi_table_ext.pfnDeviceGetProfilingDataProperties(_device_handle, properties));
}
Expand Down Expand Up @@ -187,23 +188,25 @@ 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_LEVELZERO("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_LEVELZERO(
"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_LEVELZERO(
"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
27 changes: 15 additions & 12 deletions src/plugins/intel_npu/src/backend/src/zero_remote_tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ ZeroRemoteTensor::ZeroRemoteTensor(std::shared_ptr<ov::IRemoteContext> context,
_mem_type(mem_type),
_mem(mem) {
_ze_properties.stype = ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES;
zeroUtils::throwOnFail("zeDeviceGetProperties", zeDeviceGetProperties(_init_structs->getDevice(), &_ze_properties));
THROW_ON_FAIL_FOR_LEVELZERO("zeDeviceGetProperties",
zeDeviceGetProperties(_init_structs->getDevice(), &_ze_properties));

const auto byte_size = ov::element::get_memory_size(_element_type, shape_size(_shape));

Expand Down Expand Up @@ -97,8 +98,9 @@ void ZeroRemoteTensor::allocate(const size_t bytes) {
} else {
desc = {ZE_STRUCTURE_TYPE_HOST_MEM_ALLOC_DESC, nullptr, 0};
}
zeroUtils::throwOnFail("zeMemAllocHost",
zeMemAllocHost(_init_structs->getContext(), &desc, size, STANDARD_PAGE_SIZE, &_data));
THROW_ON_FAIL_FOR_LEVELZERO(
"zeMemAllocHost",
zeMemAllocHost(_init_structs->getContext(), &desc, size, STANDARD_PAGE_SIZE, &_data));
break;
}
case MemType::SHARED_BUF: {
Expand All @@ -116,13 +118,13 @@ void ZeroRemoteTensor::allocate(const size_t bytes) {
_mem,
nullptr};
ze_device_mem_alloc_desc_t desc = {ZE_STRUCTURE_TYPE_DEVICE_MEM_ALLOC_DESC, &memory_import, 0, 0};
zeroUtils::throwOnFail("zeMemAllocDevice",
zeMemAllocDevice(_init_structs->getContext(),
&desc,
bytes,
STANDARD_PAGE_SIZE,
_init_structs->getDevice(),
&_data));
THROW_ON_FAIL_FOR_LEVELZERO("zeMemAllocDevice",
zeMemAllocDevice(_init_structs->getContext(),
&desc,
bytes,
STANDARD_PAGE_SIZE,
_init_structs->getDevice(),
&_data));
#else
// in the case of Linux platforms memory could be changed after allocation - using zeMemAllocHost for importing
// memory
Expand All @@ -131,8 +133,9 @@ void ZeroRemoteTensor::allocate(const size_t bytes) {
ZE_EXTERNAL_MEMORY_TYPE_FLAG_DMA_BUF,
static_cast<int>(reinterpret_cast<intptr_t>(_mem))};
ze_host_mem_alloc_desc_t desc = {.pNext = &memory_import};
zeroUtils::throwOnFail("zeMemAllocHost",
zeMemAllocHost(_init_structs->getContext(), &desc, bytes, STANDARD_PAGE_SIZE, &_data));
THROW_ON_FAIL_FOR_LEVELZERO(
"zeMemAllocHost",
zeMemAllocHost(_init_structs->getContext(), &desc, bytes, STANDARD_PAGE_SIZE, &_data));
#endif
break;
}
Expand Down
Loading

0 comments on commit f74635f

Please sign in to comment.