Skip to content

Commit 7db58cb

Browse files
committed
Support onnx runtime for wasi-nn.
1 parent 782c69f commit 7db58cb

File tree

7 files changed

+999
-76
lines changed

7 files changed

+999
-76
lines changed

build-scripts/config_common.cmake

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,8 @@ if (WAMR_BUILD_WASI_NN EQUAL 1)
511511
# Variant backends
512512
if (NOT WAMR_BUILD_WASI_NN_TFLITE EQUAL 1 AND
513513
NOT WAMR_BUILD_WASI_NN_OPENVINO EQUAL 1 AND
514-
NOT WAMR_BUILD_WASI_NN_LLAMACPP EQUAL 1)
514+
NOT WAMR_BUILD_WASI_NN_LLAMACPP EQUAL 1 AND
515+
NOT WAMR_BUILD_WASI_NN_ONNXRUNTIME EQUAL 1)
515516
message (FATAL_ERROR " Need to select a backend for WASI-NN")
516517
endif ()
517518

@@ -527,6 +528,10 @@ if (WAMR_BUILD_WASI_NN EQUAL 1)
527528
message (" WASI-NN: backend llamacpp enabled")
528529
add_definitions (-DWASM_ENABLE_WASI_NN_LLAMACPP)
529530
endif ()
531+
if (WAMR_BUILD_WASI_NN_ONNXRUNTIME EQUAL 1)
532+
message (" WASI-NN: backend onnxruntime enabled")
533+
add_definitions (-DWASM_ENABLE_WASI_NN_ONNXRUNTIME)
534+
endif ()
530535
# Variant devices
531536
if (WAMR_BUILD_WASI_NN_ENABLE_GPU EQUAL 1)
532537
message (" WASI-NN: GPU enabled")

core/iwasm/libraries/wasi-nn/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ $ cmake -DWAMR_BUILD_WASI_NN=1 <other options> ...
2626
- `WAMR_BUILD_WASI_NN_TFLITE`. This option designates TensorFlow Lite as the backend.
2727
- `WAMR_BUILD_WASI_NN_OPENVINO`. This option designates OpenVINO as the backend.
2828
- `WAMR_BUILD_WASI_NN_LLAMACPP`. This option designates Llama.cpp as the backend.
29+
- `WAMR_BUILD_WASI_NN_ONNXRUNTIME`. This option designates ONNX Runtime as the backend.
2930

3031
### Wasm
3132

@@ -151,7 +152,7 @@ docker run \
151152

152153
Supported:
153154

154-
- Graph encoding: `tensorflowlite`, `openvino` and `ggml`
155+
- Graph encoding: `tensorflowlite`, `openvino`, `ggml` and `onnx`
155156
- Execution target: `cpu` for all. `gpu` and `tpu` for `tensorflowlite`.
156157
- Tensor type: `fp32`.
157158

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Copyright (C) 2019 Intel Corporation. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
# Find ONNX Runtime library
5+
#
6+
# This module defines the following variables:
7+
#
8+
# ::
9+
#
10+
# onnxruntime_FOUND - True if onnxruntime is found
11+
# onnxruntime_INCLUDE_DIRS - Include directories for onnxruntime
12+
# onnxruntime_LIBRARIES - List of libraries for onnxruntime
13+
# onnxruntime_VERSION - Version of onnxruntime
14+
#
15+
# ::
16+
#
17+
# Example usage:
18+
#
19+
# find_package(onnxruntime)
20+
# if(onnxruntime_FOUND)
21+
# target_link_libraries(app onnxruntime)
22+
# endif()
23+
24+
# First try to find ONNX Runtime using the CMake config file
25+
find_package(onnxruntime CONFIG QUIET)
26+
if(onnxruntime_FOUND)
27+
message(STATUS "Found ONNX Runtime via CMake config: ${onnxruntime_DIR}")
28+
return()
29+
endif()
30+
31+
# If not found via CMake config, try to find manually
32+
find_path(onnxruntime_INCLUDE_DIR
33+
NAMES onnxruntime_c_api.h
34+
PATHS
35+
/usr/include
36+
/usr/local/include
37+
/opt/onnxruntime/include
38+
$ENV{ONNXRUNTIME_ROOT}/include
39+
${CMAKE_CURRENT_LIST_DIR}/../../../../..
40+
/home/ubuntu/onnxruntime/onnxruntime-linux-x64-1.16.3/include
41+
PATH_SUFFIXES onnxruntime
42+
)
43+
44+
find_library(onnxruntime_LIBRARY
45+
NAMES onnxruntime
46+
PATHS
47+
/usr/lib
48+
/usr/local/lib
49+
/opt/onnxruntime/lib
50+
$ENV{ONNXRUNTIME_ROOT}/lib
51+
${CMAKE_CURRENT_LIST_DIR}/../../../../..
52+
/home/ubuntu/onnxruntime/onnxruntime-linux-x64-1.16.3/lib
53+
)
54+
55+
# Try to determine version from header file
56+
if(onnxruntime_INCLUDE_DIR)
57+
file(STRINGS "${onnxruntime_INCLUDE_DIR}/onnxruntime_c_api.h" onnxruntime_version_str
58+
REGEX "^#define[\t ]+ORT_API_VERSION[\t ]+[0-9]+")
59+
60+
if(onnxruntime_version_str)
61+
string(REGEX REPLACE "^#define[\t ]+ORT_API_VERSION[\t ]+([0-9]+)" "\\1"
62+
onnxruntime_VERSION "${onnxruntime_version_str}")
63+
endif()
64+
endif()
65+
66+
include(FindPackageHandleStandardArgs)
67+
find_package_handle_standard_args(onnxruntime
68+
REQUIRED_VARS onnxruntime_LIBRARY onnxruntime_INCLUDE_DIR
69+
VERSION_VAR onnxruntime_VERSION
70+
)
71+
72+
if(onnxruntime_FOUND)
73+
set(onnxruntime_LIBRARIES ${onnxruntime_LIBRARY})
74+
set(onnxruntime_INCLUDE_DIRS ${onnxruntime_INCLUDE_DIR})
75+
76+
if(NOT TARGET onnxruntime)
77+
add_library(onnxruntime UNKNOWN IMPORTED)
78+
set_target_properties(onnxruntime PROPERTIES
79+
IMPORTED_LOCATION "${onnxruntime_LIBRARY}"
80+
INTERFACE_INCLUDE_DIRECTORIES "${onnxruntime_INCLUDE_DIRS}"
81+
)
82+
endif()
83+
endif()
84+
85+
mark_as_advanced(onnxruntime_INCLUDE_DIR onnxruntime_LIBRARY)

