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.
19 #include "ONNXHelpers.h"
20 #include "AttributeHelpers.h"
21 #include "ConvPoolHelpers.h"
23 #include "mir/ops/MaxPool2DOp.h"
28 void convertMaxPoolV1(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("MaxPool: 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("MaxPool: 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("MaxPool: attribute 'kernel_shape' has incorrect size.");
51 std::vector<std::int32_t> padding_before;
52 std::vector<std::int32_t> padding_after;
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("MaxPool: 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::MaxPool2DOpAttributes attributes;
70 attributes.window = kernel_shape;
71 attributes.strides = strides;
72 attributes.padding_before = padding_before;
73 attributes.padding_after = padding_after;
74 attributes.data_format = mir::DataFormat::NCHW;
75 auto result = createOp<mir::ops::MaxPool2DOp>(graph, input, attributes)->getOutput(0);
77 context->setNodeOutputs(onnx_node, {result});
80 void convertMaxPoolV8(const onnx::NodeProto &onnx_node, ConverterContext *context)
82 const auto storage_order = getAttributeValue<int64_t>(onnx_node, "storage_order", 0);
83 if (storage_order != 0)
84 throw std::runtime_error("Not supported storage order attribute!");
86 convertMaxPoolV1(onnx_node, context);
89 void convertMaxPoolV10(const onnx::NodeProto &onnx_node, ConverterContext *context)
91 const auto ceil_mode = getAttributeValue<int64_t>(onnx_node, "ceil_mode", 0);
93 throw std::runtime_error("Not supported ceil_mode attribute!");
95 const auto *dilations = findAttribute(onnx_node, "dilations");
96 if (dilations != nullptr)
98 // check default (=1) dilations on each spatial axis
99 for (auto index = 0; index < dilations->ints_size(); index++)
100 if (dilations->ints(index) != 1)
101 throw std::runtime_error("Not supported dilations in MaxPool operation!");
104 convertMaxPoolV8(onnx_node, context);
107 } // namespace mir_onnx