Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WebNN EP] Release WebNN MLGraphBuilder after Compile to free memory #21200

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions onnxruntime/core/providers/webnn/webnn_execution_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@

const auto& logger = *GetLogger();

if (!wnn_builder_.as<bool>()) {
// 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()) {
Expand Down Expand Up @@ -337,6 +344,9 @@
node_compute_funcs.push_back(compute_info);
}

// Explictly release the WebNN builder to free memory.

Check warning on line 347 in onnxruntime/core/providers/webnn/webnn_execution_provider.cc

View workflow job for this annotation

GitHub Actions / Optional Lint

[misspell] reported by reviewdog 🐶 "Explictly" is a misspelling of "Explicitly" Raw Output: ./onnxruntime/core/providers/webnn/webnn_execution_provider.cc:347:5: "Explictly" is a misspelling of "Explicitly"
wnn_builder_ = emscripten::val::undefined();

return Status::OK();
}

Expand Down
4 changes: 2 additions & 2 deletions onnxruntime/core/providers/webnn/webnn_execution_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class WebNNExecutionProvider : public IExecutionProvider {
std::shared_ptr<KernelRegistry> 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_;
Expand Down
Loading