hard_swish.https.any.js (20121B)
1 // META: title=test WebNN API tanh 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-hard-swish 12 // Computes the nonlinear function y = x * max(0, min(6, (x + 3))) / 6 that is 13 // introduced by MobileNetV3 on the input tensor element-wise. 14 // 15 // MLOperand hardSwish(MLOperand input); 16 17 const hardSwishTests = [ 18 { 19 'name': 'hardSwish float32 0D tensor', 20 'graph': { 21 'inputs': { 22 'hardSwishInput': { 23 'data': [0.7341583371162415], 24 'descriptor': {shape: [], dataType: 'float32'} 25 } 26 }, 27 'operators': [{ 28 'name': 'hardSwish', 29 'arguments': [{'input': 'hardSwishInput'}], 30 'outputs': 'hardSwishOutput' 31 }], 32 'expectedOutputs': { 33 'hardSwishOutput': { 34 'data': [0.4569105803966522], 35 'descriptor': {shape: [], dataType: 'float32'} 36 } 37 } 38 } 39 }, 40 { 41 'name': 'hardSwish float32 1D constant tensor', 42 'graph': { 43 'inputs': { 44 'hardSwishInput': { 45 'data': [ 46 0.7341583371162415, 9.11885929107666, 3.545238494873047, 47 2.621943950653076, -6.445507526397705, -1.6835596561431885, 48 5.52318000793457, -5.958856105804443, -9.169190406799316, 49 6.420943737030029, -3.2930312156677246, 1.041016697883606, 50 -7.2463226318359375, -0.9472730755805969, -5.7783522605896, 51 3.1852290630340576, -7.261817932128906, 4.174602508544922, 52 3.7802627086639404, -6.071240425109863, -9.909919738769531, 53 -7.744259357452393, -8.286120414733887, 8.083491325378418 54 ], 55 'descriptor': {shape: [24], dataType: 'float32'}, 56 'constant': true 57 } 58 }, 59 'operators': [{ 60 'name': 'hardSwish', 61 'arguments': [{'input': 'hardSwishInput'}], 62 'outputs': 'hardSwishOutput' 63 }], 64 'expectedOutputs': { 65 'hardSwishOutput': { 66 'data': [ 67 0.4569105803966522, 68 9.11885929107666, 69 3.545238494873047, 70 2.4567370414733887, 71 0, 72 -0.3693843185901642, 73 5.52318000793457, 74 0, 75 0, 76 6.420943737030029, 77 0, 78 0.7011276483535767, 79 0, 80 -0.3240821659564972, 81 0, 82 3.1852290630340576, 83 0, 84 4.174602508544922, 85 3.7802627086639404, 86 0, 87 0, 88 0, 89 0, 90 8.083491325378418 91 ], 92 'descriptor': {shape: [24], dataType: 'float32'} 93 } 94 } 95 } 96 }, 97 { 98 'name': 'hardSwish float32 1D tensor', 99 'graph': { 100 'inputs': { 101 'hardSwishInput': { 102 'data': [ 103 0.7341583371162415, 9.11885929107666, 3.545238494873047, 104 2.621943950653076, -6.445507526397705, -1.6835596561431885, 105 5.52318000793457, -5.958856105804443, -9.169190406799316, 106 6.420943737030029, -3.2930312156677246, 1.041016697883606, 107 -7.2463226318359375, -0.9472730755805969, -5.7783522605896, 108 3.1852290630340576, -7.261817932128906, 4.174602508544922, 109 3.7802627086639404, -6.071240425109863, -9.909919738769531, 110 -7.744259357452393, -8.286120414733887, 8.083491325378418 111 ], 112 'descriptor': {shape: [24], dataType: 'float32'} 113 } 114 }, 115 'operators': [{ 116 'name': 'hardSwish', 117 'arguments': [{'input': 'hardSwishInput'}], 118 'outputs': 'hardSwishOutput' 119 }], 120 'expectedOutputs': { 121 'hardSwishOutput': { 122 'data': [ 123 0.4569105803966522, 124 9.11885929107666, 125 3.545238494873047, 126 2.4567370414733887, 127 0, 128 -0.3693843185901642, 129 5.52318000793457, 130 0, 131 0, 132 6.420943737030029, 133 0, 134 0.7011276483535767, 135 0, 136 -0.3240821659564972, 137 0, 138 3.1852290630340576, 139 0, 140 4.174602508544922, 141 3.7802627086639404, 142 0, 143 0, 144 0, 145 0, 146 8.083491325378418 147 ], 148 'descriptor': {shape: [24], dataType: 'float32'} 149 } 150 } 151 } 152 }, 153 { 154 'name': 'hardSwish float32 2D tensor', 155 'graph': { 156 'inputs': { 157 'hardSwishInput': { 158 'data': [ 159 0.7341583371162415, 9.11885929107666, 3.545238494873047, 160 2.621943950653076, -6.445507526397705, -1.6835596561431885, 161 5.52318000793457, -5.958856105804443, -9.169190406799316, 162 6.420943737030029, -3.2930312156677246, 1.041016697883606, 163 -7.2463226318359375, -0.9472730755805969, -5.7783522605896, 164 3.1852290630340576, -7.261817932128906, 4.174602508544922, 165 3.7802627086639404, -6.071240425109863, -9.909919738769531, 166 -7.744259357452393, -8.286120414733887, 8.083491325378418 167 ], 168 'descriptor': {shape: [4, 6], dataType: 'float32'} 169 } 170 }, 171 'operators': [{ 172 'name': 'hardSwish', 173 'arguments': [{'input': 'hardSwishInput'}], 174 'outputs': 'hardSwishOutput' 175 }], 176 'expectedOutputs': { 177 'hardSwishOutput': { 178 'data': [ 179 0.4569105803966522, 180 9.11885929107666, 181 3.545238494873047, 182 2.4567370414733887, 183 0, 184 -0.3693843185901642, 185 5.52318000793457, 186 0, 187 0, 188 6.420943737030029, 189 0, 190 0.7011276483535767, 191 0, 192 -0.3240821659564972, 193 0, 194 3.1852290630340576, 195 0, 196 4.174602508544922, 197 3.7802627086639404, 198 0, 199 0, 200 0, 201 0, 202 8.083491325378418 203 ], 204 'descriptor': {shape: [4, 6], dataType: 'float32'} 205 } 206 } 207 } 208 }, 209 { 210 'name': 'hardSwish float32 3D tensor', 211 'graph': { 212 'inputs': { 213 'hardSwishInput': { 214 'data': [ 215 0.7341583371162415, 9.11885929107666, 3.545238494873047, 216 2.621943950653076, -6.445507526397705, -1.6835596561431885, 217 5.52318000793457, -5.958856105804443, -9.169190406799316, 218 6.420943737030029, -3.2930312156677246, 1.041016697883606, 219 -7.2463226318359375, -0.9472730755805969, -5.7783522605896, 220 3.1852290630340576, -7.261817932128906, 4.174602508544922, 221 3.7802627086639404, -6.071240425109863, -9.909919738769531, 222 -7.744259357452393, -8.286120414733887, 8.083491325378418 223 ], 224 'descriptor': {shape: [2, 3, 4], dataType: 'float32'} 225 } 226 }, 227 'operators': [{ 228 'name': 'hardSwish', 229 'arguments': [{'input': 'hardSwishInput'}], 230 'outputs': 'hardSwishOutput' 231 }], 232 'expectedOutputs': { 233 'hardSwishOutput': { 234 'data': [ 235 0.4569105803966522, 236 9.11885929107666, 237 3.545238494873047, 238 2.4567370414733887, 239 0, 240 -0.3693843185901642, 241 5.52318000793457, 242 0, 243 0, 244 6.420943737030029, 245 0, 246 0.7011276483535767, 247 0, 248 -0.3240821659564972, 249 0, 250 3.1852290630340576, 251 0, 252 4.174602508544922, 253 3.7802627086639404, 254 0, 255 0, 256 0, 257 0, 258 8.083491325378418 259 ], 260 'descriptor': {shape: [2, 3, 4], dataType: 'float32'} 261 } 262 } 263 } 264 }, 265 { 266 'name': 'hardSwish float32 4D tensor', 267 'graph': { 268 'inputs': { 269 'hardSwishInput': { 270 'data': [ 271 0.7341583371162415, 9.11885929107666, 3.545238494873047, 272 2.621943950653076, -6.445507526397705, -1.6835596561431885, 273 5.52318000793457, -5.958856105804443, -9.169190406799316, 274 6.420943737030029, -3.2930312156677246, 1.041016697883606, 275 -7.2463226318359375, -0.9472730755805969, -5.7783522605896, 276 3.1852290630340576, -7.261817932128906, 4.174602508544922, 277 3.7802627086639404, -6.071240425109863, -9.909919738769531, 278 -7.744259357452393, -8.286120414733887, 8.083491325378418 279 ], 280 'descriptor': {shape: [2, 2, 2, 3], dataType: 'float32'} 281 } 282 }, 283 'operators': [{ 284 'name': 'hardSwish', 285 'arguments': [{'input': 'hardSwishInput'}], 286 'outputs': 'hardSwishOutput' 287 }], 288 'expectedOutputs': { 289 'hardSwishOutput': { 290 'data': [ 291 0.4569105803966522, 292 9.11885929107666, 293 3.545238494873047, 294 2.4567370414733887, 295 0, 296 -0.3693843185901642, 297 5.52318000793457, 298 0, 299 0, 300 6.420943737030029, 301 0, 302 0.7011276483535767, 303 0, 304 -0.3240821659564972, 305 0, 306 3.1852290630340576, 307 0, 308 4.174602508544922, 309 3.7802627086639404, 310 0, 311 0, 312 0, 313 0, 314 8.083491325378418 315 ], 316 'descriptor': {shape: [2, 2, 2, 3], dataType: 'float32'} 317 } 318 } 319 } 320 }, 321 { 322 'name': 'hardSwish float32 5D tensor', 323 'graph': { 324 'inputs': { 325 'hardSwishInput': { 326 'data': [ 327 0.7341583371162415, 9.11885929107666, 3.545238494873047, 328 2.621943950653076, -6.445507526397705, -1.6835596561431885, 329 5.52318000793457, -5.958856105804443, -9.169190406799316, 330 6.420943737030029, -3.2930312156677246, 1.041016697883606, 331 -7.2463226318359375, -0.9472730755805969, -5.7783522605896, 332 3.1852290630340576, -7.261817932128906, 4.174602508544922, 333 3.7802627086639404, -6.071240425109863, -9.909919738769531, 334 -7.744259357452393, -8.286120414733887, 8.083491325378418 335 ], 336 'descriptor': {shape: [2, 1, 4, 1, 3], dataType: 'float32'} 337 } 338 }, 339 'operators': [{ 340 'name': 'hardSwish', 341 'arguments': [{'input': 'hardSwishInput'}], 342 'outputs': 'hardSwishOutput' 343 }], 344 'expectedOutputs': { 345 'hardSwishOutput': { 346 'data': [ 347 0.4569105803966522, 348 9.11885929107666, 349 3.545238494873047, 350 2.4567370414733887, 351 0, 352 -0.3693843185901642, 353 5.52318000793457, 354 0, 355 0, 356 6.420943737030029, 357 0, 358 0.7011276483535767, 359 0, 360 -0.3240821659564972, 361 0, 362 3.1852290630340576, 363 0, 364 4.174602508544922, 365 3.7802627086639404, 366 0, 367 0, 368 0, 369 0, 370 8.083491325378418 371 ], 372 'descriptor': {shape: [2, 1, 4, 1, 3], dataType: 'float32'} 373 } 374 } 375 } 376 }, 377 378 // float16 tests 379 { 380 'name': 'hardSwish float16 0D tensor', 381 'graph': { 382 'inputs': { 383 'hardSwishInput': 384 {'data': [0.734375], 'descriptor': {shape: [], dataType: 'float16'}} 385 }, 386 'operators': [{ 387 'name': 'hardSwish', 388 'arguments': [{'input': 'hardSwishInput'}], 389 'outputs': 'hardSwishOutput' 390 }], 391 'expectedOutputs': { 392 'hardSwishOutput': { 393 'data': [0.45703125], 394 'descriptor': {shape: [], dataType: 'float16'} 395 } 396 } 397 } 398 }, 399 { 400 'name': 'hardSwish float16 1D constant tensor', 401 'graph': { 402 'inputs': { 403 'hardSwishInput': { 404 'data': [ 405 0.734375, 9.1171875, 3.544921875, 2.62109375, -6.4453125, 406 -1.68359375, 5.5234375, -5.95703125, -9.171875, 6.421875, 407 -3.29296875, 1.041015625, -7.24609375, -0.947265625, -5.77734375, 408 3.185546875, -7.26171875, 4.17578125, 3.779296875, -6.0703125, 409 -9.90625, -7.74609375, -8.2890625, 8.0859375 410 ], 411 'descriptor': {shape: [24], dataType: 'float16'}, 412 'constant': true 413 } 414 }, 415 'operators': [{ 416 'name': 'hardSwish', 417 'arguments': [{'input': 'hardSwishInput'}], 418 'outputs': 'hardSwishOutput' 419 }], 420 'expectedOutputs': { 421 'hardSwishOutput': { 422 'data': [ 423 0.45703125, 9.1171875, 424 3.544921875, 2.455078125, 425 0, -0.369384765625, 426 5.5234375, 0, 427 0, 6.421875, 428 0, 0.701171875, 429 0, -0.323974609375, 430 0, 3.185546875, 431 0, 4.17578125, 432 3.779296875, 0, 433 0, 0, 434 0, 8.0859375 435 ], 436 'descriptor': {shape: [24], dataType: 'float16'} 437 } 438 } 439 } 440 }, 441 { 442 'name': 'hardSwish float16 1D tensor', 443 'graph': { 444 'inputs': { 445 'hardSwishInput': { 446 'data': [ 447 0.734375, 9.1171875, 3.544921875, 2.62109375, -6.4453125, 448 -1.68359375, 5.5234375, -5.95703125, -9.171875, 6.421875, 449 -3.29296875, 1.041015625, -7.24609375, -0.947265625, -5.77734375, 450 3.185546875, -7.26171875, 4.17578125, 3.779296875, -6.0703125, 451 -9.90625, -7.74609375, -8.2890625, 8.0859375 452 ], 453 'descriptor': {shape: [24], dataType: 'float16'} 454 } 455 }, 456 'operators': [{ 457 'name': 'hardSwish', 458 'arguments': [{'input': 'hardSwishInput'}], 459 'outputs': 'hardSwishOutput' 460 }], 461 'expectedOutputs': { 462 'hardSwishOutput': { 463 'data': [ 464 0.45703125, 9.1171875, 465 3.544921875, 2.455078125, 466 0, -0.369384765625, 467 5.5234375, 0, 468 0, 6.421875, 469 0, 0.701171875, 470 0, -0.323974609375, 471 0, 3.185546875, 472 0, 4.17578125, 473 3.779296875, 0, 474 0, 0, 475 0, 8.0859375 476 ], 477 'descriptor': {shape: [24], dataType: 'float16'} 478 } 479 } 480 } 481 }, 482 { 483 'name': 'hardSwish float16 2D tensor', 484 'graph': { 485 'inputs': { 486 'hardSwishInput': { 487 'data': [ 488 0.734375, 9.1171875, 3.544921875, 2.62109375, -6.4453125, 489 -1.68359375, 5.5234375, -5.95703125, -9.171875, 6.421875, 490 -3.29296875, 1.041015625, -7.24609375, -0.947265625, -5.77734375, 491 3.185546875, -7.26171875, 4.17578125, 3.779296875, -6.0703125, 492 -9.90625, -7.74609375, -8.2890625, 8.0859375 493 ], 494 'descriptor': {shape: [4, 6], dataType: 'float16'} 495 } 496 }, 497 'operators': [{ 498 'name': 'hardSwish', 499 'arguments': [{'input': 'hardSwishInput'}], 500 'outputs': 'hardSwishOutput' 501 }], 502 'expectedOutputs': { 503 'hardSwishOutput': { 504 'data': [ 505 0.45703125, 9.1171875, 506 3.544921875, 2.455078125, 507 0, -0.369384765625, 508 5.5234375, 0, 509 0, 6.421875, 510 0, 0.701171875, 511 0, -0.323974609375, 512 0, 3.185546875, 513 0, 4.17578125, 514 3.779296875, 0, 515 0, 0, 516 0, 8.0859375 517 ], 518 'descriptor': {shape: [4, 6], dataType: 'float16'} 519 } 520 } 521 } 522 }, 523 { 524 'name': 'hardSwish float16 3D tensor', 525 'graph': { 526 'inputs': { 527 'hardSwishInput': { 528 'data': [ 529 0.734375, 9.1171875, 3.544921875, 2.62109375, -6.4453125, 530 -1.68359375, 5.5234375, -5.95703125, -9.171875, 6.421875, 531 -3.29296875, 1.041015625, -7.24609375, -0.947265625, -5.77734375, 532 3.185546875, -7.26171875, 4.17578125, 3.779296875, -6.0703125, 533 -9.90625, -7.74609375, -8.2890625, 8.0859375 534 ], 535 'descriptor': {shape: [2, 3, 4], dataType: 'float16'} 536 } 537 }, 538 'operators': [{ 539 'name': 'hardSwish', 540 'arguments': [{'input': 'hardSwishInput'}], 541 'outputs': 'hardSwishOutput' 542 }], 543 'expectedOutputs': { 544 'hardSwishOutput': { 545 'data': [ 546 0.45703125, 9.1171875, 547 3.544921875, 2.455078125, 548 0, -0.369384765625, 549 5.5234375, 0, 550 0, 6.421875, 551 0, 0.701171875, 552 0, -0.323974609375, 553 0, 3.185546875, 554 0, 4.17578125, 555 3.779296875, 0, 556 0, 0, 557 0, 8.0859375 558 ], 559 'descriptor': {shape: [2, 3, 4], dataType: 'float16'} 560 } 561 } 562 } 563 }, 564 { 565 'name': 'hardSwish float16 4D tensor', 566 'graph': { 567 'inputs': { 568 'hardSwishInput': { 569 'data': [ 570 0.734375, 9.1171875, 3.544921875, 2.62109375, -6.4453125, 571 -1.68359375, 5.5234375, -5.95703125, -9.171875, 6.421875, 572 -3.29296875, 1.041015625, -7.24609375, -0.947265625, -5.77734375, 573 3.185546875, -7.26171875, 4.17578125, 3.779296875, -6.0703125, 574 -9.90625, -7.74609375, -8.2890625, 8.0859375 575 ], 576 'descriptor': {shape: [2, 2, 2, 3], dataType: 'float16'} 577 } 578 }, 579 'operators': [{ 580 'name': 'hardSwish', 581 'arguments': [{'input': 'hardSwishInput'}], 582 'outputs': 'hardSwishOutput' 583 }], 584 'expectedOutputs': { 585 'hardSwishOutput': { 586 'data': [ 587 0.45703125, 9.1171875, 588 3.544921875, 2.455078125, 589 0, -0.369384765625, 590 5.5234375, 0, 591 0, 6.421875, 592 0, 0.701171875, 593 0, -0.323974609375, 594 0, 3.185546875, 595 0, 4.17578125, 596 3.779296875, 0, 597 0, 0, 598 0, 8.0859375 599 ], 600 'descriptor': {shape: [2, 2, 2, 3], dataType: 'float16'} 601 } 602 } 603 } 604 }, 605 { 606 'name': 'hardSwish float16 5D tensor', 607 'graph': { 608 'inputs': { 609 'hardSwishInput': { 610 'data': [ 611 0.734375, 9.1171875, 3.544921875, 2.62109375, -6.4453125, 612 -1.68359375, 5.5234375, -5.95703125, -9.171875, 6.421875, 613 -3.29296875, 1.041015625, -7.24609375, -0.947265625, -5.77734375, 614 3.185546875, -7.26171875, 4.17578125, 3.779296875, -6.0703125, 615 -9.90625, -7.74609375, -8.2890625, 8.0859375 616 ], 617 'descriptor': {shape: [2, 1, 4, 1, 3], dataType: 'float16'} 618 } 619 }, 620 'operators': [{ 621 'name': 'hardSwish', 622 'arguments': [{'input': 'hardSwishInput'}], 623 'outputs': 'hardSwishOutput' 624 }], 625 'expectedOutputs': { 626 'hardSwishOutput': { 627 'data': [ 628 0.45703125, 9.1171875, 629 3.544921875, 2.455078125, 630 0, -0.369384765625, 631 5.5234375, 0, 632 0, 6.421875, 633 0, 0.701171875, 634 0, -0.323974609375, 635 0, 3.185546875, 636 0, 4.17578125, 637 3.779296875, 0, 638 0, 0, 639 0, 8.0859375 640 ], 641 'descriptor': {shape: [2, 1, 4, 1, 3], dataType: 'float16'} 642 } 643 } 644 } 645 } 646 ]; 647 648 webnn_conformance_test( 649 hardSwishTests, buildAndExecuteGraph, getPrecisionTolerance);