| 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_custom_op.h" |
| 16 | |
| 17 | #include <set> |
| 18 | |
| 19 | namespace pnnx { |
| 20 | |
| 21 | void fuse_custom_op(Graph& graph) |
| 22 | { |
| 23 | std::set<std::string> custom_ops; |
| 24 | |
| 25 | for (;;) |
| 26 | { |
| 27 | bool need_fuse = false; |
| 28 | |
| 29 | // fuse in reverse order |
| 30 | for (int i = (int)graph.ops.size() - 1; i >= 0; i--) |
| 31 | { |
| 32 | Operator* op = graph.ops[i]; |
| 33 | |
| 34 | if (op->type.find("::") == std::string::npos) |
| 35 | continue; |
| 36 | |
| 37 | std::string op_type_namespace = op->type.substr(0, op->type.find_first_of(':')); |
| 38 | |
| 39 | if (op_type_namespace == "aten" || op_type_namespace == "prim") |
| 40 | continue; |
| 41 | |
| 42 | custom_ops.insert(op->type); |
| 43 | |
| 44 | std::string op_type_name = op->type.substr(op->type.find_last_of(':') + 1); |
| 45 | |
| 46 | need_fuse = true; |
| 47 | |
| 48 | op->type = std::string("pnnx.custom_op.") + op_type_namespace + '.' + op_type_name; |
| 49 | |
| 50 | std::vector<Operand*> new_inputs; |
| 51 | std::vector<std::string> new_inputnames; |
| 52 | for (size_t j = 0; j < op->inputs.size(); j++) |
| 53 | { |
| 54 | Operator* arg = op->inputs[j]->producer; |
| 55 | |
| 56 | if (!arg->inputs.empty()) |
| 57 | { |
| 58 | new_inputs.push_back(op->inputs[j]); |
| 59 | new_inputnames.push_back(std::string("arg_") + std::to_string(j)); |
| 60 | continue; |
| 61 | } |
| 62 | |
| 63 | if (arg->type == "prim::Constant") |
| 64 | { |
| 65 | op->params[std::string("arg_") + std::to_string(j)] = arg->params["value"]; |
| 66 | op->inputs[j]->remove_consumer(op); |
| 67 | } |
| 68 | else if (arg->type == "pnnx.Expression") |
| 69 | { |
| 70 | op->params[std::string("arg_") + std::to_string(j)] = Parameter::parse_from_string(arg->params["expr"].s); |
| 71 | op->inputs[j]->remove_consumer(op); |
| 72 | } |
| 73 | else |
| 74 | { |
| 75 | new_inputs.push_back(op->inputs[j]); |
| 76 | new_inputnames.push_back(std::string("arg_") + std::to_string(j)); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | op->inputs = new_inputs; |
| 81 | op->inputnames = new_inputnames; |
| 82 | } |
| 83 | |
| 84 | if (!need_fuse) |
| 85 | break; |
| 86 | } |
| 87 | |
| 88 | for (auto x : custom_ops) |
| 89 | { |
| 90 | fprintf(stderr, "custom op = %s\n", x.c_str()); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | } // namespace pnnx |