| 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 "convert_Tensor_select.h" |
| 16 | |
| 17 | namespace pnnx { |
| 18 | |
| 19 | namespace ncnn { |
| 20 | |
| 21 | void convert_Tensor_select(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 != "Tensor.select") |
| 32 | continue; |
| 33 | |
| 34 | matched = true; |
| 35 | |
| 36 | op->type = "Crop"; |
| 37 | op->name = std::string("select_") + std::to_string(op_index++); |
| 38 | |
| 39 | const int batch_index = op->inputs[0]->params["__batch_index"].i; |
| 40 | |
| 41 | int axis = op->params.at("dim").i; |
| 42 | if (axis == batch_index) |
| 43 | { |
| 44 | fprintf(stderr, "select along batch axis %d is not supported\n", batch_index); |
| 45 | continue; |
| 46 | } |
| 47 | |
| 48 | if (axis < 0) |
| 49 | { |
| 50 | int input_rank = op->inputs[0]->shape.size(); |
| 51 | axis = input_rank + axis; |
| 52 | } |
| 53 | |
| 54 | if (axis > batch_index) |
| 55 | axis -= 1; |
| 56 | |
| 57 | int index = op->params.at("index").i; |
| 58 | |
| 59 | op->params["9"] = std::vector<int> {index}; |
| 60 | op->params["10"] = std::vector<int> {index + 1}; |
| 61 | op->params["11"] = std::vector<int> {axis}; |
| 62 | |
| 63 | op->params.erase("dim"); |
| 64 | op->params.erase("index"); |
| 65 | |
| 66 | // reshape for output, squeezing the select dim |
| 67 | { |
| 68 | Operand* out = op->outputs[0]; |
| 69 | |
| 70 | Operator* reshape = graph.new_operator_after("Tensor.reshape", op->name + "_ncnnreshape", op); |
| 71 | |
| 72 | Operand* reshape_in = graph.new_operand(op->name + "_ncnnreshape_in"); |
| 73 | |
| 74 | reshape->inputs.push_back(reshape_in); |
| 75 | reshape->outputs.push_back(out); |
| 76 | |
| 77 | op->outputs[0] = reshape_in; |
| 78 | |
| 79 | out->producer = reshape; |
| 80 | reshape_in->producer = op; |
| 81 | reshape_in->consumers.push_back(reshape); |
| 82 | |
| 83 | reshape->params["shape"] = out->shape; |
| 84 | } |
| 85 | |
| 86 | break; |
| 87 | } |
| 88 | |
| 89 | if (!matched) |
| 90 | break; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | } // namespace ncnn |
| 95 | |
| 96 | } // namespace pnnx |