From fe749a88a527f0ce438250d9f340332204ffa3e2 Mon Sep 17 00:00:00 2001 From: Wanming Lin Date: Wed, 27 Nov 2024 11:03:44 +0800 Subject: [PATCH] [WebNN EP] Fixed bug in usage of Array.reduce() (#22944) In JS, reduce of empty array with no initial value will throw error. Fix it by checking the array length firstly. --- js/web/lib/wasm/jsep/webnn/tensor-manager.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/web/lib/wasm/jsep/webnn/tensor-manager.ts b/js/web/lib/wasm/jsep/webnn/tensor-manager.ts index be0f38468e..4932691bda 100644 --- a/js/web/lib/wasm/jsep/webnn/tensor-manager.ts +++ b/js/web/lib/wasm/jsep/webnn/tensor-manager.ts @@ -78,7 +78,7 @@ const calculateByteLength = (dataType: MLOperandDataType, shape: readonly number if (!size) { throw new Error('Unsupported data type.'); } - return Math.ceil((shape.reduce((a, b) => a * b) * size) / 8); + return shape.length > 0 ? Math.ceil((shape.reduce((a, b) => a * b) * size) / 8) : 0; }; /**