From 82681205e458713c81a63a3170f33d292aa264e8 Mon Sep 17 00:00:00 2001 From: Wanming Lin Date: Thu, 14 Nov 2024 12:22:22 +0800 Subject: [PATCH] [WebNN] Fix MLTensorUsage is undefined issue (#22831) `MLTensorUsage` has been removed from Chromium: https://chromium-review.googlesource.com/c/chromium/src/+/6015318, but we still need to make it compatible with old Chrome versions, so just make it `undefined` for latest Chrome version. --- js/web/lib/wasm/jsep/webnn/tensor-manager.ts | 4 ++-- js/web/lib/wasm/jsep/webnn/webnn.d.ts | 3 ++- js/web/test/test-runner.ts | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/js/web/lib/wasm/jsep/webnn/tensor-manager.ts b/js/web/lib/wasm/jsep/webnn/tensor-manager.ts index a19afd4bac732..be0f38468ef8d 100644 --- a/js/web/lib/wasm/jsep/webnn/tensor-manager.ts +++ b/js/web/lib/wasm/jsep/webnn/tensor-manager.ts @@ -195,7 +195,7 @@ class TensorIdTracker { } // eslint-disable-next-line no-bitwise - const usage = MLTensorUsage.READ | MLTensorUsage.WRITE; + const usage = typeof MLTensorUsage == 'undefined' ? undefined : MLTensorUsage.READ | MLTensorUsage.WRITE; this.wrapper = await this.tensorManager.getCachedTensor(dataType, shape, usage, true, true); if (copyOld && this.activeUpload) { @@ -349,7 +349,7 @@ class TensorManagerImpl implements TensorManager { public async getCachedTensor( dataType: MLOperandDataType, shape: readonly number[], - usage: MLTensorUsageFlags, + usage: MLTensorUsageFlags | undefined, writable: boolean, readable: boolean, ): Promise { diff --git a/js/web/lib/wasm/jsep/webnn/webnn.d.ts b/js/web/lib/wasm/jsep/webnn/webnn.d.ts index ca06f5c695f8d..c513b2ec2ed8b 100644 --- a/js/web/lib/wasm/jsep/webnn/webnn.d.ts +++ b/js/web/lib/wasm/jsep/webnn/webnn.d.ts @@ -400,7 +400,8 @@ declare const MLTensorUsage: { }; interface MLTensorDescriptor extends MLOperandDescriptor { - usage: MLTensorUsageFlags; + /** @deprecated Use readable/writeable instead of usage */ + usage: MLTensorUsageFlags | undefined; importableToWebGPU?: boolean; readable?: boolean; writable?: boolean; diff --git a/js/web/test/test-runner.ts b/js/web/test/test-runner.ts index c37c10c781400..d54ba32f9f494 100644 --- a/js/web/test/test-runner.ts +++ b/js/web/test/test-runner.ts @@ -661,7 +661,7 @@ async function createMLTensorForOutput(mlContext: MLContext, type: ort.Tensor.Ty shape: dims as number[], // Assign both shape and dimensions while transitioning to new API. dimensions: dims as number[], - usage: MLTensorUsage.READ, + usage: typeof MLTensorUsage == 'undefined' ? undefined : MLTensorUsage.READ, readable: true, }); @@ -686,7 +686,7 @@ async function createMLTensorForInput(mlContext: MLContext, cpuTensor: ort.Tenso shape: cpuTensor.dims as number[], // Assign both shape and dimensions while transitioning to new API. dimensions: cpuTensor.dims as number[], - usage: MLTensorUsage.WRITE, + usage: typeof MLTensorUsage == 'undefined' ? undefined : MLTensorUsage.WRITE, writable: true, }); mlContext.writeTensor(mlTensor, cpuTensor.data);