gatherElements.https.any.js (12695B)
1 // META: title=test WebNN API gatherElements operation 2 // META: global=window 3 // META: variant=?cpu 4 // META: variant=?gpu 5 // META: variant=?npu 6 // META: script=../resources/utils.js 7 // META: timeout=long 8 9 'use strict'; 10 11 // https://www.w3.org/TR/webnn/#api-mlgraphbuilder-gatherElements 12 // Gather values of the input tensor along an axis according to the indices. 13 // 14 // dictionary MLGatherOptions { 15 // [EnforceRange] unsigned long axis = 0; 16 // }; 17 // 18 // MLOperand gatherElements( 19 // MLOperand input, MLOperand indices, 20 // optional MLGatherOptions options = {}); 21 22 const gatherElementsTests = [ 23 { 24 'name': 'gatherElements float32 2D input and uint32 indices options.axis=1', 25 'graph': { 26 'inputs': { 27 'gatherElementsInput': { 28 'data': [ 29 -66.05901336669922, -68.9197006225586, -77.02045440673828, 30 -26.158037185668945, 89.0337142944336, -45.89653396606445, 31 43.84803771972656, 48.81806945800781, 51.79948425292969 32 ], 33 'descriptor': {shape: [3, 3], dataType: 'float32'} 34 }, 35 'gatherElementsIndices': { 36 'data': [1, 0, 2, 2, 1, 0], 37 'descriptor': {shape: [3, 2], dataType: 'uint32'}, 38 'constant': true 39 } 40 }, 41 'operators': [{ 42 'name': 'gatherElements', 43 'arguments': [ 44 {'input': 'gatherElementsInput'}, 45 {'indices': 'gatherElementsIndices'}, {'options': {'axis': 1}} 46 ], 47 'outputs': 'gatherElementsOutput' 48 }], 49 'expectedOutputs': { 50 'gatherElementsOutput': { 51 'data': [ 52 -68.9197006225586, -66.05901336669922, -45.89653396606445, 53 -45.89653396606445, 48.81806945800781, 43.84803771972656 54 ], 55 'descriptor': {shape: [3, 2], dataType: 'float32'} 56 } 57 } 58 } 59 }, 60 { 61 'name': 'gatherElements float32 2D input and int32 indices options.axis=1', 62 'graph': { 63 'inputs': { 64 'gatherElementsInput': { 65 'data': [ 66 -66.05901336669922, -68.9197006225586, -77.02045440673828, 67 -26.158037185668945, 89.0337142944336, -45.89653396606445, 68 43.84803771972656, 48.81806945800781, 51.79948425292969 69 ], 70 'descriptor': {shape: [3, 3], dataType: 'float32'} 71 }, 72 'gatherElementsIndices': { 73 'data': [1, 0, 2, 2, 1, 0], 74 'descriptor': {shape: [3, 2], dataType: 'int32'}, 75 'constant': true 76 } 77 }, 78 'operators': [{ 79 'name': 'gatherElements', 80 'arguments': [ 81 {'input': 'gatherElementsInput'}, 82 {'indices': 'gatherElementsIndices'}, {'options': {'axis': 1}} 83 ], 84 'outputs': 'gatherElementsOutput' 85 }], 86 'expectedOutputs': { 87 'gatherElementsOutput': { 88 'data': [ 89 -68.9197006225586, -66.05901336669922, -45.89653396606445, 90 -45.89653396606445, 48.81806945800781, 43.84803771972656 91 ], 92 'descriptor': {shape: [3, 2], dataType: 'float32'} 93 } 94 } 95 } 96 }, 97 { 98 'name': 'gatherElements float32 2D input and int32 indices options.axis=0', 99 'graph': { 100 'inputs': { 101 'gatherElementsInput': { 102 'data': [ 103 -66.05901336669922, -68.9197006225586, -77.02045440673828, 104 -26.158037185668945, 89.0337142944336, -45.89653396606445, 105 43.84803771972656, 48.81806945800781, 51.79948425292969 106 ], 107 'descriptor': {shape: [3, 3], dataType: 'float32'} 108 }, 109 'gatherElementsIndices': { 110 'data': [1, 0, 2, 2, 1, 0], 111 'descriptor': {shape: [2, 3], dataType: 'int32'}, 112 'constant': true 113 } 114 }, 115 'operators': [{ 116 'name': 'gatherElements', 117 'arguments': [ 118 {'input': 'gatherElementsInput'}, 119 {'indices': 'gatherElementsIndices'}, {'options': {'axis': 0}} 120 ], 121 'outputs': 'gatherElementsOutput' 122 }], 123 'expectedOutputs': { 124 'gatherElementsOutput': { 125 'data': [ 126 -26.158037185668945, -68.9197006225586, 51.79948425292969, 127 43.84803771972656, 89.0337142944336, -77.02045440673828 128 ], 129 'descriptor': {shape: [2, 3], dataType: 'float32'} 130 } 131 } 132 } 133 }, 134 { 135 'name': 'gatherElements float32 3D input and int32 indices options.axis=0', 136 'graph': { 137 'inputs': { 138 'gatherElementsInput': { 139 'data': [ 140 -66.05901336669922, -68.9197006225586, -77.02045440673828, 141 -26.158037185668945, 89.0337142944336, -45.89653396606445, 142 43.84803771972656, 48.81806945800781 143 ], 144 'descriptor': {shape: [2, 2, 2], dataType: 'float32'} 145 }, 146 'gatherElementsIndices': { 147 'data': [1, 0, 0, 1], 148 'descriptor': {shape: [1, 2, 2], dataType: 'int32'}, 149 'constant': true 150 } 151 }, 152 'operators': [{ 153 'name': 'gatherElements', 154 'arguments': [ 155 {'input': 'gatherElementsInput'}, {'indices': 'gatherElementsIndices'} 156 ], 157 'outputs': 'gatherElementsOutput' 158 }], 159 'expectedOutputs': { 160 'gatherElementsOutput': { 161 'data': [ 162 89.0337142944336, -68.9197006225586, -77.02045440673828, 163 48.81806945800781 164 ], 165 'descriptor': {shape: [1, 2, 2], dataType: 'float32'} 166 } 167 } 168 } 169 }, 170 { 171 'name': 'gatherElements float32 3D input and int32 negative indices', 172 'graph': { 173 'inputs': { 174 'gatherElementsInput': { 175 'data': [ 176 -66.05901336669922, -68.9197006225586, -77.02045440673828, 177 -26.158037185668945, 89.0337142944336, -45.89653396606445, 178 43.84803771972656, 48.81806945800781 179 ], 180 'descriptor': {shape: [2, 2, 2], dataType: 'float32'} 181 }, 182 'gatherElementsIndices': { 183 'data': [-1, 0, 0, -1], 184 'descriptor': {shape: [1, 2, 2], dataType: 'int32'}, 185 'constant': true 186 } 187 }, 188 'operators': [{ 189 'name': 'gatherElements', 190 'arguments': [ 191 {'input': 'gatherElementsInput'}, {'indices': 'gatherElementsIndices'} 192 ], 193 'outputs': 'gatherElementsOutput' 194 }], 195 'expectedOutputs': { 196 'gatherElementsOutput': { 197 'data': [ 198 89.0337142944336, -68.9197006225586, -77.02045440673828, 199 48.81806945800781 200 ], 201 'descriptor': {shape: [1, 2, 2], dataType: 'float32'} 202 } 203 } 204 } 205 }, 206 { 207 'name': 'gatherElements float32 1D input and int32 out-of-bounds indices', 208 'graph': { 209 'inputs': { 210 'gatherElementsInput': { 211 'data': [ 212 -26.158037185668945, 89.0337142944336, -45.89653396606445, 213 43.84803771972656, 48.81806945800781, 51.79948425292969 214 ], 215 'descriptor': {shape: [6], dataType: 'float32'} 216 }, 217 'gatherElementsIndices': { 218 'data': [7], 219 'descriptor': {shape: [1], dataType: 'int32'}, 220 'constant': true 221 } 222 }, 223 'operators': [{ 224 'name': 'gatherElements', 225 'arguments': [ 226 {'input': 'gatherElementsInput'}, {'indices': 'gatherElementsIndices'} 227 ], 228 'outputs': 'gatherElementsOutput' 229 }], 230 'expectedOutputs': { 231 'gatherElementsOutput': { 232 'data': [51.79948425292969], 233 'descriptor': {shape: [1], dataType: 'float32'} 234 } 235 } 236 } 237 }, 238 239 // float16 tests 240 { 241 'name': 'gatherElements float16 2D input and uint32 indices options.axis=1', 242 'graph': { 243 'inputs': { 244 'gatherElementsInput': { 245 'data': [ 246 -66.0625, -68.9375, -77, -26.15625, 89.0625, -45.90625, 43.84375, 247 48.8125, 51.8125 248 ], 249 'descriptor': {'shape': [3, 3], 'dataType': 'float16'} 250 }, 251 'gatherElementsIndices': { 252 'data': [1, 0, 2, 2, 1, 0], 253 'descriptor': {'shape': [3, 2], 'dataType': 'uint32'}, 254 'constant': true 255 } 256 }, 257 'operators': [{ 258 'name': 'gatherElements', 259 'arguments': [ 260 {'input': 'gatherElementsInput'}, 261 {'indices': 'gatherElementsIndices'}, {'options': {'axis': 1}} 262 ], 263 'outputs': 'gatherElementsOutput' 264 }], 265 'expectedOutputs': { 266 'gatherElementsOutput': { 267 'data': [-68.9375, -66.0625, -45.90625, -45.90625, 48.8125, 43.84375], 268 'descriptor': {'shape': [3, 2], 'dataType': 'float16'} 269 } 270 } 271 } 272 }, 273 { 274 'name': 'gatherElements float16 2D input and int32 indices options.axis=1', 275 'graph': { 276 'inputs': { 277 'gatherElementsInput': { 278 'data': [ 279 -66.0625, -68.9375, -77, -26.15625, 89.0625, -45.90625, 43.84375, 280 48.8125, 51.8125 281 ], 282 'descriptor': {'shape': [3, 3], 'dataType': 'float16'} 283 }, 284 'gatherElementsIndices': { 285 'data': [1, 0, 2, 2, 1, 0], 286 'descriptor': {'shape': [3, 2], 'dataType': 'int32'}, 287 'constant': true 288 } 289 }, 290 'operators': [{ 291 'name': 'gatherElements', 292 'arguments': [ 293 {'input': 'gatherElementsInput'}, 294 {'indices': 'gatherElementsIndices'}, {'options': {'axis': 1}} 295 ], 296 'outputs': 'gatherElementsOutput' 297 }], 298 'expectedOutputs': { 299 'gatherElementsOutput': { 300 'data': [-68.9375, -66.0625, -45.90625, -45.90625, 48.8125, 43.84375], 301 'descriptor': {'shape': [3, 2], 'dataType': 'float16'} 302 } 303 } 304 } 305 }, 306 { 307 'name': 'gatherElements float16 2D input and int32 indices options.axis=0', 308 'graph': { 309 'inputs': { 310 'gatherElementsInput': { 311 'data': [ 312 -66.0625, -68.9375, -77, -26.15625, 89.0625, -45.90625, 43.84375, 313 48.8125, 51.8125 314 ], 315 'descriptor': {'shape': [3, 3], 'dataType': 'float16'} 316 }, 317 'gatherElementsIndices': { 318 'data': [1, 0, 2, 2, 1, 0], 319 'descriptor': {'shape': [2, 3], 'dataType': 'int32'}, 320 'constant': true 321 } 322 }, 323 'operators': [{ 324 'name': 'gatherElements', 325 'arguments': [ 326 {'input': 'gatherElementsInput'}, 327 {'indices': 'gatherElementsIndices'}, {'options': {'axis': 0}} 328 ], 329 'outputs': 'gatherElementsOutput' 330 }], 331 'expectedOutputs': { 332 'gatherElementsOutput': { 333 'data': [-26.15625, -68.9375, 51.8125, 43.84375, 89.0625, -77], 334 'descriptor': {'shape': [2, 3], 'dataType': 'float16'} 335 } 336 } 337 } 338 }, 339 { 340 'name': 'gatherElements float16 3D input and int32 indices options.axis=0', 341 'graph': { 342 'inputs': { 343 'gatherElementsInput': { 344 'data': [ 345 -66.0625, -68.9375, -77, -26.15625, 89.0625, -45.90625, 43.84375, 346 48.8125 347 ], 348 'descriptor': {'shape': [2, 2, 2], 'dataType': 'float16'} 349 }, 350 'gatherElementsIndices': { 351 'data': [1, 0, 0, 1], 352 'descriptor': {'shape': [1, 2, 2], 'dataType': 'int32'}, 353 'constant': true 354 } 355 }, 356 'operators': [{ 357 'name': 'gatherElements', 358 'arguments': [ 359 {'input': 'gatherElementsInput'}, {'indices': 'gatherElementsIndices'} 360 ], 361 'outputs': 'gatherElementsOutput' 362 }], 363 'expectedOutputs': { 364 'gatherElementsOutput': { 365 'data': [89.0625, -68.9375, -77, 48.8125], 366 'descriptor': {'shape': [1, 2, 2], 'dataType': 'float16'} 367 } 368 } 369 } 370 }, 371 { 372 'name': 'gatherElements float16 3D input and int32 negative indices', 373 'graph': { 374 'inputs': { 375 'gatherElementsInput': { 376 'data': [ 377 -66.0625, -68.9375, -77, -26.15625, 89.0625, -45.90625, 43.84375, 378 48.8125 379 ], 380 'descriptor': {'shape': [2, 2, 2], 'dataType': 'float16'} 381 }, 382 'gatherElementsIndices': { 383 'data': [-1, 0, 0, -1], 384 'descriptor': {'shape': [1, 2, 2], 'dataType': 'int32'}, 385 'constant': true 386 } 387 }, 388 'operators': [{ 389 'name': 'gatherElements', 390 'arguments': [ 391 {'input': 'gatherElementsInput'}, {'indices': 'gatherElementsIndices'} 392 ], 393 'outputs': 'gatherElementsOutput' 394 }], 395 'expectedOutputs': { 396 'gatherElementsOutput': { 397 'data': [89.0625, -68.9375, -77, 48.8125], 398 'descriptor': {'shape': [1, 2, 2], 'dataType': 'float16'} 399 } 400 } 401 } 402 } 403 ]; 404 405 webnn_conformance_test( 406 gatherElementsTests, buildAndExecuteGraph, getZeroULPTolerance);