Skip to content

[Build] Build for WASM with WebNN enabled and Exceptions disabled has error with gsl::narrow #22712

Description

@sevagh

Describe the issue

If you build for webassembly with --use_webnn and --disable_exceptions, there is a piece of code here:

splits.insert(splits.end(), split_count - 1, gsl::narrow<uint32_t>(input_shape[axis]) / split_count);
splits.insert(splits.end(), gsl::narrow<uint32_t>(input_shape[axis]) % split_count);

It uses gsl::narrow which fails to compile

  // Check that the splits evenly divide.
  if (split_count > 0 && splits.empty() && input_shape[axis] % split_count != 0) {
    // Divide inputs into variable size outputs:
    splits.insert(splits.end(), split_count - 1, gsl::narrow<uint32_t>(input_shape[axis]) / split_count);
    splits.insert(splits.end(), gsl::narrow<uint32_t>(input_shape[axis]) % split_count);
  }

If we check narrow.h, it says that we should use onnxruntime::narrow in the case where exceptions are disabled. Replacing gsl::narrow with onnxruntime::narrow lets my build succeed:

[ 70%] Linking CXX static library libonnxruntime_providers_webnn.a
[ 76%] Built target onnxruntime_providers_webnn
[ 76%] Linking CXX static library libonnxruntime_providers.a
[100%] Built target onnxruntime_providers
[100%] Bundling onnxruntime_webassembly
[100%] Built target bundling_target
2024-11-04 11:25:24,292 build [INFO] - Build complete

Urgency

No response

Target platform

WebAssembly

Build script

python ./vendor/onnxruntime/tools/ci_build/build.py \
--build_dir ./build/build-ort-wasm-simd-threads-webgpu-webnn \
--config=MinSizeRel \
--build_wasm_static_lib \
--parallel \
--minimal_build extended \
--disable_ml_ops \
--disable_rtti \
--include_ops_by_config ./onnx-models/required_operators_and_types.config \
--enable_reduced_operator_type_support \
--skip_tests \
--enable_wasm_simd \
--enable_wasm_threads \
--use_jsep \
--use_webnn \
--enable_wasm_exception_throwing_override \
--disable_exceptions

Error / output

[ 69%] Linking CXX static library libonnxruntime_lora.a
[ 69%] Built target onnxruntime_lora
[ 69%] Building CXX object CMakeFiles/onnxruntime_providers.dir/home/sevagh/repos/demucs.onnx-pro/vendor/onnxruntime/onnxruntime/core/providers/cpu/tensor/unique.cc.o
[ 69%] Building CXX object CMakeFiles/onnxruntime_providers.dir/op_reduction.generated/onnxruntime/contrib_ops/cpu/cpu_contrib_kernels.cc.o
/home/sevagh/repos/demucs.onnx-pro/vendor/onnxruntime/onnxruntime/core/providers/webnn/builders/impl/split_op_builder.cc:77:50: error: no template named 'narrow' in namespace 'gsl'; did you mean simply 'narrow'?
   77 |     splits.insert(splits.end(), split_count - 1, gsl::narrow<uint32_t>(input_shape[axis]) / split_count);
      |                                                  ^~~~~~~~~~~
      |                                                  narrow
/home/sevagh/repos/demucs.onnx-pro/vendor/onnxruntime/include/onnxruntime/core/common/narrow.h:38:34: note: 'narrow' declared here
   38 | GSL_SUPPRESS(type.1) constexpr T narrow(U u) noexcept {
      |                                  ^
/home/sevagh/repos/demucs.onnx-pro/vendor/onnxruntime/onnxruntime/core/providers/webnn/builders/impl/split_op_builder.cc:77:50: error: no template named 'narrow' in namespace 'gsl'; did you mean simply 'narrow'?
   77 |     splits.insert(splits.end(), split_count - 1, gsl::narrow<uint32_t>(input_shape[axis]) / split_count);
      |                                                  ^~~~~~~~~~~
      |                                                  narrow
/home/sevagh/repos/demucs.onnx-pro/vendor/onnxruntime/include/onnxruntime/core/common/narrow.h:38:34: note: 'narrow' declared here
   38 | GSL_SUPPRESS(type.1) constexpr T narrow(U u) noexcept {
      |                                  ^
/home/sevagh/repos/demucs.onnx-pro/vendor/onnxruntime/onnxruntime/core/providers/webnn/builders/impl/split_op_builder.cc:78:33: error: no template named 'narrow' in namespace 'gsl'; did you mean simply 'narrow'?
   78 |     splits.insert(splits.end(), gsl::narrow<uint32_t>(input_shape[axis]) % split_count);
      |                                 ^~~~~~~~~~~
      |                                 narrow
/home/sevagh/repos/demucs.onnx-pro/vendor/onnxruntime/include/onnxruntime/core/common/narrow.h:38:34: note: 'narrow' declared here
   38 | GSL_SUPPRESS(type.1) constexpr T narrow(U u) noexcept {
      |                                  ^
/home/sevagh/repos/demucs.onnx-pro/vendor/onnxruntime/onnxruntime/core/providers/webnn/builders/impl/split_op_builder.cc:78:33: error: no template named 'narrow' in namespace 'gsl'; did you mean simply 'narrow'?
   78 |     splits.insert(splits.end(), gsl::narrow<uint32_t>(input_shape[axis]) % split_count);
      |                                 ^~~~~~~~~~~
      |                     

Visual Studio Version

No response

GCC / Compiler Version

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    buildbuild issues; typically submitted using templatecontributions welcomeexternal contributions welcomeep:WebGPUort-web webgpu providerep:WebNNWebNN execution providerplatform:webissues related to ONNX Runtime web; typically submitted using template

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions