Elffuss BraidHub

kikocis/ncnn public

master · 6.6 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2024 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_slice.h"
16
17namespace pnnx {
18
19namespace ncnn {
20
21void convert_Tensor_slice(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.slice")
32 continue;
33
34 matched = true;
35
36 op->type = "Crop";
37 op->name = std::string("slice_") + std::to_string(op_index++);
38
39 std::vector<int> axes;
40 std::vector<int> starts;
41 std::vector<int> ends;
42 std::vector<int> steps;
43 std::vector<int> selects;
44
45 if (op->has_param("dims"))
46 {
47 axes = op->params.at("dims").ai;
48 }
49 else if (op->has_param("dim"))
50 {
51 axes = std::vector<int> {op->params.at("dim").i};
52 }
53 else
54 {
55 fprintf(stderr, "slice with dynamic dim is not supported\n");
56 continue;
57 }
58
59 if (op->has_param("starts"))
60 {
61 starts = op->params.at("starts").ai;
62 }
63 else if (op->has_param("start"))
64 {
65 starts = std::vector<int> {op->params.at("start").i};
66 }
67 else
68 {
69 fprintf(stderr, "slice with dynamic start is not supported\n");
70 continue;
71 }
72
73 if (op->has_param("ends"))
74 {
75 ends = op->params.at("ends").ai;
76 }
77 else if (op->has_param("end"))
78 {
79 ends = std::vector<int> {op->params.at("end").i};
80 }
81 else
82 {
83 fprintf(stderr, "slice with dynamic end is not supported\n");
84 continue;
85 }
86
87 if (op->has_param("steps"))
88 {
89 steps = op->params.at("steps").ai;
90 }
91 else if (op->has_param("step"))
92 {
93 steps = std::vector<int> {op->params.at("step").i};
94 }
95 else
96 {
97 fprintf(stderr, "slice with dynamic step is not supported\n");
98 continue;
99 }
100
101 if (op->has_param("selects"))
102 {
103 selects = op->params.at("selects").ai;
104 }
105 else if (op->has_param("select"))
106 {
107 selects = std::vector<int> {op->params.at("select").i};
108 }
109 else if (op->has_input("selects") || op->has_input("select"))
110 {
111 fprintf(stderr, "slice with dynamic select is not supported\n");
112 continue;
113 }
114 else
115 {
116 // without select index
117 }
118
119 const int axes_rank = axes.size();
120
121 bool has_select = false;
122 for (int i = 0; i < axes_rank; i++)
123 {
124 if (steps[i] == 0)
125 {
126 // simulate select as slice
127 starts[i] = selects[i];
128 ends[i] = selects[i] + 1;
129 steps[i] = 1;
130 has_select = true;
131 }
132 else if (steps[i] != 1)
133 {
134 fprintf(stderr, "slice with step %d is not supported\n", steps[i]);
135 continue;
136 }
137 }
138
139 const int batch_index = op->inputs[0]->params["__batch_index"].i;
140
141 {
142 int input_rank = op->inputs[0]->shape.size();
143
144 if (batch_index >= 0 && batch_index < input_rank)
145 input_rank -= 1;
146
147 if (input_rank > 4)
148 {
149 fprintf(stderr, "slice %d-rank tensor with %d-rank axes is not possible!\n", input_rank, axes_rank);
150 continue;
151 }
152 }
153
154 for (int i = 0; i < axes_rank; i++)
155 {
156 if (axes[i] == batch_index && (starts[i] != 0 || ends[i] != INT_MAX))
157 {
158 fprintf(stderr, "slice along batch axis is not supported\n");
159 continue;
160 }
161
162 if (axes[i] < 0)
163 {
164 int input_rank = op->inputs[0]->shape.size();
165 axes[i] = input_rank + axes[i];
166 }
167
168 if (axes[i] > batch_index)
169 axes[i] -= 1;
170
171 if (ends[i] == INT_MAX)
172 ends[i] = -233;
173 }
174
175 op->params["9"] = starts;
176 op->params["10"] = ends;
177 op->params["11"] = axes;
178
179 op->params.erase("dim");
180 op->params.erase("dims");
181 op->params.erase("start");
182 op->params.erase("starts");
183 op->params.erase("end");
184 op->params.erase("ends");
185 op->params.erase("step");
186 op->params.erase("steps");
187 op->params.erase("select");
188 op->params.erase("selects");
189
190 // reshape for output, squeezing the slice dim
191 if (has_select)
192 {
193 Operand* out = op->outputs[0];
194
195 Operator* reshape = graph.new_operator_after("Tensor.reshape", op->name + "_ncnnreshape", op);
196
197 Operand* reshape_in = graph.new_operand(op->name + "_ncnnreshape_in");
198
199 reshape_in->params["__batch_index"] = batch_index;
200
201 reshape->inputs.push_back(reshape_in);
202 reshape->outputs.push_back(out);
203
204 op->outputs[0] = reshape_in;
205
206 out->producer = reshape;
207 reshape_in->producer = op;
208 reshape_in->consumers.push_back(reshape);
209
210 reshape->params["shape"] = out->shape;
211 }
212
213 break;
214 }
215
216 if (!matched)
217 break;
218 }
219}
220
221} // namespace ncnn
222
223} // namespace pnnx