Fangjun Kuang
Committed by GitHub

Fix building for GPU support. (#195)

@@ -58,6 +58,15 @@ if(SHERPA_ONNX_ENABLE_JNI AND NOT BUILD_SHARED_LIBS) @@ -58,6 +58,15 @@ if(SHERPA_ONNX_ENABLE_JNI AND NOT BUILD_SHARED_LIBS)
58 set(BUILD_SHARED_LIBS ON CACHE BOOL "" FORCE) 58 set(BUILD_SHARED_LIBS ON CACHE BOOL "" FORCE)
59 endif() 59 endif()
60 60
  61 +if(SHERPA_ONNX_ENABLE_GPU)
  62 + message(WARNING "\
  63 +Compiling for NVIDIA GPU is enabled. Please make sure cudatoolkit
  64 +is installed on your system. Otherwise, you will get errors at runtime.
  65 +Hint: You don't need sudo permission to install CUDA toolkit. Please refer to
  66 + https://k2-fsa.github.io/k2/installation/cuda-cudnn.html
  67 +to install CUDA toolkit if you have not installed it.")
  68 +endif()
  69 +
61 if(BUILD_SHARED_LIBS AND MSVC) 70 if(BUILD_SHARED_LIBS AND MSVC)
62 set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) 71 set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
63 endif() 72 endif()
@@ -19,6 +19,7 @@ function(download_asio) @@ -19,6 +19,7 @@ function(download_asio)
19 if(EXISTS ${f}) 19 if(EXISTS ${f})
20 set(asio_URL "${f}") 20 set(asio_URL "${f}")
21 file(TO_CMAKE_PATH "${asio_URL}" asio_URL) 21 file(TO_CMAKE_PATH "${asio_URL}" asio_URL)
  22 + message(STATUS "Found local downloaded asio: ${asio_URL}")
22 set(asio_URL2) 23 set(asio_URL2)
23 break() 24 break()
24 endif() 25 endif()
@@ -18,6 +18,7 @@ function(download_cargs) @@ -18,6 +18,7 @@ function(download_cargs)
18 if(EXISTS ${f}) 18 if(EXISTS ${f})
19 set(cargs_URL "${f}") 19 set(cargs_URL "${f}")
20 file(TO_CMAKE_PATH "${cargs_URL}" cargs_URL) 20 file(TO_CMAKE_PATH "${cargs_URL}" cargs_URL)
  21 + message(STATUS "Found local downloaded cargs: ${cargs_URL}")
21 break() 22 break()
22 endif() 23 endif()
23 endforeach() 24 endforeach()
@@ -19,6 +19,7 @@ function(download_googltest) @@ -19,6 +19,7 @@ function(download_googltest)
19 if(EXISTS ${f}) 19 if(EXISTS ${f})
20 set(googletest_URL "${f}") 20 set(googletest_URL "${f}")
21 file(TO_CMAKE_PATH "${googletest_URL}" googletest_URL) 21 file(TO_CMAKE_PATH "${googletest_URL}" googletest_URL)
  22 + message(STATUS "Found local downloaded googletest: ${googletest_URL}")
22 set(googletest_URL2) 23 set(googletest_URL2)
23 break() 24 break()
24 endif() 25 endif()
@@ -19,6 +19,7 @@ function(download_json) @@ -19,6 +19,7 @@ function(download_json)
19 if(EXISTS ${f}) 19 if(EXISTS ${f})
20 set(json_URL "${f}") 20 set(json_URL "${f}")
21 file(TO_CMAKE_PATH "${json_URL}" json_URL) 21 file(TO_CMAKE_PATH "${json_URL}" json_URL)
  22 + message(STATUS "Found local downloaded json: ${json_URL}")
22 set(json_URL2) 23 set(json_URL2)
23 break() 24 break()
24 endif() 25 endif()
@@ -23,6 +23,7 @@ function(download_kaldi_native_fbank) @@ -23,6 +23,7 @@ function(download_kaldi_native_fbank)
23 if(EXISTS ${f}) 23 if(EXISTS ${f})
24 set(kaldi_native_fbank_URL "${f}") 24 set(kaldi_native_fbank_URL "${f}")
25 file(TO_CMAKE_PATH "${kaldi_native_fbank_URL}" kaldi_native_fbank_URL) 25 file(TO_CMAKE_PATH "${kaldi_native_fbank_URL}" kaldi_native_fbank_URL)
  26 + message(STATUS "Found local downloaded kaldi-native-fbank: ${kaldi_native_fbank_URL}")
26 set(kaldi_native_fbank_URL2 ) 27 set(kaldi_native_fbank_URL2 )
27 break() 28 break()
28 endif() 29 endif()
@@ -34,7 +34,17 @@ function(download_onnxruntime) @@ -34,7 +34,17 @@ function(download_onnxruntime)
34 # ./include 34 # ./include
35 # It contains all the needed header files 35 # It contains all the needed header files
36 if(SHERPA_ONNX_ENABLE_GPU) 36 if(SHERPA_ONNX_ENABLE_GPU)
37 - set(onnxruntime_URL "https://github.com/microsoft/onnxruntime/releases/download/v1.14.1/onnxruntime-linux-x64-gpu-1.14.1.tgz") 37 + set(onnxruntime_URL "https://github.com/microsoft/onnxruntime/releases/download/v1.14.0/onnxruntime-linux-x64-gpu-1.14.0.tgz")
  38 + set(onnxruntime_URL2 "https://huggingface.co/csukuangfj/sherpa-onnx-cmake-deps/resolve/main/onnxruntime-linux-x64-gpu-1.14.0.tgz")
  39 + set(onnxruntime_HASH "SHA256=d28fd59be62b9749071e2997c237b42f8e52661ae4d12862f77aa934750ead21")
  40 +
  41 + set(possible_file_locations
  42 + $ENV{HOME}/Downloads/onnxruntime-linux-x64-gpu-1.14.0.tgz
  43 + ${PROJECT_SOURCE_DIR}/onnxruntime-linux-x64-gpu-1.14.0.tgz
  44 + ${PROJECT_BINARY_DIR}/onnxruntime-linux-x64-gpu-1.14.0.tgz
  45 + /tmp/onnxruntime-linux-x64-gpu-1.14.0.tgz
  46 + /star-fj/fangjun/download/github/onnxruntime-linux-x64-gpu-1.14.0.tgz
  47 + )
38 endif() 48 endif()
39 # After downloading, it contains: 49 # After downloading, it contains:
40 # ./lib/libonnxruntime.so.1.14.1 50 # ./lib/libonnxruntime.so.1.14.1
@@ -77,6 +87,10 @@ function(download_onnxruntime) @@ -77,6 +87,10 @@ function(download_onnxruntime)
77 set(onnxruntime_URL "https://github.com/microsoft/onnxruntime/releases/download/v1.14.0/onnxruntime-win-x86-1.14.0.zip") 87 set(onnxruntime_URL "https://github.com/microsoft/onnxruntime/releases/download/v1.14.0/onnxruntime-win-x86-1.14.0.zip")
78 set(onnxruntime_URL2 "https://huggingface.co/csukuangfj/sherpa-onnx-cmake-deps/resolve/main/onnxruntime-win-x86-1.14.0.zip") 88 set(onnxruntime_URL2 "https://huggingface.co/csukuangfj/sherpa-onnx-cmake-deps/resolve/main/onnxruntime-win-x86-1.14.0.zip")
79 set(onnxruntime_HASH "SHA256=4214b130db602cbf31a6f26f25377ab077af0cf03c4ddd4651283e1fb68f56cf") 89 set(onnxruntime_HASH "SHA256=4214b130db602cbf31a6f26f25377ab077af0cf03c4ddd4651283e1fb68f56cf")
  90 +
  91 + if(SHERPA_ONNX_ENABLE_GPU)
  92 + message(FATAL_ERROR "GPU support for Win32 is not supported!")
  93 + endif()
80 else() 94 else()
81 # If you don't have access to the Internet, 95 # If you don't have access to the Internet,
82 # please pre-download onnxruntime 96 # please pre-download onnxruntime
@@ -92,6 +106,18 @@ function(download_onnxruntime) @@ -92,6 +106,18 @@ function(download_onnxruntime)
92 set(onnxruntime_URL "https://github.com/microsoft/onnxruntime/releases/download/v1.14.0/onnxruntime-win-x64-1.14.0.zip") 106 set(onnxruntime_URL "https://github.com/microsoft/onnxruntime/releases/download/v1.14.0/onnxruntime-win-x64-1.14.0.zip")
93 set(onnxruntime_URL2 "https://huggingface.co/csukuangfj/sherpa-onnx-cmake-deps/resolve/main/onnxruntime-win-x64-1.14.0.zip") 107 set(onnxruntime_URL2 "https://huggingface.co/csukuangfj/sherpa-onnx-cmake-deps/resolve/main/onnxruntime-win-x64-1.14.0.zip")
94 set(onnxruntime_HASH "SHA256=300eafef456748cde2743ee08845bd40ff1bab723697ff934eba6d4ce3519620") 108 set(onnxruntime_HASH "SHA256=300eafef456748cde2743ee08845bd40ff1bab723697ff934eba6d4ce3519620")
  109 +
  110 + if(SHERPA_ONNX_ENABLE_GPU)
  111 + set(onnxruntime_URL "https://github.com/microsoft/onnxruntime/releases/download/v1.14.0/onnxruntime-win-x64-gpu-1.14.0.zip")
  112 + set(onnxruntime_URL2 "https://huggingface.co/csukuangfj/sherpa-onnx-cmake-deps/resolve/main/onnxruntime-win-x64-gpu-1.14.0.zip")
  113 + set(onnxruntime_HASH "SHA256=b42aac412ec96db92c182b9c8b02190da00072a5efc4adcbecf9b62e933c30d3")
  114 + set(possible_file_locations
  115 + $ENV{HOME}/Downloads/onnxruntime-win-x64-gpu-1.14.0.zip
  116 + ${PROJECT_SOURCE_DIR}/onnxruntime-win-x64-gpu-1.14.0.zip
  117 + ${PROJECT_BINARY_DIR}/onnxruntime-win-x64-gpu-1.14.0.zip
  118 + /tmp/onnxruntime-win-x64-gpu-1.14.0.zip
  119 + )
  120 + endif()
95 endif() 121 endif()
96 # After downloading, it contains: 122 # After downloading, it contains:
97 # ./lib/onnxruntime.{dll,lib,pdb} 123 # ./lib/onnxruntime.{dll,lib,pdb}
@@ -105,28 +131,22 @@ function(download_onnxruntime) @@ -105,28 +131,22 @@ function(download_onnxruntime)
105 message(FATAL_ERROR "Only support Linux, macOS, and Windows at present. Will support other OSes later") 131 message(FATAL_ERROR "Only support Linux, macOS, and Windows at present. Will support other OSes later")
106 endif() 132 endif()
107 133
108 - if(NOT SHERPA_ONNX_ENABLE_GPU)  
109 - foreach(f IN LISTS possible_file_locations)  
110 - if(EXISTS ${f})  
111 - set(onnxruntime_URL "${f}")  
112 - file(TO_CMAKE_PATH "${onnxruntime_URL}" onnxruntime_URL)  
113 - set(onnxruntime_URL2)  
114 - break()  
115 - endif()  
116 - endforeach() 134 + foreach(f IN LISTS possible_file_locations)
  135 + if(EXISTS ${f})
  136 + set(onnxruntime_URL "${f}")
  137 + file(TO_CMAKE_PATH "${onnxruntime_URL}" onnxruntime_URL)
  138 + message(STATUS "Found local downloaded onnxruntime: ${onnxruntime_URL}")
  139 + set(onnxruntime_URL2)
  140 + break()
  141 + endif()
  142 + endforeach()
117 143
118 - FetchContent_Declare(onnxruntime  
119 - URL  
120 - ${onnxruntime_URL}  
121 - ${onnxruntime_URL2}  
122 - URL_HASH ${onnxruntime_HASH}  
123 - )  
124 - else()  
125 - FetchContent_Declare(onnxruntime  
126 - URL  
127 - ${onnxruntime_URL}  
128 - )  
129 - endif() 144 + FetchContent_Declare(onnxruntime
  145 + URL
  146 + ${onnxruntime_URL}
  147 + ${onnxruntime_URL2}
  148 + URL_HASH ${onnxruntime_HASH}
  149 + )
130 150
131 FetchContent_GetProperties(onnxruntime) 151 FetchContent_GetProperties(onnxruntime)
132 if(NOT onnxruntime_POPULATED) 152 if(NOT onnxruntime_POPULATED)
@@ -150,7 +170,7 @@ function(download_onnxruntime) @@ -150,7 +170,7 @@ function(download_onnxruntime)
150 INTERFACE_INCLUDE_DIRECTORIES "${onnxruntime_SOURCE_DIR}/include" 170 INTERFACE_INCLUDE_DIRECTORIES "${onnxruntime_SOURCE_DIR}/include"
151 ) 171 )
152 172
153 - if(SHERPA_ONNX_ENABLE_GPU) 173 + if(SHERPA_ONNX_ENABLE_GPU AND NOT WIN32)
154 find_library(location_onnxruntime_cuda_lib onnxruntime_providers_cuda 174 find_library(location_onnxruntime_cuda_lib onnxruntime_providers_cuda
155 PATHS 175 PATHS
156 "${onnxruntime_SOURCE_DIR}/lib" 176 "${onnxruntime_SOURCE_DIR}/lib"
@@ -172,6 +192,24 @@ function(download_onnxruntime) @@ -172,6 +192,24 @@ function(download_onnxruntime)
172 DESTINATION 192 DESTINATION
173 ${CMAKE_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE} 193 ${CMAKE_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE}
174 ) 194 )
  195 + if(SHERPA_ONNX_ENABLE_GPU)
  196 + add_library(onnxruntime_providers_cuda SHARED IMPORTED)
  197 +
  198 + set_target_properties(onnxruntime_providers_cuda PROPERTIES
  199 + IMPORTED_LOCATION ${location_onnxruntime}
  200 + INTERFACE_INCLUDE_DIRECTORIES "${onnxruntime_SOURCE_DIR}/include"
  201 + )
  202 +
  203 + set_property(TARGET onnxruntime_providers_cuda
  204 + PROPERTY
  205 + IMPORTED_IMPLIB "${onnxruntime_SOURCE_DIR}/lib/onnxruntime_providers_cuda.lib"
  206 + )
  207 +
  208 + file(COPY ${onnxruntime_SOURCE_DIR}/lib/onnxruntime_providers_cuda.dll
  209 + DESTINATION
  210 + ${CMAKE_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE}
  211 + )
  212 + endif()
