webnn: Migrated resample2d unit tests to WPTs.
This CL is migrating resample2d unit tests to WPTs by removing
MLGraphBuilderTest.Resample2dTest and MLGraphTestMojo.Resample2dTest
tests. It's also fixing axes validation issue.
Bug: 327337526 328026885
Change-Id: Ia0ae3fe2515040f37d84fef048ba558991a248c3
Cq-Include-Trybots: luci.chromium.try:win11-blink-rel
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5382118
Reviewed-by: ningxin hu <ningxin.hu@intel.com>
Commit-Queue: Feng Dai <feng.dai@intel.com>
Reviewed-by: Austin Sullivan <asully@chromium.org>
Auto-Submit: Feng Dai <feng.dai@intel.com>
Cr-Commit-Position: refs/heads/main@{#1279384}
diff --git a/webnn/resources/utils_validation.js b/webnn/resources/utils_validation.js
index b2c1c53..5c4eb08 100644
--- a/webnn/resources/utils_validation.js
+++ b/webnn/resources/utils_validation.js
@@ -259,11 +259,11 @@
/**
* Validate options.axes by given operation and input rank for
- * argMin/Max / layerNormalization / Reduction operations / resample2d operations
- * @param {(String[]|String)} operationName - An operation name array or an operation name
- * @param {Number} [inputRank]
+ * argMin/Max / layerNormalization / Reduction operations operations
+ * @param {(String[]|String)} operationName - An operation name array or an
+ * operation name
*/
-function validateOptionsAxes(operationName, inputRank) {
+function validateOptionsAxes(operationName) {
if (navigator.ml === undefined) {
return;
}
@@ -279,33 +279,25 @@
for (let subOperationName of operationNameArray) {
// TypeError is expected if any of options.axes elements is not an unsigned long interger
promise_test(async t => {
- if (inputRank === undefined) {
- // argMin/Max / layerNormalization / Reduction operations
- for (let dataType of allWebNNOperandDataTypes) {
- for (let dimensions of allWebNNDimensionsArray) {
- const rank = getRank(dimensions);
- if (rank >= 1) {
- const input = builder.input(`input${++inputIndex}`, {dataType, dimensions});
- for (let invalidAxis of invalidAxisArray) {
- assert_throws_js(TypeError, () => builder[subOperationName](input, {axes: invalidAxis}));
- }
- for (let axis of notUnsignedLongAxisArray) {
- assert_false(typeof axis === 'number' && Number.isInteger(axis), `[${subOperationName}] any of options.axes elements should be of 'unsigned long'`);
- assert_throws_js(TypeError, () => builder[subOperationName](input, {axes: [axis]}));
- }
+ for (let dataType of allWebNNOperandDataTypes) {
+ for (let dimensions of allWebNNDimensionsArray) {
+ const rank = getRank(dimensions);
+ if (rank >= 1) {
+ const input =
+ builder.input(`input${++inputIndex}`, {dataType, dimensions});
+ for (let invalidAxis of invalidAxisArray) {
+ assert_throws_js(
+ TypeError,
+ () => builder[subOperationName](input, {axes: invalidAxis}));
}
- }
- }
- } else {
- // resample2d
- for (let dataType of allWebNNOperandDataTypes) {
- const input = builder.input(`input${++inputIndex}`, {dataType, dimensions: allWebNNDimensionsArray[inputRank]});
- for (let invalidAxis of invalidAxisArray) {
- assert_throws_js(TypeError, () => builder[subOperationName](input, {axes: invalidAxis}));
- }
- for (let axis of notUnsignedLongAxisArray) {
- assert_false(typeof axis === 'number' && Number.isInteger(axis), `[${subOperationName}] any of options.axes elements should be of 'unsigned long'`);
- assert_throws_js(TypeError, () => builder[subOperationName](input, {axes: [axis]}));
+ for (let axis of notUnsignedLongAxisArray) {
+ assert_false(
+ typeof axis === 'number' && Number.isInteger(axis),
+ `[${subOperationName}] any of options.axes elements should be of 'unsigned long'`);
+ assert_throws_js(
+ TypeError,
+ () => builder[subOperationName](input, {axes: [axis]}));
+ }
}
}
}
@@ -313,54 +305,39 @@
// DataError is expected if any of options.axes elements is greater or equal to the size of input
promise_test(async t => {
- if (inputRank === undefined) {
- // argMin/Max / layerNormalization / Reduction operations
- for (let dataType of allWebNNOperandDataTypes) {
- for (let dimensions of allWebNNDimensionsArray) {
- const rank = getRank(dimensions);
- if (rank >= 1) {
- const input = builder.input(`input${++inputIndex}`, {dataType, dimensions});
- assert_throws_dom('DataError', () => builder[subOperationName](input, {axes: [rank]}));
- assert_throws_dom('DataError', () => builder[subOperationName](input, {axes: [rank + 1]}));
- }
+ for (let dataType of allWebNNOperandDataTypes) {
+ for (let dimensions of allWebNNDimensionsArray) {
+ const rank = getRank(dimensions);
+ if (rank >= 1) {
+ const input =
+ builder.input(`input${++inputIndex}`, {dataType, dimensions});
+ assert_throws_dom(
+ 'DataError',
+ () => builder[subOperationName](input, {axes: [rank]}));
+ assert_throws_dom(
+ 'DataError',
+ () => builder[subOperationName](input, {axes: [rank + 1]}));
}
}
- } else {
- // resample2d
- for (let dataType of allWebNNOperandDataTypes) {
- const input = builder.input(`input${++inputIndex}`, {dataType, dimensions: allWebNNDimensionsArray[inputRank]});
- assert_throws_dom('DataError', () => builder[subOperationName](input, {axes: [inputRank]}));
- assert_throws_dom('DataError', () => builder[subOperationName](input, {axes: [inputRank + 1]}));
- }
}
}, `[${subOperationName}] DataError is expected if any of options.axes elements is greater or equal to the size of input`);
// DataError is expected if two or more values are same in the axes sequence
promise_test(async t => {
- if (inputRank === undefined) {
- // argMin/Max / layerNormalization / Reduction operations
- for (let dataType of allWebNNOperandDataTypes) {
- for (let dimensions of allWebNNDimensionsArray) {
- const rank = getRank(dimensions);
- if (rank >= 2) {
- const input = builder.input(`input${++inputIndex}`, {dataType, dimensions});
- const axesArrayContainSameValues = getAxesArrayContainSameValues(dimensions);
- for (let axes of axesArrayContainSameValues) {
- assert_throws_dom('DataError', () => builder[subOperationName](input, {axes}));
- }
+ for (let dataType of allWebNNOperandDataTypes) {
+ for (let dimensions of allWebNNDimensionsArray) {
+ const rank = getRank(dimensions);
+ if (rank >= 2) {
+ const input =
+ builder.input(`input${++inputIndex}`, {dataType, dimensions});
+ const axesArrayContainSameValues =
+ getAxesArrayContainSameValues(dimensions);
+ for (let axes of axesArrayContainSameValues) {
+ assert_throws_dom(
+ 'DataError', () => builder[subOperationName](input, {axes}));
}
}
}
- } else {
- // resample2d
- for (let dataType of allWebNNOperandDataTypes) {
- const dimensions = allWebNNDimensionsArray[inputRank];
- const input = builder.input(`input${++inputIndex}`, {dataType, dimensions});
- const axesArrayContainSameValues = getAxesArrayContainSameValues(dimensions);
- for (let axes of axesArrayContainSameValues) {
- assert_throws_dom('DataError', () => builder[subOperationName](input, {axes}));
- }
- }
}
}, `[${subOperationName}] DataError is expected if two or more values are same in the axes sequence`);
}
diff --git a/webnn/validation_tests/resample2d.https.any.js b/webnn/validation_tests/resample2d.https.any.js
index defb099..de44c6a 100644
--- a/webnn/validation_tests/resample2d.https.any.js
+++ b/webnn/validation_tests/resample2d.https.any.js
@@ -4,7 +4,155 @@
'use strict';
-validateOptionsAxes('resample2d', 4);
+// Tests for resample2d(input, options)
+const tests = [
+ {
+ name: '[resample2d] Test building resample2d with default options',
+ input: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
+ output: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
+ },
+ {
+ name: '[resample2d] Test building resample2d with scales=[2.0, 2.0]',
+ input: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
+ options: {scales: [2.0, 2.0]},
+ output: {dataType: 'float32', dimensions: [1, 1, 4, 8]},
+ },
+ {
+ name: '[resample2d] Test building resample2d with scales=[0.5, 0.5]',
+ input: {dataType: 'float32', dimensions: [1, 1, 5, 5]},
+ options: {scales: [0.5, 0.5]},
+ output: {dataType: 'float32', dimensions: [1, 1, 2, 2]},
+ },
+ {
+ name:
+ '[resample2d] Test building resample2d with scales=[0.5, 0.5] and explicit axes=[2, 3]',
+ input: {dataType: 'float32', dimensions: [1, 1, 5, 5]},
+ options: {scales: [0.5, 0.5], axes: [2, 3]},
+ output: {dataType: 'float32', dimensions: [1, 1, 2, 2]},
+ },
+ {
+ name:
+ '[resample2d] Test building resample2d with scales=[1.0, 2.0] and axes=[0, 1]',
+ input: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
+ options: {scales: [1.0, 2.0], axes: [0, 1]},
+ output: {dataType: 'float32', dimensions: [1, 2, 2, 4]},
+ },
+ {
+ name:
+ '[resample2d] Test building resample2d with scales=[2.0, 2.0] and axes=[1, 2]',
+ input: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
+ options: {scales: [2.0, 2.0], axes: [1, 2]},
+ output: {dataType: 'float32', dimensions: [1, 2, 4, 4]},
+ },
+ {
+ name:
+ '[resample2d] Test building resample2d with sizes=[3, 6] ignored scales',
+ input: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
+ options: {scales: [2.0, 2.0], sizes: [3, 6]},
+ output: {dataType: 'float32', dimensions: [1, 1, 3, 6]},
+ },
+ {
+ name: '[resample2d] Throw if the rank of input is not 4',
+ input: {dataType: 'float32', dimensions: [2, 4]},
+ },
+ {
+ name: '[resample2d] Throw if the length of scales is not 2',
+ input: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
+ options: {scales: [1.0, 1.0, 2.0, 2.0]},
+ },
+ {
+ name: '[resample2d] Throw if any scale value is negative',
+ input: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
+ options: {scales: [1.0, -2.0]},
+ },
+ {
+ name: '[resample2d] Throw if any scale value is 0',
+ input: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
+ options: {scales: [0, 2.0]},
+ },
+ {
+ name: '[resample2d] Throw if the length of sizes is not 2',
+ input: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
+ options: {sizes: [1, 1, 4, 6]},
+ },
+ {
+ name:
+ '[resample2d] Throw if any size value is out of \'unsigned long\' value range',
+ input: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
+ options: {sizes: [kMaxUnsignedLong + 1, kMaxUnsignedLong + 1]},
+ },
+ {
+ name:
+ '[resample2d] Throw if outputHeight being floor(scaleHeight*inputHeight) is too large',
+ input: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
+ // The maximum dimension size is kMaxUnsignedLong (2 ** 32 - 1).
+ // Here scaleHeight=kMaxUnsignedLong and inputHeight=2,
+ // so outputHeight being kMaxUnsignedLong*2 > kMaxUnsignedLong .
+ options: {scales: /*[scaleHeight, scaleWidth]*/[kMaxUnsignedLong, 1]},
+ },
+ {
+ name: '[resample2d] Throw if scaleHeight is too small',
+ input: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
+ // Here scaleHeight=0.02 and inputHeight=2,
+ // so outputHeight would be 0.
+ // Link to https://github.com/webmachinelearning/webnn/issues/391.
+ options: {scales: /*[scaleHeight, scaleWidth]*/[0.02, 0.8]},
+ },
+ {
+ name:
+ '[resample2d] Throw if outputWidth being floor(scaleWidth*inputWidth) is too large',
+ input: {dataType: 'float32', dimensions: [1, 1, 4, 2]},
+ // The maximum dimension size is kMaxUnsignedLong (2 ** 32 - 1).
+ // Here scaleWidth=kMaxUnsignedLong and inputWidth=2,
+ // so outputWidth being kMaxUnsignedLong*2 > kMaxUnsignedLong .
+ options: {scales: /*[scaleHeight, scaleWidth]*/[1, kMaxUnsignedLong]},
+ },
+ {
+ name: '[resample2d] Throw if scaleWidth is too small',
+ input: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
+ // Here scaleWidth=0.1 and inputWidth=4,
+ // so outputWidth would be 0.
+ // Link to https://github.com/webmachinelearning/webnn/issues/391.
+ options: {scales: /*[scaleHeight, scaleWidth]*/[0.7, 0.1]},
+ },
+ {
+ name: '[resample2d] Throw if the length of axes is not 2',
+ input: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
+ options: {axes: [0, 1, 2]},
+ },
+ {
+ name:
+ '[resample2d] Throw if any axis value is greater than or equal to the input rank',
+ input: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
+ options: {axes: [3, 4]},
+ },
+ {
+ // The valid values in the axes sequence are [0, 1], [1, 2] or [2, 3]
+ name: '[resample2d] Throw if the values of axes are inconsecutive',
+ input: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
+ options: {axes: [0, 2]},
+ },
+ {
+ name: '[resample2d] Throw if the values of axes are same',
+ input: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
+ options: {axes: [0, 0]},
+ },
+];
+
+tests.forEach(
+ test => promise_test(async t => {
+ const input = builder.input(
+ 'input',
+ {dataType: test.input.dataType, dimensions: test.input.dimensions});
+ const options = test.options ?? {};
+ if (test.output) {
+ const output = builder.resample2d(input, options);
+ assert_equals(output.dataType(), test.output.dataType);
+ assert_array_equals(output.shape(), test.output.dimensions);
+ } else {
+ assert_throws_js(TypeError, () => builder.resample2d(input, options));
+ }
+ }, test.name));
validateInputFromAnotherBuilder(
'resample2d', {dataType: 'float32', dimensions: [2, 2, 2, 2]});