| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2022 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 "insert_reshape_pooling.h" |
| 16 | #include "pass_ncnn.h" |
| 17 | |
| 18 | namespace pnnx { |
| 19 | |
| 20 | namespace ncnn { |
| 21 | |
| 22 | void insert_reshape_pooling(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 != "nn.MaxPool1d" && op->type != "nn.MaxPool2d" && op->type != "nn.MaxPool3d") |
| 33 | continue; |
| 34 | |
| 35 | int input_rank = op->inputs[0]->shape.size(); |
| 36 | if (input_rank == 0) |
| 37 | continue; |
| 38 | |
| 39 | // nn.MaxPool1d 2d-3d-2d |
| 40 | // nn.MaxPool2d 3d-4d-3d |
| 41 | // nn.MaxPool3d 4d-5d-4d |
| 42 | bool insert_reshape = false; |
| 43 | if ((op->type == "nn.MaxPool1d" && input_rank == 2) |
| 44 | || (op->type == "nn.MaxPool2d" && input_rank == 3) |
| 45 | || (op->type == "nn.MaxPool3d" && input_rank == 4)) |
| 46 | { |
| 47 | insert_reshape = true; |
| 48 | } |
| 49 | |
| 50 | if (!insert_reshape) |
| 51 | continue; |
| 52 | |
| 53 | fprintf(stderr, "insert_reshape_pooling %d\n", input_rank); |
| 54 | |
| 55 | matched = true; |
| 56 | |
| 57 | Operand* pooling_in = op->inputs[0]; |
| 58 | Operand* pooling_out = op->outputs[0]; |
| 59 | |
| 60 | const int batch_index = pooling_in->params["__batch_index"].i; |
| 61 | |
| 62 | Operator* reshape0 = graph.new_operator_before("Tensor.reshape", op->name + "_ncnnreshape0", op); |
| 63 | Operator* reshape1 = graph.new_operator_after("Tensor.reshape", op->name + "_ncnnreshape1", op); |
| 64 | |
| 65 | Operand* reshape0_out = graph.new_operand(op->name + "_ncnnreshape0_out"); |
| 66 | Operand* reshape1_in = graph.new_operand(op->name + "_ncnnreshape1_in"); |
| 67 | |
| 68 | reshape0->inputs.push_back(pooling_in); |
| 69 | reshape0->outputs.push_back(reshape0_out); |
| 70 | reshape1->inputs.push_back(reshape1_in); |
| 71 | reshape1->outputs.push_back(pooling_out); |
| 72 | |
| 73 | for (size_t j = 0; j < pooling_in->consumers.size(); j++) |
| 74 | { |
| 75 | if (pooling_in->consumers[j] == op) |
| 76 | { |
| 77 | pooling_in->consumers[j] = reshape0; |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | pooling_out->producer = reshape1; |
| 82 | |
| 83 | op->inputs[0] = reshape0_out; |
| 84 | op->outputs[0] = reshape1_in; |
| 85 | |
| 86 | reshape0_out->producer = reshape0; |
| 87 | reshape0_out->consumers.push_back(op); |
| 88 | reshape1_in->producer = op; |
| 89 | reshape1_in->consumers.push_back(reshape1); |
| 90 | |
| 91 | reshape0_out->params["__batch_index"] = 0; |
| 92 | reshape1_in->params["__batch_index"] = batch_index; |
| 93 | |
| 94 | std::vector<int> reshape0_shape = pooling_in->shape; |
| 95 | reshape0_shape.insert(reshape0_shape.begin(), 1); |
| 96 | std::vector<int> reshape1_shape = pooling_out->shape; |
| 97 | |
| 98 | reshape0->params["shape"] = reshape0_shape; |
| 99 | reshape1->params["shape"] = reshape1_shape; |
| 100 | |
| 101 | break; |
| 102 | } |
| 103 | |
| 104 | if (!matched) |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | } // namespace ncnn |
| 110 | |
| 111 | } // namespace pnnx |