Fangjun Kuang
Committed by GitHub

Allow to not use pre-installed onnxruntime libs. (#636)

@@ -26,6 +26,7 @@ option(SHERPA_ONNX_ENABLE_WASM_ASR "Whether to enable WASM for ASR" OFF) @@ -26,6 +26,7 @@ option(SHERPA_ONNX_ENABLE_WASM_ASR "Whether to enable WASM for ASR" OFF)
26 option(SHERPA_ONNX_ENABLE_WASM_NODEJS "Whether to enable WASM for NodeJS" OFF) 26 option(SHERPA_ONNX_ENABLE_WASM_NODEJS "Whether to enable WASM for NodeJS" OFF)
27 option(SHERPA_ONNX_ENABLE_BINARY "Whether to build binaries" ON) 27 option(SHERPA_ONNX_ENABLE_BINARY "Whether to build binaries" ON)
28 option(SHERPA_ONNX_LINK_LIBSTDCPP_STATICALLY "True to link libstdc++ statically. Used only when BUILD_SHARED_LIBS is OFF on Linux" ON) 28 option(SHERPA_ONNX_LINK_LIBSTDCPP_STATICALLY "True to link libstdc++ statically. Used only when BUILD_SHARED_LIBS is OFF on Linux" ON)
  29 +option(SHERPA_ONNX_USE_PRE_INSTALLED_ONNXRUNTIME_IF_AVAILABLE "True to use pre-installed onnxruntime if available" ON)
29 30
30 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib") 31 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
31 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib") 32 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
@@ -110,6 +111,7 @@ message(STATUS "SHERPA_ONNX_ENABLE_WASM ${SHERPA_ONNX_ENABLE_WASM}") @@ -110,6 +111,7 @@ message(STATUS "SHERPA_ONNX_ENABLE_WASM ${SHERPA_ONNX_ENABLE_WASM}")
110 message(STATUS "SHERPA_ONNX_ENABLE_WASM_TTS ${SHERPA_ONNX_ENABLE_WASM_TTS}") 111 message(STATUS "SHERPA_ONNX_ENABLE_WASM_TTS ${SHERPA_ONNX_ENABLE_WASM_TTS}")
111 message(STATUS "SHERPA_ONNX_ENABLE_WASM_ASR ${SHERPA_ONNX_ENABLE_WASM_ASR}") 112 message(STATUS "SHERPA_ONNX_ENABLE_WASM_ASR ${SHERPA_ONNX_ENABLE_WASM_ASR}")
112 message(STATUS "SHERPA_ONNX_ENABLE_WASM_NODEJS ${SHERPA_ONNX_ENABLE_WASM_NODEJS}") 113 message(STATUS "SHERPA_ONNX_ENABLE_WASM_NODEJS ${SHERPA_ONNX_ENABLE_WASM_NODEJS}")
  114 +message(STATUS "SHERPA_ONNX_USE_PRE_INSTALLED_ONNXRUNTIME_IF_AVAILABLE ${SHERPA_ONNX_USE_PRE_INSTALLED_ONNXRUNTIME_IF_AVAILABLE}")
113 115
114 if(SHERPA_ONNX_ENABLE_WASM_TTS) 116 if(SHERPA_ONNX_ENABLE_WASM_TTS)
115 if(NOT SHERPA_ONNX_ENABLE_WASM) 117 if(NOT SHERPA_ONNX_ENABLE_WASM)
@@ -117,27 +117,28 @@ function(download_onnxruntime) @@ -117,27 +117,28 @@ function(download_onnxruntime)
117 set(onnxruntime_SOURCE_DIR ${onnxruntime_SOURCE_DIR} PARENT_SCOPE) 117 set(onnxruntime_SOURCE_DIR ${onnxruntime_SOURCE_DIR} PARENT_SCOPE)
118 endfunction() 118 endfunction()
119 119
120 -# First, we try to locate the header and the lib if the use has already  
121 -# installed onnxruntime. Otherwise, we will download the pre-compiled lib 120 +if(SHERPA_ONNX_USE_PRE_INSTALLED_ONNXRUNTIME_IF_AVAILABLE)
  121 + # First, we try to locate the header and the lib if the user has already
  122 + # installed onnxruntime. Otherwise, we will download the pre-compiled lib
122 123
123 -message(STATUS "CMAKE_SYSTEM_NAME: ${CMAKE_SYSTEM_NAME}")  
124 -message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}") 124 + message(STATUS "CMAKE_SYSTEM_NAME: ${CMAKE_SYSTEM_NAME}")
  125 + message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
