Elffuss BraidHub

kikocis/ncnn public

master · 7.1 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_copy.h"
16
17namespace pnnx {
18
19namespace ncnn {
20
21void convert_Tensor_slice_copy(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_copy")
32 continue;
33
34 matched = true;
35
36 op->type = "CopyTo";
37 op->name = std::string("slice_copy_") + 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_copy 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_copy 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_copy 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_copy 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_copy 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 std::vector<int> selected_axes;
123 for (int i = 0; i < axes_rank; i++)
124 {
125 if (steps[i] == 0)
126 {
127 // simulate select as slice_copy
128 starts[i] = selects[i];
129 ends[i] = selects[i] + 1;
130 steps[i] = 1;
131 has_select = true;
132 selected_axes.push_back(axes[i]);
133 }
134 else if (steps[i] != 1)
135 {
136 fprintf(stderr, "slice_copy with step %d is not supported\n", steps[i]);
137 }
138 }
139
140 const int batch_index = op->inputs[0]->params["__batch_index"].i;
141
142 {
143 int input_rank = op->inputs[0]->shape.size();
144
145 if (batch_index >= 0 && batch_index < input_rank)
146 input_rank -= 1;
147
148 if (input_rank > 4)
149 {
150 fprintf(stderr, "slice_copy %d-rank tensor with %d-rank axes is not possible!\n", input_rank, axes_rank);
151 continue;
152 }
153 }
154
155 for (int i = 0; i < axes_rank; i++)
156 {
157 if (axes[i] == batch_index && (starts[i] != 0 || ends[i] != INT_MAX))
158 {
159 fprintf(stderr, "slice_copy along batch axis is not supported\n");
160 continue;
161 }
162
163 if (axes[i] < 0)
164 {
165 int input_rank = op->inputs[0]->shape.size();
166 axes[i] = input_rank + axes[i];
167 }
168
169 if (axes[i] > batch_index)
170 axes[i] -= 1;
171
172 if (ends[i] == INT_MAX)
173 ends[i] = -233;
174 }
175
176 // op->params["9"] = starts;
177 // op->params["10"] = ends;
178 // op->params["11"] = axes;
179
180 op->params["9"] = starts;
181 // op->params["10"] = ends; // ncnn always resolve ends from src blob
182 op->params["11"] = axes;
183
184 op->params.erase("dim");
185 op->params.erase("dims");
186 op->params.erase("start");
187 op->params.erase("starts");
188 op->params.erase("end");
189 op->params.erase("ends");
190 op->params.erase("step");
191 op->params.erase("steps");
192 op->params.erase("select");
193 op->params.erase("selects");
194
195 // reshape for output, squeezing the slice_copy dim
196 if (has_select)
197 {
198 Operand* in = op->inputs[1];
199
200 Operator* reshape = graph.new_operator_before("Tensor.reshape", op->name + "_ncnnreshape", op);
201
202 Operand* reshape_out = graph.new_operand(op->name + "_ncnnreshape_out");
203
204 reshape_out->params["__batch_index"] = batch_index;
205
206 reshape->inputs.push_back(in);
207 reshape->outputs.push_back(reshape_out);
208
209 op->inputs[1] = reshape_out;
210
211 reshape_out->producer = reshape;
212 reshape_out->consumers.push_back(op);
213 in->remove_consumer(op);
214 in->consumers.push_back(reshape);
215
216 std::vector<int> shape = in->shape;
217 for (auto sa : selected_axes)
218 {
219 // unsqueeze
220 shape.insert(shape.begin() + sa, 1);
221 }
222
223 reshape->params["shape"] = shape;
224 }
225
226 break;
227 }
228
229 if (!matched)
230 break;
231 }
232}
233
234} // namespace ncnn
235
236} // namespace pnnx