Skip to content

Commit

Permalink
webnn: Migrated unit tests for input and constant interfaces into WPT
Browse files Browse the repository at this point in the history
This CL adds WPT tests for
  1) `input(name, descriptor)` interface,
  2) `constant(descriptor, bufferView)` interface,

and removes these unit tests
  1) `MLGraphBuilderTest.InputTest`,
  2) `MLGraphBuilderTest.ConstantTest`.

Bug: 327337525, 327337526, 328026885
Change-Id: I95cba29fc189854650ba7ad3bdece4c2d1e26390
Cq-Include-Trybots: luci.chromium.try:win11-blink-rel
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5365030
Commit-Queue: Feng Dai <[email protected]>
Reviewed-by: Austin Sullivan <[email protected]>
Reviewed-by: ningxin hu <[email protected]>
Auto-Submit: Feng Dai <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1273229}
  • Loading branch information
BruceDai authored and chromium-wpt-export-bot committed Mar 15, 2024
1 parent 0d0f9cc commit f9a21fc
Show file tree
Hide file tree
Showing 2 changed files with 211 additions and 0 deletions.
142 changes: 142 additions & 0 deletions webnn/validation_tests/constant.https.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// META: title=validation tests for WebNN API constant interface
// META: global=window,dedicatedworker
// META: script=../resources/utils_validation.js
// META: timeout=long

'use strict';

const tests = [
// Tests for constant(descriptor, bufferView)
{
name:
'[constant] Test building a 0-D scalar constant without presenting dimensions',
descriptor: {dataType: 'float32'},
bufferView: {type: Float32Array, byteLength: 1 * 4},
output: {dataType: 'float32', dimensions: []}
},
{
name:
'[constant] Test building a 0-D scalar constant with empty dimensions',
descriptor: {dataType: 'float32', dimensions: []},
bufferView: {type: Float32Array, byteLength: 1 * 4},
output: {dataType: 'float32', dimensions: []}
},
{
name: '[constant] Test building a constant with float32 data type',
descriptor: {dataType: 'float32', dimensions: [2, 3]},
bufferView: {type: Float32Array, byteLength: 6 * 4},
output: {dataType: 'float32', dimensions: [2, 3]}
},
{
name:
'[constant] Throw if byte length of bufferView for float32 doesn\'t match the given dimensions',
descriptor: {dataType: 'float32', dimensions: [2, 3]},
bufferView: {
type: Float32Array,
byteLength: 6 * 4 - 4 // The bufferView's byte length is less than the
// one by given dimensions
}
},
// TODO (crbug.com/329702838): Test building a constant with float16 data type
{
name: '[constant] Test building a constant with int32 data type',
descriptor: {dataType: 'int32', dimensions: [2, 3]},
bufferView: {type: Int32Array, byteLength: 6 * 4},
output: {dataType: 'int32', dimensions: [2, 3]}
},
{
name:
'[constant] Throw if byte length of bufferView for int32 doesn\'t match the given dimensions',
descriptor: {dataType: 'int32', dimensions: [2, 3]},
bufferView: {
type: Int32Array,
byteLength: 6 * 4 + 4 // The bufferView's byte length is greater than the
// one by given dimensions
}
},
{
name: '[constant] Test building a constant with uint32 data type',
descriptor: {dataType: 'uint32', dimensions: [2, 3]},
bufferView: {type: Uint32Array, byteLength: 6 * 4},
output: {dataType: 'uint32', dimensions: [2, 3]}
},
{
name:
'[constant] Throw if byte length of bufferView for uint32 doesn\'t match the given dimensions',
descriptor: {dataType: 'uint32', dimensions: [2, 3]},
bufferView: {type: Uint32Array, byteLength: 6 * 4 + 4}
},
{
name: '[constant] Test building a constant with int64 data type',
descriptor: {dataType: 'int64', dimensions: [2, 3]},
bufferView: {type: BigInt64Array, byteLength: 6 * 8},
output: {dataType: 'int64', dimensions: [2, 3]}
},
{
name:
'[constant] Throw if byte length of bufferView for int64 doesn\'t match the given dimensions',
descriptor: {dataType: 'int64', dimensions: [2, 3]},
bufferView: {type: BigInt64Array, byteLength: 6 * 8 + 8}
},
{
name: '[constant] Test building a constant with uint64 data type',
descriptor: {dataType: 'uint64', dimensions: [2, 3]},
bufferView: {type: BigUint64Array, byteLength: 6 * 8},
output: {dataType: 'uint64', dimensions: [2, 3]}
},
{
name:
'[constant] Throw if byte length of bufferView for uint64 doesn\'t match the given dimensions',
descriptor: {dataType: 'uint64', dimensions: [2, 3]},
bufferView: {type: BigUint64Array, byteLength: 6 * 8 + 8}
},
{
name: '[constant] Test building a constant with int8 data type',
descriptor: {dataType: 'int8', dimensions: [2, 3]},
bufferView: {type: Int8Array, byteLength: 6 * 1},
output: {dataType: 'int8', dimensions: [2, 3]}
},
{
name:
'[constant] Throw if byte length of bufferView for int8 doesn\'t match the given dimensions',
descriptor: {dataType: 'int8', dimensions: [2, 3]},
bufferView: {type: Int8Array, byteLength: 6 * 4 - 4}
},
{
name: '[constant] Test building a constant with uint8 data type',
descriptor: {dataType: 'uint8', dimensions: [2, 3]},
bufferView: {type: Uint8Array, byteLength: 6 * 1},
output: {dataType: 'uint8', dimensions: [2, 3]}
},
{
name:
'[constant] Throw if byte length of bufferView for uint8 doesn\'t match the given dimensions',
descriptor: {dataType: 'uint8', dimensions: [2, 3]},
bufferView: {type: Uint8Array, byteLength: 6 * 4 - 4}
},
{
name: '[constant] Throw if a dimension is 0',
descriptor: {dataType: 'float32', dimensions: [2, 0]},
bufferView: {type: Float32Array, byteLength: 2 * 4}
},
{
name:
'[constant] Throw if bufferView type doesn\'t match the operand data type',
descriptor: {dataType: 'float32', dimensions: [2, 3]},
bufferView: {type: Int32Array, byteLength: 6 * 4}
}
];

