| 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 "fuse_constant_expression.h" |
| 16 | |
| 17 | #include <string.h> |
| 18 | #include <algorithm> |
| 19 | #include "pass_level2.h" |
| 20 | |
| 21 | namespace pnnx { |
| 22 | |
| 23 | void fuse_constant_expression(Graph& graph) |
| 24 | { |
| 25 | while (1) |
| 26 | { |
| 27 | bool matched = false; |
| 28 | |
| 29 | for (size_t i = 0; i < graph.ops.size(); i++) |
| 30 | { |
| 31 | Operator* op = graph.ops[i]; |
| 32 | |
| 33 | if (op->type != "pnnx.Expression") |
| 34 | continue; |
| 35 | |
| 36 | if (op->inputs.size() != 0) |
| 37 | { |
| 38 | // dynamic expression |
| 39 | continue; |
| 40 | } |
| 41 | |
| 42 | Operand* expr_output = op->outputs[0]; |
| 43 | |
| 44 | std::vector<Operator*> new_consumers; |
| 45 | for (auto x : expr_output->consumers) |
| 46 | { |
| 47 | if (x->inputnames.empty()) |
| 48 | { |
| 49 | // x is not a function |
| 50 | new_consumers.push_back(x); |
| 51 | continue; |
| 52 | } |
| 53 | |
| 54 | for (size_t j = 0; j < x->inputs.size(); j++) |
| 55 | { |
| 56 | if (x->inputs[j] == expr_output && x->inputnames[j].empty()) |
| 57 | { |
| 58 | // no param key |
| 59 | new_consumers.push_back(x); |
| 60 | break; |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | if (new_consumers == expr_output->consumers) |
| 66 | continue; |
| 67 | |
| 68 | matched = true; |
| 69 | |
| 70 | Parameter ep = Parameter::parse_from_string(op->params.at("expr").s); |
| 71 | |
| 72 | for (auto& x : expr_output->consumers) |
| 73 | { |
| 74 | if (x->inputnames.empty()) |
| 75 | { |
| 76 | // x is not a function |
| 77 | continue; |
| 78 | } |
| 79 | |
| 80 | std::vector<Operand*> new_inputs; |
| 81 | std::vector<std::string> new_inputnames; |
| 82 | for (size_t j = 0; j < x->inputs.size(); j++) |
| 83 | { |
| 84 | if (x->inputs[j] == expr_output && !x->inputnames[j].empty()) |
| 85 | { |
| 86 | // fuse constant |
| 87 | x->params[x->inputnames[j]] = ep; |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | new_inputs.push_back(x->inputs[j]); |
| 92 | new_inputnames.push_back(x->inputnames[j]); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | x->inputs = new_inputs; |
| 97 | x->inputnames = new_inputnames; |
| 98 | } |
| 99 | |
| 100 | expr_output->consumers = new_consumers; |
| 101 | |
| 102 | if (expr_output->consumers.empty()) |
| 103 | { |
| 104 | // delete expression and expr_output |
| 105 | |
| 106 | expr_output->producer = 0; |
| 107 | graph.operands.erase(std::find(graph.operands.begin(), graph.operands.end(), expr_output)); |
| 108 | delete expr_output; |
| 109 | |
| 110 | op->inputs.clear(); |
| 111 | op->outputs.clear(); |
| 112 | |
| 113 | graph.ops.erase(std::find(graph.ops.begin(), graph.ops.end(), op)); |
| 114 | delete op; |
| 115 | } |
| 116 | |
| 117 | break; |
| 118 | } |
| 119 | |
| 120 | if (!matched) |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | } // namespace pnnx |