ONE - On-device Neural Engine
Loading...
Searching...
No Matches
MaxPool.cpp
Go to the documentation of this file.
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 "MaxPool.h"
18
19#include "ONNXHelpers.h"
20#include "AttributeHelpers.h"
21#include "ConvPoolHelpers.h"
22
23#include "mir/ops/MaxPool2DOp.h"
24
25namespace mir_onnx
26{
27
28void convertMaxPoolV1(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("MaxPool: 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("MaxPool: 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("MaxPool: attribute 'kernel_shape' has incorrect size.");
50
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"))
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("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());
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
70 attributes.window = kernel_shape;
71 attributes.strides = strides;
72 attributes.padding_before = padding_before;
73 attributes.padding_after = padding_after;
75 auto result = createOp<mir::ops::MaxPool2DOp>(graph, input, attributes)->getOutput(0);
76
77 context->setNodeOutputs(onnx_node, {result});
78}
79
80void convertMaxPoolV8(const onnx::NodeProto &onnx_node, ConverterContext *context)
81{
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!");
85
86 convertMaxPoolV1(onnx_node, context);
87}
88
89void convertMaxPoolV10(const onnx::NodeProto &onnx_node, ConverterContext *context)
90{
91 const auto ceil_mode = getAttributeValue<int64_t>(onnx_node, "ceil_mode", 0);
92 if (ceil_mode != 0)
93 throw std::runtime_error("Not supported ceil_mode attribute!");
94
95 const auto *dilations = findAttribute(onnx_node, "dilations");
96 if (dilations != nullptr)
97 {
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!");
102 }
103
104 convertMaxPoolV8(onnx_node, context);
105}
106
107} // namespace mir_onnx
void setNodeOutputs(const onnx::NodeProto &onnx_node, const std::vector< mir::Operation::Output * > &outputs)
std::vector< mir::Operation::Output * > getNodeInputs(const onnx::NodeProto &onnx_node) const
void convertMaxPoolV1(const onnx::NodeProto &onnx_node, ConverterContext *context)
Definition MaxPool.cpp:28
const onnx::AttributeProto * findAttribute(const onnx::NodeProto &node, const std::string &name)
T getAttributeValue(const onnx::AttributeProto &attribute)=delete
void convertMaxPoolV10(const onnx::NodeProto &onnx_node, ConverterContext *context)
Definition MaxPool.cpp:89
void convertMaxPoolV8(const onnx::NodeProto &onnx_node, ConverterContext *context)
Definition MaxPool.cpp:80
void inferAutoPadding(const std::string &pad_type, const mir::Shape &input_shape, const std::vector< std::int32_t > &dilations, const std::vector< std::int32_t > &strides, const std::vector< std::int32_t > &window_size, std::vector< std::int32_t > &padding_before, std::vector< std::int32_t > &padding_after)
std::vector< std::int32_t > window
Definition Attributes.h:54
std::vector< std::int32_t > padding_after
Definition Attributes.h:57
std::vector< std::int32_t > strides
Definition Attributes.h:55
std::vector< std::int32_t > padding_before
Definition Attributes.h:56