125 126
126 -if(DEFINED ENV{SHERPA_ONNXRUNTIME_INCLUDE_DIR}) 127 + if(DEFINED ENV{SHERPA_ONNXRUNTIME_INCLUDE_DIR})
127 set(location_onnxruntime_header_dir $ENV{SHERPA_ONNXRUNTIME_INCLUDE_DIR}) 128 set(location_onnxruntime_header_dir $ENV{SHERPA_ONNXRUNTIME_INCLUDE_DIR})
128 129
129 include_directories(${location_onnxruntime_header_dir}) 130 include_directories(${location_onnxruntime_header_dir})
130 -else() 131 + else()
131 find_path(location_onnxruntime_header_dir onnxruntime_cxx_api.h 132 find_path(location_onnxruntime_header_dir onnxruntime_cxx_api.h
132 PATHS 133 PATHS
133 /usr/include 134 /usr/include
134 /usr/local/include 135 /usr/local/include
135 ) 136 )
136 -endif() 137 + endif()
137 138
138 -message(STATUS "location_onnxruntime_header_dir: ${location_onnxruntime_header_dir}") 139 + message(STATUS "location_onnxruntime_header_dir: ${location_onnxruntime_header_dir}")
139 140
140 -if(DEFINED ENV{SHERPA_ONNXRUNTIME_LIB_DIR}) 141 + if(DEFINED ENV{SHERPA_ONNXRUNTIME_LIB_DIR})
141 if(APPLE) 142 if(APPLE)
142 set(location_onnxruntime_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime.dylib) 143 set(location_onnxruntime_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime.dylib)
143 else() 144 else()
@@ -157,7 +158,7 @@ if(DEFINED ENV{SHERPA_ONNXRUNTIME_LIB_DIR}) @@ -157,7 +158,7 @@ if(DEFINED ENV{SHERPA_ONNXRUNTIME_LIB_DIR})
157 set(location_onnxruntime_cuda_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime_providers_cuda.a) 158 set(location_onnxruntime_cuda_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime_providers_cuda.a)
158 endif() 159 endif()
159 endif() 160 endif()
160 -else() 161 + else()
161 find_library(location_onnxruntime_lib onnxruntime 162 find_library(location_onnxruntime_lib onnxruntime
162 PATHS 163 PATHS
163 /lib 164 /lib
@@ -173,11 +174,12 @@ else() @@ -173,11 +174,12 @@ else()
173 /usr/local/lib 174 /usr/local/lib
174 ) 175 )
175 endif() 176 endif()
176 -endif() 177 + endif()
177 178
178 -message(STATUS "location_onnxruntime_lib: ${location_onnxruntime_lib}")  
179 -if(SHERPA_ONNX_ENABLE_GPU) 179 + message(STATUS "location_onnxruntime_lib: ${location_onnxruntime_lib}")
  180 + if(SHERPA_ONNX_ENABLE_GPU)
180 message(STATUS "location_onnxruntime_cuda_lib: ${location_onnxruntime_cuda_lib}") 181 message(STATUS "location_onnxruntime_cuda_lib: ${location_onnxruntime_cuda_lib}")
  182 + endif()
181 endif() 183 endif()
182 184
183 if(location_onnxruntime_header_dir AND location_onnxruntime_lib) 185 if(location_onnxruntime_header_dir AND location_onnxruntime_lib)
@@ -195,6 +197,10 @@ if(location_onnxruntime_header_dir AND location_onnxruntime_lib) @@ -195,6 +197,10 @@ if(location_onnxruntime_header_dir AND location_onnxruntime_lib)
195 endif() 197 endif()
196 endif() 198 endif()
197 else() 199 else()
198 - message(STATUS "Could not find a pre-installed onnxruntime. Downloading pre-compiled onnxruntime") 200 + if(SHERPA_ONNX_USE_PRE_INSTALLED_ONNXRUNTIME_IF_AVAILABLE)
  201 + message(STATUS "Could not find a pre-installed onnxruntime.")
  202 + endif()
  203 + message(STATUS "Downloading pre-compiled onnxruntime")
  204 +
199 download_onnxruntime() 205 download_onnxruntime()
200 endif() 206 endif()
@@ -2,14 +2,16 @@ @@ -2,14 +2,16 @@
2 // 2 //
3 // Copyright (c) 2023-2024 Xiaomi Corporation 3 // Copyright (c) 2023-2024 Xiaomi Corporation
4 4
  5 +#include "sherpa-onnx/csrc/transducer-keyword-decoder.h"
  6 +
5 #include <algorithm> 7 #include <algorithm>
6 #include <cmath> 8 #include <cmath>
  9 +#include <cstring>
7 #include <utility> 10 #include <utility>
8 #include <vector> 11 #include <vector>
9 12
10 #include "sherpa-onnx/csrc/log.h" 13 #include "sherpa-onnx/csrc/log.h"
11 #include "sherpa-onnx/csrc/onnx-utils.h" 14 #include "sherpa-onnx/csrc/onnx-utils.h"
12 -#include "sherpa-onnx/csrc/transducer-keyword-decoder.h"  
13 15
14 namespace sherpa_onnx { 16 namespace sherpa_onnx {
15 17