forked from webmachinelearning/webnn-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request webmachinelearning#58 from Honry/sync-api
Adapt samples to sync API and require pre-allocated output buffers
- Loading branch information
Showing
26 changed files
with
228 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,29 @@ | ||
const context = navigator.ml.createContext(); | ||
|
||
// Create a model with dynamic shaped inputs. | ||
// Create a graph with dynamic shaped inputs. | ||
const builder = new MLGraphBuilder(context); | ||
const descA = {type: 'float32', dimensions: [-1, 4]}; | ||
const a = builder.input('a', descA); | ||
const descB = {type: 'float32', dimensions: [4, -1]}; | ||
const b = builder.input('b', descB); | ||
const c = builder.matmul(a, b); | ||
const graph = builder.build({'c': c}); | ||
|
||
const graph = await builder.build({c}); | ||
|
||
async function compute(shapeA, shapeB) { | ||
function compute(shapeA, shapeB, shapeC) { | ||
const bufferA = new Float32Array(sizeOfShape(shapeA)).fill(0.5); | ||
const bufferB = new Float32Array(sizeOfShape(shapeB)).fill(0.5); | ||
const bufferC = new Float32Array(sizeOfShape(shapeC)); | ||
|
||
// Specify the shape of inputs when computing. | ||
const inputs = { | ||
'a': {data: bufferA, dimensions: shapeA}, | ||
'b': {data: bufferB, dimensions: shapeB}, | ||
'a': {resource: bufferA, dimensions: shapeA}, | ||
'b': {resource: bufferB, dimensions: shapeB}, | ||
}; | ||
const outputs = await graph.compute(inputs); | ||
console.log(`shape: [${outputs.c.dimensions}], values: ${outputs.c.data}`); | ||
const outputs = {'c': bufferC}; | ||
graph.compute(inputs, outputs); | ||
console.log(`values: ${bufferC}`); | ||
} | ||
|
||
await compute([3, 4], [4, 3]); | ||
await compute([4, 4], [4, 4]); | ||
await compute([5, 4], [4, 5]); | ||
compute([3, 4], [4, 3], [3, 3]); | ||
compute([4, 4], [4, 4], [4, 4]); | ||
compute([5, 4], [4, 5], [5, 5]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,20 @@ | ||
const operandType = {type: 'float32', dimensions: [2, 2]}; | ||
const context = navigator.ml.createContext(); | ||
const builder = new MLGraphBuilder(context); | ||
// 1. Create a model of the computational graph 'C = 0.2 * A + B'. | ||
// 1. Create a computational graph 'C = 0.2 * A + B'. | ||
const constant = builder.constant(0.2); | ||
const A = builder.input('A', operandType); | ||
const B = builder.input('B', operandType); | ||
const C = builder.add(builder.mul(A, constant), B); | ||
// 2. Build the model into executable. | ||
const graph = await builder.build({'C': C}); | ||
// 3. Bind inputs to the model and execute for the result. | ||
// 2. Build the graph into an executable. | ||
const graph = builder.build({'C': C}); | ||
// 3. Bind inputs to the graph and execute for the result. | ||
const bufferA = new Float32Array(4).fill(1.0); | ||
const bufferB = new Float32Array(4).fill(0.8); | ||
const inputs = {'A': {data: bufferA}, 'B': {data: bufferB}}; | ||
const outputs = await graph.compute(inputs); | ||
const bufferC = new Float32Array(4); | ||
const inputs = {'A': bufferA, 'B': bufferB}; | ||
const outputs = {'C': bufferC}; | ||
graph.compute(inputs, outputs); | ||
// The computed result of [[1, 1], [1, 1]] is in the buffer associated with | ||
// the output operand. | ||
console.log('Output shape: ' + outputs.C.dimensions); | ||
console.log('Output value: ' + outputs.C.data); | ||
console.log('Output value: ' + bufferC); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.