| 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 "insert_reshape_numpy_binaryop_broadcast.h" |
| 16 | #include "pass_ncnn.h" |
| 17 | |
| 18 | namespace pnnx { |
| 19 | |
| 20 | namespace ncnn { |
| 21 | |
| 22 | void insert_reshape_numpy_binaryop_broadcast(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 != "BinaryOp") |
| 33 | continue; |
| 34 | |
| 35 | if (op->inputs.size() != 2) |
| 36 | continue; |
| 37 | |
| 38 | if (op->inputs[0]->shape.empty() || op->inputs[1]->shape.empty()) |
| 39 | continue; |
| 40 | |
| 41 | int batch_index0 = op->inputs[0]->params["__batch_index"].i; |
| 42 | int batch_index1 = op->inputs[1]->params["__batch_index"].i; |
| 43 | if (batch_index0 != batch_index1) |
| 44 | { |
| 45 | fprintf(stderr, "binaryop broadcast across batch axis %d and %d is not supported\n", batch_index0, batch_index1); |
| 46 | continue; |
| 47 | } |
| 48 | |
| 49 | if (op->inputs[0]->shape.size() == 5 && batch_index0 == 233) |
| 50 | { |
| 51 | if (op->inputs[0]->shape[0] == 1) |
| 52 | { |
| 53 | fprintf(stderr, "assume reshape 5-rank tensor has batch_index 0\n"); |
| 54 | batch_index0 = 0; |
| 55 | } |
| 56 | } |
| 57 | if (op->inputs[1]->shape.size() == 5 && batch_index1 == 233) |
| 58 | { |
| 59 | if (op->inputs[1]->shape[0] == 1) |
| 60 | { |
| 61 | fprintf(stderr, "assume reshape 5-rank tensor has batch_index 0\n"); |
| 62 | batch_index1 = 0; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // drop shape batch index |
| 67 | std::vector<int> new_shape0; |
| 68 | std::vector<int> new_shape1; |
| 69 | for (int j = 0; j < (int)op->inputs[0]->shape.size(); j++) |
| 70 | { |
| 71 | if (j == batch_index0 && (op->inputs[0]->shape[j] == 1 || op->inputs[0]->shape[j] == op->inputs[1]->shape[j])) |
| 72 | continue; |
| 73 | |
| 74 | new_shape0.push_back(op->inputs[0]->shape[j]); |
| 75 | } |
| 76 | for (int j = 0; j < (int)op->inputs[1]->shape.size(); j++) |
| 77 | { |
| 78 | if (j == batch_index1 && (op->inputs[1]->shape[j] == 1 || op->inputs[1]->shape[j] == op->inputs[0]->shape[j])) |
| 79 | continue; |
| 80 | |
| 81 | new_shape1.push_back(op->inputs[1]->shape[j]); |
| 82 | } |
| 83 | |
| 84 | const int input_rank0 = (int)new_shape0.size(); |
| 85 | const int input_rank1 = (int)new_shape1.size(); |
| 86 | |
| 87 | if (input_rank0 >= 5) |
| 88 | { |
| 89 | fprintf(stderr, "binaryop tensor0 with rank %d is not supported yet!\n", (int)op->inputs[0]->shape.size()); |
| 90 | } |
| 91 | |
| 92 | if (input_rank1 >= 5) |
| 93 | { |
| 94 | fprintf(stderr, "binaryop tensor1 with rank %d is not supported yet!\n", (int)op->inputs[1]->shape.size()); |
| 95 | } |
| 96 | |
| 97 | if (input_rank0 == input_rank1) |
| 98 | { |
| 99 | // no broadcast after ignoring batch index |
| 100 | continue; |
| 101 | } |
| 102 | |
| 103 | // fprintf(stderr, "insert_reshape_numpy_binaryop_broadcast %d %d\n", input_rank0, input_rank1); |
| 104 | |
| 105 | matched = true; |
| 106 | |
| 107 | const int binaryop_lower_rank_in_index = input_rank0 < input_rank1 ? 0 : 1; |
| 108 | |
| 109 | Operand* binaryop_lower_rank_in = op->inputs[binaryop_lower_rank_in_index]; |
| 110 | |
| 111 | Operator* reshape0 = graph.new_operator_before("Tensor.reshape", op->name + "_ncnnreshape0", op); |
| 112 | |
| 113 | Operand* reshape0_out = graph.new_operand(op->name + "_ncnnreshape0_out"); |
| 114 | |
| 115 | reshape0->inputs.push_back(binaryop_lower_rank_in); |
| 116 | reshape0->outputs.push_back(reshape0_out); |
| 117 | |
| 118 | for (size_t j = 0; j < binaryop_lower_rank_in->consumers.size(); j++) |
| 119 | { |
| 120 | if (binaryop_lower_rank_in->consumers[j] == op) |
| 121 | { |
| 122 | binaryop_lower_rank_in->consumers[j] = reshape0; |
| 123 | break; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | op->inputs[binaryop_lower_rank_in_index] = reshape0_out; |
| 128 | |
| 129 | reshape0_out->producer = reshape0; |
| 130 | reshape0_out->consumers.push_back(op); |
| 131 | |
| 132 | reshape0_out->params["__batch_index"] = input_rank0 < input_rank1 ? batch_index0 : batch_index1; |
| 133 | |
| 134 | // insert explicit broadcast index for missing ranks |
| 135 | std::vector<int> reshape0_shape = input_rank0 < input_rank1 ? new_shape0 : new_shape1; |
| 136 | for (int j = 0; j < std::abs(input_rank0 - input_rank1); j++) |
| 137 | { |
| 138 | reshape0_shape.insert(reshape0_shape.begin(), 1); |
| 139 | } |
| 140 | |
| 141 | if (batch_index0 != 233) |
| 142 | { |
| 143 | reshape0_shape.insert(reshape0_shape.begin() + batch_index0, 1); |
| 144 | } |
| 145 | |
| 146 | reshape0->params["shape"] = reshape0_shape; |
| 147 | |
| 148 | break; |
| 149 | } |
| 150 | |
| 151 | if (!matched) |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | } // namespace ncnn |
| 157 | |
| 158 | } // namespace pnnx |