-
Notifications
You must be signed in to change notification settings - Fork 31
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 #276 from Honry/ml-buffer
Use MLTensor and dispatch() and deprecate compute()
- Loading branch information
Showing
48 changed files
with
785 additions
and
349 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,25 +1,30 @@ | ||
// Step 0: Create a context and graph builder for 'gpu', 'cpu' or 'npu'. | ||
const context = await navigator.ml.createContext({deviceType: 'gpu'}); | ||
const builder = new MLGraphBuilder(context); | ||
const descA = {dataType: 'float32', dimensions: [3, 4], shape: [3, 4]}; | ||
const descB = {dataType: 'float32', dimensions: [4, 3], shape: [4, 3]}; | ||
// Step 1: Create a computational graph calculating `c = a * b`. | ||
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 a = builder.input('a', descA); | ||
const b = builder.input('b', descB); | ||
const c = builder.matmul(a, b); | ||
// Step 2: Compile it into an executable graph. | ||
const graph = await builder.build({c}); | ||
// Step 3: Bind input and output buffers to the graph and execute. | ||
const bufferA = new Float32Array(3*4).fill(1.0); | ||
const bufferB = new Float32Array(4*3).fill(0.8); | ||
const bufferC = new Float32Array(3*3); | ||
const results = await context.compute( | ||
graph, {'a': bufferA, 'b': bufferB}, {'c': bufferC}); | ||
descA.usage = MLTensorUsage.WRITE; | ||
descB.usage = MLTensorUsage.WRITE; | ||
const tensorA = await context.createTensor(descA); | ||
const tensorB = await context.createTensor(descB); | ||
context.writeTensor(tensorA, bufferA); | ||
context.writeTensor(tensorB, bufferB); | ||
const tensorC = await context.createTensor({ | ||
dataType: 'float32', | ||
dimensions: [3, 3], | ||
shape: [3, 3], | ||
usage: MLTensorUsage.READ, | ||
}); | ||
context.dispatch(graph, {a: tensorA, b: tensorB}, {c: tensorC}); | ||
const results = await context.readTensor(tensorC); | ||
// Step 4: Retrieve the results. | ||
console.log(`values: ${results.outputs.c}`); | ||
console.log(`values: ${new Float32Array(results)}`); |
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,21 +1,30 @@ | ||
const operandType = {dataType: 'float32', dimensions: [2, 2], shape: [2, 2]}; | ||
const desc = {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'. | ||
const constant = builder.constant( | ||
{dataType: 'float32'}, new Float32Array([0.2])); | ||
const A = builder.input('A', operandType); | ||
const B = builder.input('B', operandType); | ||
const A = builder.input('A', desc); | ||
const B = builder.input('B', desc); | ||
const C = builder.add(builder.mul(A, constant), B); | ||
// 2. Build the graph into an executable. | ||
const graph = await 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 bufferC = new Float32Array(4); | ||
const inputs = {'A': bufferA, 'B': bufferB}; | ||
const outputs = {'C': bufferC}; | ||
const results = await context.compute(graph, inputs, outputs); | ||
desc.usage = MLTensorUsage.WRITE; | ||
const tensorA = await context.createTensor(desc); | ||
const tensorB = await context.createTensor(desc); | ||
context.writeTensor(tensorA, bufferA); | ||
context.writeTensor(tensorB, bufferB); | ||
const tensorC = await context.createTensor({ | ||
...desc, | ||
usage: MLTensorUsage.READ, | ||
}); | ||
const inputs = {'A': tensorA, 'B': tensorB}; | ||
const outputs = {'C': tensorC}; | ||
context.dispatch(graph, inputs, outputs); | ||
// The computed result of [[1, 1], [1, 1]] is in the buffer associated with | ||
// the output operand. | ||
console.log('Output value: ' + results.outputs.C); | ||
const results = await context.readTensor(tensorC); | ||
console.log('Output value: ' + new Float32Array(results)); |
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,6 +1,7 @@ | ||
module.exports = { | ||
globals: { | ||
'MLGraphBuilder': 'readonly', | ||
'MLTensorUsage': 'readonly', | ||
'tf': 'readonly', | ||
}, | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
module.exports = { | ||
globals: { | ||
'MLGraphBuilder': 'readonly', | ||
'MLTensorUsage': 'readonly', | ||
'tf': 'readonly', | ||
}, | ||
}; |
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.