| 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_select_to_unbind.h" |
| 16 | |
| 17 | #include <algorithm> |
| 18 | #include "pass_level2.h" |
| 19 | |
| 20 | namespace pnnx { |
| 21 | |
| 22 | void fuse_select_to_unbind(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.select") |
| 33 | continue; |
| 34 | |
| 35 | Operand* op_in = op->inputs[0]; |
| 36 | int input_rank = op_in->shape.size(); |
| 37 | if (input_rank == 0) |
| 38 | continue; |
| 39 | |
| 40 | if (input_rank == 1) |
| 41 | { |
| 42 | // skip select scalar |
| 43 | continue; |
| 44 | } |
| 45 | |
| 46 | int dim = op->params.at("dim").i; |
| 47 | const int select_dimsize = op_in->shape[dim]; |
| 48 | if (select_dimsize == -1) |
| 49 | { |
| 50 | // skip dynamic |
| 51 | continue; |
| 52 | } |
| 53 | |
| 54 | // select 0..n |
| 55 | std::vector<int> select_n(select_dimsize, 0); |
| 56 | std::vector<Operator*> select_n_ops(select_dimsize, 0); |
| 57 | for (auto x : op_in->consumers) |
| 58 | { |
| 59 | if (x->type != "Tensor.select") |
| 60 | continue; |
| 61 | |
| 62 | if (x->inputs[0] != op_in) |
| 63 | continue; |
| 64 | |
| 65 | int dim2 = x->params.at("dim").i; |
| 66 | int index2 = x->params.at("index").i; |
| 67 | |
| 68 | if (index2 < 0) |
| 69 | index2 = select_dimsize + index2; |
| 70 | |
| 71 | if (dim == dim2) |
| 72 | { |
| 73 | select_n[index2] = 1; |
| 74 | select_n_ops[index2] = x; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | bool select_full_index = true; |
| 79 | for (auto x : select_n) |
| 80 | { |
| 81 | if (x == 0) |
| 82 | { |
| 83 | select_full_index = false; |
| 84 | break; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | if (!select_full_index) |
| 89 | continue; |
| 90 | |
| 91 | matched = true; |
| 92 | |
| 93 | // delete all select ops and replace with unbind |
| 94 | Operator* op_unbind = graph.new_operator_before("torch.unbind", op->name, op); |
| 95 | op_unbind->params["dim"] = dim; |
| 96 | |
| 97 | op_unbind->inputs.push_back(op_in); |
| 98 | for (int j = 0; j < select_dimsize; j++) |
| 99 | { |
| 100 | op_in->consumers.erase(std::find(op_in->consumers.begin(), op_in->consumers.end(), select_n_ops[j])); |
| 101 | } |
| 102 | op_in->consumers.push_back(op_unbind); |
| 103 | |
| 104 | op_unbind->outputs.resize(select_dimsize); |
| 105 | for (int j = 0; j < select_dimsize; j++) |
| 106 | { |
| 107 | op_unbind->outputs[j] = select_n_ops[j]->outputs[0]; |
| 108 | select_n_ops[j]->outputs[0]->producer = op_unbind; |
| 109 | } |
| 110 | |
| 111 | for (int j = 0; j < select_dimsize; j++) |
| 112 | { |
| 113 | graph.ops.erase(std::find(graph.ops.begin(), graph.ops.end(), select_n_ops[j])); |
| 114 | delete select_n_ops[j]; |
| 115 | } |
| 116 | |
| 117 | break; |
| 118 | } |
| 119 | |
| 120 | if (!matched) |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | } // namespace pnnx |