cos.https.any.js (19866B)
1 // META: title=test WebNN API element-wise cos 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 cosine of the input tensor, element-wise. 13 // 14 // MLOperand cos(MLOperand input); 15 16 17 const getCosPrecisionTolerance = (graphResources) => { 18 // Use the float accuracy for WGSL for float16: 19 // https://gpuweb.github.io/gpuweb/wgsl/#concrete-float-accuracy 20 const toleranceValueDict = {float32: 2 ** -10, float16: 2 ** -7}; 21 const expectedDataType = 22 getExpectedDataTypeOfSingleOutput(graphResources.expectedOutputs); 23 return {metricType: 'ATOL', value: toleranceValueDict[expectedDataType]}; 24 }; 25 26 const cosTests = [ 27 { 28 'name': 'cos float32 0D scalar', 29 'graph': { 30 'inputs': { 31 'cosInput': { 32 'data': [-2.4009041149673322], 33 'descriptor': {shape: [], dataType: 'float32'} 34 } 35 }, 36 'operators': [{ 37 'name': 'cos', 38 'arguments': [{'input': 'cosInput'}], 39 'outputs': 'cosOutput' 40 }], 41 'expectedOutputs': { 42 'cosOutput': { 43 'data': [-0.7380040884017944], 44 'descriptor': {shape: [], dataType: 'float32'} 45 } 46 } 47 } 48 }, 49 { 50 'name': 'cos float32 1D constant tensor', 51 'graph': { 52 'inputs': { 53 'cosInput': { 54 'data': [ 55 -2.4009041149673322, -1.1175411065788339, -0.429725885470269, 56 1.808886416821629, 0.04802187281199366, 1.0653891722940827, 57 -2.3977027575912615, -1.3261746248294415, -1.3153583208192998, 58 -1.1028532187407407, 1.8888955433741899, -1.4786912441727864, 59 1.8273826281967303, -2.851855166821629, -1.6838571072098958, 60 -0.743461799684809, -2.51020827298138, 1.9872438431418402, 61 1.8580675760061922, -0.7797077497373408, 1.7245051702074363, 62 2.0227612337161602, 1.1678468704855902, 2.4311166127101274 63 ], 64 'descriptor': {shape: [24], dataType: 'float32'}, 65 'constant': true 66 } 67 }, 68 'operators': [{ 69 'name': 'cos', 70 'arguments': [{'input': 'cosInput'}], 71 'outputs': 'cosOutput' 72 }], 73 'expectedOutputs': { 74 'cosOutput': { 75 'data': [ 76 -0.7380040884017944, 0.43789437413215637, 0.9090799689292908, 77 -0.23584702610969543, 0.9988471865653992, 0.48416373133659363, 78 -0.7358400821685791, 0.24218930304050446, 0.25266921520233154, 79 0.4510514736175537, -0.31276169419288635, 0.09197491407394409, 80 -0.2537800967693329, -0.9583188891410828, -0.11282006651163101, 81 0.736129879951477, -0.80721116065979, -0.4045141637325287, 82 -0.283336341381073, 0.7111190557479858, -0.1531042903661728, 83 -0.43673399090766907, 0.39213326573371887, -0.7580515146255493 84 ], 85 'descriptor': {shape: [24], dataType: 'float32'} 86 } 87 } 88 } 89 }, 90 { 91 'name': 'cos float32 1D tensor', 92 'graph': { 93 'inputs': { 94 'cosInput': { 95 'data': [ 96 -2.4009041149673322, -1.1175411065788339, -0.429725885470269, 97 1.808886416821629, 0.04802187281199366, 1.0653891722940827, 98 -2.3977027575912615, -1.3261746248294415, -1.3153583208192998, 99 -1.1028532187407407, 1.8888955433741899, -1.4786912441727864, 100 1.8273826281967303, -2.851855166821629, -1.6838571072098958, 101 -0.743461799684809, -2.51020827298138, 1.9872438431418402, 102 1.8580675760061922, -0.7797077497373408, 1.7245051702074363, 103 2.0227612337161602, 1.1678468704855902, 2.4311166127101274 104 ], 105 'descriptor': {shape: [24], dataType: 'float32'} 106 } 107 }, 108 'operators': [{ 109 'name': 'cos', 110 'arguments': [{'input': 'cosInput'}], 111 'outputs': 'cosOutput' 112 }], 113 'expectedOutputs': { 114 'cosOutput': { 115 'data': [ 116 -0.7380040884017944, 0.43789437413215637, 0.9090799689292908, 117 -0.23584702610969543, 0.9988471865653992, 0.48416373133659363, 118 -0.7358400821685791, 0.24218930304050446, 0.25266921520233154, 119 0.4510514736175537, -0.31276169419288635, 0.09197491407394409, 120 -0.2537800967693329, -0.9583188891410828, -0.11282006651163101, 121 0.736129879951477, -0.80721116065979, -0.4045141637325287, 122 -0.283336341381073, 0.7111190557479858, -0.1531042903661728, 123 -0.43673399090766907, 0.39213326573371887, -0.7580515146255493 124 ], 125 'descriptor': {shape: [24], dataType: 'float32'} 126 } 127 } 128 } 129 }, 130 { 131 'name': 'cos float32 2D tensor', 132 'graph': { 133 'inputs': { 134 'cosInput': { 135 'data': [ 136 -2.4009041149673322, -1.1175411065788339, -0.429725885470269, 137 1.808886416821629, 0.04802187281199366, 1.0653891722940827, 138 -2.3977027575912615, -1.3261746248294415, -1.3153583208192998, 139 -1.1028532187407407, 1.8888955433741899, -1.4786912441727864, 140 1.8273826281967303, -2.851855166821629, -1.6838571072098958, 141 -0.743461799684809, -2.51020827298138, 1.9872438431418402, 142 1.8580675760061922, -0.7797077497373408, 1.7245051702074363, 143 2.0227612337161602, 1.1678468704855902, 2.4311166127101274 144 ], 145 'descriptor': {shape: [4, 6], dataType: 'float32'} 146 } 147 }, 148 'operators': [{ 149 'name': 'cos', 150 'arguments': [{'input': 'cosInput'}], 151 'outputs': 'cosOutput' 152 }], 153 'expectedOutputs': { 154 'cosOutput': { 155 'data': [ 156 -0.7380040884017944, 0.43789437413215637, 0.9090799689292908, 157 -0.23584702610969543, 0.9988471865653992, 0.48416373133659363, 158 -0.7358400821685791, 0.24218930304050446, 0.25266921520233154, 159 0.4510514736175537, -0.31276169419288635, 0.09197491407394409, 160 -0.2537800967693329, -0.9583188891410828, -0.11282006651163101, 161 0.736129879951477, -0.80721116065979, -0.4045141637325287, 162 -0.283336341381073, 0.7111190557479858, -0.1531042903661728, 163 -0.43673399090766907, 0.39213326573371887, -0.7580515146255493 164 ], 165 'descriptor': {shape: [4, 6], dataType: 'float32'} 166 } 167 } 168 } 169 }, 170 { 171 'name': 'cos float32 3D tensor', 172 'graph': { 173 'inputs': { 174 'cosInput': { 175 'data': [ 176 -2.4009041149673322, -1.1175411065788339, -0.429725885470269, 177 1.808886416821629, 0.04802187281199366, 1.0653891722940827, 178 -2.3977027575912615, -1.3261746248294415, -1.3153583208192998, 179 -1.1028532187407407, 1.8888955433741899, -1.4786912441727864, 180 1.8273826281967303, -2.851855166821629, -1.6838571072098958, 181 -0.743461799684809, -2.51020827298138, 1.9872438431418402, 182 1.8580675760061922, -0.7797077497373408, 1.7245051702074363, 183 2.0227612337161602, 1.1678468704855902, 2.4311166127101274 184 ], 185 'descriptor': {shape: [2, 3, 4], dataType: 'float32'} 186 } 187 }, 188 'operators': [{ 189 'name': 'cos', 190 'arguments': [{'input': 'cosInput'}], 191 'outputs': 'cosOutput' 192 }], 193 'expectedOutputs': { 194 'cosOutput': { 195 'data': [ 196 -0.7380040884017944, 0.43789437413215637, 0.9090799689292908, 197 -0.23584702610969543, 0.9988471865653992, 0.48416373133659363, 198 -0.7358400821685791, 0.24218930304050446, 0.25266921520233154, 199 0.4510514736175537, -0.31276169419288635, 0.09197491407394409, 200 -0.2537800967693329, -0.9583188891410828, -0.11282006651163101, 201 0.736129879951477, -0.80721116065979, -0.4045141637325287, 202 -0.283336341381073, 0.7111190557479858, -0.1531042903661728, 203 -0.43673399090766907, 0.39213326573371887, -0.7580515146255493 204 ], 205 'descriptor': {shape: [2, 3, 4], dataType: 'float32'} 206 } 207 } 208 } 209 }, 210 { 211 'name': 'cos float32 4D tensor', 212 'graph': { 213 'inputs': { 214 'cosInput': { 215 'data': [ 216 -2.4009041149673322, -1.1175411065788339, -0.429725885470269, 217 1.808886416821629, 0.04802187281199366, 1.0653891722940827, 218 -2.3977027575912615, -1.3261746248294415, -1.3153583208192998, 219 -1.1028532187407407, 1.8888955433741899, -1.4786912441727864, 220 1.8273826281967303, -2.851855166821629, -1.6838571072098958, 221 -0.743461799684809, -2.51020827298138, 1.9872438431418402, 222 1.8580675760061922, -0.7797077497373408, 1.7245051702074363, 223 2.0227612337161602, 1.1678468704855902, 2.4311166127101274 224 ], 225 'descriptor': {shape: [2, 2, 2, 3], dataType: 'float32'} 226 } 227 }, 228 'operators': [{ 229 'name': 'cos', 230 'arguments': [{'input': 'cosInput'}], 231 'outputs': 'cosOutput' 232 }], 233 'expectedOutputs': { 234 'cosOutput': { 235 'data': [ 236 -0.7380040884017944, 0.43789437413215637, 0.9090799689292908, 237 -0.23584702610969543, 0.9988471865653992, 0.48416373133659363, 238 -0.7358400821685791, 0.24218930304050446, 0.25266921520233154, 239 0.4510514736175537, -0.31276169419288635, 0.09197491407394409, 240 -0.2537800967693329, -0.9583188891410828, -0.11282006651163101, 241 0.736129879951477, -0.80721116065979, -0.4045141637325287, 242 -0.283336341381073, 0.7111190557479858, -0.1531042903661728, 243 -0.43673399090766907, 0.39213326573371887, -0.7580515146255493 244 ], 245 'descriptor': {shape: [2, 2, 2, 3], dataType: 'float32'} 246 } 247 } 248 } 249 }, 250 { 251 'name': 'cos float32 5D tensor', 252 'graph': { 253 'inputs': { 254 'cosInput': { 255 'data': [ 256 -2.4009041149673322, -1.1175411065788339, -0.429725885470269, 257 1.808886416821629, 0.04802187281199366, 1.0653891722940827, 258 -2.3977027575912615, -1.3261746248294415, -1.3153583208192998, 259 -1.1028532187407407, 1.8888955433741899, -1.4786912441727864, 260 1.8273826281967303, -2.851855166821629, -1.6838571072098958, 261 -0.743461799684809, -2.51020827298138, 1.9872438431418402, 262 1.8580675760061922, -0.7797077497373408, 1.7245051702074363, 263 2.0227612337161602, 1.1678468704855902, 2.4311166127101274 264 ], 265 'descriptor': {shape: [2, 1, 4, 1, 3], dataType: 'float32'} 266 } 267 }, 268 'operators': [{ 269 'name': 'cos', 270 'arguments': [{'input': 'cosInput'}], 271 'outputs': 'cosOutput' 272 }], 273 'expectedOutputs': { 274 'cosOutput': { 275 'data': [ 276 -0.7380040884017944, 0.43789437413215637, 0.9090799689292908, 277 -0.23584702610969543, 0.9988471865653992, 0.48416373133659363, 278 -0.7358400821685791, 0.24218930304050446, 0.25266921520233154, 279 0.4510514736175537, -0.31276169419288635, 0.09197491407394409, 280 -0.2537800967693329, -0.9583188891410828, -0.11282006651163101, 281 0.736129879951477, -0.80721116065979, -0.4045141637325287, 282 -0.283336341381073, 0.7111190557479858, -0.1531042903661728, 283 -0.43673399090766907, 0.39213326573371887, -0.7580515146255493 284 ], 285 'descriptor': {shape: [2, 1, 4, 1, 3], dataType: 'float32'} 286 } 287 } 288 } 289 }, 290 291 // float16 tests 292 { 293 'name': 'cos float16 0D scalar', 294 'graph': { 295 'inputs': { 296 'cosInput': 297 {'data': [85.5625], 'descriptor': {shape: [], dataType: 'float16'}} 298 }, 299 'operators': [{ 300 'name': 'cos', 301 'arguments': [{'input': 'cosInput'}], 302 'outputs': 'cosOutput' 303 }], 304 'expectedOutputs': { 305 'cosOutput': { 306 'data': [-0.73876953125], 307 'descriptor': {shape: [], dataType: 'float16'} 308 } 309 } 310 } 311 }, 312 { 313 'name': 'cos float16 1D constant tensor', 314 'graph': { 315 'inputs': { 316 'cosInput': { 317 'data': [ 318 -2.402, -1.111, -0.4397, 1.818, 0.04092, 1.072, -2.41, -1.318, 319 -1.301, -1.115, 1.891, -1.482, 1.848, -2.88, -1.67, -0.7266, 320 -2.514, 1.977, 1.844, -0.793, 1.727, 2.006, 1.164, 2.422 321 ], 322 'descriptor': {shape: [24], dataType: 'float16'}, 323 'constant': true 324 } 325 }, 326 'operators': [{ 327 'name': 'cos', 328 'arguments': [{'input': 'cosInput'}], 329 'outputs': 'cosOutput' 330 }], 331 'expectedOutputs': { 332 'cosOutput': { 333 'data': [ 334 -0.73876953125, 0.443359375, 0.90478515625, 335 -0.2452392578125, 0.9990234375, 0.47802734375, 336 -0.744140625, 0.24951171875, 0.266845703125, 337 0.43994140625, -0.314208984375, 0.08807373046875, 338 -0.273681640625, -0.96630859375, -0.09912109375, 339 0.74755859375, -0.8095703125, -0.39501953125, 340 -0.269287109375, 0.70166015625, -0.1551513671875, 341 -0.421630859375, 0.3955078125, -0.751953125 342 ], 343 'descriptor': {shape: [24], dataType: 'float16'} 344 } 345 } 346 } 347 }, 348 { 349 'name': 'cos float16 1D tensor', 350 'graph': { 351 'inputs': { 352 'cosInput': { 353 'data': [ 354 -2.402, -1.111, -0.4397, 1.818, 0.04092, 1.072, -2.41, -1.318, 355 -1.301, -1.115, 1.891, -1.482, 1.848, -2.88, -1.67, -0.7266, 356 -2.514, 1.977, 1.844, -0.793, 1.727, 2.006, 1.164, 2.422 357 ], 358 'descriptor': {shape: [24], dataType: 'float16'} 359 } 360 }, 361 'operators': [{ 362 'name': 'cos', 363 'arguments': [{'input': 'cosInput'}], 364 'outputs': 'cosOutput' 365 }], 366 'expectedOutputs': { 367 'cosOutput': { 368 'data': [ 369 -0.73876953125, 0.443359375, 0.90478515625, 370 -0.2452392578125, 0.9990234375, 0.47802734375, 371 -0.744140625, 0.24951171875, 0.266845703125, 372 0.43994140625, -0.314208984375, 0.08807373046875, 373 -0.273681640625, -0.96630859375, -0.09912109375, 374 0.74755859375, -0.8095703125, -0.39501953125, 375 -0.269287109375, 0.70166015625, -0.1551513671875, 376 -0.421630859375, 0.3955078125, -0.751953125 377 ], 378 'descriptor': {shape: [24], dataType: 'float16'} 379 } 380 } 381 } 382 }, 383 { 384 'name': 'cos float16 2D tensor', 385 'graph': { 386 'inputs': { 387 'cosInput': { 388 'data': [ 389 -2.402, -1.111, -0.4397, 1.818, 0.04092, 1.072, -2.41, -1.318, 390 -1.301, -1.115, 1.891, -1.482, 1.848, -2.88, -1.67, -0.7266, 391 -2.514, 1.977, 1.844, -0.793, 1.727, 2.006, 1.164, 2.422 392 ], 393 'descriptor': {shape: [4, 6], dataType: 'float16'} 394 } 395 }, 396 'operators': [{ 397 'name': 'cos', 398 'arguments': [{'input': 'cosInput'}], 399 'outputs': 'cosOutput' 400 }], 401 'expectedOutputs': { 402 'cosOutput': { 403 'data': [ 404 -0.73876953125, 0.443359375, 0.90478515625, 405 -0.2452392578125, 0.9990234375, 0.47802734375, 406 -0.744140625, 0.24951171875, 0.266845703125, 407 0.43994140625, -0.314208984375, 0.08807373046875, 408 -0.273681640625, -0.96630859375, -0.09912109375, 409 0.74755859375, -0.8095703125, -0.39501953125, 410 -0.269287109375, 0.70166015625, -0.1551513671875, 411 -0.421630859375, 0.3955078125, -0.751953125 412 ], 413 'descriptor': {shape: [4, 6], dataType: 'float16'} 414 } 415 } 416 } 417 }, 418 { 419 'name': 'cos float16 3D tensor', 420 'graph': { 421 'inputs': { 422 'cosInput': { 423 'data': [ 424 -2.402, -1.111, -0.4397, 1.818, 0.04092, 1.072, -2.41, -1.318, 425 -1.301, -1.115, 1.891, -1.482, 1.848, -2.88, -1.67, -0.7266, 426 -2.514, 1.977, 1.844, -0.793, 1.727, 2.006, 1.164, 2.422 427 ], 428 'descriptor': {shape: [2, 3, 4], dataType: 'float16'} 429 } 430 }, 431 'operators': [{ 432 'name': 'cos', 433 'arguments': [{'input': 'cosInput'}], 434 'outputs': 'cosOutput' 435 }], 436 'expectedOutputs': { 437 'cosOutput': { 438 'data': [ 439 -0.73876953125, 0.443359375, 0.90478515625, 440 -0.2452392578125, 0.9990234375, 0.47802734375, 441 -0.744140625, 0.24951171875, 0.266845703125, 442 0.43994140625, -0.314208984375, 0.08807373046875, 443 -0.273681640625, -0.96630859375, -0.09912109375, 444 0.74755859375, -0.8095703125, -0.39501953125, 445 -0.269287109375, 0.70166015625, -0.1551513671875, 446 -0.421630859375, 0.3955078125, -0.751953125 447 ], 448 'descriptor': {shape: [2, 3, 4], dataType: 'float16'} 449 } 450 } 451 } 452 }, 453 { 454 'name': 'cos float16 4D tensor', 455 'graph': { 456 'inputs': { 457 'cosInput': { 458 'data': [ 459 -2.402, -1.111, -0.4397, 1.818, 0.04092, 1.072, -2.41, -1.318, 460 -1.301, -1.115, 1.891, -1.482, 1.848, -2.88, -1.67, -0.7266, 461 -2.514, 1.977, 1.844, -0.793, 1.727, 2.006, 1.164, 2.422 462 ], 463 'descriptor': {shape: [2, 2, 2, 3], dataType: 'float16'} 464 } 465 }, 466 'operators': [{ 467 'name': 'cos', 468 'arguments': [{'input': 'cosInput'}], 469 'outputs': 'cosOutput' 470 }], 471 'expectedOutputs': { 472 'cosOutput': { 473 'data': [ 474 -0.73876953125, 0.443359375, 0.90478515625, 475 -0.2452392578125, 0.9990234375, 0.47802734375, 476 -0.744140625, 0.24951171875, 0.266845703125, 477 0.43994140625, -0.314208984375, 0.08807373046875, 478 -0.273681640625, -0.96630859375, -0.09912109375, 479 0.74755859375, -0.8095703125, -0.39501953125, 480 -0.269287109375, 0.70166015625, -0.1551513671875, 481 -0.421630859375, 0.3955078125, -0.751953125 482 ], 483 'descriptor': {shape: [2, 2, 2, 3], dataType: 'float16'} 484 } 485 } 486 } 487 }, 488 { 489 'name': 'cos float16 5D tensor', 490 'graph': { 491 'inputs': { 492 'cosInput': { 493 'data': [ 494 -2.402, -1.111, -0.4397, 1.818, 0.04092, 1.072, -2.41, -1.318, 495 -1.301, -1.115, 1.891, -1.482, 1.848, -2.88, -1.67, -0.7266, 496 -2.514, 1.977, 1.844, -0.793, 1.727, 2.006, 1.164, 2.422 497 ], 498 'descriptor': {shape: [2, 1, 4, 1, 3], dataType: 'float16'} 499 } 500 }, 501 'operators': [{ 502 'name': 'cos', 503 'arguments': [{'input': 'cosInput'}], 504 'outputs': 'cosOutput' 505 }], 506 'expectedOutputs': { 507 'cosOutput': { 508 'data': [ 509 -0.73876953125, 0.443359375, 0.90478515625, 510 -0.2452392578125, 0.9990234375, 0.47802734375, 511 -0.744140625, 0.24951171875, 0.266845703125, 512 0.43994140625, -0.314208984375, 0.08807373046875, 513 -0.273681640625, -0.96630859375, -0.09912109375, 514 0.74755859375, -0.8095703125, -0.39501953125, 515 -0.269287109375, 0.70166015625, -0.1551513671875, 516 -0.421630859375, 0.3955078125, -0.751953125 517 ], 518 'descriptor': {shape: [2, 1, 4, 1, 3], dataType: 'float16'} 519 } 520 } 521 } 522 } 523 ]; 524 525 webnn_conformance_test( 526 cosTests, buildAndExecuteGraph, getCosPrecisionTolerance);