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