sqrt.https.any.js (19553B)
1 // META: title=test WebNN API element-wise sqrt 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 square root of the input tensor, element-wise. 13 // 14 // MLOperand sqrt(MLOperand input); 15 16 17 const getSqrtPrecisionTolerance = (graphResources) => { 18 const toleranceValueDict = {float32: 1, float16: 1}; 19 const expectedDataType = 20 getExpectedDataTypeOfSingleOutput(graphResources.expectedOutputs); 21 return {metricType: 'ULP', value: toleranceValueDict[expectedDataType]}; 22 }; 23 24 const sqrtTests = [ 25 { 26 'name': 'sqrt float32 0D scalar', 27 'graph': { 28 'inputs': { 29 'sqrtInput': { 30 'data': [4.0044636726379395], 31 'descriptor': {shape: [], dataType: 'float32'} 32 } 33 }, 34 'operators': [{ 35 'name': 'sqrt', 36 'arguments': [{'input': 'sqrtInput'}], 37 'outputs': 'sqrtOutput' 38 }], 39 'expectedOutputs': { 40 'sqrtOutput': { 41 'data': [2.001115560531616], 42 'descriptor': {shape: [], dataType: 'float32'} 43 } 44 } 45 } 46 }, 47 { 48 'name': 'sqrt float32 1D constant tensor', 49 'graph': { 50 'inputs': { 51 'sqrtInput': { 52 'data': [ 53 7.256007194519043, 7.786442279815674, 1.3684587478637695, 54 8.05341625213623, 9.131288528442383, 8.52578067779541, 55 4.870553493499756, 7.625959396362305, 2.705026865005493, 56 8.709602355957031, 3.2687935829162598, 4.712882995605469, 57 8.669181823730469, 8.829607009887695, 0.5529024600982666, 58 7.958771228790283, 4.09640645980835, 7.919884204864502, 59 4.424484729766846, 0.09894099831581116, 4.6900248527526855, 60 1.5277378559112549, 5.929779529571533, 6.066471576690674 61 ], 62 'descriptor': {shape: [24], dataType: 'float32'}, 63 'constant': true 64 } 65 }, 66 'operators': [{ 67 'name': 'sqrt', 68 'arguments': [{'input': 'sqrtInput'}], 69 'outputs': 'sqrtOutput' 70 }], 71 'expectedOutputs': { 72 'sqrtOutput': { 73 'data': [ 74 2.693697690963745, 2.790419816970825, 1.1698113679885864, 75 2.8378541469573975, 3.0218021869659424, 2.919893980026245, 76 2.20693302154541, 2.7615139484405518, 1.644696593284607, 77 2.9512035846710205, 1.8079805374145508, 2.170917510986328, 78 2.944347381591797, 2.9714653491973877, 0.7435740828514099, 79 2.821129322052002, 2.023958206176758, 2.8142287731170654, 80 2.1034460067749023, 0.31454887986183167, 2.165646553039551, 81 1.2360169887542725, 2.4351139068603516, 2.4630208015441895 82 ], 83 'descriptor': {shape: [24], dataType: 'float32'} 84 } 85 } 86 } 87 }, 88 { 89 'name': 'sqrt float32 1D tensor', 90 'graph': { 91 'inputs': { 92 'sqrtInput': { 93 'data': [ 94 7.256007194519043, 7.786442279815674, 1.3684587478637695, 95 8.05341625213623, 9.131288528442383, 8.52578067779541, 96 4.870553493499756, 7.625959396362305, 2.705026865005493, 97 8.709602355957031, 3.2687935829162598, 4.712882995605469, 98 8.669181823730469, 8.829607009887695, 0.5529024600982666, 99 7.958771228790283, 4.09640645980835, 7.919884204864502, 100 4.424484729766846, 0.09894099831581116, 4.6900248527526855, 101 1.5277378559112549, 5.929779529571533, 6.066471576690674 102 ], 103 'descriptor': {shape: [24], dataType: 'float32'} 104 } 105 }, 106 'operators': [{ 107 'name': 'sqrt', 108 'arguments': [{'input': 'sqrtInput'}], 109 'outputs': 'sqrtOutput' 110 }], 111 'expectedOutputs': { 112 'sqrtOutput': { 113 'data': [ 114 2.693697690963745, 2.790419816970825, 1.1698113679885864, 115 2.8378541469573975, 3.0218021869659424, 2.919893980026245, 116 2.20693302154541, 2.7615139484405518, 1.644696593284607, 117 2.9512035846710205, 1.8079805374145508, 2.170917510986328, 118 2.944347381591797, 2.9714653491973877, 0.7435740828514099, 119 2.821129322052002, 2.023958206176758, 2.8142287731170654, 120 2.1034460067749023, 0.31454887986183167, 2.165646553039551, 121 1.2360169887542725, 2.4351139068603516, 2.4630208015441895 122 ], 123 'descriptor': {shape: [24], dataType: 'float32'} 124 } 125 } 126 } 127 }, 128 { 129 'name': 'sqrt float32 2D tensor', 130 'graph': { 131 'inputs': { 132 'sqrtInput': { 133 'data': [ 134 7.256007194519043, 7.786442279815674, 1.3684587478637695, 135 8.05341625213623, 9.131288528442383, 8.52578067779541, 136 4.870553493499756, 7.625959396362305, 2.705026865005493, 137 8.709602355957031, 3.2687935829162598, 4.712882995605469, 138 8.669181823730469, 8.829607009887695, 0.5529024600982666, 139 7.958771228790283, 4.09640645980835, 7.919884204864502, 140 4.424484729766846, 0.09894099831581116, 4.6900248527526855, 141 1.5277378559112549, 5.929779529571533, 6.066471576690674 142 ], 143 'descriptor': {shape: [4, 6], dataType: 'float32'} 144 } 145 }, 146 'operators': [{ 147 'name': 'sqrt', 148 'arguments': [{'input': 'sqrtInput'}], 149 'outputs': 'sqrtOutput' 150 }], 151 'expectedOutputs': { 152 'sqrtOutput': { 153 'data': [ 154 2.693697690963745, 2.790419816970825, 1.1698113679885864, 155 2.8378541469573975, 3.0218021869659424, 2.919893980026245, 156 2.20693302154541, 2.7615139484405518, 1.644696593284607, 157 2.9512035846710205, 1.8079805374145508, 2.170917510986328, 158 2.944347381591797, 2.9714653491973877, 0.7435740828514099, 159 2.821129322052002, 2.023958206176758, 2.8142287731170654, 160 2.1034460067749023, 0.31454887986183167, 2.165646553039551, 161 1.2360169887542725, 2.4351139068603516, 2.4630208015441895 162 ], 163 'descriptor': {shape: [4, 6], dataType: 'float32'} 164 } 165 } 166 } 167 }, 168 { 169 'name': 'sqrt float32 3D tensor', 170 'graph': { 171 'inputs': { 172 'sqrtInput': { 173 'data': [ 174 7.256007194519043, 7.786442279815674, 1.3684587478637695, 175 8.05341625213623, 9.131288528442383, 8.52578067779541, 176 4.870553493499756, 7.625959396362305, 2.705026865005493, 177 8.709602355957031, 3.2687935829162598, 4.712882995605469, 178 8.669181823730469, 8.829607009887695, 0.5529024600982666, 179 7.958771228790283, 4.09640645980835, 7.919884204864502, 180 4.424484729766846, 0.09894099831581116, 4.6900248527526855, 181 1.5277378559112549, 5.929779529571533, 6.066471576690674 182 ], 183 'descriptor': {shape: [2, 3, 4], dataType: 'float32'} 184 } 185 }, 186 'operators': [{ 187 'name': 'sqrt', 188 'arguments': [{'input': 'sqrtInput'}], 189 'outputs': 'sqrtOutput' 190 }], 191 'expectedOutputs': { 192 'sqrtOutput': { 193 'data': [ 194 2.693697690963745, 2.790419816970825, 1.1698113679885864, 195 2.8378541469573975, 3.0218021869659424, 2.919893980026245, 196 2.20693302154541, 2.7615139484405518, 1.644696593284607, 197 2.9512035846710205, 1.8079805374145508, 2.170917510986328, 198 2.944347381591797, 2.9714653491973877, 0.7435740828514099, 199 2.821129322052002, 2.023958206176758, 2.8142287731170654, 200 2.1034460067749023, 0.31454887986183167, 2.165646553039551, 201 1.2360169887542725, 2.4351139068603516, 2.4630208015441895 202 ], 203 'descriptor': {shape: [2, 3, 4], dataType: 'float32'} 204 } 205 } 206 } 207 }, 208 { 209 'name': 'sqrt float32 4D tensor', 210 'graph': { 211 'inputs': { 212 'sqrtInput': { 213 'data': [ 214 7.256007194519043, 7.786442279815674, 1.3684587478637695, 215 8.05341625213623, 9.131288528442383, 8.52578067779541, 216 4.870553493499756, 7.625959396362305, 2.705026865005493, 217 8.709602355957031, 3.2687935829162598, 4.712882995605469, 218 8.669181823730469, 8.829607009887695, 0.5529024600982666, 219 7.958771228790283, 4.09640645980835, 7.919884204864502, 220 4.424484729766846, 0.09894099831581116, 4.6900248527526855, 221 1.5277378559112549, 5.929779529571533, 6.066471576690674 222 ], 223 'descriptor': {shape: [2, 2, 2, 3], dataType: 'float32'} 224 } 225 }, 226 'operators': [{ 227 'name': 'sqrt', 228 'arguments': [{'input': 'sqrtInput'}], 229 'outputs': 'sqrtOutput' 230 }], 231 'expectedOutputs': { 232 'sqrtOutput': { 233 'data': [ 234 2.693697690963745, 2.790419816970825, 1.1698113679885864, 235 2.8378541469573975, 3.0218021869659424, 2.919893980026245, 236 2.20693302154541, 2.7615139484405518, 1.644696593284607, 237 2.9512035846710205, 1.8079805374145508, 2.170917510986328, 238 2.944347381591797, 2.9714653491973877, 0.7435740828514099, 239 2.821129322052002, 2.023958206176758, 2.8142287731170654, 240 2.1034460067749023, 0.31454887986183167, 2.165646553039551, 241 1.2360169887542725, 2.4351139068603516, 2.4630208015441895 242 ], 243 'descriptor': {shape: [2, 2, 2, 3], dataType: 'float32'} 244 } 245 } 246 } 247 }, 248 { 249 'name': 'sqrt float32 5D tensor', 250 'graph': { 251 'inputs': { 252 'sqrtInput': { 253 'data': [ 254 7.256007194519043, 7.786442279815674, 1.3684587478637695, 255 8.05341625213623, 9.131288528442383, 8.52578067779541, 256 4.870553493499756, 7.625959396362305, 2.705026865005493, 257 8.709602355957031, 3.2687935829162598, 4.712882995605469, 258 8.669181823730469, 8.829607009887695, 0.5529024600982666, 259 7.958771228790283, 4.09640645980835, 7.919884204864502, 260 4.424484729766846, 0.09894099831581116, 4.6900248527526855, 261 1.5277378559112549, 5.929779529571533, 6.066471576690674 262 ], 263 'descriptor': {shape: [2, 1, 4, 1, 3], dataType: 'float32'} 264 } 265 }, 266 'operators': [{ 267 'name': 'sqrt', 268 'arguments': [{'input': 'sqrtInput'}], 269 'outputs': 'sqrtOutput' 270 }], 271 'expectedOutputs': { 272 'sqrtOutput': { 273 'data': [ 274 2.693697690963745, 2.790419816970825, 1.1698113679885864, 275 2.8378541469573975, 3.0218021869659424, 2.919893980026245, 276 2.20693302154541, 2.7615139484405518, 1.644696593284607, 277 2.9512035846710205, 1.8079805374145508, 2.170917510986328, 278 2.944347381591797, 2.9714653491973877, 0.7435740828514099, 279 2.821129322052002, 2.023958206176758, 2.8142287731170654, 280 2.1034460067749023, 0.31454887986183167, 2.165646553039551, 281 1.2360169887542725, 2.4351139068603516, 2.4630208015441895 282 ], 283 'descriptor': {shape: [2, 1, 4, 1, 3], dataType: 'float32'} 284 } 285 } 286 } 287 }, 288 289 // float16 tests 290 { 291 'name': 'sqrt float16 0D scalar', 292 'graph': { 293 'inputs': { 294 'sqrtInput': { 295 'data': [4.00390625], 296 'descriptor': {shape: [], dataType: 'float16'} 297 } 298 }, 299 'operators': [{ 300 'name': 'sqrt', 301 'arguments': [{'input': 'sqrtInput'}], 302 'outputs': 'sqrtOutput' 303 }], 304 'expectedOutputs': { 305 'sqrtOutput': 306 {'data': [2], 'descriptor': {shape: [], dataType: 'float16'}} 307 } 308 } 309 }, 310 { 311 'name': 'sqrt float16 1D constant tensor', 312 'graph': { 313 'inputs': { 314 'sqrtInput': { 315 'data': [ 316 7.2578125, 7.78515625, 1.3681640625, 8.0546875, 9.1328125, 317 8.5234375, 4.87109375, 7.625, 2.705078125, 8.7109375, 318 3.26953125, 4.7109375, 8.671875, 8.828125, 0.552734375, 319 7.95703125, 4.09765625, 7.91796875, 4.42578125, 0.09893798828125, 320 4.69140625, 1.52734375, 5.9296875, 6.06640625 321 ], 322 'descriptor': {shape: [24], dataType: 'float16'}, 323 'constant': true 324 } 325 }, 326 'operators': [{ 327 'name': 'sqrt', 328 'arguments': [{'input': 'sqrtInput'}], 329 'outputs': 'sqrtOutput' 330 }], 331 'expectedOutputs': { 332 'sqrtOutput': { 333 'data': [ 334 2.693359375, 2.791015625, 1.169921875, 2.837890625, 3.021484375, 335 2.919921875, 2.20703125, 2.76171875, 1.64453125, 2.951171875, 336 1.80859375, 2.169921875, 2.9453125, 2.970703125, 0.74365234375, 337 2.8203125, 2.0234375, 2.814453125, 2.103515625, 0.314453125, 338 2.166015625, 1.236328125, 2.435546875, 2.462890625 339 ], 340 'descriptor': {shape: [24], dataType: 'float16'} 341 } 342 } 343 } 344 }, 345 { 346 'name': 'sqrt float16 1D tensor', 347 'graph': { 348 'inputs': { 349 'sqrtInput': { 350 'data': [ 351 7.2578125, 7.78515625, 1.3681640625, 8.0546875, 9.1328125, 352 8.5234375, 4.87109375, 7.625, 2.705078125, 8.7109375, 353 3.26953125, 4.7109375, 8.671875, 8.828125, 0.552734375, 354 7.95703125, 4.09765625, 7.91796875, 4.42578125, 0.09893798828125, 355 4.69140625, 1.52734375, 5.9296875, 6.06640625 356 ], 357 'descriptor': {shape: [24], dataType: 'float16'} 358 } 359 }, 360 'operators': [{ 361 'name': 'sqrt', 362 'arguments': [{'input': 'sqrtInput'}], 363 'outputs': 'sqrtOutput' 364 }], 365 'expectedOutputs': { 366 'sqrtOutput': { 367 'data': [ 368 2.693359375, 2.791015625, 1.169921875, 2.837890625, 3.021484375, 369 2.919921875, 2.20703125, 2.76171875, 1.64453125, 2.951171875, 370 1.80859375, 2.169921875, 2.9453125, 2.970703125, 0.74365234375, 371 2.8203125, 2.0234375, 2.814453125, 2.103515625, 0.314453125, 372 2.166015625, 1.236328125, 2.435546875, 2.462890625 373 ], 374 'descriptor': {shape: [24], dataType: 'float16'} 375 } 376 } 377 } 378 }, 379 { 380 'name': 'sqrt float16 2D tensor', 381 'graph': { 382 'inputs': { 383 'sqrtInput': { 384 'data': [ 385 7.2578125, 7.78515625, 1.3681640625, 8.0546875, 9.1328125, 386 8.5234375, 4.87109375, 7.625, 2.705078125, 8.7109375, 387 3.26953125, 4.7109375, 8.671875, 8.828125, 0.552734375, 388 7.95703125, 4.09765625, 7.91796875, 4.42578125, 0.09893798828125, 389 4.69140625, 1.52734375, 5.9296875, 6.06640625 390 ], 391 'descriptor': {shape: [4, 6], dataType: 'float16'} 392 } 393 }, 394 'operators': [{ 395 'name': 'sqrt', 396 'arguments': [{'input': 'sqrtInput'}], 397 'outputs': 'sqrtOutput' 398 }], 399 'expectedOutputs': { 400 'sqrtOutput': { 401 'data': [ 402 2.693359375, 2.791015625, 1.169921875, 2.837890625, 3.021484375, 403 2.919921875, 2.20703125, 2.76171875, 1.64453125, 2.951171875, 404 1.80859375, 2.169921875, 2.9453125, 2.970703125, 0.74365234375, 405 2.8203125, 2.0234375, 2.814453125, 2.103515625, 0.314453125, 406 2.166015625, 1.236328125, 2.435546875, 2.462890625 407 ], 408 'descriptor': {shape: [4, 6], dataType: 'float16'} 409 } 410 } 411 } 412 }, 413 { 414 'name': 'sqrt float16 3D tensor', 415 'graph': { 416 'inputs': { 417 'sqrtInput': { 418 'data': [ 419 7.2578125, 7.78515625, 1.3681640625, 8.0546875, 9.1328125, 420 8.5234375, 4.87109375, 7.625, 2.705078125, 8.7109375, 421 3.26953125, 4.7109375, 8.671875, 8.828125, 0.552734375, 422 7.95703125, 4.09765625, 7.91796875, 4.42578125, 0.09893798828125, 423 4.69140625, 1.52734375, 5.9296875, 6.06640625 424 ], 425 'descriptor': {shape: [2, 3, 4], dataType: 'float16'} 426 } 427 }, 428 'operators': [{ 429 'name': 'sqrt', 430 'arguments': [{'input': 'sqrtInput'}], 431 'outputs': 'sqrtOutput' 432 }], 433 'expectedOutputs': { 434 'sqrtOutput': { 435 'data': [ 436 2.693359375, 2.791015625, 1.169921875, 2.837890625, 3.021484375, 437 2.919921875, 2.20703125, 2.76171875, 1.64453125, 2.951171875, 438 1.80859375, 2.169921875, 2.9453125, 2.970703125, 0.74365234375, 439 2.8203125, 2.0234375, 2.814453125, 2.103515625, 0.314453125, 440 2.166015625, 1.236328125, 2.435546875, 2.462890625 441 ], 442 'descriptor': {shape: [2, 3, 4], dataType: 'float16'} 443 } 444 } 445 } 446 }, 447 { 448 'name': 'sqrt float16 4D tensor', 449 'graph': { 450 'inputs': { 451 'sqrtInput': { 452 'data': [ 453 7.2578125, 7.78515625, 1.3681640625, 8.0546875, 9.1328125, 454 8.5234375, 4.87109375, 7.625, 2.705078125, 8.7109375, 455 3.26953125, 4.7109375, 8.671875, 8.828125, 0.552734375, 456 7.95703125, 4.09765625, 7.91796875, 4.42578125, 0.09893798828125, 457 4.69140625, 1.52734375, 5.9296875, 6.06640625 458 ], 459 'descriptor': {shape: [2, 2, 2, 3], dataType: 'float16'} 460 } 461 }, 462 'operators': [{ 463 'name': 'sqrt', 464 'arguments': [{'input': 'sqrtInput'}], 465 'outputs': 'sqrtOutput' 466 }], 467 'expectedOutputs': { 468 'sqrtOutput': { 469 'data': [ 470 2.693359375, 2.791015625, 1.169921875, 2.837890625, 3.021484375, 471 2.919921875, 2.20703125, 2.76171875, 1.64453125, 2.951171875, 472 1.80859375, 2.169921875, 2.9453125, 2.970703125, 0.74365234375, 473 2.8203125, 2.0234375, 2.814453125, 2.103515625, 0.314453125, 474 2.166015625, 1.236328125, 2.435546875, 2.462890625 475 ], 476 'descriptor': {shape: [2, 2, 2, 3], dataType: 'float16'} 477 } 478 } 479 } 480 }, 481 { 482 'name': 'sqrt float16 5D tensor', 483 'graph': { 484 'inputs': { 485 'sqrtInput': { 486 'data': [ 487 7.2578125, 7.78515625, 1.3681640625, 8.0546875, 9.1328125, 488 8.5234375, 4.87109375, 7.625, 2.705078125, 8.7109375, 489 3.26953125, 4.7109375, 8.671875, 8.828125, 0.552734375, 490 7.95703125, 4.09765625, 7.91796875, 4.42578125, 0.09893798828125, 491 4.69140625, 1.52734375, 5.9296875, 6.06640625 492 ], 493 'descriptor': {shape: [2, 1, 4, 1, 3], dataType: 'float16'} 494 } 495 }, 496 'operators': [{ 497 'name': 'sqrt', 498 'arguments': [{'input': 'sqrtInput'}], 499 'outputs': 'sqrtOutput' 500 }], 501 'expectedOutputs': { 502 'sqrtOutput': { 503 'data': [ 504 2.693359375, 2.791015625, 1.169921875, 2.837890625, 3.021484375, 505 2.919921875, 2.20703125, 2.76171875, 1.64453125, 2.951171875, 506 1.80859375, 2.169921875, 2.9453125, 2.970703125, 0.74365234375, 507 2.8203125, 2.0234375, 2.814453125, 2.103515625, 0.314453125, 508 2.166015625, 1.236328125, 2.435546875, 2.462890625 509 ], 510 'descriptor': {shape: [2, 1, 4, 1, 3], dataType: 'float16'} 511 } 512 } 513 } 514 } 515 ]; 516 517 webnn_conformance_test( 518 sqrtTests, buildAndExecuteGraph, getSqrtPrecisionTolerance);