Skip to content

Commit

Permalink
Update code editor sample to the latst implementation (#193)
Browse files Browse the repository at this point in the history
* Use MLGraphBuilder.constant(desc, value) for scalar
* Use `gemm` which supports 2Dx2D
* Use MLOperandDescriptor.dataType
* Remove optional output sample
  • Loading branch information
huningxin authored Feb 18, 2024
1 parent a746c24 commit c894ab0
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 35 deletions.
6 changes: 3 additions & 3 deletions code/samples/matmul.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ const context = await navigator.ml.createContext();
// The following code multiplies matrix a [3, 4] with matrix b [4, 3]
// into matrix c [3, 3].
const builder = new MLGraphBuilder(context);
const descA = {type: 'float32', dataType: 'float32', dimensions: [3, 4]};
const descA = {dataType: 'float32', dimensions: [3, 4]};
const a = builder.input('a', descA);
const descB = {type: 'float32', dataType: 'float32', dimensions: [4, 3]};
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.matmul(a, b);
const c = builder.gemm(a, b);

const graph = await builder.build({c});
const bufferA = new Float32Array(sizeOfShape(descA.dimensions)).fill(0.5);
Expand Down
5 changes: 3 additions & 2 deletions code/samples/mul_add.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const operandType = {type: 'float32', dataType: 'float32', dimensions: [2, 2]};
const operandType = {dataType: 'float32', dimensions: [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(0.2);
const constant = builder.constant(
{dataType: 'float32'}, new Float32Array([0.2]));
const A = builder.input('A', operandType);
const B = builder.input('B', operandType);
const C = builder.add(builder.mul(A, constant), B);
Expand Down
28 changes: 0 additions & 28 deletions code/samples/optional_outputs.js

This file was deleted.

2 changes: 1 addition & 1 deletion code/samples/simple_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const TENSOR_SIZE = 8;
const builder = new MLGraphBuilder(context);

// Create MLOperandDescriptor object.
const desc = {type: 'float32', dataType: 'float32', dimensions: TENSOR_DIMS};
const desc = {dataType: 'float32', dimensions: TENSOR_DIMS};

// constant1 is a constant MLOperand with the value 0.5.
const constantBuffer1 = new Float32Array(TENSOR_SIZE).fill(0.5);
Expand Down
1 change: 0 additions & 1 deletion code/samples_repo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const samples = [
'mul_add.js',
'simple_graph.js',
'matmul.js',
'optional_outputs.js',
];

class SamplesRepository {
Expand Down

0 comments on commit c894ab0

Please sign in to comment.