Skip to content

Commit

Permalink
Add a duplicate entry to support new "dataType"
Browse files Browse the repository at this point in the history
WebNN spec renames "type" as "dataType" at
webmachinelearning/webnn#464, add a duplicate entry
for "dataType" in order to workaround the compatibility issue.
  • Loading branch information
Honry committed Nov 2, 2023
1 parent b138e20 commit 306af79
Show file tree
Hide file tree
Showing 27 changed files with 164 additions and 73 deletions.
4 changes: 2 additions & 2 deletions code/samples/matmul.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ const context = await navigator.ml.createContext();
// The following code multiplies matrix a [3, 4] with matrix b [4, 3]
// into matrix c [3, 3].
const builder = new MLGraphBuilder(context);
const descA = {type: 'float32', dimensions: [3, 4]};
const descA = {type: 'float32', dataType: 'float32', dimensions: [3, 4]};
const a = builder.input('a', descA);
const descB = {type: 'float32', dimensions: [4, 3]};
const descB = {type: 'float32', dataType: 'float32', dimensions: [4, 3]};
const bufferB = new Float32Array(sizeOfShape(descB.dimensions)).fill(0.5);
const b = builder.constant(descB, bufferB);
const c = builder.matmul(a, b);
Expand Down
2 changes: 1 addition & 1 deletion code/samples/mul_add.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const operandType = {type: 'float32', dimensions: [2, 2]};
const operandType = {type: 'float32', dataType: 'float32', dimensions: [2, 2]};
const context = await navigator.ml.createContext();
const builder = new MLGraphBuilder(context);
// 1. Create a computational graph 'C = 0.2 * A + B'.
Expand Down
6 changes: 3 additions & 3 deletions code/samples/optional_outputs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ const context = await navigator.ml.createContext();

// Build a graph with two outputs.
const builder = new MLGraphBuilder(context);
const descA = {type: 'float32', dimensions: [3, 4]};
const descA = {type: 'float32', dataType: 'float32', dimensions: [3, 4]};
const a = builder.input('a', descA);
const descB = {type: 'float32', dimensions: [4, 3]};
const descB = {type: 'float32', dataType: 'float32', dimensions: [4, 3]};
const bufferB = new Float32Array(sizeOfShape(descB.dimensions)).fill(0.5);
const b = builder.constant(descB, bufferB);
const descC = {type: 'float32', dimensions: [3, 3]};
const descC = {type: 'float32', dataType: 'float32', dimensions: [3, 3]};
const bufferC = new Float32Array(sizeOfShape(descC.dimensions)).fill(1);
const c = builder.constant(descC, bufferC);
const d = builder.matmul(a, b);
Expand Down
2 changes: 1 addition & 1 deletion code/samples/simple_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const TENSOR_SIZE = 8;
const builder = new MLGraphBuilder(context);

// Create MLOperandDescriptor object.
const desc = {type: 'float32', dimensions: TENSOR_DIMS};
const desc = {type: 'float32', dataType: 'float32', dimensions: TENSOR_DIMS};

// constant1 is a constant MLOperand with the value 0.5.
const constantBuffer1 = new Float32Array(TENSOR_SIZE).fill(0.5);
Expand Down
2 changes: 1 addition & 1 deletion common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export async function buildConstantByNpy(builder, url) {
const dataView = new Uint8Array(npArray.data.buffer);
const dataView2 = dataView.slice();
const typedArray = new TypedArrayConstructor(dataView2.buffer);
return builder.constant({type, dimensions}, typedArray);
return builder.constant({dataType: type, type, dimensions}, typedArray);
}

