chromium/third_party/blink/web_tests/external/wpt/webnn/validation_tests/prelu.https.any.js

// META: title=validation tests for WebNN API prelu operation
// META: global=window,dedicatedworker
// META: variant=?cpu
// META: variant=?gpu
// META: variant=?npu
// META: script=../resources/utils_validation.js

'use strict';

validateTwoInputsFromMultipleBuilders('prelu');

const tests = [
  {
    name:
        '[prelu] Test slope\'s shape = [3, 2, 5] which is the same as input\'s shape.',
    input: {dataType: 'float32', dimensions: [3, 2, 5]},
    slope: {dataType: 'float32', dimensions: [3, 2, 5]},
    output: {dataType: 'float32', dimensions: [3, 2, 5]},
  },
  {
    name:
        '[prelu] Test slope\'s shape = [5] which is unidirectionally broadcastable to input\'s shape.',
    input: {dataType: 'float32', dimensions: [3, 2, 5]},
    slope: {dataType: 'float32', dimensions: [5]},
    output: {dataType: 'float32', dimensions: [3, 2, 5]},
  },
  {
    name:
        '[prelu] Test slope\'s shape = [] which is unidirectionally broadcastable to input\'s shape.',
    input: {dataType: 'float32', dimensions: [3, 2, 5]},
    slope: {dataType: 'float32', dimensions: []},
    output: {dataType: 'float32', dimensions: [3, 2, 5]},
  },
  {
    name:
        '[prelu] Test slope\'s shape = [2, 5] which is unidirectionally broadcastable to input\'s shape.',
    input: {dataType: 'float32', dimensions: [3, 2, 5]},
    slope: {dataType: 'float32', dimensions: [2, 5]},
    output: {dataType: 'float32', dimensions: [3, 2, 5]},
  },
  {
    name:
        '[prelu] Throw if the shape of slope is not broadcastable to the shape of input.',
    input: {dataType: 'float32', dimensions: [3, 2, 5]},
    slope: {dataType: 'float32', dimensions: [2]},
  },
  {
    name:
        '[prelu] Throw if the data type of slope does not match the data type of input.',
    input: {dataType: 'float32', dimensions: [3, 2, 5]},
    slope: {dataType: 'int32', dimensions: [3, 2, 5]},
  },
];

tests.forEach(
    test => promise_test(async t => {
      const builder = new MLGraphBuilder(context);
      const input = builder.input(
          'input',
          {dataType: test.input.dataType, dimensions: test.input.dimensions});
      const slope = builder.input(
          'input',
          {dataType: test.slope.dataType, dimensions: test.slope.dimensions});
      if (test.output) {
        const output = builder.prelu(input, slope);
        assert_equals(output.dataType(), test.output.dataType);
        assert_array_equals(output.shape(), test.output.dimensions);
      } else {
        const label = 'prelu_123';
        const options = {label};
        const regrexp = new RegExp('\\[' + label + '\\]');
        assert_throws_with_label(
            () => builder.prelu(input, slope, options), regrexp);
      }
    }, test.name));

promise_test(async t => {
  for (let dataType of allWebNNOperandDataTypes) {
    if (!context.opSupportLimits().input.dataTypes.includes(dataType)) {
      continue;
    }
    const builder = new MLGraphBuilder(context);
    const dimensions = [1];
    const input = builder.input(`input`, {dataType, dimensions});
    if (context.opSupportLimits().prelu.input.dataTypes.includes(dataType)) {
      const output = builder.prelu(input, input);
      assert_equals(output.dataType(), dataType);
      assert_array_equals(output.shape(), dimensions);
    } else {
      assert_throws_js(TypeError, () => builder.prelu(input, input));
    }
  }
}, `[prelu] Test prelu with all of the data types.`);