Skip to content

Commit

Permalink
Use boolean flags instead of MLTensorUsage (#287)
Browse files Browse the repository at this point in the history
Adapt to Chromium update:
https://chromium-review.googlesource.com/c/chromium/src/+/5933323

We will keep MLTensorUsage until it is remove from Chromium.
  • Loading branch information
Honry authored Oct 25, 2024
1 parent fff8a4b commit efa8974
Show file tree
Hide file tree
Showing 28 changed files with 66 additions and 0 deletions.
3 changes: 3 additions & 0 deletions code/samples/matmul.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ const graph = await builder.build({c});
const bufferA = new Float32Array(3*4).fill(1.0);
const bufferB = new Float32Array(4*3).fill(0.8);
descA.usage = MLTensorUsage.WRITE;
descA.writable = true;
descB.usage = MLTensorUsage.WRITE;
descB.writable = true;
const tensorA = await context.createTensor(descA);
const tensorB = await context.createTensor(descB);
context.writeTensor(tensorA, bufferA);
Expand All @@ -23,6 +25,7 @@ const tensorC = await context.createTensor({
dimensions: [3, 3],
shape: [3, 3],
usage: MLTensorUsage.READ,
readable: true,
});
context.dispatch(graph, {a: tensorA, b: tensorB}, {c: tensorC});
const results = await context.readTensor(tensorC);
Expand Down
2 changes: 2 additions & 0 deletions code/samples/mul_add.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ const graph = await builder.build({'C': C});
const bufferA = new Float32Array(4).fill(1.0);
const bufferB = new Float32Array(4).fill(0.8);
desc.usage = MLTensorUsage.WRITE;
desc.writable = true;
const tensorA = await context.createTensor(desc);
const tensorB = await context.createTensor(desc);
context.writeTensor(tensorA, bufferA);
context.writeTensor(tensorB, bufferB);
const tensorC = await context.createTensor({
...desc,
usage: MLTensorUsage.READ,
readable: true,
});
const inputs = {'A': tensorA, 'B': tensorB};
const outputs = {'C': tensorC};
Expand Down
2 changes: 2 additions & 0 deletions code/samples/simple_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const inputBuffer1 = new Float32Array(TENSOR_SIZE).fill(1);
const inputBuffer2 = new Float32Array(TENSOR_SIZE).fill(1);

desc.usage = MLTensorUsage.WRITE;
desc.writable = true;
const inputTensor1 = await context.createTensor(desc);
const inputTensor2 = await context.createTensor(desc);
context.writeTensor(inputTensor1, inputBuffer1);
Expand All @@ -60,6 +61,7 @@ context.writeTensor(inputTensor2, inputBuffer2);
const outputTensor = await context.createTensor({
...desc,
usage: MLTensorUsage.READ,
readable: true,
});

// Execute the compiled graph with the specified inputs.
Expand Down
2 changes: 2 additions & 0 deletions face_recognition/facenet_nchw.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,14 @@ export class FaceNetNchw {
};
const input = this.builder_.input('input', inputDesc);
inputDesc.usage = MLTensorUsage.WRITE;
inputDesc.writable = true;
this.inputTensor_ = await this.context_.createTensor(inputDesc);
this.outputTensor_ = await this.context_.createTensor({
dataType: 'float32',
dimensions: this.outputShape_,
shape: this.outputShape_,
usage: MLTensorUsage.READ,
readable: true,
});

const poolOptions = {windowDimensions: [3, 3], strides};
Expand Down
2 changes: 2 additions & 0 deletions face_recognition/facenet_nhwc.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,14 @@ export class FaceNetNhwc {
};
const input = this.builder_.input('input', inputDesc);
inputDesc.usage = MLTensorUsage.WRITE;
inputDesc.writable = true;
this.inputTensor_ = await this.context_.createTensor(inputDesc);
this.outputTensor_ = await this.context_.createTensor({
dataType: 'float32',
dimensions: this.outputShape_,
shape: this.outputShape_,
usage: MLTensorUsage.READ,
readable: true,
});

const poolOptions = {windowDimensions: [3, 3], strides, layout: 'nhwc'};
Expand Down
2 changes: 2 additions & 0 deletions facial_landmark_detection/face_landmark_nchw.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,14 @@ export class FaceLandmarkNchw {
};
const input = this.builder_.input('input', inputDesc);
inputDesc.usage = MLTensorUsage.WRITE;
inputDesc.writable = true;
this.inputTensor_ = await this.context_.createTensor(inputDesc);
this.outputTensor_ = await this.context_.createTensor({
dataType: 'float32',
dimensions: this.outputShape_,
shape: this.outputShape_,
usage: MLTensorUsage.READ,
readable: true,
});

const poolOptions =
Expand Down
2 changes: 2 additions & 0 deletions facial_landmark_detection/face_landmark_nhwc.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,14 @@ export class FaceLandmarkNhwc {
};
const input = this.builder_.input('input', inputDesc);
inputDesc.usage = MLTensorUsage.WRITE;
inputDesc.writable = true;
this.inputTensor_ = await this.context_.createTensor(inputDesc);
this.outputTensor_ = await this.context_.createTensor({
dataType: 'float32',
dimensions: this.outputShape_,
shape: this.outputShape_,
usage: MLTensorUsage.READ,
readable: true,
});

const poolOptions =
Expand Down
2 changes: 2 additions & 0 deletions facial_landmark_detection/ssd_mobilenetv2_face_nchw.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,15 @@ ${nameArray[1]}`;
};
const input = this.builder_.input('input', inputDesc);
inputDesc.usage = MLTensorUsage.WRITE;
inputDesc.writable = true;
this.inputTensor_ = await this.context_.createTensor(inputDesc);
for (const [key, value] of Object.entries(this.outputsInfo)) {
this.outputTensors_[key] = await this.context_.createTensor({
dataType: 'float32',
dimensions: value,
shape: value,
usage: MLTensorUsage.READ,
readable: true,
});
}

Expand Down
2 changes: 2 additions & 0 deletions facial_landmark_detection/ssd_mobilenetv2_face_nhwc.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,15 @@ ${nameArray[1]}`;
};
const input = this.builder_.input('input', inputDesc);
inputDesc.usage = MLTensorUsage.WRITE;
inputDesc.writable = true;
this.inputTensor_ = await this.context_.createTensor(inputDesc);
for (const [key, value] of Object.entries(this.outputsInfo)) {
this.outputTensors_[key] = await this.context_.createTensor({
dataType: 'float32',
dimensions: value,
shape: value,
usage: MLTensorUsage.READ,
readable: true,
});
}

