Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webnn: update validation_tests to use opSupportLimits #46936

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
webnn: update validation_tests to use opSupportLimits
Update validation_tests to check opSupportLimits for input supported
data types. And assert `builder.input` throw errors if input data type
is not supported.

Change-Id: Ia3caddf368bd64f0f998265f4bad0cd77a628106
Bug: 345271830
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5664119
Commit-Queue: Phillis Tang <phillis@chromium.org>
Reviewed-by: Austin Sullivan <asully@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1321081}
  • Loading branch information
philloooo authored and chromium-wpt-export-bot committed Jun 28, 2024
commit 1649fbc4b54077a8e75028cee6ebad0f616e8073
97 changes: 73 additions & 24 deletions webnn/resources/utils_validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ function validateTwoInputsBroadcastable(operationName) {
}
promise_test(async t => {
for (let dataType of allWebNNOperandDataTypes) {
if (!context.opSupportLimits().input.dataTypes.includes(dataType)) {
assert_throws_js(
TypeError,
() => builder.input(
`inputA${++inputAIndex}`, {dataType, dimensions1D}));
continue;
}
for (let dimensions of allWebNNDimensionsArray) {
if (dimensions.length > 0) {
const inputA = builder.input(`inputA${++inputAIndex}`, {dataType, dimensions});
Expand Down Expand Up @@ -245,9 +252,24 @@ function validateTwoInputsOfSameDataType(operationName) {
for (let subOperationName of operationNameArray) {
promise_test(async t => {
for (let dataType of allWebNNOperandDataTypes) {
if (!context.opSupportLimits().input.dataTypes.includes(dataType)) {
assert_throws_js(
TypeError,
() => builder.input(
`inputA${++inputAIndex}`, {dataType, dimensions1D}));
continue;
}
for (let dimensions of allWebNNDimensionsArray) {
const inputA = builder.input(`inputA${++inputAIndex}`, {dataType, dimensions});
for (let dataTypeB of allWebNNOperandDataTypes) {
if (!context.opSupportLimits().input.dataTypes.includes(
dataTypeB)) {
assert_throws_js(
TypeError,
() => builder.input(
`inputB${++inputBIndex}`, {dataTypeB, dimensions1D}));
continue;
}
if (dataType !== dataTypeB) {
const inputB = builder.input(`inputB${++inputBIndex}`, {dataType: dataTypeB, dimensions});
assert_throws_js(
Expand Down Expand Up @@ -283,6 +305,13 @@ function validateOptionsAxes(operationName) {
// TypeError is expected if any of options.axes elements is not an unsigned long interger
promise_test(async t => {
for (let dataType of allWebNNOperandDataTypes) {
if (!context.opSupportLimits().input.dataTypes.includes(dataType)) {
assert_throws_js(
TypeError,
() => builder.input(
`inputA${++inputAIndex}`, {dataType, dimensions1D}));
continue;
}
for (let dimensions of allWebNNDimensionsArray) {
const rank = getRank(dimensions);
if (rank >= 1) {
Expand Down Expand Up @@ -310,6 +339,13 @@ function validateOptionsAxes(operationName) {
// to the size of input
promise_test(async t => {
for (let dataType of allWebNNOperandDataTypes) {
if (!context.opSupportLimits().input.dataTypes.includes(dataType)) {
assert_throws_js(
TypeError,
() => builder.input(
`inputA${++inputAIndex}`, {dataType, dimensions1D}));
continue;
}
for (let dimensions of allWebNNDimensionsArray) {
const rank = getRank(dimensions);
if (rank >= 1) {
Expand All @@ -329,6 +365,13 @@ function validateOptionsAxes(operationName) {
// TypeError is expected if two or more values are same in the axes sequence
promise_test(async t => {
for (let dataType of allWebNNOperandDataTypes) {
if (!context.opSupportLimits().input.dataTypes.includes(dataType)) {
assert_throws_js(
TypeError,
() => builder.input(
`inputA${++inputAIndex}`, {dataType, dimensions1D}));
continue;
}
for (let dimensions of allWebNNDimensionsArray) {
const rank = getRank(dimensions);
if (rank >= 2) {
Expand Down Expand Up @@ -357,35 +400,41 @@ function validateOptionsAxes(operationName) {
*/
function validateUnaryOperation(
operationName, supportedDataTypes, alsoBuildActivation = false) {
// TODO: crbug.com/345271830 - use context.opSupportLimits to get supported
// data types for current context.
for (let dataType of supportedDataTypes) {
for (let dimensions of allWebNNDimensionsArray) {
promise_test(
async t => {
const input = builder.input(`input`, {dataType, dimensions});
const output = builder[operationName](input);
assert_equals(output.dataType(), dataType);
assert_array_equals(output.shape(), dimensions);
},
`[${operationName}] Test building an operator, dataType = ${
dataType}, dimensions = [${dimensions}]`);
promise_test(async t => {
for (let dataType of supportedDataTypes) {
if (!context.opSupportLimits().input.dataTypes.includes(dataType)) {
assert_throws_js(
TypeError,
() => builder.input(
`inputA${++inputAIndex}`, {dataType, dimensions1D}));
continue;
}
for (let dimensions of allWebNNDimensionsArray) {
const input = builder.input(`input`, {dataType, dimensions});
const output = builder[operationName](input);
assert_equals(output.dataType(), dataType);
assert_array_equals(output.shape(), dimensions);
}
}
}
}, `[${operationName}] Test building an unary operator with supported type.`);

const unsupportedDataTypes =
new Set(allWebNNOperandDataTypes).difference(new Set(supportedDataTypes));
for (let dataType of unsupportedDataTypes) {
for (let dimensions of allWebNNDimensionsArray) {
promise_test(
async t => {
const input = builder.input(`input`, {dataType, dimensions});
assert_throws_js(TypeError, () => builder[operationName](input));
},
`[${operationName}] Throw if the dataType is not supported, dataType = ${
dataType}, dimensions = [${dimensions}]`);
promise_test(async t => {
for (let dataType of unsupportedDataTypes) {
if (!context.opSupportLimits().input.dataTypes.includes(dataType)) {
assert_throws_js(
TypeError,
() => builder.input(
`inputA${++inputAIndex}`, {dataType, dimensions1D}));
continue;
}
for (let dimensions of allWebNNDimensionsArray) {
const input = builder.input(`input`, {dataType, dimensions});
assert_throws_js(TypeError, () => builder[operationName](input));
}
}
}
}, `[${operationName}] Throw if the dataType is not supported for an unary operator.`);

if (alsoBuildActivation) {
promise_test(async t => {
Expand Down
21 changes: 21 additions & 0 deletions webnn/validation_tests/clamp.https.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ validateUnaryOperation('clamp', allWebNNOperandDataTypes);

promise_test(async t => {
const options = {minValue: 1.0, maxValue: 3.0};
if (!context.opSupportLimits().input.dataTypes.includes('uint32')) {
assert_throws_js(
TypeError,
() => builder.input(
'input', {dataType: 'uint32', dimensions: [1, 2, 3]}));
return;
}
const input =
builder.input('input', {dataType: 'uint32', dimensions: [1, 2, 3]});
const output = builder.clamp(input, options);
Expand All @@ -19,6 +26,13 @@ promise_test(async t => {

promise_test(async t => {
const options = {minValue: 0, maxValue: 0};
if (!context.opSupportLimits().input.dataTypes.includes('int32')) {
assert_throws_js(
TypeError,
() => builder.input(
'input', {dataType: 'int32', dimensions: [1, 2, 3, 4]}));
return;
}
const input =
builder.input('input', {dataType: 'int32', dimensions: [1, 2, 3, 4]});
const output = builder.clamp(input, options);
Expand All @@ -28,6 +42,13 @@ promise_test(async t => {

promise_test(async t => {
const options = {minValue: 3.0, maxValue: 1.0};
if (!context.opSupportLimits().input.dataTypes.includes('uint8')) {
assert_throws_js(
TypeError,
() =>
builder.input('input', {dataType: 'uint8', dimensions: [1, 2, 3]}));
return;
}
const input =
builder.input('input', {dataType: 'uint8', dimensions: [1, 2, 3]});
assert_throws_js(TypeError, () => builder.clamp(input, options));
Expand Down
18 changes: 18 additions & 0 deletions webnn/validation_tests/elementwise-binary.https.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ const tests = [
function runElementWiseBinaryTests(operatorName, tests) {
tests.forEach(test => {
promise_test(async t => {
if (!context.opSupportLimits().input.dataTypes.includes(
test.a.dataType)) {
assert_throws_js(
TypeError,
() => builder.input(
'a',
{dataType: test.a.dataType, dimensions: test.a.dimensions}));
return;
}
if (!context.opSupportLimits().input.dataTypes.includes(
test.b.dataType)) {
assert_throws_js(
TypeError,
() => builder.input(
'b',
{dataType: test.b.dataType, dimensions: test.b.dimensions}));
return;
}
const a = builder.input(
'a', {dataType: test.a.dataType, dimensions: test.a.dimensions});
const b = builder.input(
Expand Down