Skip to content

Commit 56b6195

Browse files
Add onnxruntime as wasi-nn backend
1 parent 5b63f35 commit 56b6195

File tree

8 files changed

+959
-5
lines changed

8 files changed

+959
-5
lines changed

build-scripts/config_common.cmake

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,8 @@ if (WAMR_BUILD_WASI_NN EQUAL 1)
546546
# Variant backends
547547
if (NOT WAMR_BUILD_WASI_NN_TFLITE EQUAL 1 AND
548548
NOT WAMR_BUILD_WASI_NN_OPENVINO EQUAL 1 AND
549-
NOT WAMR_BUILD_WASI_NN_LLAMACPP EQUAL 1)
549+
NOT WAMR_BUILD_WASI_NN_LLAMACPP EQUAL 1 AND
550+
NOT WAMR_BUILD_WASI_NN_ONNX EQUAL 1)
550551
message (FATAL_ERROR " Need to select a backend for WASI-NN")
551552
endif ()
552553

@@ -562,6 +563,10 @@ if (WAMR_BUILD_WASI_NN EQUAL 1)
562563
message (" WASI-NN: backend llamacpp enabled")
563564
add_definitions (-DWASM_ENABLE_WASI_NN_LLAMACPP)
564565
endif ()
566+
if (WAMR_BUILD_WASI_NN_ONNX EQUAL 1)
567+
message (" WASI-NN: backend onnx enabled")
568+
add_definitions (-DWASM_ENABLE_WASI_NN_ONNX)
569+
endif ()
565570
# Variant devices
566571
if (WAMR_BUILD_WASI_NN_ENABLE_GPU EQUAL 1)
567572
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_ONNX`. 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: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Copyright 2025 Sony Semiconductor Solutions Corporation.
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+
26+
# If not found via CMake config, try to find manually
27+
find_path(onnxruntime_INCLUDE_DIR
28+
NAMES onnxruntime_c_api.h
29+
PATHS
30+
/usr/include
31+
/usr/local/include
32+
/opt/onnxruntime/include
33+
$ENV{ONNXRUNTIME_ROOT}/include
34+
${CMAKE_CURRENT_LIST_DIR}/../../../../..
35+
)
36+
37+
find_library(onnxruntime_LIBRARY
38+
NAMES onnxruntime
39+
PATHS
40+
/usr/lib
41+
/usr/local/lib
42+
/opt/onnxruntime/lib
43+
$ENV{ONNXRUNTIME_ROOT}/lib
44+
${CMAKE_CURRENT_LIST_DIR}/../../../../..
45+
)
46+
47+
# Try to determine version from header file
48+
if(onnxruntime_INCLUDE_DIR)
49+
file(STRINGS "${onnxruntime_INCLUDE_DIR}/onnxruntime_c_api.h" onnxruntime_version_str
50+
REGEX "^#define[\t ]+ORT_API_VERSION[\t ]+[0-9]+")
51+
52+
if(onnxruntime_version_str)
53+
string(REGEX REPLACE "^#define[\t ]+ORT_API_VERSION[\t ]+([0-9]+)" "\\1"
54+
onnxruntime_VERSION "${onnxruntime_version_str}")
55+
endif()
56+
endif()
57+
58+
include(FindPackageHandleStandardArgs)
59+
find_package_handle_standard_args(onnxruntime
60+
REQUIRED_VARS onnxruntime_LIBRARY onnxruntime_INCLUDE_DIR
61+
VERSION_VAR onnxruntime_VERSION
62+
)
63+
64+
if(onnxruntime_FOUND)
65+
set(onnxruntime_LIBRARIES ${onnxruntime_LIBRARY})
66+
set(onnxruntime_INCLUDE_DIRS ${onnxruntime_INCLUDE_DIR})
67+
68+
if(NOT TARGET onnxruntime)
69+
add_library(onnxruntime UNKNOWN IMPORTED)
70+
set_target_properties(onnxruntime PROPERTIES
71+
IMPORTED_LOCATION "${onnxruntime_LIBRARY}"
72+
INTERFACE_INCLUDE_DIRECTORIES "${onnxruntime_INCLUDE_DIRS}"
73+
)
74+
endif()
75+
endif()
76+
77+
mark_as_advanced(onnxruntime_INCLUDE_DIR onnxruntime_LIBRARY)

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

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

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

core/iwasm/libraries/wasi-nn/include/wasi_nn.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#else
2222
#define WASI_NN_IMPORT(name) \
2323
__attribute__((import_module("wasi_nn"), import_name(name)))
24-
#warning You are using "wasi_nn", which is a legacy WAMR-specific ABI. It's deperecated and will likely be removed in future versions of WAMR. Please use "wasi_ephemeral_nn" instead. (For a WASM module, use the wasi_ephemeral_nn.h header instead. For the runtime configurations, enable WASM_ENABLE_WASI_EPHEMERAL_NN/WAMR_BUILD_WASI_EPHEMERAL_NN.)
24+
#warning "You are using \"wasi_nn\", which is a legacy WAMR-specific ABI. It's deprecated and will likely be removed in future versions of WAMR. Please use \"wasi_ephemeral_nn\" instead. (For a WASM module, use the wasi_ephemeral_nn.h header instead. For the runtime configurations, enable WASM_ENABLE_WASI_EPHEMERAL_NN/WAMR_BUILD_WASI_EPHEMERAL_NN.)"
2525
#endif
2626

2727
/**

core/iwasm/libraries/wasi-nn/include/wasi_nn_types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ extern "C" {
2727
#define WASI_NN_TYPE_NAME(name) WASI_NN_NAME(type_##name)
2828
#define WASI_NN_ENCODING_NAME(name) WASI_NN_NAME(encoding_##name)
2929
#define WASI_NN_TARGET_NAME(name) WASI_NN_NAME(target_##name)
30-
#define WASI_NN_ERROR_TYPE WASI_NN_NAME(error);
30+
#define WASI_NN_ERROR_TYPE WASI_NN_NAME(error)
3131
#endif
3232

3333
/**

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
#include "wasm_export.h"
2222

2323
#if WASM_ENABLE_WASI_EPHEMERAL_NN == 0
24-
#warning You are using "wasi_nn", which is a legacy WAMR-specific ABI. It's deperecated and will likely be removed in future versions of WAMR. Please use "wasi_ephemeral_nn" instead. (For a WASM module, use the wasi_ephemeral_nn.h header instead. For the runtime configurations, enable WASM_ENABLE_WASI_EPHEMERAL_NN/WAMR_BUILD_WASI_EPHEMERAL_NN.)
24+
#warning \
25+
"You are using \"wasi_nn\", which is a legacy WAMR-specific ABI. It's deprecated and will likely be removed in future versions of WAMR. Please use \"wasi_ephemeral_nn\" instead. (For a WASM module, use the wasi_ephemeral_nn.h header instead. For the runtime configurations, enable WASM_ENABLE_WASI_EPHEMERAL_NN/WAMR_BUILD_WASI_EPHEMERAL_NN.)"
2526
#endif
2627

2728
#define HASHMAP_INITIAL_SIZE 20
@@ -33,6 +34,7 @@
3334
#define TFLITE_BACKEND_LIB "libwasi_nn_tflite" LIB_EXTENTION
3435
#define OPENVINO_BACKEND_LIB "libwasi_nn_openvino" LIB_EXTENTION
3536
#define LLAMACPP_BACKEND_LIB "libwasi_nn_llamacpp" LIB_EXTENTION
37+
#define ONNX_BACKEND_LIB "libwasi_nn_onnx" LIB_EXTENTION
3638

3739
/* Global variables */
3840
static korp_mutex wasi_nn_lock;
@@ -240,6 +242,17 @@ choose_a_backend()
240242
return openvino;
241243
}
242244

245+
#ifndef NDEBUG
246+
NN_WARN_PRINTF("%s", dlerror());
247+
#endif
248+
249+
handle = dlopen(ONNX_BACKEND_LIB, RTLD_LAZY);
250+
if (handle) {
251+
NN_INFO_PRINTF("Using onnx backend");
252+
dlclose(handle);
253+
return onnx;
254+
}
255+
243256
#ifndef NDEBUG
244257
NN_WARN_PRINTF("%s", dlerror());
245258
#endif
@@ -363,6 +376,8 @@ graph_encoding_to_backend_lib_name(graph_encoding encoding)
363376
return TFLITE_BACKEND_LIB;
364377
case ggml:
365378
return LLAMACPP_BACKEND_LIB;
379+
case onnx:
380+
return ONNX_BACKEND_LIB;
366381
default:
367382
return NULL;
368383
}

0 commit comments

Comments
 (0)