summaryrefslogtreecommitdiff
path: root/compiler/mir/src/ops/DepthwiseConv2DOp.cpp
blob: 0154bcd09cf0967f44d54a2797a164e352408061 (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
/*
 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "mir/ops/DepthwiseConv2DOp.h"

namespace mir
{
namespace ops
{

void DepthwiseConv2DOp::inferOutputTypes()
{
  // Kernel shape: [Hk, Wk, Ci, M].
  const auto &input_shape = getInputShape(0);
  const auto &kernel_shape = getInputShape(1);
  const int batch_dim_index = getDataBatchDimIndex(_attributes.data_format);
  const int channel_dim_index = getDataChannelDimIndex(_attributes.data_format);

  assert(input_shape.rank() == 4);
  assert(kernel_shape.rank() == 4);
  assert(input_shape.dim(channel_dim_index) == kernel_shape.dim(2));
  assert(_attributes.strides.size() == 2);
  assert(_attributes.padding_before.size() == 2);
  assert(_attributes.padding_after.size() == 2);

  Shape output_shape(4);

  output_shape.dim(batch_dim_index) = input_shape.dim(batch_dim_index);
  output_shape.dim(channel_dim_index) = input_shape.dim(channel_dim_index) * kernel_shape.dim(3);

  for (int i = 0; i < 2; i++)
  {
    const int spatial_dim_index = getDataSpatialDimIndex(_attributes.data_format, i);
    const std::int32_t padded_input = input_shape.dim(spatial_dim_index) +
                                      _attributes.padding_before[i] + _attributes.padding_after[i];
    // out_size = ceil((in_size - kernel_size + 1) / stride) =
    //   (in_size - kernel_size + 1 + stride - 1) / stride =
    //   (in_size - kernel_size) / stride + 1
    output_shape.dim(spatial_dim_index) =
        (padded_input - kernel_shape.dim(i)) / _attributes.strides[i] + 1;
  }

  setOutputType(0, {getInput(0)->getElementType(), output_shape});
}

} // namespace ops
} // namespace mir