Anders Xiao
Committed by GitHub

fix dml with preinstall ort (#2066)

@@ -37,6 +37,8 @@ option(SHERPA_ONNX_ENABLE_C_API "Whether to build C API" ON) @@ -37,6 +37,8 @@ option(SHERPA_ONNX_ENABLE_C_API "Whether to build C API" ON)
37 option(SHERPA_ONNX_ENABLE_WEBSOCKET "Whether to build webscoket server/client" ON) 37 option(SHERPA_ONNX_ENABLE_WEBSOCKET "Whether to build webscoket server/client" ON)
38 option(SHERPA_ONNX_ENABLE_GPU "Enable ONNX Runtime GPU support" OFF) 38 option(SHERPA_ONNX_ENABLE_GPU "Enable ONNX Runtime GPU support" OFF)
39 option(SHERPA_ONNX_ENABLE_DIRECTML "Enable ONNX Runtime DirectML support" OFF) 39 option(SHERPA_ONNX_ENABLE_DIRECTML "Enable ONNX Runtime DirectML support" OFF)
  40 +option(SHERPA_ONNX_LINK_D3D "Whether static ONNX runtime lib with DML" OFF)
  41 +
40 option(SHERPA_ONNX_ENABLE_WASM "Whether to enable WASM" OFF) 42 option(SHERPA_ONNX_ENABLE_WASM "Whether to enable WASM" OFF)
41 option(SHERPA_ONNX_ENABLE_WASM_SPEAKER_DIARIZATION "Whether to enable WASM for speaker diarization" OFF) 43 option(SHERPA_ONNX_ENABLE_WASM_SPEAKER_DIARIZATION "Whether to enable WASM for speaker diarization" OFF)
42 option(SHERPA_ONNX_ENABLE_WASM_TTS "Whether to enable WASM for TTS" OFF) 44 option(SHERPA_ONNX_ENABLE_WASM_TTS "Whether to enable WASM for TTS" OFF)
@@ -165,6 +167,7 @@ message(STATUS "SHERPA_ONNX_USE_PRE_INSTALLED_ONNXRUNTIME_IF_AVAILABLE ${SHERPA_ @@ -165,6 +167,7 @@ message(STATUS "SHERPA_ONNX_USE_PRE_INSTALLED_ONNXRUNTIME_IF_AVAILABLE ${SHERPA_
165 message(STATUS "SHERPA_ONNX_ENABLE_SANITIZER: ${SHERPA_ONNX_ENABLE_SANITIZER}") 167 message(STATUS "SHERPA_ONNX_ENABLE_SANITIZER: ${SHERPA_ONNX_ENABLE_SANITIZER}")
166 message(STATUS "SHERPA_ONNX_BUILD_C_API_EXAMPLES: ${SHERPA_ONNX_BUILD_C_API_EXAMPLES}") 168 message(STATUS "SHERPA_ONNX_BUILD_C_API_EXAMPLES: ${SHERPA_ONNX_BUILD_C_API_EXAMPLES}")
167 message(STATUS "SHERPA_ONNX_ENABLE_RKNN: ${SHERPA_ONNX_ENABLE_RKNN}") 169 message(STATUS "SHERPA_ONNX_ENABLE_RKNN: ${SHERPA_ONNX_ENABLE_RKNN}")
  170 +message(STATUS "SHERPA_ONNX_LINK_D3D: ${SHERPA_ONNX_LINK_D3D}")
168 171
169 if(BUILD_SHARED_LIBS OR SHERPA_ONNX_ENABLE_JNI) 172 if(BUILD_SHARED_LIBS OR SHERPA_ONNX_ENABLE_JNI)
170 set(CMAKE_CXX_VISIBILITY_PRESET hidden) 173 set(CMAKE_CXX_VISIBILITY_PRESET hidden)
@@ -311,6 +314,7 @@ if(WIN32) @@ -311,6 +314,7 @@ if(WIN32)
311 add_definitions(-DNOMINMAX) # Otherwise, std::max() and std::min() won't work 314 add_definitions(-DNOMINMAX) # Otherwise, std::max() and std::min() won't work
312 endif() 315 endif()
313 316
  317 +
314 if(WIN32 AND MSVC) 318 if(WIN32 AND MSVC)
315 # disable various warnings for MSVC 319 # disable various warnings for MSVC
316 # 4244: 'return': conversion from 'unsigned __int64' to 'int', possible loss of data 320 # 4244: 'return': conversion from 'unsigned __int64' to 'int', possible loss of data
@@ -19,21 +19,25 @@ if(NOT SHERPA_ONNX_ENABLE_DIRECTML) @@ -19,21 +19,25 @@ if(NOT SHERPA_ONNX_ENABLE_DIRECTML)
19 message(FATAL_ERROR "This file is for DirectML. Given SHERPA_ONNX_ENABLE_DIRECTML: ${SHERPA_ONNX_ENABLE_DIRECTML}") 19 message(FATAL_ERROR "This file is for DirectML. Given SHERPA_ONNX_ENABLE_DIRECTML: ${SHERPA_ONNX_ENABLE_DIRECTML}")
20 endif() 20 endif()
21 21
22 -set(onnxruntime_URL "https://globalcdn.nuget.org/packages/microsoft.ml.onnxruntime.directml.1.14.1.nupkg")  
23 -set(onnxruntime_URL2 "https://hf-mirror.com/csukuangfj/sherpa-onnx-cmake-deps/resolve/main/microsoft.ml.onnxruntime.directml.1.14.1.nupkg")  
24 -set(onnxruntime_HASH "SHA256=c8ae7623385b19cd5de968d0df5383e13b97d1b3a6771c9177eac15b56013a5a")  
25 -  
26 -# If you don't have access to the Internet,  
27 -# please download onnxruntime to one of the following locations.  
28 -# You can add more if you want.  
29 -set(possible_file_locations 22 +if(location_onnxruntime_header_dir AND location_onnxruntime_lib)
  23 + message("Use preinstall onnxruntime with directml: ${location_onnxruntime_lib}")
  24 +else()
  25 +
  26 + set(onnxruntime_URL "https://globalcdn.nuget.org/packages/microsoft.ml.onnxruntime.directml.1.14.1.nupkg")
  27 + set(onnxruntime_URL2 "https://hf-mirror.com/csukuangfj/sherpa-onnx-cmake-deps/resolve/main/microsoft.ml.onnxruntime.directml.1.14.1.nupkg")
  28 + set(onnxruntime_HASH "SHA256=c8ae7623385b19cd5de968d0df5383e13b97d1b3a6771c9177eac15b56013a5a")
  29 +
  30 + # If you don't have access to the Internet,
  31 + # please download onnxruntime to one of the following locations.
  32 + # You can add more if you want.
  33 + set(possible_file_locations
30 $ENV{HOME}/Downloads/microsoft.ml.onnxruntime.directml.1.14.1.nupkg 34 $ENV{HOME}/Downloads/microsoft.ml.onnxruntime.directml.1.14.1.nupkg
31 ${PROJECT_SOURCE_DIR}/microsoft.ml.onnxruntime.directml.1.14.1.nupkg 35 ${PROJECT_SOURCE_DIR}/microsoft.ml.onnxruntime.directml.1.14.1.nupkg
32 ${PROJECT_BINARY_DIR}/microsoft.ml.onnxruntime.directml.1.14.1.nupkg 36 ${PROJECT_BINARY_DIR}/microsoft.ml.onnxruntime.directml.1.14.1.nupkg
33 /tmp/microsoft.ml.onnxruntime.directml.1.14.1.nupkg 37 /tmp/microsoft.ml.onnxruntime.directml.1.14.1.nupkg
34 -) 38 + )
35 39
36 -foreach(f IN LISTS possible_file_locations) 40 + foreach(f IN LISTS possible_file_locations)
37 if(EXISTS ${f}) 41 if(EXISTS ${f})
38 set(onnxruntime_URL "${f}") 42 set(onnxruntime_URL "${f}")
39 file(TO_CMAKE_PATH "${onnxruntime_URL}" onnxruntime_URL) 43 file(TO_CMAKE_PATH "${onnxruntime_URL}" onnxruntime_URL)
@@ -41,58 +45,60 @@ foreach(f IN LISTS possible_file_locations) @@ -41,58 +45,60 @@ foreach(f IN LISTS possible_file_locations)
41 set(onnxruntime_URL2) 45 set(onnxruntime_URL2)
42 break() 46 break()
43 endif() 47 endif()
44 -endforeach() 48 + endforeach()
45 49
46 -FetchContent_Declare(onnxruntime 50 + FetchContent_Declare(onnxruntime
47 URL 51 URL
48 ${onnxruntime_URL} 52 ${onnxruntime_URL}
49 ${onnxruntime_URL2} 53 ${onnxruntime_URL2}
50 URL_HASH ${onnxruntime_HASH} 54 URL_HASH ${onnxruntime_HASH}
51 -) 55 + )
52 56
53 -FetchContent_GetProperties(onnxruntime)  
54 -if(NOT onnxruntime_POPULATED) 57 + FetchContent_GetProperties(onnxruntime)
  58 + if(NOT onnxruntime_POPULATED)
55 message(STATUS "Downloading onnxruntime from ${onnxruntime_URL}") 59 message(STATUS "Downloading onnxruntime from ${onnxruntime_URL}")
56 FetchContent_Populate(onnxruntime) 60 FetchContent_Populate(onnxruntime)
57 -endif()  
58 -message(STATUS "onnxruntime is downloaded to ${onnxruntime_SOURCE_DIR}") 61 + endif()
  62 + message(STATUS "onnxruntime is downloaded to ${onnxruntime_SOURCE_DIR}")
59 63
60 -find_library(location_onnxruntime onnxruntime 64 + find_library(location_onnxruntime onnxruntime
61 PATHS 65 PATHS
62 "${onnxruntime_SOURCE_DIR}/runtimes/win-x64/native" 66 "${onnxruntime_SOURCE_DIR}/runtimes/win-x64/native"
63 NO_CMAKE_SYSTEM_PATH 67 NO_CMAKE_SYSTEM_PATH
64 -) 68 + )
65 69
66 -message(STATUS "location_onnxruntime: ${location_onnxruntime}") 70 + message(STATUS "location_onnxruntime: ${location_onnxruntime}")
67 71
68 -add_library(onnxruntime SHARED IMPORTED) 72 + add_library(onnxruntime SHARED IMPORTED)
69 73
70 -set_target_properties(onnxruntime PROPERTIES 74 + set_target_properties(onnxruntime PROPERTIES
71 IMPORTED_LOCATION ${location_onnxruntime} 75 IMPORTED_LOCATION ${location_onnxruntime}
72 INTERFACE_INCLUDE_DIRECTORIES "${onnxruntime_SOURCE_DIR}/build/native/include" 76 INTERFACE_INCLUDE_DIRECTORIES "${onnxruntime_SOURCE_DIR}/build/native/include"
73 -) 77 + )
74 78
75 -set_property(TARGET onnxruntime 79 + set_property(TARGET onnxruntime
76 PROPERTY 80 PROPERTY
77 IMPORTED_IMPLIB "${onnxruntime_SOURCE_DIR}/runtimes/win-x64/native/onnxruntime.lib" 81 IMPORTED_IMPLIB "${onnxruntime_SOURCE_DIR}/runtimes/win-x64/native/onnxruntime.lib"
78 -) 82 + )
79 83
80 -file(COPY ${onnxruntime_SOURCE_DIR}/runtimes/win-x64/native/onnxruntime.dll 84 + file(COPY ${onnxruntime_SOURCE_DIR}/runtimes/win-x64/native/onnxruntime.dll
81 DESTINATION 85 DESTINATION
82 ${CMAKE_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE} 86 ${CMAKE_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE}
83 -) 87 + )
84 88
85 -file(GLOB onnxruntime_lib_files "${onnxruntime_SOURCE_DIR}/runtimes/win-x64/native/onnxruntime.*") 89 + file(GLOB onnxruntime_lib_files "${onnxruntime_SOURCE_DIR}/runtimes/win-x64/native/onnxruntime.*")
86 90
87 -message(STATUS "onnxruntime lib files: ${onnxruntime_lib_files}") 91 + message(STATUS "onnxruntime lib files: ${onnxruntime_lib_files}")
88 92
89 -if(SHERPA_ONNX_ENABLE_PYTHON) 93 + if(SHERPA_ONNX_ENABLE_PYTHON)
90 install(FILES ${onnxruntime_lib_files} DESTINATION ..) 94 install(FILES ${onnxruntime_lib_files} DESTINATION ..)
91 -else() 95 + else()
92 install(FILES ${onnxruntime_lib_files} DESTINATION lib) 96 install(FILES ${onnxruntime_lib_files} DESTINATION lib)
93 -endif() 97 + endif()
94 98
95 -install(FILES ${onnxruntime_lib_files} DESTINATION bin) 99 + install(FILES ${onnxruntime_lib_files} DESTINATION bin)
  100 +
  101 +endif()
96 102
97 # Setup DirectML 103 # Setup DirectML
98 104
@@ -154,6 +154,9 @@ if(SHERPA_ONNX_USE_PRE_INSTALLED_ONNXRUNTIME_IF_AVAILABLE) @@ -154,6 +154,9 @@ if(SHERPA_ONNX_USE_PRE_INSTALLED_ONNXRUNTIME_IF_AVAILABLE)
154 set(location_onnxruntime_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime.dylib) 154 set(location_onnxruntime_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime.dylib)
155 elseif(WIN32) 155 elseif(WIN32)
156 set(location_onnxruntime_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/onnxruntime.lib) 156 set(location_onnxruntime_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/onnxruntime.lib)
  157 + if(SHERPA_ONNX_ENABLE_DIRECTML)
  158 + include(onnxruntime-win-x64-directml)
  159 + endif()
157 else() 160 else()
158 set(location_onnxruntime_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime.so) 161 set(location_onnxruntime_lib $ENV{SHERPA_ONNXRUNTIME_LIB_DIR}/libonnxruntime.so)
159 endif() 162 endif()
@@ -216,6 +216,16 @@ endif() @@ -216,6 +216,16 @@ endif()
216 # Always static build 216 # Always static build
217 add_library(sherpa-onnx-core STATIC ${sources}) 217 add_library(sherpa-onnx-core STATIC ${sources})
218 218
  219 +
  220 +if(WIN32 AND SHERPA_ONNX_LINK_D3D)
  221 + target_link_libraries(sherpa-onnx-core dxguid.lib d3d12.lib dxgi.lib dxcore.lib)
  222 +endif()
  223 +
  224 +
  225 +if(TARGET directml)
  226 + target_link_libraries(sherpa-onnx-core directml)
  227 +endif()
  228 +
219 set_target_properties( 229 set_target_properties(
220 sherpa-onnx-core 230 sherpa-onnx-core
221 PROPERTIES 231 PROPERTIES