From cdc8db998475d6b259119fa89531f88a1172c3a9 Mon Sep 17 00:00:00 2001 From: Wanming Lin Date: Tue, 12 Nov 2024 13:31:24 +0800 Subject: [PATCH] [WebNN] Fixed WebNN Module undefined issue (#22795) `Module.jsepRegisterMLConstant` will be shorten by Closure Compiler in offical release, this would cause undefined error. Fix it by using `Module['jsepRegisterMLConstant']`. --- js/web/lib/wasm/jsep/backend-webnn.ts | 2 +- js/web/lib/wasm/wasm-types.ts | 17 +++++++++++++++++ onnxruntime/wasm/pre-jsep.js | 2 +- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/js/web/lib/wasm/jsep/backend-webnn.ts b/js/web/lib/wasm/jsep/backend-webnn.ts index d13c663651127..b302354c46eeb 100644 --- a/js/web/lib/wasm/jsep/backend-webnn.ts +++ b/js/web/lib/wasm/jsep/backend-webnn.ts @@ -226,7 +226,7 @@ export class WebNNBackend { return id; } - // Register WebNN Constant operands from external data. + // Register a WebNN Constant operand from external data. public registerMLConstant( externalFilePath: string, dataOffset: number, diff --git a/js/web/lib/wasm/wasm-types.ts b/js/web/lib/wasm/wasm-types.ts index 40c614fdf866a..ebeac5dc9e587 100644 --- a/js/web/lib/wasm/wasm-types.ts +++ b/js/web/lib/wasm/wasm-types.ts @@ -232,6 +232,23 @@ export declare namespace JSEP { * @returns */ jsepCreateMLContext(optionsOrGpuDevice?: MLContextOptions | GPUDevice): Promise; + + /** + * [exported from pre-jsep.js] Register a WebNN Constant operand from external data. + * @param externalFilePath - specify the external file path. + * @param dataOffset - specify the external data offset. + * @param dataLength - specify the external data length. + * @param builder - specify the MLGraphBuilder used for constructing the Constant. + * @param desc - specify the MLOperandDescriptor of the Constant. + * @returns the WebNN Constant operand for the specified external data. + */ + jsepRegisterMLConstant( + externalFilePath: string, + dataOffset: number, + dataLength: number, + builder: MLGraphBuilder, + desc: MLOperandDescriptor, + ): MLOperand; } } diff --git a/onnxruntime/wasm/pre-jsep.js b/onnxruntime/wasm/pre-jsep.js index 213f0fbc1e458..45e2475548df5 100644 --- a/onnxruntime/wasm/pre-jsep.js +++ b/onnxruntime/wasm/pre-jsep.js @@ -241,7 +241,7 @@ Module['jsepInit'] = (name, params) => { Module['jsepCreateMLContext'] = (optionsOrGpuDevice) => { return backend['createMLContext'](optionsOrGpuDevice); }; - Module.jsepRegisterMLConstant = (externalFilePath, dataOffset, dataLength, builder, desc) => { + Module['jsepRegisterMLConstant'] = (externalFilePath, dataOffset, dataLength, builder, desc) => { return backend['registerMLConstant']( externalFilePath, dataOffset, dataLength, builder, desc, Module.MountedFiles); };