| 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_index_expression.h" |
| 16 | |
| 17 | #include <algorithm> |
| 18 | |
| 19 | namespace pnnx { |
| 20 | |
| 21 | static void replaceAll(std::string& str, const std::string& from, const std::string& to) |
| 22 | { |
| 23 | size_t start_pos = 0; |
| 24 | while ((start_pos = str.find(from, start_pos)) != std::string::npos) |
| 25 | { |
| 26 | str.replace(start_pos, from.length(), to); |
| 27 | start_pos += to.length(); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | static std::string fuse_attribute_expression(Operator* op_expr) |
| 32 | { |
| 33 | std::string expr = op_expr->params["expr"].s; |
| 34 | |
| 35 | for (int i = (int)op_expr->inputs.size() - 1; i >= 0; i--) |
| 36 | { |
| 37 | Operator* op_attr = op_expr->inputs[i]->producer; |
| 38 | |
| 39 | const Attribute& attr = op_attr->attrs.begin()->second; |
| 40 | |
| 41 | std::string attr_expr; |
| 42 | |
| 43 | int count = attr.shape[0]; |
| 44 | |
| 45 | if (attr.type == 9) |
| 46 | { |
| 47 | // bool |
| 48 | const char* pdata = (const char*)attr.data.data(); |
| 49 | attr_expr += "["; |
| 50 | for (int j = 0; j < count; j++) |
| 51 | { |
| 52 | const char* ls = pdata[j] != 0 ? "True" : "False"; |
| 53 | attr_expr += ls; |
| 54 | |
| 55 | if (j != count - 1) |
| 56 | attr_expr += ","; |
| 57 | } |
| 58 | attr_expr += "]"; |
| 59 | } |
| 60 | else if (attr.type == 5) |
| 61 | { |
| 62 | // i64 |
| 63 | const int64_t* pdata = (const int64_t*)attr.data.data(); |
| 64 | attr_expr += "["; |
| 65 | for (int j = 0; j < count; j++) |
| 66 | { |
| 67 | int64_t n = pdata[j]; |
| 68 | attr_expr += std::to_string(n); |
| 69 | |
| 70 | if (j != count - 1) |
| 71 | attr_expr += ","; |
| 72 | } |
| 73 | attr_expr += "]"; |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | fprintf(stderr, "unsupported index expression input %d attr type %d\n", i, attr.type); |
| 78 | } |
| 79 | |
| 80 | // replace @i with attr_expr |
| 81 | replaceAll(expr, "@" + std::to_string(i), attr_expr); |
| 82 | } |
| 83 | |
| 84 | return expr; |
| 85 | } |
| 86 | |
| 87 | void fuse_index_expression(Graph& graph) |
| 88 | { |
| 89 | while (1) |
| 90 | { |
| 91 | bool matched = false; |
| 92 | |
| 93 | for (size_t i = 0; i < graph.ops.size(); i++) |
| 94 | { |
| 95 | Operator* op = graph.ops[i]; |
| 96 | |
| 97 | if (op->type != "Tensor.index") |
| 98 | continue; |
| 99 | |
| 100 | if (op->inputs.size() != 2) |
| 101 | continue; |
| 102 | |
| 103 | if (op->inputs[1]->consumers.size() != 1) |
| 104 | continue; |
| 105 | |
| 106 | Operator* op2 = op->inputs[1]->producer; |
| 107 | if (op2->type != "pnnx.Expression") |
| 108 | continue; |
| 109 | |
| 110 | bool all_inputs_fusable = true; |
| 111 | for (Operand* a : op2->inputs) |
| 112 | { |
| 113 | if (a->producer->type != "pnnx.Attribute") |
| 114 | { |
| 115 | all_inputs_fusable = false; |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | if (!all_inputs_fusable) |
| 120 | continue; |
| 121 | |
| 122 | matched = true; |
| 123 | |
| 124 | std::string expr = fuse_attribute_expression(op2); |
| 125 | |
| 126 | op->params["expr"] = expr; |
| 127 | |
| 128 | op->inputs[1]->producer = 0; |
| 129 | op->inputs[1]->remove_consumer(op); |
| 130 | |
| 131 | op->inputs.resize(1); |
| 132 | |
| 133 | for (auto& x : op2->inputs) |
| 134 | { |
| 135 | x->remove_consumer(op2); |
| 136 | } |
| 137 | |
| 138 | op2->inputs.clear(); |
| 139 | op2->outputs.clear(); |
| 140 | |
| 141 | graph.ops.erase(std::find(graph.ops.begin(), graph.ops.end(), op2)); |
| 142 | |
| 143 | delete op2; |
| 144 | |
| 145 | break; |
| 146 | } |
| 147 | |
| 148 | if (!matched) |
| 149 | break; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | } // namespace pnnx |