summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webnn/validation_tests/gemm.https.any.js
blob: abe0ba61936b0f11a6eade254a3e58a2ec29d11b (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// META: title=validation tests for WebNN API gemm operation
// META: global=window,dedicatedworker
// META: script=../resources/utils_validation.js

'use strict';

const kExampleInputDescriptor = {
  dataType: 'float32',
  dimensions: [2, 2]
};

validateTwoInputsFromMultipleBuilders('gemm');

multi_builder_test(async (t, builder, otherBuilder) => {
  const cFromOtherBuilder = otherBuilder.input('c', kExampleInputDescriptor);
  const options = {c: cFromOtherBuilder};

  const a = builder.input('a', kExampleInputDescriptor);
  const b = builder.input('b', kExampleInputDescriptor);
  assert_throws_js(TypeError, () => builder.gemm(a, b, options));
}, '[gemm] throw if c option is from another builder');

const tests = [
  {
    name: '[gemm] Test building gemm with default option.',
    a: {dataType: 'float32', dimensions: [2, 3]},
    b: {dataType: 'float32', dimensions: [3, 4]},
    output: {dataType: 'float32', dimensions: [2, 4]}
  },
  {
    name:
        '[gemm] Throw if inputShapeA[1] is not equal to inputShapeB[0] default options.',
    a: {dataType: 'float32', dimensions: [2, 3]},
    b: {dataType: 'float32', dimensions: [2, 4]},
  },
  {
    name: '[gemm] Test building gemm with aTranspose=true.',
    a: {dataType: 'float32', dimensions: [2, 3]},
    b: {dataType: 'float32', dimensions: [2, 4]},
    options: {
      aTranspose: true,
    },
    output: {dataType: 'float32', dimensions: [3, 4]}
  },
  {
    name:
        '[gemm] Throw if inputShapeA[0] is not equal to inputShapeB[0] with aTranspose=true.',
    a: {dataType: 'float32', dimensions: [2, 3]},
    b: {dataType: 'float32', dimensions: [3, 4]},
    options: {
      aTranspose: true,
    },
  },
  {
    name: '[gemm] Test building gemm with bTranspose=true.',
    a: {dataType: 'float32', dimensions: [2, 3]},
    b: {dataType: 'float32', dimensions: [4, 3]},
    options: {
      bTranspose: true,
    },
    output: {dataType: 'float32', dimensions: [2, 4]}
  },
  {
    name:
        '[gemm] Throw if inputShapeA[0] is not equal to inputShapeB[0] with bTranspose=true.',
    a: {dataType: 'float32', dimensions: [2, 3]},
    b: {dataType: 'float32', dimensions: [3, 4]},
    options: {
      bTranspose: true,
    },
  },
  {
    name: '[gemm] Throw if the rank of inputA is not 2.',
    a: {dataType: 'float32', dimensions: [2, 3, 1]},
    b: {dataType: 'float32', dimensions: [2, 4]},
  },
  {
    name: '[gemm] Throw if the rank of inputB is not 2.',
    a: {dataType: 'float32', dimensions: [2, 4]},
    b: {dataType: 'float32', dimensions: [2, 3, 1]},
  },
  {
    name: '[gemm] Throw if data types of two inputs do not match.',
    a: {dataType: 'float32', dimensions: [2, 3]},
    b: {dataType: 'float16', dimensions: [3, 4]},
  },
  {
    name: '[gemm] Test building gemm with inputC.',
    a: {dataType: 'float32', dimensions: [2, 3]},
    b: {dataType: 'float32', dimensions: [3, 4]},
    options: {
      c: {dataType: 'float32', dimensions: [4]},
    },
    output: {dataType: 'float32', dimensions: [2, 4]}
  },
  {
    name: '[gemm] Test building gemm with scalar inputC.',
    a: {dataType: 'float32', dimensions: [2, 3]},
    b: {dataType: 'float32', dimensions: [3, 4]},
    options: {
      c: {dataType: 'float32', dimensions: []},
    },
    output: {dataType: 'float32', dimensions: [2, 4]}
  },
  {
    name:
        '[gemm] Throw if inputShapeC is not unidirectionally broadcastable to the output shape [inputShapeA[0], inputShapeB[1]].',
    a: {dataType: 'float32', dimensions: [2, 3]},
    b: {dataType: 'float32', dimensions: [3, 4]},
    options: {
      c: {dataType: 'float32', dimensions: [2, 3]},
    },
  },
  {
    name: '[gemm] Throw if the input data type is not floating point.',
    a: {dataType: 'int32', dimensions: [2, 3]},
    b: {dataType: 'int32', dimensions: [3, 4]}
  },
  {
    name:
        '[gemm] Throw if data type of inputC does not match ones of inputA and inputB.',
    a: {dataType: 'float32', dimensions: [3, 2]},
    b: {dataType: 'float32', dimensions: [4, 3]},
    options: {
      c: {dataType: 'float16', dimensions: [2, 4]},
      aTranspose: true,
      bTranspose: true,
    },
  },
  {
    name: '[gemm] Throw if the rank of inputC is 3.',
    a: {dataType: 'float32', dimensions: [3, 2]},
    b: {dataType: 'float32', dimensions: [4, 3]},
    options: {
      c: {dataType: 'float32', dimensions: [2, 3, 4]},
      aTranspose: true,
      bTranspose: true,
    },
  },
];

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