tanh.https.any.js (20065B)
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-tanh-method 12 // Compute the hyperbolic tangent function of the input tensor. The calculation 13 // follows the expression (exp(2 * x) - 1) / (exp(2 * x) + 1). 14 // 15 // MLOperand tanh(MLOperand input); 16 17 const tanhTests = [ 18 { 19 'name': 'tanh float32 1D constant tensor', 20 'graph': { 21 'inputs': { 22 'tanhInput': { 23 'data': [ 24 5.473527431488037, -1.1535595655441284, 0.4074455797672272, 25 1.8297704458236694, 2.869000196456909, -4.570195198059082, 26 4.146744251251221, -4.065934181213379, -3.7128469944000244, 27 0.9077175259590149, -0.11083049327135086, 5.955096244812012, 28 1.7831857204437256, 4.023128509521484, 5.587857723236084, 29 -5.280653953552246, 1.4147950410842896, -5.707716941833496, 30 -1.443918228149414, -1.9129083156585693, 2.7495968341827393, 31 -0.7420240044593811, 4.856568336486816, -0.7563357949256897 32 ], 33 'descriptor': {shape: [24], dataType: 'float32'}, 34 'constant': true 35 } 36 }, 37 'operators': [{ 38 'name': 'tanh', 39 'arguments': [{'input': 'tanhInput'}], 40 'outputs': 'tanhOutput' 41 }], 42 'expectedOutputs': { 43 'tanhOutput': { 44 'data': [ 45 0.9999647736549377, -0.8189298510551453, 0.38630160689353943, 46 0.9498035907745361, 0.9935782551765442, -0.9997855424880981, 47 0.9994998574256897, -0.9994121193885803, -0.9988092184066772, 48 0.7200349569320679, -0.1103789210319519, 0.9999865293502808, 49 0.945036768913269, 0.9993596076965332, 0.9999719858169556, 50 -0.9999482035636902, 0.8885080814361572, -0.9999779462814331, 51 -0.894483745098114, -0.9573289752006531, 0.9918531775474548, 52 -0.6303664445877075, 0.9998790621757507, -0.6389135718345642 53 ], 54 'descriptor': {shape: [24], dataType: 'float32'} 55 } 56 } 57 } 58 }, 59 { 60 'name': 'tanh float32 1D tensor', 61 'graph': { 62 'inputs': { 63 'tanhInput': { 64 'data': [ 65 5.473527431488037, -1.1535595655441284, 0.4074455797672272, 66 1.8297704458236694, 2.869000196456909, -4.570195198059082, 67 4.146744251251221, -4.065934181213379, -3.7128469944000244, 68 0.9077175259590149, -0.11083049327135086, 5.955096244812012, 69 1.7831857204437256, 4.023128509521484, 5.587857723236084, 70 -5.280653953552246, 1.4147950410842896, -5.707716941833496, 71 -1.443918228149414, -1.9129083156585693, 2.7495968341827393, 72 -0.7420240044593811, 4.856568336486816, -0.7563357949256897 73 ], 74 'descriptor': {shape: [24], dataType: 'float32'} 75 } 76 }, 77 'operators': [{ 78 'name': 'tanh', 79 'arguments': [{'input': 'tanhInput'}], 80 'outputs': 'tanhOutput' 81 }], 82 'expectedOutputs': { 83 'tanhOutput': { 84 'data': [ 85 0.9999647736549377, -0.8189298510551453, 0.38630160689353943, 86 0.9498035907745361, 0.9935782551765442, -0.9997855424880981, 87 0.9994998574256897, -0.9994121193885803, -0.9988092184066772, 88 0.7200349569320679, -0.1103789210319519, 0.9999865293502808, 89 0.945036768913269, 0.9993596076965332, 0.9999719858169556, 90 -0.9999482035636902, 0.8885080814361572, -0.9999779462814331, 91 -0.894483745098114, -0.9573289752006531, 0.9918531775474548, 92 -0.6303664445877075, 0.9998790621757507, -0.6389135718345642 93 ], 94 'descriptor': {shape: [24], dataType: 'float32'} 95 } 96 } 97 } 98 }, 99 { 100 'name': 'tanh float32 2D tensor', 101 'graph': { 102 'inputs': { 103 'tanhInput': { 104 'data': [ 105 5.473527431488037, -1.1535595655441284, 0.4074455797672272, 106 1.8297704458236694, 2.869000196456909, -4.570195198059082, 107 4.146744251251221, -4.065934181213379, -3.7128469944000244, 108 0.9077175259590149, -0.11083049327135086, 5.955096244812012, 109 1.7831857204437256, 4.023128509521484, 5.587857723236084, 110 -5.280653953552246, 1.4147950410842896, -5.707716941833496, 111 -1.443918228149414, -1.9129083156585693, 2.7495968341827393, 112 -0.7420240044593811, 4.856568336486816, -0.7563357949256897 113 ], 114 'descriptor': {shape: [4, 6], dataType: 'float32'} 115 } 116 }, 117 'operators': [{ 118 'name': 'tanh', 119 'arguments': [{'input': 'tanhInput'}], 120 'outputs': 'tanhOutput' 121 }], 122 'expectedOutputs': { 123 'tanhOutput': { 124 'data': [ 125 0.9999647736549377, -0.8189298510551453, 0.38630160689353943, 126 0.9498035907745361, 0.9935782551765442, -0.9997855424880981, 127 0.9994998574256897, -0.9994121193885803, -0.9988092184066772, 128 0.7200349569320679, -0.1103789210319519, 0.9999865293502808, 129 0.945036768913269, 0.9993596076965332, 0.9999719858169556, 130 -0.9999482035636902, 0.8885080814361572, -0.9999779462814331, 131 -0.894483745098114, -0.9573289752006531, 0.9918531775474548, 132 -0.6303664445877075, 0.9998790621757507, -0.6389135718345642 133 ], 134 'descriptor': {shape: [4, 6], dataType: 'float32'} 135 } 136 } 137 } 138 }, 139 { 140 'name': 'tanh float32 3D tensor', 141 'graph': { 142 'inputs': { 143 'tanhInput': { 144 'data': [ 145 5.473527431488037, -1.1535595655441284, 0.4074455797672272, 146 1.8297704458236694, 2.869000196456909, -4.570195198059082, 147 4.146744251251221, -4.065934181213379, -3.7128469944000244, 148 0.9077175259590149, -0.11083049327135086, 5.955096244812012, 149 1.7831857204437256, 4.023128509521484, 5.587857723236084, 150 -5.280653953552246, 1.4147950410842896, -5.707716941833496, 151 -1.443918228149414, -1.9129083156585693, 2.7495968341827393, 152 -0.7420240044593811, 4.856568336486816, -0.7563357949256897 153 ], 154 'descriptor': {shape: [2, 3, 4], dataType: 'float32'} 155 } 156 }, 157 'operators': [{ 158 'name': 'tanh', 159 'arguments': [{'input': 'tanhInput'}], 160 'outputs': 'tanhOutput' 161 }], 162 'expectedOutputs': { 163 'tanhOutput': { 164 'data': [ 165 0.9999647736549377, -0.8189298510551453, 0.38630160689353943, 166 0.9498035907745361, 0.9935782551765442, -0.9997855424880981, 167 0.9994998574256897, -0.9994121193885803, -0.9988092184066772, 168 0.7200349569320679, -0.1103789210319519, 0.9999865293502808, 169 0.945036768913269, 0.9993596076965332, 0.9999719858169556, 170 -0.9999482035636902, 0.8885080814361572, -0.9999779462814331, 171 -0.894483745098114, -0.9573289752006531, 0.9918531775474548, 172 -0.6303664445877075, 0.9998790621757507, -0.6389135718345642 173 ], 174 'descriptor': {shape: [2, 3, 4], dataType: 'float32'} 175 } 176 } 177 } 178 }, 179 { 180 'name': 'tanh float32 4D tensor', 181 'graph': { 182 'inputs': { 183 'tanhInput': { 184 'data': [ 185 5.473527431488037, -1.1535595655441284, 0.4074455797672272, 186 1.8297704458236694, 2.869000196456909, -4.570195198059082, 187 4.146744251251221, -4.065934181213379, -3.7128469944000244, 188 0.9077175259590149, -0.11083049327135086, 5.955096244812012, 189 1.7831857204437256, 4.023128509521484, 5.587857723236084, 190 -5.280653953552246, 1.4147950410842896, -5.707716941833496, 191 -1.443918228149414, -1.9129083156585693, 2.7495968341827393, 192 -0.7420240044593811, 4.856568336486816, -0.7563357949256897 193 ], 194 'descriptor': {shape: [2, 2, 2, 3], dataType: 'float32'} 195 } 196 }, 197 'operators': [{ 198 'name': 'tanh', 199 'arguments': [{'input': 'tanhInput'}], 200 'outputs': 'tanhOutput' 201 }], 202 'expectedOutputs': { 203 'tanhOutput': { 204 'data': [ 205 0.9999647736549377, -0.8189298510551453, 0.38630160689353943, 206 0.9498035907745361, 0.9935782551765442, -0.9997855424880981, 207 0.9994998574256897, -0.9994121193885803, -0.9988092184066772, 208 0.7200349569320679, -0.1103789210319519, 0.9999865293502808, 209 0.945036768913269, 0.9993596076965332, 0.9999719858169556, 210 -0.9999482035636902, 0.8885080814361572, -0.9999779462814331, 211 -0.894483745098114, -0.9573289752006531, 0.9918531775474548, 212 -0.6303664445877075, 0.9998790621757507, -0.6389135718345642 213 ], 214 'descriptor': {shape: [2, 2, 2, 3], dataType: 'float32'} 215 } 216 } 217 } 218 }, 219 { 220 'name': 'tanh float32 5D tensor', 221 'graph': { 222 'inputs': { 223 'tanhInput': { 224 'data': [ 225 5.473527431488037, -1.1535595655441284, 0.4074455797672272, 226 1.8297704458236694, 2.869000196456909, -4.570195198059082, 227 4.146744251251221, -4.065934181213379, -3.7128469944000244, 228 0.9077175259590149, -0.11083049327135086, 5.955096244812012, 229 1.7831857204437256, 4.023128509521484, 5.587857723236084, 230 -5.280653953552246, 1.4147950410842896, -5.707716941833496, 231 -1.443918228149414, -1.9129083156585693, 2.7495968341827393, 232 -0.7420240044593811, 4.856568336486816, -0.7563357949256897 233 ], 234 'descriptor': {shape: [2, 1, 4, 1, 3], dataType: 'float32'} 235 } 236 }, 237 'operators': [{ 238 'name': 'tanh', 239 'arguments': [{'input': 'tanhInput'}], 240 'outputs': 'tanhOutput' 241 }], 242 'expectedOutputs': { 243 'tanhOutput': { 244 'data': [ 245 0.9999647736549377, -0.8189298510551453, 0.38630160689353943, 246 0.9498035907745361, 0.9935782551765442, -0.9997855424880981, 247 0.9994998574256897, -0.9994121193885803, -0.9988092184066772, 248 0.7200349569320679, -0.1103789210319519, 0.9999865293502808, 249 0.945036768913269, 0.9993596076965332, 0.9999719858169556, 250 -0.9999482035636902, 0.8885080814361572, -0.9999779462814331, 251 -0.894483745098114, -0.9573289752006531, 0.9918531775474548, 252 -0.6303664445877075, 0.9998790621757507, -0.6389135718345642 253 ], 254 'descriptor': {shape: [2, 1, 4, 1, 3], dataType: 'float32'} 255 } 256 } 257 } 258 }, 259 260 // float16 tests 261 { 262 'name': 'tanh float16 1D constant tensor', 263 'graph': { 264 'inputs': { 265 'tanhInput': { 266 'data': [ 267 5.47265625, -1.1533203125, 0.407470703125, 1.830078125, 268 2.869140625, -4.5703125, 4.1484375, -4.06640625, 269 -3.712890625, 0.90771484375, -0.11083984375, 5.95703125, 270 1.783203125, 4.0234375, 5.5859375, -5.28125, 271 1.4150390625, -5.70703125, -1.4443359375, -1.9130859375, 272 2.75, -0.7421875, 4.85546875, -0.75634765625 273 ], 274 'descriptor': {shape: [24], dataType: 'float16'}, 275 'constant': true 276 } 277 }, 278 'operators': [{ 279 'name': 'tanh', 280 'arguments': [{'input': 'tanhInput'}], 281 'outputs': 'tanhOutput' 282 }], 283 'expectedOutputs': { 284 'tanhOutput': { 285 'data': [ 286 1, 287 -0.81884765625, 288 0.38623046875, 289 0.94970703125, 290 0.99365234375, 291 -1, 292 0.99951171875, 293 -0.99951171875, 294 -0.9990234375, 295 0.72021484375, 296 -0.11041259765625, 297 1, 298 0.94482421875, 299 0.99951171875, 300 1, 301 -1, 302 0.888671875, 303 -1, 304 -0.89453125, 305 -0.95751953125, 306 0.99169921875, 307 -0.63037109375, 308 1, 309 -0.63916015625 310 ], 311 'descriptor': {shape: [24], dataType: 'float16'} 312 } 313 } 314 } 315 }, 316 { 317 'name': 'tanh float16 1D tensor', 318 'graph': { 319 'inputs': { 320 'tanhInput': { 321 'data': [ 322 5.47265625, -1.1533203125, 0.407470703125, 1.830078125, 323 2.869140625, -4.5703125, 4.1484375, -4.06640625, 324 -3.712890625, 0.90771484375, -0.11083984375, 5.95703125, 325 1.783203125, 4.0234375, 5.5859375, -5.28125, 326 1.4150390625, -5.70703125, -1.4443359375, -1.9130859375, 327 2.75, -0.7421875, 4.85546875, -0.75634765625 328 ], 329 'descriptor': {shape: [24], dataType: 'float16'} 330 } 331 }, 332 'operators': [{ 333 'name': 'tanh', 334 'arguments': [{'input': 'tanhInput'}], 335 'outputs': 'tanhOutput' 336 }], 337 'expectedOutputs': { 338 'tanhOutput': { 339 'data': [ 340 1, 341 -0.81884765625, 342 0.38623046875, 343 0.94970703125, 344 0.99365234375, 345 -1, 346 0.99951171875, 347 -0.99951171875, 348 -0.9990234375, 349 0.72021484375, 350 -0.11041259765625, 351 1, 352 0.94482421875, 353 0.99951171875, 354 1, 355 -1, 356 0.888671875, 357 -1, 358 -0.89453125, 359 -0.95751953125, 360 0.99169921875, 361 -0.63037109375, 362 1, 363 -0.63916015625 364 ], 365 'descriptor': {shape: [24], dataType: 'float16'} 366 } 367 } 368 } 369 }, 370 { 371 'name': 'tanh float16 2D tensor', 372 'graph': { 373 'inputs': { 374 'tanhInput': { 375 'data': [ 376 5.47265625, -1.1533203125, 0.407470703125, 1.830078125, 377 2.869140625, -4.5703125, 4.1484375, -4.06640625, 378 -3.712890625, 0.90771484375, -0.11083984375, 5.95703125, 379 1.783203125, 4.0234375, 5.5859375, -5.28125, 380 1.4150390625, -5.70703125, -1.4443359375, -1.9130859375, 381 2.75, -0.7421875, 4.85546875, -0.75634765625 382 ], 383 'descriptor': {shape: [4, 6], dataType: 'float16'} 384 } 385 }, 386 'operators': [{ 387 'name': 'tanh', 388 'arguments': [{'input': 'tanhInput'}], 389 'outputs': 'tanhOutput' 390 }], 391 'expectedOutputs': { 392 'tanhOutput': { 393 'data': [ 394 1, 395 -0.81884765625, 396 0.38623046875, 397 0.94970703125, 398 0.99365234375, 399 -1, 400 0.99951171875, 401 -0.99951171875, 402 -0.9990234375, 403 0.72021484375, 404 -0.11041259765625, 405 1, 406 0.94482421875, 407 0.99951171875, 408 1, 409 -1, 410 0.888671875, 411 -1, 412 -0.89453125, 413 -0.95751953125, 414 0.99169921875, 415 -0.63037109375, 416 1, 417 -0.63916015625 418 ], 419 'descriptor': {shape: [4, 6], dataType: 'float16'} 420 } 421 } 422 } 423 }, 424 { 425 'name': 'tanh float16 3D tensor', 426 'graph': { 427 'inputs': { 428 'tanhInput': { 429 'data': [ 430 5.47265625, -1.1533203125, 0.407470703125, 1.830078125, 431 2.869140625, -4.5703125, 4.1484375, -4.06640625, 432 -3.712890625, 0.90771484375, -0.11083984375, 5.95703125, 433 1.783203125, 4.0234375, 5.5859375, -5.28125, 434 1.4150390625, -5.70703125, -1.4443359375, -1.9130859375, 435 2.75, -0.7421875, 4.85546875, -0.75634765625 436 ], 437 'descriptor': {shape: [2, 3, 4], dataType: 'float16'} 438 } 439 }, 440 'operators': [{ 441 'name': 'tanh', 442 'arguments': [{'input': 'tanhInput'}], 443 'outputs': 'tanhOutput' 444 }], 445 'expectedOutputs': { 446 'tanhOutput': { 447 'data': [ 448 1, 449 -0.81884765625, 450 0.38623046875, 451 0.94970703125, 452 0.99365234375, 453 -1, 454 0.99951171875, 455 -0.99951171875, 456 -0.9990234375, 457 0.72021484375, 458 -0.11041259765625, 459 1, 460 0.94482421875, 461 0.99951171875, 462 1, 463 -1, 464 0.888671875, 465 -1, 466 -0.89453125, 467 -0.95751953125, 468 0.99169921875, 469 -0.63037109375, 470 1, 471 -0.63916015625 472 ], 473 'descriptor': {shape: [2, 3, 4], dataType: 'float16'} 474 } 475 } 476 } 477 }, 478 { 479 'name': 'tanh float16 4D tensor', 480 'graph': { 481 'inputs': { 482 'tanhInput': { 483 'data': [ 484 5.47265625, -1.1533203125, 0.407470703125, 1.830078125, 485 2.869140625, -4.5703125, 4.1484375, -4.06640625, 486 -3.712890625, 0.90771484375, -0.11083984375, 5.95703125, 487 1.783203125, 4.0234375, 5.5859375, -5.28125, 488 1.4150390625, -5.70703125, -1.4443359375, -1.9130859375, 489 2.75, -0.7421875, 4.85546875, -0.75634765625 490 ], 491 'descriptor': {shape: [2, 2, 2, 3], dataType: 'float16'} 492 } 493 }, 494 'operators': [{ 495 'name': 'tanh', 496 'arguments': [{'input': 'tanhInput'}], 497 'outputs': 'tanhOutput' 498 }], 499 'expectedOutputs': { 500 'tanhOutput': { 501 'data': [ 502 1, 503 -0.81884765625, 504 0.38623046875, 505 0.94970703125, 506 0.99365234375, 507 -1, 508 0.99951171875, 509 -0.99951171875, 510 -0.9990234375, 511 0.72021484375, 512 -0.11041259765625, 513 1, 514 0.94482421875, 515 0.99951171875, 516 1, 517 -1, 518 0.888671875, 519 -1, 520 -0.89453125, 521 -0.95751953125, 522 0.99169921875, 523 -0.63037109375, 524 1, 525 -0.63916015625 526 ], 527 'descriptor': {shape: [2, 2, 2, 3], dataType: 'float16'} 528 } 529 } 530 } 531 }, 532 { 533 'name': 'tanh float16 5D tensor', 534 'graph': { 535 'inputs': { 536 'tanhInput': { 537 'data': [ 538 5.47265625, -1.1533203125, 0.407470703125, 1.830078125, 539 2.869140625, -4.5703125, 4.1484375, -4.06640625, 540 -3.712890625, 0.90771484375, -0.11083984375, 5.95703125, 541 1.783203125, 4.0234375, 5.5859375, -5.28125, 542 1.4150390625, -5.70703125, -1.4443359375, -1.9130859375, 543 2.75, -0.7421875, 4.85546875, -0.75634765625 544 ], 545 'descriptor': {shape: [2, 1, 4, 1, 3], dataType: 'float16'} 546 } 547 }, 548 'operators': [{ 549 'name': 'tanh', 550 'arguments': [{'input': 'tanhInput'}], 551 'outputs': 'tanhOutput' 552 }], 553 'expectedOutputs': { 554 'tanhOutput': { 555 'data': [ 556 1, 557 -0.81884765625, 558 0.38623046875, 559 0.94970703125, 560 0.99365234375, 561 -1, 562 0.99951171875, 563 -0.99951171875, 564 -0.9990234375, 565 0.72021484375, 566 -0.11041259765625, 567 1, 568 0.94482421875, 569 0.99951171875, 570 1, 571 -1, 572 0.888671875, 573 -1, 574 -0.89453125, 575 -0.95751953125, 576 0.99169921875, 577 -0.63037109375, 578 1, 579 -0.63916015625 580 ], 581 'descriptor': {shape: [2, 1, 4, 1, 3], dataType: 'float16'} 582 } 583 } 584 } 585 } 586 ]; 587 588 webnn_conformance_test(tanhTests, buildAndExecuteGraph, getPrecisionTolerance);