Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QNN EP] [DRAFT] Make QNN EP a shared library #23120

Draft
wants to merge 68 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
d035fb4
Copy shared utils into qnn ep
adrianlizarraga Dec 7, 2024
7e46a7d
Update QNN EP's initializer transpose logic to use only functions exp…
adrianlizarraga Dec 10, 2024
a155b33
Update comment
adrianlizarraga Dec 10, 2024
e9c5f14
Added TransposeBase::DoTranspose() to provider bridge. May elect to r…
adrianlizarraga Dec 10, 2024
d0f64dc
Add TypeProto_Tensor_has_elem_type() to provider bridge
adrianlizarraga Dec 10, 2024
f8bd2f6
Add to provider bridge: TensorTypeBase class, TensorTypeBase::GetElem…
adrianlizarraga Dec 10, 2024
1f533a9
Transpose initializers within QNN EP without using CPU EP utils
adrianlizarraga Dec 11, 2024
ccaefb3
Rename transpose func
adrianlizarraga Dec 11, 2024
fb765c7
Remove TransposeBase forward declaration from provider bridge
adrianlizarraga Dec 11, 2024
0237bca
Rewrite SliceOpBuilder util GetInitializerInputData() to not use func…
adrianlizarraga Dec 11, 2024
e3705b2
Revert addition of TensorTypeBase to provider bridge
adrianlizarraga Dec 11, 2024
6f0b3c6
Remove last use of GetTensorShapeFromTensorProto
adrianlizarraga Dec 11, 2024
5939bf6
Add DataTypeUtils::ToType(std::string&) to provider bridge
adrianlizarraga Dec 11, 2024
58dbf49
Add Logger::GetSeverity() to provider bridge
adrianlizarraga Dec 11, 2024
f76b09a
Add TensorShapeProto_Dimensions__size to provider bridge
adrianlizarraga Dec 11, 2024
d189fe6
Add utils::CreateSupportedPartitions() to provider bridege
adrianlizarraga Dec 12, 2024
a0b3c75
Merge main and fix conflicts
adrianlizarraga Dec 13, 2024
48191ea
Use new namespace for NodeAttrHelper
adrianlizarraga Dec 13, 2024
e6afd72
Add to provider bridge: GraphViewer::Nodes(), ConstGraphNodes struct …
adrianlizarraga Dec 13, 2024
0b1e538
Replace usage of cbegin() and cend() in NodeAttrHelper with version t…
adrianlizarraga Dec 13, 2024
fb3618d
Add convenience function to get the default Env to provider bridge
adrianlizarraga Dec 13, 2024
6b581fd
Moving ORT includes to a separate header
adrianlizarraga Dec 14, 2024
a1129e5
Add Node::EdgeEnd wrapper class to provider bridge. Add NodeUnit cons…
adrianlizarraga Dec 15, 2024
ba86c41
Move more header includes to ort_api.h
adrianlizarraga Dec 15, 2024
2b1ea09
Add GraphViewer::NodeProducesGraphOutput() to provider bridge
adrianlizarraga Dec 15, 2024
421cd78
Replace use of InlinedVector with std::vector and fix newly discovere…
adrianlizarraga Dec 15, 2024
d94e6f7
Eliminate use of qmath.h by introducing new quantization utils for QNN
adrianlizarraga Dec 15, 2024
4eb1e80
Move includes into qnn/ort_api.h
adrianlizarraga Dec 15, 2024
d86fb6c
Add TensorProto::has_data_type() to provider bridge
adrianlizarraga Dec 15, 2024
187e3b9
Checkpoint: updating usage of provider bridge in ep
adrianlizarraga Dec 15, 2024
693dd33
Compiles but does not link (until update cmake to build as shared lib)
adrianlizarraga Dec 16, 2024
ae2dbd2
Use provider bridge function to get default Env
adrianlizarraga Dec 16, 2024
db5f0ec
It works! QNN EP is a shared library and all QNN unit tests pass on W…
adrianlizarraga Dec 16, 2024
ea2a141
Add onnxruntime_providers_qnn.dll to nuget
adrianlizarraga Dec 16, 2024
d820c9b
Pass --build_shared_lib to some QNN pipelines. Include Boost::mp11.
adrianlizarraga Dec 16, 2024
9861ec8
Copy qnn dll for java build
adrianlizarraga Dec 16, 2024
b92043d
Add linker -rpath="
adrianlizarraga Dec 17, 2024
f29ce59
two backslashes in rpath
adrianlizarraga Dec 17, 2024
5d954d6
Copy qnn ep dlls when running java unit tests
adrianlizarraga Dec 17, 2024
03614bc
Modify Java bindings to use QNN shared lib
adrianlizarraga Dec 17, 2024
421f39f
Try to build onnxruntime_providers_shared on Android
adrianlizarraga Dec 17, 2024
7aae2b3
Merge branch 'main' into adrianl/qnn-ep-dynamic-lib
adrianlizarraga Dec 17, 2024
9354f18
Pass linker flag to Android build of qnn dll
adrianlizarraga Dec 19, 2024
e8df64f
Try different linker flag for android
adrianlizarraga Dec 19, 2024
863ff04
Use -z undefs on android
adrianlizarraga Dec 19, 2024
ef1b91d
Expose ETW logger functionality via provider bridge. Fix multithreadi…
adrianlizarraga Dec 19, 2024
57e0072
Cmake java android: copy libonnxruntime_providers_shared.so and libon…
adrianlizarraga Dec 19, 2024
bd32daa
QNN Nuget Pipeline: print contents of binaries directory to see if sh…
adrianlizarraga Dec 19, 2024
8a65a1d
Fix print in yaml
adrianlizarraga Dec 19, 2024
7ff94a0
Fix cmake copy command for Java build with qnn
adrianlizarraga Dec 19, 2024
0fbff4a
Try to fix nuget shared lib files for qnn
adrianlizarraga Dec 19, 2024
9cbd0fa
Pass --build_shared_lib when bulding python wheels with qnn [pipelines]
adrianlizarraga Dec 19, 2024
f8747db
Print correct binary directory in QNN Nuget pipeline
adrianlizarraga Dec 19, 2024
058e7bb
Add onnxruntime_providers_qnn.dll/.so to setup.py so that it gets cop…
adrianlizarraga Dec 19, 2024
4172575
Edit Java bindins to allow loading/extracting shared provider libs on…
adrianlizarraga Dec 19, 2024
3e39b88
Create temp directory for java android
adrianlizarraga Dec 19, 2024
5ed035f
consistent library loading logic for java android
adrianlizarraga Dec 20, 2024
6347c5f
Merge branch 'main' into adrianl/qnn-ep-dynamic-lib
adrianlizarraga Dec 20, 2024
6562433
Add temporary logging
adrianlizarraga Dec 22, 2024
0bbf3af
fix typo
adrianlizarraga Dec 22, 2024
c04176a
fix another type in temporary logging code for debugging android qnn …
adrianlizarraga Dec 22, 2024
cc14971
Android: Go back to not extracting shared libs from classpath resources.
adrianlizarraga Dec 22, 2024
4c6a985
Try linking shared.so with qnn.so for android
adrianlizarraga Dec 22, 2024
fc00346
Use --undefined=Provider_GetHost
adrianlizarraga Dec 23, 2024
b707c46
prepend _ to linker arg
adrianlizarraga Dec 23, 2024
7f50558
Add linker option -z global to libonnxruntime_providers_shared.so on …
adrianlizarraga Dec 23, 2024
17c3bde
Try to use libc++_shared.so for android qnn build
adrianlizarraga Dec 23, 2024
7e7fe68
Merge main and fix conflicts
adrianlizarraga Dec 23, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion cmake/onnxruntime.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ set(onnxruntime_INTERNAL_LIBRARIES
${PROVIDERS_COREML}
${PROVIDERS_DML}
${PROVIDERS_NNAPI}
${PROVIDERS_QNN}
${PROVIDERS_SNPE}
${PROVIDERS_RKNPU}
${PROVIDERS_VSINPU}
Expand Down
20 changes: 18 additions & 2 deletions cmake/onnxruntime_java.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ if (WIN32)
if(NOT onnxruntime_ENABLE_STATIC_ANALYSIS)
add_custom_command(TARGET onnxruntime4j_jni POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:onnxruntime> ${JAVA_PACKAGE_LIB_DIR}/$<TARGET_FILE_NAME:onnxruntime>)
add_custom_command(TARGET onnxruntime4j_jni POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:onnxruntime4j_jni> ${JAVA_PACKAGE_JNI_DIR}/$<TARGET_FILE_NAME:onnxruntime4j_jni>)
if (onnxruntime_USE_CUDA OR onnxruntime_USE_DNNL OR onnxruntime_USE_OPENVINO OR onnxruntime_USE_TENSORRT)
if (onnxruntime_USE_CUDA OR onnxruntime_USE_DNNL OR onnxruntime_USE_OPENVINO OR onnxruntime_USE_TENSORRT OR onnxruntime_USE_QNN)
add_custom_command(TARGET onnxruntime4j_jni POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:onnxruntime_providers_shared> ${JAVA_PACKAGE_LIB_DIR}/$<TARGET_FILE_NAME:onnxruntime_providers_shared>)
endif()
if (onnxruntime_USE_CUDA)
Expand All @@ -163,11 +163,14 @@ if (WIN32)
if (onnxruntime_USE_TENSORRT)
add_custom_command(TARGET onnxruntime4j_jni POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:onnxruntime_providers_tensorrt> ${JAVA_PACKAGE_LIB_DIR}/$<TARGET_FILE_NAME:onnxruntime_providers_tensorrt>)
endif()
if (onnxruntime_USE_QNN)
add_custom_command(TARGET onnxruntime4j_jni POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:onnxruntime_providers_qnn> ${JAVA_PACKAGE_LIB_DIR}/$<TARGET_FILE_NAME:onnxruntime_providers_qnn>)
endif()
endif()
else()
add_custom_command(TARGET onnxruntime4j_jni POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:onnxruntime> ${JAVA_PACKAGE_LIB_DIR}/$<TARGET_LINKER_FILE_NAME:onnxruntime>)
add_custom_command(TARGET onnxruntime4j_jni POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:onnxruntime4j_jni> ${JAVA_PACKAGE_JNI_DIR}/$<TARGET_LINKER_FILE_NAME:onnxruntime4j_jni>)
if (onnxruntime_USE_CUDA OR onnxruntime_USE_DNNL OR onnxruntime_USE_OPENVINO OR onnxruntime_USE_TENSORRT)
if (onnxruntime_USE_CUDA OR onnxruntime_USE_DNNL OR onnxruntime_USE_OPENVINO OR onnxruntime_USE_TENSORRT OR onnxruntime_USE_QNN)
add_custom_command(TARGET onnxruntime4j_jni POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:onnxruntime_providers_shared> ${JAVA_PACKAGE_LIB_DIR}/$<TARGET_LINKER_FILE_NAME:onnxruntime_providers_shared>)
endif()
if (onnxruntime_USE_CUDA)
Expand All @@ -182,6 +185,9 @@ else()
if (onnxruntime_USE_TENSORRT)
add_custom_command(TARGET onnxruntime4j_jni POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:onnxruntime_providers_tensorrt> ${JAVA_PACKAGE_LIB_DIR}/$<TARGET_LINKER_FILE_NAME:onnxruntime_providers_tensorrt>)
endif()
if (onnxruntime_USE_QNN)
add_custom_command(TARGET onnxruntime4j_jni POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:onnxruntime_providers_qnn> ${JAVA_PACKAGE_LIB_DIR}/$<TARGET_LINKER_FILE_NAME:onnxruntime_providers_qnn>)
endif()
endif()

# run the build process (this copies the results back into CMAKE_CURRENT_BINARY_DIR)
Expand All @@ -208,6 +214,16 @@ if (ANDROID)
add_custom_command(TARGET onnxruntime4j_jni POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:onnxruntime> ${ANDROID_PACKAGE_ABI_DIR}/$<TARGET_LINKER_FILE_NAME:onnxruntime>)
add_custom_command(TARGET onnxruntime4j_jni POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:onnxruntime4j_jni> ${ANDROID_PACKAGE_ABI_DIR}/$<TARGET_LINKER_FILE_NAME:onnxruntime4j_jni>)

# If using QNN, also copy libonnxruntime_providers_shared.so and libonnxruntime_providers_qnn.so
if (onnxruntime_USE_QNN)
add_custom_command(TARGET onnxruntime4j_jni POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different
$<TARGET_FILE:onnxruntime_providers_shared>
${ANDROID_PACKAGE_ABI_DIR}/$<TARGET_LINKER_FILE_NAME:onnxruntime_providers_shared>)
add_custom_command(TARGET onnxruntime4j_jni POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different
$<TARGET_FILE:onnxruntime_providers_qnn>
${ANDROID_PACKAGE_ABI_DIR}/$<TARGET_LINKER_FILE_NAME:onnxruntime_providers_qnn>)
endif()

# Generate the Android AAR package
add_custom_command(TARGET onnxruntime4j_jni
POST_BUILD
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
12 changes: 10 additions & 2 deletions cmake/onnxruntime_providers_cpu.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ set_target_properties(onnxruntime_providers PROPERTIES FOLDER "ONNXRuntime")

if (NOT onnxruntime_MINIMAL_BUILD AND NOT onnxruntime_EXTENDED_MINIMAL_BUILD
AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin|iOS|visionOS"
AND NOT CMAKE_SYSTEM_NAME STREQUAL "Android"
AND NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
file(GLOB onnxruntime_providers_shared_cc_srcs CONFIGURE_DEPENDS
"${ONNXRUNTIME_ROOT}/core/providers/shared/*.h"
Expand All @@ -239,7 +238,16 @@ if (NOT onnxruntime_MINIMAL_BUILD AND NOT onnxruntime_EXTENDED_MINIMAL_BUILD
set_property(TARGET onnxruntime_providers_shared APPEND_STRING PROPERTY LINK_FLAGS "-Xlinker -exported_symbols_list ${ONNXRUNTIME_ROOT}/core/providers/shared/exported_symbols.lst")
elseif(UNIX)
if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "AIX")
set_property(TARGET onnxruntime_providers_shared APPEND_STRING PROPERTY LINK_FLAGS "-Xlinker --version-script=${ONNXRUNTIME_ROOT}/core/providers/shared/version_script.lds -Xlinker --gc-sections")
if(CMAKE_SYSTEM_NAME STREQUAL "Android")
target_link_options(onnxruntime_providers_shared PRIVATE
"LINKER:--version-script=${ONNXRUNTIME_ROOT}/core/providers/shared/version_script.lds"
"LINKER:--gc-sections"
"LINKER:-z,global")
else()
target_link_options(onnxruntime_providers_shared PRIVATE
"LINKER:--version-script=${ONNXRUNTIME_ROOT}/core/providers/shared/version_script.lds"
"LINKER:--gc-sections")
endif()
endif()
elseif(WIN32)
set_property(TARGET onnxruntime_providers_shared APPEND_STRING PROPERTY LINK_FLAGS "-DEF:${ONNXRUNTIME_ROOT}/core/providers/shared/symbols.def")
Expand Down
80 changes: 52 additions & 28 deletions cmake/onnxruntime_providers_qnn.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,65 @@

add_compile_definitions(USE_QNN=1)

# These are shared utils,
# TODO, move to a separate lib when used by EPs other than QNN, NNAPI and CoreML
file(GLOB onnxruntime_providers_shared_utils_cc_srcs CONFIGURE_DEPENDS
"${ONNXRUNTIME_ROOT}/core/providers/shared/utils/utils.h"
"${ONNXRUNTIME_ROOT}/core/providers/shared/utils/utils.cc"
)

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_shared_utils_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 Boost::mp11 safeint_interface)
target_link_libraries(onnxruntime_providers_qnn PRIVATE ${ONNXRUNTIME_PROVIDERS_SHARED} ${ABSEIL_LIBS} ${CMAKE_DL_LIBS})
add_dependencies(onnxruntime_providers_qnn onnxruntime_providers_shared ${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)
if(CMAKE_SYSTEM_NAME STREQUAL "Android")
target_link_options(onnxruntime_providers_qnn PRIVATE
"LINKER:--version-script=${ONNXRUNTIME_ROOT}/core/providers/qnn/version_script.lds"
"LINKER:--gc-sections"
"LINKER:-rpath=\$ORIGIN"
"LINKER:-z,undefs"
)
else()
target_link_options(onnxruntime_providers_qnn PRIVATE
"LINKER:--version-script=${ONNXRUNTIME_ROOT}/core/providers/qnn/version_script.lds"
"LINKER:--gc-sections"
"LINKER:-rpath=\$ORIGIN"
)
endif()
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
9 changes: 4 additions & 5 deletions cmake/onnxruntime_unittests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -631,12 +631,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 @@ -707,8 +706,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 Expand Up @@ -1286,7 +1284,8 @@ if (NOT onnxruntime_ENABLE_TRAINING_TORCH_INTEROP)

file(GLOB onnxruntime_qnn_ctx_gen_src CONFIGURE_DEPENDS
${onnxruntime_qnn_ctx_gen_src_patterns}
)
)

onnxruntime_add_executable(onnxruntime_qnn_ctx_gen ${onnxruntime_qnn_ctx_gen_src})
target_include_directories(onnxruntime_qnn_ctx_gen PRIVATE ${onnx_test_runner_src_dir} ${ONNXRUNTIME_ROOT}
${eigen_INCLUDE_DIRS} ${onnxruntime_graph_header} ${onnxruntime_exec_src_dir}
Expand Down
22 changes: 19 additions & 3 deletions java/src/main/java/ai/onnxruntime/OnnxRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ final class OnnxRuntime {
/** The short name of the ONNX runtime TensorRT provider library */
static final String ONNXRUNTIME_LIBRARY_TENSORRT_NAME = "onnxruntime_providers_tensorrt";

/** The short name of the ONNX runtime QNN provider library */
static final String ONNXRUNTIME_LIBRARY_QNN_NAME = "onnxruntime_providers_qnn";

/** The OS & CPU architecture string */
private static final String OS_ARCH_STR = initOsArch();

Expand Down Expand Up @@ -159,8 +162,11 @@ static synchronized void init() throws IOException {
// the ONNX Runtime native library will load it
extractProviderLibrary(ONNXRUNTIME_LIBRARY_SHARED_NAME);

load(ONNXRUNTIME_LIBRARY_NAME);
if (!isAndroid()) {
load(ONNXRUNTIME_LIBRARY_NAME);
}
load(ONNXRUNTIME_JNI_LIBRARY_NAME);

ortApiHandle = initialiseAPIBase(ORT_API_VERSION_14);
if (ortApiHandle == 0L) {
throw new IllegalStateException(
Expand Down Expand Up @@ -252,6 +258,16 @@ static boolean extractTensorRT() {
return extractProviderLibrary(ONNXRUNTIME_LIBRARY_TENSORRT_NAME);
}

/**
* Extracts the QNN provider library from the classpath resources if present, or checks to see if
* the QNN provider library is in the directory specified by {@link #ONNXRUNTIME_NATIVE_PATH}.
*
* @return True if the QNN provider library is ready for loading, false otherwise.
*/
static boolean extractQNN() {
return extractProviderLibrary(ONNXRUNTIME_LIBRARY_QNN_NAME);
}

/**
* Extracts a shared provider library from the classpath resources if present, or checks to see if
* that library is in the directory specified by {@link #ONNXRUNTIME_NATIVE_PATH}.
Expand All @@ -260,7 +276,7 @@ static boolean extractTensorRT() {
* @return True if the library is ready for loading by ORT's native code, false otherwise.
*/
static synchronized boolean extractProviderLibrary(String libraryName) {
// Android does not need to extract library and it has no shared provider library
// Android does not need to extract provider libraries.
if (isAndroid()) {
return false;
}
Expand Down Expand Up @@ -312,7 +328,7 @@ static boolean isAndroid() {
private static void load(String library) throws IOException {
// On Android, we simply use System.loadLibrary
if (isAndroid()) {
System.loadLibrary("onnxruntime4j_jni");
System.loadLibrary(library);
return;
}

Expand Down
9 changes: 8 additions & 1 deletion java/src/main/java/ai/onnxruntime/OrtSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -1320,7 +1320,14 @@ public void addXnnpack(Map<String, String> providerOptions) throws OrtException
*/
public void addQnn(Map<String, String> providerOptions) throws OrtException {
String qnnProviderName = "QNN";
addExecutionProvider(qnnProviderName, providerOptions);
if (OnnxRuntime.isAndroid()) {
addExecutionProvider(qnnProviderName, providerOptions);
} else if (OnnxRuntime.extractQNN()) {
addExecutionProvider(qnnProviderName, providerOptions);
} else {
throw new OrtException(
OrtException.OrtErrorCode.ORT_EP_FAIL, "Failed to find QNN shared provider");
}
}

/**
Expand Down
Loading
Loading