Skip to content

Commit

Permalink
[WebNN EP] Fixed bug in usage of Array.reduce() (#22944)
Browse files Browse the repository at this point in the history
In JS, reduce of empty array with no initial value will throw error. Fix
it by checking the array length firstly.
  • Loading branch information
Honry authored Nov 27, 2024
1 parent c284a68 commit fe749a8
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion js/web/lib/wasm/jsep/webnn/tensor-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

/**
Expand Down

0 comments on commit fe749a8

Please sign in to comment.