summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webnn/validation_tests/matmul.https.any.js
blob: 8db16242c9efb5612123333eb80d93167d754503 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// META: title=validation tests for WebNN API matmul operation
// META: global=window,dedicatedworker
// META: script=../resources/utils_validation.js

'use strict';

validateTwoInputsFromMultipleBuilders('matmul');

const tests = [
  {
    name: '[matmul] Throw if first input\'s rank is less than 2',
    inputs: {
      a: {dataType: 'float32', dimensions: [2]},
      b: {dataType: 'float32', dimensions: [2, 2]}
    }
  },
  {
    name: '[matmul] Throw if second input\'s rank is less than 2',
    inputs: {
      a: {dataType: 'float32', dimensions: [2, 2]},
      b: {dataType: 'float32', dimensions: [2]}
    }
  },
  {
    name: '[matmul] Test with 2-D input and 4-D input',
    inputs: {
      a: {dataType: 'float32', dimensions: [1, 4]},
      b: {dataType: 'float32', dimensions: [2, 2, 4, 2]}
    },
    output: {dataType: 'float32', dimensions: [2, 2, 1, 2]}
  },
  {
    name: '[matmul] Test with 2-D input and 2-D input',
    inputs: {
      a: {dataType: 'float32', dimensions: [4, 2]},
      b: {dataType: 'float32', dimensions: [2, 3]}
    },
    output: {dataType: 'float32', dimensions: [4, 3]}
  },
  {
    // batchShape is a clone of inputShape with the spatial dimensions
    // (last 2 items) removed.
    name:
        '[matmul] Test with 3-D input and 3-D input of broadcastable batchShape',
    inputs: {
      a: {dataType: 'float32', dimensions: [2, 3, 4]},
      b: {dataType: 'float32', dimensions: [1, 4, 1]}
    },
    output: {dataType: 'float32', dimensions: [2, 3, 1]}
  },
  {
    // batchShape is a clone of inputShape with the spatial dimensions
    // (last 2 items) removed.
    name:
        '[matmul] Test with 4-D input and 3-D input of broadcastable batchShape',
    inputs: {
      a: {dataType: 'float32', dimensions: [2, 2, 3, 4]},
      b: {dataType: 'float32', dimensions: [1, 4, 5]}
    },
    output: {dataType: 'float32', dimensions: [2, 2, 3, 5]}
  },
  {
    name: '[matmul] Test with 3-D input and 3-D input',
    inputs: {
      a: {dataType: 'float32', dimensions: [2, 3, 4]},
      b: {dataType: 'float32', dimensions: [2, 4, 5]}
    },
    output: {dataType: 'float32', dimensions: [2, 3, 5]}
  },
  {
    name: '[matmul] Throw if the input data type is not floating point',
    inputs: {
      a: {dataType: 'uint32', dimensions: [2, 3, 4]},
      b: {dataType: 'uint32', dimensions: [2, 4, 5]}
    }
  },
  {
    name: '[matmul] Throw if data type of two inputs don\'t match',
    inputs: {
      a: {dataType: 'float32', dimensions: [2, 3, 4]},
      b: {dataType: 'float16', dimensions: [2, 4, 5]}
    }
  },
  {
    name:
        '[matmul] Throw if columns of first input\'s shape doesn\'t match the rows of second input\'s shape',
    inputs: {
      a: {dataType: 'float32', dimensions: /* [rows, columns] */[2, 3]},
      b: {dataType: 'float32', dimensions: /* [rows, columns] */[2, 4]}
    },
  },
  {
    // batchShape is a clone of inputShape with the spatial dimensions
    // (last 2 items) removed.
    name: '[matmul] Throw if batchShapes aren\'t bidirectionally broadcastable',
    inputs: {
      a: {dataType: 'float32', dimensions: [3, 3, 4]},
      b: {dataType: 'float32', dimensions: [2, 4, 1]}
    },
  },
];

tests.forEach(test => promise_test(async t => {
                const inputA = builder.input('a', {
                  dataType: test.inputs.a.dataType,
                  dimensions: test.inputs.a.dimensions
                });
                const inputB = builder.input('b', {
                  dataType: test.inputs.b.dataType,
                  dimensions: test.inputs.b.dimensions
                });
                if (test.output) {
                  const output = builder.matmul(inputA, inputB);
                  assert_equals(output.dataType(), test.output.dataType);
                  assert_array_equals(output.shape(), test.output.dimensions);
                } else {
                  assert_throws_js(
                      TypeError, () => builder.matmul(inputA, inputB));
                }
              }, test.name));