tor-browser

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

sign.https.any.js (4936B)


      1 // META: title=test WebNN API sign 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 // Represents the sign operation that return elementwise -1/0/1 depending on
     13 // element sign.
     14 //
     15 // MLOperand sign(MLOperand input, optional MLOperatorOptions options = {});
     16 
     17 
     18 const getSignPrecisionTolerance = (graphResources) => {
     19  return {metricType: 'ULP', value: 0};
     20 };
     21 
     22 const signTests = [
     23  {
     24    'name': 'sign float32 1D constant tensor',
     25    'graph': {
     26      'inputs': {
     27        'signInput': {
     28          'data': [
     29            -0.946033775806427, 0.9996118545532227, 0.21998752653598785,
     30            -0.22639396786689758
     31          ],
     32          'descriptor': {shape: [4], dataType: 'float32'},
     33          'constant': true
     34        }
     35      },
     36      'operators': [{
     37        'name': 'sign',
     38        'arguments': [{'input': 'signInput'}],
     39        'outputs': 'signOutput'
     40      }],
     41      'expectedOutputs': {
     42        'signOutput': {
     43          'data': [-1, 1, 1, -1],
     44          'descriptor': {shape: [4], dataType: 'float32'}
     45        }
     46      }
     47    }
     48  },
     49  {
     50    'name': 'sign float16 1D tensor',
     51    'graph': {
     52      'inputs': {
     53        'signInput': {
     54          'data': [
     55            -0.946033775806427, 0.9996118545532227, 0.21998752653598785,
     56            -0.22639396786689758
     57          ],
     58          'descriptor': {shape: [4], dataType: 'float16'}
     59        }
     60      },
     61      'operators': [{
     62        'name': 'sign',
     63        'arguments': [{'input': 'signInput'}],
     64        'outputs': 'signOutput'
     65      }],
     66      'expectedOutputs': {
     67        'signOutput': {
     68          'data': [-1, 1, 1, -1],
     69          'descriptor': {shape: [4], dataType: 'float16'}
     70        }
     71      }
     72    }
     73  },
     74  {
     75    'name': 'sign float32 1D tensor',
     76    'graph': {
     77      'inputs': {
     78        'signInput': {
     79          'data': [
     80            -0.946033775806427, 0.9996118545532227, 0.21998752653598785, 0.0
     81          ],
     82          'descriptor': {shape: [4], dataType: 'float32'}
     83        }
     84      },
     85      'operators': [{
     86        'name': 'sign',
     87        'arguments': [{'input': 'signInput'}],
     88        'outputs': 'signOutput'
     89      }],
     90      'expectedOutputs': {
     91        'signOutput': {
     92          'data': [-1, 1, 1, 0],
     93          'descriptor': {shape: [4], dataType: 'float32'}
     94        }
     95      }
     96    }
     97  },
     98  {
     99    'name': 'sign float32 1D tensor with -infinity and +infinity',
    100    'graph': {
    101      'inputs': {
    102        'signInput': {
    103          'data': [-0.946033775806427, 0.9996118545532227, -Infinity, Infinity],
    104          'descriptor': {shape: [4], dataType: 'float32'}
    105        }
    106      },
    107      'operators': [{
    108        'name': 'sign',
    109        'arguments': [{'input': 'signInput'}],
    110        'outputs': 'signOutput'
    111      }],
    112      'expectedOutputs': {
    113        'signOutput': {
    114          'data': [-1, 1, -1, 1],
    115          'descriptor': {shape: [4], dataType: 'float32'}
    116        }
    117      }
    118    }
    119  },
    120  {
    121    'name': 'sign int32 2D tensor',
    122    'graph': {
    123      'inputs': {
    124        'signInput': {
    125          // int32 range: [/* -(2**31) */ -2147483648, /* 2**31 - 1 */ 2147483647]
    126          'data': [-2147483648, 0, 2147483646, 2147483647],
    127          'descriptor': {shape: [2, 2], dataType: 'int32'}
    128        }
    129      },
    130      'operators': [{
    131        'name': 'sign',
    132        'arguments': [{'input': 'signInput'}],
    133        'outputs': 'signOutput'
    134      }],
    135      'expectedOutputs': {
    136        'signOutput': {
    137          'data': [-1, 0, 1, 1],
    138          'descriptor': {shape: [2, 2], dataType: 'int32'}
    139        }
    140      }
    141    }
    142  },
    143  {
    144    'name': 'sign int64 3D tensor',
    145    'graph': {
    146      'inputs': {
    147        'signInput': {
    148          'data': [-1, 0, 1, 2, -2, -1, 0, 1],
    149          'descriptor': {shape: [2, 2, 2], dataType: 'int64'}
    150        }
    151      },
    152      'operators': [{
    153        'name': 'sign',
    154        'arguments': [{'input': 'signInput'}],
    155        'outputs': 'signOutput'
    156      }],
    157      'expectedOutputs': {
    158        'signOutput': {
    159          'data': [-1, 0, 1, 1, -1, -1, 0, 1],
    160          'descriptor': {shape: [2, 2, 2], dataType: 'int64'}
    161        }
    162      }
    163    }
    164  },
    165  {
    166    'name': 'sign int8 4D tensor',
    167    'graph': {
    168      'inputs': {
    169        'signInput': {
    170          // int8 range: [/* -(2**7) */ -128, /* 2**7 - 1 */ 127]
    171          'data': [-128, 0, 1, 2, -2, -1, 0, 127],
    172          'descriptor': {shape: [1, 2, 2, 2], dataType: 'int8'}
    173        }
    174      },
    175      'operators': [{
    176        'name': 'sign',
    177        'arguments': [{'input': 'signInput'}],
    178        'outputs': 'signOutput'
    179      }],
    180      'expectedOutputs': {
    181        'signOutput': {
    182          'data': [-1, 0, 1, 1, -1, -1, 0, 1],
    183          'descriptor': {shape: [1, 2, 2, 2], dataType: 'int8'}
    184        }
    185      }
    186    }
    187  },
    188 ];
    189 
    190 webnn_conformance_test(
    191    signTests, buildAndExecuteGraph, getSignPrecisionTolerance);