Skip to content

enable using vcpkg for Dawn #24699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
May 19, 2025
Merged

enable using vcpkg for Dawn #24699

merged 18 commits into from
May 19, 2025

Conversation

fs-eire
Copy link
Contributor

@fs-eire fs-eire commented May 8, 2025

Description

Enable vcpkg for webgpu

Motivation and Context

@fs-eire fs-eire force-pushed the dawn-vcpkg branch 5 times, most recently from a797876 to 511ac36 Compare May 12, 2025 21:39
@fs-eire fs-eire force-pushed the dawn-vcpkg branch 5 times, most recently from 60e2c45 to ad12b0e Compare May 14, 2025 00:39
@fs-eire
Copy link
Contributor Author

fs-eire commented May 15, 2025

The Java binding is failing on macOS. @Craigacp could you please help to take a look at it?

I need to add build flag --cmake_extra_defines onnxruntime_BUILD_DAWN_MONOLITHIC_LIBRARY=ON to the build, which adds a new dynamic library libwebgpu_dawn.dylib. While the Node.js binding tests passed, the Java binding tests failed because of cannot find "@rpath/libwebgpu_dawn.dylib". The error is still not fixed after applying commit 9125736 and ad12b0e

To reproduce the issue locally, use the following build command line:

./build.sh --parallel --use_binskim_compliant_compile_flags --build_shared_lib --build_nodejs --build_java --use_webgpu --cmake_extra_defines onnxruntime_BUILD_DAWN_MONOLITHIC_LIBRARY=ON --use_vcpkg --config Release

@Craigacp
Copy link
Contributor

I'm a bit confused about what the context is here. Why would the Java binding be available in a context where webgpu was being used?

@fs-eire
Copy link
Contributor Author

fs-eire commented May 15, 2025

I'm a bit confused about what the context is here. Why would the Java binding be available in a context where webgpu was being used?

The name of "WebGPU" is confusing. Let me try to explain it a little bit.

WebGPU is a web standard initially, which is designed for web (browser environment). However, Dawn, the native implementation of WebGPU can be integrated to native so that we can leverage the GPU capabilities in non-browser environment too. In this context, "WebGPU" means the WebGPU EP, which uses the native port of webgpu (webgpu.h and the native implementation). So, since it is native, it should work with language bindings including Java binding.

In the build, dawn will be compiled into a dynamic library (libwebgpu_dawn.dylib on macOS) and WebGPU EP will load it at runtime.

@Craigacp
Copy link
Contributor

Oh ok. I'll take a look.

@Craigacp
Copy link
Contributor

Craigacp commented May 15, 2025

Ok, if libwebgpu_dawn.dylib is supposed to be packaged inside the jar file (which it currently is doing at /ai/onnxruntime/native/osx-aarch64/libwebgpu_dawn.dylib) then it'll need to be unpacked the way the shared provider lib is unpacked (https://github.com/microsoft/onnxruntime/blob/main/java/src/main/java/ai/onnxruntime/OnnxRuntime.java#L163). Otherwise it's not in the same folder as the location we load libonnxruntime.dylib and libonnxruntime4j_jni.dylib from so the linker can't find it. Presumably this would want to be conditional in the way the unpacking of the shared provider lib is, so another call to extractProviderLibrary with the right path should work.

@Craigacp
Copy link
Contributor

This diff was sufficient to make the Java tests pass:

diff --git a/java/src/main/java/ai/onnxruntime/OnnxRuntime.java b/java/src/main/java/ai/onnxruntime/OnnxRuntime.java
index c28c79f1e7..4de3ee6338 100644
--- a/java/src/main/java/ai/onnxruntime/OnnxRuntime.java
+++ b/java/src/main/java/ai/onnxruntime/OnnxRuntime.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
  * Licensed under the MIT License.
  */
 package ai.onnxruntime;
@@ -79,6 +79,9 @@ final class OnnxRuntime {
   /** The short name of the ONNX runtime QNN provider library */
   static final String ONNXRUNTIME_LIBRARY_QNN_NAME = "onnxruntime_providers_qnn";
 
+  /** The short name of the ONNX runtime DAWN library */
+  static final String ONNXRUNTIME_LIBRARY_DAWN = "webgpu_dawn";
+
   /** The OS & CPU architecture string */
   private static final String OS_ARCH_STR = initOsArch();
 
@@ -161,6 +164,8 @@ final class OnnxRuntime {
       // Extract and prepare the shared provider library but don't try to load it,
       // the ONNX Runtime native library will load it
       extractProviderLibrary(ONNXRUNTIME_LIBRARY_SHARED_NAME);
+      // Extract the DAWN WebGPU implementation library and load it if it exists.
+      load(ONNXRUNTIME_LIBRARY_DAWN);
 
       if (!isAndroid()) {
         load(ONNXRUNTIME_LIBRARY_NAME);

I've not checked to see if that causes problems when Dawn isn't present. We typically have a little more conditionality in the loading to avoid causing trouble (e.g. turning it off when on Android), but I'm not really sure when it's going to be packaged in, or if we want to let users override the location. If you can tell me a little more about it then I can try to figure out what guards we need.

@fs-eire fs-eire force-pushed the dawn-vcpkg branch 2 times, most recently from 076db09 to d1b2836 Compare May 15, 2025 20:51
@fs-eire
Copy link
Contributor Author

fs-eire commented May 15, 2025

@Craigacp thank you for the explanation! The CI failure shows that load(ONNXRUNTIME_LIBRARY_DAWN) will fail if cannot find the file.

Is there a way to create a boolean value depending on build flags in Java? Specifically, the following C++ code does it:

#if defined(BUILD_DAWN_MONOLITHIC_LIBRARY)
constexpr bool SHOULD_LOAD_DAWN_DYNAMIC_LIB = true;
#else
constexpr bool SHOULD_LOAD_DAWN_DYNAMIC_LIB = false;
#endif

@Craigacp
Copy link
Contributor

Unfortunately Java doesn't have conditional compilation, so we'll need another signalling mechanism. Modifying the load method to make it allow optional values so we can load it if it's present is probably the best way.

@fs-eire fs-eire force-pushed the dawn-vcpkg branch 5 times, most recently from 8739f01 to 868e075 Compare May 16, 2025 23:39
@fs-eire fs-eire changed the title [WIP] enable using vcpkg for Dawn enable using vcpkg for Dawn May 18, 2025
@fs-eire fs-eire marked this pull request as ready for review May 18, 2025 23:56
@fs-eire fs-eire requested a review from a team as a code owner May 18, 2025 23:56
@snnn
Copy link
Member

snnn commented May 19, 2025

The "webgpu_build_x64_RelWithDebInfo" pipeline did not run.

@fs-eire
Copy link
Contributor Author

fs-eire commented May 19, 2025

webgpu_build_x64_RelWithDebInfo

"webgpu_build_x64_RelWithDebInfo" is not becoming 2 runs:

  • webgpu_build_x64_RelWithDebInfo (vcpkg)
  • webgpu_build_x64_RelWithDebInfo (novcpkg)

Copy link
Member

@snnn snnn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really thanks!

@fs-eire fs-eire merged commit 5915bc8 into microsoft:main May 19, 2025
84 of 87 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants