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

This would help release the constants bound by the MLGraphBuilder.
This commit is contained in:
Wanming Lin 2024-07-09 23:49:58 +08:00 committed by GitHub
parent 2c53b4a534
commit eeb8fc0931
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 2 deletions

View file

@ -81,6 +81,13 @@ WebNNExecutionProvider::GetCapability(const onnxruntime::GraphViewer& graph_view
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()) {
@ -322,6 +329,9 @@ common::Status WebNNExecutionProvider::Compile(const std::vector<FusedNodeAndGra
node_compute_funcs.push_back(compute_info);
}
// Explictly release the WebNN builder to free memory.
wnn_builder_ = emscripten::val::undefined();
return Status::OK();
}

View file

@ -42,8 +42,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_;