[webnn] support tests with constant operands (#38276)
* [webnn] Support float32 conv2d tests with filter as constant operand.
fixes #38274
* [webnn] Support float32 batchNormalization tests with mean and variance as constant operands.
diff --git a/webnn/resources/test_data/batch_normalization.json b/webnn/resources/test_data/batch_normalization.json
index 429992c..19f414b 100644
--- a/webnn/resources/test_data/batch_normalization.json
+++ b/webnn/resources/test_data/batch_normalization.json
@@ -1,7 +1,7 @@
{
"tests": [
{
- "name": "batchNormalization float32 2D tensor default options",
+ "name": "batchNormalization float32 2D tensor (mean and variance are non-constant) default options",
"inputs": {
"input": {
"shape": [4, 6],
@@ -90,6 +90,97 @@
}
},
{
+ "name": "batchNormalization float32 2D tensor default options",
+ "inputs": {
+ "input": {
+ "shape": [4, 6],
+ "data": [
+ -41.30732967168238,
+ 64.08864045143429,
+ -63.376671841233104,
+ -46.7903680211994,
+ 83.0222726565018,
+ -80.08049213079853,
+ -62.144380499565784,
+ -0.10012771650340824,
+ -40.902161220312074,
+ 56.963061967558474,
+ 37.37249789808206,
+ 57.04647733193906,
+ 82.05679802728304,
+ -86.11642850667623,
+ 76.88317547014259,
+ 97.03362133579051,
+ -21.35103988063949,
+ -96.93823797160586,
+ -9.359310262561493,
+ 80.2082435478892,
+ -85.36802529512583,
+ 62.35185227095363,
+ -68.4724340974042,
+ -12.107167478351585
+ ],
+ "type": "float32"
+ },
+ "mean": {
+ "shape": [6],
+ "data": [
+ -7.814267643537363,
+ -95.6412954353084,
+ 38.154405424391285,
+ -55.9520351570133,
+ -87.8650062484926,
+ -41.636455304505525
+ ],
+ "type": "float32",
+ "constant": true
+ },
+ "variance": {
+ "shape": [6],
+ "data": [
+ 60.311864286118144,
+ 26.432604504927436,
+ 53.275636661772396,
+ 40.146120500534074,
+ 59.41098289103077,
+ 35.99981828466932
+ ],
+ "type": "float32",
+ "constant": true
+ }
+ },
+ "expected": {
+ "shape": [4, 6],
+ "data": [
+ -4.312741756439209,
+ 31.068212509155273,
+ -13.910240173339844,
+ 1.4459478855133057,
+ 22.170541763305664,
+ -6.407354354858398,
+ -6.995829105377197,
+ 18.583200454711914,
+ -10.831125259399414,
+ 17.820920944213867,
+ 16.2480411529541,
+ 16.447195053100586,
+ 11.57226848602295,
+ 1.8526301383972168,
+ 5.306026458740234,
+ 24.145092010498047,
+ 8.629376411437988,
+ -9.216986656188965,
+ -0.1989477425813675,
+ 34.203548431396484,
+ -16.923160552978516,
+ 18.671411514282227,
+ 2.5159497261047363,
+ 4.921559810638428
+ ],
+ "type": "float32"
+ }
+ },
+ {
"name": "batchNormalization float32 3D tensor default options",
"inputs": {
"input": {
@@ -129,7 +220,8 @@
63.13715247590298,
-61.6298331266107
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
},
"variance": {
"shape": [3],
@@ -138,7 +230,8 @@
41.847234441124506,
16.128282835364406
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"expected": {
@@ -212,7 +305,8 @@
99.36075401019252,
-96.14736495234415
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
},
"variance": {
"shape": [3],
@@ -221,7 +315,8 @@
86.36218998771075,
73.88454888724165
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"expected": {
@@ -293,14 +388,16 @@
"data": [
35.407824570198215
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
},
"variance": {
"shape": [1],
"data": [
40.931099315280846
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"expected": {
@@ -374,7 +471,8 @@
99.36075401019252,
-96.14736495234415
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
},
"variance": {
"shape": [3],
@@ -383,7 +481,8 @@
86.36218998771075,
73.88454888724165
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"options": {
@@ -460,7 +559,8 @@
99.36075401019252,
-96.14736495234415
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
},
"variance": {
"shape": [3],
@@ -469,7 +569,8 @@
86.36218998771075,
73.88454888724165
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"options": {
@@ -546,7 +647,8 @@
99.36075401019252,
-96.14736495234415
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
},
"variance": {
"shape": [3],
@@ -555,7 +657,8 @@
86.36218998771075,
73.88454888724165
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"options": {
@@ -566,7 +669,8 @@
-71.00757080938772,
-5.569730877611249
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"expected": {
@@ -640,7 +744,8 @@
99.36075401019252,
-96.14736495234415
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
},
"variance": {
"shape": [3],
@@ -649,7 +754,8 @@
86.36218998771075,
73.88454888724165
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"options": {
@@ -660,7 +766,8 @@
75.28591851497856,
-84.57243127763437
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"expected": {
@@ -734,7 +841,8 @@
99.36075401019252,
-96.14736495234415
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
},
"variance": {
"shape": [3],
@@ -743,7 +851,8 @@
86.36218998771075,
73.88454888724165
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"options": {
@@ -820,7 +929,8 @@
99.36075401019252,
-96.14736495234415
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
},
"variance": {
"shape": [3],
@@ -829,7 +939,8 @@
86.36218998771075,
73.88454888724165
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"options": {
@@ -906,7 +1017,8 @@
99.36075401019252,
-96.14736495234415
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
},
"variance": {
"shape": [3],
@@ -915,7 +1027,8 @@
86.36218998771075,
73.88454888724165
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"options": {
diff --git a/webnn/resources/test_data/conv2d.json b/webnn/resources/test_data/conv2d.json
index a4b97d3..ca7dc74 100644
--- a/webnn/resources/test_data/conv2d.json
+++ b/webnn/resources/test_data/conv2d.json
@@ -1,7 +1,7 @@
{
"tests": [
{
- "name": "conv2d float32 4D input and filter tensors default options",
+ "name": "conv2d float32 4D input and filter(non-constant) tensors default options",
"inputs": {
"input": {
"shape": [1, 1, 5, 5],
@@ -68,6 +68,74 @@
}
},
{
+ "name": "conv2d float32 4D input and filter tensors default options",
+ "inputs": {
+ "input": {
+ "shape": [1, 1, 5, 5],
+ "data": [
+ -36.64786517159748,
+ -11.288432057442094,
+ -76.58016110067685,
+ 27.405805828795508,
+ -80.47998789027426,
+ 76.04182611584571,
+ 72.35851634441039,
+ 96.98349851260227,
+ -27.84947185731808,
+ 50.58418221559799,
+ -32.43859311335213,
+ 23.48380170680882,
+ -85.38685191828938,
+ -73.94300271939952,
+ -15.851227160619004,
+ 31.57834732127077,
+ 0.42632253229469086,
+ -49.37459452307884,
+ -38.32858663836096,
+ 95.58856379258197,
+ -38.56091554697314,
+ -79.08006476009297,
+ -24.02026158435868,
+ 81.30008164674476,
+ 16.073515803100065
+ ],
+ "type": "float32"
+ },
+ "filter": {
+ "shape": [1, 1, 3, 3],
+ "data": [
+ -38.07863707159655,
+ 54.79507725238136,
+ -88.91290358136841,
+ 71.58386890759388,
+ -97.45390798644782,
+ -69.46620941454853,
+ 72.25696967588178,
+ 91.71009194525874,
+ -45.17980970346187
+ ],
+ "type": "float32",
+ "constant": true
+ }
+ },
+ "expected": {
+ "name": "output",
+ "shape": [1, 1, 3, 3],
+ "data": [
+ 2908.13525390625,
+ -11333.4560546875,
+ 5481.1240234375,
+ -1681.341796875,
+ 17408.30078125,
+ -18923.2109375,
+ 6809.322265625,
+ -3083.824951171875,
+ -836.0403442382812
+ ],
+ "type": "float32"
+ }
+ },
+ {
"name": "conv2d float32 4D input and filter tensors options.padding",
"inputs": {
"input": {
@@ -114,7 +182,8 @@
91.71009194525874,
-45.17980970346187
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"options": {
@@ -200,7 +269,8 @@
91.71009194525874,
-45.17980970346187
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"options": {
@@ -265,7 +335,8 @@
91.71009194525874,
-45.17980970346187
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"options": {
@@ -327,7 +398,8 @@
91.71009194525874,
-45.17980970346187
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"options": {
@@ -405,7 +477,8 @@
91.71009194525874,
-45.17980970346187
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"options": {
@@ -462,7 +535,8 @@
91.71009194525874,
-45.17980970346187
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"options": {
@@ -529,7 +603,8 @@
91.71009194525874,
-45.17980970346187
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"options": {
@@ -600,7 +675,8 @@
91.71009194525874,
-45.17980970346187
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"options": {
@@ -670,7 +746,8 @@
88.16357174255782,
27.032330434044923
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"options": {
@@ -731,7 +808,8 @@
67.67284398422174,
-70.88191146932456
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"options": {
@@ -812,7 +890,8 @@
67.67284398422174,
-70.88191146932456
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"options": {
@@ -893,7 +972,8 @@
67.67284398422174,
-70.88191146932456
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"options": {
@@ -974,7 +1054,8 @@
-25.359619829289443,
-70.88191146932456
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"options": {
@@ -1055,7 +1136,8 @@
67.67284398422174,
-70.88191146932456
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"options": {
@@ -1136,7 +1218,8 @@
-25.359619829289443,
-70.88191146932456
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"options": {
@@ -1217,7 +1300,8 @@
67.67284398422174,
-70.88191146932456
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"options": {
@@ -1299,7 +1383,8 @@
-25.359619829289443,
-70.88191146932456
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"options": {
@@ -1381,7 +1466,8 @@
67.67284398422174,
-70.88191146932456
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"options": {
@@ -1463,7 +1549,8 @@
-25.359619829289443,
-70.88191146932456
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"options": {
@@ -1545,7 +1632,8 @@
67.67284398422174,
-70.88191146932456
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"options": {
@@ -1634,7 +1722,8 @@
67.67284398422174,
-70.88191146932456
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"options": {
@@ -1715,7 +1804,8 @@
67.67284398422174,
-70.88191146932456
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"options": {
@@ -1832,7 +1922,8 @@
4.087872503607585,
-16.408531598421618
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"options": {
@@ -1925,7 +2016,8 @@
69.12384362305653,
-61.16795516567643
],
- "type": "float32"
+ "type": "float32",
+ "constant": true
}
},
"options": {
diff --git a/webnn/resources/utils.js b/webnn/resources/utils.js
index 4c7b6bc..a7d6782 100644
--- a/webnn/resources/utils.js
+++ b/webnn/resources/utils.js
@@ -445,8 +445,13 @@
let inputOperands = [];
const inputOperandNameArray = Object.keys(resources.inputs);
inputOperandNameArray.forEach(inputOperandName => {
- const inputOperand = createSingleInputOperand(builder, resources, inputOperandName);
- inputOperands.push(inputOperand);
+ let operand;
+ if (resources.inputs[inputOperandName].hasOwnProperty('constant') && resources.inputs[inputOperandName]['constant']) {
+ operand = createConstantOperand(builder, resources.inputs[inputOperandName]);
+ } else {
+ operand = createSingleInputOperand(builder, resources, inputOperandName);
+ }
+ inputOperands.push(operand);
});
return inputOperands;
};
@@ -498,12 +503,16 @@
if (Array.isArray(resources.inputs)) {
// the inputs of concat() is a sequence
for (let subInput of resources.inputs) {
- inputs[subInput.name] = new TypedArrayDict[subInput.type](subInput.data);
+ if (!subInput.hasOwnProperty('constant') || !subInput.constant) {
+ inputs[subInput.name] = new TypedArrayDict[subInput.type](subInput.data);
+ }
}
} else {
for (let inputName in resources.inputs) {
const subTestByName = resources.inputs[inputName];
- inputs[inputName] = new TypedArrayDict[subTestByName.type](subTestByName.data);
+ if (!subTestByName.hasOwnProperty('constant') || !subTestByName.constant) {
+ inputs[inputName] = new TypedArrayDict[subTestByName.type](subTestByName.data);
+ }
}
}
let outputs = {};