Expand Down
2 changes: 2 additions & 0 deletions image_classification/efficientnet_fp16_nchw.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,14 @@ export class EfficientNetFP16Nchw {
};
let data = this.builder_.input('input', inputDesc);
inputDesc.usage = MLTensorUsage.WRITE;
inputDesc.writable = true;
this.inputTensor_ = await this.context_.createTensor(inputDesc);
this.outputTensor_ = await this.context_.createTensor({
dataType: 'float32',
dimensions: this.outputShape_,
shape: this.outputShape_,
usage: MLTensorUsage.READ,
readable: true,
});
data = this.builder_.cast(data, 'float16');
// Block 0
Expand Down
2 changes: 2 additions & 0 deletions image_classification/mobilenet_nchw.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,14 @@ export class MobileNetV2Nchw {
};
let data = this.builder_.input('input', inputDesc);
inputDesc.usage = MLTensorUsage.WRITE;
inputDesc.writable = true;
this.inputTensor_ = await this.context_.createTensor(inputDesc);
this.outputTensor_ = await this.context_.createTensor({
dataType: 'float32',
dimensions: this.outputShape_,
shape: this.outputShape_,
usage: MLTensorUsage.READ,
readable: true,
});
if (this.dataType_ === 'float16') {
data = this.builder_.cast(data, 'float16');
Expand Down
2 changes: 2 additions & 0 deletions image_classification/mobilenet_nhwc.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,14 @@ export class MobileNetV2Nhwc {
};
const input = this.builder_.input('input', inputDesc);
inputDesc.usage = MLTensorUsage.WRITE;
inputDesc.writable = true;
this.inputTensor_ = await this.context_.createTensor(inputDesc);
this.outputTensor_ = await this.context_.createTensor({
dataType: 'float32',
dimensions: this.outputShape_,
shape: this.outputShape_,
usage: MLTensorUsage.READ,
readable: true,
});
const conv0 = this.buildConv_(
input, '90', 'Conv_Conv2D', true, {strides, autoPad, filterLayout});
Expand Down
2 changes: 2 additions & 0 deletions image_classification/resnet50v1_fp16_nchw.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,14 @@ export class ResNet50V1FP16Nchw {
};
let data = this.builder_.input('input', inputDesc);
inputDesc.usage = MLTensorUsage.WRITE;
inputDesc.writable = true;
this.inputTensor_ = await this.context_.createTensor(inputDesc);
this.outputTensor_ = await this.context_.createTensor({
dataType: 'float32',
dimensions: this.outputShape_,
shape: this.outputShape_,
usage: MLTensorUsage.READ,
readable: true,
});
data = this.builder_.cast(data, 'float16');
const conv1 = await this.buildConv_(
Expand Down
2 changes: 2 additions & 0 deletions image_classification/resnet50v2_nchw.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,14 @@ export class ResNet50V2Nchw {
};
const data = this.builder_.input('input', inputDesc);
inputDesc.usage = MLTensorUsage.WRITE;
inputDesc.writable = true;
this.inputTensor_ = await this.context_.createTensor(inputDesc);
this.outputTensor_ = await this.context_.createTensor({
dataType: 'float32',
dimensions: this.outputShape_,
shape: this.outputShape_,
usage: MLTensorUsage.READ,
readable: true,
});
const bn1 = this.buildBatchNorm_(data, '0', '', false);
const conv0 = this.buildConv_(
Expand Down
2 changes: 2 additions & 0 deletions image_classification/resnet50v2_nhwc.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,14 @@ export class ResNet50V2Nhwc {
};
const input = this.builder_.input('input', inputDesc);
inputDesc.usage = MLTensorUsage.WRITE;
inputDesc.writable = true;
this.inputTensor_ = await this.context_.createTensor(inputDesc);
this.outputTensor_ = await this.context_.createTensor({
dataType: 'float32',
dimensions: this.outputShape_,
shape: this.outputShape_,
usage: MLTensorUsage.READ,
readable: true,
});
const conv1 = await this.buildConv_(
input, ['', '', '1'], {strides, padding: [3, 3, 3, 3]}, false);
Expand Down
2 changes: 2 additions & 0 deletions image_classification/squeezenet_nchw.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ export class SqueezeNetNchw {
};
const data = this.builder_.input('input', inputDesc);
inputDesc.usage = MLTensorUsage.WRITE;
inputDesc.writable = true;
this.inputTensor_ = await this.context_.createTensor(inputDesc);
this.outputTensor_ = await this.context_.createTensor({
dataType: 'float32',
dimensions: this.outputShape_,
shape: this.outputShape_,
usage: MLTensorUsage.READ,
readable: true,
});
const conv0 = this.buildConv_(data, 'conv0', {strides: [2, 2]});
const pool0 = this.builder_.maxPool2d(
Expand Down
2 changes: 2 additions & 0 deletions image_classification/squeezenet_nhwc.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ export class SqueezeNetNhwc {
};
const placeholder = this.builder_.input('input', inputDesc);
inputDesc.usage = MLTensorUsage.WRITE;
inputDesc.writable = true;
this.inputTensor_ = await this.context_.createTensor(inputDesc);
this.outputTensor_ = await this.context_.createTensor({
dataType: 'float32',
dimensions: this.outputShape_,
shape: this.outputShape_,
usage: MLTensorUsage.READ,
readable: true,
});
const conv1 = this.buildConv_(
placeholder, 'conv1', {strides, autoPad: 'same-upper'});
Expand Down
2 changes: 2 additions & 0 deletions lenet/lenet.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ export class LeNet {
};
let input = this.builder_.input('input', inputDesc);
inputDesc.usage = MLTensorUsage.WRITE;
inputDesc.writable = true;
this.inputTensor_ = await this.context_.createTensor(inputDesc);
this.outputTensor_ = await this.context_.createTensor({
dataType: 'float32',
dimensions: this.outputShape_,
shape: this.outputShape_,
usage: MLTensorUsage.READ,
readable: true,
});
if (this.layout_ === 'nhwc') {
input = this.builder_.transpose(
Expand Down
1 change: 1 addition & 0 deletions nnotepad/js/nnotepad.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class WebNNUtil {
dimensions: operand.shape(),
shape: operand.shape(),
usage: MLTensorUsage.READ,
readable: true,
};
const tensor = await context.createTensor(desc);
return tensor;
Expand Down
4 changes: 4 additions & 0 deletions nsnet2/nsnet2.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export class NSNet2 {
const input = this.builder_.input('input', inputDesc);

inputDesc.usage = MLTensorUsage.WRITE;
inputDesc.writable = true;
this.inputTensor_ = await this.context_.createTensor(inputDesc);

const relu20 = this.builder_.relu(this.builder_.add(this.builder_.matmul(input, weight172), biasFcIn0));
Expand All @@ -63,6 +64,7 @@ export class NSNet2 {
const initialState155 = this.builder_.input('initialState155', initialStateDesc);

initialStateDesc.usage = MLTensorUsage.WRITE;
initialStateDesc.writable = true;
this.initialState92Tensor_ = await this.context_.createTensor(initialStateDesc);
this.initialState155Tensor_ = await this.context_.createTensor(initialStateDesc);

Expand All @@ -71,13 +73,15 @@ export class NSNet2 {
dimensions: inputShape,
shape: inputShape, // Same as inputShape.
usage: MLTensorUsage.READ,
readable: true,
});
const gruOutputShape = [1, batchSize, this.hiddenSize];
const gruOutputDesc = {
dataType: 'float32',
dimensions: gruOutputShape,
shape: gruOutputShape,
usage: MLTensorUsage.READ,
readable: true,
};
this.gru94Tensor_ = await this.context_.createTensor(gruOutputDesc);
this.gru157Tensor_ = await this.context_.createTensor(gruOutputDesc);
Expand Down
3 changes: 3 additions & 0 deletions object_detection/ssd_mobilenetv1_nchw.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,21 @@ ${nameArray[1]}_BatchNorm_batchnorm`;
};
let input = this.builder_.input('input', inputDesc);
inputDesc.usage = MLTensorUsage.WRITE;
inputDesc.writable = true;
this.inputTensor_ = await this.context_.createTensor(inputDesc);
this.boxesTensor_ = await this.context_.createTensor({
dataType: 'float32',
dimensions: this.boxesShape_,
shape: this.boxesShape_,
usage: MLTensorUsage.READ,
readable: true,
});
this.scoresTensor_ = await this.context_.createTensor({
dataType: 'float32',
dimensions: this.scoresShape_,
shape: this.scoresShape_,
usage: MLTensorUsage.READ,
readable: true,
});

if (this.targetDataType_ === 'float16') {
Expand Down
3 changes: 3 additions & 0 deletions object_detection/ssd_mobilenetv1_nhwc.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,21 @@ ${nameArray[1]}_BatchNorm_batchnorm`;
};
const input = this.builder_.input('input', inputDesc);
inputDesc.usage = MLTensorUsage.WRITE;
inputDesc.writable = true;
this.inputTensor_ = await this.context_.createTensor(inputDesc);
this.boxesTensor_ = await this.context_.createTensor({
dataType: 'float32',
dimensions: this.boxesShape_,
shape: this.boxesShape_,
usage: MLTensorUsage.READ,
readable: true,
});
this.scoresTensor_ = await this.context_.createTensor({
dataType: 'float32',
dimensions: this.scoresShape_,
shape: this.scoresShape_,
usage: MLTensorUsage.READ,
readable: true,
});

const strides = [2, 2];
Expand Down
2 changes: 2 additions & 0 deletions object_detection/tiny_yolov2_nchw.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,14 @@ export class TinyYoloV2Nchw {
};
let image = this.builder_.input('input', inputDesc);
inputDesc.usage = MLTensorUsage.WRITE;
inputDesc.writable = true;
this.inputTensor_ = await this.context_.createTensor(inputDesc);
this.outputTensor_ = await this.context_.createTensor({
dataType: 'float32',
dimensions: this.outputShape_,
shape: this.outputShape_,
usage: MLTensorUsage.READ,
readable: true,
});

let mulScale = this.builder_.constant(
Expand Down
2 changes: 2 additions & 0 deletions object_detection/tiny_yolov2_nhwc.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ export class TinyYoloV2Nhwc {
};
const input = this.builder_.input('input', inputDesc);
inputDesc.usage = MLTensorUsage.WRITE;
inputDesc.writable = true;
this.inputTensor_ = await this.context_.createTensor(inputDesc);
this.outputTensor_ = await this.context_.createTensor({
dataType: 'float32',
dimensions: this.outputShape_,
shape: this.outputShape_,
usage: MLTensorUsage.READ,
readable: true,
});

const poolOptions = {
Expand Down
Loading

0 comments on commit efa8974

Please sign in to comment.