From ddea0a855e474865eb73d7267abc22e8352ab26e Mon Sep 17 00:00:00 2001 From: Ningxin Hu Date: Sun, 18 Feb 2024 15:57:28 +0800 Subject: [PATCH] Update code editor sample to the latst implementation * Use MLGraphBuilder.constant(desc, value) for scalar * Use `gemm` which supports 2Dx2D * Use MLOperandDescriptor.dataType * Remove optional output sample --- code/samples/matmul.js | 6 +++--- code/samples/mul_add.js | 5 +++-- code/samples/optional_outputs.js | 28 ---------------------------- code/samples/simple_graph.js | 2 +- code/samples_repo.js | 1 - 5 files changed, 7 insertions(+), 35 deletions(-) delete mode 100644 code/samples/optional_outputs.js diff --git a/code/samples/matmul.js b/code/samples/matmul.js index 20e9d906..03d54698 100644 --- a/code/samples/matmul.js +++ b/code/samples/matmul.js @@ -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); diff --git a/code/samples/mul_add.js b/code/samples/mul_add.js index 08267de6..b4a6c564 100644 --- a/code/samples/mul_add.js +++ b/code/samples/mul_add.js @@ -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); diff --git a/code/samples/optional_outputs.js b/code/samples/optional_outputs.js deleted file mode 100644 index 4e3da239..00000000 --- a/code/samples/optional_outputs.js +++ /dev/null @@ -1,28 +0,0 @@ -const context = await navigator.ml.createContext(); - -// Build a graph with two outputs. -const builder = new MLGraphBuilder(context); -const descA = {type: 'float32', dataType: 'float32', dimensions: [3, 4]}; -const a = builder.input('a', descA); -const descB = {type: 'float32', dataType: 'float32', dimensions: [4, 3]}; -const bufferB = new Float32Array(sizeOfShape(descB.dimensions)).fill(0.5); -const b = builder.constant(descB, bufferB); -const descC = {type: 'float32', dataType: 'float32', dimensions: [3, 3]}; -const bufferC = new Float32Array(sizeOfShape(descC.dimensions)).fill(1); -const c = builder.constant(descC, bufferC); -const d = builder.matmul(a, b); -const e = builder.add(d, c); -const graph = await builder.build({'d': d, 'e': e}); - -const bufferA = new Float32Array(sizeOfShape(descA.dimensions)).fill(0.5); -const inputs = {'a': bufferA}; - -// Compute d. -const bufferD = new Float32Array(sizeOfShape([3, 3])); -const resultsD = await context.compute(graph, inputs, {'d': bufferD}); -console.log(`values: ${resultsD.outputs.d}`); - -// Compute e. -const bufferE = new Float32Array(sizeOfShape([3, 3])); -const resultsE = await context.compute(graph, inputs, {'e': bufferE}); -console.log(`values: ${resultsE.outputs.e}`); diff --git a/code/samples/simple_graph.js b/code/samples/simple_graph.js index e5e480d5..ff03401e 100644 --- a/code/samples/simple_graph.js +++ b/code/samples/simple_graph.js @@ -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); diff --git a/code/samples_repo.js b/code/samples_repo.js index 5e3880bf..161dd863 100644 --- a/code/samples_repo.js +++ b/code/samples_repo.js @@ -3,7 +3,6 @@ const samples = [ 'mul_add.js', 'simple_graph.js', 'matmul.js', - 'optional_outputs.js', ]; class SamplesRepository {