Skip to content

Commit

Permalink
Demo reusable code
Browse files Browse the repository at this point in the history
  • Loading branch information
BruceDai committed Nov 25, 2022
1 parent 15e7e8b commit 3edeb4c
Show file tree
Hide file tree
Showing 13 changed files with 4,842 additions and 1 deletion.
17 changes: 17 additions & 0 deletions webnn/clamp.https.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// META: title=test WebNN API clamp operation
// META: global=window,dedicatedworker
// META: script=./resources/utils.js
// META: timeout=long

'use strict';

// https://webmachinelearning.github.io/webnn/#api-mlgraphbuilder-clamp

const computeClamp = (builder, resources) => {
// MLOperand clamp(MLOperand x, optional MLClampOptions options = {});
// use 'x' for input operand name
const [inputOperand] = createInputOperands(builder, resources, ['x']);
return builder.clamp(inputOperand, resources.options);
};

testWebNNOperation('clamp', '/webnn/resources/test_data/clamp.json', computeClamp);
1 change: 0 additions & 1 deletion webnn/concat.https.any.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// META: title=test WebNN API concat operation
// META: global=window,dedicatedworker
// META: script=./resources/utils.js
// META: script=./webnn-polyfill.js
// META: timeout=long

'use strict';
Expand Down
45 changes: 45 additions & 0 deletions webnn/gemm.https.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// META: title=test WebNN API gemm operation
// META: global=window,dedicatedworker
// META: script=./resources/utils.js
// META: timeout=long

'use strict';

// https://webmachinelearning.github.io/webnn/#api-mlgraphbuilder-gemm

const computeGemm = (builder, resources) => {
// MLOperand gemm(MLOperand a, MLOperand b, optional MLGemmOptions options = {});
// use 'a' and 'b' for input operands name
const [inputOperandA, inputOperandB] = createInputOperands(builder, resources, ['a', 'b']);
const operandType = resources.type;
const TestTypedArray = TypedArrayDict[operandType];
const options = resources.options;
const gemmOptions = {};
if (options !== undefined) {
if (options.c !== undefined) {
if (typeof options.c === 'number') {
// scalar
gemmOptions.c = new TestTypedArray([options.c])[0];
} else {
gemmOptions.c = builder.constant({type: operandType, dimensions: options.c.shape}, new TestTypedArray(options.c.data));
}
}
if (options.alpha !== undefined) {
// scalar
gemmOptions.alpha = new TestTypedArray([options.alpha])[0];
}
if (options.beta !== undefined) {
// scalar
gemmOptions.beta = new TestTypedArray([options.beta])[0];
}
if (options.aTranspose !== undefined) {
gemmOptions.aTranspose = options.aTranspose;
}
if (options.bTranspose !== undefined) {
gemmOptions.bTranspose = options.bTranspose;
}
}
return builder.gemm(inputOperandA, inputOperandB, gemmOptions);
};

testWebNNOperation('gemm', '/webnn/resources/test_data/gemm.json', computeGemm);
17 changes: 17 additions & 0 deletions webnn/matmul.https.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// META: title=test WebNN API matmul operation
// META: global=window,dedicatedworker
// META: script=./resources/utils.js
// META: timeout=long

'use strict';

// https://webmachinelearning.github.io/webnn/#api-mlgraphbuilder-matmul

const computeMatmul = (builder, resources) => {
// MLOperand matmul(MLOperand a, MLOperand b);
// use 'a' and 'b' for input operands name
const [inputOperandA, inputOperandB] = createInputOperands(builder, resources, ['a', 'b']);
return builder.matmul(inputOperandA, inputOperandB);
};

testWebNNOperation('matmul', '/webnn/resources/test_data/matmul.json', computeMatmul);
17 changes: 17 additions & 0 deletions webnn/relu.https.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// META: title=test WebNN API relu operation
// META: global=window,dedicatedworker
// META: script=./resources/utils.js
// META: timeout=long

'use strict';

// https://webmachinelearning.github.io/webnn/#api-mlgraphbuilder-relu

const computeRelu = (builder, resources) => {
// MLOperand relu(MLOperand x);
// use 'x' for input operand name
const [inputOperand] = createInputOperands(builder, resources, ['x']);
return builder.relu(inputOperand);
};

testWebNNOperation('relu', '/webnn/resources/test_data/relu.json', computeRelu);
18 changes: 18 additions & 0 deletions webnn/reshape.https.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// META: title=test WebNN API reshape operation
// META: global=window,dedicatedworker
// META: script=./resources/utils.js
// META: timeout=long

'use strict';

// https://webmachinelearning.github.io/webnn/#api-mlgraphbuilder-reshape

const computeReshape = (builder, resources) => {
// MLOperand reshape(MLOperand input, sequence<long> newShape);
// use 'input' for input operand name
const [inputOperand] = createInputOperands(builder, resources, ['input']);
return builder.reshape(inputOperand, resources.newShape);
};

testWebNNOperation('reshape', '/webnn/resources/test_data/reshape.json', computeReshape);

Loading

0 comments on commit 3edeb4c

Please sign in to comment.