Elffuss BraidHub

kikocis/ncnn public

master · 12.6 KiBRawPermalink
1// yala is pleased to support the open source community by making ncnn available.
2//
3//
4// Copyright (C) 2022 yala <zhaojunchao@loongson.cn>;<junchao82@qq.com>. All rights reserved.
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 "crop_loongarch.h"
16
17#if __loongarch_sx
18#include <lsxintrin.h>
19#endif // __loongarch_sx
20
21namespace ncnn {
22
23Crop_loongarch::Crop_loongarch()
24{
25#if __loongarch_sx
26 support_packing = true;
27#endif // __loongarch_sx
28}
29
30#if __loongarch_sx
31static void crop_pack4_lsx(const Mat& src, Mat& dst, int top, int left)
32{
33 int w = dst.w;
34 int h = dst.h;
35 int right = src.w - dst.w - left;
36
37 const float* ptr = src.row(top) + left * 4;
38 float* outptr = dst;
39
40 for (int y = 0; y < h; y++)
41 {
42 for (int x = 0; x < w; x++)
43 {
44 __m128 _p = (__m128)__lsx_vld(ptr, 0);
45 __lsx_vst(_p, outptr, 0);
46
47 ptr += 4;
48 outptr += 4;
49 }
50
51 ptr += (left + right) * 4;
52 }
53}
54#endif // __loongarch_sx
55
56int Crop_loongarch::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
57{
58 int w = bottom_blob.w;
59 int h = bottom_blob.h;
60 int d = bottom_blob.d;
61 int channels = bottom_blob.c;
62 int dims = bottom_blob.dims;
63 size_t elemsize = bottom_blob.elemsize;
64 int elempack = bottom_blob.elempack;
65
66#if __loongarch_sx
67 if (elempack == 4)
68 {
69 int _woffset, _hoffset, _doffset, _coffset;
70 int _outw, _outh, _outd, _outc;
71 resolve_crop_roi(bottom_blob.shape(), _woffset, _hoffset, _doffset, _coffset, _outw, _outh, _outd, _outc);
72
73 if (dims == 1)
74 {
75 int out_elempack = _outw % 4 == 0 ? 4 : 1;
76 size_t out_elemsize = elemsize / elempack * out_elempack;
77
78 if (_outw / out_elempack == w && out_elempack == 4)
79 {
80 top_blob = bottom_blob;
81 return 0;
82 }
83
84 if (_woffset % 4 == 0 && out_elempack == 4)
85 {
86 top_blob.create(_outw / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
87 if (top_blob.empty())
88 return -100;
89
90 crop_pack4_lsx(bottom_blob, top_blob, 0, _woffset / elempack);
91
92 return 0;
93 }
94 }
95
96 if (dims == 2)
97 {
98 int out_elempack = _outh % 4 == 0 ? 4 : 1;
99 size_t out_elemsize = elemsize / elempack * out_elempack;
100
101 if (_outw == w && _outh / out_elempack == h && out_elempack == 4)
102 {
103 top_blob = bottom_blob;
104 return 0;
105 }
106
107 if (_hoffset % 4 == 0 && out_elempack == 4)
108 {
109 top_blob.create(_outw, _outh / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
110 if (top_blob.empty())
111 return -100;
112
113 crop_pack4_lsx(bottom_blob, top_blob, _hoffset / elempack, _woffset);
114
115 return 0;
116 }
117 }
118
119 if (dims == 3)
120 {
121 int out_elempack = _outc % 4 == 0 ? 4 : 1;
122 size_t out_elemsize = elemsize / elempack * out_elempack;
123
124 if (_outw == w && _outh == h && _outc / out_elempack == channels && out_elempack == 4)
125 {
126 top_blob = bottom_blob;
127 return 0;
128 }
129
130 if (_coffset % 4 == 0 && out_elempack == 4)
131 {
132 const Mat bottom_blob_sliced = bottom_blob.channel_range(_coffset / out_elempack, _outc / out_elempack);
133
134 if (_outw == w && _outh == h)
135 {
136 top_blob = bottom_blob_sliced.clone();
137 if (top_blob.empty())
138 return -100;
139 }
140
141 top_blob.create(_outw, _outh, _outc / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
142 if (top_blob.empty())
143 return -100;
144
145 #pragma omp parallel for num_threads(opt.num_threads)
146 for (int q = 0; q < top_blob.c; q++)
147 {
148 const Mat m = bottom_blob_sliced.channel(q);
149 Mat borderm = top_blob.channel(q);
150
151 crop_pack4_lsx(m, borderm, _hoffset, _woffset);
152 }
153
154 return 0;
155 }
156 }
157
158 if (dims == 4)
159 {
160 int out_elempack = _outc % 4 == 0 ? 4 : 1;
161 size_t out_elemsize = elemsize / elempack * out_elempack;
162
163 if (_outw == w && _outh == h && _outd == d && _outc / out_elempack == channels && out_elempack == 4)
164 {
165 top_blob = bottom_blob;
166 return 0;
167 }
168
169 if (_coffset % 4 == 0 && out_elempack == 4)
170 {
171 const Mat bottom_blob_sliced = bottom_blob.channel_range(_coffset / out_elempack, _outc / out_elempack);
172
173 if (_outw == w && _outh == h && _outd == d)
174 {
175 top_blob = bottom_blob_sliced.clone();
176 if (top_blob.empty())
177 return -100;
178 }
179
180 top_blob.create(_outw, _outh, _outd, _outc / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
181 if (top_blob.empty())
182 return -100;
183
184 #pragma omp parallel for num_threads(opt.num_threads)
185 for (int q = 0; q < top_blob.c; q++)
186 {
187 for (int z = 0; z < _outd; z++)
188 {
189 const Mat m = bottom_blob_sliced.channel(q).depth(z + _doffset);
190 Mat borderm = top_blob.channel(q).depth(z);
191
192 crop_pack4_lsx(m, borderm, _hoffset, _woffset);
193 }
194 }
195
196 return 0;
197 }
198 }
199 }
200#endif // __loongarch_sx
201
202 Mat bottom_blob_unpacked = bottom_blob;
203 if (elempack != 1)
204 {
205 Option opt_pack1 = opt;
206 opt_pack1.blob_allocator = opt.workspace_allocator;
207
208 convert_packing(bottom_blob, bottom_blob_unpacked, 1, opt_pack1);
209 }
210
211 return Crop::forward(bottom_blob_unpacked, top_blob, opt);
212}
213
214int Crop_loongarch::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
215{
216 const Mat& bottom_blob = bottom_blobs[0];
217 const Mat& reference_blob = bottom_blobs[1];
218
219 int w = bottom_blob.w;
220 int h = bottom_blob.h;
221 int d = bottom_blob.d;
222 int channels = bottom_blob.c;
223 int dims = bottom_blob.dims;
224 size_t elemsize = bottom_blob.elemsize;
225 int elempack = bottom_blob.elempack;
226
227 int ref_elempack = reference_blob.elempack;
228
229 Mat& top_blob = top_blobs[0];
230
231#if __loongarch_sx
232 if (elempack == 4)
233 {
234 int _woffset, _hoffset, _doffset, _coffset;
235 int _outw, _outh, _outd, _outc;
236 if (woffset == -233)
237 {
238 resolve_crop_roi(bottom_blob.shape(), (const int*)reference_blob, _woffset, _hoffset, _doffset, _coffset, _outw, _outh, _outd, _outc);
239 }
240 else
241 {
242 resolve_crop_roi(bottom_blob.shape(), reference_blob.shape(), _woffset, _hoffset, _doffset, _coffset, _outw, _outh, _outd, _outc);
243 }
244
245 if (dims == 1)
246 {
247 int out_elempack = _outw % 4 == 0 ? 4 : 1;
248 size_t out_elemsize = elemsize / elempack * out_elempack;
249
250 if (_outw / out_elempack == w && out_elempack == 4)
251 {
252 top_blob = bottom_blob;
253 return 0;
254 }
255
256 if (_woffset % 4 == 0 && out_elempack == 4)
257 {
258 top_blob.create(_outw / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
259 if (top_blob.empty())
260 return -100;
261
262 crop_pack4_lsx(bottom_blob, top_blob, 0, _woffset / elempack);
263
264 return 0;
265 }
266 }
267
268 if (dims == 2)
269 {
270 int out_elempack = _outh % 4 == 0 ? 4 : 1;
271 size_t out_elemsize = elemsize / elempack * out_elempack;
272
273 if (_outw == w && _outh / out_elempack == h && out_elempack == 4)
274 {
275 top_blob = bottom_blob;
276 return 0;
277 }
278
279 if (_hoffset % 4 == 0 && out_elempack == 4)
280 {
281 top_blob.create(_outw, _outh / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
282 if (top_blob.empty())
283 return -100;
284
285 crop_pack4_lsx(bottom_blob, top_blob, _hoffset / elempack, _woffset);
286
287 return 0;
288 }
289 }
290
291 if (dims == 3)
292 {
293 int out_elempack = _outc % 4 == 0 ? 4 : 1;
294 size_t out_elemsize = elemsize / elempack * out_elempack;
295
296 if (_outw == w && _outh == h && _outc / out_elempack == channels && out_elempack == 4)
297 {
298 top_blob = bottom_blob;
299 return 0;
300 }
301
302 if (_coffset % 4 == 0 && out_elempack == 4)
303 {
304 const Mat bottom_blob_sliced = bottom_blob.channel_range(_coffset / out_elempack, _outc / out_elempack);
305
306 if (_outw == w && _outh == h)
307 {
308 top_blob = bottom_blob_sliced.clone();
309 if (top_blob.empty())
310 return -100;
311 }
312
313 top_blob.create(_outw, _outh, _outc / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
314 if (top_blob.empty())
315 return -100;
316
317 #pragma omp parallel for num_threads(opt.num_threads)
318 for (int q = 0; q < top_blob.c; q++)
319 {
320 const Mat m = bottom_blob_sliced.channel(q);
321 Mat borderm = top_blob.channel(q);
322
323 crop_pack4_lsx(m, borderm, _hoffset, _woffset);
324 }
325
326 return 0;
327 }
328 }
329
330 if (dims == 4)
331 {
332 int out_elempack = _outc % 4 == 0 ? 4 : 1;
333 size_t out_elemsize = elemsize / elempack * out_elempack;
334
335 if (_outw == w && _outh == h && _outd == d && _outc / out_elempack == channels && out_elempack == 4)
336 {
337 top_blob = bottom_blob;
338 return 0;
339 }
340
341 if (_coffset % 4 == 0 && out_elempack == 4)
342 {
343 const Mat bottom_blob_sliced = bottom_blob.channel_range(_coffset / out_elempack, _outc / out_elempack);
344
345 if (_outw == w && _outh == h && _outd == d)
346 {
347 top_blob = bottom_blob_sliced.clone();
348 if (top_blob.empty())
349 return -100;
350 }
351
352 top_blob.create(_outw, _outh, _outd, _outc / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
353 if (top_blob.empty())
354 return -100;
355
356 #pragma omp parallel for num_threads(opt.num_threads)
357 for (int q = 0; q < top_blob.c; q++)
358 {
359 for (int z = 0; z < _outd; z++)
360 {
361 const Mat m = bottom_blob_sliced.channel(q).depth(z + _doffset);
362 Mat borderm = top_blob.channel(q).depth(z);
363
364 crop_pack4_lsx(m, borderm, _hoffset, _woffset);
365 }
366 }
367
368 return 0;
369 }
370 }
371 }
372#endif // __loongarch_sx
373
374 Mat bottom_blob_unpacked = bottom_blob;
375 if (elempack != 1)
376 {
377 Option opt_pack1 = opt;
378 opt_pack1.blob_allocator = opt.workspace_allocator;
379
380 convert_packing(bottom_blob, bottom_blob_unpacked, 1, opt_pack1);
381 }
382
383 Mat reference_blob_unpacked = reference_blob;
384 if (ref_elempack != 1)
385 {
386 Option opt_pack1 = opt;
387 opt_pack1.blob_allocator = opt.workspace_allocator;
388
389 convert_packing(reference_blob, reference_blob_unpacked, 1, opt_pack1);
390 }
391
392 std::vector<Mat> bottom_blobs_unpacked(2);
393 bottom_blobs_unpacked[0] = bottom_blob_unpacked;
394 bottom_blobs_unpacked[1] = reference_blob_unpacked;
395
396 return Crop::forward(bottom_blobs_unpacked, top_blobs, opt);
397}
398
399} // namespace ncnn