From b83df4bd373ea6ade9d6442400b1ef4bbb499b76 Mon Sep 17 00:00:00 2001 From: BruceDai Date: Mon, 27 Mar 2023 09:14:59 +0800 Subject: [PATCH] [webnn] Add float32 tests for WebNN prelu op. (#39048) --- webnn/prelu.https.any.js | 10 + webnn/resources/test_data/prelu.json | 783 +++++++++++++++++++++++++++ webnn/resources/utils.js | 1 + 3 files changed, 794 insertions(+) create mode 100644 webnn/prelu.https.any.js create mode 100644 webnn/resources/test_data/prelu.json diff --git a/webnn/prelu.https.any.js b/webnn/prelu.https.any.js new file mode 100644 index 000000000000000..c1b2e9fa2a0afff --- /dev/null +++ b/webnn/prelu.https.any.js @@ -0,0 +1,10 @@ +// META: title=test WebNN API prelu operation +// META: global=window,dedicatedworker +// META: script=./resources/utils.js +// META: timeout=long + +'use strict'; + +// https://webmachinelearning.github.io/webnn/#api-mlgraphbuilder-prelu + +testWebNNOperation('prelu', buildOperationWithTwoInputs); \ No newline at end of file diff --git a/webnn/resources/test_data/prelu.json b/webnn/resources/test_data/prelu.json new file mode 100644 index 000000000000000..f3855b0f4e19b4e --- /dev/null +++ b/webnn/resources/test_data/prelu.json @@ -0,0 +1,783 @@ +{ + "tests": [ + { + "name": "prelu float32 1D tensors", + "inputs": { + "x": { + "shape": [24], + "data": [ + -2.5491681672317235, + -4.794857500523286, + 8.413617520687886, + 6.108623511441287, + -8.492292847462334, + 3.3143364551069876, + 1.1687212422078108, + -0.1417628422981423, + -6.714651930677875, + 5.787421746282195, + -3.7556274239369847, + -4.898286418001732, + 7.329515995865023, + -3.9542297833176665, + 7.067297201348715, + 9.439736453375971, + -2.5581801134299775, + -8.658834185932482, + 8.475073893236225, + 4.551425642121982, + -9.267870491380243, + -0.26217744010265776, + 1.325895534951659, + -7.418311114031475 + ], + "type": "float32" + }, + "slope": { + "shape": [24], + "data": [ + 9.343093097130314, + 0.2800687126385206, + -4.617085001507579, + 1.1202747481570352, + -1.4334710772165593, + -3.157594908065091, + -6.289953987035659, + -5.010787777546328, + -6.899077379471157, + 3.572534824190212, + 6.861966530502535, + -1.9615313732548287, + 4.583203900668886, + 2.664350255679274, + 9.19295531798543, + -9.554700087649, + -5.505102187715796, + -2.3927369956553557, + 3.5821293765315954, + -2.32240037283157, + -1.9816573593209075, + 4.155890213640276, + -1.7995220850663962, + 9.29585020267449 + ], + "type": "float32" + } + }, + "expected": { + "name": "ouput", + "shape": [24], + "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 + ], + "type": "float32" + } + }, + { + "name": "prelu float32 2D tensors", + "inputs": { + "x": { + "shape": [4, 6], + "data": [ + -2.5491681672317235, + -4.794857500523286, + 8.413617520687886, + 6.108623511441287, + -8.492292847462334, + 3.3143364551069876, + 1.1687212422078108, + -0.1417628422981423, + -6.714651930677875, + 5.787421746282195, + -3.7556274239369847, + -4.898286418001732, + 7.329515995865023, + -3.9542297833176665, + 7.067297201348715, + 9.439736453375971, + -2.5581801134299775, + -8.658834185932482, + 8.475073893236225, + 4.551425642121982, + -9.267870491380243, + -0.26217744010265776, + 1.325895534951659, + -7.418311114031475 + ], + "type": "float32" + }, + "slope": { + "shape": [4, 6], + "data": [ + 9.343093097130314, + 0.2800687126385206, + -4.617085001507579, + 1.1202747481570352, + -1.4334710772165593, + -3.157594908065091, + -6.289953987035659, + -5.010787777546328, + -6.899077379471157, + 3.572534824190212, + 6.861966530502535, + -1.9615313732548287, + 4.583203900668886, + 2.664350255679274, + 9.19295531798543, + -9.554700087649, + -5.505102187715796, + -2.3927369956553557, + 3.5821293765315954, + -2.32240037283157, + -1.9816573593209075, + 4.155890213640276, + -1.7995220850663962, + 9.29585020267449 + ], + "type": "float32" + } + }, + "expected": { + "name": "ouput", + "shape": [4, 6], + "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 + ], + "type": "float32" + } + }, + { + "name": "prelu float32 3D tensors", + "inputs": { + "x": { + "shape": [2, 3, 4], + "data": [ + -2.5491681672317235, + -4.794857500523286, + 8.413617520687886, + 6.108623511441287, + -8.492292847462334, + 3.3143364551069876, + 1.1687212422078108, + -0.1417628422981423, + -6.714651930677875, + 5.787421746282195, + -3.7556274239369847, + -4.898286418001732, + 7.329515995865023, + -3.9542297833176665, + 7.067297201348715, + 9.439736453375971, + -2.5581801134299775, + -8.658834185932482, + 8.475073893236225, + 4.551425642121982, + -9.267870491380243, + -0.26217744010265776, + 1.325895534951659, + -7.418311114031475 + ], + "type": "float32" + }, + "slope": { + "shape": [2, 3, 4], + "data": [ + 9.343093097130314, + 0.2800687126385206, + -4.617085001507579, + 1.1202747481570352, + -1.4334710772165593, + -3.157594908065091, + -6.289953987035659, + -5.010787777546328, + -6.899077379471157, + 3.572534824190212, + 6.861966530502535, + -1.9615313732548287, + 4.583203900668886, + 2.664350255679274, + 9.19295531798543, + -9.554700087649, + -5.505102187715796, + -2.3927369956553557, + 3.5821293765315954, + -2.32240037283157, + -1.9816573593209075, + 4.155890213640276, + -1.7995220850663962, + 9.29585020267449 + ], + "type": "float32" + } + }, + "expected": { + "name": "ouput", + "shape": [2, 3, 4], + "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 + ], + "type": "float32" + } + }, + { + "name": "prelu float32 4D tensors", + "inputs": { + "x": { + "shape": [2, 2, 2, 3], + "data": [ + -2.5491681672317235, + -4.794857500523286, + 8.413617520687886, + 6.108623511441287, + -8.492292847462334, + 3.3143364551069876, + 1.1687212422078108, + -0.1417628422981423, + -6.714651930677875, + 5.787421746282195, + -3.7556274239369847, + -4.898286418001732, + 7.329515995865023, + -3.9542297833176665, + 7.067297201348715, + 9.439736453375971, + -2.5581801134299775, + -8.658834185932482, + 8.475073893236225, + 4.551425642121982, + -9.267870491380243, + -0.26217744010265776, + 1.325895534951659, + -7.418311114031475 + ], + "type": "float32" + }, + "slope": { + "shape": [2, 2, 2, 3], + "data": [ + 9.343093097130314, + 0.2800687126385206, + -4.617085001507579, + 1.1202747481570352, + -1.4334710772165593, + -3.157594908065091, + -6.289953987035659, + -5.010787777546328, + -6.899077379471157, + 3.572534824190212, + 6.861966530502535, + -1.9615313732548287, + 4.583203900668886, + 2.664350255679274, + 9.19295531798543, + -9.554700087649, + -5.505102187715796, + -2.3927369956553557, + 3.5821293765315954, + -2.32240037283157, + -1.9816573593209075, + 4.155890213640276, + -1.7995220850663962, + 9.29585020267449 + ], + "type": "float32" + } + }, + "expected": { + "name": "ouput", + "shape": [2, 2, 2, 3], + "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 + ], + "type": "float32" + } + }, + { + "name": "prelu float32 5D tensors", + "inputs": { + "x": { + "shape": [2, 2, 1, 2, 3], + "data": [ + -2.5491681672317235, + -4.794857500523286, + 8.413617520687886, + 6.108623511441287, + -8.492292847462334, + 3.3143364551069876, + 1.1687212422078108, + -0.1417628422981423, + -6.714651930677875, + 5.787421746282195, + -3.7556274239369847, + -4.898286418001732, + 7.329515995865023, + -3.9542297833176665, + 7.067297201348715, + 9.439736453375971, + -2.5581801134299775, + -8.658834185932482, + 8.475073893236225, + 4.551425642121982, + -9.267870491380243, + -0.26217744010265776, + 1.325895534951659, + -7.418311114031475 + ], + "type": "float32" + }, + "slope": { + "shape": [2, 2, 1, 2, 3], + "data": [ + 9.343093097130314, + 0.2800687126385206, + -4.617085001507579, + 1.1202747481570352, + -1.4334710772165593, + -3.157594908065091, + -6.289953987035659, + -5.010787777546328, + -6.899077379471157, + 3.572534824190212, + 6.861966530502535, + -1.9615313732548287, + 4.583203900668886, + 2.664350255679274, + 9.19295531798543, + -9.554700087649, + -5.505102187715796, + -2.3927369956553557, + 3.5821293765315954, + -2.32240037283157, + -1.9816573593209075, + 4.155890213640276, + -1.7995220850663962, + 9.29585020267449 + ], + "type": "float32" + } + }, + "expected": { + "name": "ouput", + "shape": [2, 2, 1, 2, 3], + "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 + ], + "type": "float32" + } + }, + { + "name": "prelu float32 broadcast 4D x 1D slope", + "inputs": { + "x": { + "shape": [2, 2, 2, 3], + "data": [ + -2.5491681672317235, + -4.794857500523286, + 8.413617520687886, + 6.108623511441287, + -8.492292847462334, + 3.3143364551069876, + 1.1687212422078108, + -0.1417628422981423, + -6.714651930677875, + 5.787421746282195, + -3.7556274239369847, + -4.898286418001732, + 7.329515995865023, + -3.9542297833176665, + 7.067297201348715, + 9.439736453375971, + -2.5581801134299775, + -8.658834185932482, + 8.475073893236225, + 4.551425642121982, + -9.267870491380243, + -0.26217744010265776, + 1.325895534951659, + -7.418311114031475 + ], + "type": "float32" + }, + "slope": { + "shape": [1], + "data": [ + 5.0114545056636395 + ], + "type": "float32" + } + }, + "expected": { + "name": "ouput", + "shape": [2, 2, 2, 3], + "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 + ], + "type": "float32" + } + }, + { + "name": "prelu float32 broadcast 4D x 2D slope", + "inputs": { + "x": { + "shape": [2, 2, 2, 3], + "data": [ + -2.5491681672317235, + -4.794857500523286, + 8.413617520687886, + 6.108623511441287, + -8.492292847462334, + 3.3143364551069876, + 1.1687212422078108, + -0.1417628422981423, + -6.714651930677875, + 5.787421746282195, + -3.7556274239369847, + -4.898286418001732, + 7.329515995865023, + -3.9542297833176665, + 7.067297201348715, + 9.439736453375971, + -2.5581801134299775, + -8.658834185932482, + 8.475073893236225, + 4.551425642121982, + -9.267870491380243, + -0.26217744010265776, + 1.325895534951659, + -7.418311114031475 + ], + "type": "float32" + }, + "slope": { + "shape": [2, 3], + "data": [ + 4.8742760041637645, + -8.50163357869285, + 1.1819270477816914, + -9.985190366937573, + -4.424202960837338, + -6.654683521499036 + ], + "type": "float32" + } + }, + "expected": { + "name": "ouput", + "shape": [2, 2, 2, 3], + "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 + ], + "type": "float32" + } + }, + { + "name": "prelu float32 broadcast 4D x 3D slope", + "inputs": { + "x": { + "shape": [2, 2, 2, 3], + "data": [ + -2.5491681672317235, + -4.794857500523286, + 8.413617520687886, + 6.108623511441287, + -8.492292847462334, + 3.3143364551069876, + 1.1687212422078108, + -0.1417628422981423, + -6.714651930677875, + 5.787421746282195, + -3.7556274239369847, + -4.898286418001732, + 7.329515995865023, + -3.9542297833176665, + 7.067297201348715, + 9.439736453375971, + -2.5581801134299775, + -8.658834185932482, + 8.475073893236225, + 4.551425642121982, + -9.267870491380243, + -0.26217744010265776, + 1.325895534951659, + -7.418311114031475 + ], + "type": "float32" + }, + "slope": { + "shape": [1, 1, 3], + "data": [ + 5.073923296191488, + 0.48077445494619653, + -7.091750168010829 + ], + "type": "float32" + } + }, + "expected": { + "name": "ouput", + "shape": [2, 2, 2, 3], + "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 + ], + "type": "float32" + } + }, + { + "name": "prelu float32 broadcast 4D x 4D slope", + "inputs": { + "x": { + "shape": [2, 2, 2, 3], + "data": [ + -2.5491681672317235, + -4.794857500523286, + 8.413617520687886, + 6.108623511441287, + -8.492292847462334, + 3.3143364551069876, + 1.1687212422078108, + -0.1417628422981423, + -6.714651930677875, + 5.787421746282195, + -3.7556274239369847, + -4.898286418001732, + 7.329515995865023, + -3.9542297833176665, + 7.067297201348715, + 9.439736453375971, + -2.5581801134299775, + -8.658834185932482, + 8.475073893236225, + 4.551425642121982, + -9.267870491380243, + -0.26217744010265776, + 1.325895534951659, + -7.418311114031475 + ], + "type": "float32" + }, + "slope": { + "shape": [1, 1, 1, 1], + "data": [ + 5.0114545056636395 + ], + "type": "float32" + } + }, + "expected": { + "name": "ouput", + "shape": [2, 2, 2, 3], + "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 + ], + "type": "float32" + } + } + ] +} \ No newline at end of file diff --git a/webnn/resources/utils.js b/webnn/resources/utils.js index 0c903998f17dda1..1aec133aef00d6e 100644 --- a/webnn/resources/utils.js +++ b/webnn/resources/utils.js @@ -273,6 +273,7 @@ const PrecisionMetrics = { averagePool2d: {ULP: {float32: getAveragePool2dPrecisionTolerance, float16: getAveragePool2dPrecisionTolerance}}, maxPool2d: {ULP: {float32: 0, float16: 0}}, // End Pooling operations + prelu: {ULP: {float32: 1, float16: 1}}, // Begin Reduction operations reduceMax: {ULP: {float32: 0, float16: 0}}, reduceMean: {ULP: {float32: getReductionPrecisionTolerance, float16: getReductionPrecisionTolerance}},