Bug 1886677 [wpt PR 45243] - webnn: migrate unary operations unit tests to WPTs., a=testonly

Automatic update from web-platform-tests
webnn: migrate unary operations unit tests to WPTs.

This CL migrates unary operations unit tests to WPTs, including
element-wise unary (abs, ceil, cos, erf, exp, floor, identity, log, neg,
reciprocal, sin, sqrt, tan), hardSwish, relu, sigmoid, softsign and
tanh.

Bug: 327337526, 328026885
Change-Id: I45505f5c7327625791a5dcf3bf47777a68d8f5df
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5384746
Reviewed-by: ningxin hu <ningxin.hu@intel.com>
Reviewed-by: Austin Sullivan <asully@chromium.org>
Auto-Submit: Shiyi Zou <shiyi.zou@intel.com>
Commit-Queue: Shiyi Zou <shiyi.zou@intel.com>
Cr-Commit-Position: refs/heads/main@{#1278180}

--

wpt-commits: a281d0833c088055f474f6c5ae5d8108deb7255d
wpt-pr: 45243
This commit is contained in:
Shiyi Zou 2024-03-27 23:10:01 +00:00 committed by moz-wptsync-bot
parent 17fd0a9dc1
commit cbcc3ed727
7 changed files with 90 additions and 0 deletions

View File

@ -17,6 +17,11 @@ const allWebNNOperandDataTypes = [
// range [0, 4294967295].
// 4294967295 = 2 ** 32 - 1
const kMaxUnsignedLong = 2 ** 32 - 1;
const floatingPointTypes = ['float32', 'float16'];
const signedIntegerTypes = ['int32', 'int64', 'int8'];
const unsignedLongType = 'unsigned long';
const dimensions0D = [];
@ -361,6 +366,51 @@ function validateOptionsAxes(operationName, inputRank) {
}
}
/**
* Validate a unary operation
* @param {String} operationName - An operation name
* @param {Array} supportedDataTypes - Test building with these data types
* succeeds and test building with all other data types fails
* @param {Boolean} alsoBuildActivation - If test building this operation as an
* activation
*/
function validateUnaryOperation(
operationName, supportedDataTypes, alsoBuildActivation = false) {
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}]`);
}
}
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}]`);
}
}
if (alsoBuildActivation) {
promise_test(async t => {
builder[operationName]();
}, `[${operationName}] Test building an activation`);
}
}
/**
* Basic test that the builder method specified by `operationName` throws if
* given an input from another builder. Operands which do not accept a float32

View File

@ -12,3 +12,28 @@ const kElementwiseUnaryOperators = [
kElementwiseUnaryOperators.forEach((operatorName) => {
validateInputFromAnotherBuilder(operatorName);
});
const kElementwiseUnaryOperations = [
{
name: 'abs',
supportedDataTypes: [...floatingPointTypes, ...signedIntegerTypes]
},
{name: 'ceil', supportedDataTypes: floatingPointTypes},
{name: 'exp', supportedDataTypes: floatingPointTypes},
{name: 'floor', supportedDataTypes: floatingPointTypes},
{name: 'log', supportedDataTypes: floatingPointTypes}, {
name: 'neg',
supportedDataTypes: [...floatingPointTypes, ...signedIntegerTypes]
},
{name: 'sin', supportedDataTypes: floatingPointTypes},
{name: 'tan', supportedDataTypes: floatingPointTypes},
{name: 'erf', supportedDataTypes: floatingPointTypes},
{name: 'identity', supportedDataTypes: allWebNNOperandDataTypes},
{name: 'logicalNot', supportedDataTypes: ['uint8']},
{name: 'reciprocal', supportedDataTypes: floatingPointTypes},
{name: 'sqrt', supportedDataTypes: floatingPointTypes}
];
kElementwiseUnaryOperations.forEach((operation) => {
validateUnaryOperation(operation.name, operation.supportedDataTypes);
});

View File

@ -5,3 +5,6 @@
'use strict';
validateInputFromAnotherBuilder('hardSwish');
validateUnaryOperation(
'hardSwish', floatingPointTypes, /*alsoBuildActivation=*/ true);

View File

@ -5,3 +5,6 @@
'use strict';
validateInputFromAnotherBuilder('relu');
validateUnaryOperation(
'relu', allWebNNOperandDataTypes, /*alsoBuildActivation=*/ true);

View File

@ -5,3 +5,6 @@
'use strict';
validateInputFromAnotherBuilder('sigmoid');
validateUnaryOperation(
'sigmoid', floatingPointTypes, /*alsoBuildActivation=*/ true);

View File

@ -5,3 +5,6 @@
'use strict';
validateInputFromAnotherBuilder('softsign');
validateUnaryOperation(
'softsign', floatingPointTypes, /*alsoBuildActivation=*/ true);

View File

@ -5,3 +5,6 @@
'use strict';
validateInputFromAnotherBuilder('tanh');
validateUnaryOperation(
'tanh', floatingPointTypes, /*alsoBuildActivation=*/ true);