2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "AveragePool.h"
19 #include "ONNXHelpers.h"
20 #include "AttributeHelpers.h"
21 #include "ConvPoolHelpers.h"
23 #include "mir/ops/AvgPool2DOp.h"
28 void convertAveragePoolV1(const onnx::NodeProto &onnx_node, ConverterContext *context)
30 std::vector<mir::Operation::Output *> inputs = context->getNodeInputs(onnx_node);
31 mir::Graph *graph = context->getGraph();
33 assert(inputs.size() == 1);
34 auto input = inputs[0];
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.");
40 constexpr int num_spatial_dims = 2;
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.");
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.");
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"))
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());
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,
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);
78 context->setNodeOutputs(onnx_node, {result});
81 void convertAveragePoolV7(const onnx::NodeProto &onnx_node, ConverterContext *context)
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!");
87 convertAveragePoolV1(onnx_node, context);
90 void convertAveragePoolV10(const onnx::NodeProto &onnx_node, ConverterContext *context)
92 const auto ceil_mode = getAttributeValue<int64_t>(onnx_node, "ceil_mode", 0);
94 throw std::runtime_error("Not supported ceil_mode attribute!");
96 convertAveragePoolV7(onnx_node, context);
99 } // namespace mir_onnx