core/iwasm/libraries/wasi-nn/cmake/wasi_nn.cmake

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,30 @@ if(WAMR_BUILD_WASI_NN_LLAMACPP EQUAL 1)
109109

110110
install(TARGETS wasi_nn_llamacpp DESTINATION lib)
111111
endif()
112+
113+
# - onnxruntime
114+
if(WAMR_BUILD_WASI_NN_ONNXRUNTIME EQUAL 1)
115+
find_package(onnxruntime REQUIRED)
116+
enable_language(CXX)
117+
118+
add_library(
119+
wasi_nn_onnxruntime
120+
SHARED
121+
${WASI_NN_ROOT}/src/wasi_nn_onnxruntime.cpp
122+
)
123+
124+
target_include_directories(
125+
wasi_nn_onnxruntime
126+
PUBLIC
127+
${onnxruntime_INCLUDE_DIRS}
128+
)
129+
130+
target_link_libraries(
131+
wasi_nn_onnxruntime
132+
PUBLIC
133+
vmlib
134+
onnxruntime
135+
)
136+
137+
install(TARGETS wasi_nn_onnxruntime DESTINATION lib)
138+
endif()

core/iwasm/libraries/wasi-nn/src/wasi_nn.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#define TFLITE_BACKEND_LIB "libwasi_nn_tflite.so"
2525
#define OPENVINO_BACKEND_LIB "libwasi_nn_openvino.so"
2626
#define LLAMACPP_BACKEND_LIB "libwasi_nn_llamacpp.so"
27+
#define ONNXRUNTIME_BACKEND_LIB "libwasi_nn_onnxruntime.so"
2728

2829
/* Global variables */
2930
struct backends_api_functions {
@@ -212,6 +213,17 @@ choose_a_backend()
212213
return openvino;
213214
}
214215

216+
#ifndef NDEBUG
217+
NN_WARN_PRINTF("%s", dlerror());
218+
#endif
219+
220+
handle = dlopen(ONNXRUNTIME_BACKEND_LIB, RTLD_LAZY);
221+
if (handle) {
222+
NN_INFO_PRINTF("Using onnxruntime backend");
223+
dlclose(handle);
224+
return onnx;
225+
}
226+
215227
#ifndef NDEBUG
216228
NN_WARN_PRINTF("%s", dlerror());
217229
#endif
@@ -335,6 +347,8 @@ graph_encoding_to_backend_lib_name(graph_encoding encoding)
335347
return TFLITE_BACKEND_LIB;
336348
case ggml:
337349
return LLAMACPP_BACKEND_LIB;
350+
case onnx:
351+
return ONNXRUNTIME_BACKEND_LIB;
338352
default:
339353
return NULL;
340354
}

0 commit comments

Comments
 (0)