Skip to content

[Web] Fail to link static Wasm library with WebNN EP support #24936

@huningxin

Description

@huningxin

Describe the issue

Fail to link static Wasm library with WebNN EP support. Projects written in C++ want to do model inference with ORT C++ API with WebNN EP for Web scenario. It compiles to Wasm and requires to link against static ORT Wasm library with WebNN EP support.

To reproduce

Follow instructions for the answer of "Q: I have a C++ project for web scenario..." at https://onnxruntime.ai/docs/build/web.html#faq,

Build static Ort Wasm library with WebNN EP support with the following command-line:

$ ./build.sh --config Release --build_wasm_static_lib --enable_wasm_simd --enable_wasm_threads --skip_tests --disable_wasm_exception_catching --disable_rtti --parallel --use_jsep --use_webnn

Copy the headers to the C++ project, including

include/onnxruntime/core/session/onnxruntime_c_api.h
include/onnxruntime/core/session/onnxruntime_cxx_api.h
include/onnxruntime/core/session/onnxruntime_cxx_inline.h

Copy ORT libs including

build/Linux/Release/libonnxruntime_common.a
build/Linux/Release/libonnxruntime_framework.a
build/Linux/Release/libonnxruntime_graph.a
build/Linux/Release/libonnxruntime_lora.a
build/Linux/Release/libonnxruntime_mlas.a
build/Linux/Release/libonnxruntime_optimizer.a
build/Linux/Release/libonnxruntime_providers.a
build/Linux/Release/libonnxruntime_providers_webnn.a
build/Linux/Release/libonnxruntime_session.a
build/Linux/Release/libonnxruntime_util.a
build/Linux/Release/libonnxruntime_flatbuffers.a
build/Linux/Release/libonnxruntime_webassembly.a

And also copy other 3rd party dependent libs, including protobuf, re2, abseil, nsync etc.,

The C++ project has the following build flags setting in CMakeLists.txt:

set(CMAKE_CXX_FLAGS "-s ERROR_ON_UNDEFINED_SYMBOLS=1 \
                     -s -pthread \
                     -s -Wno-pthreads-mem-growth \
                     -s PROXY_TO_PTHREAD=1 \
                     -s MODULARIZE=1 \
                     -s EXPORTED_FUNCTIONS=['_run_inference'] \
                     -s EXPORT_NAME=ORTNexusSample \
                     -s EXIT_RUNTIME=0 \
                     -s INVOKE_RUN=0 \
                     -s DISABLE_EXCEPTION_CATCHING=0 \
                     -s TOTAL_MEMORY=1500MB \
                     -s ENVIRONMENT='web,worker' \
                     -s EXPORTED_RUNTIME_METHODS=['ccall','cwrap']")

The build fails with the following link error:

wasm-ld: error: lto.tmp: undefined symbol: onnxruntime::JsProviderFactoryCreator::Create(std::__2::unordered_map<std::__2::basic_string<char, std::__2::char_traits<char>, std::__2::allocator<char>>, std::__2::basic_string<char, std::__2::char_traits<char>, std::__2::allocator<char>>, std::__2::hash<std::__2::basic_string<char, std::__2::char_traits<char>, std::__2::allocator<char>>>, std::__2::equal_to<std::__2::basic_string<char, std::__2::char_traits<char>, std::__2::allocator<char>>>, std::__2::allocator<std::__2::pair<std::__2::basic_string<char, std::__2::char_traits<char>, std::__2::allocator<char>> const, std::__2::basic_string<char, std::__2::char_traits<char>, std::__2::allocator<char>>>>> const&, onnxruntime::SessionOptions const*)

Urgency

No response

ONNX Runtime Installation

Built from Source

ONNX Runtime Version or Commit ID

89a2ff9

Execution Provider

Other / Unknown, 'webgpu' (WebGPU)

Metadata

Metadata

Assignees

No one assigned

    Labels

    ep:WebGPUort-web webgpu providerep:WebNNWebNN execution providerplatform:webissues related to ONNX Runtime web; typically submitted using template

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions