Skip to content

Commit 6f4e51e

Browse files
authored
Fix bug in usage of dequantizeLinear and quantizeLinear (#331)
Latest WebNN requires dequantizeLinear and quantizeLinear ops have the same rank of input, scale and zeroPoint.
1 parent 083ad23 commit 6f4e51e

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

image_classification/mobilenet_uint8_nhwc.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ export class MobileNetV2Uint8Nhwc {
2525
}
2626

2727
dequantizeLinear_(input, quantizateParams, dataType) {
28+
// WebNN dequantizeLinear op requires the rank of scale and zeroPoint should be same as input.
29+
const missingDims = input.shape.length - quantizateParams.shape.length;
30+
if (missingDims > 0) {
31+
quantizateParams.shape.push(...Array(missingDims).fill(1));
32+
}
33+
2834
const scale = this.builder_.constant( {dataType: 'float32', shape: quantizateParams.shape},
2935
new Float32Array(quantizateParams.scale));
3036
let zeroPoint;
@@ -41,6 +47,12 @@ export class MobileNetV2Uint8Nhwc {
4147
}
4248

4349
quantizeLinear_(input, quantizateParams) {
50+
// WebNN quantizeLinear op requires the rank of scale and zeroPoint should be same as input.
51+
const missingDims = input.shape.length - quantizateParams.shape.length;
52+
if (missingDims > 0) {
53+
quantizateParams.shape.push(...Array(missingDims).fill(1));
54+
}
55+
4456
const scale = this.builder_.constant( {dataType: 'float32', shape: quantizateParams.shape},
4557
new Float32Array(quantizateParams.scale));
4658
const zeroPoint = this.builder_.constant( {dataType: 'uint8', shape: quantizateParams.shape},

0 commit comments

Comments
 (0)