|
9 | 9 | if (onnxruntime_ENABLE_WEBASSEMBLY_THREADS)
|
10 | 10 | add_definitions(-DENABLE_WEBASSEMBLY_THREADS=1)
|
11 | 11 | endif()
|
| 12 | + if (onnxruntime_WGSL_TEMPLATE STREQUAL "dynamic") |
| 13 | + if (onnxruntime_DISABLE_EXCEPTIONS) |
| 14 | + message(FATAL_ERROR "Dynamic WGSL template generation requires exception handling to be enabled.") |
| 15 | + endif() |
| 16 | + if (CMAKE_SYSTEM_NAME STREQUAL "Emscripten") |
| 17 | + message(FATAL_ERROR "Dynamic WGSL template generation is not supported when targeting WebAssembly.") |
| 18 | + endif() |
| 19 | + add_definitions(-DORT_WGSL_TEMPLATE_DYNAMIC=1) |
| 20 | + elseif (NOT onnxruntime_WGSL_TEMPLATE STREQUAL "static") |
| 21 | + message(FATAL_ERROR "Unsupported value for onnxruntime_WGSL_TEMPLATE: ${onnxruntime_WGSL_TEMPLATE}. Supported values are 'static' or 'dynamic'.") |
| 22 | + endif() |
12 | 23 | file(GLOB_RECURSE onnxruntime_providers_webgpu_cc_srcs CONFIGURE_DEPENDS
|
13 | 24 | "${ONNXRUNTIME_ROOT}/core/providers/webgpu/*.h"
|
14 | 25 | "${ONNXRUNTIME_ROOT}/core/providers/webgpu/*.cc"
|
|
20 | 31 |
|
21 | 32 | source_group(TREE ${REPO_ROOT} FILES ${onnxruntime_providers_webgpu_cc_srcs})
|
22 | 33 | onnxruntime_add_static_library(onnxruntime_providers_webgpu ${onnxruntime_providers_webgpu_cc_srcs})
|
| 34 | + target_compile_features(onnxruntime_providers_webgpu PRIVATE cxx_std_20) |
23 | 35 | onnxruntime_add_include_to_target(onnxruntime_providers_webgpu
|
24 | 36 | onnxruntime_common onnx onnx_proto flatbuffers::flatbuffers Boost::mp11 safeint_interface)
|
25 | 37 |
|
|
117 | 129 | endif()
|
118 | 130 |
|
119 | 131 | add_dependencies(onnxruntime_providers_webgpu ${onnxruntime_EXTERNAL_DEPENDENCIES})
|
| 132 | + |
| 133 | + if (onnxruntime_WGSL_TEMPLATE) |
| 134 | + # Define the WGSL templates directory and output directory |
| 135 | + set(WGSL_TEMPLATES_DIR "${ONNXRUNTIME_ROOT}/core/providers/webgpu/wgsl_templates") |
| 136 | + set(WGSL_GENERATED_ROOT "${CMAKE_CURRENT_BINARY_DIR}/wgsl_generated") |
| 137 | + |
| 138 | + # Find npm and node executables |
| 139 | + find_program(NPM_EXECUTABLE "npm.cmd" "npm" REQUIRED) |
| 140 | + if(NOT NPM_EXECUTABLE) |
| 141 | + message(FATAL_ERROR "npm is required for WGSL template generation but was not found") |
| 142 | + endif() |
| 143 | + find_program(NODE_EXECUTABLE "node" REQUIRED) |
| 144 | + if (NOT NODE_EXECUTABLE) |
| 145 | + message(FATAL_ERROR "Node is required for WGSL template generation but was not found") |
| 146 | + endif() |
| 147 | + |
| 148 | + # Install npm dependencies |
| 149 | + add_custom_command( |
| 150 | + OUTPUT "${WGSL_TEMPLATES_DIR}/node_modules/.install_complete" |
| 151 | + COMMAND ${NPM_EXECUTABLE} ci |
| 152 | + COMMAND ${CMAKE_COMMAND} -E touch "${WGSL_TEMPLATES_DIR}/node_modules/.install_complete" |
| 153 | + DEPENDS "${WGSL_TEMPLATES_DIR}/package.json" "${WGSL_TEMPLATES_DIR}/package-lock.json" |
| 154 | + WORKING_DIRECTORY ${WGSL_TEMPLATES_DIR} |
| 155 | + COMMENT "Installing npm dependencies for WGSL template generation" |
| 156 | + VERBATIM |
| 157 | + ) |
| 158 | + |
| 159 | + if (onnxruntime_WGSL_TEMPLATE STREQUAL "static") |
| 160 | + set(WGSL_GENERATED_DIR "${WGSL_GENERATED_ROOT}/wgsl_template_gen") |
| 161 | + # set(WGSL_GEN_OUTPUTS "${WGSL_GENERATED_DIR}/index.h" "${WGSL_GENERATED_DIR}/index_impl.h") |
| 162 | + # Define the output files that will be generated |
| 163 | + set(WGSL_GENERATED_INDEX_H "${WGSL_GENERATED_DIR}/index.h") |
| 164 | + set(WGSL_GENERATED_INDEX_IMPL_H "${WGSL_GENERATED_DIR}/index_impl.h") |
| 165 | + elseif(onnxruntime_WGSL_TEMPLATE STREQUAL "dynamic") |
| 166 | + set(WGSL_GENERATED_DIR "${WGSL_GENERATED_ROOT}/dynamic") |
| 167 | + # set(WGSL_GEN_OUTPUTS "${WGSL_GENERATED_DIR}/templates.js") |
| 168 | + set(WGSL_GENERATED_TEMPLATES_JS "${WGSL_GENERATED_DIR}/templates.js") |
| 169 | + endif() |
| 170 | + |
| 171 | + # Ensure the output directory exists |
| 172 | + file(MAKE_DIRECTORY ${WGSL_GENERATED_DIR}) |
| 173 | + |
| 174 | + # Find all WGSL template input files |
| 175 | + file(GLOB_RECURSE WGSL_TEMPLATE_FILES "${ONNXRUNTIME_ROOT}/core/providers/webgpu/*.wgsl.template") |
| 176 | + |
| 177 | + # 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") |
| 179 | + if (onnxruntime_WGSL_TEMPLATE STREQUAL "static") |
| 180 | + if (CMAKE_BUILD_TYPE STREQUAL "Debug") |
| 181 | + list(APPEND WGSL_GEN_OPTIONS "--generator" "static-cpp-literal") |
| 182 | + else() |
| 183 | + list(APPEND WGSL_GEN_OPTIONS "--generator" "static-cpp") |
| 184 | + endif() |
| 185 | + elseif(onnxruntime_WGSL_TEMPLATE STREQUAL "dynamic") |
| 186 | + list(APPEND WGSL_GEN_OPTIONS "--generator" "dynamic") |
| 187 | + endif() |
| 188 | + |
| 189 | + # Generate WGSL templates |
| 190 | + add_custom_command( |
| 191 | + OUTPUT ${WGSL_GENERATED_INDEX_H} ${WGSL_GENERATED_INDEX_IMPL_H} ${WGSL_GENERATED_TEMPLATES_JS} |
| 192 | + COMMAND ${NPM_EXECUTABLE} run gen -- ${WGSL_GEN_OPTIONS} |
| 193 | + DEPENDS "${WGSL_TEMPLATES_DIR}/node_modules/.install_complete" ${WGSL_TEMPLATE_FILES} |
| 194 | + WORKING_DIRECTORY ${WGSL_TEMPLATES_DIR} |
| 195 | + COMMENT "Generating WGSL templates from *.wgsl.template files" |
| 196 | + COMMAND_EXPAND_LISTS |
| 197 | + VERBATIM |
| 198 | + ) |
| 199 | + |
| 200 | + # Create a target to represent the generation step |
| 201 | + add_custom_target(onnxruntime_webgpu_wgsl_generation |
| 202 | + DEPENDS ${WGSL_GENERATED_INDEX_H} ${WGSL_GENERATED_INDEX_IMPL_H} ${WGSL_GENERATED_TEMPLATES_JS} |
| 203 | + SOURCES ${WGSL_TEMPLATE_FILES} |
| 204 | + ) |
| 205 | + |
| 206 | + if (onnxruntime_WGSL_TEMPLATE STREQUAL "static") |
| 207 | + # Add the generated directory to include paths |
| 208 | + target_include_directories(onnxruntime_providers_webgpu PRIVATE ${WGSL_GENERATED_ROOT}) |
| 209 | + 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) |
| 212 | + target_link_libraries(onnxruntime_providers_webgpu duktape_static) |
| 213 | + target_include_directories(onnxruntime_providers_webgpu PRIVATE ${duktape_SOURCE_DIR}/src) |
| 214 | + # Define the path to the generated templates.js file |
| 215 | + target_compile_definitions(onnxruntime_providers_webgpu PRIVATE |
| 216 | + "ORT_WGSL_TEMPLATES_JS_PATH=\"${WGSL_GENERATED_TEMPLATES_JS}\"") |
| 217 | + endif() |
| 218 | + |
| 219 | + # Make sure generation happens before building the provider |
| 220 | + add_dependencies(onnxruntime_providers_webgpu onnxruntime_webgpu_wgsl_generation) |
| 221 | + endif() |
| 222 | + |
120 | 223 | set_target_properties(onnxruntime_providers_webgpu PROPERTIES FOLDER "ONNXRuntime")
|
0 commit comments