summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webnn/validation_tests/resample2d.https.any.js
blob: de44c6a333a660cfcfcb463759e56ce9f82d66ef (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
// META: title=validation tests for WebNN API resample2d operation
// META: global=window,dedicatedworker
// META: script=../resources/utils_validation.js

'use strict';

// Tests for resample2d(input, options)
const tests = [
  {
    name: '[resample2d] Test building resample2d with default options',
    input: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
    output: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
  },
  {
    name: '[resample2d] Test building resample2d with scales=[2.0, 2.0]',
    input: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
    options: {scales: [2.0, 2.0]},
    output: {dataType: 'float32', dimensions: [1, 1, 4, 8]},
  },
  {
    name: '[resample2d] Test building resample2d with scales=[0.5, 0.5]',
    input: {dataType: 'float32', dimensions: [1, 1, 5, 5]},
    options: {scales: [0.5, 0.5]},
    output: {dataType: 'float32', dimensions: [1, 1, 2, 2]},
  },
  {
    name:
        '[resample2d] Test building resample2d with scales=[0.5, 0.5] and explicit axes=[2, 3]',
    input: {dataType: 'float32', dimensions: [1, 1, 5, 5]},
    options: {scales: [0.5, 0.5], axes: [2, 3]},
    output: {dataType: 'float32', dimensions: [1, 1, 2, 2]},
  },
  {
    name:
        '[resample2d] Test building resample2d with scales=[1.0, 2.0] and axes=[0, 1]',
    input: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
    options: {scales: [1.0, 2.0], axes: [0, 1]},
    output: {dataType: 'float32', dimensions: [1, 2, 2, 4]},
  },
  {
    name:
        '[resample2d] Test building resample2d with scales=[2.0, 2.0] and axes=[1, 2]',
    input: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
    options: {scales: [2.0, 2.0], axes: [1, 2]},
    output: {dataType: 'float32', dimensions: [1, 2, 4, 4]},
  },
  {
    name:
        '[resample2d] Test building resample2d with sizes=[3, 6] ignored scales',
    input: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
    options: {scales: [2.0, 2.0], sizes: [3, 6]},
    output: {dataType: 'float32', dimensions: [1, 1, 3, 6]},
  },
  {
    name: '[resample2d] Throw if the rank of input is not 4',
    input: {dataType: 'float32', dimensions: [2, 4]},
  },
  {
    name: '[resample2d] Throw if the length of scales is not 2',
    input: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
    options: {scales: [1.0, 1.0, 2.0, 2.0]},
  },
  {
    name: '[resample2d] Throw if any scale value is negative',
    input: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
    options: {scales: [1.0, -2.0]},
  },
  {
    name: '[resample2d] Throw if any scale value is 0',
    input: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
    options: {scales: [0, 2.0]},
  },
  {
    name: '[resample2d] Throw if the length of sizes is not 2',
    input: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
    options: {sizes: [1, 1, 4, 6]},
  },
  {
    name:
        '[resample2d] Throw if any size value is out of \'unsigned long\' value range',
    input: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
    options: {sizes: [kMaxUnsignedLong + 1, kMaxUnsignedLong + 1]},
  },
  {
    name:
        '[resample2d] Throw if outputHeight being floor(scaleHeight*inputHeight) is too large',
    input: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
    // The maximum dimension size is kMaxUnsignedLong (2 ** 32 - 1).
    // Here scaleHeight=kMaxUnsignedLong and inputHeight=2,
    // so outputHeight being kMaxUnsignedLong*2 > kMaxUnsignedLong .
    options: {scales: /*[scaleHeight, scaleWidth]*/[kMaxUnsignedLong, 1]},
  },
  {
    name: '[resample2d] Throw if scaleHeight is too small',
    input: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
    // Here scaleHeight=0.02 and inputHeight=2,
    // so outputHeight would be 0.
    // Link to https://github.com/webmachinelearning/webnn/issues/391.
    options: {scales: /*[scaleHeight, scaleWidth]*/[0.02, 0.8]},
  },
  {
    name:
        '[resample2d] Throw if outputWidth being floor(scaleWidth*inputWidth) is too large',
    input: {dataType: 'float32', dimensions: [1, 1, 4, 2]},
    // The maximum dimension size is kMaxUnsignedLong (2 ** 32 - 1).
    // Here scaleWidth=kMaxUnsignedLong and inputWidth=2,
    // so outputWidth being kMaxUnsignedLong*2 > kMaxUnsignedLong .
    options: {scales: /*[scaleHeight, scaleWidth]*/[1, kMaxUnsignedLong]},
  },
  {
    name: '[resample2d] Throw if scaleWidth is too small',
    input: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
    // Here scaleWidth=0.1 and inputWidth=4,
    // so outputWidth would be 0.
    // Link to https://github.com/webmachinelearning/webnn/issues/391.
    options: {scales: /*[scaleHeight, scaleWidth]*/[0.7, 0.1]},
  },
  {
    name: '[resample2d] Throw if the length of axes is not 2',
    input: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
    options: {axes: [0, 1, 2]},
  },
  {
    name:
        '[resample2d] Throw if any axis value is greater than or equal to the input rank',
    input: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
    options: {axes: [3, 4]},
  },
  {
    // The valid values in the axes sequence are [0, 1], [1, 2] or [2, 3]
    name: '[resample2d] Throw if the values of axes are inconsecutive',
    input: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
    options: {axes: [0, 2]},
  },
  {
    name: '[resample2d] Throw if the values of axes are same',
    input: {dataType: 'float32', dimensions: [1, 1, 2, 4]},
    options: {axes: [0, 0]},
  },
];

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

validateInputFromAnotherBuilder(
    'resample2d', {dataType: 'float32', dimensions: [2, 2, 2, 2]});