tor-browser

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

cumulative_sum.https.any.js (8339B)


      1 // META: title=test WebNN API cumulativeSum 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://github.com/webmachinelearning/webnn/issues/375#issuecomment-2292466613
     12 // Sums the elements of a tensor along an axis.
     13 //
     14 // dictionary MLCumulativeSumOptions {
     15 //   bool exclusive = false; // Post-sum addition rather than inclusive pre-sum.
     16 //   bool reversed = false; // Reverse the summation direction.
     17 // };
     18 //
     19 // MLOperand cumulativeSum(MLOperand input, unsigned long axis, optional
     20 // MLCumulativeSumOptions options = {});
     21 
     22 const getCumulativeSumPrecisionTolerance = (graphResources) => {
     23  const args = graphResources.operators[0].arguments;
     24  const inputShape =
     25      graphResources.inputs[args[0][Object.keys(args[0])[0]]].descriptor.shape;
     26  const axis = args[1][Object.keys(args[1])[0]];
     27  let tolerance = inputShape[axis] - 1;
     28 
     29  const toleranceValueDict = {float32: tolerance, float16: tolerance, int32: 0};
     30  const expectedDataType =
     31      getExpectedDataTypeOfSingleOutput(graphResources.expectedOutputs);
     32  return {metricType: 'ULP', value: toleranceValueDict[expectedDataType]};
     33 };
     34 
     35 const cumulativeSumTests = [
     36  // float32 tests
     37  {
     38    'name': 'cumulativeSum with float32 input and default options.',
     39    'graph': {
     40      'inputs': {
     41        'cumulativeSumInput': {
     42          'data': [
     43            60.42374038696289, -86.92247772216797, -19.496112823486328,
     44            -15.150615692138672, 13.455190658569336, 45.433597564697266,
     45            61.082862854003906, 70.71882629394531, -31.278579711914062,
     46            56.08354187011719, 38.992767333984375, -3.27536940574646
     47          ],
     48          'descriptor': {shape: [1, 1, 3, 4], dataType: 'float32'}
     49        }
     50      },
     51      'operators': [{
     52        'name': 'cumulativeSum',
     53        'arguments': [
     54          {'input': 'cumulativeSumInput'},
     55          {'axis': 3}
     56        ],
     57        'outputs': 'cumulativeSumOutput'
     58      }],
     59      'expectedOutputs': {
     60        'cumulativeSumOutput': {
     61          'data': [
     62            60.4237404, -26.4987373, -45.994854, -61.1454659, 13.4551907,
     63            58.8887863, 119.9716568, 190.6904907, -31.2785797, 24.8049622,
     64            63.7977295, 60.5223611
     65          ],
     66          'descriptor': {shape: [1, 1, 3, 4], dataType: 'float32'}
     67        }
     68      }
     69    }
     70  },
     71  {
     72    'name': 'cumulativeSum with float32 input and set exclusive to true.',
     73    'graph': {
     74      'inputs': {
     75        'cumulativeSumInput': {
     76          'data': [
     77            60.42374038696289, -86.92247772216797, -19.496112823486328,
     78            -15.150615692138672, 13.455190658569336, 45.433597564697266,
     79            61.082862854003906, 70.71882629394531, -31.278579711914062,
     80            56.08354187011719, 38.992767333984375, -3.27536940574646
     81          ],
     82          'descriptor': {shape: [1, 1, 3, 4], dataType: 'float32'}
     83        }
     84      },
     85      'operators': [{
     86        'name': 'cumulativeSum',
     87        'arguments': [
     88          {'input': 'cumulativeSumInput'},
     89          {'axis': 3},
     90          {'options': {'exclusive': true}}
     91        ],
     92        'outputs': 'cumulativeSumOutput'
     93      }],
     94      'expectedOutputs': {
     95        'cumulativeSumOutput': {
     96          'data': [
     97            0.0, 60.4237404, -26.4987373, -45.994854, 0.0, 13.4551907,
     98            58.8887863, 119.9716568, 0.0, -31.2785797, 24.8049622, 63.7977295
     99          ],
    100          'descriptor': {shape: [1, 1, 3, 4], dataType: 'float32'}
    101        }
    102      }
    103    }
    104  },
    105  {
    106    'name': 'cumulativeSum with float32 input and set reversed to true.',
    107    'graph': {
    108      'inputs': {
    109        'cumulativeSumInput': {
    110          'data': [
    111            60.42374038696289, -86.92247772216797, -19.496112823486328,
    112            -15.150615692138672, 13.455190658569336, 45.433597564697266,
    113            61.082862854003906, 70.71882629394531, -31.278579711914062,
    114            56.08354187011719, 38.992767333984375, -3.27536940574646
    115          ],
    116          'descriptor': {shape: [1, 1, 3, 4], dataType: 'float32'}
    117        }
    118      },
    119      'operators': [{
    120        'name': 'cumulativeSum',
    121        'arguments': [
    122          {'input': 'cumulativeSumInput'},
    123          {'axis': 3},
    124          {'options': {'reversed': true}}
    125        ],
    126        'outputs': 'cumulativeSumOutput'
    127      }],
    128      'expectedOutputs': {
    129        'cumulativeSumOutput': {
    130          'data': [
    131            -61.1454659, -121.5692139, -34.6467285, -15.1506157, 190.6904907,
    132            177.2352905, 131.8016968, 70.7188263, 60.5223618, 91.8009415,
    133            35.7173996, -3.2753694
    134          ],
    135          'descriptor': {shape: [1, 1, 3, 4], dataType: 'float32'}
    136        }
    137      }
    138    }
    139  },
    140 
    141  // float16 tests
    142  {
    143    'name': 'cumulativeSum with float16 input and default options.',
    144    'graph': {
    145      'inputs': {
    146        'cumulativeSumInput': {
    147          'data': [
    148            60.4375, -86.9375, -19.5, -15.1484375, 13.453125, 45.4375, 61.09375,
    149            70.75, -31.28125, 56.09375, 39, -3.275390625
    150          ],
    151          'descriptor': {shape: [1, 1, 3, 4], dataType: 'float16'}
    152        }
    153      },
    154      'operators': [{
    155        'name': 'cumulativeSum',
    156        'arguments': [{'input': 'cumulativeSumInput'}, {'axis': 3}],
    157        'outputs': 'cumulativeSumOutput'
    158      }],
    159      'expectedOutputs': {
    160        'cumulativeSumOutput': {
    161          'data': [
    162            60.4375, -26.5, -46, -61.15625, 13.453125, 58.875, 120, 190.75,
    163            -31.28125, 24.8125, 63.8125, 60.53125
    164          ],
    165          'descriptor': {shape: [1, 1, 3, 4], dataType: 'float16'}
    166        }
    167      }
    168    }
    169  },
    170  {
    171    'name': 'cumulativeSum with float16 input and set exclusive to true.',
    172    'graph': {
    173      'inputs': {
    174        'cumulativeSumInput': {
    175          'data': [
    176            60.4375, -86.9375, -19.5, -15.1484375, 13.453125, 45.4375, 61.09375,
    177            70.75, -31.28125, 56.09375, 39, -3.275390625
    178          ],
    179          'descriptor': {shape: [1, 1, 3, 4], dataType: 'float16'}
    180        }
    181      },
    182      'operators': [{
    183        'name': 'cumulativeSum',
    184        'arguments': [
    185          {'input': 'cumulativeSumInput'}, {'axis': 3},
    186          {'options': {'exclusive': true}}
    187        ],
    188        'outputs': 'cumulativeSumOutput'
    189      }],
    190      'expectedOutputs': {
    191        'cumulativeSumOutput': {
    192          'data': [
    193            0, 60.4375, -26.5, -46, 0, 13.453125, 58.875, 120, 0, -31.28125,
    194            24.8125, 63.8125
    195          ],
    196          'descriptor': {shape: [1, 1, 3, 4], dataType: 'float16'}
    197        }
    198      }
    199    }
    200  },
    201  {
    202    'name': 'cumulativeSum with float16 input and set reversed to true.',
    203    'graph': {
    204      'inputs': {
    205        'cumulativeSumInput': {
    206          'data': [
    207            60.4375, -86.9375, -19.5, -15.1484375, 13.453125, 45.4375, 61.09375,
    208            70.75, -31.28125, 56.09375, 39, -3.275390625
    209          ],
    210          'descriptor': {shape: [1, 1, 3, 4], dataType: 'float16'}
    211        }
    212      },
    213      'operators': [{
    214        'name': 'cumulativeSum',
    215        'arguments': [
    216          {'input': 'cumulativeSumInput'}, {'axis': 3},
    217          {'options': {'reversed': true}}
    218        ],
    219        'outputs': 'cumulativeSumOutput'
    220      }],
    221      'expectedOutputs': {
    222        'cumulativeSumOutput': {
    223          'data': [
    224            -61.15625, -121.5625, -34.65625, -15.1484375, 190.75, 177.25,
    225            131.875, 70.75, 60.53125, 91.8125, 35.71875, -3.275390625
    226          ],
    227          'descriptor': {shape: [1, 1, 3, 4], dataType: 'float16'}
    228        }
    229      }
    230    }
    231  },
    232 
    233  // int32 tests
    234  {
    235    'name': 'cumulativeSum with int32 input and axis = 2.',
    236    'graph': {
    237      'inputs': {
    238        'cumulativeSumInput': {
    239          'data': [2, 1, 3, 5, 3, 8, 7, 3, 9, 6, 2, 4],
    240          'descriptor': {shape: [1, 1, 3, 4], dataType: 'int32'}
    241        }
    242      },
    243      'operators': [{
    244        'name': 'cumulativeSum',
    245        'arguments': [
    246          {'input': 'cumulativeSumInput'},
    247          {'axis': 2}
    248        ],
    249        'outputs': 'cumulativeSumOutput'
    250      }],
    251      'expectedOutputs': {
    252        'cumulativeSumOutput': {
    253          'data': [2, 1, 3, 5, 5, 9, 10, 8, 14, 15, 12, 12],
    254          'descriptor': {shape: [1, 1, 3, 4], dataType: 'int32'}
    255        }
    256      }
    257    }
    258  }
    259 ];
    260 
    261 webnn_conformance_test(
    262    cumulativeSumTests, buildAndExecuteGraph,
    263    getCumulativeSumPrecisionTolerance);