Source code

Revision control

Copy as Markdown

Other Tools

Test Info: Warnings

// META: title=test WebNN API prelu operation
// META: global=window,dedicatedworker
// META: variant=?cpu
// META: variant=?gpu
// META: variant=?npu
// META: script=../resources/utils.js
// META: timeout=long
'use strict';
// Calculate the parametric version of rectified linear function (Parametric
// ReLU) on the input tensor element-wise. The calculation follows the
// expression max(0, x) + slope * min(0, x).
//
// MLOperand prelu(MLOperand input, MLOperand slope);
const preluTests = [
{
'name': 'prelu float32 0D scalar',
'graph': {
'inputs': {
'preluInput': {
'data': [-4.794857501983643],
'descriptor': {shape: [], dataType: 'float32'},
'constant': true
},
'preluSlope': {
'data': [1.1202747821807861],
'descriptor': {shape: [], dataType: 'float32'},
'constant': true
}
},
'operators': [{
'name': 'prelu',
'arguments': [{'input': 'preluInput'}, {'slope': 'preluSlope'}],
'outputs': 'preluOutput'
}],
'expectedOutputs': {
'preluOutput': {
'data': [-5.371557712554932],
'descriptor': {shape: [], dataType: 'float32'}
}
}
}
},
{
'name': 'prelu float32 1D constant tensors',
'graph': {
'inputs': {
'preluInput': {
'data': [
-2.549168109893799, -4.794857501983643, 8.413617134094238,
6.108623504638672, -8.492292404174805, 3.3143365383148193,
1.1687211990356445, -0.141762837767601, -6.714652061462402,
5.787421703338623, -3.755627393722534, -4.89828634262085,
7.3295159339904785, -3.9542298316955566, 7.067296981811523,
9.439736366271973, -2.558180093765259, -8.658834457397461,
8.47507381439209, 4.551425457000732, -9.267870903015137,
-0.262177437543869, 1.3258955478668213, -7.41831111907959
],
'descriptor': {shape: [24], dataType: 'float32'},
'constant': true
},
'preluSlope': {
'data': [
9.343092918395996, 0.2800687253475189, -4.617084980010986,
1.1202747821807861, -1.4334710836410522, -3.157594919204712,
-6.28995418548584, -5.0107879638671875, -6.899077415466309,
3.5725347995758057, 6.861966609954834, -1.961531400680542,
4.5832037925720215, 2.6643502712249756, 9.192955017089844,
-9.554699897766113, -5.505102157592773, -2.3927369117736816,
3.58212947845459, -2.3224003314971924, -1.9816573858261108,
4.155889987945557, -1.799522042274475, 9.295849800109863
],
'descriptor': {shape: [24], dataType: 'float32'},
'constant': true
}
},
'operators': [{
'name': 'prelu',
'arguments': [{'input': 'preluInput'}, {'slope': 'preluSlope'}],
'outputs': 'preluOutput'
}],
'expectedOutputs': {
'preluOutput': {
'data': [
-23.817113876342773, -1.342889666557312, 8.413617134094238,
6.108623504638672, 12.173455238342285, 3.3143365383148193,
1.1687211990356445, 0.7103435397148132, 46.32490539550781,
5.787421703338623, -25.7709903717041, 9.608142852783203,
7.3295159339904785, -10.535453796386719, 7.067296981811523,
9.439736366271973, 14.083043098449707, 20.718313217163086,
8.47507381439209, 4.551425457000732, 18.365745544433594,
-1.0895805358886719, 1.3258955478668213, -68.95950317382812
],
'descriptor': {shape: [24], dataType: 'float32'}
}
}
}
},
{
'name': 'prelu float32 1D tensors',
'graph': {
'inputs': {
'preluInput': {
'data': [
-2.549168109893799, -4.794857501983643, 8.413617134094238,
6.108623504638672, -8.492292404174805, 3.3143365383148193,
1.1687211990356445, -0.141762837767601, -6.714652061462402,
5.787421703338623, -3.755627393722534, -4.89828634262085,
7.3295159339904785, -3.9542298316955566, 7.067296981811523,
9.439736366271973, -2.558180093765259, -8.658834457397461,
8.47507381439209, 4.551425457000732, -9.267870903015137,
-0.262177437543869, 1.3258955478668213, -7.41831111907959
],
'descriptor': {shape: [24], dataType: 'float32'},
'constant': true
},
'preluSlope': {
'data': [
9.343092918395996, 0.2800687253475189, -4.617084980010986,
1.1202747821807861, -1.4334710836410522, -3.157594919204712,
-6.28995418548584, -5.0107879638671875, -6.899077415466309,
3.5725347995758057, 6.861966609954834, -1.961531400680542,
4.5832037925720215, 2.6643502712249756, 9.192955017089844,
-9.554699897766113, -5.505102157592773, -2.3927369117736816,
3.58212947845459, -2.3224003314971924, -1.9816573858261108,
4.155889987945557, -1.799522042274475, 9.295849800109863
],
'descriptor': {shape: [24], dataType: 'float32'},
'constant': true
}
},
'operators': [{
'name': 'prelu',
'arguments': [{'input': 'preluInput'}, {'slope': 'preluSlope'}],
'outputs': 'preluOutput'
}],
'expectedOutputs': {
'preluOutput': {
'data': [
-23.817113876342773, -1.342889666557312, 8.413617134094238,
6.108623504638672, 12.173455238342285, 3.3143365383148193,
1.1687211990356445, 0.7103435397148132, 46.32490539550781,
5.787421703338623, -25.7709903717041, 9.608142852783203,
7.3295159339904785, -10.535453796386719, 7.067296981811523,
9.439736366271973, 14.083043098449707, 20.718313217163086,
8.47507381439209, 4.551425457000732, 18.365745544433594,
-1.0895805358886719, 1.3258955478668213, -68.95950317382812
],
'descriptor': {shape: [24], dataType: 'float32'}
}
}
}
},
{
'name': 'prelu float32 2D tensors',
'graph': {
'inputs': {
'preluInput': {
'data': [
-2.549168109893799, -4.794857501983643, 8.413617134094238,
6.108623504638672, -8.492292404174805, 3.3143365383148193,
1.1687211990356445, -0.141762837767601, -6.714652061462402,
5.787421703338623, -3.755627393722534, -4.89828634262085,
7.3295159339904785, -3.9542298316955566, 7.067296981811523,
9.439736366271973, -2.558180093765259, -8.658834457397461,
8.47507381439209, 4.551425457000732, -9.267870903015137,
-0.262177437543869, 1.3258955478668213, -7.41831111907959
],
'descriptor': {shape: [4, 6], dataType: 'float32'},
'constant': true
},
'preluSlope': {
'data': [
9.343092918395996, 0.2800687253475189, -4.617084980010986,
1.1202747821807861, -1.4334710836410522, -3.157594919204712,
-6.28995418548584, -5.0107879638671875, -6.899077415466309,
3.5725347995758057, 6.861966609954834, -1.961531400680542,
4.5832037925720215, 2.6643502712249756, 9.192955017089844,
-9.554699897766113, -5.505102157592773, -2.3927369117736816,
3.58212947845459, -2.3224003314971924, -1.9816573858261108,
4.155889987945557, -1.799522042274475, 9.295849800109863
],
'descriptor': {shape: [4, 6], dataType: 'float32'},
'constant': true
}
},
'operators': [{
'name': 'prelu',
'arguments': [{'input': 'preluInput'}, {'slope': 'preluSlope'}],
'outputs': 'preluOutput'
}],
'expectedOutputs': {
'preluOutput': {
'data': [
-23.817113876342773, -1.342889666557312, 8.413617134094238,
6.108623504638672, 12.173455238342285, 3.3143365383148193,
1.1687211990356445, 0.7103435397148132, 46.32490539550781,
5.787421703338623, -25.7709903717041, 9.608142852783203,
7.3295159339904785, -10.535453796386719, 7.067296981811523,
9.439736366271973, 14.083043098449707, 20.718313217163086,
8.47507381439209, 4.551425457000732, 18.365745544433594,
-1.0895805358886719, 1.3258955478668213, -68.95950317382812
],
'descriptor': {shape: [4, 6], dataType: 'float32'}
}
}
}
},
{
'name': 'prelu float32 3D tensors',
'graph': {
'inputs': {
'preluInput': {
'data': [
-2.549168109893799, -4.794857501983643, 8.413617134094238,
6.108623504638672, -8.492292404174805, 3.3143365383148193,
1.1687211990356445, -0.141762837767601, -6.714652061462402,
5.787421703338623, -3.755627393722534, -4.89828634262085,
7.3295159339904785, -3.9542298316955566, 7.067296981811523,
9.439736366271973, -2.558180093765259, -8.658834457397461,
8.47507381439209, 4.551425457000732, -9.267870903015137,
-0.262177437543869, 1.3258955478668213, -7.41831111907959
],
'descriptor': {shape: [2, 3, 4], dataType: 'float32'},
'constant': true
},
'preluSlope': {
'data': [
9.343092918395996, 0.2800687253475189, -4.617084980010986,
1.1202747821807861, -1.4334710836410522, -3.157594919204712,
-6.28995418548584, -5.0107879638671875, -6.899077415466309,
3.5725347995758057, 6.861966609954834, -1.961531400680542,
4.5832037925720215, 2.6643502712249756, 9.192955017089844,
-9.554699897766113, -5.505102157592773, -2.3927369117736816,
3.58212947845459, -2.3224003314971924, -1.9816573858261108,
4.155889987945557, -1.799522042274475, 9.295849800109863
],
'descriptor': {shape: [2, 3, 4], dataType: 'float32'},
'constant': true
}
},
'operators': [{
'name': 'prelu',
'arguments': [{'input': 'preluInput'}, {'slope': 'preluSlope'}],
'outputs': 'preluOutput'
}],
'expectedOutputs': {
'preluOutput': {
'data': [
-23.817113876342773, -1.342889666557312, 8.413617134094238,
6.108623504638672, 12.173455238342285, 3.3143365383148193,
1.1687211990356445, 0.7103435397148132, 46.32490539550781,
5.787421703338623, -25.7709903717041, 9.608142852783203,
7.3295159339904785, -10.535453796386719, 7.067296981811523,
9.439736366271973, 14.083043098449707, 20.718313217163086,
8.47507381439209, 4.551425457000732, 18.365745544433594,
-1.0895805358886719, 1.3258955478668213, -68.95950317382812
],
'descriptor': {shape: [2, 3, 4], dataType: 'float32'}
}
}
}
},
{
'name': 'prelu float32 4D tensors',
'graph': {
'inputs': {
'preluInput': {
'data': [
-2.549168109893799, -4.794857501983643, 8.413617134094238,
6.108623504638672, -8.492292404174805, 3.3143365383148193,
1.1687211990356445, -0.141762837767601, -6.714652061462402,
5.787421703338623, -3.755627393722534, -4.89828634262085,
7.3295159339904785, -3.9542298316955566, 7.067296981811523,
9.439736366271973, -2.558180093765259, -8.658834457397461,
8.47507381439209, 4.551425457000732, -9.267870903015137,
-0.262177437543869, 1.3258955478668213, -7.41831111907959
],
'descriptor': {shape: [2, 2, 2, 3], dataType: 'float32'},
'constant': true
},
'preluSlope': {
'data': [
9.343092918395996, 0.2800687253475189, -4.617084980010986,
1.1202747821807861, -1.4334710836410522, -3.157594919204712,
-6.28995418548584, -5.0107879638671875, -6.899077415466309,
3.5725347995758057, 6.861966609954834, -1.961531400680542,
4.5832037925720215, 2.6643502712249756, 9.192955017089844,
-9.554699897766113, -5.505102157592773, -2.3927369117736816,
3.58212947845459, -2.3224003314971924, -1.9816573858261108,
4.155889987945557, -1.799522042274475, 9.295849800109863
],
'descriptor': {shape: [2, 2, 2, 3], dataType: 'float32'},
'constant': true
}
},
'operators': [{
'name': 'prelu',
'arguments': [{'input': 'preluInput'}, {'slope': 'preluSlope'}],
'outputs': 'preluOutput'
}],
'expectedOutputs': {
'preluOutput': {
'data': [
-23.817113876342773, -1.342889666557312, 8.413617134094238,
6.108623504638672, 12.173455238342285, 3.3143365383148193,
1.1687211990356445, 0.7103435397148132, 46.32490539550781,
5.787421703338623, -25.7709903717041, 9.608142852783203,
7.3295159339904785, -10.535453796386719, 7.067296981811523,
9.439736366271973, 14.083043098449707, 20.718313217163086,
8.47507381439209, 4.551425457000732, 18.365745544433594,
-1.0895805358886719, 1.3258955478668213, -68.95950317382812
],
'descriptor': {shape: [2, 2, 2, 3], dataType: 'float32'}
}
}
}
},
{
'name': 'prelu float32 5D tensors',
'graph': {
'inputs': {
'preluInput': {
'data': [
-2.549168109893799, -4.794857501983643, 8.413617134094238,
6.108623504638672, -8.492292404174805, 3.3143365383148193,
1.1687211990356445, -0.141762837767601, -6.714652061462402,
5.787421703338623, -3.755627393722534, -4.89828634262085,
7.3295159339904785, -3.9542298316955566, 7.067296981811523,
9.439736366271973, -2.558180093765259, -8.658834457397461,
8.47507381439209, 4.551425457000732, -9.267870903015137,
-0.262177437543869, 1.3258955478668213, -7.41831111907959
],
'descriptor': {shape: [2, 2, 1, 2, 3], dataType: 'float32'},
'constant': true
},
'preluSlope': {
'data': [
9.343092918395996, 0.2800687253475189, -4.617084980010986,
1.1202747821807861, -1.4334710836410522, -3.157594919204712,
-6.28995418548584, -5.0107879638671875, -6.899077415466309,
3.5725347995758057, 6.861966609954834, -1.961531400680542,
4.5832037925720215, 2.6643502712249756, 9.192955017089844,
-9.554699897766113, -5.505102157592773, -2.3927369117736816,
3.58212947845459, -2.3224003314971924, -1.9816573858261108,
4.155889987945557, -1.799522042274475, 9.295849800109863
],
'descriptor': {shape: [2, 2, 1, 2, 3], dataType: 'float32'},
'constant': true
}
},
'operators': [{
'name': 'prelu',
'arguments': [{'input': 'preluInput'}, {'slope': 'preluSlope'}],
'outputs': 'preluOutput'
}],
'expectedOutputs': {
'preluOutput': {
'data': [
-23.817113876342773, -1.342889666557312, 8.413617134094238,
6.108623504638672, 12.173455238342285, 3.3143365383148193,
1.1687211990356445, 0.7103435397148132, 46.32490539550781,
5.787421703338623, -25.7709903717041, 9.608142852783203,
7.3295159339904785, -10.535453796386719, 7.067296981811523,
9.439736366271973, 14.083043098449707, 20.718313217163086,
8.47507381439209, 4.551425457000732, 18.365745544433594,
-1.0895805358886719, 1.3258955478668213, -68.95950317382812
],
'descriptor': {shape: [2, 2, 1, 2, 3], dataType: 'float32'}
}
}
}
},
{
'name': 'prelu float32 broadcast 4D x 1D slope',
'graph': {
'inputs': {
'preluInput': {
'data': [
-2.549168109893799, -4.794857501983643, 8.413617134094238,
6.108623504638672, -8.492292404174805, 3.3143365383148193,
1.1687211990356445, -0.141762837767601, -6.714652061462402,
5.787421703338623, -3.755627393722534, -4.89828634262085,
7.3295159339904785, -3.9542298316955566, 7.067296981811523,
9.439736366271973, -2.558180093765259, -8.658834457397461,
8.47507381439209, 4.551425457000732, -9.267870903015137,
-0.262177437543869, 1.3258955478668213, -7.41831111907959
],
'descriptor': {shape: [2, 2, 2, 3], dataType: 'float32'},
'constant': true
},
'preluSlope': {
'data': [5.073923110961914, 0.480774462223053, -7.091750144958496],
'descriptor': {shape: [3], dataType: 'float32'},
'constant': true
}
},
'operators': [{
'name': 'prelu',
'arguments': [{'input': 'preluInput'}, {'slope': 'preluSlope'}],
'outputs': 'preluOutput'
}],
'expectedOutputs': {
'preluOutput': {
'data': [
-12.934283256530762, -2.3052449226379395, 8.413617134094238,
6.108623504638672, -4.082877159118652, 3.3143365383148193,
1.1687211990356445, -0.06815595179796219, 47.61863327026367,
5.787421703338623, -1.8056097030639648, 34.737422943115234,
7.3295159339904785, -1.901092767715454, 7.067296981811523,
9.439736366271973, -1.2299076318740845, 61.40629196166992,
8.47507381439209, 4.551425457000732, 65.72542572021484,
-1.330268144607544, 1.3258955478668213, 52.60881042480469
],
'descriptor': {shape: [2, 2, 2, 3], dataType: 'float32'}
}
}
}
},
{
'name': 'prelu float32 broadcast 4D x 1D slope of shape [1]',
'graph': {
'inputs': {
'preluInput': {
'data': [
-2.549168109893799, -4.794857501983643, 8.413617134094238,
6.108623504638672, -8.492292404174805, 3.3143365383148193,
1.1687211990356445, -0.141762837767601, -6.714652061462402,
5.787421703338623, -3.755627393722534, -4.89828634262085,
7.3295159339904785, -3.9542298316955566, 7.067296981811523,
9.439736366271973, -2.558180093765259, -8.658834457397461,
8.47507381439209, 4.551425457000732, -9.267870903015137,
-0.262177437543869, 1.3258955478668213, -7.41831111907959
],
'descriptor': {shape: [2, 2, 2, 3], dataType: 'float32'},
'constant': true
},
'preluSlope': {
'data': [5.0114545822143555],
'descriptor': {shape: [1], dataType: 'float32'},
'constant': true
}
},
'operators': [{
'name': 'prelu',
'arguments': [{'input': 'preluInput'}, {'slope': 'preluSlope'}],
'outputs': 'preluOutput'
}],
'expectedOutputs': {
'preluOutput': {
'data': [
-12.775040626525879, -24.029211044311523, 8.413617134094238,
6.108623504638672, -42.558738708496094, 3.3143365383148193,
1.1687211990356445, -0.7104380130767822, -33.65017318725586,
5.787421703338623, -18.821155548095703, -24.54753875732422,
7.3295159339904785, -19.816442489624023, 7.067296981811523,
9.439736366271973, -12.82020378112793, -43.39335632324219,
8.47507381439209, 4.551425457000732, -46.44551467895508,
-1.3138903379440308, 1.3258955478668213, -37.17652893066406
],
'descriptor': {shape: [2, 2, 2, 3], dataType: 'float32'}
}
}
}
},
{
'name': 'prelu float32 broadcast 4D x 2D slope',
'graph': {
'inputs': {
'preluInput': {
'data': [
-2.549168109893799, -4.794857501983643, 8.413617134094238,
6.108623504638672, -8.492292404174805, 3.3143365383148193,
1.1687211990356445, -0.141762837767601, -6.714652061462402,
5.787421703338623, -3.755627393722534, -4.89828634262085,
7.3295159339904785, -3.9542298316955566, 7.067296981811523,
9.439736366271973, -2.558180093765259, -8.658834457397461,
8.47507381439209, 4.551425457000732, -9.267870903015137,
-0.262177437543869, 1.3258955478668213, -7.41831111907959
],
'descriptor': {shape: [2, 2, 2, 3], dataType: 'float32'},
'constant': true
},
'preluSlope': {
'data': [
4.874276161193848, -8.501633644104004, 1.1819270849227905,
-9.985190391540527, -4.424202919006348, -6.654683589935303
],
'descriptor': {shape: [2, 3], dataType: 'float32'},
'constant': true
}
},
'operators': [{
'name': 'prelu',
'arguments': [{'input': 'preluInput'}, {'slope': 'preluSlope'}],
'outputs': 'preluOutput'
}],
'expectedOutputs': {
'preluOutput': {
'data': [
-12.425349235534668, 40.764122009277344, 8.413617134094238,
6.108623504638672, 37.571624755859375, 3.3143365383148193,
1.1687211990356445, 1.2052156925201416, -7.936229228973389,
5.787421703338623, 16.615657806396484, 32.5965461730957,
7.3295159339904785, 33.61741256713867, 7.067296981811523,
9.439736366271973, 11.31790828704834, 57.621803283691406,
8.47507381439209, 4.551425457000732, -10.953948020935059,
2.617891550064087, 1.3258955478668213, 49.366512298583984
],
'descriptor': {shape: [2, 2, 2, 3], dataType: 'float32'}
}
}
}
},
{
'name': 'prelu float32 broadcast 4D x 3D slope',
'graph': {
'inputs': {
'preluInput': {
'data': [
-2.549168109893799, -4.794857501983643, 8.413617134094238,
6.108623504638672, -8.492292404174805, 3.3143365383148193,
1.1687211990356445, -0.141762837767601, -6.714652061462402,
5.787421703338623, -3.755627393722534, -4.89828634262085,
7.3295159339904785, -3.9542298316955566, 7.067296981811523,
9.439736366271973, -2.558180093765259, -8.658834457397461,
8.47507381439209, 4.551425457000732, -9.267870903015137,
-0.262177437543869, 1.3258955478668213, -7.41831111907959
],
'descriptor': {shape: [2, 2, 2, 3], dataType: 'float32'},
'constant': true
},
'preluSlope': {
'data': [5.073923110961914, 0.480774462223053, -7.091750144958496],
'descriptor': {shape: [1, 1, 3], dataType: 'float32'},
'constant': true
}
},
'operators': [{
'name': 'prelu',
'arguments': [{'input': 'preluInput'}, {'slope': 'preluSlope'}],
'outputs': 'preluOutput'
}],
'expectedOutputs': {
'preluOutput': {
'data': [
-12.934283256530762, -2.3052449226379395, 8.413617134094238,
6.108623504638672, -4.082877159118652, 3.3143365383148193,
1.1687211990356445, -0.06815595179796219, 47.61863327026367,
5.787421703338623, -1.8056097030639648, 34.737422943115234,
7.3295159339904785, -1.901092767715454, 7.067296981811523,
9.439736366271973, -1.2299076318740845, 61.40629196166992,
8.47507381439209, 4.551425457000732, 65.72542572021484,
-1.330268144607544, 1.3258955478668213, 52.60881042480469
],
'descriptor': {shape: [2, 2, 2, 3], dataType: 'float32'}
}
}
}
},
{
'name': 'prelu float32 broadcast 4D x 4D slope',
'graph': {
'inputs': {
'preluInput': {
'data': [
-2.549168109893799, -4.794857501983643, 8.413617134094238,
6.108623504638672, -8.492292404174805, 3.3143365383148193,
1.1687211990356445, -0.141762837767601, -6.714652061462402,
5.787421703338623, -3.755627393722534, -4.89828634262085,
7.3295159339904785, -3.9542298316955566, 7.067296981811523,
9.439736366271973, -2.558180093765259, -8.658834457397461,
8.47507381439209, 4.551425457000732, -9.267870903015137,
-0.262177437543869, 1.3258955478668213, -7.41831111907959
],
'descriptor': {shape: [2, 2, 2, 3], dataType: 'float32'},
'constant': true
},
'preluSlope': {
'data': [5.0114545822143555],
'descriptor': {shape: [1, 1, 1, 1], dataType: 'float32'},
'constant': true
}
},
'operators': [{
'name': 'prelu',
'arguments': [{'input': 'preluInput'}, {'slope': 'preluSlope'}],
'outputs': 'preluOutput'
}],
'expectedOutputs': {
'preluOutput': {
'data': [
-12.775040626525879, -24.029211044311523, 8.413617134094238,
6.108623504638672, -42.558738708496094, 3.3143365383148193,
1.1687211990356445, -0.7104380130767822, -33.65017318725586,
5.787421703338623, -18.821155548095703, -24.54753875732422,
7.3295159339904785, -19.816442489624023, 7.067296981811523,
9.439736366271973, -12.82020378112793, -43.39335632324219,
8.47507381439209, 4.551425457000732, -46.44551467895508,
-1.3138903379440308, 1.3258955478668213, -37.17652893066406
],
'descriptor': {shape: [2, 2, 2, 3], dataType: 'float32'}
}
}
}
}
];
if (navigator.ml) {
preluTests.forEach((test) => {
webnn_conformance_test(buildAndExecuteGraph, getPrecisionTolerance, test);
});
} else {
test(() => assert_implements(navigator.ml, 'missing navigator.ml'));
}