summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webnn/resources/utils_validation.js
blob: 7f1d4a4a9457e6a782063a17c10ea601f765adee (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
'use strict';

// https://webmachinelearning.github.io/webnn/#enumdef-mloperanddatatype
const allWebNNOperandDataTypes = [
  'float32',
  'float16',
  'int32',
  'uint32',
  'int64',
  'uint64',
  'int8',
  'uint8'
];

const unsignedLongType = 'unsigned long';

const dimensions0D = [];
const dimensions1D = [2];
const dimensions2D = [2, 3];
const dimensions3D = [2, 3, 4];
const dimensions4D = [2, 3, 4, 5];
const dimensions5D = [2, 3, 4, 5, 6];

const adjustOffsetsArray = [
  // Decrease 1
  -1,
  // Increase 1
  1
];

// TODO
// Add more 5+ dimensions
const allWebNNDimensionsArray = [
  dimensions0D,
  dimensions1D,
  dimensions2D,
  dimensions3D,
  dimensions4D,
  dimensions5D
];

const notUnsignedLongAxisArray = [
  // String
  'abc',
  // BigInt
  BigInt(100),
  // Object
  {
    value: 1
  },
  // Array Object
  [0, 1],
  // Date Object
  new Date("2024-01-01"),
];

function getRank(inputDimensions) {
  return inputDimensions.length;
}

function getAxisArray(inputDimensions) {
  return Array.from({length: inputDimensions.length}, (_, i) => i);
}

function getAxesArrayContainSameValues(inputDimensions) {
  // TODO
  // Currently this function returns an array containing each element which all have the same value.
  // For example axes: [0, 1, 2] for 3D input tensor
  // this function returns
  // [
  //   // two values are same
  //   [0, 0],
  //   [1, 1],
  //   [2, 2],
  //   // three values are same
  //   [0, 0, 0],
  //   [1, 1, 1]
  //   [2, 2, 2]
  // ]
  // while it should return
  // [
  //   // two values are same
  //   [0, 0],
  //   [1, 1],
  //   [2, 2],
  //   [0, 0, 1],
  //   [0, 0, 2],
  //   [0, 1, 0],
  //   [0, 2, 0],
  //   [1, 0, 0],
  //   [2, 0, 0],
  //   [1, 1, 0],
  //   [1, 1, 2],
  //   [1, 0, 1],
  //   [1, 2, 1],
  //   [0, 1, 1],
  //   [2, 1, 1],
  //   [2, 2, 0],
  //   [2, 2, 1],
  //   [2, 0, 2],
  //   [2, 1, 2],
  //   [0, 2, 2],
  //   [1, 2, 2],
  //   // three (all) values are same
  //   [0, 0, 0],
  //   [1, 1, 1]
  //   [2, 2, 2]
  // ]
  const axesArrayContainSameValues = [];
  const length = inputDimensions.length;
  if (length >= 2) {
    const validAxesArrayFull = getAxisArray(inputDimensions);
    for (let index = 0; index < length; index++) {
      axesArrayContainSameValues.push(new Array(2).fill(validAxesArrayFull[index]));
      if (length > 2) {
        axesArrayContainSameValues.push(new Array(3).fill(validAxesArrayFull[index]));
      }
    }
  }
  return axesArrayContainSameValues;
}

function generateUnbroadcastableDimensionsArray(dimensions) {
  // Currently this function returns an array of some unbroadcastable dimensions.
  // for example given dimensions [2, 3, 4]
  // this function returns
  // [
  //   [3, 3, 4],
  //   [2, 2, 4],
  //   [2, 4, 4],
  //   [2, 3, 3],
  //   [2, 3, 5],
  //   [3],
  //   [5],
  //   [1, 3],
  //   [1, 5],
  //   [1, 1, 3],
  //   [1, 1, 5],
  //   [1, 1, 1, 3],
  //   [1, 1, 1, 5],
  // ]
  if (dimensions.every(v => v === 1)) {
    throw new Error(`[${dimensions}] always can be broadcasted`);
  }
  const resultDimensions = [];
  const length = dimensions.length;
  if (!dimensions.slice(0, length - 1).every(v => v === 1)) {
    for (let i = 0; i < length; i++) {
      if (dimensions[i] !== 1) {
        for (let offset of [-1, 1]) {
          const dimensionsB = dimensions.slice();
          dimensionsB[i] += offset;
          if (dimensionsB[i] !== 1) {
            resultDimensions.push(dimensionsB);
          }
        }
      }
    }
  }
  const lastDimensionSize = dimensions[length - 1];
  if (lastDimensionSize !== 1) {
    for (let j = 0; j <= length; j++) {
      if (lastDimensionSize > 2) {
        resultDimensions.push(Array(j).fill(1).concat([lastDimensionSize - 1]));
      }
      resultDimensions.push(Array(j).fill(1).concat([lastDimensionSize + 1]));
    }
  }
  return resultDimensions;
}

function generateOutOfRangeValuesArray(type) {
  let range, outsideValueArray;
  switch (type) {
    case 'unsigned long':
      // https://webidl.spec.whatwg.org/#idl-unsigned-long
      // The unsigned long type is an unsigned integer type that has values in the range [0, 4294967295].
      range = [0, 4294967295];
      break;
    default:
      throw new Error(`Unsupport ${type}`);
  }
  outsideValueArray = [range[0] - 1, range[1] + 1];
  return outsideValueArray;
}

let inputIndex = 0;
let inputAIndex = 0;
let inputBIndex = 0;
let context, builder;

test(() => assert_not_equals(navigator.ml, undefined, "ml property is defined on navigator"));

promise_setup(async () => {
  if (navigator.ml === undefined) {
    return;
  }
  context = await navigator.ml.createContext();
  builder = new MLGraphBuilder(context);
}, {explicit_timeout: true});

function validateTwoInputsBroadcastable(operationName) {
  if (navigator.ml === undefined) {
    return;
  }
  promise_test(async t => {
    for (let dataType of allWebNNOperandDataTypes) {
      for (let dimensions of allWebNNDimensionsArray) {
        if (dimensions.length > 0) {
          const inputA = builder.input(`inputA${++inputAIndex}`, {dataType, dimensions});
          const unbroadcastableDimensionsArray = generateUnbroadcastableDimensionsArray(dimensions);
          for (let unbroadcastableDimensions of unbroadcastableDimensionsArray) {
            const inputB = builder.input(`inputB${++inputBIndex}`, {dataType, dimensions: unbroadcastableDimensions});
            assert_throws_dom('DataError', () => builder[operationName](inputA, inputB));
            assert_throws_dom('DataError', () => builder[operationName](inputB, inputA));
          }
        }
      }
    }
  }, `[${operationName}] DataError is expected if two inputs aren't broadcastable`);
}

function validateTwoInputsOfSameDataType(operationName) {
  if (navigator.ml === undefined) {
    return;
  }
  let operationNameArray;
  if (typeof operationName === 'string') {
    operationNameArray = [operationName];
  } else if (Array.isArray(operationName)) {
    operationNameArray = operationName;
  } else {
    throw new Error(`${operationName} should be an operation name string or an operation name string array`);
  }
  for (let subOperationName of operationNameArray) {
    promise_test(async t => {
      for (let dataType of allWebNNOperandDataTypes) {
        for (let dimensions of allWebNNDimensionsArray) {
          const inputA = builder.input(`inputA${++inputAIndex}`, {dataType, dimensions});
          for (let dataTypeB of allWebNNOperandDataTypes) {
            if (dataType !== dataTypeB) {
              const inputB = builder.input(`inputB${++inputBIndex}`, {dataType: dataTypeB, dimensions});
              assert_throws_dom('DataError', () => builder[subOperationName](inputA, inputB));
            }
          }
        }
      }
    }, `[${subOperationName}] DataError is expected if two inputs aren't of same data type`);
  }
}

/**
 * Validate options.axes by given operation and input rank for
 * argMin/Max / layerNormalization / Reduction operations / resample2d operations
 * @param {(String[]|String)} operationName - An operation name array or an operation name
 * @param {Number} [inputRank]
 */
function validateOptionsAxes(operationName, inputRank) {
  if (navigator.ml === undefined) {
    return;
  }
  let operationNameArray;
  if (typeof operationName === 'string') {
    operationNameArray = [operationName];
  } else if (Array.isArray(operationName)) {
    operationNameArray = operationName;
  } else {
    throw new Error(`${operationName} should be an operation name string or an operation name string array`);
  }
  const invalidAxisArray = generateOutOfRangeValuesArray(unsignedLongType);
  for (let subOperationName of operationNameArray) {
    // TypeError is expected if any of options.axes elements is not an unsigned long interger
    promise_test(async t => {
      if (inputRank === undefined) {
        // argMin/Max / layerNormalization / Reduction operations
        for (let dataType of allWebNNOperandDataTypes) {
          for (let dimensions of allWebNNDimensionsArray) {
            const rank = getRank(dimensions);
            if (rank >= 1) {
              const input = builder.input(`input${++inputIndex}`, {dataType, dimensions});
              for (let invalidAxis of invalidAxisArray) {
                assert_throws_js(TypeError, () => builder[subOperationName](input, {axes: invalidAxis}));
              }
              for (let axis of notUnsignedLongAxisArray) {
                assert_false(typeof axis === 'number' && Number.isInteger(axis), `[${subOperationName}] any of options.axes elements should be of 'unsigned long'`);
                assert_throws_js(TypeError, () => builder[subOperationName](input, {axes: [axis]}));
              }
            }
          }
        }
      } else {
        // resample2d
        for (let dataType of allWebNNOperandDataTypes) {
          const input = builder.input(`input${++inputIndex}`, {dataType, dimensions: allWebNNDimensionsArray[inputRank]});
          for (let invalidAxis of invalidAxisArray) {
            assert_throws_js(TypeError, () => builder[subOperationName](input, {axes: invalidAxis}));
          }
          for (let axis of notUnsignedLongAxisArray) {
            assert_false(typeof axis === 'number' && Number.isInteger(axis), `[${subOperationName}]  any of options.axes elements should be of 'unsigned long'`);
            assert_throws_js(TypeError, () => builder[subOperationName](input, {axes: [axis]}));
          }
        }
      }
    }, `[${subOperationName}] TypeError is expected if any of options.axes elements is not an unsigned long interger`);

    // DataError is expected if any of options.axes elements is greater or equal to the size of input
    promise_test(async t => {
      if (inputRank === undefined) {
        // argMin/Max / layerNormalization / Reduction operations
        for (let dataType of allWebNNOperandDataTypes) {
          for (let dimensions of allWebNNDimensionsArray) {
            const rank = getRank(dimensions);
            if (rank >= 1) {
              const input = builder.input(`input${++inputIndex}`, {dataType, dimensions});
              assert_throws_dom('DataError', () => builder[subOperationName](input, {axes: [rank]}));
              assert_throws_dom('DataError', () => builder[subOperationName](input, {axes: [rank + 1]}));
            }
          }
        }
      } else {
        // resample2d
        for (let dataType of allWebNNOperandDataTypes) {
          const input = builder.input(`input${++inputIndex}`, {dataType, dimensions: allWebNNDimensionsArray[inputRank]});
          assert_throws_dom('DataError', () => builder[subOperationName](input, {axes: [inputRank]}));
          assert_throws_dom('DataError', () => builder[subOperationName](input, {axes: [inputRank + 1]}));
        }
      }
    }, `[${subOperationName}] DataError is expected if any of options.axes elements is greater or equal to the size of input`);

    // DataError is expected if two or more values are same in the axes sequence
    promise_test(async t => {
      if (inputRank === undefined) {
        // argMin/Max / layerNormalization / Reduction operations
        for (let dataType of allWebNNOperandDataTypes) {
          for (let dimensions of allWebNNDimensionsArray) {
            const rank = getRank(dimensions);
            if (rank >= 2) {
              const input = builder.input(`input${++inputIndex}`, {dataType, dimensions});
              const axesArrayContainSameValues = getAxesArrayContainSameValues(dimensions);
              for (let axes of axesArrayContainSameValues) {
                assert_throws_dom('DataError', () => builder[subOperationName](input, {axes}));
              }
            }
          }
        }
      } else {
        // resample2d
        for (let dataType of allWebNNOperandDataTypes) {
          const dimensions = allWebNNDimensionsArray[inputRank];
          const input = builder.input(`input${++inputIndex}`, {dataType, dimensions});
          const axesArrayContainSameValues = getAxesArrayContainSameValues(dimensions);
          for (let axes of axesArrayContainSameValues) {
            assert_throws_dom('DataError', () => builder[subOperationName](input, {axes}));
          }
        }
      }
    }, `[${subOperationName}] DataError is expected if two or more values are same in the axes sequence`);
  }
}