From ee603ee3265dbf6eac112baf273b6b69bf696085 Mon Sep 17 00:00:00 2001 From: Wanming Lin Date: Thu, 11 Apr 2024 11:30:03 +0800 Subject: [PATCH] [WebNN EP] Fixed WebNN constant operand is detached issue (#20229) Wasm allows growing the memory size, this will cause all array buffers reallocation. WebNN EP passes a wasm view to a WebNN constant directly which would lead to the WebNN constant be treated as detached buffers in JS side. Simply create a copy for WebNN constant to fix it. --- .../providers/webnn/builders/model_builder.cc | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/onnxruntime/core/providers/webnn/builders/model_builder.cc b/onnxruntime/core/providers/webnn/builders/model_builder.cc index 56f7ead8ccf5d..1bfc73ddf0824 100644 --- a/onnxruntime/core/providers/webnn/builders/model_builder.cc +++ b/onnxruntime/core/providers/webnn/builders/model_builder.cc @@ -197,14 +197,10 @@ Status ModelBuilder::RegisterInitializers() { default: break; } -#ifdef ENABLE_WEBASSEMBLY_THREADS - // Workaround for WebAssembly multi-threads enabled since WebNN API only accepts non-shared ArrayBufferView. - // https://www.w3.org/TR/webnn/#typedefdef-mlnamedarraybufferviews - operand = wnn_builder_.call("constant", desc, view.call("slice")); -#else - operand = wnn_builder_.call("constant", desc, view); -#endif + // Wasm memory grow will cause all array buffers reallocation, which will be treated as detached + // buffers in JS side. Simply create a copy to fix it. + operand = wnn_builder_.call("constant", desc, view.call("slice")); } else { // TODO: support other type. return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, @@ -360,13 +356,10 @@ Status ModelBuilder::AddOperandFromPersistMemoryBuffer( desc.set("dimensions", emscripten::val::array(shape)); emscripten::val operand = emscripten::val::object(); -#ifdef ENABLE_WEBASSEMBLY_THREADS - // Workaround for WebAssembly multi-threads enabled since WebNN API only accepts non-shared ArrayBufferView. - // https://www.w3.org/TR/webnn/#typedefdef-mlnamedarraybufferviews + // Wasm memory grow will cause all array buffers reallocation, which will be treated as detached + // buffers in JS side. Simply create a copy to fix it. operand = wnn_builder_.call("constant", desc, view.call("slice")); -#else - operand = wnn_builder_.call("constant", desc, view); -#endif + AddOperand(name, operand); mem_persist_buffers_.push_back(std::move(persist_buffer)); return Status::OK();