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

This commit is contained in:
Fangjun Kuang
2024-03-06 14:40:23 +08:00
committed by GitHub
parent 13260cdf49
commit bdf9243940
3 changed files with 61 additions and 51 deletions

View File

@@ -26,6 +26,7 @@ option(SHERPA_ONNX_ENABLE_WASM_ASR "Whether to enable WASM for ASR" OFF)
option(SHERPA_ONNX_ENABLE_WASM_NODEJS "Whether to enable WASM for NodeJS" OFF) option(SHERPA_ONNX_ENABLE_WASM_NODEJS "Whether to enable WASM for NodeJS" OFF)
option(SHERPA_ONNX_ENABLE_BINARY "Whether to build binaries" ON) option(SHERPA_ONNX_ENABLE_BINARY "Whether to build binaries" ON)
option(SHERPA_ONNX_LINK_LIBSTDCPP_STATICALLY "True to link libstdc++ statically. Used only when BUILD_SHARED_LIBS is OFF on Linux" ON) option(SHERPA_ONNX_LINK_LIBSTDCPP_STATICALLY "True to link libstdc++ statically. Used only when BUILD_SHARED_LIBS is OFF on Linux" ON)
option(SHERPA_ONNX_USE_PRE_INSTALLED_ONNXRUNTIME_IF_AVAILABLE "True to use pre-installed onnxruntime if available" ON)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib") set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib") set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
@@ -110,6 +111,7 @@ message(STATUS "SHERPA_ONNX_ENABLE_WASM ${SHERPA_ONNX_ENABLE_WASM}")
message(STATUS "SHERPA_ONNX_ENABLE_WASM_TTS ${SHERPA_ONNX_ENABLE_WASM_TTS}") message(STATUS "SHERPA_ONNX_ENABLE_WASM_TTS ${SHERPA_ONNX_ENABLE_WASM_TTS}")
message(STATUS "SHERPA_ONNX_ENABLE_WASM_ASR ${SHERPA_ONNX_ENABLE_WASM_ASR}") message(STATUS "SHERPA_ONNX_ENABLE_WASM_ASR ${SHERPA_ONNX_ENABLE_WASM_ASR}")
message(STATUS "SHERPA_ONNX_ENABLE_WASM_NODEJS ${SHERPA_ONNX_ENABLE_WASM_NODEJS}") message(STATUS "SHERPA_ONNX_ENABLE_WASM_NODEJS ${SHERPA_ONNX_ENABLE_WASM_NODEJS}")
message(STATUS "SHERPA_ONNX_USE_PRE_INSTALLED_ONNXRUNTIME_IF_AVAILABLE ${SHERPA_ONNX_USE_PRE_INSTALLED_ONNXRUNTIME_IF_AVAILABLE}")
if(SHERPA_ONNX_ENABLE_WASM_TTS) if(SHERPA_ONNX_ENABLE_WASM_TTS)
if(NOT SHERPA_ONNX_ENABLE_WASM) if(NOT SHERPA_ONNX_ENABLE_WASM)

View File

