Skip to content

Commit

Permalink
Allow to not use pre-installed onnxruntime libs. (#636)
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj authored Mar 6, 2024
1 parent 13260cd commit bdf9243
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 48 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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_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_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_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
Expand Down Expand Up @@ -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_ASR ${SHERPA_ONNX_ENABLE_WASM_ASR}")
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(NOT SHERPA_ONNX_ENABLE_WASM)
Expand Down
100 changes: 53 additions & 47 deletions cmake/onnxruntime.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -117,67 +117,69 @@ function(download_onnxruntime)
set(onnxruntime_SOURCE_DIR ${onnxruntime_SOURCE_DIR} PARENT_SCOPE)
endfunction()

# First, we try to locate the header and the lib if the use has already
# installed onnxruntime. Otherwise, we will download the pre-compiled lib
if(SHERPA_ONNX_USE_PRE_INSTALLED_ONNXRUNTIME_IF_AVAILABLE)
# 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_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")

if(DEFINED ENV{SHERPA_ONNXRUNTIME_INCLUDE_DIR})
set(location_onnxruntime_header_dir $ENV{SHERPA_ONNXRUNTIME_INCLUDE_DIR})

include_directories(${location_onnxruntime_header_dir})
else()
find_path(location_onnxruntime_header_dir onnxruntime_cxx_api.h
PATHS
/usr/include
/usr/local/include
)
endif()
message(STATUS "CMAKE_SYSTEM_NAME: ${CMAKE_SYSTEM_NAME}")
message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")

message(STATUS "location_onnxruntime_header_dir: ${location_onnxruntime_header_dir}")
if(DEFINED ENV{SHERPA_ONNXRUNTIME_INCLUDE_DIR})
set(location_onnxruntime_header_dir $ENV{SHERPA_ONNXRUNTIME_INCLUDE_DIR})

if(DEFINED ENV{SHERPA_ONNXRUNTIME_LIB_DIR})
if(APPLE)
set(location_onnxruntime_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime.dylib)
include_directories(${location_onnxruntime_header_dir})
else()
set(location_onnxruntime_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime.so)
find_path(location_onnxruntime_header_dir onnxruntime_cxx_api.h
PATHS
/usr/include
/usr/local/include
)
endif()
if(NOT EXISTS ${location_onnxruntime_lib})
set(location_onnxruntime_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime.a)

message(STATUS "location_onnxruntime_header_dir: ${location_onnxruntime_header_dir}")

if(DEFINED ENV{SHERPA_ONNXRUNTIME_LIB_DIR})
if(APPLE)
set(location_onnxruntime_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime.dylib)
else()
set(location_onnxruntime_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime.so)
endif()
if(NOT EXISTS ${location_onnxruntime_lib})
message(FATAL_ERROR "${location_onnxruntime_lib} cannot be found")
set(location_onnxruntime_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime.a)
if(NOT EXISTS ${location_onnxruntime_lib})
message(FATAL_ERROR "${location_onnxruntime_lib} cannot be found")
endif()
set(onnxruntime_lib_files $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime.a)
message("Use static lib: ${onnxruntime_lib_files}")
endif()
set(onnxruntime_lib_files $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime.a)
message("Use static lib: ${onnxruntime_lib_files}")
endif()
if(SHERPA_ONNX_ENABLE_GPU)
set(location_onnxruntime_cuda_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime_providers_cuda.so)
if(NOT EXISTS ${location_onnxruntime_cuda_lib})
set(location_onnxruntime_cuda_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime_providers_cuda.a)
if(SHERPA_ONNX_ENABLE_GPU)
set(location_onnxruntime_cuda_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime_providers_cuda.so)
if(NOT EXISTS ${location_onnxruntime_cuda_lib})
set(location_onnxruntime_cuda_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime_providers_cuda.a)
endif()
endif()
endif()
else()
find_library(location_onnxruntime_lib onnxruntime
PATHS
/lib
/usr/lib
/usr/local/lib
)

if(SHERPA_ONNX_ENABLE_GPU)
find_library(location_onnxruntime_cuda_lib onnxruntime_providers_cuda
else()
find_library(location_onnxruntime_lib onnxruntime
PATHS
/lib
/usr/lib
/usr/local/lib
)

if(SHERPA_ONNX_ENABLE_GPU)
find_library(location_onnxruntime_cuda_lib onnxruntime_providers_cuda
PATHS
/lib
/usr/lib
/usr/local/lib
)
endif()
endif()
endif()

message(STATUS "location_onnxruntime_lib: ${location_onnxruntime_lib}")
if(SHERPA_ONNX_ENABLE_GPU)
message(STATUS "location_onnxruntime_cuda_lib: ${location_onnxruntime_cuda_lib}")
message(STATUS "location_onnxruntime_lib: ${location_onnxruntime_lib}")
if(SHERPA_ONNX_ENABLE_GPU)
message(STATUS "location_onnxruntime_cuda_lib: ${location_onnxruntime_cuda_lib}")
endif()
endif()

if(location_onnxruntime_header_dir AND location_onnxruntime_lib)
Expand All @@ -195,6 +197,10 @@ if(location_onnxruntime_header_dir AND location_onnxruntime_lib)
endif()
endif()
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()
endif()
4 changes: 3 additions & 1 deletion sherpa-onnx/csrc/transducer-keyword-decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
//
// Copyright (c) 2023-2024 Xiaomi Corporation

#include "sherpa-onnx/csrc/transducer-keyword-decoder.h"

#include <algorithm>
#include <cmath>
#include <cstring>
#include <utility>
#include <vector>

#include "sherpa-onnx/csrc/log.h"
#include "sherpa-onnx/csrc/onnx-utils.h"
#include "sherpa-onnx/csrc/transducer-keyword-decoder.h"

namespace sherpa_onnx {

Expand Down

0 comments on commit bdf9243

Please sign in to comment.