Skip to content

Commit 0c6c816

Browse files
fs-eireguschmue
authored andcommitted
add 2 CMake build options of Dawn (#23096)
### Description This change adds the following CMake build options for Dawn: - onnxruntime_BUILD_DAWN_MONOLITHIC_LIBRARY - OFF by default - when enabled, builds Dawn as a monolithic library (webgpu_dawn.dll) - onnxruntime_ENABLE_DAWN_BACKEND_VULKAN - OFF by default - when enabled, build with Vulkan backend for Dawn on Windows - onnxruntime_ENABLE_DAWN_BACKEND_D3D12 - ON by default - when enabled, build with DirectX 12 backend for Dawn on Windows ### File Size Comparison (Windows) | Build | cmdline | File Size | |---|---|---| | Baseline | --config Release<br/> --build_shared_lib | `12,755,456 onnxruntime.dll` | | WebGPU D3D12 (default) | --use_webgpu<br/> --config Release<br/> --build_shared_lib | `17,082,368 dxcompiler.dll`<br/>` 1,508,472 dxil.dll`<br/>`18,708,480 onnxruntime.dll` | | WebGPU D3D12+Vulkan | --use_webgpu<br/> --config Release<br/> --build_shared_lib<br/> --cmake_extra_defines<br/> onnxruntime_ENABLE_DAWN_BACKEND_D3D12=1<br/> onnxruntime_ENABLE_DAWN_BACKEND_VULKAN=1 | `17,081,344 dxcompiler.dll`<br/>` 1,508,472 dxil.dll`<br/>`19,388,416 onnxruntime.dll` | | WebGPU Vulkan | --use_webgpu<br/> --config Release<br/> --build_shared_lib<br/> --cmake_extra_defines<br/> onnxruntime_ENABLE_DAWN_BACKEND_D3D12=0<br/> onnxruntime_ENABLE_DAWN_BACKEND_VULKAN=1 | `17,615,872 onnxruntime.dll` | | Monolithic | --use_webgpu<br/> --config Release<br/> --build_shared_lib<br/> --cmake_extra_defines<br/> onnxruntime_BUILD_DAWN_MONOLITHIC_LIBRARY=1 | `17,082,368 dxcompiler.dll`<br/>` 1,508,472 dxil.dll`<br/>`13,277,696 onnxruntime.dll`<br/>` 5,616,640 webgpu_dawn.dll` | | External Dawn | --use_webgpu<br/> --config Release<br/> --build_shared_lib<br/> --cmake_extra_defines<br/> onnxruntime_USE_EXTERNAL_DAWN=1<br/> --skip_tests | `17,081,344 dxcompiler.dll`<br/>` 1,508,472 dxil.dll`<br/>`13,277,184 onnxruntime.dll`
1 parent 129c382 commit 0c6c816

7 files changed

+97
-14
lines changed

cmake/CMakeLists.txt

+13
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ option(onnxruntime_USE_WEBNN "Build with WebNN support. Enable hardware accelera
149149
option(onnxruntime_USE_WEBGPU "Build with WebGPU support. Enable WebGPU via C/C++ interface." OFF)
150150
option(onnxruntime_USE_EXTERNAL_DAWN "Build with treating Dawn as external dependency. Will not link Dawn at build time." OFF)
151151
option(onnxruntime_CUSTOM_DAWN_SRC_PATH "Path to custom Dawn src dir.")
152+
option(onnxruntime_BUILD_DAWN_MONOLITHIC_LIBRARY "Build Dawn as a monolithic library" OFF)
153+
# The following 2 options are only for Windows
154+
option(onnxruntime_ENABLE_DAWN_BACKEND_VULKAN "Enable Vulkan backend for Dawn (on Windows)" OFF)
155+
option(onnxruntime_ENABLE_DAWN_BACKEND_D3D12 "Enable D3D12 backend for Dawn (on Windows)" ON)
152156

153157
# Options related to reducing the binary size produced by the build
154158
# XNNPACK EP requires the internal NHWC contrib ops to be available, so this option must be OFF when onnxruntime_USE_XNNPACK is ON
@@ -955,9 +959,18 @@ if (onnxruntime_USE_WEBGPU)
955959
list(APPEND ORT_PROVIDER_FLAGS -DUSE_WEBGPU=1)
956960
list(APPEND ORT_PROVIDER_CMAKE_FLAGS -Donnxruntime_USE_WEBGPU=1)
957961
list(APPEND ONNXRUNTIME_PROVIDER_NAMES webgpu)
962+
if (onnxruntime_BUILD_DAWN_MONOLITHIC_LIBRARY)
963+
list(APPEND ORT_PROVIDER_FLAGS -DBUILD_DAWN_MONOLITHIC_LIBRARY=1)
964+
endif()
958965
if (onnxruntime_USE_EXTERNAL_DAWN)
959966
list(APPEND ORT_PROVIDER_FLAGS -DUSE_EXTERNAL_DAWN=1)
960967
endif()
968+
if (onnxruntime_ENABLE_DAWN_BACKEND_VULKAN)
969+
list(APPEND ORT_PROVIDER_FLAGS -DDAWN_ENABLE_VULKAN=1)
970+
endif()
971+
if (onnxruntime_ENABLE_DAWN_BACKEND_D3D12)
972+
list(APPEND ORT_PROVIDER_FLAGS -DDAWN_ENABLE_D3D12=1)
973+
endif()
961974
endif()
962975
if (onnxruntime_USE_CANN)
963976
list(APPEND ORT_PROVIDER_FLAGS -DUSE_CANN=1)

cmake/external/onnxruntime_external_deps.cmake

+33-8
Original file line numberDiff line numberDiff line change
@@ -635,10 +635,19 @@ if (onnxruntime_USE_WEBGPU)
635635
)
636636
endif()
637637

638-
# use dawn::dawn_native and dawn::dawn_proc instead of the monolithic dawn::webgpu_dawn to minimize binary size
639-
set(DAWN_BUILD_MONOLITHIC_LIBRARY OFF CACHE BOOL "" FORCE)
638+
if (onnxruntime_BUILD_DAWN_MONOLITHIC_LIBRARY)
639+
set(DAWN_BUILD_MONOLITHIC_LIBRARY ON CACHE BOOL "" FORCE)
640+
set(DAWN_ENABLE_INSTALL ON CACHE BOOL "" FORCE)
641+
642+
if (onnxruntime_USE_EXTERNAL_DAWN)
643+
message(FATAL_ERROR "onnxruntime_USE_EXTERNAL_DAWN and onnxruntime_BUILD_DAWN_MONOLITHIC_LIBRARY cannot be enabled at the same time.")
644+
endif()
645+
else()
646+
# use dawn::dawn_native and dawn::dawn_proc instead of the monolithic dawn::webgpu_dawn to minimize binary size
647+
set(DAWN_BUILD_MONOLITHIC_LIBRARY OFF CACHE BOOL "" FORCE)
648+
set(DAWN_ENABLE_INSTALL OFF CACHE BOOL "" FORCE)
649+
endif()
640650
set(DAWN_BUILD_SAMPLES OFF CACHE BOOL "" FORCE)
641-
set(DAWN_ENABLE_INSTALL OFF CACHE BOOL "" FORCE)
642651
set(DAWN_ENABLE_NULL OFF CACHE BOOL "" FORCE)
643652
set(DAWN_FETCH_DEPENDENCIES ON CACHE BOOL "" FORCE)
644653

@@ -667,18 +676,34 @@ if (onnxruntime_USE_WEBGPU)
667676
set(DAWN_USE_BUILT_DXC ON CACHE BOOL "" FORCE)
668677
set(TINT_BUILD_HLSL_WRITER ON CACHE BOOL "" FORCE)
669678

670-
# Vulkan may optionally be included in a Windows build. Exclude until we have an explicit use case that requires it.
671-
set(DAWN_ENABLE_VULKAN OFF CACHE BOOL "" FORCE)
679+
if ((NOT onnxruntime_ENABLE_DAWN_BACKEND_VULKAN) AND (NOT onnxruntime_ENABLE_DAWN_BACKEND_D3D12))
680+
message(FATAL_ERROR "At least one of onnxruntime_ENABLE_DAWN_BACKEND_VULKAN or onnxruntime_ENABLE_DAWN_BACKEND_D3D12 must be enabled when using Dawn on Windows.")
681+
endif()
682+
if (onnxruntime_ENABLE_DAWN_BACKEND_VULKAN)
683+
set(DAWN_ENABLE_VULKAN ON CACHE BOOL "" FORCE)
684+
set(TINT_BUILD_SPV_WRITER ON CACHE BOOL "" FORCE)
685+
else()
686+
set(DAWN_ENABLE_VULKAN OFF CACHE BOOL "" FORCE)
687+
endif()
688+
if (onnxruntime_ENABLE_DAWN_BACKEND_D3D12)
689+
set(DAWN_ENABLE_D3D12 ON CACHE BOOL "" FORCE)
690+
else()
691+
set(DAWN_ENABLE_D3D12 OFF CACHE BOOL "" FORCE)
692+
endif()
672693
# We are currently always using the D3D12 backend.
673694
set(DAWN_ENABLE_D3D11 OFF CACHE BOOL "" FORCE)
674695
endif()
675696

676697
onnxruntime_fetchcontent_makeavailable(dawn)
677698

678-
if (NOT onnxruntime_USE_EXTERNAL_DAWN)
679-
list(APPEND onnxruntime_EXTERNAL_LIBRARIES dawn::dawn_native)
699+
if (onnxruntime_BUILD_DAWN_MONOLITHIC_LIBRARY)
700+
list(APPEND onnxruntime_EXTERNAL_LIBRARIES dawn::webgpu_dawn)
701+
else()
702+
if (NOT onnxruntime_USE_EXTERNAL_DAWN)
703+
list(APPEND onnxruntime_EXTERNAL_LIBRARIES dawn::dawn_native)
704+
endif()
705+
list(APPEND onnxruntime_EXTERNAL_LIBRARIES dawn::dawn_proc)
680706
endif()
681-
list(APPEND onnxruntime_EXTERNAL_LIBRARIES dawn::dawn_proc)
682707
endif()
683708

684709
set(onnxruntime_LINK_DIRS)

cmake/onnxruntime_providers_webgpu.cmake

+19-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,25 @@
2222
onnxruntime_add_static_library(onnxruntime_providers_webgpu ${onnxruntime_providers_webgpu_cc_srcs})
2323
onnxruntime_add_include_to_target(onnxruntime_providers_webgpu
2424
onnxruntime_common dawn::dawncpp_headers dawn::dawn_headers onnx onnx_proto flatbuffers::flatbuffers Boost::mp11 safeint_interface)
25-
if (NOT onnxruntime_USE_EXTERNAL_DAWN)
26-
target_link_libraries(onnxruntime_providers_webgpu dawn::dawn_native)
25+
26+
if (onnxruntime_BUILD_DAWN_MONOLITHIC_LIBRARY)
27+
target_link_libraries(onnxruntime_providers_webgpu dawn::webgpu_dawn)
28+
29+
if (onnxruntime_ENABLE_DELAY_LOADING_WIN_DLLS)
30+
list(APPEND onnxruntime_DELAYLOAD_FLAGS "/DELAYLOAD:webgpu_dawn.dll")
31+
endif()
32+
33+
# Copy webgpu_dawn.dll to the output directory
34+
add_custom_command(
35+
TARGET onnxruntime_providers_webgpu
36+
POST_BUILD
37+
COMMAND ${CMAKE_COMMAND} -E copy_if_different "$<TARGET_FILE:dawn::webgpu_dawn>" "$<TARGET_FILE_DIR:onnxruntime_providers_webgpu>"
38+
VERBATIM )
39+
else()
40+
if (NOT onnxruntime_USE_EXTERNAL_DAWN)
41+
target_link_libraries(onnxruntime_providers_webgpu dawn::dawn_native)
42+
endif()
43+
target_link_libraries(onnxruntime_providers_webgpu dawn::dawn_proc)
2744
endif()
28-
target_link_libraries(onnxruntime_providers_webgpu dawn::dawn_proc)
2945

3046
set_target_properties(onnxruntime_providers_webgpu PROPERTIES FOLDER "ONNXRuntime")

onnxruntime/core/providers/webgpu/webgpu_context.cc

+5-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ void WebGpuContext::Initialize(const WebGpuExecutionProviderInfo& webgpu_ep_info
2828
// Initialization.Step.1 - Create wgpu::Instance
2929
if (instance_ == nullptr) {
3030
const DawnProcTable* dawn_procs = reinterpret_cast<const DawnProcTable*>(dawn_proc_table);
31+
#if defined(BUILD_DAWN_MONOLITHIC_LIBRARY)
32+
ORT_ENFORCE(dawn_procs == nullptr, "setting DawnProcTable is not allowed when dynamically linked to webgpu_dawn.");
33+
#else
3134
#if !defined(USE_EXTERNAL_DAWN)
3235
if (dawn_procs == nullptr) {
3336
dawn_procs = &dawn::native::GetProcs();
@@ -36,6 +39,7 @@ void WebGpuContext::Initialize(const WebGpuExecutionProviderInfo& webgpu_ep_info
3639
ORT_ENFORCE(dawn_procs != nullptr, "DawnProcTable must be provided.");
3740
#endif
3841
dawnProcSetProcs(dawn_procs);
42+
#endif
3943

4044
wgpu::InstanceDescriptor instance_desc{};
4145
instance_desc.features.timedWaitAnyEnable = true;
@@ -49,9 +53,7 @@ void WebGpuContext::Initialize(const WebGpuExecutionProviderInfo& webgpu_ep_info
4953
wgpu::RequestAdapterOptions req_adapter_options = {};
5054
wgpu::DawnTogglesDescriptor adapter_toggles_desc = {};
5155
req_adapter_options.nextInChain = &adapter_toggles_desc;
52-
#ifdef _WIN32
53-
req_adapter_options.backendType = wgpu::BackendType::D3D12;
54-
#endif
56+
req_adapter_options.backendType = static_cast<wgpu::BackendType>(webgpu_ep_info.backend_type);
5557
req_adapter_options.powerPreference = wgpu::PowerPreference::HighPerformance;
5658

5759
auto enabled_adapter_toggles = GetEnabledAdapterToggles();

onnxruntime/core/providers/webgpu/webgpu_execution_provider.h

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ struct WebGpuExecutionProviderInfo {
2626
WebGpuExecutionProviderInfo(DataLayout data_layout, bool enable_graph_capture)
2727
: data_layout{data_layout},
2828
enable_graph_capture{enable_graph_capture},
29+
backend_type{},
2930
storage_buffer_cache_mode{},
3031
uniform_buffer_cache_mode{},
3132
query_resolve_buffer_cache_mode{},
@@ -36,6 +37,7 @@ struct WebGpuExecutionProviderInfo {
3637

3738
DataLayout data_layout;
3839
bool enable_graph_capture;
40+
int backend_type;
3941
webgpu::BufferCacheMode storage_buffer_cache_mode;
4042
webgpu::BufferCacheMode uniform_buffer_cache_mode;
4143
webgpu::BufferCacheMode query_resolve_buffer_cache_mode;

onnxruntime/core/providers/webgpu/webgpu_provider_factory.cc

+20
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,26 @@ std::shared_ptr<IExecutionProviderFactory> WebGpuProviderFactoryCreator::Create(
6767
}
6868
LOGS_DEFAULT(VERBOSE) << "WebGPU EP graph capture enable: " << webgpu_ep_info.enable_graph_capture;
6969

70+
std::string backend_type_str;
71+
if (config_options.TryGetConfigEntry(kDawnBackendType, backend_type_str)) {
72+
#ifdef _WIN32
73+
// Setup Windows default backend type based on the build configuration
74+
#if defined(onnxruntime_ENABLE_DAWN_BACKEND_D3D12)
75+
webgpu_ep_info.backend_type = static_cast<int>(WGPUBackendType_D3D12);
76+
#elif defined(onnxruntime_ENABLE_DAWN_BACKEND_VULKAN)
77+
webgpu_ep_info.backend_type = static_cast<int>(WGPUBackendType_Vulkan);
78+
#endif
79+
#endif
80+
if (backend_type_str == kDawnBackendType_D3D12) {
81+
webgpu_ep_info.backend_type = static_cast<int>(WGPUBackendType_D3D12);
82+
} else if (backend_type_str == kDawnBackendType_Vulkan) {
83+
webgpu_ep_info.backend_type = static_cast<int>(WGPUBackendType_Vulkan);
84+
} else {
85+
ORT_THROW("Invalid Dawn backend type: ", backend_type_str);
86+
}
87+
}
88+
LOGS_DEFAULT(VERBOSE) << "WebGPU EP Dawn backend type: " << webgpu_ep_info.backend_type;
89+
7090
auto parse_buffer_cache_mode = [&config_options](const std::string& config_entry_str,
7191
webgpu::BufferCacheMode default_value) -> webgpu::BufferCacheMode {
7292
std::string buffer_cache_mode_str;

onnxruntime/core/providers/webgpu/webgpu_provider_options.h

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ constexpr const char* kEnableGraphCapture = "WebGPU:enableGraphCapture";
1414

1515
constexpr const char* kDawnProcTable = "WebGPU:dawnProcTable";
1616

17+
constexpr const char* kDawnBackendType = "WebGPU:dawnBackendType";
18+
1719
constexpr const char* kDeviceId = "WebGPU:deviceId";
1820
constexpr const char* kWebGpuInstance = "WebGPU:webgpuInstance";
1921
constexpr const char* kWebGpuAdapter = "WebGPU:webgpuAdapter";
@@ -30,6 +32,9 @@ constexpr const char* kForceCpuNodeNames = "WebGPU:forceCpuNodeNames";
3032

3133
// The following are the possible values for the provider options.
3234

35+
constexpr const char* kDawnBackendType_D3D12 = "D3D12";
36+
constexpr const char* kDawnBackendType_Vulkan = "Vulkan";
37+
3338
constexpr const char* kPreferredLayout_NCHW = "NCHW";
3439
constexpr const char* kPreferredLayout_NHWC = "NHWC";
3540

0 commit comments

Comments
 (0)