0

webnn: add a test for constant reshape optimization

Bug: 364687938, 380623032
Change-Id: I549b99e72817a218502df4270c08a4e75c287c87
Cq-Include-Trybots: luci.chromium.try:win11-blink-rel,mac14-blink-rel,mac15.arm64-blink-rel,mac15-blink-rel
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5926125
Commit-Queue: Weizhong Xia <weizhong@google.com>
Reviewed-by: ningxin hu <ningxin.hu@intel.com>
Reviewed-by: Phillis Tang <phillis@chromium.org>
Auto-Submit: Feng Dai <feng.dai@intel.com>
Reviewed-by: Weizhong Xia <weizhong@google.com>
Cr-Commit-Position: refs/heads/main@{#1433606}
This commit is contained in:
BruceDai
2025-03-17 10:17:27 -07:00
committed by Chromium LUCI CQ
parent e59b3b0b09
commit 7163c65e1f
11 changed files with 171 additions and 10 deletions
third_party/blink/web_tests
VirtualTestSuites
external
platform
mac
virtual
webnn-service-on-cpu
external
wpt
webnn-service-with-gpu
external
wpt
win
virtual
webnn-service-on-cpu
external
wpt
webnn-service-with-gpu
external
wpt
win11-arm64
virtual
webnn-service-with-gpu
external
wpt
virtual
webnn-service-on-npu
external
wpt

@ -2093,6 +2093,7 @@
"external/wpt/webnn/conformance_tests/ceil.https.any.html?gpu",
"external/wpt/webnn/conformance_tests/clamp.https.any.html?gpu",
"external/wpt/webnn/conformance_tests/concat.https.any.html?gpu",
"external/wpt/webnn/conformance_tests/constant-reshape-optimization.https.any.html?gpu",
"external/wpt/webnn/conformance_tests/conv_transpose2d.https.any.html?gpu",
"external/wpt/webnn/conformance_tests/conv2d.https.any.html?gpu",
"external/wpt/webnn/conformance_tests/cumulative_sum.https.any.html?gpu",
@ -2344,6 +2345,7 @@
"external/wpt/webnn/conformance_tests/ceil.https.any.html?cpu",
"external/wpt/webnn/conformance_tests/clamp.https.any.html?cpu",
"external/wpt/webnn/conformance_tests/concat.https.any.html?cpu",
"external/wpt/webnn/conformance_tests/constant-reshape-optimization.https.any.html?cpu",
"external/wpt/webnn/conformance_tests/conv_transpose2d.https.any.html?cpu",
"external/wpt/webnn/conformance_tests/conv2d.https.any.html?cpu",
"external/wpt/webnn/conformance_tests/cumulative_sum.https.any.html?cpu",
@ -2591,6 +2593,7 @@
"external/wpt/webnn/conformance_tests/ceil.https.any.html?npu",
"external/wpt/webnn/conformance_tests/clamp.https.any.html?npu",
"external/wpt/webnn/conformance_tests/concat.https.any.html?npu",
"external/wpt/webnn/conformance_tests/constant-reshape-optimization.https.any.html?npu",
"external/wpt/webnn/conformance_tests/conv_transpose2d.https.any.html?npu",
"external/wpt/webnn/conformance_tests/conv2d.https.any.html?npu",
"external/wpt/webnn/conformance_tests/cumulative_sum.https.any.html?npu",

@ -0,0 +1,96 @@
// META: title=test constant reshape optimization
// META: global=window
// META: variant=?cpu
// META: variant=?gpu
// META: variant=?npu
// META: script=../resources/utils.js
// META: timeout=long
'use strict';
const tests = [{
'name': 'reshape + reshape + reshape + instanceNormalization float32',
'graph': {
'inputs': {
'originalInput': {
'data': [
-97.949951171875, 29.44037628173828, -73.92131042480469,
-38.11185836791992, 41.33772659301758, -59.77853012084961,
-74.66901397705078, -68.16508483886719, 35.82481384277344,
-6.948329448699951, 54.42462158203125, 47.53074645996094,
66.93562316894531, 76.74034881591797, 5.6758809089660645,
25.68659210205078, 37.37651062011719, 56.252689361572266,
-16.574905395507812, 42.949893951416016, 73.8739242553711,
-99.00035095214844, -33.11322784423828, -17.380685806274414
],
'descriptor': {shape: [3, 8], dataType: 'float32'},
'constant': true
},
'originalScale': {
'data': [-94.42772674560547, 66.69620513916016, -98.56572723388672],
'descriptor': {shape: [1, 3, 1, 1], dataType: 'float32'},
'constant': true
},
'originalBias': {
'data': [-33.048641204833984, 4.511423587799072, -37.93617248535156],
'descriptor': {shape: [1, 3, 1, 1], dataType: 'float32'},
'constant': true
},
},
'operators': [
{
'name': 'reshape',
'arguments': [{'input': 'originalInput'}, {'newShape': [2, 3, 2, 2]}],
'outputs': 'reshapedInput'
},
{
'name': 'reshape',
'arguments': [{'input': 'originalScale'}, {'newShape': [3]}],
'outputs': 'reshapedScale'
},
{
'name': 'reshape',
'arguments': [{'input': 'originalBias'}, {'newShape': [3]}],
'outputs': 'reshapedBias'
},
{
'name': 'instanceNormalization',
'arguments': [
{'input': 'reshapedInput'}, {
'options': {
'scale': 'reshapedScale',
'bias': 'reshapedBias',
'epsilon': 0.000001,
'layout': 'nchw'
}
}
],
'outputs': 'instanceNormOutput'
}
],
'expectedOutputs': {
'instanceNormOutput': {
'data': [
70.77738189697266, -179.65554809570312, 23.540178298950195,
-46.8565788269043, 119.31526184082031, -22.847837448120117,
-43.782920837402344, -34.6388053894043, -50.821895599365234,
126.01134490966797, -127.71744537353516, -99.2166976928711,
-108.09159851074219, -139.83889770507812, 90.26488494873047,
25.471038818359375, 22.237276077270508, 67.60342407226562,
-107.4271011352539, 35.6320915222168, -186.15142822265625,
90.01669311523438, -15.238543510437012, -40.37141418457031
],
'descriptor': {shape: [2, 3, 2, 2], dataType: 'float32'}
}
}
}
}];
if (navigator.ml) {
tests.forEach((test) => {
webnn_conformance_test(
buildAndExecuteGraph, getInstanceNormPrecisionTolerance, test);
});
} else {
test(() => assert_implements(navigator.ml, 'missing navigator.ml'));
}

@ -21,16 +21,6 @@
// MLOperand instanceNormalization(
// MLOperand input, optional MLInstanceNormalizationOptions options = {});
const getInstanceNormPrecisionTolerance = (graphResources) => {
// according to
// https://github.com/web-platform-tests/wpt/pull/43891#discussion_r1457026316
const toleranceValueDict = {float32: 840, float16: 8400};
const expectedDataType =
getExpectedDataTypeOfSingleOutput(graphResources.expectedOutputs);
return {metricType: 'ULP', value: toleranceValueDict[expectedDataType]};
};
const instanceNormTests = [
{
'name': 'instanceNormalization float32 4D tensor default options',

@ -56,6 +56,48 @@ const reshapeTests = [
}
}
},
{
'name':
'reshape float32 constant tensor to a new shape (reorder all dimensions)',
'graph': {
'inputs': {
'reshapeInput': {
'data': [
-30.0561466217041, 99.56941986083984, 88.04620361328125,
-91.87507629394531, -23.7972354888916, -91.28665161132812,
-63.15204620361328, 12.0669527053833, -96.1172866821289,
-44.77365493774414, -80.08650970458984, -64.43756866455078,
27.64195442199707, -96.86306762695312, 83.6834716796875,
50.599483489990234, -20.18765640258789, -1.3904608488082886,
-96.93603515625, 65.34143829345703, 34.835994720458984,
62.01485824584961, -2.8698415756225586, 27.903749465942383
],
'descriptor': {shape: [2, 3, 4], dataType: 'float32'},
'constant': true
}
},
'operators': [{
'name': 'reshape',
'arguments': [{'input': 'reshapeInput'}, {'newShape': [4, 2, 3]}],
'outputs': 'reshapeOutput'
}],
'expectedOutputs': {
'reshapeOutput': {
'data': [
-30.0561466217041, 99.56941986083984, 88.04620361328125,
-91.87507629394531, -23.7972354888916, -91.28665161132812,
-63.15204620361328, 12.0669527053833, -96.1172866821289,
-44.77365493774414, -80.08650970458984, -64.43756866455078,
27.64195442199707, -96.86306762695312, 83.6834716796875,
50.599483489990234, -20.18765640258789, -1.3904608488082886,
-96.93603515625, 65.34143829345703, 34.835994720458984,
62.01485824584961, -2.8698415756225586, 27.903749465942383
],
'descriptor': {shape: [4, 2, 3], dataType: 'float32'}
}
}
}
},
{
'name': 'reshape float32 tensor to a new shape (reduce dimensions)',
'graph': {

@ -975,6 +975,15 @@ const getConv2dPrecisionTolerance =
return {metricType: 'ULP', value: toleranceValueDict[expectedDataType]};
};
const getInstanceNormPrecisionTolerance = (graphResources) => {
// according to
// https://github.com/web-platform-tests/wpt/pull/43891#discussion_r1457026316
const toleranceValueDict = {float32: 840, float16: 8400};
const expectedDataType =
getExpectedDataTypeOfSingleOutput(graphResources.expectedOutputs);
return {metricType: 'ULP', value: toleranceValueDict[expectedDataType]};
};
const getExpectedDataTypeOfSingleOutput = (expectedOutput) => {
const expectedDescriptor =
expectedOutput[Object.keys(expectedOutput)[0]].descriptor;

@ -1,4 +1,6 @@
This is a testharness.js-based test.
[FAIL] reshape float32 constant tensor to a new shape (reorder all dimensions)
promise_test: Unhandled rejection with value: object "InvalidStateError: Failed to execute 'build' on 'MLGraphBuilder': Context is lost."
[FAIL] reshape (unsqueeze) float32 5D tensor by adding 4th dimension
promise_test: Unhandled rejection with value: object "NotSupportedError: Failed to execute 'build' on 'MLGraphBuilder': Unsupported rank for reshape. It should be between 0 to 5."
Harness: the test ran to completion.

@ -1,4 +1,6 @@
This is a testharness.js-based test.
[FAIL] reshape float32 constant tensor to a new shape (reorder all dimensions)
promise_test: Unhandled rejection with value: object "InvalidStateError: Failed to execute 'build' on 'MLGraphBuilder': Context is lost."
[FAIL] reshape (unsqueeze) float32 5D tensor by adding 4th dimension
promise_test: Unhandled rejection with value: object "NotSupportedError: Failed to execute 'build' on 'MLGraphBuilder': Unsupported rank for reshape. It should be between 0 to 5."
Harness: the test ran to completion.

@ -0,0 +1,5 @@
This is a testharness.js-based test.
[FAIL] reshape float32 constant tensor to a new shape (reorder all dimensions)
promise_test: Unhandled rejection with value: object "InvalidStateError: Failed to execute 'build' on 'MLGraphBuilder': Context is lost."
Harness: the test ran to completion.

@ -0,0 +1,5 @@
This is a testharness.js-based test.
[FAIL] reshape float32 constant tensor to a new shape (reorder all dimensions)
promise_test: Unhandled rejection with value: object "InvalidStateError: Failed to execute 'build' on 'MLGraphBuilder': Context is lost."
Harness: the test ran to completion.

@ -0,0 +1,5 @@
This is a testharness.js-based test.
[FAIL] reshape float32 constant tensor to a new shape (reorder all dimensions)
promise_test: Unhandled rejection with value: object "InvalidStateError: Failed to execute 'build' on 'MLGraphBuilder': Context is lost."
Harness: the test ran to completion.

@ -1,4 +1,6 @@
This is a testharness.js-based test.
[FAIL] reshape float32 constant tensor to a new shape (reorder all dimensions)
promise_test: Unhandled rejection with value: object "InvalidStateError: Failed to execute 'build' on 'MLGraphBuilder': Context is lost."
[FAIL] reshape (unsqueeze) float32 5D tensor by adding 4th dimension
promise_test: Unhandled rejection with value: object "NotSupportedError: Failed to execute 'build' on 'MLGraphBuilder': Unsupported rank for reshape. It should be between 0 to 5."
Harness: the test ran to completion.