@@ -117,27 +117,28 @@ function(download_onnxruntime)
set(onnxruntime_SOURCE_DIR ${onnxruntime_SOURCE_DIR} PARENT_SCOPE) set(onnxruntime_SOURCE_DIR ${onnxruntime_SOURCE_DIR} PARENT_SCOPE)
endfunction() endfunction()
# First, we try to locate the header and the lib if the use has already if(SHERPA_ONNX_USE_PRE_INSTALLED_ONNXRUNTIME_IF_AVAILABLE)
# installed onnxruntime. Otherwise, we will download the pre-compiled lib # First, we try to locate the header and the lib if the user has already
# installed onnxruntime. Otherwise, we will download the pre-compiled lib
message(STATUS "CMAKE_SYSTEM_NAME: ${CMAKE_SYSTEM_NAME}") message(STATUS "CMAKE_SYSTEM_NAME: ${CMAKE_SYSTEM_NAME}")
message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}") message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
if(DEFINED ENV{SHERPA_ONNXRUNTIME_INCLUDE_DIR}) if(DEFINED ENV{SHERPA_ONNXRUNTIME_INCLUDE_DIR})
set(location_onnxruntime_header_dir $ENV{SHERPA_ONNXRUNTIME_INCLUDE_DIR}) set(location_onnxruntime_header_dir $ENV{SHERPA_ONNXRUNTIME_INCLUDE_DIR})
include_directories(${location_onnxruntime_header_dir}) include_directories(${location_onnxruntime_header_dir})
else() else()
find_path(location_onnxruntime_header_dir onnxruntime_cxx_api.h find_path(location_onnxruntime_header_dir onnxruntime_cxx_api.h
PATHS PATHS
/usr/include /usr/include
/usr/local/include /usr/local/include
) )
endif() endif()
message(STATUS "location_onnxruntime_header_dir: ${location_onnxruntime_header_dir}") message(STATUS "location_onnxruntime_header_dir: ${location_onnxruntime_header_dir}")
if(DEFINED ENV{SHERPA_ONNXRUNTIME_LIB_DIR}) if(DEFINED ENV{SHERPA_ONNXRUNTIME_LIB_DIR})
if(APPLE) if(APPLE)
set(location_onnxruntime_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime.dylib) set(location_onnxruntime_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime.dylib)
else() else()
@@ -157,7 +158,7 @@ if(DEFINED ENV{SHERPA_ONNXRUNTIME_LIB_DIR})
set(location_onnxruntime_cuda_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime_providers_cuda.a) set(location_onnxruntime_cuda_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime_providers_cuda.a)
endif() endif()
endif() endif()
else() else()
find_library(location_onnxruntime_lib onnxruntime find_library(location_onnxruntime_lib onnxruntime
PATHS PATHS
/lib /lib
@@ -173,11 +174,12 @@ else()
/usr/local/lib /usr/local/lib
) )
endif() endif()
endif() endif()
message(STATUS "location_onnxruntime_lib: ${location_onnxruntime_lib}") message(STATUS "location_onnxruntime_lib: ${location_onnxruntime_lib}")
if(SHERPA_ONNX_ENABLE_GPU) if(SHERPA_ONNX_ENABLE_GPU)
message(STATUS "location_onnxruntime_cuda_lib: ${location_onnxruntime_cuda_lib}") message(STATUS "location_onnxruntime_cuda_lib: ${location_onnxruntime_cuda_lib}")
endif()
endif() endif()
if(location_onnxruntime_header_dir AND location_onnxruntime_lib) if(location_onnxruntime_header_dir AND location_onnxruntime_lib)
@@ -195,6 +197,10 @@ if(location_onnxruntime_header_dir AND location_onnxruntime_lib)
endif() endif()
endif() endif()
else() else()
message(STATUS "Could not find a pre-installed onnxruntime. Downloading pre-compiled onnxruntime") if(SHERPA_ONNX_USE_PRE_INSTALLED_ONNXRUNTIME_IF_AVAILABLE)
message(STATUS "Could not find a pre-installed onnxruntime.")
endif()
message(STATUS "Downloading pre-compiled onnxruntime")
download_onnxruntime() download_onnxruntime()
endif() endif()

View File

@@ -2,14 +2,16 @@
// //
// Copyright (c) 2023-2024 Xiaomi Corporation // Copyright (c) 2023-2024 Xiaomi Corporation
#include "sherpa-onnx/csrc/transducer-keyword-decoder.h"
#include <algorithm> #include <algorithm>
#include <cmath> #include <cmath>
#include <cstring>
#include <utility> #include <utility>
#include <vector> #include <vector>
#include "sherpa-onnx/csrc/log.h" #include "sherpa-onnx/csrc/log.h"
#include "sherpa-onnx/csrc/onnx-utils.h" #include "sherpa-onnx/csrc/onnx-utils.h"
#include "sherpa-onnx/csrc/transducer-keyword-decoder.h"
namespace sherpa_onnx { namespace sherpa_onnx {