| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2023 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_noop_expand.h" |
| 16 | |
| 17 | #include <algorithm> |
| 18 | #include "pass_level2.h" |
| 19 | |
| 20 | namespace pnnx { |
| 21 | |
| 22 | void eliminate_noop_expand(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 != "Tensor.expand_as" && op->type != "Tensor.expand") |
| 33 | continue; |
| 34 | |
| 35 | Operand* expand_out = op->outputs[0]; |
| 36 | |
| 37 | bool all_consumers_are_expr = true; |
| 38 | for (auto& x : expand_out->consumers) |
| 39 | { |
| 40 | if (x->type != "pnnx.Expression") |
| 41 | { |
| 42 | all_consumers_are_expr = false; |
| 43 | break; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | if (!all_consumers_are_expr) |
| 48 | continue; |
| 49 | |
| 50 | // Tensor.expand_as expand 2 1 in b in2 |
| 51 | // pnnx.Expression add 2 1 in2 b out |
| 52 | |
| 53 | const std::vector<int>& inshape = op->inputs[0]->shape; |
| 54 | if (inshape.empty()) |
| 55 | continue; |
| 56 | |
| 57 | bool noop_expand = true; |
| 58 | for (auto& x : expand_out->consumers) |
| 59 | { |
| 60 | const std::vector<int>& outshape = x->outputs[0]->shape; |
| 61 | if (outshape.empty()) |
| 62 | { |
| 63 | noop_expand = false; |
| 64 | break; |
| 65 | } |
| 66 | |
| 67 | // check if inshape can be binary broadcast to outshape |
| 68 | if (inshape.size() != outshape.size()) |
| 69 | { |
| 70 | noop_expand = false; |
| 71 | break; |
| 72 | } |
| 73 | |
| 74 | for (size_t j = 0; j < inshape.size(); j++) |
| 75 | { |
| 76 | if ((inshape[j] == outshape[j] && outshape[j] != -1) || inshape[j] == 1 || outshape[j] == 1) |
| 77 | continue; |
| 78 | |
| 79 | noop_expand = false; |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // check if our expand is the base shape |
| 85 | // so we do not drop expand for add(expand(x,shape),1.2) |
| 86 | for (auto& x : expand_out->consumers) |
| 87 | { |
| 88 | const std::vector<int>& outshape = x->outputs[0]->shape; |
| 89 | |
| 90 | std::vector<int> broadcasted_shape = inshape; |
| 91 | for (const auto& r : x->inputs) |
| 92 | { |
| 93 | if (r == expand_out) |
| 94 | continue; |
| 95 | |
| 96 | if (r->shape.size() != inshape.size()) |
| 97 | continue; |
| 98 | |
| 99 | for (size_t j = 0; j < broadcasted_shape.size(); j++) |
| 100 | { |
| 101 | broadcasted_shape[j] = std::max(broadcasted_shape[j], r->shape[j]); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | if (broadcasted_shape != outshape) |
| 106 | { |
| 107 | noop_expand = false; |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | if (!noop_expand) |
| 113 | continue; |
| 114 | |
| 115 | // delete noop-like expand |
| 116 | matched = true; |
| 117 | |
| 118 | for (auto& x : op->inputs) |
| 119 | { |
| 120 | x->remove_consumer(op); |
| 121 | } |
| 122 | |
| 123 | for (auto& x : expand_out->consumers) |
| 124 | { |
| 125 | for (size_t j = 0; j < x->inputs.size(); j++) |
| 126 | { |
| 127 | if (x->inputs[j] == expand_out) |
| 128 | x->inputs[j] = op->inputs[0]; |
| 129 | } |
| 130 | |
| 131 | op->inputs[0]->consumers.push_back(x); |
| 132 | } |
| 133 | |
| 134 | op->inputs[0]->name = expand_out->name; |
| 135 | |
| 136 | expand_out->producer = 0; |
| 137 | expand_out->consumers.clear(); |
| 138 | |
| 139 | graph.operands.erase(std::find(graph.operands.begin(), graph.operands.end(), expand_out)); |
| 140 | delete expand_out; |
| 141 | |
| 142 | op->inputs.clear(); |
| 143 | op->outputs.clear(); |
| 144 | |
| 145 | graph.ops.erase(graph.ops.begin() + i); |
| 146 | delete op; |
| 147 | |
| 148 | break; |
| 149 | } |
| 150 | |
| 151 | if (!matched) |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | } // namespace pnnx |