From da855e6210766cf919b5c4586a53121697edcf2b Mon Sep 17 00:00:00 2001 From: Miao Bin Date: Fri, 13 Sep 2024 18:32:44 -0700 Subject: [PATCH] WebNN: Add IDL and mojo definitions for cumulativeSum operator This CL adds IDL and mojo definitions for `cumulativeSum` operator which is proposed by WebML WG [1]. This CL also implements data type limits, inputs validation and adds validation tests for `cumulativeSum` operator. [1]: https://github.com/webmachinelearning/webnn/issues/375#issuecomment-2292466613 Bug: 363677531 Change-Id: I5d10918598a7f1331c7281b2e3533c63448ffc30 Cq-Include-Trybots: luci.chromium.try:win11-blink-rel, mac14.arm64-blink-rel, mac14-blink-rel, mac15.arm64-blink-rel, mac15-blink-rel, linux-blink-rel Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5845069 Reviewed-by: Weizhong Xia Reviewed-by: ningxin hu Reviewed-by: Alex Gough Commit-Queue: Bin Miao Cr-Commit-Position: refs/heads/main@{#1355527} --- .../cumulativeSum.https.any.js | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 webnn/validation_tests/cumulativeSum.https.any.js diff --git a/webnn/validation_tests/cumulativeSum.https.any.js b/webnn/validation_tests/cumulativeSum.https.any.js new file mode 100644 index 00000000000000..65eef7ed89d1be --- /dev/null +++ b/webnn/validation_tests/cumulativeSum.https.any.js @@ -0,0 +1,81 @@ +// META: title=validation tests for WebNN API relu operation +// META: global=window,dedicatedworker +// META: variant=?cpu +// META: variant=?gpu +// META: variant=?npu +// META: script=../resources/utils_validation.js + +'use strict'; + +const tests = [ + { + name: '[cumulativeSum] Test with default options', + input: {dataType: 'float32', dimensions: [3, 2, 5]}, + axis: 0, + output: {dataType: 'float32', dimensions: [3, 2, 5]} + }, + { + name: '[cumulativeSum] Test with axis=1', + input: {dataType: 'float32', dimensions: [3, 2, 5]}, + axis: 1, + output: {dataType: 'float32', dimensions: [3, 2, 5]} + }, + { + name: '[cumulativeSum] Test with exclusive=true', + input: {dataType: 'float32', dimensions: [3, 2, 5]}, + axis: 1, + options: {exclusive: true}, + output: {dataType: 'float32', dimensions: [3, 2, 5]} + }, + { + name: '[cumulativeSum] Test with reversed=true', + input: {dataType: 'float32', dimensions: [3, 2, 5]}, + axis: 1, + options: {reversed: true}, + output: {dataType: 'float32', dimensions: [3, 2, 5]} + }, + { + name: '[cumulativeSum] Throw if input is a scalar', + input: {dataType: 'float32', dimensions: []}, + axis: 0 + }, + { + name: '[cumulativeSum] Throw if axis is invalid', + input: {dataType: 'float32', dimensions: [1, 2, 3]}, + axis: 3 + }, +] + +tests.forEach( + test => promise_test(async t => { + const builder = new MLGraphBuilder(context); + const input = builder.input('input', test.input); + + const options = {}; + if (test.options) { + if (test.options.exclusive) { + options.exclusive = test.options.exclusive; + } + if (test.options.reversed) { + options.reversed = test.options.reversed; + } + } + if (test.output) { + const output = builder.cumulativeSum(input, test.axis, options); + assert_equals(output.dataType(), test.output.dataType); + assert_array_equals(output.shape(), test.output.dimensions); + } else { + const label = 'cumulative_sum'; + options.label = label; + const regexp = new RegExp('\\[' + label + '\\]'); + assert_throws_with_label( + () => builder.cumulativeSum(input, test.axis, options), regexp); + } + }, test.name)); + +multi_builder_test(async (t, builder, otherBuilder) => { + const inputFromOtherBuilder = + otherBuilder.input('input', {dataType: 'float32', dimensions: [3, 2, 5]}); + assert_throws_js( + TypeError, () => builder.cumulativeSum(inputFromOtherBuilder, 0)); +}, '[cumulativeSum] throw if input is from another builder');