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,67 +117,69 @@ function(download_onnxruntime) @@ -117,67 +117,69 @@ 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}")  
125 -  
126 -if(DEFINED ENV{SHERPA_ONNXRUNTIME_INCLUDE_DIR})  
127 - set(location_onnxruntime_header_dir $ENV{SHERPA_ONNXRUNTIME_INCLUDE_DIR})  
128 -  
129 - include_directories(${location_onnxruntime_header_dir})  
130 -else()  
131 - find_path(location_onnxruntime_header_dir onnxruntime_cxx_api.h  
132 - PATHS  
133 - /usr/include  
134 - /usr/local/include  
135 - )  
136 -endif() 124 + message(STATUS "CMAKE_SYSTEM_NAME: ${CMAKE_SYSTEM_NAME}")
  125 + message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
137 126
138 -message(STATUS "location_onnxruntime_header_dir: ${location_onnxruntime_header_dir}") 127 + if(DEFINED ENV{SHERPA_ONNXRUNTIME_INCLUDE_DIR})
  128 + set(location_onnxruntime_header_dir $ENV{SHERPA_ONNXRUNTIME_INCLUDE_DIR})
139 129
140 -if(DEFINED ENV{SHERPA_ONNXRUNTIME_LIB_DIR})  
141 - if(APPLE)  
142 - set(location_onnxruntime_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime.dylib) 130 + include_directories(${location_onnxruntime_header_dir})
143 else() 131 else()
144 - set(location_onnxruntime_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime.so) 132 + find_path(location_onnxruntime_header_dir onnxruntime_cxx_api.h
  133 + PATHS
  134 + /usr/include
  135 + /usr/local/include
  136 + )
145 endif() 137 endif()
146 - if(NOT EXISTS ${location_onnxruntime_lib})  
147 - set(location_onnxruntime_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime.a) 138 +
  139 + message(STATUS "location_onnxruntime_header_dir: ${location_onnxruntime_header_dir}")
  140 +
  141 + if(DEFINED ENV{SHERPA_ONNXRUNTIME_LIB_DIR})
  142 + if(APPLE)
  143 + set(location_onnxruntime_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime.dylib)
  144 + else()
  145 + set(location_onnxruntime_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime.so)
  146 + endif()
148 if(NOT EXISTS ${location_onnxruntime_lib}) 147 if(NOT EXISTS ${location_onnxruntime_lib})
149 - message(FATAL_ERROR "${location_onnxruntime_lib} cannot be found") 148 + set(location_onnxruntime_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime.a)
  149 + if(NOT EXISTS ${location_onnxruntime_lib})
  150 + message(FATAL_ERROR "${location_onnxruntime_lib} cannot be found")
  151 + endif()
  152 + set(onnxruntime_lib_files $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime.a)
  153 + message("Use static lib: ${onnxruntime_lib_files}")
150 endif() 154 endif()
151 - set(onnxruntime_lib_files $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime.a)  
152 - message("Use static lib: ${onnxruntime_lib_files}")  
153 - endif()  
154 - if(SHERPA_ONNX_ENABLE_GPU)  
155 - set(location_onnxruntime_cuda_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime_providers_cuda.so)  
156 - if(NOT EXISTS ${location_onnxruntime_cuda_lib})  
157 - set(location_onnxruntime_cuda_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime_providers_cuda.a) 155 + if(SHERPA_ONNX_ENABLE_GPU)
  156 + set(location_onnxruntime_cuda_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime_providers_cuda.so)
  157 + if(NOT EXISTS ${location_onnxruntime_cuda_lib})
  158 + set(location_onnxruntime_cuda_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime_providers_cuda.a)
  159 + endif()
158 endif() 160 endif()
159 - endif()  
160 -else()  
161 - find_library(location_onnxruntime_lib onnxruntime  
162 - PATHS  
163 - /lib  
164 - /usr/lib  
165 - /usr/local/lib  
166 - )  
167 -  
168 - if(SHERPA_ONNX_ENABLE_GPU)  
169 - find_library(location_onnxruntime_cuda_lib onnxruntime_providers_cuda 161 + else()
  162 + find_library(location_onnxruntime_lib onnxruntime
170 PATHS 163 PATHS
171 /lib 164 /lib
172 /usr/lib 165 /usr/lib
173 /usr/local/lib 166 /usr/local/lib
174 ) 167 )
  168 +
  169 + if(SHERPA_ONNX_ENABLE_GPU)
  170 + find_library(location_onnxruntime_cuda_lib onnxruntime_providers_cuda
  171 + PATHS
  172 + /lib
  173 + /usr/lib
  174 + /usr/local/lib
  175 + )
  176 + endif()
175 endif() 177 endif()
176 -endif()  
177 178
178 -message(STATUS "location_onnxruntime_lib: ${location_onnxruntime_lib}")  
179 -if(SHERPA_ONNX_ENABLE_GPU)  
180 - message(STATUS "location_onnxruntime_cuda_lib: ${location_onnxruntime_cuda_lib}") 179 + message(STATUS "location_onnxruntime_lib: ${location_onnxruntime_lib}")
  180 + if(SHERPA_ONNX_ENABLE_GPU)
  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