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,81 +19,87 @@ if(NOT SHERPA_ONNX_ENABLE_DIRECTML) @@ -19,81 +19,87 @@ 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  
30 - $ENV{HOME}/Downloads/microsoft.ml.onnxruntime.directml.1.14.1.nupkg  
31 - ${PROJECT_SOURCE_DIR}/microsoft.ml.onnxruntime.directml.1.14.1.nupkg  
32 - ${PROJECT_BINARY_DIR}/microsoft.ml.onnxruntime.directml.1.14.1.nupkg  
33 - /tmp/microsoft.ml.onnxruntime.directml.1.14.1.nupkg  
34 -)  
35 -  
36 -foreach(f IN LISTS possible_file_locations)  
37 - if(EXISTS ${f})  
38 - set(onnxruntime_URL "${f}")  
39 - file(TO_CMAKE_PATH "${onnxruntime_URL}" onnxruntime_URL)  
40 - message(STATUS "Found local downloaded onnxruntime: ${onnxruntime_URL}")  
41 - set(onnxruntime_URL2)  
42 - break()  
43 - endif()  
44 -endforeach()  
45 -  
46 -FetchContent_Declare(onnxruntime  
47 - URL  
48 - ${onnxruntime_URL}  
49 - ${onnxruntime_URL2}  
50 - URL_HASH ${onnxruntime_HASH}  
51 -)  
52 -  
53 -FetchContent_GetProperties(onnxruntime)  
54 -if(NOT onnxruntime_POPULATED)  
55 - message(STATUS "Downloading onnxruntime from ${onnxruntime_URL}")  
56 - FetchContent_Populate(onnxruntime)  
57 -endif()  
58 -message(STATUS "onnxruntime is downloaded to ${onnxruntime_SOURCE_DIR}")  
59 -  
60 -find_library(location_onnxruntime onnxruntime  
61 - PATHS  
62 - "${onnxruntime_SOURCE_DIR}/runtimes/win-x64/native"  
63 - NO_CMAKE_SYSTEM_PATH  
64 -)  
65 -  
66 -message(STATUS "location_onnxruntime: ${location_onnxruntime}")  
67 -  
68 -add_library(onnxruntime SHARED IMPORTED)  
69 -  
70 -set_target_properties(onnxruntime PROPERTIES  
71 - IMPORTED_LOCATION ${location_onnxruntime}  
72 - INTERFACE_INCLUDE_DIRECTORIES "${onnxruntime_SOURCE_DIR}/build/native/include"  
73 -)  
74 -  
75 -set_property(TARGET onnxruntime  
76 - PROPERTY  
77 - IMPORTED_IMPLIB "${onnxruntime_SOURCE_DIR}/runtimes/win-x64/native/onnxruntime.lib"  
78 -)  
79 -  
80 -file(COPY ${onnxruntime_SOURCE_DIR}/runtimes/win-x64/native/onnxruntime.dll  
81 - DESTINATION  
82 - ${CMAKE_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE}  
83 -)  
84 -  
85 -file(GLOB onnxruntime_lib_files "${onnxruntime_SOURCE_DIR}/runtimes/win-x64/native/onnxruntime.*") 22 +if(location_onnxruntime_header_dir AND location_onnxruntime_lib)
  23 + message("Use preinstall onnxruntime with directml: ${location_onnxruntime_lib}")
  24 +else()
86 25
87 -message(STATUS "onnxruntime lib files: ${onnxruntime_lib_files}") 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
  34 + $ENV{HOME}/Downloads/microsoft.ml.onnxruntime.directml.1.14.1.nupkg
  35 + ${PROJECT_SOURCE_DIR}/microsoft.ml.onnxruntime.directml.1.14.1.nupkg
  36 + ${PROJECT_BINARY_DIR}/microsoft.ml.onnxruntime.directml.1.14.1.nupkg
  37 + /tmp/microsoft.ml.onnxruntime.directml.1.14.1.nupkg
  38 + )
  39 +
  40 + foreach(f IN LISTS possible_file_locations)
  41 + if(EXISTS ${f})
  42 + set(onnxruntime_URL "${f}")
  43 + file(TO_CMAKE_PATH "${onnxruntime_URL}" onnxruntime_URL)
  44 + message(STATUS "Found local downloaded onnxruntime: ${onnxruntime_URL}")
  45 + set(onnxruntime_URL2)
  46 + break()
  47 + endif()
  48 + endforeach()
  49 +
  50 + FetchContent_Declare(onnxruntime
  51 + URL
  52 + ${onnxruntime_URL}
  53 + ${onnxruntime_URL2}
  54 + URL_HASH ${onnxruntime_HASH}
  55 + )
  56 +
  57 + FetchContent_GetProperties(onnxruntime)
  58 + if(NOT onnxruntime_POPULATED)
  59 + message(STATUS "Downloading onnxruntime from ${onnxruntime_URL}")
  60 + FetchContent_Populate(onnxruntime)
  61 + endif()
  62 + message(STATUS "onnxruntime is downloaded to ${onnxruntime_SOURCE_DIR}")
  63 +
  64 + find_library(location_onnxruntime onnxruntime
  65 + PATHS
  66 + "${onnxruntime_SOURCE_DIR}/runtimes/win-x64/native"
  67 + NO_CMAKE_SYSTEM_PATH
  68 + )
  69 +
  70 + message(STATUS "location_onnxruntime: ${location_onnxruntime}")
  71 +
  72 + add_library(onnxruntime SHARED IMPORTED)
  73 +
  74 + set_target_properties(onnxruntime PROPERTIES
  75 + IMPORTED_LOCATION ${location_onnxruntime}
  76 + INTERFACE_INCLUDE_DIRECTORIES "${onnxruntime_SOURCE_DIR}/build/native/include"
  77 + )
  78 +
  79 + set_property(TARGET onnxruntime
  80 + PROPERTY
  81 + IMPORTED_IMPLIB "${onnxruntime_SOURCE_DIR}/runtimes/win-x64/native/onnxruntime.lib"
  82 + )
  83 +
  84 + file(COPY ${onnxruntime_SOURCE_DIR}/runtimes/win-x64/native/onnxruntime.dll
  85 + DESTINATION
  86 + ${CMAKE_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE}
  87 + )
  88 +
  89 + file(GLOB onnxruntime_lib_files "${onnxruntime_SOURCE_DIR}/runtimes/win-x64/native/onnxruntime.*")
  90 +
  91 + message(STATUS "onnxruntime lib files: ${onnxruntime_lib_files}")
  92 +
  93 + if(SHERPA_ONNX_ENABLE_PYTHON)
  94 + install(FILES ${onnxruntime_lib_files} DESTINATION ..)
  95 + else()
  96 + install(FILES ${onnxruntime_lib_files} DESTINATION lib)
  97 + endif()
  98 +
  99 + install(FILES ${onnxruntime_lib_files} DESTINATION bin)
88 100
89 -if(SHERPA_ONNX_ENABLE_PYTHON)  
90 - install(FILES ${onnxruntime_lib_files} DESTINATION ..)  
91 -else()  
92 - install(FILES ${onnxruntime_lib_files} DESTINATION lib)  
93 endif() 101 endif()
94 102
95 -install(FILES ${onnxruntime_lib_files} DESTINATION bin)  
96 -  
97 # Setup DirectML 103 # Setup DirectML
98 104
99 set(directml_URL "https://www.nuget.org/api/v2/package/Microsoft.AI.DirectML/1.15.0") 105 set(directml_URL "https://www.nuget.org/api/v2/package/Microsoft.AI.DirectML/1.15.0")
@@ -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