From fff8a4b64c77b7e5c394753a6a4c30320cf7ffe8 Mon Sep 17 00:00:00 2001 From: Belem Zhang Date: Fri, 25 Oct 2024 09:22:38 +0800 Subject: [PATCH] Fix #288 Add empty shape for 0-D scalar constant / scalar input (#289) --- code/samples/mul_add.js | 2 +- nnotepad/js/nnotepad.js | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/code/samples/mul_add.js b/code/samples/mul_add.js index 3ccf0741..a1c76fd5 100644 --- a/code/samples/mul_add.js +++ b/code/samples/mul_add.js @@ -3,7 +3,7 @@ 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])); + {dataType: 'float32', shape: []}, new Float32Array([0.2])); const A = builder.input('A', desc); const B = builder.input('B', desc); const C = builder.add(builder.mul(A, constant), B); diff --git a/nnotepad/js/nnotepad.js b/nnotepad/js/nnotepad.js index ee7ed7da..a46eda7f 100644 --- a/nnotepad/js/nnotepad.js +++ b/nnotepad/js/nnotepad.js @@ -447,8 +447,9 @@ export class NNotepad { function serializeScalar(number, dataType) { const ctor = WebNNUtil.dataTypeToBufferType(dataType); - return `_.constant({dataType:"${dataType}"}, new ${ctor.name}([${ - Util.stringifyNumber(number, dataType)}]))`; + // building a 0-D scalar input with empty shape + return `_.constant({dataType:"${dataType}", dimensions: [], shape: []}, + new ${ctor.name}([${Util.stringifyNumber(number, dataType)}]))`; } function suffixToDataType(suffix) { return {