mirror of
https://github.com/saymrwulf/onnxruntime.git
synced 2026-05-14 20:48:00 +00:00
Fix delay load for WebGPU EP and DML EP (#23111)
### Description This change fixes the DLL delay load problem for the WebGPU EP and DirectML EP. See detailed explanation below. ### Problem When onnxruntime.dll uses delay loading for its dependencies, the dependencies are loaded using `LoadLibraryEx()`, which search the directory of process (.exe) instead of this library (onnxruntime.dll). This is a problem for usages of Node.js binding and python binding, because Windows will try to find the dependencies in the directory of node.exe or python.exe, which is not the directory of onnxruntime.dll. There was previous attempt to fix this by loading DirectML.dll in the initialization of onnxruntime nodejs binding, which works for DML EP but is not a good solution because it does not really "delay" the load. For WebGPU, the situation became worse because webgpu_dawn.dll depends on dxil.dll and dxcompiler.dll, which are explicitly dynamically loaded in the code using `LoadLibraryA()`. This has the same problem of the DLL search. ### Solutions For onnxruntime.dll loading its direct dependencies, it can be resolved by set the [`__pfnDliNotifyHook2` hook](https://learn.microsoft.com/en-us/cpp/build/reference/understanding-the-helper-function?view=msvc-170#structure-and-constant-definitions) to load from an absolute path that constructed from the onnxruntime.dll folder and the DLL name. For webgpu_dawn.dll loading dxil.dll and dxcompiler.dll, since they are explicitly loaded in the code, the hook does not work. Instead, it can be resolved by ~~using WIN32 API `SetDllDirectory()` to add the onnxruntime.dll folder to the search path.~~ preloading the 2 DLLs from the onnxruntime.dll folder .
This commit is contained in:
parent
780735098d
commit
8680244ebc
16 changed files with 324 additions and 66 deletions
|
|
@ -77,6 +77,7 @@ if(WIN32)
|
|||
onnxruntime_add_shared_library(onnxruntime
|
||||
${SYMBOL_FILE}
|
||||
"${ONNXRUNTIME_ROOT}/core/dll/dllmain.cc"
|
||||
"${ONNXRUNTIME_ROOT}/core/dll/delay_load_hook.cc"
|
||||
"${ONNXRUNTIME_ROOT}/core/dll/onnxruntime.rc"
|
||||
)
|
||||
elseif(onnxruntime_BUILD_APPLE_FRAMEWORK)
|
||||
|
|
|
|||
|
|
@ -60,15 +60,26 @@ else()
|
|||
endif()
|
||||
endif()
|
||||
|
||||
# a list of DLLs that the Node.js binding depends on
|
||||
set(NODEJS_DLL_DEPS)
|
||||
|
||||
# setup providers
|
||||
if (onnxruntime_USE_CUDA)
|
||||
set(NODEJS_BINDING_USE_CUDA "--use_cuda")
|
||||
endif()
|
||||
if (onnxruntime_USE_DML)
|
||||
set(NODEJS_BINDING_USE_DML "--use_dml")
|
||||
list(APPEND NODEJS_DLL_DEPS "$<TARGET_FILE_DIR:onnxruntime>/DirectML.dll")
|
||||
endif()
|
||||
if (onnxruntime_USE_WEBGPU)
|
||||
set(NODEJS_BINDING_USE_WEBGPU "--use_webgpu")
|
||||
if (WIN32 AND onnxruntime_ENABLE_DAWN_BACKEND_D3D12)
|
||||
list(APPEND NODEJS_DLL_DEPS "$<TARGET_FILE_DIR:dxcompiler>/dxil.dll")
|
||||
list(APPEND NODEJS_DLL_DEPS "$<TARGET_FILE_DIR:dxcompiler>/dxcompiler.dll")
|
||||
endif()
|
||||
if (onnxruntime_BUILD_DAWN_MONOLITHIC_LIBRARY)
|
||||
list(APPEND NODEJS_DLL_DEPS "$<TARGET_FILE:dawn::webgpu_dawn>")
|
||||
endif()
|
||||
endif()
|
||||
if (onnxruntime_USE_TENSORRT)
|
||||
set(NODEJS_BINDING_USE_TENSORRT "--use_tensorrt")
|
||||
|
|
@ -94,9 +105,12 @@ add_custom_target(js_common_npm_ci ALL
|
|||
|
||||
add_custom_target(nodejs_binding_wrapper ALL
|
||||
COMMAND ${NPM_CLI} ci
|
||||
COMMAND ${NPM_CLI} run build -- --onnxruntime-build-dir=${CMAKE_CURRENT_BINARY_DIR} --config=${CMAKE_BUILD_TYPE} --onnxruntime-generator=${CMAKE_GENERATOR}
|
||||
--arch=${NODEJS_BINDING_ARCH} ${NODEJS_BINDING_USE_CUDA} ${NODEJS_BINDING_USE_DML} ${NODEJS_BINDING_USE_WEBGPU} ${NODEJS_BINDING_USE_TENSORRT}
|
||||
${NODEJS_BINDING_USE_COREML} ${NODEJS_BINDING_USE_QNN}
|
||||
COMMAND ${NPM_CLI} run build -- "--onnxruntime-build-dir=${CMAKE_CURRENT_BINARY_DIR}"
|
||||
--config=${CMAKE_BUILD_TYPE}
|
||||
"--onnxruntime-generator=${CMAKE_GENERATOR}"
|
||||
"--dll_deps=${NODEJS_DLL_DEPS}"
|
||||
--arch=${NODEJS_BINDING_ARCH} ${NODEJS_BINDING_USE_CUDA} ${NODEJS_BINDING_USE_DML} ${NODEJS_BINDING_USE_WEBGPU}
|
||||
${NODEJS_BINDING_USE_TENSORRT} ${NODEJS_BINDING_USE_COREML} ${NODEJS_BINDING_USE_QNN}
|
||||
WORKING_DIRECTORY ${JS_NODE_ROOT}
|
||||
COMMENT "Using cmake-js to build OnnxRuntime Node.js binding")
|
||||
|
||||
|
|
|
|||
|
|
@ -23,19 +23,18 @@
|
|||
onnxruntime_add_include_to_target(onnxruntime_providers_webgpu
|
||||
onnxruntime_common dawn::dawncpp_headers dawn::dawn_headers onnx onnx_proto flatbuffers::flatbuffers Boost::mp11 safeint_interface)
|
||||
|
||||
set(onnxruntime_providers_webgpu_dll_deps)
|
||||
|
||||
if (onnxruntime_BUILD_DAWN_MONOLITHIC_LIBRARY)
|
||||
target_link_libraries(onnxruntime_providers_webgpu dawn::webgpu_dawn)
|
||||
|
||||
if (onnxruntime_ENABLE_DELAY_LOADING_WIN_DLLS)
|
||||
list(APPEND onnxruntime_DELAYLOAD_FLAGS "/DELAYLOAD:webgpu_dawn.dll")
|
||||
endif()
|
||||
if (WIN32)
|
||||
if (onnxruntime_ENABLE_DELAY_LOADING_WIN_DLLS)
|
||||
list(APPEND onnxruntime_DELAYLOAD_FLAGS "/DELAYLOAD:webgpu_dawn.dll")
|
||||
endif()
|
||||
|
||||
# Copy webgpu_dawn.dll to the output directory
|
||||
add_custom_command(
|
||||
TARGET onnxruntime_providers_webgpu
|
||||
POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different "$<TARGET_FILE:dawn::webgpu_dawn>" "$<TARGET_FILE_DIR:onnxruntime_providers_webgpu>"
|
||||
VERBATIM )
|
||||
list(APPEND onnxruntime_providers_webgpu_dll_deps "$<TARGET_FILE:dawn::webgpu_dawn>")
|
||||
endif()
|
||||
else()
|
||||
if (NOT onnxruntime_USE_EXTERNAL_DAWN)
|
||||
target_link_libraries(onnxruntime_providers_webgpu dawn::dawn_native)
|
||||
|
|
@ -43,4 +42,23 @@
|
|||
target_link_libraries(onnxruntime_providers_webgpu dawn::dawn_proc)
|
||||
endif()
|
||||
|
||||
if (WIN32 AND onnxruntime_ENABLE_DAWN_BACKEND_D3D12)
|
||||
# Ensure dxil.dll and dxcompiler.dll exist in the output directory $<TARGET_FILE_DIR:dxcompiler>
|
||||
add_dependencies(onnxruntime_providers_webgpu copy_dxil_dll)
|
||||
add_dependencies(onnxruntime_providers_webgpu dxcompiler)
|
||||
|
||||
list(APPEND onnxruntime_providers_webgpu_dll_deps "$<TARGET_FILE_DIR:dxcompiler>/dxil.dll")
|
||||
list(APPEND onnxruntime_providers_webgpu_dll_deps "$<TARGET_FILE_DIR:dxcompiler>/dxcompiler.dll")
|
||||
endif()
|
||||
|
||||
if (onnxruntime_providers_webgpu_dll_deps)
|
||||
# Copy dependency DLLs to the output directory
|
||||
add_custom_command(
|
||||
TARGET onnxruntime_providers_webgpu
|
||||
POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different "${onnxruntime_providers_webgpu_dll_deps}" "$<TARGET_FILE_DIR:onnxruntime_providers_webgpu>"
|
||||
COMMAND_EXPAND_LISTS
|
||||
VERBATIM )
|
||||
endif()
|
||||
|
||||
set_target_properties(onnxruntime_providers_webgpu PROPERTIES FOLDER "ONNXRuntime")
|
||||
|
|
|
|||
|
|
@ -525,6 +525,9 @@ set (onnxruntime_global_thread_pools_test_SRC
|
|||
set (onnxruntime_webgpu_external_dawn_test_SRC
|
||||
${TEST_SRC_DIR}/webgpu/external_dawn/main.cc)
|
||||
|
||||
set (onnxruntime_webgpu_delay_load_test_SRC
|
||||
${TEST_SRC_DIR}/webgpu/delay_load/main.cc)
|
||||
|
||||
# tests from lowest level library up.
|
||||
# the order of libraries should be maintained, with higher libraries being added first in the list
|
||||
|
||||
|
|
@ -1864,4 +1867,13 @@ if (onnxruntime_USE_WEBGPU AND onnxruntime_USE_EXTERNAL_DAWN)
|
|||
onnxruntime_add_include_to_target(onnxruntime_webgpu_external_dawn_test dawn::dawncpp_headers dawn::dawn_headers)
|
||||
endif()
|
||||
|
||||
if (onnxruntime_USE_WEBGPU AND WIN32 AND onnxruntime_BUILD_SHARED_LIB AND NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten" AND NOT onnxruntime_MINIMAL_BUILD)
|
||||
AddTest(DYN
|
||||
TARGET onnxruntime_webgpu_delay_load_test
|
||||
SOURCES ${onnxruntime_webgpu_delay_load_test_SRC}
|
||||
LIBS ${SYS_PATH_LIB}
|
||||
DEPENDS ${all_dependencies}
|
||||
)
|
||||
endif()
|
||||
|
||||
include(onnxruntime_fuzz_test.cmake)
|
||||
|
|
|
|||
|
|
@ -113,10 +113,12 @@ endif()
|
|||
if (WIN32)
|
||||
file(COPY ${ONNXRUNTIME_WIN_BIN_DIR}/onnxruntime.dll
|
||||
DESTINATION ${dist_folder})
|
||||
if (USE_DML)
|
||||
file(COPY ${ONNXRUNTIME_WIN_BIN_DIR}/DirectML.dll
|
||||
DESTINATION ${dist_folder})
|
||||
endif ()
|
||||
if (ORT_NODEJS_DLL_DEPS)
|
||||
foreach(dll ${ORT_NODEJS_DLL_DEPS})
|
||||
file(COPY ${dll} DESTINATION ${dist_folder})
|
||||
endforeach()
|
||||
endif()
|
||||
|
||||
elseif (APPLE)
|
||||
file(COPY ${ONNXRUNTIME_BUILD_DIR}/libonnxruntime.dylib
|
||||
DESTINATION ${dist_folder} FOLLOW_SYMLINK_CHAIN)
|
||||
|
|
|
|||
|
|
@ -39,6 +39,8 @@ const USE_TENSORRT = !!buildArgs.use_tensorrt;
|
|||
const USE_COREML = !!buildArgs.use_coreml;
|
||||
// --use_qnn
|
||||
const USE_QNN = !!buildArgs.use_qnn;
|
||||
// --dll_deps=
|
||||
const DLL_DEPS = buildArgs.dll_deps;
|
||||
|
||||
// build path
|
||||
const ROOT_FOLDER = path.join(__dirname, '..');
|
||||
|
|
@ -82,6 +84,9 @@ if (USE_COREML) {
|
|||
if (USE_QNN) {
|
||||
args.push('--CDUSE_QNN=ON');
|
||||
}
|
||||
if (DLL_DEPS) {
|
||||
args.push(`--CDORT_NODEJS_DLL_DEPS=${DLL_DEPS}`);
|
||||
}
|
||||
|
||||
// set CMAKE_OSX_ARCHITECTURES for macOS build
|
||||
if (os.platform() === 'darwin') {
|
||||
|
|
|
|||
|
|
@ -1,37 +0,0 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#ifdef _WIN32
|
||||
#include "common.h"
|
||||
#include "windows.h"
|
||||
|
||||
void LoadDirectMLDll(Napi::Env env) {
|
||||
DWORD pathLen = MAX_PATH;
|
||||
std::wstring path(pathLen, L'\0');
|
||||
HMODULE moduleHandle = nullptr;
|
||||
|
||||
GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
|
||||
reinterpret_cast<LPCSTR>(&LoadDirectMLDll), &moduleHandle);
|
||||
|
||||
DWORD getModuleFileNameResult = GetModuleFileNameW(moduleHandle, const_cast<wchar_t*>(path.c_str()), pathLen);
|
||||
while (getModuleFileNameResult == 0 || getModuleFileNameResult == pathLen) {
|
||||
int ret = GetLastError();
|
||||
if (ret == ERROR_INSUFFICIENT_BUFFER && pathLen < 32768) {
|
||||
pathLen *= 2;
|
||||
path.resize(pathLen);
|
||||
getModuleFileNameResult = GetModuleFileNameW(moduleHandle, const_cast<wchar_t*>(path.c_str()), pathLen);
|
||||
} else {
|
||||
ORT_NAPI_THROW_ERROR(env, "Failed getting path to load DirectML.dll, error code: ", ret);
|
||||
}
|
||||
}
|
||||
|
||||
path.resize(path.rfind(L'\\') + 1);
|
||||
path.append(L"DirectML.dll");
|
||||
HMODULE libraryLoadResult = LoadLibraryW(path.c_str());
|
||||
|
||||
if (!libraryLoadResult) {
|
||||
int ret = GetLastError();
|
||||
ORT_NAPI_THROW_ERROR(env, "Failed loading bundled DirectML.dll, error code: ", ret);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#if defined(USE_DML) && defined(_WIN32)
|
||||
void LoadDirectMLDll(Napi::Env env);
|
||||
#endif
|
||||
|
|
@ -4,7 +4,6 @@
|
|||
#include "onnxruntime_cxx_api.h"
|
||||
|
||||
#include "common.h"
|
||||
#include "directml_load_helper.h"
|
||||
#include "inference_session_wrap.h"
|
||||
#include "run_options_helper.h"
|
||||
#include "session_options_helper.h"
|
||||
|
|
@ -19,9 +18,6 @@ Napi::FunctionReference& InferenceSessionWrap::GetTensorConstructor() {
|
|||
}
|
||||
|
||||
Napi::Object InferenceSessionWrap::Init(Napi::Env env, Napi::Object exports) {
|
||||
#if defined(USE_DML) && defined(_WIN32)
|
||||
LoadDirectMLDll(env);
|
||||
#endif
|
||||
// create ONNX runtime env
|
||||
Ort::InitApi();
|
||||
ORT_NAPI_THROW_ERROR_IF(
|
||||
|
|
|
|||
83
onnxruntime/core/dll/delay_load_hook.cc
Normal file
83
onnxruntime/core/dll/delay_load_hook.cc
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
// == workaround for delay loading of dependencies of onnxruntime.dll ==
|
||||
//
|
||||
// Problem:
|
||||
//
|
||||
// When onnxruntime.dll uses delay loading for its dependencies, the dependencies are loaded using LoadLibraryEx,
|
||||
// which search the directory of process (.exe) instead of this library (onnxruntime.dll). This is a problem for
|
||||
// usages of Node.js binding and python binding, because Windows will try to find the dependencies in the directory
|
||||
// of node.exe or python.exe, which is not the directory of onnxruntime.dll.
|
||||
//
|
||||
// Solution:
|
||||
//
|
||||
// By using the delay load hook `__pfnDliNotifyHook2`, we can intervene the loading procedure by loading from an
|
||||
// absolute path. The absolute path is constructed by appending the name of the DLL to load to the directory of
|
||||
// onnxruntime.dll. This way, we can ensure that the dependencies are loaded from the same directory as onnxruntime.dll.
|
||||
//
|
||||
// See also:
|
||||
// - https://learn.microsoft.com/en-us/cpp/build/reference/understanding-the-helper-function?view=msvc-170#structure-and-constant-definitions
|
||||
// - https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order#alternate-search-order-for-unpackaged-apps
|
||||
//
|
||||
// The DLL DelayLoad hook is only enabled when the compiler is MSVC and at least one of the following is True:
|
||||
// - both USE_WEBGPU and BUILD_DAWN_MONOLITHIC_LIBRARY are defined
|
||||
// - USE_DML is defined
|
||||
//
|
||||
#define ORT_DELAY_LOAD_WEBGPU_DAWN_DLL (defined(USE_WEBGPU) && defined(BUILD_DAWN_MONOLITHIC_LIBRARY))
|
||||
#define ORT_DELAY_LOAD_DIRECTML_DLL defined(USE_DML)
|
||||
#if defined(_MSC_VER) && (ORT_DELAY_LOAD_WEBGPU_DAWN_DLL || ORT_DELAY_LOAD_DIRECTML_DLL)
|
||||
|
||||
#include <Windows.h>
|
||||
#include <delayimp.h>
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
|
||||
#include "core/platform/env.h"
|
||||
|
||||
namespace {
|
||||
|
||||
#define DEFINE_KNOWN_DLL(name) {#name ".dll", L#name L".dll"}
|
||||
|
||||
constexpr struct {
|
||||
const char* str;
|
||||
const wchar_t* wstr;
|
||||
} known_dlls[] = {
|
||||
#if ORT_DELAY_LOAD_WEBGPU_DAWN_DLL
|
||||
DEFINE_KNOWN_DLL(webgpu_dawn),
|
||||
#endif
|
||||
#if ORT_DELAY_LOAD_DIRECTML_DLL
|
||||
DEFINE_KNOWN_DLL(DirectML),
|
||||
#endif
|
||||
};
|
||||
} // namespace
|
||||
|
||||
FARPROC WINAPI delay_load_hook(unsigned dliNotify, PDelayLoadInfo pdli) {
|
||||
if (dliNotify == dliNotePreLoadLibrary) {
|
||||
for (size_t i = 0; i < _countof(known_dlls); ++i) {
|
||||
if (_stricmp(pdli->szDll, known_dlls[i].str) == 0) {
|
||||
// Try to load the DLL from the same directory as onnxruntime.dll
|
||||
|
||||
// First, get the path to onnxruntime.dll
|
||||
auto path = Env::Default().GetRuntimePath();
|
||||
if (path.empty()) {
|
||||
// Failed to get the path to onnxruntime.dll. In this case, we will just return NULL and let the system
|
||||
// search for the DLL in the default search order.
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Append the name of the DLL. Now `path` is the absolute path to the DLL to load.
|
||||
path.append(known_dlls[i].wstr);
|
||||
|
||||
// Load the DLL
|
||||
return FARPROC(LoadLibraryExW(path.c_str(), NULL,
|
||||
LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR));
|
||||
}
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
extern "C" const PfnDliHook __pfnDliNotifyHook2 = delay_load_hook;
|
||||
|
||||
#endif
|
||||
|
|
@ -13,7 +13,7 @@
|
|||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
// dllmain.cpp : Defines the entry point for the DLL application.
|
||||
// dllmain.cc : Defines the entry point for the DLL application.
|
||||
BOOL APIENTRY DllMain(HMODULE /*hModule*/,
|
||||
DWORD ul_reason_for_call,
|
||||
LPVOID /*lpReserved*/
|
||||
|
|
|
|||
|
|
@ -10,6 +10,8 @@
|
|||
#endif
|
||||
|
||||
#include "core/common/common.h"
|
||||
#include "core/common/path_string.h"
|
||||
#include "core/platform/env.h"
|
||||
|
||||
#include "core/providers/webgpu/compute_context.h"
|
||||
#include "core/providers/webgpu/webgpu_context.h"
|
||||
|
|
@ -50,6 +52,30 @@ void WebGpuContext::Initialize(const WebGpuExecutionProviderInfo& webgpu_ep_info
|
|||
|
||||
// Initialization.Step.2 - Create wgpu::Adapter
|
||||
if (adapter_ == nullptr) {
|
||||
#if !defined(__EMSCRIPTEN__) && defined(_MSC_VER) && defined(DAWN_ENABLE_D3D12) && !defined(USE_EXTERNAL_DAWN)
|
||||
// If we are using the D3D12 backend on Windows and the build does not use external Dawn, dxil.dll and dxcompiler.dll are required.
|
||||
//
|
||||
// Dawn will try to load them later, but if they are in the different directory to the executable, it may fail to find them.
|
||||
// To avoid this issue, we try to load them from the same directory as current module (usually onnxruntime.dll).
|
||||
auto runtime_path = Env::Default().GetRuntimePath();
|
||||
if (!runtime_path.empty()) {
|
||||
Status status;
|
||||
void* module_handle = nullptr;
|
||||
|
||||
PathString dxil_path = runtime_path + ToPathString(L"dxil.dll");
|
||||
status = Env::Default().LoadDynamicLibrary(dxil_path, false, &module_handle);
|
||||
if (status.IsOK() && module_handle != nullptr) {
|
||||
modules_.Add(dxil_path, module_handle);
|
||||
}
|
||||
|
||||
PathString dxcompiler_path = runtime_path + ToPathString(L"dxcompiler.dll");
|
||||
status = Env::Default().LoadDynamicLibrary(dxcompiler_path, false, &module_handle);
|
||||
if (status.IsOK() && module_handle != nullptr) {
|
||||
modules_.Add(dxcompiler_path, module_handle);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
wgpu::RequestAdapterOptions req_adapter_options = {};
|
||||
wgpu::DawnTogglesDescriptor adapter_toggles_desc = {};
|
||||
req_adapter_options.nextInChain = &adapter_toggles_desc;
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@
|
|||
#include <webgpu/webgpu_cpp.h>
|
||||
|
||||
#include "core/common/common.h"
|
||||
#include "core/framework/library_handles.h"
|
||||
#include "core/providers/webgpu/webgpu_execution_provider.h"
|
||||
#include "core/providers/webgpu/buffer_manager.h"
|
||||
#include "core/providers/webgpu/program_manager.h"
|
||||
|
|
@ -153,6 +154,8 @@ class WebGpuContext final {
|
|||
|
||||
std::once_flag init_flag_;
|
||||
|
||||
LibraryHandles modules_;
|
||||
|
||||
wgpu::Instance instance_;
|
||||
wgpu::Adapter adapter_;
|
||||
wgpu::Device device_;
|
||||
|
|
|
|||
142
onnxruntime/test/webgpu/delay_load/main.cc
Normal file
142
onnxruntime/test/webgpu/delay_load/main.cc
Normal file
|
|
@ -0,0 +1,142 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#include <iostream>
|
||||
#include <Windows.h>
|
||||
#include <stdlib.h>
|
||||
#include <filesystem>
|
||||
#define ORT_API_MANUAL_INIT
|
||||
#include "core/session/onnxruntime_cxx_api.h"
|
||||
|
||||
// This program is to test the delay loading of onnxruntime.dll.
|
||||
//
|
||||
// To verify the delay loading actually works, we need to do the test in 2 steps:
|
||||
//
|
||||
// 1. Prepare a folder structure like below:
|
||||
//
|
||||
// ├── webgpu_delay_load_test_root (newly created folder)
|
||||
// │ ├── dlls
|
||||
// │ │ ├── onnxruntime.dll
|
||||
// │ │ ├── webgpu_dawn.dll
|
||||
// │ │ ├── dxil.dll
|
||||
// │ │ └── dxcompiler.dll
|
||||
// │ └── test.exe
|
||||
// └── onnxruntime_webgpu_delay_load_test.exe (this binary)
|
||||
//
|
||||
// This folder structure ensures no DLLs are in the same folder as the executable (test.exe).
|
||||
//
|
||||
// 2. Launch the test binary from the root folder of the above structure.
|
||||
//
|
||||
// So, there are 2 modes of this program:
|
||||
// 1. "Prepare" mode: Do the step 1 above. (default)
|
||||
// 2. "Test" mode: Do the step 2 above. (specified by --test argument)
|
||||
|
||||
int prepare_main();
|
||||
int test_main();
|
||||
|
||||
int wmain(int argc, wchar_t* argv[]) {
|
||||
if (argc == 2 && wcscmp(argv[1], L"--test") == 0) {
|
||||
return test_main();
|
||||
} else {
|
||||
return prepare_main();
|
||||
}
|
||||
}
|
||||
|
||||
int prepare_main() {
|
||||
std::wstring path_str(32768, L'\0');
|
||||
GetModuleFileNameW(NULL, path_str.data(), static_cast<DWORD>(path_str.size()));
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
fs::path exe_full_path{path_str}; // <TEST_DIR>/onnxruntime_webgpu_delay_load_test.exe
|
||||
fs::path test_dir = exe_full_path.parent_path(); // <TEST_DIR>/
|
||||
fs::path exe_name = exe_full_path.filename(); // onnxruntime_webgpu_delay_load_test.exe
|
||||
fs::path root_folder = test_dir / L"webgpu_delay_load_test_root\\"; // <TEST_DIR>/webgpu_delay_load_test_root/
|
||||
fs::path dlls_folder = root_folder / L"dlls\\"; // <TEST_DIR>/webgpu_delay_load_test_root/dlls/
|
||||
|
||||
// ensure the test folder exists and is empty
|
||||
if (fs::exists(root_folder)) {
|
||||
fs::remove_all(root_folder);
|
||||
}
|
||||
fs::create_directories(dlls_folder);
|
||||
|
||||
fs::current_path(test_dir);
|
||||
|
||||
// copy the required DLLs to the dlls folder
|
||||
fs::copy_file(L"onnxruntime.dll", dlls_folder / L"onnxruntime.dll");
|
||||
fs::copy_file(L"dxil.dll", dlls_folder / L"dxil.dll");
|
||||
fs::copy_file(L"dxcompiler.dll", dlls_folder / L"dxcompiler.dll");
|
||||
if (fs::exists(L"webgpu_dawn.dll")) {
|
||||
fs::copy_file(L"webgpu_dawn.dll", dlls_folder / L"webgpu_dawn.dll");
|
||||
}
|
||||
|
||||
// copy the test binary to the root folder
|
||||
fs::copy_file(exe_full_path, root_folder / L"test.exe");
|
||||
|
||||
// run "test.exe --test" from the test root folder
|
||||
fs::current_path(root_folder);
|
||||
return _wsystem(L"test.exe --test");
|
||||
}
|
||||
|
||||
int run() {
|
||||
Ort::Env env{nullptr};
|
||||
int retval = 0;
|
||||
try {
|
||||
env = Ort::Env{ORT_LOGGING_LEVEL_WARNING, "Default"};
|
||||
|
||||
// model is https://github.com/onnx/onnx/blob/v1.15.0/onnx/backend/test/data/node/test_abs/model.onnx
|
||||
constexpr uint8_t MODEL_DATA[] = {8, 7, 18, 12, 98, 97, 99, 107, 101, 110,
|
||||
100, 45, 116, 101, 115, 116, 58, 73, 10, 11,
|
||||
10, 1, 120, 18, 1, 121, 34, 3, 65, 98,
|
||||
115, 18, 8, 116, 101, 115, 116, 95, 97, 98,
|
||||
115, 90, 23, 10, 1, 120, 18, 18, 10, 16,
|
||||
8, 1, 18, 12, 10, 2, 8, 3, 10, 2,
|
||||
8, 4, 10, 2, 8, 5, 98, 23, 10, 1,
|
||||
121, 18, 18, 10, 16, 8, 1, 18, 12, 10,
|
||||
2, 8, 3, 10, 2, 8, 4, 10, 2, 8,
|
||||
5, 66, 4, 10, 0, 16, 13};
|
||||
|
||||
Ort::SessionOptions session_options;
|
||||
session_options.DisableMemPattern();
|
||||
std::unordered_map<std::string, std::string> provider_options;
|
||||
session_options.AppendExecutionProvider("WebGPU", provider_options);
|
||||
Ort::Session session{env, MODEL_DATA, sizeof(MODEL_DATA), session_options};
|
||||
|
||||
// successfully initialized
|
||||
std::cout << "Successfully initialized WebGPU EP." << std::endl;
|
||||
retval = 0;
|
||||
} catch (const std::exception& ex) {
|
||||
std::cerr << ex.what() << std::endl;
|
||||
|
||||
std::cerr << "Unexpected exception." << std::endl;
|
||||
retval = -1;
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
int test_main() {
|
||||
HMODULE hModule = LoadLibraryA("dlls\\onnxruntime.dll");
|
||||
if (hModule == NULL) {
|
||||
std::cout << "Failed to load dlls\\onnxruntime.dll" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int retval = 0;
|
||||
|
||||
using OrtGetApiBaseFunction = decltype(&OrtGetApiBase);
|
||||
auto fnOrtGetApiBase = (OrtGetApiBaseFunction)GetProcAddress(hModule, "OrtGetApiBase");
|
||||
if (fnOrtGetApiBase == NULL) {
|
||||
std::cout << "Failed to get OrtGetApiBase" << std::endl;
|
||||
retval = 1;
|
||||
goto cleanup;
|
||||
}
|
||||
Ort::InitApi(fnOrtGetApiBase()->GetApi(ORT_API_VERSION));
|
||||
|
||||
retval = run();
|
||||
|
||||
cleanup:
|
||||
if (hModule != NULL) {
|
||||
FreeLibrary(hModule);
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
|
@ -1,5 +1,4 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// SPDX-FileCopyrightText: Copyright 2024 Arm Limited and/or its affiliates <open-source-office@arm.com>
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#include <iostream>
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ stages:
|
|||
--enable_pybind
|
||||
--build_nodejs
|
||||
--use_webgpu
|
||||
--cmake_extra_defines onnxruntime_BUILD_UNIT_TESTS=ON
|
||||
--cmake_extra_defines onnxruntime_BUILD_UNIT_TESTS=ON onnxruntime_BUILD_DAWN_MONOLITHIC_LIBRARY=ON
|
||||
msbuildPlatform: x64
|
||||
isX86: false
|
||||
job_name_suffix: x64_RelWithDebInfo
|
||||
|
|
|
|||
Loading…
Reference in a new issue