| 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 "convert_torch_stack.h" |
| 16 | |
| 17 | namespace pnnx { |
| 18 | |
| 19 | namespace ncnn { |
| 20 | |
| 21 | void convert_torch_stack(Graph& graph) |
| 22 | { |
| 23 | int op_index = 0; |
| 24 | |
| 25 | while (1) |
| 26 | { |
| 27 | bool matched = false; |
| 28 | |
| 29 | for (Operator* op : graph.ops) |
| 30 | { |
| 31 | if (op->type != "torch.stack") |
| 32 | continue; |
| 33 | |
| 34 | matched = true; |
| 35 | |
| 36 | op->type = "Concat"; |
| 37 | op->name = std::string("stack_") + std::to_string(op_index++); |
| 38 | |
| 39 | const int batch_index = op->inputs[0]->params["__batch_index"].i; |
| 40 | const int input_rank = op->inputs[0]->shape.size(); |
| 41 | |
| 42 | int axis = op->params.at("dim").i; |
| 43 | if (axis == batch_index) |
| 44 | { |
| 45 | fprintf(stderr, "stack along batch axis %d is not supported\n", batch_index); |
| 46 | continue; |
| 47 | } |
| 48 | |
| 49 | if (axis < 0) |
| 50 | { |
| 51 | axis = input_rank + 1 + axis; |
| 52 | } |
| 53 | |
| 54 | if (axis > batch_index) |
| 55 | axis -= 1; |
| 56 | |
| 57 | op->params["0"] = axis; |
| 58 | |
| 59 | if (axis == input_rank) |
| 60 | { |
| 61 | // stack -> reshape(x,y,..,1) + concat |
| 62 | // reshape for input, expand the stack dim |
| 63 | for (size_t i = 0; i < op->inputs.size(); i++) |
| 64 | { |
| 65 | Operand* in = op->inputs[i]; |
| 66 | |
| 67 | Operator* reshape = graph.new_operator_before("Tensor.reshape", op->name + "_ncnnreshape_" + std::to_string(i), op); |
| 68 | |
| 69 | Operand* reshape_out = graph.new_operand(op->name + "_ncnnreshape_in"); |
| 70 | reshape_out->params["__batch_index"] = batch_index; |
| 71 | |
| 72 | reshape->inputs.push_back(in); |
| 73 | reshape->outputs.push_back(reshape_out); |
| 74 | |
| 75 | op->inputs[i] = reshape_out; |
| 76 | |
| 77 | in->remove_consumer(op); |
| 78 | in->consumers.push_back(reshape); |
| 79 | reshape_out->producer = reshape; |
| 80 | reshape_out->consumers.push_back(op); |
| 81 | |
| 82 | std::vector<int> shape = in->shape; |
| 83 | if (shape.size() != 0) |
| 84 | { |
| 85 | shape.push_back(1); |
| 86 | reshape->params["shape"] = shape; |
| 87 | } |
| 88 | reshape_out->shape = shape; |
| 89 | } |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | // reshape for output, expand the stack dim |
| 94 | Operand* out = op->outputs[0]; |
| 95 | |
| 96 | Operator* reshape = graph.new_operator_after("Tensor.reshape", op->name + "_ncnnreshape", op); |
| 97 | |
| 98 | Operand* reshape_in = graph.new_operand(op->name + "_ncnnreshape_in"); |
| 99 | |
| 100 | std::vector<int> shape = op->inputs[0]->shape; |
| 101 | if (shape.size() != 0) |
| 102 | { |
| 103 | shape[op->params.at("dim").i] *= op->inputs.size(); |
| 104 | reshape_in->shape = shape; |
| 105 | } |
| 106 | reshape_in->params["__batch_index"] = batch_index; |
| 107 | |
| 108 | reshape->inputs.push_back(reshape_in); |
| 109 | reshape->outputs.push_back(out); |
| 110 | |
| 111 | op->outputs[0] = reshape_in; |
| 112 | |
| 113 | out->producer = reshape; |
| 114 | reshape_in->producer = op; |
| 115 | reshape_in->consumers.push_back(reshape); |
| 116 | |
| 117 | reshape->params["shape"] = out->shape; |
| 118 | } |
| 119 | |
| 120 | op->params.erase("dim"); |
| 121 | |
| 122 | break; |
| 123 | } |
| 124 | |
| 125 | if (!matched) |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | } // namespace ncnn |
| 131 | |
| 132 | } // namespace pnnx |