Skip to content

Commit 591003b

Browse files
authored
[webgpu] a few optimization to WGSL template (#25333)
### Description This change is a follow up to #25130. - consume duktape from vcpkg if --use_vcpkg is specified - ~~add a Windows CI pipeline for dynamic WGSL template~~ (Will do in a separate PR) - upgrade wgsl-template package from 0.1.10 to 0.1.13 - support adding contribop folder as input
1 parent e6658c0 commit 591003b

File tree

6 files changed

+39
-17
lines changed

6 files changed

+39
-17
lines changed

cmake/external/onnxruntime_external_deps.cmake

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -774,13 +774,24 @@ if (onnxruntime_USE_WEBGPU)
774774
endif()
775775

776776
if (NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten" AND onnxruntime_WGSL_TEMPLATE STREQUAL "dynamic")
777-
onnxruntime_fetchcontent_declare(
778-
duktape
779-
URL ${DEP_URL_duktape}
780-
URL_HASH SHA1=${DEP_SHA1_duktape}
781-
EXCLUDE_FROM_ALL
782-
)
783-
onnxruntime_fetchcontent_makeavailable(duktape)
777+
if(onnxruntime_USE_VCPKG)
778+
find_package(unofficial-duktape CONFIG REQUIRED)
779+
add_library(duktape_static ALIAS unofficial::duktape::duktape)
780+
else()
781+
onnxruntime_fetchcontent_declare(
782+
duktape
783+
URL ${DEP_URL_duktape}
784+
URL_HASH SHA1=${DEP_SHA1_duktape}
785+
EXCLUDE_FROM_ALL
786+
)
787+
onnxruntime_fetchcontent_makeavailable(duktape)
788+
789+
if(NOT TARGET duktape_static)
790+
add_library(duktape_static STATIC "${duktape_SOURCE_DIR}/src/duktape.c")
791+
target_compile_features(duktape_static PRIVATE c_std_99)
792+
target_include_directories(duktape_static INTERFACE $<BUILD_INTERFACE:${duktape_SOURCE_DIR}/src>)
793+
endif()
794+
endif()
784795
endif()
785796
endif()
786797

cmake/onnxruntime_providers_webgpu.cmake

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,12 @@
172172
file(MAKE_DIRECTORY ${WGSL_GENERATED_DIR})
173173

174174
# Find all WGSL template input files
175-
file(GLOB_RECURSE WGSL_TEMPLATE_FILES "${ONNXRUNTIME_ROOT}/core/providers/webgpu/*.wgsl.template")
175+
file(GLOB_RECURSE WGSL_TEMPLATE_FILES
176+
"${ONNXRUNTIME_ROOT}/core/providers/webgpu/*.wgsl.template"
177+
"${ONNXRUNTIME_ROOT}/contrib_ops/webgpu/*.wgsl.template")
176178

177179
# Set wgsl-gen command line options as a list
178-
set(WGSL_GEN_OPTIONS "-i" "../" "--output" "${WGSL_GENERATED_DIR}" "-I" "wgsl_template_gen/" "--preserve-code-ref" "--verbose")
180+
set(WGSL_GEN_OPTIONS "-i" "${ONNXRUNTIME_ROOT}/core/providers/webgpu/" "-i" "${ONNXRUNTIME_ROOT}/contrib_ops/webgpu/" "--output" "${WGSL_GENERATED_DIR}" "-I" "wgsl_template_gen/" "--preserve-code-ref" "--verbose")
179181
if (onnxruntime_WGSL_TEMPLATE STREQUAL "static")
180182
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
181183
list(APPEND WGSL_GEN_OPTIONS "--generator" "static-cpp-literal")
@@ -207,10 +209,9 @@
207209
# Add the generated directory to include paths
208210
target_include_directories(onnxruntime_providers_webgpu PRIVATE ${WGSL_GENERATED_ROOT})
209211
elseif(onnxruntime_WGSL_TEMPLATE STREQUAL "dynamic")
210-
add_library(duktape_static STATIC "${duktape_SOURCE_DIR}/src/duktape.c")
211-
target_compile_features(duktape_static PRIVATE c_std_99)
212212
target_link_libraries(onnxruntime_providers_webgpu duktape_static)
213-
target_include_directories(onnxruntime_providers_webgpu PRIVATE ${duktape_SOURCE_DIR}/src)
213+
onnxruntime_add_include_to_target(onnxruntime_providers_webgpu duktape_static)
214+
214215
# Define the path to the generated templates.js file
215216
target_compile_definitions(onnxruntime_providers_webgpu PRIVATE
216217
"ORT_WGSL_TEMPLATES_JS_PATH=\"${WGSL_GENERATED_TEMPLATES_JS}\"")

cmake/vcpkg.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@
9393
"webgpu-ep": {
9494
"description": "Build with WebGPU EP",
9595
"dependencies": []
96+
},
97+
"webgpu-ep-wgsl-template-dynamic": {
98+
"description": "Build with WebGPU EP with dynamic WGSL template code generator",
99+
"dependencies": ["duktape"]
96100
}
97101
},
98102
"overrides": [
@@ -103,6 +107,10 @@
103107
{
104108
"name": "flatbuffers",
105109
"version": "23.5.26"
110+
},
111+
{
112+
"name": "duktape",
113+
"version": "2.7.0#2"
106114
}
107115
]
108116
}

onnxruntime/core/providers/webgpu/wgsl_templates/package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

onnxruntime/core/providers/webgpu/wgsl_templates/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
"author": "",
1111
"license": "MIT",
1212
"dependencies": {
13-
"@fs-eire/wgsl-template": "^0.1.3"
13+
"@fs-eire/wgsl-template": "^0.1.13"
1414
}
1515
}

tools/ci_build/build.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@ def generate_vcpkg_install_options(build_dir, args):
284284
vcpkg_install_options.append("--x-feature=vsinpu-ep")
285285
if args.use_webgpu:
286286
vcpkg_install_options.append("--x-feature=webgpu-ep")
287+
if args.wgsl_template == "dynamic":
288+
vcpkg_install_options.append("--x-feature=webgpu-ep-wgsl-template-dynamic")
287289
if args.use_webnn:
288290
vcpkg_install_options.append("--x-feature=webnn-ep")
289291
if args.use_xnnpack:

0 commit comments

Comments
 (0)