tests.forEach(
test => promise_test(async t => {
const buffer = new ArrayBuffer(test.bufferView.byteLength);
const bufferView = new test.bufferView.type(buffer);
if (test.output) {
const constantOperand = builder.constant(test.descriptor, bufferView);
assert_equals(constantOperand.dataType(), test.output.dataType);
assert_array_equals(constantOperand.shape(), test.output.dimensions);
} else {
assert_throws_js(
TypeError, () => builder.constant(test.descriptor, bufferView));
}
}, test.name));
69 changes: 69 additions & 0 deletions webnn/validation_tests/input.https.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// META: title=validation tests for WebNN API input interface
// META: global=window,dedicatedworker
// META: script=../resources/utils_validation.js
// META: timeout=long

'use strict';

// Tests for input(name, descriptor)
const tests = [
{
testName:
'[input] Test building a 0-D scalar input without presenting dimensions',
name: 'input',
descriptor: {dataType: 'float32'},
output: {dataType: 'float32', dimensions: []},
},
{
testName: '[input] Test building a 0-D scalar input with empty dimensions',
name: 'input',
descriptor: {dataType: 'float32', dimensions: []},
output: {dataType: 'float32', dimensions: []},
},
{
testName: '[input] Test building a 1-D input with int64 data type',
name: 'input',
descriptor: {dataType: 'int64', dimensions: [3]},
output: {dataType: 'int64', dimensions: [3]},
},
{
testName: '[input] Test building a 2-D input without errors',
name: 'input',
descriptor: {dataType: 'float32', dimensions: [3, 4]},
output: {dataType: 'float32', dimensions: [3, 4]},
},
{
testName: '[input] Throw if the name is empty',
name: '',
descriptor: {dataType: 'float32', dimensions: [3, 4]}
},
{
testName: '[input] Throw if a dimension size is 0',
name: 'input',
descriptor: {dataType: 'float32', dimensions: [3, 0]}
},
{
testName: '[input] Throw if the number of elements is too large',
name: 'input',
descriptor: {
dataType: 'float32',
dimensions: [
// Refer to
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Invalid_array_length
2 ** 33 + 1
]
}
}
];

tests.forEach(
test => promise_test(async t => {
if (test.output) {
const inputOperand = builder.input(test.name, test.descriptor);
assert_equals(inputOperand.dataType(), test.output.dataType);
assert_array_equals(inputOperand.shape(), test.output.dimensions);
} else {
assert_throws_js(
TypeError, () => builder.input(test.name, test.descriptor));
}
}, test.testName));

0 comments on commit f9a21fc

Please sign in to comment.