summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webnn/validation_tests/pooling-and-reduction-keep-dims.https.any.js
blob: 9f6b9fb3384249ee9b5bd9b3453d1700d703a38e (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
// META: title=validation tests for pooling and reduction operators keep dimensions
// META: global=window,dedicatedworker
// META: script=../resources/utils.js
// META: script=../resources/utils_validation.js
// META: timeout=long

'use strict';

// This is used to reproduce an issue(crbug.com/331841268) of averagePool2d in
// ResNetV2 50 model.
//      [input]
//         |
//  [globalAveragePool]
//         |
//     [conv2d]
//         |
//     [reshape]
//         |
//     [output]
promise_test(async t => {
  const avgPool2dInputShape = [1, 7, 7, 2048];
  const avgPool2dInput = builder.input(
      `avgPool2dInput`, {dataType: 'float32', dimensions: avgPool2dInputShape});
  const avgPool2dOutput =
      builder.averagePool2d(avgPool2dInput, {layout: 'nhwc'});
  const conv2dFilterShape = [1001, 1, 1, 2048];
  const conv2dFilter = builder.constant(
      {dataType: 'float32', dimensions: conv2dFilterShape},
      new Float32Array(sizeOfShape(conv2dFilterShape)).fill(1));
  const conv2dBias = builder.constant(
      {dataType: 'float32', dimensions: [1001]},
      new Float32Array(1001).fill(0.01));
  const conv2dOutput = builder.conv2d(avgPool2dOutput, conv2dFilter, {
    inputLayout: 'nhwc',
    filterLayout: 'ohwi',
    padding: [0, 0, 0, 0],
    bias: conv2dBias
  });
  const newShape = [1, 1001];
  const reshapeOutput = builder.reshape(conv2dOutput, newShape);
  assert_equals(reshapeOutput.dataType(), avgPool2dInput.dataType());
  assert_array_equals(reshapeOutput.shape(), newShape);
  const graph = await builder.build({reshapeOutput});
  const result = await context.compute(
      graph, {
        'avgPool2dInput':
            new Float32Array(sizeOfShape(avgPool2dInputShape)).fill(0.1)
      },
      {'reshapeOutput': new Float32Array(1001)});
}, 'Test global average pool operator\'s output shape for ResNetV2 50 model.');

// This is used to reproduce an issue(crbug.com/331841268) of reduceMean in
// ResNetV2 50 model.
//      [input]
//         |
//    [reduceMean]
//         |
//     [conv2d]
//         |
//     [reshape]
//         |
//     [output]
promise_test(async t => {
  const reduceMeanInputShape = [1, 7, 7, 2048];
  const reduceMeanInput = builder.input(
      `reduceMeanInput`,
      {dataType: 'float32', dimensions: reduceMeanInputShape});
  const reduceMeanOutput =
      builder.reduceMean(reduceMeanInput, {axes: [1, 2], keepDimensions: true});
  const conv2dFilterShape = [1001, 1, 1, 2048];
  const conv2dFilter = builder.constant(
      {dataType: 'float32', dimensions: conv2dFilterShape},
      new Float32Array(sizeOfShape(conv2dFilterShape)).fill(1));
  const conv2dBias = builder.constant(
      {dataType: 'float32', dimensions: [1001]},
      new Float32Array(1001).fill(0.01));
  const conv2dOutput = builder.conv2d(reduceMeanOutput, conv2dFilter, {
    inputLayout: 'nhwc',
    filterLayout: 'ohwi',
    padding: [0, 0, 0, 0],
    bias: conv2dBias
  });
  const newShape = [1, 1001];
  const reshapeOutput = builder.reshape(conv2dOutput, newShape);
  assert_equals(reshapeOutput.dataType(), reduceMeanInput.dataType());
  assert_array_equals(reshapeOutput.shape(), newShape);
  const graph = await builder.build({reshapeOutput});
  const result = await context.compute(
      graph, {
        'reduceMeanInput':
            new Float32Array(sizeOfShape(reduceMeanInputShape)).fill(0.1)
      },
      {'reshapeOutput': new Float32Array(1001)});
}, 'Test reduceMean operator\'s output shape for ResNetV2 50 model.');