[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.
This commit is contained in:
Wanming Lin 2024-11-27 11:03:44 +08:00 committed by GitHub
parent c284a686f2
commit fe749a88a5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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;
};
/**