tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

softmax.https.any.js (14165B)


      1 // META: title=test WebNN API softmax 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-softmax-method
     12 // Compute the softmax values of the N-D input tensor along the given axis.
     13 //
     14 // MLOperand softmax(MLOperand input, unsigned long axis);
     15 
     16 const softmaxTests = [
     17  {
     18    'name': 'softmax float32 2D constant tensor all positive',
     19    'graph': {
     20      'inputs': {
     21        'softmaxInput': {
     22          'data': [
     23            7.9037346839904785, 6.358251571655273,   4.833756923675537,
     24            9.5791654586792,    0.21071857213974,    4.554958820343018,
     25            7.150174140930176,  8.330297470092773,   1.5359858274459839,
     26            6.63361930847168,   1.4539369344711304,  0.213418647646904,
     27            5.257819652557373,  8.192137718200684,   8.16172981262207,
     28            2.874434232711792,  8.950733184814453,   6.111632823944092,
     29            1.6371468305587769, 0.27626121044158936, 5.02822732925415,
     30            3.8983259201049805, 2.8967113494873047,  6.88947057723999
     31          ],
     32          'descriptor': {shape: [4, 6], dataType: 'float32'},
     33          'constant': true
     34        }
     35      },
     36      'operators': [{
     37        'name': 'softmax',
     38        'arguments': [{'input': 'softmaxInput'}, {'axis': 1}],
     39        'outputs': 'softmaxOutput'
     40      }],
     41      'expectedOutputs': {
     42        'softmaxOutput': {
     43          'data': [
     44            0.15068615972995758,    0.03212761878967285,
     45            0.006995180621743202,   0.8048291206359863,
     46            0.00006871300138300285, 0.005293202120810747,
     47            0.2057899534702301,     0.6698001027107239,
     48            0.0007502624066546559,  0.1227685883641243,
     49            0.0006911618984304368,  0.00019990770670119673,
     50            0.012398251332342625,   0.23319464921951294,
     51            0.22621041536331177,    0.0011435872875154018,
     52            0.4979347288608551,     0.029118351638317108,
     53            0.004253828432410955,   0.001090824487619102,
     54            0.12633030116558075,    0.040812913328409195,
     55            0.014990009367465973,   0.8125221133232117
     56          ],
     57          'descriptor': {shape: [4, 6], dataType: 'float32'}
     58        }
     59      }
     60    }
     61  },
     62  {
     63    'name': 'softmax float32 2D tensor all positive',
     64    'graph': {
     65      'inputs': {
     66        'softmaxInput': {
     67          'data': [
     68            7.9037346839904785, 6.358251571655273,   4.833756923675537,
     69            9.5791654586792,    0.21071857213974,    4.554958820343018,
     70            7.150174140930176,  8.330297470092773,   1.5359858274459839,
     71            6.63361930847168,   1.4539369344711304,  0.213418647646904,
     72            5.257819652557373,  8.192137718200684,   8.16172981262207,
     73            2.874434232711792,  8.950733184814453,   6.111632823944092,
     74            1.6371468305587769, 0.27626121044158936, 5.02822732925415,
     75            3.8983259201049805, 2.8967113494873047,  6.88947057723999
     76          ],
     77          'descriptor': {shape: [4, 6], dataType: 'float32'}
     78        }
     79      },
     80      'operators': [{
     81        'name': 'softmax',
     82        'arguments': [{'input': 'softmaxInput'}, {'axis': 1}],
     83        'outputs': 'softmaxOutput'
     84      }],
     85      'expectedOutputs': {
     86        'softmaxOutput': {
     87          'data': [
     88            0.15068615972995758,    0.03212761878967285,
     89            0.006995180621743202,   0.8048291206359863,
     90            0.00006871300138300285, 0.005293202120810747,
     91            0.2057899534702301,     0.6698001027107239,
     92            0.0007502624066546559,  0.1227685883641243,
     93            0.0006911618984304368,  0.00019990770670119673,
     94            0.012398251332342625,   0.23319464921951294,
     95            0.22621041536331177,    0.0011435872875154018,
     96            0.4979347288608551,     0.029118351638317108,
     97            0.004253828432410955,   0.001090824487619102,
     98            0.12633030116558075,    0.040812913328409195,
     99            0.014990009367465973,   0.8125221133232117
    100          ],
    101          'descriptor': {shape: [4, 6], dataType: 'float32'}
    102        }
    103      }
    104    }
    105  },
    106  {
    107    'name': 'softmax float32 2D tensor all negative',
    108    'graph': {
    109      'inputs': {
    110        'softmaxInput': {
    111          'data': [
    112            -3.3118433952331543, -3.3389549255371094, -3.4102790355682373,
    113            -6.697193145751953,  -7.896223545074463,  -3.308168888092041,
    114            -3.2309720516204834, -4.315771579742432,  -9.311088562011719,
    115            -3.9236626625061035, -3.780721426010132,  -6.034926891326904,
    116            -3.9196677207946777, -2.2234842777252197, -9.326531410217285,
    117            -1.4882491827011108, -6.302842617034912,  -5.53147554397583,
    118            -1.8421411514282227, -4.994808197021484,  -9.527292251586914,
    119            -4.985682964324951,  -8.421041488647461,  -6.235629558563232
    120          ],
    121          'descriptor': {shape: [4, 6], dataType: 'float32'}
    122        }
    123      },
    124      'operators': [{
    125        'name': 'softmax',
    126        'arguments': [{'input': 'softmaxInput'}, {'axis': 1}],
    127        'outputs': 'softmaxOutput'
    128      }],
    129      'expectedOutputs': {
    130        'softmaxOutput': {
    131          'data': [
    132            0.2546302080154419,   0.24781952798366547,   0.2307596504688263,
    133            0.008623254485428333, 0.002599793951958418,  0.2555675804615021,
    134            0.40352678298950195,  0.13637976348400116,   0.0009232329903170466,
    135            0.20185552537441254,  0.23287305235862732,   0.024441635236144066,
    136            0.0551743283867836,   0.3008708655834198,    0.0002474947541486472,
    137            0.6276082992553711,   0.0050902292132377625, 0.011008745059370995,
    138            0.9090295433998108,   0.0388500951230526,    0.00041779119055718184,
    139            0.039206232875585556, 0.0012629841221496463, 0.011233373545110226
    140          ],
    141          'descriptor': {shape: [4, 6], dataType: 'float32'}
    142        }
    143      }
    144    }
    145  },
    146  {
    147    'name': 'softmax float32 3D constant tensor',
    148    'graph': {
    149      'inputs': {
    150        'softmaxInput': {
    151          'data': [
    152            0.4301910996437073, 0.5471914410591125, -1.1637765169143677,
    153            0.18390046060085297, 0.583903968334198, 0.17356790602207184,
    154            0.5397239923477173, -0.9535139799118042, -0.5920282602310181,
    155            -0.17344485223293304, 0.14395014941692352, -0.37920907139778137
    156          ],
    157          'descriptor': {shape: [1, 3, 4], dataType: 'float32'},
    158          'constant': true
    159        }
    160      },
    161      'operators': [{
    162        'name': 'softmax',
    163        'arguments': [{'input': 'softmaxInput'}, {'axis': 1}],
    164        'outputs': 'softmaxOutput'
    165      }],
    166      'expectedOutputs': {
    167        'softmaxOutput': {
    168          'data': [
    169            0.39589041471481323, 0.45983806252479553, 0.09812675416469574,
    170            0.529077410697937, 0.4616699814796448, 0.31647709012031555,
    171            0.5390242338180542, 0.16964708268642426, 0.142439603805542,
    172            0.22368484735488892, 0.36284899711608887, 0.3012755215167999
    173          ],
    174          'descriptor': {shape: [1, 3, 4], dataType: 'float32'}
    175        }
    176      }
    177    }
    178  },
    179  {
    180    'name': 'softmax float32 4D tensor',
    181    'graph': {
    182      'inputs': {
    183        'softmaxInput': {
    184          'data': [
    185            0.4301910996437073, 0.5471914410591125, -1.1637765169143677,
    186            0.18390046060085297, 0.583903968334198, 0.17356790602207184,
    187            0.5397239923477173, -0.9535139799118042, -0.5920282602310181,
    188            -0.17344485223293304, 0.14395014941692352, -0.37920907139778137
    189          ],
    190          'descriptor': {shape: [3, 4, 1, 1], dataType: 'float32'}
    191        }
    192      },
    193      'operators': [{
    194        'name': 'softmax',
    195        'arguments': [{'input': 'softmaxInput'}, {'axis': 1}],
    196        'outputs': 'softmaxOutput'
    197      }],
    198      'expectedOutputs': {
    199        'softmaxOutput': {
    200          'data': [
    201            0.3216537833213806, 0.3615773916244507, 0.06533370912075043,
    202            0.25143513083457947, 0.35271573066711426, 0.23400123417377472,
    203            0.33747196197509766, 0.07581108063459396, 0.17110128700733185,
    204            0.26004093885421753, 0.3571779429912567, 0.2116798311471939
    205          ],
    206          'descriptor': {shape: [3, 4, 1, 1], dataType: 'float32'}
    207        }
    208      }
    209    }
    210  },
    211 
    212  // float16 tests
    213  {
    214    'name': 'softmax float16 2D constant tensor all positive',
    215    'graph': {
    216      'inputs': {
    217        'softmaxInput': {
    218          'data': [
    219            7.90234375,     6.359375,   4.83203125,   9.578125,
    220            0.210693359375, 4.5546875,  7.1484375,    8.328125,
    221            1.5361328125,   6.6328125,  1.4541015625, 0.21337890625,
    222            5.2578125,      8.1953125,  8.1640625,    2.875,
    223            8.953125,       6.11328125, 1.63671875,   0.2763671875,
    224            5.02734375,     3.8984375,  2.896484375,  6.890625
    225          ],
    226          'descriptor': {shape: [4, 6], dataType: 'float16'},
    227          'constant': true
    228        }
    229      },
    230      'operators': [{
    231        'name': 'softmax',
    232        'arguments': [{'input': 'softmaxInput'}, {'axis': 1}],
    233        'outputs': 'softmaxOutput'
    234      }],
    235      'expectedOutputs': {
    236        'softmaxOutput': {
    237          'data': [
    238            0.150634765625,         0.032196044921875,
    239            0.006988525390625,      0.8046875,
    240            0.00006878376007080078, 0.005298614501953125,
    241            0.205810546875,         0.66943359375,
    242            0.0007519721984863281,  0.1229248046875,
    243            0.0006923675537109375,  0.0002002716064453125,
    244            0.01236724853515625,    0.2333984375,
    245            0.2261962890625,        0.0011415481567382812,
    246            0.497802734375,         0.0290985107421875,
    247            0.00424957275390625,    0.0010900497436523438,
    248            0.1260986328125,        0.040771484375,
    249            0.01497650146484375,    0.81298828125
    250          ],
    251          'descriptor': {shape: [4, 6], dataType: 'float16'}
    252        }
    253      }
    254    }
    255  },
    256  {
    257    'name': 'softmax float16 2D tensor all positive',
    258    'graph': {
    259      'inputs': {
    260        'softmaxInput': {
    261          'data': [
    262            7.90234375,     6.359375,   4.83203125,   9.578125,
    263            0.210693359375, 4.5546875,  7.1484375,    8.328125,
    264            1.5361328125,   6.6328125,  1.4541015625, 0.21337890625,
    265            5.2578125,      8.1953125,  8.1640625,    2.875,
    266            8.953125,       6.11328125, 1.63671875,   0.2763671875,
    267            5.02734375,     3.8984375,  2.896484375,  6.890625
    268          ],
    269          'descriptor': {shape: [4, 6], dataType: 'float16'}
    270        }
    271      },
    272      'operators': [{
    273        'name': 'softmax',
    274        'arguments': [{'input': 'softmaxInput'}, {'axis': 1}],
    275        'outputs': 'softmaxOutput'
    276      }],
    277      'expectedOutputs': {
    278        'softmaxOutput': {
    279          'data': [
    280            0.150634765625,         0.032196044921875,
    281            0.006988525390625,      0.8046875,
    282            0.00006878376007080078, 0.005298614501953125,
    283            0.205810546875,         0.66943359375,
    284            0.0007519721984863281,  0.1229248046875,
    285            0.0006923675537109375,  0.0002002716064453125,
    286            0.01236724853515625,    0.2333984375,
    287            0.2261962890625,        0.0011415481567382812,
    288            0.497802734375,         0.0290985107421875,
    289            0.00424957275390625,    0.0010900497436523438,
    290            0.1260986328125,        0.040771484375,
    291            0.01497650146484375,    0.81298828125
    292          ],
    293          'descriptor': {shape: [4, 6], dataType: 'float16'}
    294        }
    295      }
    296    }
    297  },
    298  {
    299    'name': 'softmax float16 2D tensor all negative',
    300    'graph': {
    301      'inputs': {
    302        'softmaxInput': {
    303          'data': [
    304            -3.3125,     -3.33984375, -3.41015625,  -6.6953125,   -7.89453125,
    305            -3.30859375, -3.23046875, -4.31640625,  -9.3125,      -3.923828125,
    306            -3.78125,    -6.03515625, -3.919921875, -2.22265625,  -9.328125,
    307            -1.48828125, -6.3046875,  -5.53125,     -1.841796875, -4.99609375,
    308            -9.5234375,  -4.984375,   -8.421875,    -6.234375
    309          ],
    310          'descriptor': {shape: [4, 6], dataType: 'float16'}
    311        }
    312      },
    313      'operators': [{
    314        'name': 'softmax',
    315        'arguments': [{'input': 'softmaxInput'}, {'axis': 1}],
    316        'outputs': 'softmaxOutput'
    317      }],
    318      'expectedOutputs': {
    319        'softmaxOutput': {
    320          'data': [
    321            0.254638671875,      0.2476806640625,       0.2308349609375,
    322            0.00864410400390625, 0.002605438232421875,  0.255615234375,
    323            0.40380859375,       0.1363525390625,       0.0009222030639648438,
    324            0.2017822265625,     0.2327880859375,       0.024444580078125,
    325            0.055145263671875,   0.301025390625,        0.00024700164794921875,
    326            0.62744140625,       0.0050811767578125,    0.01100921630859375,
    327            0.9091796875,        0.038787841796875,     0.00041937828063964844,
    328            0.03924560546875,    0.0012617111206054688, 0.0112457275390625
    329          ],
    330          'descriptor': {shape: [4, 6], dataType: 'float16'}
    331        }
    332      }
    333    }
    334  },
    335  {
    336    'name': 'softmax float16 4D tensor',
    337    'graph': {
    338      'inputs': {
    339        'softmaxInput': {
    340          'data': [
    341            0.43017578125, 0.54736328125, -1.1640625, 0.1839599609375,
    342            0.583984375, 0.173583984375, 0.53955078125, -0.95361328125,
    343            -0.591796875, -0.1734619140625, 0.1439208984375, -0.379150390625
    344          ],
    345          'descriptor': {shape: [3, 4, 1, 1], dataType: 'float16'}
    346        }
    347      },
    348      'operators': [{
    349        'name': 'softmax',
    350        'arguments': [{'input': 'softmaxInput'}, {'axis': 1}],
    351        'outputs': 'softmaxOutput'
    352      }],
    353      'expectedOutputs': {
    354        'softmaxOutput': {
    355          'data': [
    356            0.321533203125, 0.361572265625, 0.0653076171875, 0.25146484375,
    357            0.352783203125, 0.2340087890625, 0.33740234375, 0.0758056640625,
    358            0.171142578125, 0.260009765625, 0.357177734375, 0.211669921875
    359          ],
    360          'descriptor': {shape: [3, 4, 1, 1], dataType: 'float16'}
    361        }
    362      }
    363    }
    364  }
    365 ];
    366 
    367 webnn_conformance_test(
    368    softmaxTests, buildAndExecuteGraph, getPrecisionTolerance);