diff --git a/onnxruntime/core/providers/webnn/webnn_execution_provider.cc b/onnxruntime/core/providers/webnn/webnn_execution_provider.cc index 13ed29667deb..6b747cd35d20 100644 --- a/onnxruntime/core/providers/webnn/webnn_execution_provider.cc +++ b/onnxruntime/core/providers/webnn/webnn_execution_provider.cc @@ -96,6 +96,13 @@ WebNNExecutionProvider::GetCapability(const onnxruntime::GraphViewer& graph_view const auto& logger = *GetLogger(); + if (!wnn_builder_.as()) { + // The GetCapability function may be called again after Compile due to the logic in the + // PartitionOnnxFormatModel function (see onnxruntime/core/framework/graph_partitioner.cc). + // We need to re-create the wnn_builder_ here to avoid it's been released in last Compile. + wnn_builder_ = emscripten::val::global("MLGraphBuilder").new_(wnn_context_); + } + const auto node_groups = webnn::GetSupportedNodes(graph_viewer, wnn_builder_, wnn_device_type_, logger); if (node_groups.empty()) { @@ -337,6 +344,9 @@ common::Status WebNNExecutionProvider::Compile(const std::vector GetKernelRegistry() const override; private: - emscripten::val wnn_context_ = emscripten::val::object(); - emscripten::val wnn_builder_ = emscripten::val::object(); + emscripten::val wnn_context_ = emscripten::val::undefined(); + mutable emscripten::val wnn_builder_ = emscripten::val::undefined(); DataLayout preferred_layout_; webnn::WebnnDeviceType wnn_device_type_;