Skip to content

Commit

Permalink
Convert MLOperand methods into readonly attributes
Browse files Browse the repository at this point in the history
Adapt to spec change at webmachinelearning/webnn#774
  • Loading branch information
Honry committed Oct 30, 2024
1 parent 41ca020 commit dc42b35
Show file tree
Hide file tree
Showing 15 changed files with 40 additions and 40 deletions.
6 changes: 3 additions & 3 deletions face_recognition/facenet_nchw.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ export class FaceNetNchw {
if (options.autoPad == 'same-upper') {
options.padding =
computePadding2DForAutoPad(
/* nchw */[input.shape()[2], input.shape()[3]],
/* oihw */[weights.shape()[2], weights.shape()[3]],
/* nchw */[input.shape[2], input.shape[3]],
/* oihw */[weights.shape[2], weights.shape[3]],
options.strides, options.dilations, options.autoPad);
}
const conv2d = this.builder_.conv2d(input, weights, options);
Expand Down Expand Up @@ -266,7 +266,7 @@ export class FaceNetNchw {

const averagePool = this.builder_.averagePool2d(await block8_6);
// Use reshape to implement squeeze(averagePool, {axes: [2, 3]});
const squeezed_shape = averagePool.shape();
const squeezed_shape = averagePool.shape;
squeezed_shape.splice(2, 2);
const squeeze = this.builder_.reshape(averagePool, squeezed_shape);
const gemm = await this.buildGemm_(squeeze);
Expand Down
4 changes: 2 additions & 2 deletions face_recognition/facenet_nhwc.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ export class FaceNetNhwc {
if (options.autoPad == 'same-upper') {
options.padding =
computePadding2DForAutoPad(
/* nwhc */[input.shape()[1], input.shape()[2]],
/* ohwi */[weights.shape()[1], weights.shape()[2]],
/* nwhc */[input.shape[1], input.shape[2]],
/* ohwi */[weights.shape[1], weights.shape[2]],
options.strides, options.dilations, options.autoPad);
}
const conv2d = this.builder_.conv2d(input, weights, options);
Expand Down
4 changes: 2 additions & 2 deletions facial_landmark_detection/ssd_mobilenetv2_face_nchw.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ ${nameArray[1]}`;
const weights = buildConstantByNpy(this.builder_, weightsName);
const biasName = prefix + biasSuffix;
const bias = buildConstantByNpy(this.builder_, biasName);
const inputShape = (await input).shape();
const weightsShape = (await weights).shape();
const inputShape = (await input).shape;
const weightsShape = (await weights).shape;
options.padding = computePadding2DForAutoPad(
/* nchw */[inputShape[2], inputShape[3]],
/* oihw */[weightsShape[2], weightsShape[3]],
Expand Down
4 changes: 2 additions & 2 deletions facial_landmark_detection/ssd_mobilenetv2_face_nhwc.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ ${nameArray[1]}`;
options.filterLayout = 'ihwo';
}
options.bias = await bias;
const inputShape = (await input).shape();
const weightsShape = (await weights).shape();
const inputShape = (await input).shape;
const weightsShape = (await weights).shape;
options.padding = computePadding2DForAutoPad(
/* nhwc */[inputShape[1], inputShape[2]],
/* ohwi or ihwo */[weightsShape[1], weightsShape[2]],
Expand Down
4 changes: 2 additions & 2 deletions image_classification/mobilenet_nhwc.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export class MobileNetV2Nhwc {
if (options.autoPad == 'same-upper') {
options.padding =
computePadding2DForAutoPad(
/* nwhc */[await input.shape()[1], await input.shape()[2]],
/* ohwi or ihwo */[weights.shape()[1], weights.shape()[2]],
/* nwhc */[await input.shape[1], await input.shape[2]],
/* ohwi or ihwo */[weights.shape[1], weights.shape[2]],
options.strides, options.dilations, options.autoPad);
}
const conv2d = this.builder_.conv2d(await input, weights, options);
Expand Down
6 changes: 3 additions & 3 deletions image_classification/resnet50v2_nhwc.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ export class ResNet50V2Nhwc {
if (options.autoPad == 'same-upper') {
options.padding =
computePadding2DForAutoPad(
/* nwhc */[await input.shape()[1], await input.shape()[2]],
/* ohwi */[weights.shape()[1], weights.shape()[2]],
/* nwhc */[await input.shape[1], await input.shape[2]],
/* ohwi */[weights.shape[1], weights.shape[2]],
options.strides, options.dilations, options.autoPad);
}
const conv2d = this.builder_.conv2d(await input, weights, options);
Expand Down Expand Up @@ -144,7 +144,7 @@ export class ResNet50V2Nhwc {
const pool = this.builder_.maxPool2d(
conv1, {windowDimensions, strides, layout,
padding: computePadding2DForAutoPad(
/* nhwc */ [conv1.shape()[1], conv1.shape()[2]],
/* nhwc */ [conv1.shape[1], conv1.shape[2]],
windowDimensions, strides, /* dilations */ undefined,
'same-upper')});
// Block 1
Expand Down
4 changes: 2 additions & 2 deletions image_classification/squeezenet_nhwc.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export class SqueezeNetNhwc {
if (options.autoPad == 'same-upper') {
options.padding =
computePadding2DForAutoPad(
/* nwhc */[await input.shape()[1], await input.shape()[2]],
/* ohwi */[weights.shape()[1], weights.shape()[2]],
/* nwhc */[await input.shape[1], await input.shape[2]],
/* ohwi */[weights.shape[1], weights.shape[2]],
options.strides, options.dilations, options.autoPad);
}
const conv2d = this.builder_.conv2d(await input, weights, options);
Expand Down
16 changes: 8 additions & 8 deletions nnotepad/js/nnotepad.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ const kArgTypeOperand = 3;

class WebNNUtil {
static bufferForOperand(operand) {
const size = [...operand.shape()].reduce((a, b) => a * b, 1);
const ctor = WebNNUtil.dataTypeToBufferType(operand.dataType());
const size = [...operand.shape].reduce((a, b) => a * b, 1);
const ctor = WebNNUtil.dataTypeToBufferType(operand.dataType);
return Reflect.construct(ctor, [size]);
}

static async tensorForOperand(operand, context) {
const desc = {
dataType: operand.dataType(),
dimensions: operand.shape(),
shape: operand.shape(),
dataType: operand.dataType,
dimensions: operand.shape,
shape: operand.shape,
usage: MLTensorUsage.READ,
readable: true,
};
Expand Down Expand Up @@ -613,9 +613,9 @@ export class NNotepad {

return outputOperands.map(
(op, index) => ({
dataType: op.dataType(),
dimensions: op.shape(),
shape: op.shape(),
dataType: op.dataType,
dimensions: op.shape,
shape: op.shape,
buffer: maybeProxyForFloat16Array(outputBuffers[`output-${index}`]),
}));
}
Expand Down
4 changes: 2 additions & 2 deletions nsnet2/nsnet2.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class NSNet2 {
const [gru94, gru93] = this.builder_.gru(transpose31, weight192, recurrentWeight193, frames, this.hiddenSize,
{bias: bias194, recurrentBias: recurrentBias194, initialHiddenState: initialState92, returnSequence: true});
// Use reshape to implement squeeze(gru93, {axes: [1]});
const squeeze95Shape = gru93.shape();
const squeeze95Shape = gru93.shape;
squeeze95Shape.splice(1, 1);
const squeeze95 = this.builder_.reshape(gru93, squeeze95Shape);
const initialState155 = this.builder_.input('initialState155', initialStateDesc);
Expand Down Expand Up @@ -89,7 +89,7 @@ export class NSNet2 {
const [gru157, gru156] = this.builder_.gru(squeeze95, weight212, recurrentWeight213, frames, this.hiddenSize,
{bias: bias214, recurrentBias: recurrentBias214, initialHiddenState: initialState155, returnSequence: true});
// Use reshape to implement squeeze(gru156, {axes: [1]});
const squeeze158Shape = gru156.shape();
const squeeze158Shape = gru156.shape;
squeeze158Shape.splice(1, 1);
const squeeze158 = this.builder_.reshape(gru156, squeeze158Shape);
const transpose159 = this.builder_.transpose(squeeze158, {permutation: [1, 0, 2]});
Expand Down
4 changes: 2 additions & 2 deletions object_detection/ssd_mobilenetv1_nchw.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ ${nameArray[1]}_BatchNorm_batchnorm`;
const bias = await buildConstantByNpy(
this.builder_, biasName, this.targetDataType_);
options.padding = computePadding2DForAutoPad(
/* nchw */[input.shape()[2], input.shape()[3]],
/* oihw */[weights.shape()[2], weights.shape()[3]],
/* nchw */[input.shape[2], input.shape[3]],
/* oihw */[weights.shape[2], weights.shape[3]],
options.strides, options.dilations, 'same-upper');
options.bias = bias;
const conv2d = this.builder_.conv2d(input, weights, options);
Expand Down
4 changes: 2 additions & 2 deletions object_detection/ssd_mobilenetv1_nhwc.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ ${nameArray[1]}_BatchNorm_batchnorm`;
}
options.bias = bias;
options.padding = computePadding2DForAutoPad(
/* nhwc */[input.shape()[1], input.shape()[2]],
/* ohwi or ihwo */[weights.shape()[1], weights.shape()[2]],
/* nhwc */[input.shape[1], input.shape[2]],
/* ohwi or ihwo */[weights.shape[1], weights.shape[2]],
options.strides, options.dilations, 'same-upper');
const conv2d = this.builder_.conv2d(input, weights, options);
if (relu6) {
Expand Down
6 changes: 3 additions & 3 deletions object_detection/tiny_yolov2_nchw.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export class TinyYoloV2Nchw {
this.builder_, weightName, this.targetDataType_);
const options = {autoPad: 'same-upper'};
options.padding = computePadding2DForAutoPad(
/* nchw */[input.shape()[2], input.shape()[3]],
/* oihw */[weight.shape()[2], weight.shape()[3]],
/* nchw */[input.shape[2], input.shape[3]],
/* oihw */[weight.shape[2], weight.shape[3]],
options.strides, options.dilations, 'same-upper');
options.bias = await buildConstantByNpy(
this.builder_, biasName, this.targetDataType_);
Expand All @@ -53,7 +53,7 @@ export class TinyYoloV2Nchw {

buildMaxPool2d_(input, options) {
options.padding = computePadding2DForAutoPad(
/* nchw */[input.shape()[2], input.shape()[3]],
/* nchw */[input.shape[2], input.shape[3]],
options.windowDimensions,
options.strides, options.dilations, 'same-upper');
return this.builder_.maxPool2d(input, options);
Expand Down
6 changes: 3 additions & 3 deletions object_detection/tiny_yolov2_nhwc.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export class TinyYoloV2Nhwc {
};
options.bias = bias;
options.padding = computePadding2DForAutoPad(
/* nhwc */[input.shape()[1], input.shape()[2]],
/* ohwi */[weights.shape()[1], weights.shape()[2]],
/* nhwc */[input.shape[1], input.shape[2]],
/* ohwi */[weights.shape[1], weights.shape[2]],
options.strides, options.dilations, 'same-upper');
let conv = this.builder_.conv2d(input, weights, options);
if (leakyRelu) {
Expand All @@ -48,7 +48,7 @@ export class TinyYoloV2Nhwc {

buildMaxPool2d_(input, options) {
options.padding = computePadding2DForAutoPad(
/* nhwc */[input.shape()[1], input.shape()[2]],
/* nhwc */[input.shape[1], input.shape[2]],
options.windowDimensions,
options.strides, options.dilations, 'same-upper');
return this.builder_.maxPool2d(input, options);
Expand Down
4 changes: 2 additions & 2 deletions semantic_segmentation/deeplabv3_mnv2_nhwc.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export class DeepLabV3MNV2Nhwc {
options.filterLayout = 'ohwi';
}
options.padding = computePadding2DForAutoPad(
/* nhwc */[input.shape()[1], input.shape()[2]],
/* ohwi or ihwo */[weights.shape()[1], weights.shape()[2]],
/* nhwc */[input.shape[1], input.shape[2]],
/* ohwi or ihwo */[weights.shape[1], weights.shape[2]],
options.strides, options.dilations, 'same-upper');
options.bias = bias;
const conv2d = this.builder_.conv2d(input, weights, options);
Expand Down
4 changes: 2 additions & 2 deletions style_transfer/fast_style_transfer_net.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export class FastStyleTransferNet {
buildInstanceNormalization_(conv2D, variableMul, variableAdd) {
if ('instanceNormalization' in this.builder_) {
// Use reshape to implement squeeze(variableMul); and squeeze(variableAdd);
const mulShape = variableMul.shape().filter((dim) => dim !==1);
const addShape = variableAdd.shape().filter((dim) => dim !==1);
const mulShape = variableMul.shape.filter((dim) => dim !==1);
const addShape = variableAdd.shape.filter((dim) => dim !==1);
const mulSqueeze = this.builder_.reshape(variableMul, mulShape);
const addSqueeze = this.builder_.reshape(variableAdd, addShape);
return this.builder_.instanceNormalization(conv2D, {scale: mulSqueeze, bias: addSqueeze});
Expand Down

0 comments on commit dc42b35

Please sign in to comment.