| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #include "eliminate_maxpool_indices.h" |
| 16 | |
| 17 | #include <algorithm> |
| 18 | #include "pass_level2.h" |
| 19 | |
| 20 | namespace pnnx { |
| 21 | |
| 22 | void eliminate_maxpool_indices(Graph& graph) |
| 23 | { |
| 24 | while (1) |
| 25 | { |
| 26 | bool matched = false; |
| 27 | |
| 28 | for (size_t i = 0; i < graph.ops.size(); i++) |
| 29 | { |
| 30 | Operator* op = graph.ops[i]; |
| 31 | |
| 32 | if (op->type != "F.adaptive_max_pool1d" && op->type != "F.adaptive_max_pool2d" && op->type != "F.adaptive_max_pool3d" |
| 33 | && op->type != "F.max_pool1d" && op->type != "F.max_pool2d" && op->type != "F.max_pool3d" |
| 34 | && op->type != "nn.AdaptiveMaxPool1d" && op->type != "nn.AdaptiveMaxPool2d" && op->type != "nn.AdaptiveMaxPool3d" |
| 35 | && op->type != "nn.MaxPool1d" && op->type != "nn.MaxPool2d" && op->type != "nn.MaxPool3d") |
| 36 | continue; |
| 37 | |
| 38 | if (op->outputs.size() != 2) |
| 39 | continue; |
| 40 | |
| 41 | if (op->params.find("return_indices") == op->params.end()) |
| 42 | continue; |
| 43 | |
| 44 | if (op->params.at("return_indices").b == false) |
| 45 | continue; |
| 46 | |
| 47 | Operand* op_indices = op->outputs[1]; |
| 48 | |
| 49 | if (!op_indices->consumers.empty()) |
| 50 | continue; |
| 51 | |
| 52 | matched = true; |
| 53 | |
| 54 | op->params["return_indices"] = false; |
| 55 | op->outputs.resize(1); |
| 56 | |
| 57 | op_indices->producer = 0; |
| 58 | |
| 59 | graph.operands.erase(std::find(graph.operands.begin(), graph.operands.end(), op_indices)); |
| 60 | delete op_indices; |
| 61 | |
| 62 | break; |
| 63 | } |
| 64 | |
| 65 | if (!matched) |
| 66 | break; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | } // namespace pnnx |