Skip to content

Commit 67f3db6

Browse files
authored
Add a duplicate entry to support new "dataType" (#186)
WebNN spec renames "type" as "dataType" at webmachinelearning/webnn#464, add a duplicate entry for "dataType" in order to workaround the compatibility issue.
1 parent b6ad9c8 commit 67f3db6

27 files changed

Lines changed: 164 additions & 73 deletions

code/samples/matmul.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ const context = await navigator.ml.createContext();
33
// The following code multiplies matrix a [3, 4] with matrix b [4, 3]
44
// into matrix c [3, 3].
55
const builder = new MLGraphBuilder(context);
6-
const descA = {type: 'float32', dimensions: [3, 4]};
6+
const descA = {type: 'float32', dataType: 'float32', dimensions: [3, 4]};
77
const a = builder.input('a', descA);
8-
const descB = {type: 'float32', dimensions: [4, 3]};
8+
const descB = {type: 'float32', dataType: 'float32', dimensions: [4, 3]};
99
const bufferB = new Float32Array(sizeOfShape(descB.dimensions)).fill(0.5);
1010
const b = builder.constant(descB, bufferB);
1111
const c = builder.matmul(a, b);

code/samples/mul_add.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const operandType = {type: 'float32', dimensions: [2, 2]};
1+
const operandType = {type: 'float32', dataType: 'float32', dimensions: [2, 2]};
22
const context = await navigator.ml.createContext();
33
const builder = new MLGraphBuilder(context);
44
// 1. Create a computational graph 'C = 0.2 * A + B'.

code/samples/optional_outputs.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ const context = await navigator.ml.createContext();
22

33
// Build a graph with two outputs.
44
const builder = new MLGraphBuilder(context);
5-
const descA = {type: 'float32', dimensions: [3, 4]};
5+
const descA = {type: 'float32', dataType: 'float32', dimensions: [3, 4]};
66
const a = builder.input('a', descA);
7-
const descB = {type: 'float32', dimensions: [4, 3]};
7+
const descB = {type: 'float32', dataType: 'float32', dimensions: [4, 3]};
88
const bufferB = new Float32Array(sizeOfShape(descB.dimensions)).fill(0.5);
99
const b = builder.constant(descB, bufferB);
10-
const descC = {type: 'float32', dimensions: [3, 3]};
10+
const descC = {type: 'float32', dataType: 'float32', dimensions: [3, 3]};
1111
const bufferC = new Float32Array(sizeOfShape(descC.dimensions)).fill(1);
1212
const c = builder.constant(descC, bufferC);
1313
const d = builder.matmul(a, b);

code/samples/simple_graph.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const TENSOR_SIZE = 8;
1818
const builder = new MLGraphBuilder(context);
1919

2020
// Create MLOperandDescriptor object.
21-
const desc = {type: 'float32', dimensions: TENSOR_DIMS};
21+
const desc = {type: 'float32', dataType: 'float32', dimensions: TENSOR_DIMS};
2222

2323
// constant1 is a constant MLOperand with the value 0.5.
2424
const constantBuffer1 = new Float32Array(TENSOR_SIZE).fill(0.5);

common/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export async function buildConstantByNpy(builder, url) {
5252
const dataView = new Uint8Array(npArray.data.buffer);
5353
const dataView2 = dataView.slice();
5454
const typedArray = new TypedArrayConstructor(dataView2.buffer);
55-
return builder.constant({type, dimensions}, typedArray);
55+
return builder.constant({dataType: type, type, dimensions}, typedArray);
5656
}
5757

5858
// Convert video frame to a canvas element

face_recognition/facenet_nchw.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,11 @@ export class FaceNetNchw {
124124
async load(contextOptions) {
125125
this.context_ = await navigator.ml.createContext(contextOptions);
126126
this.builder_ = new MLGraphBuilder(this.context_);
127-
const input = this.builder_.input('input',
128-
{type: 'float32', dimensions: this.inputOptions.inputDimensions});
127+
const input = this.builder_.input('input', {
128+
type: 'float32',
129+
dataType: 'float32',
130+
dimensions: this.inputOptions.inputDimensions,
131+
});
129132

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

face_recognition/facenet_nhwc.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,11 @@ export class FaceNetNhwc {
125125
async load(contextOptions) {
126126
this.context_ = await navigator.ml.createContext(contextOptions);
127127
this.builder_ = new MLGraphBuilder(this.context_);
128-
const input = this.builder_.input('input',
129-
{type: 'float32', dimensions: this.inputOptions.inputDimensions});
128+
const input = this.builder_.input('input', {
129+
type: 'float32',
130+
dataType: 'float32',
131+
dimensions: this.inputOptions.inputDimensions,
132+
});
130133

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

facial_landmark_detection/face_landmark_nchw.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,11 @@ export class FaceLandmarkNchw {
6464
async load(contextOptions) {
6565
this.context_ = await navigator.ml.createContext(contextOptions);
6666
this.builder_ = new MLGraphBuilder(this.context_);
67-
const input = this.builder_.input('input',
68-
{type: 'float32', dimensions: this.inputOptions.inputDimensions});
67+
const input = this.builder_.input('input', {
68+
type: 'float32',
69+
dataType: 'float32',
70+
dimensions: this.inputOptions.inputDimensions,
71+
});
6972

7073
const poolOptions =
7174
{windowDimensions: [2, 2], strides: [2, 2]};

facial_landmark_detection/face_landmark_nhwc.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,11 @@ export class FaceLandmarkNhwc {
6565
async load(contextOptions) {
6666
this.context_ = await navigator.ml.createContext(contextOptions);
6767
this.builder_ = new MLGraphBuilder(this.context_);
68-
const input = this.builder_.input('input',
69-
{type: 'float32', dimensions: this.inputOptions.inputDimensions});
68+
const input = this.builder_.input('input', {
69+
type: 'float32',
70+
dataType: 'float32',
71+
dimensions: this.inputOptions.inputDimensions,
72+
});
7073

7174
const poolOptions =
7275
{windowDimensions: [2, 2], strides: [2, 2], layout: 'nhwc'};

facial_landmark_detection/ssd_mobilenetv2_face_nchw.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,11 @@ ${nameArray[1]}`;
121121
this.context_ = await navigator.ml.createContext(contextOptions);
122122
this.deviceType_ = contextOptions.deviceType;
123123
this.builder_ = new MLGraphBuilder(this.context_);
124-
const input = this.builder_.input('input',
125-
{type: 'float32', dimensions: this.inputOptions.inputDimensions});
124+
const input = this.builder_.input('input', {
125+
type: 'float32',
126+
dataType: 'float32',
127+
dimensions: this.inputOptions.inputDimensions,
128+
});
126129

127130
const bottleneck0 = await this.buildLinearBottleneck_(
128131
input, '0', false, 32, 'convRelu6');

0 commit comments

Comments
 (0)