| 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 "fuse_slice_copy.h" |
| 16 | |
| 17 | #include <limits.h> |
| 18 | #include <algorithm> |
| 19 | #include <stack> |
| 20 | #include "pass_level2.h" |
| 21 | |
| 22 | namespace pnnx { |
| 23 | |
| 24 | void fuse_slice_copy(Graph& graph) |
| 25 | { |
| 26 | while (1) |
| 27 | { |
| 28 | bool matched = false; |
| 29 | |
| 30 | for (size_t i = 0; i < graph.ops.size(); i++) |
| 31 | { |
| 32 | Operator* op = graph.ops[i]; |
| 33 | |
| 34 | if (op->type != "Tensor.copy") |
| 35 | continue; |
| 36 | |
| 37 | // collect slice / select op chain |
| 38 | std::stack<Operator*> slice_select_ops; |
| 39 | int descent_dim_current = INT_MAX; |
| 40 | Operand* in0 = op->inputs[0]; |
| 41 | while (in0->producer->type == "Tensor.slice" || in0->producer->type == "Tensor.select") |
| 42 | { |
| 43 | Operator* sop = in0->producer; |
| 44 | if (sop->type == "Tensor.slice") |
| 45 | { |
| 46 | if (!sop->has_param("dim") && !sop->has_param("dims")) |
| 47 | { |
| 48 | fprintf(stderr, "dynamic dims in slice copy chain is not supported\n"); |
| 49 | break; |
| 50 | } |
| 51 | |
| 52 | int dims0 = sop->has_param("dim") ? sop->params.at("dim").i : sop->params.at("dims").ai[0]; |
| 53 | if (descent_dim_current < dims0) |
| 54 | { |
| 55 | break; |
| 56 | } |
| 57 | |
| 58 | descent_dim_current = dims0; |
| 59 | } |
| 60 | |
| 61 | if (sop->type == "Tensor.select") |
| 62 | { |
| 63 | if (!sop->has_param("dim")) |
| 64 | { |
| 65 | fprintf(stderr, "dynamic dim in select copy chain is not supported\n"); |
| 66 | break; |
| 67 | } |
| 68 | |
| 69 | int dim = sop->params.at("dim").i; |
| 70 | if (descent_dim_current < dim) |
| 71 | { |
| 72 | break; |
| 73 | } |
| 74 | |
| 75 | descent_dim_current = dim; |
| 76 | } |
| 77 | |
| 78 | slice_select_ops.push(sop); |
| 79 | in0 = sop->inputs[0]; |
| 80 | } |
| 81 | |
| 82 | matched = true; |
| 83 | |
| 84 | if (slice_select_ops.empty()) |
| 85 | { |
| 86 | // eliminate noop copy |
| 87 | Operand* out = op->outputs[0]; |
| 88 | |
| 89 | for (auto& x : out->consumers) |
| 90 | { |
| 91 | for (size_t j = 0; j < x->inputs.size(); j++) |
| 92 | { |
| 93 | if (x->inputs[j] == out) |
| 94 | x->inputs[j] = op->inputs[1]; |
| 95 | } |
| 96 | |
| 97 | op->inputs[1]->consumers.push_back(x); |
| 98 | } |
| 99 | |
| 100 | op->inputs[0]->remove_consumer(op); |
| 101 | op->inputs[1]->remove_consumer(op); |
| 102 | |
| 103 | op->inputs[1]->name = out->name; |
| 104 | |
| 105 | out->producer = 0; |
| 106 | out->consumers.clear(); |
| 107 | |
| 108 | graph.operands.erase(std::find(graph.operands.begin(), graph.operands.end(), out)); |
| 109 | delete out; |
| 110 | |
| 111 | op->inputs.clear(); |
| 112 | op->outputs.clear(); |
| 113 | |
| 114 | graph.ops.erase(graph.ops.begin() + i); |
| 115 | delete op; |
| 116 | |
| 117 | break; |
| 118 | } |
| 119 | |
| 120 | Operator* top_sop = slice_select_ops.top(); |
| 121 | |
| 122 | op->type = "Tensor.slice_copy"; |
| 123 | |
| 124 | // insert clone just after the producer |
| 125 | Operator* op_clone = graph.new_operator_after("Tensor.clone", op->name + "_ncnnclone", top_sop->inputs[0]->producer); |
| 126 | Operand* clone_out = graph.new_operand(op->name + "_ncnnclone_out"); |
| 127 | |
| 128 | clone_out->type = top_sop->inputs[0]->type; |
| 129 | clone_out->shape = top_sop->inputs[0]->shape; |
| 130 | |
| 131 | op_clone->inputs.push_back(top_sop->inputs[0]); |
| 132 | top_sop->inputs[0]->consumers.push_back(op_clone); |
| 133 | |
| 134 | op_clone->outputs.push_back(clone_out); |
| 135 | clone_out->producer = op_clone; |
| 136 | |
| 137 | op->inputs[0]->remove_consumer(op); |
| 138 | op->inputs[0] = clone_out; |
| 139 | clone_out->consumers.push_back(op); |
| 140 | |
| 141 | if (top_sop->type == "Tensor.slice") |
| 142 | { |
| 143 | // shadow slice params and inputs to slice_copy |
| 144 | op->params = top_sop->params; |
| 145 | // self op->inputs[0] |
| 146 | // src op->inputs[1] |
| 147 | |
| 148 | if (top_sop->has_input("start")) |
| 149 | { |
| 150 | Operand* start = top_sop->named_input("start"); |
| 151 | op->inputs.push_back(start); |
| 152 | op->inputnames.push_back("start"); |
| 153 | start->consumers.push_back(op); |
| 154 | } |
| 155 | if (top_sop->has_input("starts")) |
| 156 | { |
| 157 | Operand* starts = top_sop->named_input("starts"); |
| 158 | op->inputs.push_back(starts); |
| 159 | op->inputnames.push_back("starts"); |
| 160 | starts->consumers.push_back(op); |
| 161 | } |
| 162 | if (top_sop->has_input("end")) |
| 163 | { |
| 164 | Operand* end = top_sop->named_input("end"); |
| 165 | op->inputs.push_back(end); |
| 166 | op->inputnames.push_back("end"); |
| 167 | end->consumers.push_back(op); |
| 168 | } |
| 169 | if (top_sop->has_input("ends")) |
| 170 | { |
| 171 | Operand* ends = top_sop->named_input("ends"); |
| 172 | op->inputs.push_back(ends); |
| 173 | op->inputnames.push_back("ends"); |
| 174 | ends->consumers.push_back(op); |
| 175 | } |
| 176 | if (top_sop->has_input("step")) |
| 177 | { |
| 178 | Operand* step = top_sop->named_input("step"); |
| 179 | op->inputs.push_back(step); |
| 180 | op->inputnames.push_back("step"); |
| 181 | step->consumers.push_back(op); |
| 182 | } |
| 183 | if (top_sop->has_input("steps")) |
| 184 | { |
| 185 | Operand* steps = top_sop->named_input("steps"); |
| 186 | op->inputs.push_back(steps); |
| 187 | op->inputnames.push_back("steps"); |
| 188 | steps->consumers.push_back(op); |
| 189 | } |
| 190 | if (top_sop->has_input("select")) |
| 191 | { |
| 192 | Operand* select = top_sop->named_input("select"); |
| 193 | op->inputs.push_back(select); |
| 194 | op->inputnames.push_back("select"); |
| 195 | select->consumers.push_back(op); |
| 196 | } |
| 197 | if (top_sop->has_input("selects")) |
| 198 | { |
| 199 | Operand* selects = top_sop->named_input("selects"); |
| 200 | op->inputs.push_back(selects); |
| 201 | op->inputnames.push_back("selects"); |
| 202 | selects->consumers.push_back(op); |
| 203 | } |
| 204 | } |
| 205 | else // if (top_sop->type == "Tensor.select") |
| 206 | { |
| 207 | op->params["dim"] = top_sop->params.at("dim").i; |
| 208 | op->params["start"] = 0; |
| 209 | op->params["end"] = 0; |
| 210 | op->params["step"] = 0; |
| 211 | |
| 212 | if (top_sop->has_param("index")) |
| 213 | { |
| 214 | op->params["select"] = top_sop->params.at("index").i; |
| 215 | } |
| 216 | else // if (top_sop->has_input("index")) |
| 217 | { |
| 218 | Operand* index = top_sop->named_input("index"); |
| 219 | op->inputs.push_back(index); |
| 220 | op->inputnames.push_back("select"); |
| 221 | index->consumers.push_back(op); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | break; |
| 226 | } |
| 227 | |
| 228 | if (!matched) |
| 229 | break; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | } // namespace pnnx |