Skip to content

Commit

Permalink
Deploying to gh-pages from @ e0f10f0 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
Honry committed May 27, 2024
1 parent 5f01118 commit 8e7d738
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
30 changes: 14 additions & 16 deletions code/samples/matmul.js
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}`);
2 changes: 1 addition & 1 deletion code/samples_repo.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// The samples under ./samples folder
const samples = [
'matmul.js',
'mul_add.js',
'simple_graph.js',
'matmul.js',
];

class SamplesRepository {
Expand Down

0 comments on commit 8e7d738

Please sign in to comment.