Imported Upstream version 1.4.0
[platform/core/ml/nnfw.git] / compiler / mir / src / mir_onnx_importer / Op / AveragePool.cpp
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "AveragePool.h"
18
19 #include "ONNXHelpers.h"
20 #include "AttributeHelpers.h"
21 #include "ConvPoolHelpers.h"
22
23 #include "mir/ops/AvgPool2DOp.h"
24
25 namespace mir_onnx
26 {
27
28 void convertAveragePoolV1(const onnx::NodeProto &onnx_node, ConverterContext *context)
29 {
30   std::vector<mir::Operation::Output *> inputs = context->getNodeInputs(onnx_node);
31   mir::Graph *graph = context->getGraph();
32
33   assert(inputs.size() == 1);
34   auto input = inputs[0];
35
36   const auto &input_shape = input->getShape();
37   if (input_shape.rank() != 4)
38     throw std::runtime_error("AveragePool: only 2-D input is supported.");
39
40   constexpr int num_spatial_dims = 2;
41
42   const auto strides =
43       getAttributeValue(onnx_node, "strides", std::vector<std::int32_t>(num_spatial_dims, 1));
44   if (strides.size() != num_spatial_dims)
45     throw std::runtime_error("AveragePool: attribute 'strides' has incorrect size.");
46
47   const auto kernel_shape = getAttributeValue<std::vector<std::int32_t>>(onnx_node, "kernel_shape");
48   if (kernel_shape.size() != num_spatial_dims)
49     throw std::runtime_error("AveragePool: attribute 'kernel_shape' has incorrect size.");
50
51   std::vector<std::int32_t> padding_before(num_spatial_dims, 0);
52   std::vector<std::int32_t> padding_after(num_spatial_dims, 0);
53   if (const auto *pads_attr = findAttribute(onnx_node, "pads"))
54   {
55     const auto pads = getAttributeValue<std::vector<std::int32_t>>(*pads_attr);
56     if (pads.size() != num_spatial_dims * 2)
57       throw std::runtime_error("AveragePool: attribute 'pads' has incorrect size.");
58     padding_before.assign(pads.cbegin(), std::next(pads.cbegin(), num_spatial_dims));
59     padding_after.assign(std::next(pads.cbegin(), num_spatial_dims), pads.cend());
60   }
61   else
62   {
63     const auto auto_pad = getAttributeValue<std::string>(onnx_node, "auto_pad", "NOTSET");
64     const std::vector<std::int32_t> dilations(num_spatial_dims, 1);
65     inferAutoPadding(auto_pad, input_shape, dilations, strides, kernel_shape, padding_before,
66                      padding_after);
67   }
68
69   mir::AvgPool2DOpAttributes attributes;
70   attributes.window = kernel_shape;
71   attributes.strides = strides;
72   attributes.padding_before = padding_before;
73   attributes.padding_after = padding_after;
74   attributes.include_pad = false;
75   attributes.data_format = mir::DataFormat::NCHW;
76   auto result = createOp<mir::ops::AvgPool2DOp>(graph, input, attributes)->getOutput(0);
77
78   context->setNodeOutputs(onnx_node, {result});
79 }
80
81 void convertAveragePoolV7(const onnx::NodeProto &onnx_node, ConverterContext *context)
82 {
83   const auto count_include_pad = getAttributeValue<int64_t>(onnx_node, "count_include_pad", 0);
84   if (count_include_pad != 0)
85     throw std::runtime_error("Not supported count_include_pad attribute!");
86
87   convertAveragePoolV1(onnx_node, context);
88 }
89
90 void convertAveragePoolV10(const onnx::NodeProto &onnx_node, ConverterContext *context)
91 {
92   const auto ceil_mode = getAttributeValue<int64_t>(onnx_node, "ceil_mode", 0);
93   if (ceil_mode != 0)
94     throw std::runtime_error("Not supported ceil_mode attribute!");
95
96   convertAveragePoolV7(onnx_node, context);
97 }
98
99 } // namespace mir_onnx