reciprocal.https.any.js (21694B)
1 // META: title=test WebNN API element-wise reciprocal 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-unary 12 // Compute the reciprocal of the input tensor, element-wise. 13 // 14 // MLOperand reciprocal(MLOperand input); 15 16 17 const getReciprocalPrecisionTolerance = (graphResources) => { 18 const toleranceValueDict = {float32: 2, float16: 2}; 19 const expectedDataType = 20 getExpectedDataTypeOfSingleOutput(graphResources.expectedOutputs); 21 return {metricType: 'ULP', value: toleranceValueDict[expectedDataType]}; 22 }; 23 24 const reciprocalTests = [ 25 { 26 'name': 'reciprocal float32 0D scalar', 27 'graph': { 28 'inputs': { 29 'reciprocalInput': { 30 'data': [-9.36349105834961], 31 'descriptor': {shape: [], dataType: 'float32'} 32 } 33 }, 34 'operators': [{ 35 'name': 'reciprocal', 36 'arguments': [{'input': 'reciprocalInput'}], 37 'outputs': 'reciprocalOutput' 38 }], 39 'expectedOutputs': { 40 'reciprocalOutput': { 41 'data': [-0.10679777711629868], 42 'descriptor': {shape: [], dataType: 'float32'} 43 } 44 } 45 } 46 }, 47 { 48 'name': 'reciprocal float32 1D constant tensor', 49 'graph': { 50 'inputs': { 51 'reciprocalInput': { 52 'data': [ 53 -1.0110862255096436, 4.6215386390686035, 2.417376756668091, 54 -8.934871673583984, -3.3485584259033203, -8.803302764892578, 55 8.826321601867676, 6.572828769683838, 6.639494895935059, 56 2.6842703819274902, -4.627160549163818, -6.873653411865234, 57 -1.1854121685028076, 0.9572811722755432, 0.02132055163383484, 58 0.35645851492881775, 4.827780246734619, 1.3128081560134888, 59 8.246277809143066, -0.2039169818162918, -9.039937019348145, 60 9.942809104919434, -9.540112495422363, -0.08022662252187729 61 ], 62 'descriptor': {shape: [24], dataType: 'float32'}, 63 'constant': true 64 } 65 }, 66 'operators': [{ 67 'name': 'reciprocal', 68 'arguments': [{'input': 'reciprocalInput'}], 69 'outputs': 'reciprocalOutput' 70 }], 71 'expectedOutputs': { 72 'reciprocalOutput': { 73 'data': [ 74 -0.9890353083610535, 0.21637815237045288, 0.4136715531349182, 75 -0.111921027302742, -0.29863595962524414, -0.11359372735023499, 76 0.1132974773645401, 0.1521414965391159, 0.15061387419700623, 77 0.3725407123565674, -0.21611526608467102, -0.14548304677009583, 78 -0.8435884118080139, 1.044625163078308, 46.90310287475586, 79 2.805375576019287, 0.20713452994823456, 0.761725902557373, 80 0.12126683443784714, -4.903956413269043, -0.11062023788690567, 81 0.10057520121335983, -0.10482056438922882, -12.464690208435059 82 ], 83 'descriptor': {shape: [24], dataType: 'float32'} 84 } 85 } 86 } 87 }, 88 { 89 'name': 'reciprocal float32 1D tensor', 90 'graph': { 91 'inputs': { 92 'reciprocalInput': { 93 'data': [ 94 -1.0110862255096436, 4.6215386390686035, 2.417376756668091, 95 -8.934871673583984, -3.3485584259033203, -8.803302764892578, 96 8.826321601867676, 6.572828769683838, 6.639494895935059, 97 2.6842703819274902, -4.627160549163818, -6.873653411865234, 98 -1.1854121685028076, 0.9572811722755432, 0.02132055163383484, 99 0.35645851492881775, 4.827780246734619, 1.3128081560134888, 100 8.246277809143066, -0.2039169818162918, -9.039937019348145, 101 9.942809104919434, -9.540112495422363, -0.08022662252187729 102 ], 103 'descriptor': {shape: [24], dataType: 'float32'} 104 } 105 }, 106 'operators': [{ 107 'name': 'reciprocal', 108 'arguments': [{'input': 'reciprocalInput'}], 109 'outputs': 'reciprocalOutput' 110 }], 111 'expectedOutputs': { 112 'reciprocalOutput': { 113 'data': [ 114 -0.9890353083610535, 0.21637815237045288, 0.4136715531349182, 115 -0.111921027302742, -0.29863595962524414, -0.11359372735023499, 116 0.1132974773645401, 0.1521414965391159, 0.15061387419700623, 117 0.3725407123565674, -0.21611526608467102, -0.14548304677009583, 118 -0.8435884118080139, 1.044625163078308, 46.90310287475586, 119 2.805375576019287, 0.20713452994823456, 0.761725902557373, 120 0.12126683443784714, -4.903956413269043, -0.11062023788690567, 121 0.10057520121335983, -0.10482056438922882, -12.464690208435059 122 ], 123 'descriptor': {shape: [24], dataType: 'float32'} 124 } 125 } 126 } 127 }, 128 { 129 'name': 'reciprocal float32 2D tensor', 130 'graph': { 131 'inputs': { 132 'reciprocalInput': { 133 'data': [ 134 -1.0110862255096436, 4.6215386390686035, 2.417376756668091, 135 -8.934871673583984, -3.3485584259033203, -8.803302764892578, 136 8.826321601867676, 6.572828769683838, 6.639494895935059, 137 2.6842703819274902, -4.627160549163818, -6.873653411865234, 138 -1.1854121685028076, 0.9572811722755432, 0.02132055163383484, 139 0.35645851492881775, 4.827780246734619, 1.3128081560134888, 140 8.246277809143066, -0.2039169818162918, -9.039937019348145, 141 9.942809104919434, -9.540112495422363, -0.08022662252187729 142 ], 143 'descriptor': {shape: [4, 6], dataType: 'float32'} 144 } 145 }, 146 'operators': [{ 147 'name': 'reciprocal', 148 'arguments': [{'input': 'reciprocalInput'}], 149 'outputs': 'reciprocalOutput' 150 }], 151 'expectedOutputs': { 152 'reciprocalOutput': { 153 'data': [ 154 -0.9890353083610535, 0.21637815237045288, 0.4136715531349182, 155 -0.111921027302742, -0.29863595962524414, -0.11359372735023499, 156 0.1132974773645401, 0.1521414965391159, 0.15061387419700623, 157 0.3725407123565674, -0.21611526608467102, -0.14548304677009583, 158 -0.8435884118080139, 1.044625163078308, 46.90310287475586, 159 2.805375576019287, 0.20713452994823456, 0.761725902557373, 160 0.12126683443784714, -4.903956413269043, -0.11062023788690567, 161 0.10057520121335983, -0.10482056438922882, -12.464690208435059 162 ], 163 'descriptor': {shape: [4, 6], dataType: 'float32'} 164 } 165 } 166 } 167 }, 168 { 169 'name': 'reciprocal float32 3D tensor', 170 'graph': { 171 'inputs': { 172 'reciprocalInput': { 173 'data': [ 174 -1.0110862255096436, 4.6215386390686035, 2.417376756668091, 175 -8.934871673583984, -3.3485584259033203, -8.803302764892578, 176 8.826321601867676, 6.572828769683838, 6.639494895935059, 177 2.6842703819274902, -4.627160549163818, -6.873653411865234, 178 -1.1854121685028076, 0.9572811722755432, 0.02132055163383484, 179 0.35645851492881775, 4.827780246734619, 1.3128081560134888, 180 8.246277809143066, -0.2039169818162918, -9.039937019348145, 181 9.942809104919434, -9.540112495422363, -0.08022662252187729 182 ], 183 'descriptor': {shape: [2, 3, 4], dataType: 'float32'} 184 } 185 }, 186 'operators': [{ 187 'name': 'reciprocal', 188 'arguments': [{'input': 'reciprocalInput'}], 189 'outputs': 'reciprocalOutput' 190 }], 191 'expectedOutputs': { 192 'reciprocalOutput': { 193 'data': [ 194 -0.9890353083610535, 0.21637815237045288, 0.4136715531349182, 195 -0.111921027302742, -0.29863595962524414, -0.11359372735023499, 196 0.1132974773645401, 0.1521414965391159, 0.15061387419700623, 197 0.3725407123565674, -0.21611526608467102, -0.14548304677009583, 198 -0.8435884118080139, 1.044625163078308, 46.90310287475586, 199 2.805375576019287, 0.20713452994823456, 0.761725902557373, 200 0.12126683443784714, -4.903956413269043, -0.11062023788690567, 201 0.10057520121335983, -0.10482056438922882, -12.464690208435059 202 ], 203 'descriptor': {shape: [2, 3, 4], dataType: 'float32'} 204 } 205 } 206 } 207 }, 208 { 209 'name': 'reciprocal float32 4D tensor', 210 'graph': { 211 'inputs': { 212 'reciprocalInput': { 213 'data': [ 214 -1.0110862255096436, 4.6215386390686035, 2.417376756668091, 215 -8.934871673583984, -3.3485584259033203, -8.803302764892578, 216 8.826321601867676, 6.572828769683838, 6.639494895935059, 217 2.6842703819274902, -4.627160549163818, -6.873653411865234, 218 -1.1854121685028076, 0.9572811722755432, 0.02132055163383484, 219 0.35645851492881775, 4.827780246734619, 1.3128081560134888, 220 8.246277809143066, -0.2039169818162918, -9.039937019348145, 221 9.942809104919434, -9.540112495422363, -0.08022662252187729 222 ], 223 'descriptor': {shape: [2, 2, 2, 3], dataType: 'float32'} 224 } 225 }, 226 'operators': [{ 227 'name': 'reciprocal', 228 'arguments': [{'input': 'reciprocalInput'}], 229 'outputs': 'reciprocalOutput' 230 }], 231 'expectedOutputs': { 232 'reciprocalOutput': { 233 'data': [ 234 -0.9890353083610535, 0.21637815237045288, 0.4136715531349182, 235 -0.111921027302742, -0.29863595962524414, -0.11359372735023499, 236 0.1132974773645401, 0.1521414965391159, 0.15061387419700623, 237 0.3725407123565674, -0.21611526608467102, -0.14548304677009583, 238 -0.8435884118080139, 1.044625163078308, 46.90310287475586, 239 2.805375576019287, 0.20713452994823456, 0.761725902557373, 240 0.12126683443784714, -4.903956413269043, -0.11062023788690567, 241 0.10057520121335983, -0.10482056438922882, -12.464690208435059 242 ], 243 'descriptor': {shape: [2, 2, 2, 3], dataType: 'float32'} 244 } 245 } 246 } 247 }, 248 { 249 'name': 'reciprocal float32 5D tensor', 250 'graph': { 251 'inputs': { 252 'reciprocalInput': { 253 'data': [ 254 -1.0110862255096436, 4.6215386390686035, 2.417376756668091, 255 -8.934871673583984, -3.3485584259033203, -8.803302764892578, 256 8.826321601867676, 6.572828769683838, 6.639494895935059, 257 2.6842703819274902, -4.627160549163818, -6.873653411865234, 258 -1.1854121685028076, 0.9572811722755432, 0.02132055163383484, 259 0.35645851492881775, 4.827780246734619, 1.3128081560134888, 260 8.246277809143066, -0.2039169818162918, -9.039937019348145, 261 9.942809104919434, -9.540112495422363, -0.08022662252187729 262 ], 263 'descriptor': {shape: [2, 1, 4, 1, 3], dataType: 'float32'} 264 } 265 }, 266 'operators': [{ 267 'name': 'reciprocal', 268 'arguments': [{'input': 'reciprocalInput'}], 269 'outputs': 'reciprocalOutput' 270 }], 271 'expectedOutputs': { 272 'reciprocalOutput': { 273 'data': [ 274 -0.9890353083610535, 0.21637815237045288, 0.4136715531349182, 275 -0.111921027302742, -0.29863595962524414, -0.11359372735023499, 276 0.1132974773645401, 0.1521414965391159, 0.15061387419700623, 277 0.3725407123565674, -0.21611526608467102, -0.14548304677009583, 278 -0.8435884118080139, 1.044625163078308, 46.90310287475586, 279 2.805375576019287, 0.20713452994823456, 0.761725902557373, 280 0.12126683443784714, -4.903956413269043, -0.11062023788690567, 281 0.10057520121335983, -0.10482056438922882, -12.464690208435059 282 ], 283 'descriptor': {shape: [2, 1, 4, 1, 3], dataType: 'float32'} 284 } 285 } 286 } 287 }, 288 289 // float16 tests 290 { 291 'name': 'reciprocal float16 0D scalar', 292 'graph': { 293 'inputs': { 294 'reciprocalInput': { 295 'data': [-9.3671875], 296 'descriptor': {shape: [], dataType: 'float16'} 297 } 298 }, 299 'operators': [{ 300 'name': 'reciprocal', 301 'arguments': [{'input': 'reciprocalInput'}], 302 'outputs': 'reciprocalOutput' 303 }], 304 'expectedOutputs': { 305 'reciprocalOutput': { 306 'data': [-0.10675048828125], 307 'descriptor': {shape: [], dataType: 'float16'} 308 } 309 } 310 } 311 }, 312 { 313 'name': 'reciprocal float16 1D constant tensor', 314 'graph': { 315 'inputs': { 316 'reciprocalInput': { 317 'data': [ 318 -1.0107421875, 4.62109375, 2.41796875, 319 -8.9375, -3.34765625, -8.8046875, 320 8.828125, 6.57421875, 6.640625, 321 2.68359375, -4.62890625, -6.875, 322 -1.185546875, 0.95751953125, 0.0213165283203125, 323 0.3564453125, 4.828125, 1.3125, 324 8.25, -0.203857421875, -9.0390625, 325 9.9453125, -9.5390625, -0.0802001953125 326 ], 327 'descriptor': {shape: [24], dataType: 'float16'}, 328 'constant': true 329 } 330 }, 331 'operators': [{ 332 'name': 'reciprocal', 333 'arguments': [{'input': 'reciprocalInput'}], 334 'outputs': 'reciprocalOutput' 335 }], 336 'expectedOutputs': { 337 'reciprocalOutput': { 338 'data': [ 339 -0.9892578125, 0.2164306640625, 0.41357421875, 340 -0.11187744140625, -0.298828125, -0.11358642578125, 341 0.11328125, 0.152099609375, 0.150634765625, 342 0.37255859375, -0.216064453125, -0.1455078125, 343 -0.84326171875, 1.0439453125, 46.90625, 344 2.8046875, 0.2071533203125, 0.76171875, 345 0.1212158203125, -4.90625, -0.11065673828125, 346 0.10052490234375, -0.1048583984375, -12.46875 347 ], 348 'descriptor': {shape: [24], dataType: 'float16'} 349 } 350 } 351 } 352 }, 353 { 354 'name': 'reciprocal float16 1D tensor', 355 'graph': { 356 'inputs': { 357 'reciprocalInput': { 358 'data': [ 359 -1.0107421875, 4.62109375, 2.41796875, 360 -8.9375, -3.34765625, -8.8046875, 361 8.828125, 6.57421875, 6.640625, 362 2.68359375, -4.62890625, -6.875, 363 -1.185546875, 0.95751953125, 0.0213165283203125, 364 0.3564453125, 4.828125, 1.3125, 365 8.25, -0.203857421875, -9.0390625, 366 9.9453125, -9.5390625, -0.0802001953125 367 ], 368 'descriptor': {shape: [24], dataType: 'float16'} 369 } 370 }, 371 'operators': [{ 372 'name': 'reciprocal', 373 'arguments': [{'input': 'reciprocalInput'}], 374 'outputs': 'reciprocalOutput' 375 }], 376 'expectedOutputs': { 377 'reciprocalOutput': { 378 'data': [ 379 -0.9892578125, 0.2164306640625, 0.41357421875, 380 -0.11187744140625, -0.298828125, -0.11358642578125, 381 0.11328125, 0.152099609375, 0.150634765625, 382 0.37255859375, -0.216064453125, -0.1455078125, 383 -0.84326171875, 1.0439453125, 46.90625, 384 2.8046875, 0.2071533203125, 0.76171875, 385 0.1212158203125, -4.90625, -0.11065673828125, 386 0.10052490234375, -0.1048583984375, -12.46875 387 ], 388 'descriptor': {shape: [24], dataType: 'float16'} 389 } 390 } 391 } 392 }, 393 { 394 'name': 'reciprocal float16 2D tensor', 395 'graph': { 396 'inputs': { 397 'reciprocalInput': { 398 'data': [ 399 -1.0107421875, 4.62109375, 2.41796875, 400 -8.9375, -3.34765625, -8.8046875, 401 8.828125, 6.57421875, 6.640625, 402 2.68359375, -4.62890625, -6.875, 403 -1.185546875, 0.95751953125, 0.0213165283203125, 404 0.3564453125, 4.828125, 1.3125, 405 8.25, -0.203857421875, -9.0390625, 406 9.9453125, -9.5390625, -0.0802001953125 407 ], 408 'descriptor': {shape: [4, 6], dataType: 'float16'} 409 } 410 }, 411 'operators': [{ 412 'name': 'reciprocal', 413 'arguments': [{'input': 'reciprocalInput'}], 414 'outputs': 'reciprocalOutput' 415 }], 416 'expectedOutputs': { 417 'reciprocalOutput': { 418 'data': [ 419 -0.9892578125, 0.2164306640625, 0.41357421875, 420 -0.11187744140625, -0.298828125, -0.11358642578125, 421 0.11328125, 0.152099609375, 0.150634765625, 422 0.37255859375, -0.216064453125, -0.1455078125, 423 -0.84326171875, 1.0439453125, 46.90625, 424 2.8046875, 0.2071533203125, 0.76171875, 425 0.1212158203125, -4.90625, -0.11065673828125, 426 0.10052490234375, -0.1048583984375, -12.46875 427 ], 428 'descriptor': {shape: [4, 6], dataType: 'float16'} 429 } 430 } 431 } 432 }, 433 { 434 'name': 'reciprocal float16 3D tensor', 435 'graph': { 436 'inputs': { 437 'reciprocalInput': { 438 'data': [ 439 -1.0107421875, 4.62109375, 2.41796875, 440 -8.9375, -3.34765625, -8.8046875, 441 8.828125, 6.57421875, 6.640625, 442 2.68359375, -4.62890625, -6.875, 443 -1.185546875, 0.95751953125, 0.0213165283203125, 444 0.3564453125, 4.828125, 1.3125, 445 8.25, -0.203857421875, -9.0390625, 446 9.9453125, -9.5390625, -0.0802001953125 447 ], 448 'descriptor': {shape: [2, 3, 4], dataType: 'float16'} 449 } 450 }, 451 'operators': [{ 452 'name': 'reciprocal', 453 'arguments': [{'input': 'reciprocalInput'}], 454 'outputs': 'reciprocalOutput' 455 }], 456 'expectedOutputs': { 457 'reciprocalOutput': { 458 'data': [ 459 -0.9892578125, 0.2164306640625, 0.41357421875, 460 -0.11187744140625, -0.298828125, -0.11358642578125, 461 0.11328125, 0.152099609375, 0.150634765625, 462 0.37255859375, -0.216064453125, -0.1455078125, 463 -0.84326171875, 1.0439453125, 46.90625, 464 2.8046875, 0.2071533203125, 0.76171875, 465 0.1212158203125, -4.90625, -0.11065673828125, 466 0.10052490234375, -0.1048583984375, -12.46875 467 ], 468 'descriptor': {shape: [2, 3, 4], dataType: 'float16'} 469 } 470 } 471 } 472 }, 473 { 474 'name': 'reciprocal float16 4D tensor', 475 'graph': { 476 'inputs': { 477 'reciprocalInput': { 478 'data': [ 479 -1.0107421875, 4.62109375, 2.41796875, 480 -8.9375, -3.34765625, -8.8046875, 481 8.828125, 6.57421875, 6.640625, 482 2.68359375, -4.62890625, -6.875, 483 -1.185546875, 0.95751953125, 0.0213165283203125, 484 0.3564453125, 4.828125, 1.3125, 485 8.25, -0.203857421875, -9.0390625, 486 9.9453125, -9.5390625, -0.0802001953125 487 ], 488 'descriptor': {shape: [2, 2, 2, 3], dataType: 'float16'} 489 } 490 }, 491 'operators': [{ 492 'name': 'reciprocal', 493 'arguments': [{'input': 'reciprocalInput'}], 494 'outputs': 'reciprocalOutput' 495 }], 496 'expectedOutputs': { 497 'reciprocalOutput': { 498 'data': [ 499 -0.9892578125, 0.2164306640625, 0.41357421875, 500 -0.11187744140625, -0.298828125, -0.11358642578125, 501 0.11328125, 0.152099609375, 0.150634765625, 502 0.37255859375, -0.216064453125, -0.1455078125, 503 -0.84326171875, 1.0439453125, 46.90625, 504 2.8046875, 0.2071533203125, 0.76171875, 505 0.1212158203125, -4.90625, -0.11065673828125, 506 0.10052490234375, -0.1048583984375, -12.46875 507 ], 508 'descriptor': {shape: [2, 2, 2, 3], dataType: 'float16'} 509 } 510 } 511 } 512 }, 513 { 514 'name': 'reciprocal float16 5D tensor', 515 'graph': { 516 'inputs': { 517 'reciprocalInput': { 518 'data': [ 519 -1.0107421875, 4.62109375, 2.41796875, 520 -8.9375, -3.34765625, -8.8046875, 521 8.828125, 6.57421875, 6.640625, 522 2.68359375, -4.62890625, -6.875, 523 -1.185546875, 0.95751953125, 0.0213165283203125, 524 0.3564453125, 4.828125, 1.3125, 525 8.25, -0.203857421875, -9.0390625, 526 9.9453125, -9.5390625, -0.0802001953125 527 ], 528 'descriptor': {shape: [2, 1, 4, 1, 3], dataType: 'float16'} 529 } 530 }, 531 'operators': [{ 532 'name': 'reciprocal', 533 'arguments': [{'input': 'reciprocalInput'}], 534 'outputs': 'reciprocalOutput' 535 }], 536 'expectedOutputs': { 537 'reciprocalOutput': { 538 'data': [ 539 -0.9892578125, 0.2164306640625, 0.41357421875, 540 -0.11187744140625, -0.298828125, -0.11358642578125, 541 0.11328125, 0.152099609375, 0.150634765625, 542 0.37255859375, -0.216064453125, -0.1455078125, 543 -0.84326171875, 1.0439453125, 46.90625, 544 2.8046875, 0.2071533203125, 0.76171875, 545 0.1212158203125, -4.90625, -0.11065673828125, 546 0.10052490234375, -0.1048583984375, -12.46875 547 ], 548 'descriptor': {shape: [2, 1, 4, 1, 3], dataType: 'float16'} 549 } 550 } 551 } 552 } 553 ]; 554 555 webnn_conformance_test( 556 reciprocalTests, buildAndExecuteGraph, getReciprocalPrecisionTolerance);