Skip to content

Commit

Permalink
It works! QNN EP is a shared library and all QNN unit tests pass on W…
Browse files Browse the repository at this point in the history
…indows ARM64. [disabled ETW code for now]
  • Loading branch information
adrianlizarraga committed Dec 16, 2024
1 parent ae2dbd2 commit db5f0ec
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 36 deletions.
1 change: 0 additions & 1 deletion cmake/onnxruntime.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ set(onnxruntime_INTERNAL_LIBRARIES
${PROVIDERS_COREML}
${PROVIDERS_DML}
${PROVIDERS_NNAPI}
${PROVIDERS_QNN}
${PROVIDERS_SNPE}
${PROVIDERS_RKNPU}
${PROVIDERS_VSINPU}
Expand Down
3 changes: 0 additions & 3 deletions cmake/onnxruntime_providers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ endif()
if(onnxruntime_USE_JSEP)
set(PROVIDERS_JS onnxruntime_providers_js)
endif()
if(onnxruntime_USE_QNN)
set(PROVIDERS_QNN onnxruntime_providers_qnn)
endif()
if(onnxruntime_USE_RKNPU)
set(PROVIDERS_RKNPU onnxruntime_providers_rknpu)
endif()
Expand Down
59 changes: 39 additions & 20 deletions cmake/onnxruntime_providers_qnn.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,51 @@
add_compile_definitions(USE_QNN=1)

file(GLOB_RECURSE
onnxruntime_providers_qnn_ep_cc_srcs CONFIGURE_DEPENDS
onnxruntime_providers_qnn_cc_srcs CONFIGURE_DEPENDS
"${ONNXRUNTIME_ROOT}/core/providers/qnn/*.h"
"${ONNXRUNTIME_ROOT}/core/providers/qnn/*.cc"
)

file(GLOB_RECURSE
onnxruntime_providers_qnn_builder_cc_srcs CONFIGURE_DEPENDS
"${ONNXRUNTIME_ROOT}/core/providers/qnn/builder/*.h"
"${ONNXRUNTIME_ROOT}/core/providers/qnn/builder/*.cc"
)

set(onnxruntime_providers_qnn_cc_srcs
${onnxruntime_providers_qnn_ep_cc_srcs}
${onnxruntime_providers_qnn_builder_cc_srcs}
"${ONNXRUNTIME_ROOT}/core/providers/qnn/builder/qnn_node_group/*.h"
"${ONNXRUNTIME_ROOT}/core/providers/qnn/builder/qnn_node_group/*.cc"
"${ONNXRUNTIME_ROOT}/core/providers/qnn/builder/opbuilder/*.h"
"${ONNXRUNTIME_ROOT}/core/providers/qnn/builder/opbuilder/*.cc"
"${ONNXRUNTIME_ROOT}/core/providers/shared_library/*.h"
"${ONNXRUNTIME_ROOT}/core/providers/shared_library/*.cc"
)

source_group(TREE ${ONNXRUNTIME_ROOT}/core FILES ${onnxruntime_providers_qnn_cc_srcs})
onnxruntime_add_static_library(onnxruntime_providers_qnn ${onnxruntime_providers_qnn_cc_srcs})
onnxruntime_add_include_to_target(onnxruntime_providers_qnn onnxruntime_common onnxruntime_framework onnx onnx_proto protobuf::libprotobuf-lite flatbuffers::flatbuffers Boost::mp11)
target_link_libraries(onnxruntime_providers_qnn)
add_dependencies(onnxruntime_providers_qnn onnx ${onnxruntime_EXTERNAL_DEPENDENCIES})
set_target_properties(onnxruntime_providers_qnn PROPERTIES CXX_STANDARD_REQUIRED ON)
set_target_properties(onnxruntime_providers_qnn PROPERTIES FOLDER "ONNXRuntime")
target_include_directories(onnxruntime_providers_qnn PRIVATE ${ONNXRUNTIME_ROOT} ${onnxruntime_QNN_HOME}/include/QNN ${onnxruntime_QNN_HOME}/include)
set_target_properties(onnxruntime_providers_qnn PROPERTIES LINKER_LANGUAGE CXX)
# ignore the warning unknown-pragmas on "pragma region"
if(NOT MSVC)
onnxruntime_add_shared_library_module(onnxruntime_providers_qnn ${onnxruntime_providers_qnn_cc_srcs})
onnxruntime_add_include_to_target(onnxruntime_providers_qnn ${ONNXRUNTIME_PROVIDERS_SHARED} ${GSL_TARGET} onnx onnxruntime_common safeint_interface)
target_link_libraries(onnxruntime_providers_qnn PRIVATE ${ONNXRUNTIME_PROVIDERS_SHARED} ${ABSEIL_LIBS})
add_dependencies(onnxruntime_providers_qnn ${onnxruntime_EXTERNAL_DEPENDENCIES})
target_include_directories(onnxruntime_providers_qnn PRIVATE ${ONNXRUNTIME_ROOT}
${CMAKE_CURRENT_BINARY_DIR}
${onnxruntime_QNN_HOME}/include/QNN
${onnxruntime_QNN_HOME}/include)

# Set linker flags for function(s) exported by EP DLL
if(UNIX)
set_property(TARGET onnxruntime_providers_qnn APPEND_STRING PROPERTY LINK_FLAGS "-Xlinker --version-script=${ONNXRUNTIME_ROOT}/core/providers/qnn/version_script.lds -Xlinker --gc-sections")
elseif(WIN32)
set_property(TARGET onnxruntime_providers_qnn APPEND_STRING PROPERTY LINK_FLAGS "-DEF:${ONNXRUNTIME_ROOT}/core/providers/qnn/symbols.def")
else()
message(FATAL_ERROR "onnxruntime_providers_qnn unknown platform, need to specify shared library exports for it")
endif()

# Set compile options
if(MSVC)
target_compile_options(onnxruntime_providers_qnn PUBLIC /wd4099 /wd4005)
else()
# ignore the warning unknown-pragmas on "pragma region"
target_compile_options(onnxruntime_providers_qnn PRIVATE "-Wno-unknown-pragmas")
endif()

set_target_properties(onnxruntime_providers_qnn PROPERTIES LINKER_LANGUAGE CXX)
set_target_properties(onnxruntime_providers_qnn PROPERTIES CXX_STANDARD_REQUIRED ON)
set_target_properties(onnxruntime_providers_qnn PROPERTIES FOLDER "ONNXRuntime")

install(TARGETS onnxruntime_providers_qnn
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
8 changes: 7 additions & 1 deletion cmake/onnxruntime_python.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ target_link_libraries(onnxruntime_pybind11_state PRIVATE
${PROVIDERS_XNNPACK}
${PROVIDERS_WEBGPU}
${PROVIDERS_AZURE}
${PROVIDERS_QNN}
onnxruntime_optimizer
onnxruntime_providers
onnxruntime_util
Expand Down Expand Up @@ -997,6 +996,13 @@ if (onnxruntime_USE_COREML)
endif()

if (onnxruntime_USE_QNN)
add_custom_command(
TARGET onnxruntime_pybind11_state POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
$<TARGET_FILE:onnxruntime_providers_qnn>
$<TARGET_FILE:onnxruntime_providers_shared>
$<TARGET_FILE_DIR:${build_output_target}>/onnxruntime/capi/
)
add_custom_command(
TARGET onnxruntime_pybind11_state POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
Expand Down
6 changes: 2 additions & 4 deletions cmake/onnxruntime_unittests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -628,12 +628,11 @@ set(ONNXRUNTIME_TEST_LIBS
onnxruntime_session
${ONNXRUNTIME_INTEROP_TEST_LIBS}
${onnxruntime_libs}
# CUDA, ROCM, TENSORRT, MIGRAPHX, DNNL, and OpenVINO are dynamically loaded at runtime
# CUDA, ROCM, TENSORRT, MIGRAPHX, DNNL, OpenVINO, and QNN are dynamically loaded at runtime
${PROVIDERS_NNAPI}
${PROVIDERS_VSINPU}
${PROVIDERS_JS}
${PROVIDERS_WEBGPU}
${PROVIDERS_QNN}
${PROVIDERS_SNPE}
${PROVIDERS_RKNPU}
${PROVIDERS_DML}
Expand Down Expand Up @@ -704,8 +703,7 @@ endif()
if(onnxruntime_USE_QNN AND NOT onnxruntime_MINIMAL_BUILD AND NOT onnxruntime_REDUCED_OPS_BUILD)
list(APPEND onnxruntime_test_framework_src_patterns ${TEST_SRC_DIR}/providers/qnn/*)
list(APPEND onnxruntime_test_framework_libs onnxruntime_providers_qnn)
list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_qnn)
list(APPEND onnxruntime_test_providers_libs onnxruntime_providers_qnn)
list(APPEND onnxruntime_test_providers_dependencies onnxruntime_providers_qnn onnxruntime_providers_shared)
endif()

if(onnxruntime_USE_SNPE)
Expand Down
10 changes: 6 additions & 4 deletions onnxruntime/core/providers/qnn/builder/qnn_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -902,21 +902,23 @@ static bool GetClipMinMaxImpl(const GraphViewer& graph_viewer, const Node& node,

switch (input_type) {
case ONNX_NAMESPACE::TensorProto_DataType_FLOAT: {
auto status = onnxruntime::utils::UnpackTensor(*initializer, graph_viewer.ModelPath(), &value, 1);
std::vector<uint8_t> bytes(sizeof(float));
auto status = onnxruntime::utils::UnpackInitializerData(*initializer, graph_viewer.ModelPath(), bytes);
if (!status.IsOK()) {
LOGS(logger, ERROR) << "GetClipMinMax() failed to unpack float initializer: " << status.ErrorMessage();
return false;
}
value = *reinterpret_cast<float*>(bytes.data());
break;
}
case ONNX_NAMESPACE::TensorProto_DataType_FLOAT16: {
MLFloat16 f16_val{};
auto status = onnxruntime::utils::UnpackTensor(*initializer, graph_viewer.ModelPath(), &f16_val, 1);
std::vector<uint8_t> bytes(sizeof(MLFloat16));
auto status = onnxruntime::utils::UnpackInitializerData(*initializer, graph_viewer.ModelPath(), bytes);
if (!status.IsOK()) {
LOGS(logger, ERROR) << "GetClipMinMax() failed to unpack float16 initializer: " << status.ErrorMessage();
return false;
}
value = f16_val.ToFloat();
value = reinterpret_cast<MLFloat16*>(bytes.data())->ToFloat();
break;
}
default:
Expand Down
6 changes: 5 additions & 1 deletion onnxruntime/core/providers/qnn/ort_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define BUILD_QNN_EP_STATIC 0

#if BUILD_QNN_EP_STATIC
// Includes when building QNN EP statically
#include "onnx/defs/data_type_utils.h"
#include "core/common/common.h"
#include "core/common/status.h"
Expand All @@ -32,10 +33,13 @@
#include "core/optimizer/qdq_transformer/selectors_actions/shared/utils.h"
#include "core/providers/common.h"
#include "core/providers/partitioning_utils.h"
#include "core/session/onnxruntime_cxx_api.h"
#else
// Includes when building QNN EP as a shared library
#include "core/providers/shared_library/provider_api.h"
#define ORT_API_MANUAL_INIT
#include "core/session/onnxruntime_cxx_api.h"
#endif

#include "core/session/onnxruntime_session_options_config_keys.h"
#include "core/session/onnxruntime_run_options_config_keys.h"
#include "core/session/onnxruntime_cxx_api.h"
3 changes: 2 additions & 1 deletion onnxruntime/core/providers/qnn/qnn_execution_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ QNNExecutionProvider::QNNExecutionProvider(const ProviderOptions& provider_optio
const ConfigOptions* config_options)
: IExecutionProvider{onnxruntime::kQnnExecutionProvider} {
InitProviderOrtApi();
metadef_id_generator_ = ModelMetadefIdGenerator::Create();

if (config_options) {
disable_cpu_ep_fallback_ = config_options->GetConfigOrDefault(
Expand Down Expand Up @@ -654,7 +655,7 @@ QNNExecutionProvider::GetCapability(const onnxruntime::GraphViewer& graph_viewer

const auto gen_metadef_name = [&]() {
uint64_t model_hash;
int metadef_id = metadef_id_generator_.GenerateId(graph_viewer, model_hash);
int metadef_id = metadef_id_generator_->GenerateId(graph_viewer, model_hash);
return MakeString(QNN, context_node_name_prefix_, "_", model_hash, "_", metadef_id);
};

Expand Down
2 changes: 1 addition & 1 deletion onnxruntime/core/providers/qnn/qnn_execution_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class QNNExecutionProvider : public IExecutionProvider {
bool qnn_context_embed_mode_ = true;
int32_t vtcm_size_in_mb_ = 0;
std::unique_ptr<onnxruntime::Model> qnn_ep_context_model_;
ModelMetadefIdGenerator metadef_id_generator_;
std::unique_ptr<ModelMetadefIdGenerator> metadef_id_generator_;
uint32_t device_id_ = 0;
qnn::HtpPerformanceMode default_htp_performance_mode_ = qnn::HtpPerformanceMode::kHtpDefault;
uint32_t default_rpc_control_latency_ = 0;
Expand Down

0 comments on commit db5f0ec

Please sign in to comment.