Skip to content

Commit

Permalink
move function getLatestBuildError to zero_utils.hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaojieZhuIntel committed Oct 9, 2024
1 parent 5b8892b commit 3c7e35d
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 106 deletions.
77 changes: 77 additions & 0 deletions src/plugins/intel_npu/src/backend/include/zero_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <ze_api.h>
#include <ze_graph_ext.h>

#include <type_traits>

#include "intel_npu/al/config/runtime.hpp"
#include "intel_npu/utils/logger/logger.hpp"
#include "intel_npu/utils/zero/zero_result.hpp"
Expand All @@ -16,6 +18,19 @@ namespace intel_npu {

namespace zeroUtils {

#define THROW_ON_FAIL(step, result, graph_ddi_table_ext) \
OPENVINO_THROW("L0 ", \
step, \
" result: ", \
ze_result_to_string(result), \
", code 0x", \
std::hex, \
uint64_t(result), \
" - ", \
ze_result_to_description(result), \
" . ", \
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 ",
Expand Down Expand Up @@ -215,5 +230,67 @@ 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 (ifNotSupportLogHandle) {
return "";
} else {
// Get log size
uint32_t size = 0;
// Null graph handle to get error log
auto result = _graph_ddi_table_ext.pfnBuildLogGetString(nullptr, &size, nullptr);
if (ZE_RESULT_SUCCESS != result) {
// The failure will not break normal execution, only warning here
_logger.warning("getLatestBuildError Failed to get size of latest error log!");
return "";
}

if (size <= 0) {
// The failure will not break normal execution, only warning here
_logger.warning("getLatestBuildError No error log stored in driver when error "
"detected, may not be compiler issue!");
return "";
}

// Get log content
std::string logContent{};
logContent.resize(size);
result = _graph_ddi_table_ext.pfnBuildLogGetString(nullptr, &size, const_cast<char*>(logContent.data()));
if (ZE_RESULT_SUCCESS != result) {
// The failure will not break normal execution, only warning here
_logger.warning("getLatestBuildError size of latest error log > 0, failed to get "
"content of latest error log!");
return "";
}
_logger.debug("getLatestBuildError end");
return logContent;
}
}

} // namespace zeroUtils
} // namespace intel_npu
9 changes: 5 additions & 4 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,11 @@ 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);
if (ZE_RESULT_SUCCESS != result) {
THROW_ON_FAIL("pfnCreate", result, _graph_ddi_table_ext);
}

} else {
_logger.debug("reuse graph handle created from compiler");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,11 @@
#include "intel_npu/utils/zero/zero_api.hpp"
#include "zero_executor.hpp"

#define PFN_THROW_ON_FAIL(step, result) \
if (ZE_RESULT_SUCCESS != result) { \
OPENVINO_THROW("L0 ", \
step, \
" result: ", \
ze_result_to_string(result), \
", code 0x", \
std::hex, \
uint64_t(result), \
". ", \
getLatestBuildError()); \
}

namespace intel_npu {
namespace driverCompilerAdapter {

using SerializedIR = std::pair<size_t, std::shared_ptr<uint8_t>>;

#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)

#define NotSupportQuery(T) (std::is_same<T, ze_graph_dditable_ext_1_2_t>::value)

// ext version == 1.3 && 1.4, support API (pfnQueryNetworkCreate, pfnQueryNetworkDestroy,
Expand Down Expand Up @@ -199,14 +183,6 @@ class LevelZeroCompilerInDriver final : public ICompiler {
const uint32_t& flags,
ze_graph_handle_t* graph) const;

template <typename T = TableExtension, typename std::enable_if_t<!NotSupportLogHandle(T), bool> = true>
std::string getLatestBuildError() const;

template <typename T = TableExtension, typename std::enable_if_t<NotSupportLogHandle(T), bool> = true>
std::string getLatestBuildError() const {
return "";
}

private:
ze_driver_handle_t _driverHandle = nullptr;
ze_device_handle_t _deviceHandle = nullptr;
Expand Down
122 changes: 44 additions & 78 deletions src/plugins/intel_npu/src/compiler/src/zero_compiler_in_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -667,8 +667,9 @@ ze_result_t LevelZeroCompilerInDriver<TableExtension>::seriazlideIRModelAndQuery

// Create querynetwork handle
ze_result_t result = _graphDdiTableExt.pfnQueryNetworkCreate(_context, _deviceHandle, &desc, &hGraphQueryNetwork);
PFN_THROW_ON_FAIL("seriazlideIRModelAndQueryNetworkCreateV1", result);

if (ZE_RESULT_SUCCESS != result) {
THROW_ON_FAIL("seriazlideIRModelAndQueryNetworkCreateV1", result, _graphDdiTableExt);
}
return result;
}

Expand All @@ -682,8 +683,9 @@ std::unordered_set<std::string> LevelZeroCompilerInDriver<TableExtension>::query

ze_device_graph_properties_t deviceGraphProperties{};
auto result = _graphDdiTableExt.pfnDeviceGetGraphProperties(_deviceHandle, &deviceGraphProperties);
PFN_THROW_ON_FAIL("pfnDeviceGetGraphProperties", result);

if (ZE_RESULT_SUCCESS != result) {
THROW_ON_FAIL("pfnDeviceGetGraphProperties", result, _graphDdiTableExt);
}
ze_graph_query_network_handle_t hGraphQueryNetwork = nullptr;

result = seriazlideIRModelAndQueryNetworkCreateV1(model,
Expand Down Expand Up @@ -723,8 +725,9 @@ ze_result_t LevelZeroCompilerInDriver<TableExtension>::seriazlideIRModelAndQuery
// Create querynetwork handle
_logger.debug("seriazlideIRModelAndQueryNetworkCreateV2 - performing pfnQueryNetworkCreate2");
ze_result_t result = _graphDdiTableExt.pfnQueryNetworkCreate2(_context, _deviceHandle, &desc, &hGraphQueryNetwork);
PFN_THROW_ON_FAIL("seriazlideIRModelAndQueryNetworkCreateV2", result);

if (ZE_RESULT_SUCCESS != result) {
THROW_ON_FAIL("seriazlideIRModelAndQueryNetworkCreateV2", result, _graphDdiTableExt);
}
return result;
}

Expand All @@ -738,8 +741,9 @@ std::unordered_set<std::string> LevelZeroCompilerInDriver<TableExtension>::query

ze_device_graph_properties_t deviceGraphProperties{};
auto result = _graphDdiTableExt.pfnDeviceGetGraphProperties(_deviceHandle, &deviceGraphProperties);
PFN_THROW_ON_FAIL("pfnDeviceGetGraphProperties", result);

if (ZE_RESULT_SUCCESS != result) {
THROW_ON_FAIL("pfnDeviceGetGraphProperties", result, _graphDdiTableExt);
}
ze_graph_query_network_handle_t hGraphQueryNetwork = nullptr;

result = seriazlideIRModelAndQueryNetworkCreateV2(model,
Expand All @@ -761,34 +765,21 @@ std::unordered_set<std::string> LevelZeroCompilerInDriver<TableExtension>::getQu
result = _graphDdiTableExt.pfnQueryNetworkGetSupportedLayers(hGraphQueryNetwork, &size, nullptr);
if (ZE_RESULT_SUCCESS != result) {
_graphDdiTableExt.pfnQueryNetworkDestroy(hGraphQueryNetwork);
OPENVINO_THROW("L0 pfnQueryNetworkGetSupportedLayers get size of query result",
" result: ",
ze_result_to_string(result),
", code 0x",
std::hex,
uint64_t(result),
". ",
getLatestBuildError());
THROW_ON_FAIL("pfnQueryNetworkGetSupportedLayers get size of query result", result, _graphDdiTableExt);
}

// Get the result data of query
std::vector<char> supportedLayers(size);
result = _graphDdiTableExt.pfnQueryNetworkGetSupportedLayers(hGraphQueryNetwork, &size, supportedLayers.data());
if (ZE_RESULT_SUCCESS != result) {
_graphDdiTableExt.pfnQueryNetworkDestroy(hGraphQueryNetwork);
OPENVINO_THROW("L0 pfnQueryNetworkGetSupportedLayers get result data of query",
" result: ",
ze_result_to_string(result),
", code 0x",
std::hex,
uint64_t(result),
". ",
getLatestBuildError());
THROW_ON_FAIL("pfnQueryNetworkGetSupportedLayers get result data of query", result, _graphDdiTableExt);
}

result = _graphDdiTableExt.pfnQueryNetworkDestroy(hGraphQueryNetwork);
PFN_THROW_ON_FAIL("pfnQueryNetworkDestroy", result);

if (ZE_RESULT_SUCCESS != result) {
THROW_ON_FAIL("pfnQueryNetworkDestroy", result, _graphDdiTableExt);
}
return parseQueryResult(supportedLayers);
}

Expand Down Expand Up @@ -832,8 +823,9 @@ ze_result_t LevelZeroCompilerInDriver<TableExtension>::createGraph(const ze_grap
_logger.debug("createGraph - performing pfnCreate");
// Create querynetwork handle
auto result = _graphDdiTableExt.pfnCreate(_context, _deviceHandle, &desc, graph);
PFN_THROW_ON_FAIL("pfnCreate", result);

if (ZE_RESULT_SUCCESS != result) {
THROW_ON_FAIL("pfnCreate", result, _graphDdiTableExt);
}
return result;
}

Expand All @@ -856,8 +848,9 @@ ze_result_t LevelZeroCompilerInDriver<TableExtension>::createGraph(const ze_grap
_logger.debug("createGraph - performing pfnCreate2");
// Create querynetwork handle
auto result = _graphDdiTableExt.pfnCreate2(_context, _deviceHandle, &desc, graph);
PFN_THROW_ON_FAIL("pfnCreate2", result);

if (ZE_RESULT_SUCCESS != result) {
THROW_ON_FAIL("pfnCreate2", result, _graphDdiTableExt);
}
return result;
}
template <typename TableExtension>
Expand Down Expand Up @@ -900,8 +893,9 @@ NetworkDescription LevelZeroCompilerInDriver<TableExtension>::compile(const std:

ze_device_graph_properties_t deviceGraphProperties{};
auto result = _graphDdiTableExt.pfnDeviceGetGraphProperties(_deviceHandle, &deviceGraphProperties);
PFN_THROW_ON_FAIL("pfnDeviceGetGraphProperties, Failed to compile network.", result);

if (ZE_RESULT_SUCCESS != result) {
THROW_ON_FAIL("pfnDeviceGetGraphProperties, Failed to compile network.", result, _graphDdiTableExt);
}
// Graph handle should be used only in scope of compile / parse functions.
ze_graph_handle_t graphHandle;

Expand Down Expand Up @@ -934,7 +928,9 @@ NetworkMetadata LevelZeroCompilerInDriver<TableExtension>::parse(const std::vect

auto result = _graphDdiTableExt.pfnCreate(_context, _deviceHandle, &desc, &graphHandle);
OV_ITT_TASK_NEXT(PARSE_BLOB, "_graphDdiTableExt");
PFN_THROW_ON_FAIL("pfnCreate", result);
if (ZE_RESULT_SUCCESS != result) {
THROW_ON_FAIL("pfnCreate", result, _graphDdiTableExt);
}
} else {
OPENVINO_THROW("Empty blob");
}
Expand All @@ -954,7 +950,9 @@ uint32_t LevelZeroCompilerInDriver<TableExtension>::getSupportedOpsetVersion() c
ze_device_graph_properties_t graphProperties;

auto result = _graphDdiTableExt.pfnDeviceGetGraphProperties(_deviceHandle, &graphProperties);
PFN_THROW_ON_FAIL("pfnDeviceGetGraphProperties", result);
if (ZE_RESULT_SUCCESS != result) {
THROW_ON_FAIL("pfnDeviceGetGraphProperties", result, _graphDdiTableExt);
}
const auto maxOpsetVersion = graphProperties.maxOVOpsetVersionSupported;
_logger.info("getSupportedOpsetVersion Max supported version of opset in CiD: %d", maxOpsetVersion);
_logger.debug("getSupportedOpset end");
Expand Down Expand Up @@ -1025,8 +1023,9 @@ void LevelZeroCompilerInDriver<TableExtension>::getMetadata(ze_graph_dditable_ex
std::vector<IODescriptor>& outputs) const {
ze_graph_argument_properties_3_t arg;
auto result = graphDdiTableExt.pfnGetArgumentProperties3(graphHandle, index, &arg);
PFN_THROW_ON_FAIL("pfnGetArgumentProperties3", result);

if (ZE_RESULT_SUCCESS != result) {
THROW_ON_FAIL("pfnGetArgumentProperties3", result, _graphDdiTableExt);
}
switch (arg.type) {
case ZE_GRAPH_ARGUMENT_TYPE_INPUT: {
inputs.push_back(getIODescriptor(arg, std::nullopt));
Expand All @@ -1049,15 +1048,17 @@ void LevelZeroCompilerInDriver<TableExtension>::getMetadata(ze_graph_dditable_ex
std::vector<IODescriptor>& outputs) const {
ze_graph_argument_properties_3_t arg;
auto result = graphDdiTableExt.pfnGetArgumentProperties3(graphHandle, index, &arg);
PFN_THROW_ON_FAIL("pfnGetArgumentProperties3", result);

if (ZE_RESULT_SUCCESS != result) {
THROW_ON_FAIL("pfnGetArgumentProperties3", result, _graphDdiTableExt);
}
std::optional<ze_graph_argument_metadata_t> optionalMetadata = std::nullopt;

if (!isStateInputName(arg.name) && !isStateOutputName(arg.name) && !isShapeTensorName(arg.name)) {
ze_graph_argument_metadata_t metadata;
result = graphDdiTableExt.pfnGraphGetArgumentMetadata(graphHandle, index, &metadata);
PFN_THROW_ON_FAIL("pfnGraphGetArgumentMetadata", result);

if (ZE_RESULT_SUCCESS != result) {
THROW_ON_FAIL("pfnGraphGetArgumentMetadata", result, _graphDdiTableExt);
}
optionalMetadata = std::optional(metadata);
}

Expand All @@ -1079,8 +1080,9 @@ NetworkMetadata LevelZeroCompilerInDriver<TableExtension>::getNetworkMeta(ze_gra
ze_graph_properties_t graphProperties{};

auto result = _graphDdiTableExt.pfnGetProperties(graphHandle, &graphProperties);
PFN_THROW_ON_FAIL("pfnGetProperties", result);

if (ZE_RESULT_SUCCESS != result) {
THROW_ON_FAIL("pfnGetProperties", result, _graphDdiTableExt);
}
NetworkMetadata meta;

for (uint32_t index = 0; index < graphProperties.numGraphArgs; ++index) {
Expand All @@ -1095,42 +1097,6 @@ NetworkMetadata LevelZeroCompilerInDriver<TableExtension>::getNetworkMeta(ze_gra
return meta;
}

template <typename TableExtension>
template <typename T, typename std::enable_if_t<!NotSupportLogHandle(T), bool>>
std::string LevelZeroCompilerInDriver<TableExtension>::getLatestBuildError() const {
_logger.debug("getLatestBuildError start");

// Get log size
uint32_t size = 0;
// Null graph handle to get erro log
auto result = _graphDdiTableExt.pfnBuildLogGetString(nullptr, &size, nullptr);
if (ZE_RESULT_SUCCESS != result) {
// The failure will not break normal execution, only warning here
_logger.warning("getLatestBuildError Failed to get size of latest error log!");
return "";
}

if (size <= 0) {
// The failure will not break normal execution, only warning here
_logger.warning("getLatestBuildError No error log stored in driver when error "
"detected, may not be compiler issue!");
return "";
}

// Get log content
std::string logContent{};
logContent.resize(size);
result = _graphDdiTableExt.pfnBuildLogGetString(nullptr, &size, const_cast<char*>(logContent.data()));
if (ZE_RESULT_SUCCESS != result) {
// The failure will not break normal execution, only warning here
_logger.warning("getLatestBuildError size of latest error log > 0, failed to get "
"content of latest error log!");
return "";
}
_logger.debug("getLatestBuildError end");
return logContent;
}

template class LevelZeroCompilerInDriver<ze_graph_dditable_ext_1_2_t>;
template class LevelZeroCompilerInDriver<ze_graph_dditable_ext_1_3_t>;
template class LevelZeroCompilerInDriver<ze_graph_dditable_ext_1_4_t>;
Expand Down

0 comments on commit 3c7e35d

Please sign in to comment.