Skip to content

Commit

Permalink
webnn: Migrate ElementWiseBinary validation tests to WPTs
Browse files Browse the repository at this point in the history
This CL adds WPT tests for ElementWiseBinary and removes the unit tests
`MLGraphBuilderElementWiseBinaryTest.TestElementWiseBinary` ,
`MLGraphTestMojo.ElementWiseBinaryTest`,
`MLGraphTestMojo.ElementWiseBinaryLogicalTest` and `MLGraphTest,
ElementWiseBinaryTest`.

Bug: 327337526, 328026885
Change-Id: I21b96ea03dc5dc070173151f53ca7d5b63a73281
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5531844
Reviewed-by: Austin Sullivan <asully@chromium.org>
Commit-Queue: Shanxing Mei <shanxing.mei@intel.com>
Reviewed-by: ningxin hu <ningxin.hu@intel.com>
Cr-Commit-Position: refs/heads/main@{#1303007}
  • Loading branch information
mei1127 authored and chromium-wpt-export-bot committed May 18, 2024
1 parent a7f0f76 commit 2ad1106
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
13 changes: 8 additions & 5 deletions webnn/resources/utils_validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,15 @@ function validateTwoInputsBroadcastable(operationName) {
const unbroadcastableDimensionsArray = generateUnbroadcastableDimensionsArray(dimensions);
for (let unbroadcastableDimensions of unbroadcastableDimensionsArray) {
const inputB = builder.input(`inputB${++inputBIndex}`, {dataType, dimensions: unbroadcastableDimensions});
assert_throws_dom('DataError', () => builder[operationName](inputA, inputB));
assert_throws_dom('DataError', () => builder[operationName](inputB, inputA));
assert_throws_js(
TypeError, () => builder[operationName](inputA, inputB));
assert_throws_js(
TypeError, () => builder[operationName](inputB, inputA));
}
}
}
}
}, `[${operationName}] DataError is expected if two inputs aren't broadcastable`);
}, `[${operationName}] TypeError is expected if two inputs aren't broadcastable`);
}

function validateTwoInputsOfSameDataType(operationName) {
Expand All @@ -248,12 +250,13 @@ function validateTwoInputsOfSameDataType(operationName) {
for (let dataTypeB of allWebNNOperandDataTypes) {
if (dataType !== dataTypeB) {
const inputB = builder.input(`inputB${++inputBIndex}`, {dataType: dataTypeB, dimensions});
assert_throws_dom('DataError', () => builder[subOperationName](inputA, inputB));
assert_throws_js(
TypeError, () => builder[subOperationName](inputA, inputB));
}
}
}
}
}, `[${subOperationName}] DataError is expected if two inputs aren't of same data type`);
}, `[${subOperationName}] TypeError is expected if two inputs aren't of same data type`);
}
}

Expand Down
56 changes: 56 additions & 0 deletions webnn/validation_tests/elementwise-binary.https.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,64 @@ const kElementwiseBinaryOperators = [
'pow',
];


const tests = [
{
name: '[binary] Test bidirectionally broadcastable dimensions.',
// Both inputs have axes of length one which are expanded
// during broadcasting.
a: {dataType: 'float32', dimensions: [8, 1, 6, 1]},
b: {dataType: 'float32', dimensions: [7, 1, 5]},
output: {dataType: 'float32', dimensions: [8, 7, 6, 5]}
},
{
name: '[binary] Test unidirectionally broadcastable dimensions.',
// Input a has a single axis of length one which is
// expanded during broadcasting.
a: {dataType: 'float32', dimensions: [4, 2, 1]},
b: {dataType: 'float32', dimensions: [4]},
output: {dataType: 'float32', dimensions: [4, 2, 4]}
},
{
name: '[binary] Test scalar broadcasting.',
a: {dataType: 'float32', dimensions: [4, 2, 4]},
b: {dataType: 'float32', dimensions: []},
output: {dataType: 'float32', dimensions: [4, 2, 4]}
},
{
name: '[binary] Throw if the input shapes are not broadcastable.',
a: {dataType: 'float32', dimensions: [4, 2]},
b: {dataType: 'float32', dimensions: [4]},
},
{
name: '[binary] Throw if the input types don\'t match.',
a: {dataType: 'float32', dimensions: [4, 2]},
b: {dataType: 'int32', dimensions: [1]},
},
];

function runElementWiseBinaryTests(operatorName, tests) {
tests.forEach(test => {
promise_test(async t => {
const a = builder.input(
'a', {dataType: test.a.dataType, dimensions: test.a.dimensions});
const b = builder.input(
'b', {dataType: test.b.dataType, dimensions: test.b.dimensions});

if (test.output) {
const output = builder[operatorName](a, b);
assert_equals(output.dataType(), test.output.dataType);
assert_array_equals(output.shape(), test.output.dimensions);
} else {
assert_throws_js(TypeError, () => builder[operatorName](a, b));
}
}, test.name.replace('[binary]', `[${operatorName}]`));
});
}

kElementwiseBinaryOperators.forEach((operatorName) => {
validateTwoInputsOfSameDataType(operatorName);
validateTwoInputsBroadcastable(operatorName);
validateTwoInputsFromMultipleBuilders(operatorName);
runElementWiseBinaryTests(operatorName, tests);
});

0 comments on commit 2ad1106

Please sign in to comment.