175 endif() 213 endif()
176 214
177 if(UNIX AND NOT APPLE) 215 if(UNIX AND NOT APPLE)
@@ -20,6 +20,7 @@ function(download_portaudio) @@ -20,6 +20,7 @@ function(download_portaudio)
20 if(EXISTS ${f}) 20 if(EXISTS ${f})
21 set(portaudio_URL "${f}") 21 set(portaudio_URL "${f}")
22 file(TO_CMAKE_PATH "${portaudio_URL}" portaudio_URL) 22 file(TO_CMAKE_PATH "${portaudio_URL}" portaudio_URL)
  23 + message(STATUS "Found local downloaded portaudio: ${portaudio_URL}")
23 set(portaudio_URL2) 24 set(portaudio_URL2)
24 break() 25 break()
25 endif() 26 endif()
@@ -19,6 +19,7 @@ function(download_pybind11) @@ -19,6 +19,7 @@ function(download_pybind11)
19 if(EXISTS ${f}) 19 if(EXISTS ${f})
20 set(pybind11_URL "${f}") 20 set(pybind11_URL "${f}")
21 file(TO_CMAKE_PATH "${pybind11_URL}" pybind11_URL) 21 file(TO_CMAKE_PATH "${pybind11_URL}" pybind11_URL)
  22 + message(STATUS "Found local downloaded pybind11: ${pybind11_URL}")
22 set(pybind11_URL2) 23 set(pybind11_URL2)
23 break() 24 break()
24 endif() 25 endif()
@@ -20,6 +20,7 @@ function(download_websocketpp) @@ -20,6 +20,7 @@ function(download_websocketpp)
20 if(EXISTS ${f}) 20 if(EXISTS ${f})
21 set(websocketpp_URL "${f}") 21 set(websocketpp_URL "${f}")
22 file(TO_CMAKE_PATH "${websocketpp_URL}" websocketpp_URL) 22 file(TO_CMAKE_PATH "${websocketpp_URL}" websocketpp_URL)
  23 + message(STATUS "Found local downloaded websocketpp: ${websocketpp_URL}")
23 set(websocketpp_URL2) 24 set(websocketpp_URL2)
24 break() 25 break()
25 endif() 26 endif()