// Convert video frame to a canvas element
Expand Down
7 changes: 5 additions & 2 deletions face_recognition/facenet_nchw.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,11 @@ export class FaceNetNchw {
async load(contextOptions) {
this.context_ = await navigator.ml.createContext(contextOptions);
this.builder_ = new MLGraphBuilder(this.context_);
const input = this.builder_.input('input',
{type: 'float32', dimensions: this.inputOptions.inputDimensions});
const input = this.builder_.input('input', {
type: 'float32',
dataType: 'float32',
dimensions: this.inputOptions.inputDimensions,
});

const poolOptions = {windowDimensions: [3, 3], strides};

Expand Down
7 changes: 5 additions & 2 deletions face_recognition/facenet_nhwc.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,11 @@ export class FaceNetNhwc {
async load(contextOptions) {
this.context_ = await navigator.ml.createContext(contextOptions);
this.builder_ = new MLGraphBuilder(this.context_);
const input = this.builder_.input('input',
{type: 'float32', dimensions: this.inputOptions.inputDimensions});
const input = this.builder_.input('input', {
type: 'float32',
dataType: 'float32',
dimensions: this.inputOptions.inputDimensions,
});

const poolOptions = {windowDimensions: [3, 3], strides, layout: 'nhwc'};

Expand Down
7 changes: 5 additions & 2 deletions facial_landmark_detection/face_landmark_nchw.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ export class FaceLandmarkNchw {
async load(contextOptions) {
this.context_ = await navigator.ml.createContext(contextOptions);
this.builder_ = new MLGraphBuilder(this.context_);
const input = this.builder_.input('input',
{type: 'float32', dimensions: this.inputOptions.inputDimensions});
const input = this.builder_.input('input', {
type: 'float32',
dataType: 'float32',
dimensions: this.inputOptions.inputDimensions,
});

const poolOptions =
{windowDimensions: [2, 2], strides: [2, 2]};
Expand Down
7 changes: 5 additions & 2 deletions facial_landmark_detection/face_landmark_nhwc.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@ export class FaceLandmarkNhwc {
async load(contextOptions) {
this.context_ = await navigator.ml.createContext(contextOptions);
this.builder_ = new MLGraphBuilder(this.context_);
const input = this.builder_.input('input',
{type: 'float32', dimensions: this.inputOptions.inputDimensions});
const input = this.builder_.input('input', {
type: 'float32',
dataType: 'float32',
dimensions: this.inputOptions.inputDimensions,
});

const poolOptions =
{windowDimensions: [2, 2], strides: [2, 2], layout: 'nhwc'};
Expand Down
7 changes: 5 additions & 2 deletions facial_landmark_detection/ssd_mobilenetv2_face_nchw.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,11 @@ ${nameArray[1]}`;
this.context_ = await navigator.ml.createContext(contextOptions);
this.deviceType_ = contextOptions.deviceType;
this.builder_ = new MLGraphBuilder(this.context_);
const input = this.builder_.input('input',
{type: 'float32', dimensions: this.inputOptions.inputDimensions});
const input = this.builder_.input('input', {
type: 'float32',
dataType: 'float32',
dimensions: this.inputOptions.inputDimensions,
});

const bottleneck0 = await this.buildLinearBottleneck_(
input, '0', false, 32, 'convRelu6');
Expand Down
7 changes: 5 additions & 2 deletions facial_landmark_detection/ssd_mobilenetv2_face_nhwc.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,11 @@ ${nameArray[1]}`;
this.context_ = await navigator.ml.createContext(contextOptions);
this.deviceType_ = contextOptions.deviceType;
this.builder_ = new MLGraphBuilder(this.context_);
const input = this.builder_.input('input',
{type: 'float32', dimensions: this.inputOptions.inputDimensions});
const input = this.builder_.input('input', {
type: 'float32',
dataType: 'float32',
dimensions: this.inputOptions.inputDimensions,
});

const bottleneck0 = await this.buildLinearBottleneck_(
input, '0', false, 32, 'convRelu6');
Expand Down
7 changes: 5 additions & 2 deletions image_classification/mobilenet_nchw.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@ export class MobileNetV2Nchw {
this.context_ = await navigator.ml.createContext(contextOptions);
this.deviceType_ = contextOptions.deviceType;
this.builder_ = new MLGraphBuilder(this.context_);
const data = this.builder_.input('input',
{type: 'float32', dimensions: this.inputOptions.inputDimensions});
const data = this.builder_.input('input', {
type: 'float32',
dataType: 'float32',
dimensions: this.inputOptions.inputDimensions,
});
const conv0 = await this.buildConv_(
data, '0', true, {padding: [1, 1, 1, 1], strides: [2, 2]});
const conv1 = await this.buildConv_(
Expand Down
7 changes: 5 additions & 2 deletions image_classification/mobilenet_nhwc.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,11 @@ export class MobileNetV2Nhwc {
const strides = [2, 2];
const autoPad = 'same-upper';
const filterLayout = 'ohwi';
const input = this.builder_.input(
'input', {type: 'float32', dimensions: this.inputOptions.inputDimensions});
const input = this.builder_.input('input', {
type: 'float32',
dataType: 'float32',
dimensions: this.inputOptions.inputDimensions,
});
const conv0 = await this.buildConv_(
input, '90', 'Conv_Conv2D', true, {strides, autoPad, filterLayout});
const conv1 = await this.buildConv_(
Expand Down
7 changes: 5 additions & 2 deletions image_classification/resnet50v2_nchw.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,11 @@ export class ResNet50V2Nchw {
async load(contextOptions) {
this.context_ = await navigator.ml.createContext(contextOptions);
this.builder_ = new MLGraphBuilder(this.context_);
const data = this.builder_.input('input',
{type: 'float32', dimensions: this.inputOptions.inputDimensions});
const data = this.builder_.input('input', {
type: 'float32',
dataType: 'float32',
dimensions: this.inputOptions.inputDimensions,
});
const bn1 = await this.buildBatchNorm_(data, '0', '', false);
const conv0 = await this.buildConv_(
bn1, '0', '', {padding: [3, 3, 3, 3], strides: [2, 2]});
Expand Down
7 changes: 5 additions & 2 deletions image_classification/resnet50v2_nhwc.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,11 @@ export class ResNet50V2Nhwc {
async load(contextOptions) {
this.context_ = await navigator.ml.createContext(contextOptions);
this.builder_ = new MLGraphBuilder(this.context_);
const input = this.builder_.input('input',
{type: 'float32', dimensions: this.inputOptions.inputDimensions});
const input = this.builder_.input('input', {
type: 'float32',
dataType: 'float32',
dimensions: this.inputOptions.inputDimensions,
});
const conv1 = await this.buildConv_(
input, ['', '', '1'], {strides, padding: [3, 3, 3, 3]}, false);
const pool = this.builder_.maxPool2d(
Expand Down
7 changes: 5 additions & 2 deletions image_classification/squeezenet_nchw.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ export class SqueezeNetNchw {
async load(contextOptions) {
this.context_ = await navigator.ml.createContext(contextOptions);
this.builder_ = new MLGraphBuilder(this.context_);
const data = this.builder_.input('input',
{type: 'float32', dimensions: this.inputOptions.inputDimensions});
const data = this.builder_.input('input', {
type: 'float32',
dataType: 'float32',
dimensions: this.inputOptions.inputDimensions,
});
const conv0 = await this.buildConv_(data, 'conv0', {strides: [2, 2]});
const pool0 = this.builder_.maxPool2d(
conv0, {windowDimensions: [3, 3], strides: [2, 2]});
Expand Down
7 changes: 5 additions & 2 deletions image_classification/squeezenet_nhwc.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ export class SqueezeNetNhwc {
this.builder_ = new MLGraphBuilder(this.context_);
const strides = [2, 2];
const layout = 'nhwc';
const placeholder = this.builder_.input('input',
{type: 'float32', dimensions: this.inputOptions.inputDimensions});
const placeholder = this.builder_.input('input', {
type: 'float32',
dataType: 'float32',
dimensions: this.inputOptions.inputDimensions,
});
const conv1 = await this.buildConv_(
placeholder, 'conv1', {strides, autoPad: 'same-upper'});
const maxpool1 = this.builder_.maxPool2d(
Expand Down
28 changes: 17 additions & 11 deletions lenet/lenet.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@ export class LeNet {
this.context_ = await navigator.ml.createContext(contextOptions);
this.builder_ = new MLGraphBuilder(this.context_);
const inputShape = [1, 1, 28, 28];
const input =
this.builder_.input('input', {type: 'float32', dimensions: inputShape});
const input = this.builder_.input('input', {
type: 'float32',
dataType: 'float32',
dimensions: inputShape,
});

const conv1FitlerShape = [20, 1, 5, 5];
let byteOffset = 0;
const conv1FilterData = new Float32Array(
arrayBuffer, byteOffset, sizeOfShape(conv1FitlerShape));
const conv1Filter = this.builder_.constant(
{type: 'float32', dimensions: conv1FitlerShape},
{type: 'float32', dataType: 'float32', dimensions: conv1FitlerShape},
conv1FilterData);
byteOffset +=
sizeOfShape(conv1FitlerShape) * Float32Array.BYTES_PER_ELEMENT;
Expand All @@ -39,7 +42,9 @@ export class LeNet {
const add1BiasData =
new Float32Array(arrayBuffer, byteOffset, sizeOfShape(add1BiasShape));
const add1Bias = this.builder_.constant(
{type: 'float32', dimensions: add1BiasShape}, add1BiasData);
{type: 'float32', dataType: 'float32', dimensions: add1BiasShape},
add1BiasData,
);
byteOffset += sizeOfShape(add1BiasShape) * Float32Array.BYTES_PER_ELEMENT;
const add1 = this.builder_.add(conv1, add1Bias);

Expand All @@ -51,16 +56,17 @@ export class LeNet {

const conv2FilterShape = [50, 20, 5, 5];
const conv2Filter = this.builder_.constant(
{type: 'float32', dimensions: conv2FilterShape},
{type: 'float32', dataType: 'float32', dimensions: conv2FilterShape},
new Float32Array(
arrayBuffer, byteOffset, sizeOfShape(conv2FilterShape)));
arrayBuffer, byteOffset, sizeOfShape(conv2FilterShape)),
);
byteOffset +=
sizeOfShape(conv2FilterShape) * Float32Array.BYTES_PER_ELEMENT;
const conv2 = this.builder_.conv2d(pool1, conv2Filter);

const add2BiasShape = [1, 50, 1, 1];
const add2Bias = this.builder_.constant(
{type: 'float32', dimensions: add2BiasShape},
{type: 'float32', dataType: 'float32', dimensions: add2BiasShape},
new Float32Array(arrayBuffer, byteOffset, sizeOfShape(add2BiasShape)));
byteOffset += sizeOfShape(add2BiasShape) * Float32Array.BYTES_PER_ELEMENT;
const add2 = this.builder_.add(conv2, add2Bias);
Expand All @@ -79,15 +85,15 @@ export class LeNet {

const matmul1Shape = [500, 800];
const matmul1Weights = this.builder_.constant(
{type: 'float32', dimensions: matmul1Shape},
{type: 'float32', dataType: 'float32', dimensions: matmul1Shape},
new Float32Array(arrayBuffer, byteOffset, sizeOfShape(matmul1Shape)));
byteOffset += sizeOfShape(matmul1Shape) * Float32Array.BYTES_PER_ELEMENT;
const matmul1WeightsTransposed = this.builder_.transpose(matmul1Weights);
const matmul1 = this.builder_.matmul(reshape1, matmul1WeightsTransposed);

const add3BiasShape = [1, 500];
const add3Bias = this.builder_.constant(
{type: 'float32', dimensions: add3BiasShape},
{type: 'float32', dataType: 'float32', dimensions: add3BiasShape},
new Float32Array(arrayBuffer, byteOffset, sizeOfShape(add3BiasShape)));
byteOffset += sizeOfShape(add3BiasShape) * Float32Array.BYTES_PER_ELEMENT;
const add3 = this.builder_.add(matmul1, add3Bias);
Expand All @@ -99,15 +105,15 @@ export class LeNet {

const matmul2Shape = [10, 500];
const matmul2Weights = this.builder_.constant(
{type: 'float32', dimensions: matmul2Shape},
{type: 'float32', dataType: 'float32', dimensions: matmul2Shape},
new Float32Array(arrayBuffer, byteOffset, sizeOfShape(matmul2Shape)));
byteOffset += sizeOfShape(matmul2Shape) * Float32Array.BYTES_PER_ELEMENT;
const matmul2WeightsTransposed = this.builder_.transpose(matmul2Weights);
const matmul2 = this.builder_.matmul(reshape2, matmul2WeightsTransposed);

const add4BiasShape = [1, 10];
const add4Bias = this.builder_.constant(
{type: 'float32', dimensions: add4BiasShape},
{type: 'float32', dataType: 'float32', dimensions: add4BiasShape},
new Float32Array(arrayBuffer, byteOffset, sizeOfShape(add4BiasShape)));
const add4 = this.builder_.add(matmul2, add4Bias);

Expand Down
20 changes: 15 additions & 5 deletions nsnet2/nsnet2.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,26 @@ export class NSNet2 {
const weight217 = await buildConstantByNpy(this.builder_, baseUrl + '217.npy');
const biasFcOut4 = await buildConstantByNpy(this.builder_, baseUrl + 'fc_out_4_bias.npy');
// Build up the network.
const input = this.builder_.input('input', {type: 'float32', dimensions: [batchSize, frames, this.frameSize]});
const input = this.builder_.input('input', {
type: 'float32',
dataType: 'float32',
dimensions: [batchSize, frames, this.frameSize],
});
const relu20 = this.builder_.relu(this.builder_.add(this.builder_.matmul(input, weight172), biasFcIn0));
const transpose31 = this.builder_.transpose(relu20, {permutation: [1, 0, 2]});
const initialState92 = this.builder_.input(
'initialState92', {type: 'float32', dimensions: [1, batchSize, this.hiddenSize]});
const initialState92 = this.builder_.input('initialState92', {
type: 'float32',
dataType: 'float32',
dimensions: [1, batchSize, this.hiddenSize],
});
const [gru94, gru93] = this.builder_.gru(transpose31, weight192, recurrentWeight193, frames, this.hiddenSize,
{bias: bias194, recurrentBias: recurrentBias194, initialHiddenState: initialState92, returnSequence: true});
const squeeze95 = this.builder_.squeeze(gru93, {axes: [1]});
const initialState155 = this.builder_.input(
'initialState155', {type: 'float32', dimensions: [1, batchSize, this.hiddenSize]});
const initialState155 = this.builder_.input('initialState155', {
type: 'float32',
dataType: 'float32',
dimensions: [1, batchSize, this.hiddenSize],
});
const [gru157, gru156] = this.builder_.gru(squeeze95, weight212, recurrentWeight213, frames, this.hiddenSize,
{bias: bias214, recurrentBias: recurrentBias214, initialHiddenState: initialState155, returnSequence: true});
const squeeze158 = this.builder_.squeeze(gru156, {axes: [1]});
Expand Down
7 changes: 5 additions & 2 deletions object_detection/ssd_mobilenetv1_nchw.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,11 @@ ${nameArray[1]}_BatchNorm_batchnorm`;
this.context_ = await navigator.ml.createContext(contextOptions);
this.deviceType_ = contextOptions.deviceType;
this.builder_ = new MLGraphBuilder(this.context_);
const input = this.builder_.input('input',
{type: 'float32', dimensions: this.inputOptions.inputDimensions});
const input = this.builder_.input('input', {
type: 'float32',
dataType: 'float32',
dimensions: this.inputOptions.inputDimensions,
});
const strides = [2, 2];
const conv0 = await this.buildConv_(
input, ['', '0', '', '165__cf__168'],
Expand Down
7 changes: 5 additions & 2 deletions object_detection/ssd_mobilenetv1_nhwc.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,11 @@ ${nameArray[1]}_BatchNorm_batchnorm`;
this.context_ = await navigator.ml.createContext(contextOptions);
this.deviceType_ = contextOptions.deviceType;
this.builder_ = new MLGraphBuilder(this.context_);
const input = this.builder_.input('input',
{type: 'float32', dimensions: this.inputOptions.inputDimensions});
const input = this.builder_.input('input', {
type: 'float32',
dataType: 'float32',
dimensions: this.inputOptions.inputDimensions,
});
const strides = [2, 2];
const conv0 = await this.buildConv_(
input, ['', '0', '', '165__cf__168'], true, {strides});
Expand Down
19 changes: 13 additions & 6 deletions object_detection/tiny_yolov2_nchw.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,20 @@ export class TinyYoloV2Nchw {
async load(contextOptions) {
this.context_ = await navigator.ml.createContext(contextOptions);
this.builder_ = new MLGraphBuilder(this.context_);
const image = this.builder_.input('input',
{type: 'float32', dimensions: this.inputOptions.inputDimensions});
const image = this.builder_.input('input', {
type: 'float32',
dataType: 'float32',
dimensions: this.inputOptions.inputDimensions,
});

const mulScale = this.builder_.constant({type: 'float32',
dimensions: [1]}, new Float32Array([0.003921568859368563]));
const addBias = this.builder_.constant({type: 'float32',
dimensions: [3, 1, 1]}, new Float32Array([0, 0, 0]));
const mulScale = this.builder_.constant(
{type: 'float32', dataType: 'float32', dimensions: [1]},
new Float32Array([0.003921568859368563]),
);
const addBias = this.builder_.constant(
{type: 'float32', dataType: 'float32', dimensions: [3, 1, 1]},
new Float32Array([0, 0, 0]),
);
const poolOptions = {
windowDimensions: [2, 2],
strides: [2, 2],
Expand Down
Loading

0 comments on commit 306af79

Please sign in to comment.