Skip to content

Commit 77a98b9

Browse files
authored
Merge pull request #293 from Honry/mloperand-method
Convert MLOperand methods into readonly attributes
2 parents 41ca020 + bb45b24 commit 77a98b9

15 files changed

+96
-42
lines changed

face_recognition/facenet_nchw.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@ export class FaceNetNchw {
4747
}
4848

4949
input = await input;
50-
50+
const isShapeMethod = typeof input.shape === 'function';
51+
const inputShape = isShapeMethod ? input.shape() : input.shape;
52+
const weightsShape = isShapeMethod ? weights.shape() : weights.shape;
5153
// WebNN spec drops autoPad support, compute the explicit padding instead.
5254
if (options.autoPad == 'same-upper') {
5355
options.padding =
5456
computePadding2DForAutoPad(
55-
/* nchw */[input.shape()[2], input.shape()[3]],
56-
/* oihw */[weights.shape()[2], weights.shape()[3]],
57+
/* nchw */[inputShape[2], inputShape[3]],
58+
/* oihw */[weightsShape[2], weightsShape[3]],
5759
options.strides, options.dilations, options.autoPad);
5860
}
5961
const conv2d = this.builder_.conv2d(input, weights, options);
@@ -266,7 +268,8 @@ export class FaceNetNchw {
266268

267269
const averagePool = this.builder_.averagePool2d(await block8_6);
268270
// Use reshape to implement squeeze(averagePool, {axes: [2, 3]});
269-
const squeezed_shape = averagePool.shape();
271+
const squeezed_shape = typeof averagePool.shape === 'function' ?
272+
averagePool.shape() : averagePool.shape;
270273
squeezed_shape.splice(2, 2);
271274
const squeeze = this.builder_.reshape(averagePool, squeezed_shape);
272275
const gemm = await this.buildGemm_(squeeze);

face_recognition/facenet_nhwc.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,15 @@ export class FaceNetNhwc {
4949
}
5050

5151
input = await input;
52-
52+
const isShapeMethod = typeof input.shape === 'function';
53+
const inputShape = isShapeMethod ? input.shape() : input.shape;
54+
const weightsShape = isShapeMethod ? weights.shape() : weights.shape;
5355
// WebNN spec drops autoPad support, compute the explicit padding instead.
5456
if (options.autoPad == 'same-upper') {
5557
options.padding =
5658
computePadding2DForAutoPad(
57-
/* nwhc */[input.shape()[1], input.shape()[2]],
58-
/* ohwi */[weights.shape()[1], weights.shape()[2]],
59+
/* nwhc */[inputShape[1], inputShape[2]],
60+
/* ohwi */[weightsShape[1], weightsShape[2]],
5961
options.strides, options.dilations, options.autoPad);
6062
}
6163
const conv2d = this.builder_.conv2d(input, weights, options);

facial_landmark_detection/ssd_mobilenetv2_face_nchw.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,12 @@ ${nameArray[1]}`;
6969
const weights = buildConstantByNpy(this.builder_, weightsName);
7070
const biasName = prefix + biasSuffix;
7171
const bias = buildConstantByNpy(this.builder_, biasName);
72-
const inputShape = (await input).shape();
73-
const weightsShape = (await weights).shape();
72+
73+
const isShapeMethod = typeof (await input).shape === 'function';
74+
const inputShape = isShapeMethod ? (await input).shape() :
75+
(await input).shape;
76+
const weightsShape = isShapeMethod ? (await weights).shape() :
77+
(await weights).shape;
7478
options.padding = computePadding2DForAutoPad(
7579
/* nchw */[inputShape[2], inputShape[3]],
7680
/* oihw */[weightsShape[2], weightsShape[3]],

facial_landmark_detection/ssd_mobilenetv2_face_nhwc.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,11 @@ ${nameArray[1]}`;
8282
options.filterLayout = 'ihwo';
8383
}
8484
options.bias = await bias;
85-
const inputShape = (await input).shape();
86-
const weightsShape = (await weights).shape();
85+
const isShapeMethod = typeof (await input).shape === 'function';
86+
const inputShape = isShapeMethod ? (await input).shape() :
87+
(await input).shape;
88+
const weightsShape = isShapeMethod ? (await weights).shape() :
89+
(await weights).shape;
8790
options.padding = computePadding2DForAutoPad(
8891
/* nhwc */[inputShape[1], inputShape[2]],
8992
/* ohwi or ihwo */[weightsShape[1], weightsShape[2]],

image_classification/mobilenet_nhwc.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@ export class MobileNetV2Nhwc {
3434
options.bias = await bias;
3535
// WebNN spec drops autoPad support, compute the explicit padding instead.
3636
if (options.autoPad == 'same-upper') {
37+
const isShapeMethod = typeof weights.shape === 'function';
38+
const inputShape = isShapeMethod ? (await input).shape() : (await input).shape;
39+
const weightsShape = isShapeMethod ? weights.shape() : weights.shape;
3740
options.padding =
3841
computePadding2DForAutoPad(
39-
/* nwhc */[await input.shape()[1], await input.shape()[2]],
40-
/* ohwi or ihwo */[weights.shape()[1], weights.shape()[2]],
42+
/* nwhc */[inputShape[1], inputShape[2]],
43+
/* ohwi or ihwo */[weightsShape[1], weightsShape[2]],
4144
options.strides, options.dilations, options.autoPad);
4245
}
4346
const conv2d = this.builder_.conv2d(await input, weights, options);

image_classification/resnet50v2_nhwc.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,14 @@ export class ResNet50V2Nhwc {
5252
options.bias = await bias;
5353
// WebNN spec drops autoPad support, compute the explicit padding instead.
5454
if (options.autoPad == 'same-upper') {
55+
const isShapeMethod = typeof weights.shape === 'function';
56+
const inputShape = isShapeMethod ? (await input).shape() :
57+
(await input).shape;
58+
const weightsShape = isShapeMethod ? weights.shape() : weights.shape;
5559
options.padding =
5660
computePadding2DForAutoPad(
57-
/* nwhc */[await input.shape()[1], await input.shape()[2]],
58-
/* ohwi */[weights.shape()[1], weights.shape()[2]],
61+
/* nwhc */[inputShape[1], inputShape[2]],
62+
/* ohwi */[weightsShape[1], weightsShape[2]],
5963
options.strides, options.dilations, options.autoPad);
6064
}
6165
const conv2d = this.builder_.conv2d(await input, weights, options);
@@ -141,10 +145,12 @@ export class ResNet50V2Nhwc {
141145
const conv1 = await this.buildConv_(
142146
input, ['', '', '1'], {strides, padding: [3, 3, 3, 3]}, false);
143147
const windowDimensions = [3, 3];
148+
const conv1Shape = typeof conv1.shape === 'function' ?
149+
conv1.shape() : conv1.shape;
144150
const pool = this.builder_.maxPool2d(
145151
conv1, {windowDimensions, strides, layout,
146152
padding: computePadding2DForAutoPad(
147-
/* nhwc */ [conv1.shape()[1], conv1.shape()[2]],
153+
/* nhwc */ [conv1Shape[1], conv1Shape[2]],
148154
windowDimensions, strides, /* dilations */ undefined,
149155
'same-upper')});
150156
// Block 1

image_classification/squeezenet_nhwc.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,14 @@ export class SqueezeNetNhwc {
3333
options.bias = await bias;
3434
// WebNN spec drops autoPad support, compute the explicit padding instead.
3535
if (options.autoPad == 'same-upper') {
36+
const isShapeMethod = typeof weights.shape === 'function';
37+
const inputShape = isShapeMethod ? (await input).shape() :
38+
(await input).shape;
39+
const weightsShape = isShapeMethod ? weights.shape() : weights.shape;
3640
options.padding =
3741
computePadding2DForAutoPad(
38-
/* nwhc */[await input.shape()[1], await input.shape()[2]],
39-
/* ohwi */[weights.shape()[1], weights.shape()[2]],
42+
/* nwhc */[inputShape[1], inputShape[2]],
43+
/* ohwi */[weightsShape[1], weightsShape[2]],
4044
options.strides, options.dilations, options.autoPad);
4145
}
4246
const conv2d = this.builder_.conv2d(await input, weights, options);

nnotepad/js/nnotepad.js

+14-8
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,21 @@ const kArgTypeOperand = 3;
3737

3838
class WebNNUtil {
3939
static bufferForOperand(operand) {
40-
const size = [...operand.shape()].reduce((a, b) => a * b, 1);
41-
const ctor = WebNNUtil.dataTypeToBufferType(operand.dataType());
40+
const isShapeMethod = typeof operand.shape === 'function';
41+
const operandShape = isShapeMethod ? operand.shape() : operand.shape;
42+
const operandDataType = isShapeMethod ? operand.dataType() :
43+
operand.dataType;
44+
const size = [...operandShape].reduce((a, b) => a * b, 1);
45+
const ctor = WebNNUtil.dataTypeToBufferType(operandDataType);
4246
return Reflect.construct(ctor, [size]);
4347
}
4448

4549
static async tensorForOperand(operand, context) {
50+
const isShapeMethod = typeof operand.shape === 'function';
4651
const desc = {
47-
dataType: operand.dataType(),
48-
dimensions: operand.shape(),
49-
shape: operand.shape(),
52+
dataType: isShapeMethod ? operand.dataType() : operand.dataType,
53+
dimensions: isShapeMethod ? operand.shape() : operand.shape,
54+
shape: isShapeMethod ? operand.shape() : operand.shape,
5055
usage: MLTensorUsage.READ,
5156
readable: true,
5257
};
@@ -613,9 +618,10 @@ export class NNotepad {
613618

614619
return outputOperands.map(
615620
(op, index) => ({
616-
dataType: op.dataType(),
617-
dimensions: op.shape(),
618-
shape: op.shape(),
621+
dataType: typeof op.shape === 'function' ? op.dataType() :
622+
op.dataType,
623+
dimensions: typeof op.shape === 'function' ? op.shape() : op.shape,
624+
shape: typeof op.shape === 'function' ? op.shape() : op.shape,
619625
buffer: maybeProxyForFloat16Array(outputBuffers[`output-${index}`]),
620626
}));
621627
}

nsnet2/nsnet2.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ export class NSNet2 {
5858
const [gru94, gru93] = this.builder_.gru(transpose31, weight192, recurrentWeight193, frames, this.hiddenSize,
5959
{bias: bias194, recurrentBias: recurrentBias194, initialHiddenState: initialState92, returnSequence: true});
6060
// Use reshape to implement squeeze(gru93, {axes: [1]});
61-
const squeeze95Shape = gru93.shape();
61+
const isShapeMethod = typeof gru93.shape === 'function';
62+
const squeeze95Shape = isShapeMethod ? gru93.shape() : gru93.shape;
6263
squeeze95Shape.splice(1, 1);
6364
const squeeze95 = this.builder_.reshape(gru93, squeeze95Shape);
6465
const initialState155 = this.builder_.input('initialState155', initialStateDesc);
@@ -89,7 +90,7 @@ export class NSNet2 {
8990
const [gru157, gru156] = this.builder_.gru(squeeze95, weight212, recurrentWeight213, frames, this.hiddenSize,
9091
{bias: bias214, recurrentBias: recurrentBias214, initialHiddenState: initialState155, returnSequence: true});
9192
// Use reshape to implement squeeze(gru156, {axes: [1]});
92-
const squeeze158Shape = gru156.shape();
93+
const squeeze158Shape = isShapeMethod ? gru156.shape() : gru156.shape;
9394
squeeze158Shape.splice(1, 1);
9495
const squeeze158 = this.builder_.reshape(gru156, squeeze158Shape);
9596
const transpose159 = this.builder_.transpose(squeeze158, {permutation: [1, 0, 2]});

object_detection/ssd_mobilenetv1_nchw.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,12 @@ ${nameArray[1]}_BatchNorm_batchnorm`;
6868
const biasName = this.biasUrl_ + prefix + biasSuffix;
6969
const bias = await buildConstantByNpy(
7070
this.builder_, biasName, this.targetDataType_);
71+
const isShapeMethod = typeof input.shape === 'function';
72+
const inputShape = isShapeMethod ? input.shape() : input.shape;
73+
const weightsShape = isShapeMethod ? weights.shape() : weights.shape;
7174
options.padding = computePadding2DForAutoPad(
72-
/* nchw */[input.shape()[2], input.shape()[3]],
73-
/* oihw */[weights.shape()[2], weights.shape()[3]],
75+
/* nchw */[inputShape[2], inputShape[3]],
76+
/* oihw */[weightsShape[2], weightsShape[3]],
7477
options.strides, options.dilations, 'same-upper');
7578
options.bias = bias;
7679
const conv2d = this.builder_.conv2d(input, weights, options);

object_detection/ssd_mobilenetv1_nhwc.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,12 @@ ${nameArray[1]}_BatchNorm_batchnorm`;
7575
options.filterLayout = 'ihwo';
7676
}
7777
options.bias = bias;
78+
const isShapeMethod = typeof input.shape === 'function';
79+
const inputShape = isShapeMethod ? input.shape() : input.shape;
80+
const weightsShape = isShapeMethod ? weights.shape() : weights.shape;
7881
options.padding = computePadding2DForAutoPad(
79-
/* nhwc */[input.shape()[1], input.shape()[2]],
80-
/* ohwi or ihwo */[weights.shape()[1], weights.shape()[2]],
82+
/* nhwc */[inputShape[1], inputShape[2]],
83+
/* ohwi or ihwo */[weightsShape[1], weightsShape[2]],
8184
options.strides, options.dilations, 'same-upper');
8285
const conv2d = this.builder_.conv2d(input, weights, options);
8386
if (relu6) {

object_detection/tiny_yolov2_nchw.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,12 @@ export class TinyYoloV2Nchw {
3737
const weight = await buildConstantByNpy(
3838
this.builder_, weightName, this.targetDataType_);
3939
const options = {autoPad: 'same-upper'};
40+
const isShapeMethod = typeof input.shape === 'function';
41+
const inputShape = isShapeMethod ? input.shape() : input.shape;
42+
const weightShape = isShapeMethod ? weight.shape() : weight.shape;
4043
options.padding = computePadding2DForAutoPad(
41-
/* nchw */[input.shape()[2], input.shape()[3]],
42-
/* oihw */[weight.shape()[2], weight.shape()[3]],
44+
/* nchw */[inputShape[2], inputShape[3]],
45+
/* oihw */[weightShape[2], weightShape[3]],
4346
options.strides, options.dilations, 'same-upper');
4447
options.bias = await buildConstantByNpy(
4548
this.builder_, biasName, this.targetDataType_);
@@ -52,8 +55,10 @@ export class TinyYoloV2Nchw {
5255
}
5356

5457
buildMaxPool2d_(input, options) {
58+
const isShapeMethod = typeof input.shape === 'function';
59+
const inputShape = isShapeMethod ? input.shape() : input.shape;
5560
options.padding = computePadding2DForAutoPad(
56-
/* nchw */[input.shape()[2], input.shape()[3]],
61+
/* nchw */[inputShape[2], inputShape[3]],
5762
options.windowDimensions,
5863
options.strides, options.dilations, 'same-upper');
5964
return this.builder_.maxPool2d(input, options);

object_detection/tiny_yolov2_nhwc.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@ export class TinyYoloV2Nhwc {
3434
filterLayout: 'ohwi',
3535
};
3636
options.bias = bias;
37+
const isShapeMethod = typeof input.shape === 'function';
38+
const inputShape = isShapeMethod ? input.shape() : input.shape;
39+
const weightsShape = isShapeMethod ? weights.shape() : weights.shape;
3740
options.padding = computePadding2DForAutoPad(
38-
/* nhwc */[input.shape()[1], input.shape()[2]],
39-
/* ohwi */[weights.shape()[1], weights.shape()[2]],
41+
/* nhwc */[inputShape[1], inputShape[2]],
42+
/* ohwi */[weightsShape[1], weightsShape[2]],
4043
options.strides, options.dilations, 'same-upper');
4144
let conv = this.builder_.conv2d(input, weights, options);
4245
if (leakyRelu) {
@@ -47,8 +50,10 @@ export class TinyYoloV2Nhwc {
4750
}
4851

4952
buildMaxPool2d_(input, options) {
53+
const isShapeMethod = typeof input.shape === 'function';
54+
const inputShape = isShapeMethod ? input.shape() : input.shape;
5055
options.padding = computePadding2DForAutoPad(
51-
/* nhwc */[input.shape()[1], input.shape()[2]],
56+
/* nhwc */[inputShape[1], inputShape[2]],
5257
options.windowDimensions,
5358
options.strides, options.dilations, 'same-upper');
5459
return this.builder_.maxPool2d(input, options);

semantic_segmentation/deeplabv3_mnv2_nhwc.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,12 @@ export class DeepLabV3MNV2Nhwc {
4444
} else {
4545
options.filterLayout = 'ohwi';
4646
}
47+
const isShapeMethod = typeof input.shape === 'function';
48+
const inputShape = isShapeMethod ? input.shape() : input.shape;
49+
const weightsShape = isShapeMethod ? weights.shape() : weights.shape;
4750
options.padding = computePadding2DForAutoPad(
48-
/* nhwc */[input.shape()[1], input.shape()[2]],
49-
/* ohwi or ihwo */[weights.shape()[1], weights.shape()[2]],
51+
/* nhwc */[inputShape[1], inputShape[2]],
52+
/* ohwi or ihwo */[weightsShape[1], weightsShape[2]],
5053
options.strides, options.dilations, 'same-upper');
5154
options.bias = bias;
5255
const conv2d = this.builder_.conv2d(input, weights, options);

style_transfer/fast_style_transfer_net.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@ export class FastStyleTransferNet {
2525

2626
buildInstanceNormalization_(conv2D, variableMul, variableAdd) {
2727
if ('instanceNormalization' in this.builder_) {
28+
const isShapeMethod = typeof variableMul.shape === 'function';
29+
const variableMulShape = isShapeMethod ? variableMul.shape() : variableMul.shape;
30+
const variableAddShape = isShapeMethod ? variableAdd.shape() : variableAdd.shape;
2831
// Use reshape to implement squeeze(variableMul); and squeeze(variableAdd);
29-
const mulShape = variableMul.shape().filter((dim) => dim !==1);
30-
const addShape = variableAdd.shape().filter((dim) => dim !==1);
32+
const mulShape = variableMulShape.filter((dim) => dim !==1);
33+
const addShape = variableAddShape.filter((dim) => dim !==1);
3134
const mulSqueeze = this.builder_.reshape(variableMul, mulShape);
3235
const addSqueeze = this.builder_.reshape(variableAdd, addShape);
3336
return this.builder_.instanceNormalization(conv2D, {scale: mulSqueeze, bias: addSqueeze});

0 commit comments

Comments
 (0)