-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deploying to gh-pages from @ e0f10f0 🚀
- Loading branch information
Showing
2 changed files
with
15 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,17 @@ | ||
const context = await navigator.ml.createContext(); | ||
|
||
// The following code multiplies matrix a [3, 4] with matrix b [4, 3] | ||
// into matrix c [3, 3]. | ||
// 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]}; | ||
const a = builder.input('a', descA); | ||
const descB = {dataType: 'float32', dimensions: [4, 3]}; | ||
const bufferB = new Float32Array(sizeOfShape(descB.dimensions)).fill(0.5); | ||
const b = builder.constant(descB, bufferB); | ||
const c = builder.gemm(a, b); | ||
|
||
// Step 1: Create a computational graph calculating `c = a * b`. | ||
const a = builder.input('a', {dataType: 'float32', dimensions: [3, 4]}); | ||
const b = builder.input('b', {dataType: 'float32', dimensions: [4, 3]}); | ||
const c = builder.matmul(a, b); | ||
// Step 2: Compile it into an executable graph. | ||
const graph = await builder.build({c}); | ||
const bufferA = new Float32Array(sizeOfShape(descA.dimensions)).fill(0.5); | ||
const bufferC = new Float32Array(sizeOfShape([3, 3])); | ||
const inputs = {'a': bufferA}; | ||
const outputs = {'c': bufferC}; | ||
const results = await context.compute(graph, inputs, outputs); | ||
// 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}); | ||
// Step 4: Retrieve the results. | ||
console.log(`values: ${results.outputs.c}`); |
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