Skip to content

Commit

Permalink
keep dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Honry committed Sep 18, 2024
1 parent 42628ed commit b83bed6
Show file tree
Hide file tree
Showing 30 changed files with 71 additions and 21 deletions.
12 changes: 10 additions & 2 deletions code/samples/matmul.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@
const context = await navigator.ml.createContext({deviceType: 'gpu'});
const builder = new MLGraphBuilder(context);
// Step 1: Create a computational graph calculating `c = a * b`.
const a = builder.input('a', {dataType: 'float32', shape: [3, 4]});
const b = builder.input('b', {dataType: 'float32', shape: [4, 3]});
const a = builder.input('a', {
dataType: 'float32',
dimensions: [3, 4],
shape: [3, 4],
});
const b = builder.input('b', {
dataType: 'float32',
dimensions: [4, 3],
shape: [4, 3],
});
const c = builder.matmul(a, b);
// Step 2: Compile it into an executable graph.
const graph = await builder.build({c});
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 = {dataType: 'float32', shape: [2, 2]};
const operandType = {dataType: 'float32', dimensions: [2, 2], shape: [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
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 = {dataType: 'float32', shape: TENSOR_DIMS};
const desc = {dataType: 'float32', dimensions: TENSOR_DIMS, shape: TENSOR_DIMS};

// constant1 is a constant MLOperand with the value 0.5.
const constantBuffer1 = new Float32Array(TENSOR_SIZE).fill(0.5);
Expand Down
3 changes: 2 additions & 1 deletion common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ export async function buildConstantByNpy(builder, url, targetType = 'float32') {
throw new Error(`Conversion from ${npArray.dataType} ` +
`to ${targetType} is not supported.`);
}
return builder.constant({dataType: type, shape}, typedArray);
return builder.constant(
{dataType: type, dimensions: shape, shape}, typedArray);
}

// Convert video frame to a canvas element
Expand Down
1 change: 1 addition & 0 deletions face_recognition/facenet_nchw.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export class FaceNetNchw {
this.builder_ = new MLGraphBuilder(this.context_);
const input = this.builder_.input('input', {
dataType: 'float32',
dimensions: this.inputOptions.inputShape,
shape: this.inputOptions.inputShape,
});

Expand Down
1 change: 1 addition & 0 deletions face_recognition/facenet_nhwc.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export class FaceNetNhwc {
this.builder_ = new MLGraphBuilder(this.context_);
const input = this.builder_.input('input', {
dataType: 'float32',
dimensions: this.inputOptions.inputShape,
shape: this.inputOptions.inputShape,
});

Expand Down
1 change: 1 addition & 0 deletions facial_landmark_detection/face_landmark_nchw.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export class FaceLandmarkNchw {
this.builder_ = new MLGraphBuilder(this.context_);
const input = this.builder_.input('input', {
dataType: 'float32',
dimensions: this.inputOptions.inputShape,
shape: this.inputOptions.inputShape,
});

Expand Down
1 change: 1 addition & 0 deletions facial_landmark_detection/face_landmark_nhwc.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export class FaceLandmarkNhwc {
this.builder_ = new MLGraphBuilder(this.context_);
const input = this.builder_.input('input', {
dataType: 'float32',
dimensions: this.inputOptions.inputShape,
shape: this.inputOptions.inputShape,
});

Expand Down
1 change: 1 addition & 0 deletions facial_landmark_detection/ssd_mobilenetv2_face_nchw.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ ${nameArray[1]}`;
this.builder_ = new MLGraphBuilder(this.context_);
const input = this.builder_.input('input', {
dataType: 'float32',
dimensions: this.inputOptions.inputShape,
shape: this.inputOptions.inputShape,
});

Expand Down
1 change: 1 addition & 0 deletions facial_landmark_detection/ssd_mobilenetv2_face_nhwc.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ ${nameArray[1]}`;
this.builder_ = new MLGraphBuilder(this.context_);
const input = this.builder_.input('input', {
dataType: 'float32',
dimensions: this.inputOptions.inputShape,
shape: this.inputOptions.inputShape,
});

Expand Down
1 change: 1 addition & 0 deletions image_classification/efficientnet_fp16_nchw.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export class EfficientNetFP16Nchw {
this.builder_ = new MLGraphBuilder(this.context_);
let data = this.builder_.input('input', {
dataType: 'float32',
dimensions: this.inputOptions.inputShape,
shape: this.inputOptions.inputShape,
});
data = this.builder_.cast(data, 'float16');
Expand Down
1 change: 1 addition & 0 deletions image_classification/mobilenet_nchw.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export class MobileNetV2Nchw {
this.builder_ = new MLGraphBuilder(this.context_);
let data = this.builder_.input('input', {
dataType: 'float32',
dimensions: this.inputOptions.inputShape,
shape: this.inputOptions.inputShape,
});
if (this.dataType_ === 'float16') {
Expand Down
1 change: 1 addition & 0 deletions image_classification/mobilenet_nhwc.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export class MobileNetV2Nhwc {
const filterLayout = 'ohwi';
const input = this.builder_.input('input', {
dataType: 'float32',
dimensions: this.inputOptions.inputShape,
shape: this.inputOptions.inputShape,
});
const conv0 = this.buildConv_(
Expand Down
1 change: 1 addition & 0 deletions image_classification/resnet50v1_fp16_nchw.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export class ResNet50V1FP16Nchw {
this.builder_ = new MLGraphBuilder(this.context_);
let data = this.builder_.input('input', {
dataType: 'float32',
dimensions: this.inputOptions.inputShape,
shape: this.inputOptions.inputShape,
});
data = this.builder_.cast(data, 'float16');
Expand Down
1 change: 1 addition & 0 deletions image_classification/resnet50v2_nchw.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export class ResNet50V2Nchw {
this.builder_ = new MLGraphBuilder(this.context_);
const data = this.builder_.input('input', {
dataType: 'float32',
dimensions: this.inputOptions.inputShape,
shape: this.inputOptions.inputShape,
});
const bn1 = this.buildBatchNorm_(data, '0', '', false);
Expand Down
1 change: 1 addition & 0 deletions image_classification/resnet50v2_nhwc.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export class ResNet50V2Nhwc {
this.builder_ = new MLGraphBuilder(this.context_);
const input = this.builder_.input('input', {
dataType: 'float32',
dimensions: this.inputOptions.inputShape,
shape: this.inputOptions.inputShape,
});
const conv1 = await this.buildConv_(
Expand Down
1 change: 1 addition & 0 deletions image_classification/squeezenet_nchw.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class SqueezeNetNchw {
this.builder_ = new MLGraphBuilder(this.context_);
const data = this.builder_.input('input', {
dataType: 'float32',
dimensions: this.inputOptions.inputShape,
shape: this.inputOptions.inputShape,
});
const conv0 = this.buildConv_(data, 'conv0', {strides: [2, 2]});
Expand Down
1 change: 1 addition & 0 deletions image_classification/squeezenet_nhwc.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export class SqueezeNetNhwc {
const layout = 'nhwc';
const placeholder = this.builder_.input('input', {
dataType: 'float32',
dimensions: this.inputOptions.inputShape,
shape: this.inputOptions.inputShape,
});
const conv1 = this.buildConv_(
Expand Down
24 changes: 16 additions & 8 deletions lenet/lenet.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class LeNet {
const inputShape = /* nchw */ [1, 1, 28, 28];
let input = this.builder_.input('input', {
dataType: 'float32',
dimensions: inputShape,
shape: inputShape,
});

Expand All @@ -50,7 +51,11 @@ export class LeNet {
conv1FilterData, conv1FitlerShape, this.oihwToOhwiPermutation_);
}
const conv1Filter = this.builder_.constant(
{dataType: 'float32', shape: conv1FitlerShape},
{
dataType: 'float32',
dimensions: conv1FitlerShape,
shape: conv1FitlerShape,
},
conv1FilterData);
byteOffset +=
sizeOfShape(conv1FitlerShape) * Float32Array.BYTES_PER_ELEMENT;
Expand All @@ -59,7 +64,7 @@ export class LeNet {
const add1BiasData =
new Float32Array(arrayBuffer, byteOffset, sizeOfShape(add1BiasShape));
const add1Bias = this.builder_.constant(
{dataType: 'float32', shape: add1BiasShape},
{dataType: 'float32', dimensions: add1BiasShape, shape: add1BiasShape},
add1BiasData,
);
byteOffset += sizeOfShape(add1BiasShape) * Float32Array.BYTES_PER_ELEMENT;
Expand Down Expand Up @@ -87,14 +92,17 @@ export class LeNet {
conv2FilterData, conv2FilterShape, this.oihwToOhwiPermutation_);
}
const conv2Filter = this.builder_.constant(
{dataType: 'float32', shape: conv2FilterShape},
{
dataType: 'float32',
dimensions: conv2FilterShape,
shape: conv2FilterShape},
conv2FilterData);
byteOffset +=
sizeOfShape(conv2FilterShape) * Float32Array.BYTES_PER_ELEMENT;

const add2BiasShape = [50];
const add2Bias = this.builder_.constant(
{dataType: 'float32', shape: add2BiasShape},
{dataType: 'float32', dimensions: add2BiasShape, shape: add2BiasShape},
new Float32Array(arrayBuffer, byteOffset, sizeOfShape(add2BiasShape)));
byteOffset += sizeOfShape(add2BiasShape) * Float32Array.BYTES_PER_ELEMENT;
conv2Options.bias = add2Bias;
Expand All @@ -120,15 +128,15 @@ export class LeNet {

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

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

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

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

Expand Down
1 change: 1 addition & 0 deletions nnotepad/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ function explain(outputs) {
.map((output) =>
[
'dataType: ' + output.dataType,
'dimensions: ' + Util.stringify(output.shape),
'shape: ' + Util.stringify(output.shape),
'tensor: ' + dumpTensor(output.shape, output.buffer, 8),
].join('\n'),
Expand Down
10 changes: 7 additions & 3 deletions nnotepad/js/nnotepad.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,8 @@ export class NNotepad {
});
}(tensor, 0));
const ctor = WebNNUtil.dataTypeToBufferType(dataType);
return `_.constant({dataType: "${dataType}", shape: ${
return `_.constant({dataType: "${dataType}", dimensions: ${
Util.stringify(shape)}}, shape: ${
Util.stringify(shape)}}, new ${ctor.name}([${
elements.map((n) => Util.stringifyNumber(n, dataType)).join(',')}]))`;
}
Expand All @@ -500,7 +501,8 @@ export class NNotepad {
}
const dims = shape.value.map((expr) => expr.value);
const ctor = WebNNUtil.dataTypeToBufferType(dataType.value);
return `_.constant({dataType: "${dataType.value}", shape: ${
return `_.constant({dataType: "${dataType.value}", dimensions: ${
Util.stringify(dims)}}, shape: ${
Util.stringify(dims)}}, new ${
ctor.name}(await Util.loadBuffer(${Util.stringify(url.value)})))`;
}
Expand All @@ -516,7 +518,8 @@ export class NNotepad {
const dims = shape.value.map((expr) => expr.value);
const ctor = WebNNUtil.dataTypeToBufferType(dataType.value);
const len = dims.reduce((a, b) => a * b, 1);
return `_.constant({dataType: "${dataType.value}", shape: ${
return `_.constant({dataType: "${dataType.value}", dimensions: ${
Util.stringify(dims)}}, shape: ${
Util.stringify(dims)}}, new ${
ctor.name}(${len}))`;
}
Expand Down Expand Up @@ -595,6 +598,7 @@ export class NNotepad {
return outputOperands.map(
(op, index) => ({
dataType: op.dataType(),
dimensions: op.shape(),
shape: op.shape(),
buffer: maybeProxyForFloat16Array(result.outputs[`output-${index}`]),
}));
Expand Down
3 changes: 3 additions & 0 deletions nsnet2/nsnet2.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ export class NSNet2 {
// Build up the network.
const input = this.builder_.input('input', {
dataType: 'float32',
dimensions: [batchSize, frames, this.frameSize],
shape: [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', {
dataType: 'float32',
dimensions: [1, batchSize, this.hiddenSize],
shape: [1, batchSize, this.hiddenSize],
});
const [gru94, gru93] = this.builder_.gru(transpose31, weight192, recurrentWeight193, frames, this.hiddenSize,
Expand All @@ -54,6 +56,7 @@ export class NSNet2 {
const squeeze95 = this.builder_.reshape(gru93, squeeze95Shape);
const initialState155 = this.builder_.input('initialState155', {
dataType: 'float32',
dimensions: [1, batchSize, this.hiddenSize],
shape: [1, batchSize, this.hiddenSize],
});
const [gru157, gru156] = this.builder_.gru(squeeze95, weight212, recurrentWeight213, frames, this.hiddenSize,
Expand Down
1 change: 1 addition & 0 deletions object_detection/ssd_mobilenetv1_nchw.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ ${nameArray[1]}_BatchNorm_batchnorm`;
this.builder_ = new MLGraphBuilder(this.context_);
let input = this.builder_.input('input', {
dataType: 'float32',
dimensions: this.inputOptions.inputShape,
shape: this.inputOptions.inputShape,
});
if (this.targetDataType_ === 'float16') {
Expand Down
1 change: 1 addition & 0 deletions object_detection/ssd_mobilenetv1_nhwc.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ ${nameArray[1]}_BatchNorm_batchnorm`;
this.builder_ = new MLGraphBuilder(this.context_);
const input = this.builder_.input('input', {
dataType: 'float32',
dimensions: this.inputOptions.inputShape,
shape: this.inputOptions.inputShape,
});
const strides = [2, 2];
Expand Down
3 changes: 2 additions & 1 deletion object_detection/tiny_yolov2_nchw.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ export class TinyYoloV2Nchw {
this.builder_ = new MLGraphBuilder(this.context_);
let image = this.builder_.input('input', {
dataType: 'float32',
dimensions: this.inputOptions.inputShape,
shape: this.inputOptions.inputShape,
});
let mulScale = this.builder_.constant(
{dataType: 'float32', shape: [1]},
{dataType: 'float32', dimensions: [1], shape: [1]},
new Float32Array([0.003921568859368563]),
);
const poolOptions = {
Expand Down
1 change: 1 addition & 0 deletions object_detection/tiny_yolov2_nhwc.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class TinyYoloV2Nhwc {
this.builder_ = new MLGraphBuilder(this.context_);
const input = this.builder_.input('input', {
dataType: 'float32',
dimensions: this.inputOptions.inputShape,
shape: this.inputOptions.inputShape,
});

Expand Down
4 changes: 4 additions & 0 deletions rnnoise/rnnoise.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export class RNNoise {
// Build up the network.
const input = this.builder_.input('input', {
dataType: 'float32',
dimensions: [this.batchSize_, this.frames_, this.featureSize],
shape: [this.batchSize_, this.frames_, this.featureSize],
});
const inputDense0 = this.builder_.matmul(input, inputDenseKernel0);
Expand All @@ -68,6 +69,7 @@ export class RNNoise {
[1, 3 * this.vadGruHiddenSize]);
const vadGruInitialH = this.builder_.input('vadGruInitialH', {
dataType: 'float32',
dimensions: [1, this.batchSize_, this.vadGruHiddenSize],
shape: [1, this.batchSize_, this.vadGruHiddenSize],
});
const [vadGruYH, vadGruY] = this.builder_.gru(vadGruX,
Expand Down Expand Up @@ -95,6 +97,7 @@ export class RNNoise {
[1, 3 * this.noiseGruHiddenSize]);
const noiseGruInitialH = this.builder_.input('noiseGruInitialH', {
dataType: 'float32',
dimensions: [1, this.batchSize_, this.noiseGruHiddenSize],
shape: [1, this.batchSize_, this.noiseGruHiddenSize],
});
const [noiseGruYH, noiseGruY] = this.builder_.gru(noiseGruX,
Expand Down Expand Up @@ -122,6 +125,7 @@ export class RNNoise {
[1, 3 * this.denoiseGruHiddenSize]);
const denoiseGruInitialH = this.builder_.input('denoiseGruInitialH', {
dataType: 'float32',
dimensions: [1, this.batchSize_, this.denoiseGruHiddenSize],
shape: [1, this.batchSize_, this.denoiseGruHiddenSize],
});
const [denoiseGruYH, denoiseGruY] = this.builder_.gru(denoiseGruX,
Expand Down
1 change: 1 addition & 0 deletions semantic_segmentation/deeplabv3_mnv2_nchw.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export class DeepLabV3MNV2Nchw {

const input = this.builder_.input('input', {
dataType: 'float32',
dimensions: this.inputOptions.inputShape,
shape: this.inputOptions.inputShape,
});
const conv0 = this.buildConv_(
Expand Down
1 change: 1 addition & 0 deletions semantic_segmentation/deeplabv3_mnv2_nhwc.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export class DeepLabV3MNV2Nhwc {
const strides = [2, 2];
const input = this.builder_.input('input', {
dataType: 'float32',
dimensions: this.inputOptions.inputShape,
shape: this.inputOptions.inputShape,
});
const conv0 = await this.buildConv_(
Expand Down
Loading

0 comments on commit b83bed6

Please sign in to comment.