Elffuss BraidHub

kikocis/ncnn public

master · 16.8 KiBRawPermalink
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 "gridsample_x86.h"
16
17#if __SSE2__
18#include <emmintrin.h>
19#include "sse_mathfun.h"
20#if __AVX__
21#include <immintrin.h>
22#include "avx_mathfun.h"
23#if __AVX512F__
24#include "avx512_mathfun.h"
25#endif // __AVX512F__
26#endif // __AVX__
27#endif // __SSE2__
28#include "x86_usability.h"
29#include "cpu.h"
30
31namespace ncnn {
32
33#include "gridsample_compute_blob.h"
34#include "gridsample_bilinear_apply_interpolation.h"
35#include "gridsample_bicubic_apply_interpolation.h"
36#include "gridsample_nearest_apply_interpolation.h"
37
38GridSample_x86::GridSample_x86()
39{
40#if __SSE2__
41 support_packing = true;
42#endif // __SSE2__
43}
44
45int GridSample_x86::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
46{
47 const Mat& bottom_blob = bottom_blobs[0];
48 const Mat& grid = bottom_blobs[1];
49 Mat& top_blob = top_blobs[0];
50 int elempack = bottom_blob.elempack;
51
52 int channels = bottom_blob.c;
53 int dims = bottom_blob.dims;
54 size_t elemsize = bottom_blob.elemsize;
55
56 int outw, outh, outd;
57 Mat offset_value_blob;
58
59 Mat grid_p1;
60 if (grid.elempack != 1)
61 {
62 convert_packing(grid, grid_p1, 1, opt);
63 }
64 else
65 {
66 grid_p1 = grid;
67 }
68
69 if (dims == 3)
70 {
71 outw = permute_fusion == 0 ? grid_p1.h : grid_p1.w;
72 outh = permute_fusion == 0 ? grid_p1.c : grid_p1.h;
73
74 top_blob.create(outw, outh, channels, elemsize, elempack, opt.blob_allocator);
75 if (top_blob.empty())
76 return -100;
77
78 if (sample_type == GridSample::Interpolation_BILINEAR)
79 {
80 offset_value_blob.create(outw, outh, elemsize * 6, 6, opt.workspace_allocator);
81 if (offset_value_blob.empty())
82 return -100;
83
84 if (padding_mode == GridSample::Padding_ZEROS)
85 {
86 if (align_corner == 0)
87 {
88 gridsample_2d_bilinear_compute_blob<GridSample::Padding_ZEROS, false>(bottom_blob, grid_p1, offset_value_blob, permute_fusion);
89 }
90 else
91 {
92 gridsample_2d_bilinear_compute_blob<GridSample::Padding_ZEROS, true>(bottom_blob, grid_p1, offset_value_blob, permute_fusion);
93 }
94 }
95 else if (padding_mode == GridSample::Padding_BORDER)
96 {
97 if (align_corner == 0)
98 {
99 gridsample_2d_bilinear_compute_blob<GridSample::Padding_BORDER, false>(bottom_blob, grid_p1, offset_value_blob, permute_fusion);
100 }
101 else
102 {
103 gridsample_2d_bilinear_compute_blob<GridSample::Padding_BORDER, true>(bottom_blob, grid_p1, offset_value_blob, permute_fusion);
104 }
105 }
106 else if (padding_mode == GridSample::Padding_REFLECTION)
107 {
108 if (align_corner == 0)
109 {
110 gridsample_2d_bilinear_compute_blob<GridSample::Padding_REFLECTION, false>(bottom_blob, grid_p1, offset_value_blob, permute_fusion);
111 }
112 else
113 {
114 gridsample_2d_bilinear_compute_blob<GridSample::Padding_REFLECTION, true>(bottom_blob, grid_p1, offset_value_blob, permute_fusion);
115 }
116 }
117 else
118 {
119 NCNN_LOGE("gridsample padding_mode error\n");
120 return -100;
121 }
122 }
123
124 if (sample_type == GridSample::Interpolation_NEAREST)
125 {
126 offset_value_blob.create(outw, outh, 1, elemsize, 1, opt.workspace_allocator);
127 if (offset_value_blob.empty())
128 return -100;
129
130 if (padding_mode == GridSample::Padding_ZEROS)
131 {
132 if (align_corner == 0)
133 {
134 gridsample_2d_nearest_compute_blob<GridSample::Padding_ZEROS, false>(bottom_blob, grid_p1, offset_value_blob, permute_fusion);
135 }
136 else
137 {
138 gridsample_2d_nearest_compute_blob<GridSample::Padding_ZEROS, true>(bottom_blob, grid_p1, offset_value_blob, permute_fusion);
139 }
140 }
141 else if (padding_mode == GridSample::Padding_BORDER)
142 {
143 if (align_corner == 0)
144 {
145 gridsample_2d_nearest_compute_blob<GridSample::Padding_BORDER, false>(bottom_blob, grid_p1, offset_value_blob, permute_fusion);
146 }
147 else
148 {
149 gridsample_2d_nearest_compute_blob<GridSample::Padding_BORDER, true>(bottom_blob, grid_p1, offset_value_blob, permute_fusion);
150 }
151 }
152 else if (padding_mode == GridSample::Padding_REFLECTION)
153 {
154 if (align_corner == 0)
155 {
156 gridsample_2d_nearest_compute_blob<GridSample::Padding_REFLECTION, false>(bottom_blob, grid_p1, offset_value_blob, permute_fusion);
157 }
158 else
159 {
160 gridsample_2d_nearest_compute_blob<GridSample::Padding_REFLECTION, true>(bottom_blob, grid_p1, offset_value_blob, permute_fusion);
161 }
162 }
163 else
164 {
165 NCNN_LOGE("gridsample padding_mode error\n");
166 return -100;
167 }
168 }
169
170 if (sample_type == GridSample::Interpolation_BICUBIC)
171 {
172 offset_value_blob.create(outw, outh, elemsize * 18, 18, opt.workspace_allocator);
173 if (offset_value_blob.empty())
174 return -100;
175
176 if (padding_mode == GridSample::Padding_ZEROS)
177 {
178 if (align_corner == 0)
179 {
180 gridsample_2d_bicubic_compute_blob<GridSample::Padding_ZEROS, false>(bottom_blob, grid_p1, offset_value_blob, permute_fusion);
181 }
182 else
183 {
184 gridsample_2d_bicubic_compute_blob<GridSample::Padding_ZEROS, true>(bottom_blob, grid_p1, offset_value_blob, permute_fusion);
185 }
186 }
187 else if (padding_mode == GridSample::Padding_BORDER)
188 {
189 if (align_corner == 0)
190 {
191 gridsample_2d_bicubic_compute_blob<GridSample::Padding_BORDER, false>(bottom_blob, grid_p1, offset_value_blob, permute_fusion);
192 }
193 else
194 {
195 gridsample_2d_bicubic_compute_blob<GridSample::Padding_BORDER, true>(bottom_blob, grid_p1, offset_value_blob, permute_fusion);
196 }
197 }
198 else if (padding_mode == GridSample::Padding_REFLECTION)
199 {
200 if (align_corner == 0)
201 {
202 gridsample_2d_bicubic_compute_blob<GridSample::Padding_REFLECTION, false>(bottom_blob, grid_p1, offset_value_blob, permute_fusion);
203 }
204 else
205 {
206 gridsample_2d_bicubic_compute_blob<GridSample::Padding_REFLECTION, true>(bottom_blob, grid_p1, offset_value_blob, permute_fusion);
207 }
208 }
209 else
210 {
211 NCNN_LOGE("gridsample padding_mode error\n");
212 return -100;
213 }
214 }
215 }
216
217 if (dims == 4)
218 {
219 outw = permute_fusion == 0 ? grid_p1.h : grid_p1.w;
220 outh = permute_fusion == 0 ? grid_p1.d : grid_p1.h;
221 outd = permute_fusion == 0 ? grid_p1.c : grid_p1.d;
222
223 top_blob.create(outw, outh, outd, channels, elemsize, elempack, opt.blob_allocator);
224 if (top_blob.empty())
225 return -100;
226
227 if (sample_type == GridSample::Interpolation_BILINEAR)
228 {
229 offset_value_blob.create(outw, outh, outd, elemsize * 11, 11, opt.workspace_allocator);
230 if (offset_value_blob.empty())
231 return -100;
232
233 if (padding_mode == GridSample::Padding_ZEROS)
234 {
235 if (align_corner == 0)
236 {
237 gridsample_3d_bilinear_compute_blob<GridSample::Padding_ZEROS, false>(bottom_blob, grid_p1, offset_value_blob, permute_fusion);
238 }
239 else
240 {
241 gridsample_3d_bilinear_compute_blob<GridSample::Padding_ZEROS, true>(bottom_blob, grid_p1, offset_value_blob, permute_fusion);
242 }
243 }
244 else if (padding_mode == GridSample::Padding_BORDER)
245 {
246 if (align_corner == 0)
247 {
248 gridsample_3d_bilinear_compute_blob<GridSample::Padding_BORDER, false>(bottom_blob, grid_p1, offset_value_blob, permute_fusion);
249 }
250 else
251 {
252 gridsample_3d_bilinear_compute_blob<GridSample::Padding_BORDER, true>(bottom_blob, grid_p1, offset_value_blob, permute_fusion);
253 }
254 }
255 else if (padding_mode == GridSample::Padding_REFLECTION)
256 {
257 if (align_corner == 0)
258 {
259 gridsample_3d_bilinear_compute_blob<GridSample::Padding_REFLECTION, false>(bottom_blob, grid_p1, offset_value_blob, permute_fusion);
260 }
261 else
262 {
263 gridsample_3d_bilinear_compute_blob<GridSample::Padding_REFLECTION, true>(bottom_blob, grid_p1, offset_value_blob, permute_fusion);
264 }
265 }
266 else
267 {
268 NCNN_LOGE("gridsample padding_mode error\n");
269 return -100;
270 }
271 }
272
273 if (sample_type == GridSample::Interpolation_NEAREST)
274 {
275 offset_value_blob.create(outw, outh, outd, 1, elemsize, 1, opt.workspace_allocator);
276 if (offset_value_blob.empty())
277 return -100;
278
279 if (padding_mode == GridSample::Padding_ZEROS)
280 {
281 if (align_corner == 0)
282 {
283 gridsample_3d_nearest_compute_blob<GridSample::Padding_ZEROS, false>(bottom_blob, grid_p1, offset_value_blob, permute_fusion);
284 }
285 else
286 {
287 gridsample_3d_nearest_compute_blob<GridSample::Padding_ZEROS, true>(bottom_blob, grid_p1, offset_value_blob, permute_fusion);
288 }
289 }
290 else if (padding_mode == GridSample::Padding_BORDER)
291 {
292 if (align_corner == 0)
293 {
294 gridsample_3d_nearest_compute_blob<GridSample::Padding_BORDER, false>(bottom_blob, grid_p1, offset_value_blob, permute_fusion);
295 }
296 else
297 {
298 gridsample_3d_nearest_compute_blob<GridSample::Padding_BORDER, true>(bottom_blob, grid_p1, offset_value_blob, permute_fusion);
299 }
300 }
301 else if (padding_mode == GridSample::Padding_REFLECTION)
302 {
303 if (align_corner == 0)
304 {
305 gridsample_3d_nearest_compute_blob<GridSample::Padding_REFLECTION, false>(bottom_blob, grid_p1, offset_value_blob, permute_fusion);
306 }
307 else
308 {
309 gridsample_3d_nearest_compute_blob<GridSample::Padding_REFLECTION, true>(bottom_blob, grid_p1, offset_value_blob, permute_fusion);
310 }
311 }
312 else
313 {
314 NCNN_LOGE("gridsample padding_mode error\n");
315 return -100;
316 }
317 }
318
319 if (sample_type == 3)
320 {
321 NCNN_LOGE("unsupported bicubic when dims == 4");
322 return -100;
323 }
324 }
325
326#if __SSE2__
327#if __AVX__
328#if __AVX512F__
329 if (elempack == 16)
330 {
331 if (dims == 3)
332 {
333 if (sample_type == GridSample::Interpolation_BILINEAR)
334 {
335 gridsample_2d_bilinear_apply_interpolation_p16(bottom_blob, top_blob, offset_value_blob, opt);
336 }
337 else if (sample_type == GridSample::Interpolation_NEAREST)
338 {
339 gridsample_nearest_apply_interpolation_p16(bottom_blob, top_blob, offset_value_blob, opt);
340 }
341 else if (sample_type == GridSample::Interpolation_BICUBIC)
342 {
343 gridsample_2d_bicubic_apply_interpolation_p16(bottom_blob, top_blob, offset_value_blob, opt);
344 }
345 }
346 else if (dims == 4)
347 {
348 if (sample_type == GridSample::Interpolation_BILINEAR)
349 {
350 gridsample_3d_bilinear_apply_interpolation_p16(bottom_blob, top_blob, offset_value_blob, opt);
351 }
352 else if (sample_type == GridSample::Interpolation_NEAREST)
353 {
354 gridsample_nearest_apply_interpolation_p16(bottom_blob, top_blob, offset_value_blob, opt);
355 }
356 }
357 }
358#endif // __AVX512F__
359 if (elempack == 8)
360 {
361 if (dims == 3)
362 {
363 if (sample_type == GridSample::Interpolation_BILINEAR)
364 {
365 gridsample_2d_bilinear_apply_interpolation_p8(bottom_blob, top_blob, offset_value_blob, opt);
366 }
367 else if (sample_type == GridSample::Interpolation_NEAREST)
368 {
369 gridsample_nearest_apply_interpolation_p8(bottom_blob, top_blob, offset_value_blob, opt);
370 }
371 else if (sample_type == GridSample::Interpolation_BICUBIC)
372 {
373 gridsample_2d_bicubic_apply_interpolation_p8(bottom_blob, top_blob, offset_value_blob, opt);
374 }
375 }
376 else if (dims == 4)
377 {
378 if (sample_type == GridSample::Interpolation_BILINEAR)
379 {
380 gridsample_3d_bilinear_apply_interpolation_p8(bottom_blob, top_blob, offset_value_blob, opt);
381 }
382 else if (sample_type == GridSample::Interpolation_NEAREST)
383 {
384 gridsample_nearest_apply_interpolation_p8(bottom_blob, top_blob, offset_value_blob, opt);
385 }
386 }
387 }
388
389#endif // __AVX__
390 if (elempack == 4)
391 {
392 if (dims == 3)
393 {
394 if (sample_type == GridSample::Interpolation_BILINEAR)
395 {
396 gridsample_2d_bilinear_apply_interpolation_p4(bottom_blob, top_blob, offset_value_blob, opt);
397 }
398 else if (sample_type == GridSample::Interpolation_NEAREST)
399 {
400 gridsample_nearest_apply_interpolation_p4(bottom_blob, top_blob, offset_value_blob, opt);
401 }
402 else if (sample_type == GridSample::Interpolation_BICUBIC)
403 {
404 gridsample_2d_bicubic_apply_interpolation_p4(bottom_blob, top_blob, offset_value_blob, opt);
405 }
406 }
407 else if (dims == 4)
408 {
409 if (sample_type == GridSample::Interpolation_BILINEAR)
410 {
411 gridsample_3d_bilinear_apply_interpolation_p4(bottom_blob, top_blob, offset_value_blob, opt);
412 }
413 else if (sample_type == GridSample::Interpolation_NEAREST)
414 {
415 gridsample_nearest_apply_interpolation_p4(bottom_blob, top_blob, offset_value_blob, opt);
416 }
417 }
418 }
419
420#endif // __SSE2__
421
422 if (elempack == 1)
423 {
424 if (dims == 3)
425 {
426 if (sample_type == GridSample::Interpolation_BILINEAR)
427 {
428 gridsample_2d_bilinear_apply_interpolation_p1(bottom_blob, top_blob, offset_value_blob, opt);
429 }
430 else if (sample_type == GridSample::Interpolation_NEAREST)
431 {
432 gridsample_nearest_apply_interpolation_p1(bottom_blob, top_blob, offset_value_blob, opt);
433 }
434 else if (sample_type == GridSample::Interpolation_BICUBIC)
435 {
436 gridsample_2d_bicubic_apply_interpolation_p1(bottom_blob, top_blob, offset_value_blob, opt);
437 }
438 }
439 else if (dims == 4)
440 {
441 if (sample_type == GridSample::Interpolation_BILINEAR)
442 {
443 gridsample_3d_bilinear_apply_interpolation_p1(bottom_blob, top_blob, offset_value_blob, opt);
444 }
445 else if (sample_type == GridSample::Interpolation_NEAREST)
446 {
447 gridsample_nearest_apply_interpolation_p1(bottom_blob, top_blob, offset_value_blob, opt);
448 }
449 }
450 }
451
452 return 0;
453}
454
455} // namespace ncnn