Elffuss BraidHub

kikocis/ncnn public

master · 12.1 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
15template<GridSample::PaddingMode pd, bool align_corner>
16void gridsample_2d_nearest_compute_blob(const Mat& src, const Mat& grid, Mat& offset_value, int permute_fusion)
17{
18 const int grid_size = grid.w * grid.h;
19
20 float* offset_ptr = offset_value.channel(0);
21
22 grid_sample_unormalize<align_corner> unormalize;
23 compute_coord<pd, align_corner> get_coord;
24
25 if (permute_fusion == 0)
26 {
27 for (int y = 0; y < grid.c; y++)
28 {
29 const float* gridptr = grid.channel(y);
30 int x = 0;
31#if __AVX__
32 for (; x + 15 < grid_size; x += 16)
33 {
34 __m256 gx = _mm256_loadu_ps(gridptr);
35 __m256 gy = _mm256_loadu_ps(gridptr + 8);
36
37 transpose2x8_ps(gx, gy);
38
39 gx = unormalize(_mm256_set1_ps(src.w), gx);
40 gx = get_coord(_mm256_set1_ps(src.w), gx);
41
42 gy = unormalize(_mm256_set1_ps(src.h), gy);
43 gy = get_coord(_mm256_set1_ps(src.h), gy);
44
45 gx = _mm256_floor_ps(_mm256_add_ps(gx, _mm256_set1_ps(0.5f)));
46 gy = _mm256_floor_ps(_mm256_add_ps(gy, _mm256_set1_ps(0.5f)));
47
48 __m256 v_in_range = _mm256_and_ps(_mm256_and_ps(_mm256_cmp_ps(gx, _mm256_set1_ps(-1), _CMP_GT_OS), _mm256_cmp_ps(_mm256_set1_ps(src.w), gx, _CMP_GT_OS)),
49 _mm256_and_ps(_mm256_cmp_ps(gy, _mm256_set1_ps(-1), _CMP_GT_OS), _mm256_cmp_ps(_mm256_set1_ps(src.h), gy, _CMP_GT_OS)));
50
51 __m256 offset = _mm256_mul_ps(_mm256_comp_fmadd_ps(gy, _mm256_set1_ps(src.w), gx), _mm256_set1_ps(src.elempack));
52
53 offset = _mm256_blendv_ps(_mm256_set1_ps(-1.0f), _mm256_castsi256_ps(_mm256_cvtps_epi32(offset)), v_in_range);
54
55 _mm256_storeu_ps(offset_ptr, offset);
56
57 gridptr += 16;
58 offset_ptr += 8;
59 }
60
61#endif // __AVX__
62
63 for (; x < grid_size; x += 2)
64 {
65 float sample_x = *gridptr;
66 float sample_y = *(gridptr + 1);
67
68 sample_x = unormalize(src.w, sample_x);
69 sample_x = get_coord(src.w, sample_x);
70
71 sample_y = unormalize(src.h, sample_y);
72 sample_y = get_coord(src.h, sample_y);
73
74 int x0 = static_cast<int>(floorf(sample_x + 0.5f));
75 int y0 = static_cast<int>(floorf(sample_y + 0.5f));
76
77 bool in_bound = ((x0 > -1) & (x0 < src.w) & (y0 > -1) & (y0 < src.h));
78
79 int* iptr = (int*)offset_ptr;
80 *iptr = in_bound ? (x0 + y0 * src.w) * src.elempack : -1.0;
81
82 gridptr += 2;
83 offset_ptr++;
84 }
85 }
86 }
87 else
88 {
89 const float* gridptr_x = grid.channel(0);
90 const float* gridptr_y = grid.channel(1);
91
92 int x = 0;
93#if __AVX__
94 for (; x + 7 < grid_size; x += 8)
95 {
96 __m256 gx = _mm256_loadu_ps(gridptr_x);
97 __m256 gy = _mm256_loadu_ps(gridptr_y);
98
99 gx = unormalize(_mm256_set1_ps(src.w), gx);
100 gx = get_coord(_mm256_set1_ps(src.w), gx);
101
102 gy = unormalize(_mm256_set1_ps(src.h), gy);
103 gy = get_coord(_mm256_set1_ps(src.h), gy);
104
105 gx = _mm256_floor_ps(_mm256_add_ps(gx, _mm256_set1_ps(0.5f)));
106 gy = _mm256_floor_ps(_mm256_add_ps(gy, _mm256_set1_ps(0.5f)));
107
108 __m256 v_in_range = _mm256_and_ps(_mm256_and_ps(_mm256_cmp_ps(gx, _mm256_set1_ps(-1), _CMP_GT_OS), _mm256_cmp_ps(_mm256_set1_ps(src.w), gx, _CMP_GT_OS)),
109 _mm256_and_ps(_mm256_cmp_ps(gy, _mm256_set1_ps(-1), _CMP_GT_OS), _mm256_cmp_ps(_mm256_set1_ps(src.h), gy, _CMP_GT_OS)));
110
111 __m256 offset = _mm256_mul_ps(_mm256_comp_fmadd_ps(gy, _mm256_set1_ps(src.w), gx), _mm256_set1_ps(src.elempack));
112
113 offset = _mm256_blendv_ps(_mm256_set1_ps(-1.0f), _mm256_castsi256_ps(_mm256_cvtps_epi32(offset)), v_in_range);
114
115 _mm256_storeu_ps(offset_ptr, offset);
116
117 gridptr_x += 8;
118 gridptr_y += 8;
119 offset_ptr += 8;
120 }
121
122#endif // __AVX__
123
124 for (; x < grid_size; x++)
125 {
126 float sample_x = *gridptr_x;
127 float sample_y = *gridptr_y;
128
129 sample_x = unormalize(src.w, sample_x);
130 sample_x = get_coord(src.w, sample_x);
131
132 sample_y = unormalize(src.h, sample_y);
133 sample_y = get_coord(src.h, sample_y);
134
135 int x0 = static_cast<int>(floorf(sample_x + 0.5f));
136 int y0 = static_cast<int>(floorf(sample_y + 0.5f));
137
138 bool in_bound = ((x0 > -1) & (x0 < src.w) & (y0 > -1) & (y0 < src.h));
139
140 int* iptr = (int*)offset_ptr;
141 *iptr = in_bound ? (x0 + y0 * src.w) * src.elempack : -1.0;
142
143 gridptr_x++;
144 gridptr_y++;
145
146 offset_ptr++;
147 }
148 }
149}
150
151template<GridSample::PaddingMode pd, bool align_corner>
152void gridsample_3d_nearest_compute_blob(const Mat& src, const Mat& grid, Mat& offset_value, int permute_fusion)
153{
154 const int grid_size = grid.w * grid.h * grid.d;
155
156 float* offset_ptr = offset_value.channel(0);
157
158 grid_sample_unormalize<align_corner> unormalize;
159 compute_coord<pd, align_corner> get_coord;
160
161 if (permute_fusion == 0)
162 {
163 for (int y = 0; y < grid.c; y++)
164 {
165 const float* gridptr = grid.channel(y);
166 int x = 0;
167#if __AVX__
168 for (; x + 23 < grid_size; x += 24)
169 {
170 __m256 gx = _mm256_loadu_ps(gridptr);
171 __m256 gy = _mm256_loadu_ps(gridptr + 8);
172 __m256 gz = _mm256_loadu_ps(gridptr + 16);
173
174 transpose3x8_ps(gx, gy, gz);
175
176 gx = unormalize(_mm256_set1_ps(src.w), gx);
177 gx = get_coord(_mm256_set1_ps(src.w), gx);
178
179 gy = unormalize(_mm256_set1_ps(src.h), gy);
180 gy = get_coord(_mm256_set1_ps(src.h), gy);
181
182 gz = unormalize(_mm256_set1_ps(src.d), gz);
183 gz = get_coord(_mm256_set1_ps(src.d), gz);
184
185 gx = _mm256_floor_ps(_mm256_add_ps(gx, _mm256_set1_ps(0.5f)));
186 gy = _mm256_floor_ps(_mm256_add_ps(gy, _mm256_set1_ps(0.5f)));
187 gz = _mm256_floor_ps(_mm256_add_ps(gz, _mm256_set1_ps(0.5f)));
188
189 __m256 v_in_range = _mm256_and_ps(_mm256_and_ps(_mm256_cmp_ps(gx, _mm256_set1_ps(-1), _CMP_GT_OS), _mm256_cmp_ps(_mm256_set1_ps(src.w), gx, _CMP_GT_OS)),
190 _mm256_and_ps(_mm256_cmp_ps(gy, _mm256_set1_ps(-1), _CMP_GT_OS), _mm256_cmp_ps(_mm256_set1_ps(src.h), gy, _CMP_GT_OS)));
191 v_in_range = _mm256_and_ps(v_in_range, _mm256_and_ps(_mm256_cmp_ps(gz, _mm256_set1_ps(-1), _CMP_GT_OS), _mm256_cmp_ps(_mm256_set1_ps(src.d), gz, _CMP_GT_OS)));
192
193 __m256 offset = _mm256_mul_ps(_mm256_comp_fmadd_ps(_mm256_mul_ps(_mm256_set1_ps(src.w), _mm256_set1_ps(src.h)), gz,
194 _mm256_comp_fmadd_ps(gy, _mm256_set1_ps(src.w), gx)),
195 _mm256_set1_ps(src.elempack));
196
197 offset = _mm256_blendv_ps(_mm256_set1_ps(-1.0f), _mm256_castsi256_ps(_mm256_cvtps_epi32(offset)), v_in_range);
198
199 _mm256_storeu_ps(offset_ptr, offset);
200
201 gridptr += 24;
202 offset_ptr += 8;
203 }
204
205#endif // __AVX__
206
207 for (; x < grid_size; x += 3)
208 {
209 float sample_x = *gridptr;
210 float sample_y = *(gridptr + 1);
211 float sample_z = *(gridptr + 2);
212
213 sample_x = unormalize(src.w, sample_x);
214 sample_x = get_coord(src.w, sample_x);
215
216 sample_y = unormalize(src.h, sample_y);
217 sample_y = get_coord(src.h, sample_y);
218
219 sample_z = unormalize(src.d, sample_z);
220 sample_z = get_coord(src.d, sample_z);
221
222 int x0 = static_cast<int>(floorf(sample_x + 0.5f));
223 int y0 = static_cast<int>(floorf(sample_y + 0.5f));
224 int z0 = static_cast<int>(floorf(sample_z + 0.5f));
225
226 bool in_bound = ((x0 > -1) & (x0 < src.w) & (y0 > -1) & (y0 < src.h) & (z0 > -1) & (z0 < src.d));
227
228 int* iptr = (int*)offset_ptr;
229 *iptr = in_bound ? (x0 + y0 * src.w + z0 * src.w * src.h) * src.elempack : -1.0;
230
231 gridptr += 3;
232 offset_ptr++;
233 }
234 }
235 }
236 else
237 {
238 const float* gridptr_x = grid.channel(0);
239 const float* gridptr_y = grid.channel(1);
240 const float* gridptr_z = grid.channel(2);
241
242 int x = 0;
243#if __AVX__
244 for (; x + 7 < grid_size; x += 8)
245 {
246 __m256 gx = _mm256_loadu_ps(gridptr_x);
247 __m256 gy = _mm256_loadu_ps(gridptr_y);
248 __m256 gz = _mm256_loadu_ps(gridptr_z);
249
250 gx = unormalize(_mm256_set1_ps(src.w), gx);
251 gx = get_coord(_mm256_set1_ps(src.w), gx);
252
253 gy = unormalize(_mm256_set1_ps(src.h), gy);
254 gy = get_coord(_mm256_set1_ps(src.h), gy);
255
256 gz = unormalize(_mm256_set1_ps(src.d), gz);
257 gz = get_coord(_mm256_set1_ps(src.d), gz);
258
259 gx = _mm256_floor_ps(_mm256_add_ps(gx, _mm256_set1_ps(0.5f)));
260 gy = _mm256_floor_ps(_mm256_add_ps(gy, _mm256_set1_ps(0.5f)));
261 gz = _mm256_floor_ps(_mm256_add_ps(gz, _mm256_set1_ps(0.5f)));
262
263 __m256 v_in_range = _mm256_and_ps(_mm256_and_ps(_mm256_cmp_ps(gx, _mm256_set1_ps(-1), _CMP_GT_OS), _mm256_cmp_ps(_mm256_set1_ps(src.w), gx, _CMP_GT_OS)),
264 _mm256_and_ps(_mm256_cmp_ps(gy, _mm256_set1_ps(-1), _CMP_GT_OS), _mm256_cmp_ps(_mm256_set1_ps(src.h), gy, _CMP_GT_OS)));
265 v_in_range = _mm256_and_ps(v_in_range, _mm256_and_ps(_mm256_cmp_ps(gz, _mm256_set1_ps(-1), _CMP_GT_OS), _mm256_cmp_ps(_mm256_set1_ps(src.d), gz, _CMP_GT_OS)));
266
267 __m256 offset = _mm256_mul_ps(_mm256_comp_fmadd_ps(_mm256_mul_ps(_mm256_set1_ps(src.w), _mm256_set1_ps(src.h)), gz,
268 _mm256_comp_fmadd_ps(gy, _mm256_set1_ps(src.w), gx)),
269 _mm256_set1_ps(src.elempack));
270
271 offset = _mm256_blendv_ps(_mm256_set1_ps(-1.0f), _mm256_castsi256_ps(_mm256_cvtps_epi32(offset)), v_in_range);
272
273 _mm256_storeu_ps(offset_ptr, offset);
274
275 gridptr_x += 8;
276 gridptr_y += 8;
277 gridptr_z += 8;
278
279 offset_ptr += 8;
280 }
281
282#endif // __AVX__
283
284 for (; x < grid_size; x++)
285 {
286 float sample_x = *gridptr_x;
287 float sample_y = *gridptr_y;
288 float sample_z = *gridptr_z;
289
290 sample_x = unormalize(src.w, sample_x);
291 sample_x = get_coord(src.w, sample_x);
292
293 sample_y = unormalize(src.h, sample_y);
294 sample_y = get_coord(src.h, sample_y);
295
296 sample_z = unormalize(src.d, sample_z);
297 sample_z = get_coord(src.d, sample_z);
298
299 int x0 = static_cast<int>(floorf(sample_x + 0.5f));
300 int y0 = static_cast<int>(floorf(sample_y + 0.5f));
301 int z0 = static_cast<int>(floorf(sample_z + 0.5f));
302
303 bool in_bound = ((x0 > -1) & (x0 < src.w) & (y0 > -1) & (y0 < src.h) & (z0 > -1) & (z0 < src.d));
304
305 int* iptr = (int*)offset_ptr;
306 *iptr = in_bound ? (x0 + y0 * src.w + z0 * src.w * src.h) * src.elempack : -1.0;
307
308 gridptr_x++;
309 gridptr_y++;
310 gridptr_z++;
311
312 offset_ptr++;
313 }
314 }
315}