Elffuss BraidHub

kikocis/ncnn public

master · 89.1 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2022 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 "softmax_x86.h"
16
17#include <float.h>
18
19#if __SSE2__
20#include <emmintrin.h>
21#include "sse_mathfun.h"
22#if __AVX__
23#include <immintrin.h>
24#include "avx_mathfun.h"
25#if __AVX512F__
26#include "avx512_mathfun.h"
27#endif // __AVX512F__
28#endif // __AVX__
29#endif // __SSE2__
30
31#include "x86_usability.h"
32
33namespace ncnn {
34
35Softmax_x86::Softmax_x86()
36{
37#if __SSE2__
38 support_packing = true;
39#endif // __SSE2__
40}
41
42int Softmax_x86::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
43{
44 int dims = bottom_top_blob.dims;
45 size_t elemsize = bottom_top_blob.elemsize;
46 int elempack = bottom_top_blob.elempack;
47 int positive_axis = axis < 0 ? dims + axis : axis;
48
49#if __SSE2__
50#if __AVX__
51#if __AVX512F__
52 if (elempack == 16)
53 {
54 if (dims == 1) // positive_axis == 0
55 {
56 int w = bottom_top_blob.w;
57
58 float* ptr = bottom_top_blob;
59
60 __m512 _max = _mm512_set1_ps(-FLT_MAX);
61 for (int i = 0; i < w; i++)
62 {
63 __m512 _p = _mm512_load_ps(ptr + i * 16);
64 _max = _mm512_max_ps(_max, _p);
65 }
66 _max = _mm512_max_ps(_max, _mm512_permute_ps(_max, _MM_SHUFFLE(2, 3, 0, 1)));
67 _max = _mm512_max_ps(_max, _mm512_permute_ps(_max, _MM_SHUFFLE(1, 0, 3, 2)));
68 _max = _mm512_max_ps(_max, _mm512_shuffle_f32x4(_max, _max, _MM_SHUFFLE(2, 3, 0, 1)));
69 _max = _mm512_max_ps(_max, _mm512_shuffle_f32x4(_max, _max, _MM_SHUFFLE(1, 0, 3, 2)));
70
71 __m512 _sum = _mm512_setzero_ps();
72 for (int i = 0; i < w; i++)
73 {
74 __m512 _p = _mm512_load_ps(ptr + i * 16);
75 _p = exp512_ps(_mm512_sub_ps(_p, _max));
76 _mm512_store_ps(ptr + i * 16, _p);
77 _sum = _mm512_add_ps(_sum, _p);
78 }
79 _sum = _mm512_add_ps(_sum, _mm512_permute_ps(_sum, _MM_SHUFFLE(2, 3, 0, 1)));
80 _sum = _mm512_add_ps(_sum, _mm512_permute_ps(_sum, _MM_SHUFFLE(1, 0, 3, 2)));
81 _sum = _mm512_add_ps(_sum, _mm512_shuffle_f32x4(_sum, _sum, _MM_SHUFFLE(2, 3, 0, 1)));
82 _sum = _mm512_add_ps(_sum, _mm512_shuffle_f32x4(_sum, _sum, _MM_SHUFFLE(1, 0, 3, 2)));
83
84 for (int i = 0; i < w; i++)
85 {
86 __m512 _p = _mm512_load_ps(ptr + i * 16);
87 _p = _mm512_div_ps(_p, _sum);
88 _mm512_store_ps(ptr + i * 16, _p);
89 }
90 }
91
92 if (dims == 2 && positive_axis == 0)
93 {
94 int w = bottom_top_blob.w;
95 int h = bottom_top_blob.h;
96
97 Mat max;
98 max.create(w, 4u, 1, opt.workspace_allocator);
99 if (max.empty())
100 return -100;
101 max.fill(-FLT_MAX);
102
103 for (int i = 0; i < h; i++)
104 {
105 const float* ptr = bottom_top_blob.row(i);
106 float* pmax = max;
107
108 int j = 0;
109 for (; j + 15 < w; j += 16)
110 {
111 __m512 _p0 = _mm512_load_ps(ptr);
112 __m512 _p1 = _mm512_load_ps(ptr + 16);
113 __m512 _p2 = _mm512_load_ps(ptr + 16 * 2);
114 __m512 _p3 = _mm512_load_ps(ptr + 16 * 3);
115 __m512 _p4 = _mm512_load_ps(ptr + 16 * 4);
116 __m512 _p5 = _mm512_load_ps(ptr + 16 * 5);
117 __m512 _p6 = _mm512_load_ps(ptr + 16 * 6);
118 __m512 _p7 = _mm512_load_ps(ptr + 16 * 7);
119 __m512 _p8 = _mm512_load_ps(ptr + 16 * 8);
120 __m512 _p9 = _mm512_load_ps(ptr + 16 * 9);
121 __m512 _pa = _mm512_load_ps(ptr + 16 * 10);
122 __m512 _pb = _mm512_load_ps(ptr + 16 * 11);
123 __m512 _pc = _mm512_load_ps(ptr + 16 * 12);
124 __m512 _pd = _mm512_load_ps(ptr + 16 * 13);
125 __m512 _pe = _mm512_load_ps(ptr + 16 * 14);
126 __m512 _pf = _mm512_load_ps(ptr + 16 * 15);
127 transpose16x16_ps(_p0, _p1, _p2, _p3, _p4, _p5, _p6, _p7, _p8, _p9, _pa, _pb, _pc, _pd, _pe, _pf);
128 __m512 _max01 = _mm512_max_ps(_p0, _p1);
129 __m512 _max23 = _mm512_max_ps(_p2, _p3);
130 __m512 _max45 = _mm512_max_ps(_p4, _p5);
131 __m512 _max67 = _mm512_max_ps(_p6, _p7);
132 __m512 _max89 = _mm512_max_ps(_p8, _p9);
133 __m512 _maxab = _mm512_max_ps(_pa, _pb);
134 __m512 _maxcd = _mm512_max_ps(_pc, _pd);
135 __m512 _maxef = _mm512_max_ps(_pe, _pf);
136 __m512 _max0123 = _mm512_max_ps(_max01, _max23);
137 __m512 _max4567 = _mm512_max_ps(_max45, _max67);
138 __m512 _max89ab = _mm512_max_ps(_max89, _maxab);
139 __m512 _maxcdef = _mm512_max_ps(_maxcd, _maxef);
140 __m512 _max01234567 = _mm512_max_ps(_max0123, _max4567);
141 __m512 _max89abcdef = _mm512_max_ps(_max89ab, _maxcdef);
142 __m512 _max = _mm512_load_ps(pmax);
143 _max = _mm512_max_ps(_max, _mm512_max_ps(_max01234567, _max89abcdef));
144 _mm512_store_ps(pmax, _max);
145
146 ptr += 256;
147 pmax += 16;
148 }
149 for (; j < w; j++)
150 {
151 __m512 _p = _mm512_load_ps(ptr);
152 *pmax = std::max(*pmax, _mm512_comp_reduce_max_ps(_p));
153
154 ptr += 16;
155 pmax++;
156 }
157 }
158
159 Mat sum;
160 sum.create(w, 4u, 1, opt.workspace_allocator);
161 if (sum.empty())
162 return -100;
163 sum.fill(0.f);
164
165 for (int i = 0; i < h; i++)
166 {
167 float* ptr = bottom_top_blob.row(i);
168 float* psum = sum;
169
170 int j = 0;
171 for (; j + 15 < w; j += 16)
172 {
173 __m512 _p0 = _mm512_load_ps(ptr);
174 __m512 _p1 = _mm512_load_ps(ptr + 16);
175 __m512 _p2 = _mm512_load_ps(ptr + 16 * 2);
176 __m512 _p3 = _mm512_load_ps(ptr + 16 * 3);
177 __m512 _p4 = _mm512_load_ps(ptr + 16 * 4);
178 __m512 _p5 = _mm512_load_ps(ptr + 16 * 5);
179 __m512 _p6 = _mm512_load_ps(ptr + 16 * 6);
180 __m512 _p7 = _mm512_load_ps(ptr + 16 * 7);
181 __m512 _p8 = _mm512_load_ps(ptr + 16 * 8);
182 __m512 _p9 = _mm512_load_ps(ptr + 16 * 9);
183 __m512 _pa = _mm512_load_ps(ptr + 16 * 10);
184 __m512 _pb = _mm512_load_ps(ptr + 16 * 11);
185 __m512 _pc = _mm512_load_ps(ptr + 16 * 12);
186 __m512 _pd = _mm512_load_ps(ptr + 16 * 13);
187 __m512 _pe = _mm512_load_ps(ptr + 16 * 14);
188 __m512 _pf = _mm512_load_ps(ptr + 16 * 15);
189 _p0 = exp512_ps(_mm512_sub_ps(_p0, _mm512_set1_ps(max[j])));
190 _p1 = exp512_ps(_mm512_sub_ps(_p1, _mm512_set1_ps(max[j + 1])));
191 _p2 = exp512_ps(_mm512_sub_ps(_p2, _mm512_set1_ps(max[j + 2])));
192 _p3 = exp512_ps(_mm512_sub_ps(_p3, _mm512_set1_ps(max[j + 3])));
193 _p4 = exp512_ps(_mm512_sub_ps(_p4, _mm512_set1_ps(max[j + 4])));
194 _p5 = exp512_ps(_mm512_sub_ps(_p5, _mm512_set1_ps(max[j + 5])));
195 _p6 = exp512_ps(_mm512_sub_ps(_p6, _mm512_set1_ps(max[j + 6])));
196 _p7 = exp512_ps(_mm512_sub_ps(_p7, _mm512_set1_ps(max[j + 7])));
197 _p8 = exp512_ps(_mm512_sub_ps(_p8, _mm512_set1_ps(max[j + 8])));
198 _p9 = exp512_ps(_mm512_sub_ps(_p9, _mm512_set1_ps(max[j + 9])));
199 _pa = exp512_ps(_mm512_sub_ps(_pa, _mm512_set1_ps(max[j + 10])));
200 _pb = exp512_ps(_mm512_sub_ps(_pb, _mm512_set1_ps(max[j + 11])));
201 _pc = exp512_ps(_mm512_sub_ps(_pc, _mm512_set1_ps(max[j + 12])));
202 _pd = exp512_ps(_mm512_sub_ps(_pd, _mm512_set1_ps(max[j + 13])));
203 _pe = exp512_ps(_mm512_sub_ps(_pe, _mm512_set1_ps(max[j + 14])));
204 _pf = exp512_ps(_mm512_sub_ps(_pf, _mm512_set1_ps(max[j + 15])));
205 _mm512_store_ps(ptr, _p0);
206 _mm512_store_ps(ptr + 16, _p1);
207 _mm512_store_ps(ptr + 16 * 2, _p2);
208 _mm512_store_ps(ptr + 16 * 3, _p3);
209 _mm512_store_ps(ptr + 16 * 4, _p4);
210 _mm512_store_ps(ptr + 16 * 5, _p5);
211 _mm512_store_ps(ptr + 16 * 6, _p6);
212 _mm512_store_ps(ptr + 16 * 7, _p7);
213 _mm512_store_ps(ptr + 16 * 8, _p8);
214 _mm512_store_ps(ptr + 16 * 9, _p9);
215 _mm512_store_ps(ptr + 16 * 10, _pa);
216 _mm512_store_ps(ptr + 16 * 11, _pb);
217 _mm512_store_ps(ptr + 16 * 12, _pc);
218 _mm512_store_ps(ptr + 16 * 13, _pd);
219 _mm512_store_ps(ptr + 16 * 14, _pe);
220 _mm512_store_ps(ptr + 16 * 15, _pf);
221 transpose16x16_ps(_p0, _p1, _p2, _p3, _p4, _p5, _p6, _p7, _p8, _p9, _pa, _pb, _pc, _pd, _pe, _pf);
222 __m512 _sum01 = _mm512_add_ps(_p0, _p1);
223 __m512 _sum23 = _mm512_add_ps(_p2, _p3);
224 __m512 _sum45 = _mm512_add_ps(_p4, _p5);
225 __m512 _sum67 = _mm512_add_ps(_p6, _p7);
226 __m512 _sum89 = _mm512_add_ps(_p8, _p9);
227 __m512 _sumab = _mm512_add_ps(_pa, _pb);
228 __m512 _sumcd = _mm512_add_ps(_pc, _pd);
229 __m512 _sumef = _mm512_add_ps(_pe, _pf);
230 __m512 _sum0123 = _mm512_add_ps(_sum01, _sum23);
231 __m512 _sum4567 = _mm512_add_ps(_sum45, _sum67);
232 __m512 _sum89ab = _mm512_add_ps(_sum89, _sumab);
233 __m512 _sumcdef = _mm512_add_ps(_sumcd, _sumef);
234 __m512 _sum01234567 = _mm512_add_ps(_sum0123, _sum4567);
235 __m512 _sum89abcdef = _mm512_add_ps(_sum89ab, _sumcdef);
236 __m512 _sum = _mm512_load_ps(psum);
237 _sum = _mm512_add_ps(_sum, _mm512_add_ps(_sum01234567, _sum89abcdef));
238 _mm512_store_ps(psum, _sum);
239
240 ptr += 256;
241 psum += 16;
242 }
243 for (; j < w; j++)
244 {
245 __m512 _p = _mm512_load_ps(ptr);
246 __m512 _max = _mm512_set1_ps(max[j]);
247 _p = exp512_ps(_mm512_sub_ps(_p, _max));
248 _mm512_store_ps(ptr, _p);
249 *psum += _mm512_comp_reduce_add_ps(_p);
250
251 ptr += 16;
252 psum++;
253 }
254 }
255
256 for (int i = 0; i < h; i++)
257 {
258 float* ptr = bottom_top_blob.row(i);
259 for (int j = 0; j < w; j++)
260 {
261 __m512 _p = _mm512_load_ps(ptr);
262 __m512 _sum = _mm512_set1_ps(sum[j]);
263 _p = _mm512_div_ps(_p, _sum);
264 _mm512_store_ps(ptr, _p);
265
266 ptr += 16;
267 }
268 }
269 }
270
271 if (dims == 2 && positive_axis == 1)
272 {
273 int w = bottom_top_blob.w;
274 int h = bottom_top_blob.h;
275
276 #pragma omp parallel for num_threads(opt.num_threads)
277 for (int i = 0; i < h; i++)
278 {
279 float* ptr = bottom_top_blob.row(i);
280
281 __m512 _max = _mm512_set1_ps(-FLT_MAX);
282 for (int j = 0; j < w; j++)
283 {
284 __m512 _p = _mm512_load_ps(ptr + j * 16);
285 _max = _mm512_max_ps(_max, _p);
286 }
287
288 __m512 _sum = _mm512_setzero_ps();
289 for (int j = 0; j < w; j++)
290 {
291 __m512 _p = _mm512_load_ps(ptr + j * 16);
292 _p = exp512_ps(_mm512_sub_ps(_p, _max));
293 _mm512_store_ps(ptr + j * 16, _p);
294 _sum = _mm512_add_ps(_sum, _p);
295 }
296
297 for (int j = 0; j < w; j++)
298 {
299 __m512 _p = _mm512_load_ps(ptr + j * 16);
300 _p = _mm512_div_ps(_p, _sum);
301 _mm512_store_ps(ptr + j * 16, _p);
302 }
303 }
304 }
305
306 if (dims == 3 && positive_axis == 0)
307 {
308 int w = bottom_top_blob.w;
309 int h = bottom_top_blob.h;
310 int channels = bottom_top_blob.c;
311 int size = w * h;
312
313 Mat max;
314 max.create(w, h, 4u, 1, opt.workspace_allocator);
315 if (max.empty())
316 return -100;
317 max.fill(-FLT_MAX);
318
319 for (int q = 0; q < channels; q++)
320 {
321 const float* ptr = bottom_top_blob.channel(q);
322 float* pmax = max;
323
324 int i = 0;
325 for (; i + 15 < size; i += 16)
326 {
327 __m512 _p0 = _mm512_load_ps(ptr);
328 __m512 _p1 = _mm512_load_ps(ptr + 16);
329 __m512 _p2 = _mm512_load_ps(ptr + 16 * 2);
330 __m512 _p3 = _mm512_load_ps(ptr + 16 * 3);
331 __m512 _p4 = _mm512_load_ps(ptr + 16 * 4);
332 __m512 _p5 = _mm512_load_ps(ptr + 16 * 5);
333 __m512 _p6 = _mm512_load_ps(ptr + 16 * 6);
334 __m512 _p7 = _mm512_load_ps(ptr + 16 * 7);
335 __m512 _p8 = _mm512_load_ps(ptr + 16 * 8);
336 __m512 _p9 = _mm512_load_ps(ptr + 16 * 9);
337 __m512 _pa = _mm512_load_ps(ptr + 16 * 10);
338 __m512 _pb = _mm512_load_ps(ptr + 16 * 11);
339 __m512 _pc = _mm512_load_ps(ptr + 16 * 12);
340 __m512 _pd = _mm512_load_ps(ptr + 16 * 13);
341 __m512 _pe = _mm512_load_ps(ptr + 16 * 14);
342 __m512 _pf = _mm512_load_ps(ptr + 16 * 15);
343 transpose16x16_ps(_p0, _p1, _p2, _p3, _p4, _p5, _p6, _p7, _p8, _p9, _pa, _pb, _pc, _pd, _pe, _pf);
344 __m512 _max01 = _mm512_max_ps(_p0, _p1);
345 __m512 _max23 = _mm512_max_ps(_p2, _p3);
346 __m512 _max45 = _mm512_max_ps(_p4, _p5);
347 __m512 _max67 = _mm512_max_ps(_p6, _p7);
348 __m512 _max89 = _mm512_max_ps(_p8, _p9);
349 __m512 _maxab = _mm512_max_ps(_pa, _pb);
350 __m512 _maxcd = _mm512_max_ps(_pc, _pd);
351 __m512 _maxef = _mm512_max_ps(_pe, _pf);
352 __m512 _max0123 = _mm512_max_ps(_max01, _max23);
353 __m512 _max4567 = _mm512_max_ps(_max45, _max67);
354 __m512 _max89ab = _mm512_max_ps(_max89, _maxab);
355 __m512 _maxcdef = _mm512_max_ps(_maxcd, _maxef);
356 __m512 _max01234567 = _mm512_max_ps(_max0123, _max4567);
357 __m512 _max89abcdef = _mm512_max_ps(_max89ab, _maxcdef);
358 __m512 _max = _mm512_load_ps(pmax);
359 _max = _mm512_max_ps(_max, _mm512_max_ps(_max01234567, _max89abcdef));
360 _mm512_store_ps(pmax, _max);
361
362 ptr += 256;
363 pmax += 16;
364 }
365 for (; i < size; i++)
366 {
367 __m512 _p = _mm512_load_ps(ptr);
368 *pmax = std::max(*pmax, _mm512_comp_reduce_max_ps(_p));
369
370 ptr += 16;
371 pmax++;
372 }
373 }
374
375 Mat sum;
376 sum.create(w, h, 4u, 1, opt.workspace_allocator);
377 if (sum.empty())
378 return -100;
379 sum.fill(0.f);
380
381 for (int q = 0; q < channels; q++)
382 {
383 float* ptr = bottom_top_blob.channel(q);
384 float* psum = sum;
385
386 int i = 0;
387 for (; i + 15 < size; i += 16)
388 {
389 __m512 _p0 = _mm512_load_ps(ptr);
390 __m512 _p1 = _mm512_load_ps(ptr + 16);
391 __m512 _p2 = _mm512_load_ps(ptr + 16 * 2);
392 __m512 _p3 = _mm512_load_ps(ptr + 16 * 3);
393 __m512 _p4 = _mm512_load_ps(ptr + 16 * 4);
394 __m512 _p5 = _mm512_load_ps(ptr + 16 * 5);
395 __m512 _p6 = _mm512_load_ps(ptr + 16 * 6);
396 __m512 _p7 = _mm512_load_ps(ptr + 16 * 7);
397 __m512 _p8 = _mm512_load_ps(ptr + 16 * 8);
398 __m512 _p9 = _mm512_load_ps(ptr + 16 * 9);
399 __m512 _pa = _mm512_load_ps(ptr + 16 * 10);
400 __m512 _pb = _mm512_load_ps(ptr + 16 * 11);
401 __m512 _pc = _mm512_load_ps(ptr + 16 * 12);
402 __m512 _pd = _mm512_load_ps(ptr + 16 * 13);
403 __m512 _pe = _mm512_load_ps(ptr + 16 * 14);
404 __m512 _pf = _mm512_load_ps(ptr + 16 * 15);
405 _p0 = exp512_ps(_mm512_sub_ps(_p0, _mm512_set1_ps(max[i])));
406 _p1 = exp512_ps(_mm512_sub_ps(_p1, _mm512_set1_ps(max[i + 1])));
407 _p2 = exp512_ps(_mm512_sub_ps(_p2, _mm512_set1_ps(max[i + 2])));
408 _p3 = exp512_ps(_mm512_sub_ps(_p3, _mm512_set1_ps(max[i + 3])));
409 _p4 = exp512_ps(_mm512_sub_ps(_p4, _mm512_set1_ps(max[i + 4])));
410 _p5 = exp512_ps(_mm512_sub_ps(_p5, _mm512_set1_ps(max[i + 5])));
411 _p6 = exp512_ps(_mm512_sub_ps(_p6, _mm512_set1_ps(max[i + 6])));
412 _p7 = exp512_ps(_mm512_sub_ps(_p7, _mm512_set1_ps(max[i + 7])));
413 _p8 = exp512_ps(_mm512_sub_ps(_p8, _mm512_set1_ps(max[i + 8])));
414 _p9 = exp512_ps(_mm512_sub_ps(_p9, _mm512_set1_ps(max[i + 9])));
415 _pa = exp512_ps(_mm512_sub_ps(_pa, _mm512_set1_ps(max[i + 10])));
416 _pb = exp512_ps(_mm512_sub_ps(_pb, _mm512_set1_ps(max[i + 11])));
417 _pc = exp512_ps(_mm512_sub_ps(_pc, _mm512_set1_ps(max[i + 12])));
418 _pd = exp512_ps(_mm512_sub_ps(_pd, _mm512_set1_ps(max[i + 13])));
419 _pe = exp512_ps(_mm512_sub_ps(_pe, _mm512_set1_ps(max[i + 14])));
420 _pf = exp512_ps(_mm512_sub_ps(_pf, _mm512_set1_ps(max[i + 15])));
421 _mm512_store_ps(ptr, _p0);
422 _mm512_store_ps(ptr + 16, _p1);
423 _mm512_store_ps(ptr + 16 * 2, _p2);
424 _mm512_store_ps(ptr + 16 * 3, _p3);
425 _mm512_store_ps(ptr + 16 * 4, _p4);
426 _mm512_store_ps(ptr + 16 * 5, _p5);
427 _mm512_store_ps(ptr + 16 * 6, _p6);
428 _mm512_store_ps(ptr + 16 * 7, _p7);
429 _mm512_store_ps(ptr + 16 * 8, _p8);
430 _mm512_store_ps(ptr + 16 * 9, _p9);
431 _mm512_store_ps(ptr + 16 * 10, _pa);
432 _mm512_store_ps(ptr + 16 * 11, _pb);
433 _mm512_store_ps(ptr + 16 * 12, _pc);
434 _mm512_store_ps(ptr + 16 * 13, _pd);
435 _mm512_store_ps(ptr + 16 * 14, _pe);
436 _mm512_store_ps(ptr + 16 * 15, _pf);
437 transpose16x16_ps(_p0, _p1, _p2, _p3, _p4, _p5, _p6, _p7, _p8, _p9, _pa, _pb, _pc, _pd, _pe, _pf);
438 __m512 _sum01 = _mm512_add_ps(_p0, _p1);
439 __m512 _sum23 = _mm512_add_ps(_p2, _p3);
440 __m512 _sum45 = _mm512_add_ps(_p4, _p5);
441 __m512 _sum67 = _mm512_add_ps(_p6, _p7);
442 __m512 _sum89 = _mm512_add_ps(_p8, _p9);
443 __m512 _sumab = _mm512_add_ps(_pa, _pb);
444 __m512 _sumcd = _mm512_add_ps(_pc, _pd);
445 __m512 _sumef = _mm512_add_ps(_pe, _pf);
446 __m512 _sum0123 = _mm512_add_ps(_sum01, _sum23);
447 __m512 _sum4567 = _mm512_add_ps(_sum45, _sum67);
448 __m512 _sum89ab = _mm512_add_ps(_sum89, _sumab);
449 __m512 _sumcdef = _mm512_add_ps(_sumcd, _sumef);
450 __m512 _sum01234567 = _mm512_add_ps(_sum0123, _sum4567);
451 __m512 _sum89abcdef = _mm512_add_ps(_sum89ab, _sumcdef);
452 __m512 _sum = _mm512_load_ps(psum);
453 _sum = _mm512_add_ps(_sum, _mm512_add_ps(_sum01234567, _sum89abcdef));
454 _mm512_store_ps(psum, _sum);
455
456 ptr += 256;
457 psum += 16;
458 }
459 for (; i < size; i++)
460 {
461 __m512 _p = _mm512_load_ps(ptr);
462 __m512 _max = _mm512_set1_ps(max[i]);
463 _p = exp512_ps(_mm512_sub_ps(_p, _max));
464 _mm512_store_ps(ptr, _p);
465 *psum += _mm512_comp_reduce_add_ps(_p);
466
467 ptr += 16;
468 psum++;
469 }
470 }
471
472 #pragma omp parallel for num_threads(opt.num_threads)
473 for (int q = 0; q < channels; q++)
474 {
475 float* ptr = bottom_top_blob.channel(q);
476
477 for (int i = 0; i < size; i++)
478 {
479 __m512 _p = _mm512_load_ps(ptr);
480 __m512 _sum = _mm512_set1_ps(sum[i]);
481 _p = _mm512_div_ps(_p, _sum);
482 _mm512_store_ps(ptr, _p);
483
484 ptr += 16;
485 }
486 }
487 }
488
489 if (dims == 3 && positive_axis == 1)
490 {
491 int w = bottom_top_blob.w;
492 int h = bottom_top_blob.h;
493 int channels = bottom_top_blob.c;
494
495 Mat max;
496 max.create(w, channels, elemsize, elempack, opt.workspace_allocator);
497 if (max.empty())
498 return -100;
499 max.fill(_mm512_set1_ps(-FLT_MAX));
500
501 #pragma omp parallel for num_threads(opt.num_threads)
502 for (int q = 0; q < channels; q++)
503 {
504 const float* ptr = bottom_top_blob.channel(q);
505
506 for (int i = 0; i < h; i++)
507 {
508 float* maxptr = max.row(q);
509
510 for (int j = 0; j < w; j++)
511 {
512 __m512 _p = _mm512_load_ps(ptr);
513 __m512 _max = _mm512_load_ps(maxptr);
514 _max = _mm512_max_ps(_max, _p);
515 _mm512_store_ps(maxptr, _max);
516
517 ptr += 16;
518 maxptr += 16;
519 }
520 }
521 }
522
523 Mat sum;
524 sum.create(w, channels, elemsize, elempack, opt.workspace_allocator);
525 if (sum.empty())
526 return -100;
527 sum.fill(_mm512_setzero_ps());
528
529 #pragma omp parallel for num_threads(opt.num_threads)
530 for (int q = 0; q < channels; q++)
531 {
532 float* ptr = bottom_top_blob.channel(q);
533
534 for (int i = 0; i < h; i++)
535 {
536 float* maxptr = max.row(q);
537 float* sumptr = sum.row(q);
538
539 for (int j = 0; j < w; j++)
540 {
541 __m512 _p = _mm512_load_ps(ptr);
542 __m512 _max = _mm512_load_ps(maxptr);
543 _p = exp512_ps(_mm512_sub_ps(_p, _max));
544 _mm512_store_ps(ptr, _p);
545 __m512 _sum = _mm512_load_ps(sumptr);
546 _sum = _mm512_add_ps(_sum, _p);
547 _mm512_store_ps(sumptr, _sum);
548
549 ptr += 16;
550 maxptr += 16;
551 sumptr += 16;
552 }
553 }
554 }
555
556 #pragma omp parallel for num_threads(opt.num_threads)
557 for (int q = 0; q < channels; q++)
558 {
559 float* ptr = bottom_top_blob.channel(q);
560
561 for (int i = 0; i < h; i++)
562 {
563 float* sumptr = sum.row(q);
564
565 for (int j = 0; j < w; j++)
566 {
567 __m512 _p = _mm512_load_ps(ptr);
568 __m512 _sum = _mm512_load_ps(sumptr);
569 _p = _mm512_div_ps(_p, _sum);
570 _mm512_store_ps(ptr, _p);
571
572 ptr += 16;
573 sumptr += 16;
574 }
575 }
576 }
577 }
578
579 if (dims == 3 && positive_axis == 2)
580 {
581 int w = bottom_top_blob.w;
582 int h = bottom_top_blob.h;
583 int channels = bottom_top_blob.c;
584
585 #pragma omp parallel for num_threads(opt.num_threads)
586 for (int q = 0; q < channels; q++)
587 {
588 float* ptr = bottom_top_blob.channel(q);
589
590 for (int i = 0; i < h; i++)
591 {
592 __m512 _max = _mm512_set1_ps(-FLT_MAX);
593 for (int j = 0; j < w; j++)
594 {
595 __m512 _p = _mm512_load_ps(ptr + j * 16);
596 _max = _mm512_max_ps(_max, _p);
597 }
598
599 __m512 _sum = _mm512_setzero_ps();
600 for (int j = 0; j < w; j++)
601 {
602 __m512 _p = _mm512_load_ps(ptr + j * 16);
603 _p = exp512_ps(_mm512_sub_ps(_p, _max));
604 _mm512_store_ps(ptr + j * 16, _p);
605 _sum = _mm512_add_ps(_sum, _p);
606 }
607
608 for (int j = 0; j < w; j++)
609 {
610 __m512 _p = _mm512_load_ps(ptr + j * 16);
611 _p = _mm512_div_ps(_p, _sum);
612 _mm512_store_ps(ptr + j * 16, _p);
613 }
614
615 ptr += w * 16;
616 }
617 }
618 }
619
620 return 0;
621 }
622#endif // __AVX512F__
623
624 if (elempack == 8)
625 {
626 if (dims == 1) // positive_axis == 0
627 {
628 int w = bottom_top_blob.w;
629
630 float* ptr = bottom_top_blob;
631
632 __m256 _max = _mm256_set1_ps(-FLT_MAX);
633 for (int i = 0; i < w; i++)
634 {
635 __m256 _p = _mm256_load_ps(ptr + i * 8);
636 _max = _mm256_max_ps(_max, _p);
637 }
638 _max = _mm256_max_ps(_max, _mm256_permute_ps(_max, _MM_SHUFFLE(2, 3, 0, 1)));
639 _max = _mm256_max_ps(_max, _mm256_permute_ps(_max, _MM_SHUFFLE(1, 0, 3, 2)));
640 _max = _mm256_max_ps(_max, _mm256_permute2f128_ps(_max, _max, _MM_SHUFFLE(0, 0, 0, 1)));
641
642 __m256 _sum = _mm256_setzero_ps();
643 for (int i = 0; i < w; i++)
644 {
645 __m256 _p = _mm256_load_ps(ptr + i * 8);
646 _p = exp256_ps(_mm256_sub_ps(_p, _max));
647 _mm256_store_ps(ptr + i * 8, _p);
648 _sum = _mm256_add_ps(_sum, _p);
649 }
650 _sum = _mm256_add_ps(_sum, _mm256_permute_ps(_sum, _MM_SHUFFLE(2, 3, 0, 1)));
651 _sum = _mm256_add_ps(_sum, _mm256_permute_ps(_sum, _MM_SHUFFLE(1, 0, 3, 2)));
652 _sum = _mm256_add_ps(_sum, _mm256_permute2f128_ps(_sum, _sum, _MM_SHUFFLE(0, 0, 0, 1)));
653
654 for (int i = 0; i < w; i++)
655 {
656 __m256 _p = _mm256_load_ps(ptr + i * 8);
657 _p = _mm256_div_ps(_p, _sum);
658 _mm256_store_ps(ptr + i * 8, _p);
659 }
660 }
661
662 if (dims == 2 && positive_axis == 0)
663 {
664 int w = bottom_top_blob.w;
665 int h = bottom_top_blob.h;
666
667 Mat max;
668 max.create(w, 4u, 1, opt.workspace_allocator);
669 if (max.empty())
670 return -100;
671 max.fill(-FLT_MAX);
672
673 for (int i = 0; i < h; i++)
674 {
675 const float* ptr = bottom_top_blob.row(i);
676 float* pmax = max;
677
678 int j = 0;
679 for (; j + 7 < w; j += 8)
680 {
681 __m256 _p0 = _mm256_load_ps(ptr);
682 __m256 _p1 = _mm256_load_ps(ptr + 8);
683 __m256 _p2 = _mm256_load_ps(ptr + 8 * 2);
684 __m256 _p3 = _mm256_load_ps(ptr + 8 * 3);
685 __m256 _p4 = _mm256_load_ps(ptr + 8 * 4);
686 __m256 _p5 = _mm256_load_ps(ptr + 8 * 5);
687 __m256 _p6 = _mm256_load_ps(ptr + 8 * 6);
688 __m256 _p7 = _mm256_load_ps(ptr + 8 * 7);
689 transpose8x8_ps(_p0, _p1, _p2, _p3, _p4, _p5, _p6, _p7);
690 __m256 _max01 = _mm256_max_ps(_p0, _p1);
691 __m256 _max23 = _mm256_max_ps(_p2, _p3);
692 __m256 _max45 = _mm256_max_ps(_p4, _p5);
693 __m256 _max67 = _mm256_max_ps(_p6, _p7);
694 __m256 _max0123 = _mm256_max_ps(_max01, _max23);
695 __m256 _max4567 = _mm256_max_ps(_max45, _max67);
696 __m256 _max01234567 = _mm256_max_ps(_max0123, _max4567);
697 __m256 _max = _mm256_load_ps(pmax);
698 _max = _mm256_max_ps(_max, _max01234567);
699 _mm256_store_ps(pmax, _max);
700
701 ptr += 64;
702 pmax += 8;
703 }
704 for (; j < w; j++)
705 {
706 __m256 _p = _mm256_load_ps(ptr);
707 *pmax = std::max(*pmax, _mm256_reduce_max_ps(_p));
708
709 ptr += 8;
710 pmax++;
711 }
712 }
713
714 Mat sum;
715 sum.create(w, 4u, 1, opt.workspace_allocator);
716 if (sum.empty())
717 return -100;
718 sum.fill(0.f);
719
720 for (int i = 0; i < h; i++)
721 {
722 float* ptr = bottom_top_blob.row(i);
723 float* psum = sum;
724 int j = 0;
725 for (; j + 7 < w; j += 8)
726 {
727 __m256 _p0 = _mm256_load_ps(ptr);
728 __m256 _p1 = _mm256_load_ps(ptr + 8);
729 __m256 _p2 = _mm256_load_ps(ptr + 8 * 2);
730 __m256 _p3 = _mm256_load_ps(ptr + 8 * 3);
731 __m256 _p4 = _mm256_load_ps(ptr + 8 * 4);
732 __m256 _p5 = _mm256_load_ps(ptr + 8 * 5);
733 __m256 _p6 = _mm256_load_ps(ptr + 8 * 6);
734 __m256 _p7 = _mm256_load_ps(ptr + 8 * 7);
735 _p0 = exp256_ps(_mm256_sub_ps(_p0, _mm256_set1_ps(max[j])));
736 _p1 = exp256_ps(_mm256_sub_ps(_p1, _mm256_set1_ps(max[j + 1])));
737 _p2 = exp256_ps(_mm256_sub_ps(_p2, _mm256_set1_ps(max[j + 2])));
738 _p3 = exp256_ps(_mm256_sub_ps(_p3, _mm256_set1_ps(max[j + 3])));
739 _p4 = exp256_ps(_mm256_sub_ps(_p4, _mm256_set1_ps(max[j + 4])));
740 _p5 = exp256_ps(_mm256_sub_ps(_p5, _mm256_set1_ps(max[j + 5])));
741 _p6 = exp256_ps(_mm256_sub_ps(_p6, _mm256_set1_ps(max[j + 6])));
742 _p7 = exp256_ps(_mm256_sub_ps(_p7, _mm256_set1_ps(max[j + 7])));
743 _mm256_store_ps(ptr, _p0);
744 _mm256_store_ps(ptr + 8, _p1);
745 _mm256_store_ps(ptr + 8 * 2, _p2);
746 _mm256_store_ps(ptr + 8 * 3, _p3);
747 _mm256_store_ps(ptr + 8 * 4, _p4);
748 _mm256_store_ps(ptr + 8 * 5, _p5);
749 _mm256_store_ps(ptr + 8 * 6, _p6);
750 _mm256_store_ps(ptr + 8 * 7, _p7);
751 transpose8x8_ps(_p0, _p1, _p2, _p3, _p4, _p5, _p6, _p7);
752 __m256 _sum01 = _mm256_add_ps(_p0, _p1);
753 __m256 _sum23 = _mm256_add_ps(_p2, _p3);
754 __m256 _sum45 = _mm256_add_ps(_p4, _p5);
755 __m256 _sum67 = _mm256_add_ps(_p6, _p7);
756 __m256 _sum0123 = _mm256_add_ps(_sum01, _sum23);
757 __m256 _sum4567 = _mm256_add_ps(_sum45, _sum67);
758 __m256 _sum01234567 = _mm256_add_ps(_sum0123, _sum4567);
759 __m256 _sum = _mm256_load_ps(psum);
760 _sum = _mm256_add_ps(_sum, _sum01234567);
761 _mm256_store_ps(psum, _sum);
762
763 ptr += 64;
764 psum += 8;
765 }
766 for (; j < w; j++)
767 {
768 __m256 _p = _mm256_load_ps(ptr);
769 __m256 _max = _mm256_set1_ps(max[j]);
770 _p = exp256_ps(_mm256_sub_ps(_p, _max));
771 _mm256_store_ps(ptr, _p);
772 *psum += _mm256_reduce_add_ps(_p);
773
774 ptr += 8;
775 psum++;
776 }
777 }
778
779 for (int i = 0; i < h; i++)
780 {
781 float* ptr = bottom_top_blob.row(i);
782 for (int j = 0; j < w; j++)
783 {
784 __m256 _p = _mm256_load_ps(ptr);
785 __m256 _sum = _mm256_set1_ps(sum[j]);
786 _p = _mm256_div_ps(_p, _sum);
787 _mm256_store_ps(ptr, _p);
788
789 ptr += 8;
790 }
791 }
792 }
793
794 if (dims == 2 && positive_axis == 1)
795 {
796 int w = bottom_top_blob.w;
797 int h = bottom_top_blob.h;
798
799 #pragma omp parallel for num_threads(opt.num_threads)
800 for (int i = 0; i < h; i++)
801 {
802 float* ptr = bottom_top_blob.row(i);
803
804 __m256 _max = _mm256_set1_ps(-FLT_MAX);
805 for (int j = 0; j < w; j++)
806 {
807 __m256 _p = _mm256_load_ps(ptr + j * 8);
808 _max = _mm256_max_ps(_max, _p);
809 }
810
811 __m256 _sum = _mm256_setzero_ps();
812 for (int j = 0; j < w; j++)
813 {
814 __m256 _p = _mm256_load_ps(ptr + j * 8);
815 _p = exp256_ps(_mm256_sub_ps(_p, _max));
816 _mm256_store_ps(ptr + j * 8, _p);
817 _sum = _mm256_add_ps(_sum, _p);
818 }
819
820 for (int j = 0; j < w; j++)
821 {
822 __m256 _p = _mm256_load_ps(ptr + j * 8);
823 _p = _mm256_div_ps(_p, _sum);
824 _mm256_store_ps(ptr + j * 8, _p);
825 }
826 }
827 }
828
829 if (dims == 3 && positive_axis == 0)
830 {
831 int w = bottom_top_blob.w;
832 int h = bottom_top_blob.h;
833 int channels = bottom_top_blob.c;
834 int size = w * h;
835
836 Mat max;
837 max.create(w, h, 4u, 1, opt.workspace_allocator);
838 if (max.empty())
839 return -100;
840 max.fill(-FLT_MAX);
841 for (int q = 0; q < channels; q++)
842 {
843 const float* ptr = bottom_top_blob.channel(q);
844 float* pmax = max;
845
846 int i = 0;
847 for (; i + 7 < size; i += 8)
848 {
849 __m256 _p0 = _mm256_load_ps(ptr);
850 __m256 _p1 = _mm256_load_ps(ptr + 8);
851 __m256 _p2 = _mm256_load_ps(ptr + 8 * 2);
852 __m256 _p3 = _mm256_load_ps(ptr + 8 * 3);
853 __m256 _p4 = _mm256_load_ps(ptr + 8 * 4);
854 __m256 _p5 = _mm256_load_ps(ptr + 8 * 5);
855 __m256 _p6 = _mm256_load_ps(ptr + 8 * 6);
856 __m256 _p7 = _mm256_load_ps(ptr + 8 * 7);
857 transpose8x8_ps(_p0, _p1, _p2, _p3, _p4, _p5, _p6, _p7);
858 __m256 _max01 = _mm256_max_ps(_p0, _p1);
859 __m256 _max23 = _mm256_max_ps(_p2, _p3);
860 __m256 _max45 = _mm256_max_ps(_p4, _p5);
861 __m256 _max67 = _mm256_max_ps(_p6, _p7);
862 __m256 _max0123 = _mm256_max_ps(_max01, _max23);
863 __m256 _max4567 = _mm256_max_ps(_max45, _max67);
864 __m256 _max01234567 = _mm256_max_ps(_max0123, _max4567);
865 __m256 _max = _mm256_load_ps(pmax);
866 _max = _mm256_max_ps(_max, _max01234567);
867 _mm256_store_ps(pmax, _max);
868
869 ptr += 64;
870 pmax += 8;
871 }
872 for (; i < size; i++)
873 {
874 __m256 _p = _mm256_load_ps(ptr);
875 *pmax = std::max(*pmax, _mm256_reduce_max_ps(_p));
876
877 ptr += 8;
878 pmax++;
879 }
880 }
881
882 Mat sum;
883 sum.create(w, h, 4u, 1, opt.workspace_allocator);
884 if (sum.empty())
885 return -100;
886 sum.fill(0.f);
887 for (int q = 0; q < channels; q++)
888 {
889 float* ptr = bottom_top_blob.channel(q);
890 float* psum = sum;
891
892 int i = 0;
893 for (; i + 7 < size; i += 8)
894 {
895 __m256 _p0 = _mm256_load_ps(ptr);
896 __m256 _p1 = _mm256_load_ps(ptr + 8);
897 __m256 _p2 = _mm256_load_ps(ptr + 8 * 2);
898 __m256 _p3 = _mm256_load_ps(ptr + 8 * 3);
899 __m256 _p4 = _mm256_load_ps(ptr + 8 * 4);
900 __m256 _p5 = _mm256_load_ps(ptr + 8 * 5);
901 __m256 _p6 = _mm256_load_ps(ptr + 8 * 6);
902 __m256 _p7 = _mm256_load_ps(ptr + 8 * 7);
903 _p0 = exp256_ps(_mm256_sub_ps(_p0, _mm256_set1_ps(max[i])));
904 _p1 = exp256_ps(_mm256_sub_ps(_p1, _mm256_set1_ps(max[i + 1])));
905 _p2 = exp256_ps(_mm256_sub_ps(_p2, _mm256_set1_ps(max[i + 2])));
906 _p3 = exp256_ps(_mm256_sub_ps(_p3, _mm256_set1_ps(max[i + 3])));
907 _p4 = exp256_ps(_mm256_sub_ps(_p4, _mm256_set1_ps(max[i + 4])));
908 _p5 = exp256_ps(_mm256_sub_ps(_p5, _mm256_set1_ps(max[i + 5])));
909 _p6 = exp256_ps(_mm256_sub_ps(_p6, _mm256_set1_ps(max[i + 6])));
910 _p7 = exp256_ps(_mm256_sub_ps(_p7, _mm256_set1_ps(max[i + 7])));
911 _mm256_store_ps(ptr, _p0);
912 _mm256_store_ps(ptr + 8, _p1);
913 _mm256_store_ps(ptr + 8 * 2, _p2);
914 _mm256_store_ps(ptr + 8 * 3, _p3);
915 _mm256_store_ps(ptr + 8 * 4, _p4);
916 _mm256_store_ps(ptr + 8 * 5, _p5);
917 _mm256_store_ps(ptr + 8 * 6, _p6);
918 _mm256_store_ps(ptr + 8 * 7, _p7);
919 transpose8x8_ps(_p0, _p1, _p2, _p3, _p4, _p5, _p6, _p7);
920 __m256 _sum01 = _mm256_add_ps(_p0, _p1);
921 __m256 _sum23 = _mm256_add_ps(_p2, _p3);
922 __m256 _sum45 = _mm256_add_ps(_p4, _p5);
923 __m256 _sum67 = _mm256_add_ps(_p6, _p7);
924 __m256 _sum0123 = _mm256_add_ps(_sum01, _sum23);
925 __m256 _sum4567 = _mm256_add_ps(_sum45, _sum67);
926 __m256 _sum01234567 = _mm256_add_ps(_sum0123, _sum4567);
927 __m256 _sum = _mm256_load_ps(psum);
928 _sum = _mm256_add_ps(_sum, _sum01234567);
929 _mm256_store_ps(psum, _sum);
930
931 ptr += 64;
932 psum += 8;
933 }
934 for (; i < size; i++)
935 {
936 __m256 _p = _mm256_load_ps(ptr);
937 __m256 _max = _mm256_set1_ps(max[i]);
938 _p = exp256_ps(_mm256_sub_ps(_p, _max));
939 _mm256_store_ps(ptr, _p);
940 *psum += _mm256_reduce_add_ps(_p);
941
942 ptr += 8;
943 psum++;
944 }
945 }
946
947 #pragma omp parallel for num_threads(opt.num_threads)
948 for (int q = 0; q < channels; q++)
949 {
950 float* ptr = bottom_top_blob.channel(q);
951
952 for (int i = 0; i < size; i++)
953 {
954 __m256 _p = _mm256_load_ps(ptr);
955 __m256 _sum = _mm256_set1_ps(sum[i]);
956 _p = _mm256_div_ps(_p, _sum);
957 _mm256_store_ps(ptr, _p);
958
959 ptr += 8;
960 }
961 }
962 }
963
964 if (dims == 3 && positive_axis == 1)
965 {
966 int w = bottom_top_blob.w;
967 int h = bottom_top_blob.h;
968 int channels = bottom_top_blob.c;
969
970 Mat max;
971 max.create(w, channels, elemsize, elempack, opt.workspace_allocator);
972 if (max.empty())
973 return -100;
974 max.fill(_mm256_set1_ps(-FLT_MAX));
975
976 #pragma omp parallel for num_threads(opt.num_threads)
977 for (int q = 0; q < channels; q++)
978 {
979 const float* ptr = bottom_top_blob.channel(q);
980
981 for (int i = 0; i < h; i++)
982 {
983 float* maxptr = max.row(q);
984
985 for (int j = 0; j < w; j++)
986 {
987 __m256 _p = _mm256_load_ps(ptr);
988 __m256 _max = _mm256_load_ps(maxptr);
989 _max = _mm256_max_ps(_max, _p);
990 _mm256_store_ps(maxptr, _max);
991
992 ptr += 8;
993 maxptr += 8;
994 }
995 }
996 }
997
998 Mat sum;
999 sum.create(w, channels, elemsize, elempack, opt.workspace_allocator);
1000 if (sum.empty())
1001 return -100;
1002 sum.fill(_mm256_setzero_ps());
1003
1004 #pragma omp parallel for num_threads(opt.num_threads)
1005 for (int q = 0; q < channels; q++)
1006 {
1007 float* ptr = bottom_top_blob.channel(q);
1008
1009 for (int i = 0; i < h; i++)
1010 {
1011 float* maxptr = max.row(q);
1012 float* sumptr = sum.row(q);
1013
1014 for (int j = 0; j < w; j++)
1015 {
1016 __m256 _p = _mm256_load_ps(ptr);
1017 __m256 _max = _mm256_load_ps(maxptr);
1018 _p = exp256_ps(_mm256_sub_ps(_p, _max));
1019 _mm256_store_ps(ptr, _p);
1020 __m256 _sum = _mm256_load_ps(sumptr);
1021 _sum = _mm256_add_ps(_sum, _p);
1022 _mm256_store_ps(sumptr, _sum);
1023
1024 ptr += 8;
1025 maxptr += 8;
1026 sumptr += 8;
1027 }
1028 }
1029 }
1030
1031 #pragma omp parallel for num_threads(opt.num_threads)
1032 for (int q = 0; q < channels; q++)
1033 {
1034 float* ptr = bottom_top_blob.channel(q);
1035
1036 for (int i = 0; i < h; i++)
1037 {
1038 float* sumptr = sum.row(q);
1039
1040 for (int j = 0; j < w; j++)
1041 {
1042 __m256 _p = _mm256_load_ps(ptr);
1043 __m256 _sum = _mm256_load_ps(sumptr);
1044 _p = _mm256_div_ps(_p, _sum);
1045 _mm256_store_ps(ptr, _p);
1046
1047 ptr += 8;
1048 sumptr += 8;
1049 }
1050 }
1051 }
1052 }
1053
1054 if (dims == 3 && positive_axis == 2)
1055 {
1056 int w = bottom_top_blob.w;
1057 int h = bottom_top_blob.h;
1058 int channels = bottom_top_blob.c;
1059
1060 #pragma omp parallel for num_threads(opt.num_threads)
1061 for (int q = 0; q < channels; q++)
1062 {
1063 float* ptr = bottom_top_blob.channel(q);
1064
1065 for (int i = 0; i < h; i++)
1066 {
1067 __m256 _max = _mm256_set1_ps(-FLT_MAX);
1068 for (int j = 0; j < w; j++)
1069 {
1070 __m256 _p = _mm256_load_ps(ptr + j * 8);
1071 _max = _mm256_max_ps(_max, _p);
1072 }
1073
1074 __m256 _sum = _mm256_setzero_ps();
1075 for (int j = 0; j < w; j++)
1076 {
1077 __m256 _p = _mm256_load_ps(ptr + j * 8);
1078 _p = exp256_ps(_mm256_sub_ps(_p, _max));
1079 _mm256_store_ps(ptr + j * 8, _p);
1080 _sum = _mm256_add_ps(_sum, _p);
1081 }
1082
1083 for (int j = 0; j < w; j++)
1084 {
1085 __m256 _p = _mm256_load_ps(ptr + j * 8);
1086 _p = _mm256_div_ps(_p, _sum);
1087 _mm256_store_ps(ptr + j * 8, _p);
1088 }
1089
1090 ptr += w * 8;
1091 }
1092 }
1093 }
1094
1095 return 0;
1096 }
1097#endif // __AVX__
1098
1099 if (elempack == 4)
1100 {
1101 if (dims == 1) // positive_axis == 0
1102 {
1103 int w = bottom_top_blob.w;
1104
1105 float* ptr = bottom_top_blob;
1106
1107 __m128 _max = _mm_set1_ps(-FLT_MAX);
1108 for (int i = 0; i < w; i++)
1109 {
1110 __m128 _p = _mm_load_ps(ptr + i * 4);
1111 _max = _mm_max_ps(_max, _p);
1112 }
1113 _max = _mm_max_ps(_max, _mm_shuffle_ps(_max, _max, _MM_SHUFFLE(2, 3, 0, 1)));
1114 _max = _mm_max_ps(_max, _mm_shuffle_ps(_max, _max, _MM_SHUFFLE(1, 0, 3, 2)));
1115
1116 __m128 _sum = _mm_setzero_ps();
1117 for (int i = 0; i < w; i++)
1118 {
1119 __m128 _p = _mm_load_ps(ptr + i * 4);
1120 _p = exp_ps(_mm_sub_ps(_p, _max));
1121 _mm_store_ps(ptr + i * 4, _p);
1122 _sum = _mm_add_ps(_sum, _p);
1123 }
1124 _sum = _mm_add_ps(_sum, _mm_shuffle_ps(_sum, _sum, _MM_SHUFFLE(2, 3, 0, 1)));
1125 _sum = _mm_add_ps(_sum, _mm_shuffle_ps(_sum, _sum, _MM_SHUFFLE(1, 0, 3, 2)));
1126
1127 for (int i = 0; i < w; i++)
1128 {
1129 __m128 _p = _mm_load_ps(ptr + i * 4);
1130 _p = _mm_div_ps(_p, _sum);
1131 _mm_store_ps(ptr + i * 4, _p);
1132 }
1133 }
1134
1135 if (dims == 2 && positive_axis == 0)
1136 {
1137 int w = bottom_top_blob.w;
1138 int h = bottom_top_blob.h;
1139
1140 Mat max;
1141 max.create(w, 4u, 1, opt.workspace_allocator);
1142 if (max.empty())
1143 return -100;
1144 max.fill(-FLT_MAX);
1145
1146 for (int i = 0; i < h; i++)
1147 {
1148 const float* ptr = bottom_top_blob.row(i);
1149 float* pmax = max;
1150
1151 int j = 0;
1152 for (; j + 3 < w; j += 4)
1153 {
1154 __m128 _p0 = _mm_load_ps(ptr);
1155 __m128 _p1 = _mm_load_ps(ptr + 4);
1156 __m128 _p2 = _mm_load_ps(ptr + 8);
1157 __m128 _p3 = _mm_load_ps(ptr + 12);
1158 _MM_TRANSPOSE4_PS(_p0, _p1, _p2, _p3);
1159 __m128 _max01 = _mm_max_ps(_p0, _p1);
1160 __m128 _max23 = _mm_max_ps(_p2, _p3);
1161 __m128 _max0123 = _mm_max_ps(_max01, _max23);
1162 __m128 _max = _mm_load_ps(pmax);
1163 _max = _mm_max_ps(_max, _max0123);
1164 _mm_store_ps(pmax, _max);
1165
1166 ptr += 16;
1167 pmax += 4;
1168 }
1169 for (; j < w; j++)
1170 {
1171 __m128 _p = _mm_load_ps(ptr);
1172 *pmax = std::max(*pmax, _mm_reduce_max_ps(_p));
1173
1174 ptr += 4;
1175 pmax++;
1176 }
1177 }
1178
1179 Mat sum;
1180 sum.create(w, 4u, 1, opt.workspace_allocator);
1181 if (sum.empty())
1182 return -100;
1183 sum.fill(0.f);
1184
1185 for (int i = 0; i < h; i++)
1186 {
1187 float* ptr = bottom_top_blob.row(i);
1188 float* psum = sum;
1189 int j = 0;
1190 for (; j + 3 < w; j += 4)
1191 {
1192 __m128 _p0 = _mm_load_ps(ptr);
1193 __m128 _p1 = _mm_load_ps(ptr + 4);
1194 __m128 _p2 = _mm_load_ps(ptr + 8);
1195 __m128 _p3 = _mm_load_ps(ptr + 12);
1196 __m128 _max0 = _mm_set1_ps(max[j]);
1197 __m128 _max1 = _mm_set1_ps(max[j + 1]);
1198 __m128 _max2 = _mm_set1_ps(max[j + 2]);
1199 __m128 _max3 = _mm_set1_ps(max[j + 3]);
1200 _p0 = exp_ps(_mm_sub_ps(_p0, _max0));
1201 _p1 = exp_ps(_mm_sub_ps(_p1, _max1));
1202 _p2 = exp_ps(_mm_sub_ps(_p2, _max2));
1203 _p3 = exp_ps(_mm_sub_ps(_p3, _max3));
1204 _mm_store_ps(ptr, _p0);
1205 _mm_store_ps(ptr + 4, _p1);
1206 _mm_store_ps(ptr + 8, _p2);
1207 _mm_store_ps(ptr + 12, _p3);
1208 _MM_TRANSPOSE4_PS(_p0, _p1, _p2, _p3);
1209 __m128 _sum01 = _mm_add_ps(_p0, _p1);
1210 __m128 _sum23 = _mm_add_ps(_p2, _p3);
1211 __m128 _sum0123 = _mm_add_ps(_sum01, _sum23);
1212 __m128 _sum = _mm_load_ps(psum);
1213 _sum = _mm_add_ps(_sum, _sum0123);
1214 _mm_store_ps(psum, _sum);
1215
1216 ptr += 16;
1217 psum += 4;
1218 }
1219 for (; j < w; j++)
1220 {
1221 __m128 _p = _mm_load_ps(ptr);
1222 __m128 _max = _mm_set1_ps(max[j]);
1223 _p = exp_ps(_mm_sub_ps(_p, _max));
1224 _mm_store_ps(ptr, _p);
1225 *psum += _mm_reduce_add_ps(_p);
1226
1227 ptr += 4;
1228 psum++;
1229 }
1230 }
1231
1232 for (int i = 0; i < h; i++)
1233 {
1234 float* ptr = bottom_top_blob.row(i);
1235 for (int j = 0; j < w; j++)
1236 {
1237 __m128 _p = _mm_load_ps(ptr);
1238 __m128 _sum = _mm_set1_ps(sum[j]);
1239 _p = _mm_div_ps(_p, _sum);
1240 _mm_store_ps(ptr, _p);
1241
1242 ptr += 4;
1243 }
1244 }
1245 }
1246
1247 if (dims == 2 && positive_axis == 1)
1248 {
1249 int w = bottom_top_blob.w;
1250 int h = bottom_top_blob.h;
1251
1252 #pragma omp parallel for num_threads(opt.num_threads)
1253 for (int i = 0; i < h; i++)
1254 {
1255 float* ptr = bottom_top_blob.row(i);
1256
1257 __m128 _max = _mm_set1_ps(-FLT_MAX);
1258 for (int j = 0; j < w; j++)
1259 {
1260 __m128 _p = _mm_load_ps(ptr + j * 4);
1261 _max = _mm_max_ps(_max, _p);
1262 }
1263
1264 __m128 _sum = _mm_setzero_ps();
1265 for (int j = 0; j < w; j++)
1266 {
1267 __m128 _p = _mm_load_ps(ptr + j * 4);
1268 _p = exp_ps(_mm_sub_ps(_p, _max));
1269 _mm_store_ps(ptr + j * 4, _p);
1270 _sum = _mm_add_ps(_sum, _p);
1271 }
1272
1273 for (int j = 0; j < w; j++)
1274 {
1275 __m128 _p = _mm_load_ps(ptr + j * 4);
1276 _p = _mm_div_ps(_p, _sum);
1277 _mm_store_ps(ptr + j * 4, _p);
1278 }
1279 }
1280 }
1281
1282 if (dims == 3 && positive_axis == 0)
1283 {
1284 int w = bottom_top_blob.w;
1285 int h = bottom_top_blob.h;
1286 int channels = bottom_top_blob.c;
1287 int size = w * h;
1288
1289 Mat max;
1290 max.create(w, h, 4u, 1, opt.workspace_allocator);
1291 if (max.empty())
1292 return -100;
1293 max.fill(-FLT_MAX);
1294 for (int q = 0; q < channels; q++)
1295 {
1296 const float* ptr = bottom_top_blob.channel(q);
1297 float* pmax = max;
1298
1299 int i = 0;
1300 for (; i + 3 < size; i += 4)
1301 {
1302 __m128 _p0 = _mm_load_ps(ptr);
1303 __m128 _p1 = _mm_load_ps(ptr + 4);
1304 __m128 _p2 = _mm_load_ps(ptr + 8);
1305 __m128 _p3 = _mm_load_ps(ptr + 12);
1306 _MM_TRANSPOSE4_PS(_p0, _p1, _p2, _p3);
1307 __m128 _max01 = _mm_max_ps(_p0, _p1);
1308 __m128 _max23 = _mm_max_ps(_p2, _p3);
1309 __m128 _max0123 = _mm_max_ps(_max01, _max23);
1310 __m128 _max = _mm_load_ps(pmax);
1311 _max = _mm_max_ps(_max, _max0123);
1312 _mm_store_ps(pmax, _max);
1313
1314 ptr += 16;
1315 pmax += 4;
1316 }
1317 for (; i < size; i++)
1318 {
1319 __m128 _p = _mm_load_ps(ptr);
1320 *pmax = std::max(*pmax, _mm_reduce_max_ps(_p));
1321
1322 ptr += 4;
1323 pmax++;
1324 }
1325 }
1326
1327 Mat sum;
1328 sum.create(w, h, 4u, 1, opt.workspace_allocator);
1329 if (sum.empty())
1330 return -100;
1331 sum.fill(0.f);
1332 for (int q = 0; q < channels; q++)
1333 {
1334 float* ptr = bottom_top_blob.channel(q);
1335 float* psum = sum;
1336
1337 int i = 0;
1338 for (; i + 3 < size; i += 4)
1339 {
1340 __m128 _p0 = _mm_load_ps(ptr);
1341 __m128 _p1 = _mm_load_ps(ptr + 4);
1342 __m128 _p2 = _mm_load_ps(ptr + 8);
1343 __m128 _p3 = _mm_load_ps(ptr + 12);
1344 __m128 _max0 = _mm_set1_ps(max[i]);
1345 __m128 _max1 = _mm_set1_ps(max[i + 1]);
1346 __m128 _max2 = _mm_set1_ps(max[i + 2]);
1347 __m128 _max3 = _mm_set1_ps(max[i + 3]);
1348 _p0 = exp_ps(_mm_sub_ps(_p0, _max0));
1349 _p1 = exp_ps(_mm_sub_ps(_p1, _max1));
1350 _p2 = exp_ps(_mm_sub_ps(_p2, _max2));
1351 _p3 = exp_ps(_mm_sub_ps(_p3, _max3));
1352 _mm_store_ps(ptr, _p0);
1353 _mm_store_ps(ptr + 4, _p1);
1354 _mm_store_ps(ptr + 8, _p2);
1355 _mm_store_ps(ptr + 12, _p3);
1356 _MM_TRANSPOSE4_PS(_p0, _p1, _p2, _p3);
1357 __m128 _sum01 = _mm_add_ps(_p0, _p1);
1358 __m128 _sum23 = _mm_add_ps(_p2, _p3);
1359 __m128 _sum0123 = _mm_add_ps(_sum01, _sum23);
1360 __m128 _sum = _mm_load_ps(psum);
1361 _sum = _mm_add_ps(_sum, _sum0123);
1362 _mm_store_ps(psum, _sum);
1363
1364 ptr += 16;
1365 psum += 4;
1366 }
1367 for (; i < size; i++)
1368 {
1369 __m128 _p = _mm_load_ps(ptr);
1370 __m128 _max = _mm_set1_ps(max[i]);
1371 _p = exp_ps(_mm_sub_ps(_p, _max));
1372 _mm_store_ps(ptr, _p);
1373 *psum += _mm_reduce_add_ps(_p);
1374
1375 ptr += 4;
1376 psum++;
1377 }
1378 }
1379
1380 #pragma omp parallel for num_threads(opt.num_threads)
1381 for (int q = 0; q < channels; q++)
1382 {
1383 float* ptr = bottom_top_blob.channel(q);
1384
1385 for (int i = 0; i < size; i++)
1386 {
1387 __m128 _p = _mm_load_ps(ptr);
1388 __m128 _sum = _mm_set1_ps(sum[i]);
1389 _p = _mm_div_ps(_p, _sum);
1390 _mm_store_ps(ptr, _p);
1391
1392 ptr += 4;
1393 }
1394 }
1395 }
1396
1397 if (dims == 3 && positive_axis == 1)
1398 {
1399 int w = bottom_top_blob.w;
1400 int h = bottom_top_blob.h;
1401 int channels = bottom_top_blob.c;
1402
1403 Mat max;
1404 max.create(w, channels, elemsize, elempack, opt.workspace_allocator);
1405 if (max.empty())
1406 return -100;
1407 max.fill(_mm_set1_ps(-FLT_MAX));
1408 #pragma omp parallel for num_threads(opt.num_threads)
1409 for (int q = 0; q < channels; q++)
1410 {
1411 const float* ptr = bottom_top_blob.channel(q);
1412
1413 for (int i = 0; i < h; i++)
1414 {
1415 float* maxptr = max.row(q);
1416
1417 for (int j = 0; j < w; j++)
1418 {
1419 __m128 _p = _mm_load_ps(ptr);
1420 __m128 _max = _mm_load_ps(maxptr);
1421 _max = _mm_max_ps(_max, _p);
1422 _mm_store_ps(maxptr, _max);
1423
1424 ptr += 4;
1425 maxptr += 4;
1426 }
1427 }
1428 }
1429
1430 Mat sum;
1431 sum.create(w, channels, elemsize, elempack, opt.workspace_allocator);
1432 if (sum.empty())
1433 return -100;
1434 sum.fill(_mm_setzero_ps());
1435 #pragma omp parallel for num_threads(opt.num_threads)
1436 for (int q = 0; q < channels; q++)
1437 {
1438 float* ptr = bottom_top_blob.channel(q);
1439
1440 for (int i = 0; i < h; i++)
1441 {
1442 float* maxptr = max.row(q);
1443 float* sumptr = sum.row(q);
1444
1445 for (int j = 0; j < w; j++)
1446 {
1447 __m128 _p = _mm_load_ps(ptr);
1448 __m128 _max = _mm_load_ps(maxptr);
1449 _p = exp_ps(_mm_sub_ps(_p, _max));
1450 _mm_store_ps(ptr, _p);
1451 __m128 _sum = _mm_load_ps(sumptr);
1452 _sum = _mm_add_ps(_sum, _p);
1453 _mm_store_ps(sumptr, _sum);
1454
1455 ptr += 4;
1456 maxptr += 4;
1457 sumptr += 4;
1458 }
1459 }
1460 }
1461
1462 #pragma omp parallel for num_threads(opt.num_threads)
1463 for (int q = 0; q < channels; q++)
1464 {
1465 float* ptr = bottom_top_blob.channel(q);
1466
1467 for (int i = 0; i < h; i++)
1468 {
1469 float* sumptr = sum.row(q);
1470
1471 for (int j = 0; j < w; j++)
1472 {
1473 __m128 _p = _mm_load_ps(ptr);
1474 __m128 _sum = _mm_load_ps(sumptr);
1475 _p = _mm_div_ps(_p, _sum);
1476 _mm_store_ps(ptr, _p);
1477
1478 ptr += 4;
1479 sumptr += 4;
1480 }
1481 }
1482 }
1483 }
1484
1485 if (dims == 3 && positive_axis == 2)
1486 {
1487 int w = bottom_top_blob.w;
1488 int h = bottom_top_blob.h;
1489 int channels = bottom_top_blob.c;
1490
1491 #pragma omp parallel for num_threads(opt.num_threads)
1492 for (int q = 0; q < channels; q++)
1493 {
1494 float* ptr = bottom_top_blob.channel(q);
1495
1496 for (int i = 0; i < h; i++)
1497 {
1498 __m128 _max = _mm_set1_ps(-FLT_MAX);
1499 for (int j = 0; j < w; j++)
1500 {
1501 __m128 _p = _mm_load_ps(ptr + j * 4);
1502 _max = _mm_max_ps(_max, _p);
1503 }
1504
1505 __m128 _sum = _mm_setzero_ps();
1506 for (int j = 0; j < w; j++)
1507 {
1508 __m128 _p = _mm_load_ps(ptr + j * 4);
1509 _p = exp_ps(_mm_sub_ps(_p, _max));
1510 _mm_store_ps(ptr + j * 4, _p);
1511 _sum = _mm_add_ps(_sum, _p);
1512 }
1513
1514 for (int j = 0; j < w; j++)
1515 {
1516 __m128 _p = _mm_load_ps(ptr + j * 4);
1517 _p = _mm_div_ps(_p, _sum);
1518 _mm_store_ps(ptr + j * 4, _p);
1519 }
1520
1521 ptr += w * 4;
1522 }
1523 }
1524 }
1525
1526 return 0;
1527 }
1528#endif // __SSE2__
1529
1530 if (dims == 1) // positive_axis == 0
1531 {
1532 int w = bottom_top_blob.w;
1533
1534 float* ptr = bottom_top_blob;
1535
1536 float max = -FLT_MAX;
1537 {
1538 int i = 0;
1539#if __SSE2__
1540#if __AVX__
1541#if __AVX512F__
1542 __m512 _max_avx512 = _mm512_set1_ps(-FLT_MAX);
1543 for (; i + 15 < w; i += 16)
1544 {
1545 __m512 _p = _mm512_load_ps(ptr + i);
1546 _max_avx512 = _mm512_max_ps(_max_avx512, _p);
1547 }
1548 max = std::max(max, _mm512_comp_reduce_max_ps(_max_avx512));
1549#endif // __AVX512F__
1550 __m256 _max_avx = _mm256_set1_ps(-FLT_MAX);
1551 for (; i + 7 < w; i += 8)
1552 {
1553 __m256 _p = _mm256_load_ps(ptr + i);
1554 _max_avx = _mm256_max_ps(_max_avx, _p);
1555 }
1556 max = std::max(max, _mm256_reduce_max_ps(_max_avx));
1557#endif // __AVX__
1558 __m128 _max = _mm_set1_ps(-FLT_MAX);
1559 for (; i + 3 < w; i += 4)
1560 {
1561 __m128 _p = _mm_load_ps(ptr + i);
1562 _max = _mm_max_ps(_max, _p);
1563 }
1564 max = std::max(max, _mm_reduce_max_ps(_max));
1565#endif // __SSE2__
1566 for (; i < w; i++)
1567 {
1568 max = std::max(max, ptr[i]);
1569 }
1570 }
1571
1572 float sum = 0.f;
1573 {
1574 int i = 0;
1575#if __SSE2__
1576#if __AVX__
1577#if __AVX512F__
1578 __m512 _sum_avx512 = _mm512_setzero_ps();
1579 __m512 _max_avx512 = _mm512_set1_ps(max);
1580 for (; i + 15 < w; i += 16)
1581 {
1582 __m512 _p = _mm512_load_ps(ptr + i);
1583 _p = exp512_ps(_mm512_sub_ps(_p, _max_avx512));
1584 _mm512_storeu_ps(ptr + i, _p);
1585 _sum_avx512 = _mm512_add_ps(_sum_avx512, _p);
1586 }
1587 sum += _mm512_comp_reduce_add_ps(_sum_avx512);
1588#endif // __AVX512F__
1589 __m256 _sum_avx = _mm256_setzero_ps();
1590 __m256 _max_avx = _mm256_set1_ps(max);
1591 for (; i + 7 < w; i += 8)
1592 {
1593 __m256 _p = _mm256_load_ps(ptr + i);
1594 _p = exp256_ps(_mm256_sub_ps(_p, _max_avx));
1595 _mm256_storeu_ps(ptr + i, _p);
1596 _sum_avx = _mm256_add_ps(_sum_avx, _p);
1597 }
1598 sum += _mm256_reduce_add_ps(_sum_avx);
1599#endif // __AVX__
1600 __m128 _sum = _mm_setzero_ps();
1601 __m128 _max = _mm_set1_ps(max);
1602 for (; i + 3 < w; i += 4)
1603 {
1604 __m128 _p = _mm_load_ps(ptr + i);
1605 _p = exp_ps(_mm_sub_ps(_p, _max));
1606 _mm_store_ps(ptr + i, _p);
1607 _sum = _mm_add_ps(_sum, _p);
1608 }
1609 sum += _mm_reduce_add_ps(_sum);
1610#endif // __SSE2__
1611 for (; i < w; i++)
1612 {
1613 ptr[i] = expf(ptr[i] - max);
1614 sum += ptr[i];
1615 }
1616 }
1617
1618 {
1619 int i = 0;
1620#if __SSE2__
1621#if __AVX__
1622#if __AVX512F__
1623 __m512 _sum_avx512 = _mm512_set1_ps(sum);
1624 for (; i + 15 < w; i += 16)
1625 {
1626 __m512 _p = _mm512_load_ps(ptr + i);
1627 _p = _mm512_div_ps(_p, _sum_avx512);
1628 _mm512_store_ps(ptr + i, _p);
1629 }
1630#endif // __AVX512F__
1631 __m256 _sum_avx = _mm256_set1_ps(sum);
1632 for (; i + 7 < w; i += 8)
1633 {
1634 __m256 _p = _mm256_load_ps(ptr + i);
1635 _p = _mm256_div_ps(_p, _sum_avx);
1636 _mm256_store_ps(ptr + i, _p);
1637 }
1638#endif // __AVX__
1639 __m128 _sum = _mm_set1_ps(sum);
1640 for (; i + 3 < w; i += 4)
1641 {
1642 __m128 _p = _mm_load_ps(ptr + i);
1643 _p = _mm_div_ps(_p, _sum);
1644 _mm_store_ps(ptr + i, _p);
1645 }
1646#endif // __SSE2__
1647 for (; i < w; i++)
1648 {
1649 ptr[i] /= sum;
1650 }
1651 }
1652 }
1653
1654 if (dims == 2 && positive_axis == 0)
1655 {
1656 int w = bottom_top_blob.w;
1657 int h = bottom_top_blob.h;
1658
1659 Mat max;
1660 max.create(w, elemsize, opt.workspace_allocator);
1661 if (max.empty())
1662 return -100;
1663 max.fill(-FLT_MAX);
1664
1665 for (int i = 0; i < h; i++)
1666 {
1667 const float* ptr = bottom_top_blob.row(i);
1668 float* pmax = max;
1669
1670 int j = 0;
1671#if __SSE2__
1672#if __AVX__
1673#if __AVX512F__
1674 for (; j + 15 < w; j += 16)
1675 {
1676 __m512 _p = _mm512_loadu_ps(ptr);
1677 __m512 _max = _mm512_load_ps(pmax);
1678 _max = _mm512_max_ps(_max, _p);
1679 _mm512_store_ps(pmax, _max);
1680
1681 ptr += 16;
1682 pmax += 16;
1683 }
1684#endif // __AVX512F__
1685 for (; j + 7 < w; j += 8)
1686 {
1687 __m256 _p = _mm256_loadu_ps(ptr);
1688 __m256 _max = _mm256_load_ps(pmax);
1689 _max = _mm256_max_ps(_max, _p);
1690 _mm256_store_ps(pmax, _max);
1691
1692 ptr += 8;
1693 pmax += 8;
1694 }
1695#endif // __AVX__
1696 for (; j + 3 < w; j += 4)
1697 {
1698 __m128 _p = _mm_loadu_ps(ptr);
1699 __m128 _max = _mm_load_ps(pmax);
1700 _max = _mm_max_ps(_max, _p);
1701 _mm_store_ps(pmax, _max);
1702
1703 ptr += 4;
1704 pmax += 4;
1705 }
1706#endif // __SSE2__
1707 for (; j < w; j++)
1708 {
1709 *pmax = std::max(*pmax, *ptr);
1710
1711 ptr++;
1712 pmax++;
1713 }
1714 }
1715
1716 Mat sum;
1717 sum.create(w, elemsize, opt.workspace_allocator);
1718 if (sum.empty())
1719 return -100;
1720 sum.fill(0.f);
1721
1722 for (int i = 0; i < h; i++)
1723 {
1724 float* ptr = bottom_top_blob.row(i);
1725 const float* pmax = max;
1726 float* psum = sum;
1727
1728 int j = 0;
1729#if __SSE2__
1730#if __AVX__
1731#if __AVX512F__
1732 for (; j + 15 < w; j += 16)
1733 {
1734 __m512 _p = _mm512_loadu_ps(ptr);
1735 __m512 _max = _mm512_load_ps(pmax);
1736 __m512 _sum = _mm512_load_ps(psum);
1737 _p = exp512_ps(_mm512_sub_ps(_p, _max));
1738 _sum = _mm512_add_ps(_sum, _p);
1739 _mm512_storeu_ps(ptr, _p);
1740 _mm512_store_ps(psum, _sum);
1741
1742 ptr += 16;
1743 pmax += 16;
1744 psum += 16;
1745 }
1746#endif // __AVX512F__
1747 for (; j + 7 < w; j += 8)
1748 {
1749 __m256 _p = _mm256_loadu_ps(ptr);
1750 __m256 _max = _mm256_load_ps(pmax);
1751 __m256 _sum = _mm256_load_ps(psum);
1752 _p = exp256_ps(_mm256_sub_ps(_p, _max));
1753 _sum = _mm256_add_ps(_sum, _p);
1754 _mm256_storeu_ps(ptr, _p);
1755 _mm256_store_ps(psum, _sum);
1756
1757 ptr += 8;
1758 pmax += 8;
1759 psum += 8;
1760 }
1761#endif // __AVX__
1762 for (; j + 3 < w; j += 4)
1763 {
1764 __m128 _p = _mm_loadu_ps(ptr);
1765 __m128 _max = _mm_load_ps(pmax);
1766 __m128 _sum = _mm_load_ps(psum);
1767 _p = exp_ps(_mm_sub_ps(_p, _max));
1768 _sum = _mm_add_ps(_sum, _p);
1769 _mm_storeu_ps(ptr, _p);
1770 _mm_store_ps(psum, _sum);
1771
1772 ptr += 4;
1773 pmax += 4;
1774 psum += 4;
1775 }
1776#endif // __SSE2__
1777 for (; j < w; j++)
1778 {
1779 *ptr = expf(*ptr - *pmax);
1780 *psum += *ptr;
1781
1782 ptr++;
1783 pmax++;
1784 psum++;
1785 }
1786 }
1787
1788 for (int i = 0; i < h; i++)
1789 {
1790 float* ptr = bottom_top_blob.row(i);
1791 const float* psum = sum;
1792
1793 int j = 0;
1794#if __SSE2__
1795#if __AVX__
1796#if __AVX512F__
1797 for (; j + 15 < w; j += 16)
1798 {
1799 __m512 _p = _mm512_loadu_ps(ptr);
1800 __m512 _sum = _mm512_load_ps(psum);
1801 _p = _mm512_div_ps(_p, _sum);
1802 _mm512_storeu_ps(ptr, _p);
1803
1804 ptr += 16;
1805 psum += 16;
1806 }
1807#endif // __AVX512F__
1808 for (; j + 7 < w; j += 8)
1809 {
1810 __m256 _p = _mm256_loadu_ps(ptr);
1811 __m256 _sum = _mm256_load_ps(psum);
1812 _p = _mm256_div_ps(_p, _sum);
1813 _mm256_storeu_ps(ptr, _p);
1814
1815 ptr += 8;
1816 psum += 8;
1817 }
1818#endif // __AVX__
1819 for (; j + 3 < w; j += 4)
1820 {
1821 __m128 _p = _mm_loadu_ps(ptr);
1822 __m128 _sum = _mm_load_ps(psum);
1823 _p = _mm_div_ps(_p, _sum);
1824 _mm_storeu_ps(ptr, _p);
1825
1826 ptr += 4;
1827 psum += 4;
1828 }
1829#endif // __SSE2__
1830 for (; j < w; j++)
1831 {
1832 *ptr /= *psum;
1833
1834 ptr++;
1835 psum++;
1836 }
1837 }
1838 }
1839
1840 if (dims == 2 && positive_axis == 1)
1841 {
1842 int w = bottom_top_blob.w;
1843 int h = bottom_top_blob.h;
1844
1845 #pragma omp parallel for num_threads(opt.num_threads)
1846 for (int i = 0; i < h; i++)
1847 {
1848 float* ptr = bottom_top_blob.row(i);
1849
1850 float max = -FLT_MAX;
1851 {
1852 int j = 0;
1853#if __SSE2__
1854#if __AVX__
1855#if __AVX512F__
1856 __m512 _max_avx512 = _mm512_set1_ps(-FLT_MAX);
1857 for (; j + 15 < w; j += 16)
1858 {
1859 __m512 _p = _mm512_loadu_ps(ptr + j);
1860 _max_avx512 = _mm512_max_ps(_max_avx512, _p);
1861 }
1862 max = std::max(max, _mm512_comp_reduce_max_ps(_max_avx512));
1863#endif // __AVX512F__
1864 __m256 _max_avx = _mm256_set1_ps(-FLT_MAX);
1865 for (; j + 7 < w; j += 8)
1866 {
1867 __m256 _p = _mm256_loadu_ps(ptr + j);
1868 _max_avx = _mm256_max_ps(_max_avx, _p);
1869 }
1870 max = std::max(max, _mm256_reduce_max_ps(_max_avx));
1871#endif // __AVX__
1872 __m128 _max = _mm_set1_ps(-FLT_MAX);
1873 for (; j + 3 < w; j += 4)
1874 {
1875 __m128 _p = _mm_loadu_ps(ptr + j);
1876 _max = _mm_max_ps(_max, _p);
1877 }
1878 max = std::max(max, _mm_reduce_max_ps(_max));
1879#endif // __SSE2__
1880 for (; j < w; j++)
1881 {
1882 max = std::max(max, ptr[j]);
1883 }
1884 }
1885
1886 float sum = 0.f;
1887 {
1888 int j = 0;
1889#if __SSE2__
1890#if __AVX__
1891#if __AVX512F__
1892 __m512 _sum_avx512 = _mm512_setzero_ps();
1893 __m512 _max_avx512 = _mm512_set1_ps(max);
1894 for (; j + 15 < w; j += 16)
1895 {
1896 __m512 _p = _mm512_loadu_ps(ptr + j);
1897 _p = exp512_ps(_mm512_sub_ps(_p, _max_avx512));
1898 _mm512_storeu_ps(ptr + j, _p);
1899 _sum_avx512 = _mm512_add_ps(_sum_avx512, _p);
1900 }
1901 sum += _mm512_comp_reduce_add_ps(_sum_avx512);
1902#endif // __AVX512F__
1903 __m256 _sum_avx = _mm256_setzero_ps();
1904 __m256 _max_avx = _mm256_set1_ps(max);
1905 for (; j + 7 < w; j += 8)
1906 {
1907 __m256 _p = _mm256_loadu_ps(ptr + j);
1908 _p = exp256_ps(_mm256_sub_ps(_p, _max_avx));
1909 _mm256_storeu_ps(ptr + j, _p);
1910 _sum_avx = _mm256_add_ps(_sum_avx, _p);
1911 }
1912 sum += _mm256_reduce_add_ps(_sum_avx);
1913#endif // __AVX__
1914 __m128 _sum = _mm_setzero_ps();
1915 __m128 _max = _mm_set1_ps(max);
1916 for (; j + 3 < w; j += 4)
1917 {
1918 __m128 _p = _mm_loadu_ps(ptr + j);
1919 _p = exp_ps(_mm_sub_ps(_p, _max));
1920 _mm_storeu_ps(ptr + j, _p);
1921 _sum = _mm_add_ps(_sum, _p);
1922 }
1923 sum += _mm_reduce_add_ps(_sum);
1924#endif // __SSE2__
1925 for (; j < w; j++)
1926 {
1927 ptr[j] = expf(ptr[j] - max);
1928 sum += ptr[j];
1929 }
1930 }
1931
1932 {
1933 int j = 0;
1934#if __SSE2__
1935#if __AVX__
1936#if __AVX512F__
1937 __m512 _sum_avx512 = _mm512_set1_ps(sum);
1938 for (; j + 15 < w; j += 16)
1939 {
1940 __m512 _p = _mm512_loadu_ps(ptr + j);
1941 _p = _mm512_div_ps(_p, _sum_avx512);
1942 _mm512_storeu_ps(ptr + j, _p);
1943 }
1944#endif // __AVX512F__
1945 __m256 _sum_avx = _mm256_set1_ps(sum);
1946 for (; j + 7 < w; j += 8)
1947 {
1948 __m256 _p = _mm256_loadu_ps(ptr + j);
1949 _p = _mm256_div_ps(_p, _sum_avx);
1950 _mm256_storeu_ps(ptr + j, _p);
1951 }
1952#endif // __AVX__
1953 __m128 _sum = _mm_set1_ps(sum);
1954 for (; j + 3 < w; j += 4)
1955 {
1956 __m128 _p = _mm_loadu_ps(ptr + j);
1957 _p = _mm_div_ps(_p, _sum);
1958 _mm_storeu_ps(ptr + j, _p);
1959 }
1960#endif // __SSE2__
1961 for (; j < w; j++)
1962 {
1963 ptr[j] /= sum;
1964 }
1965 }
1966 }
1967 }
1968
1969 if (dims == 3 && positive_axis == 0)
1970 {
1971 int w = bottom_top_blob.w;
1972 int h = bottom_top_blob.h;
1973 int channels = bottom_top_blob.c;
1974 int size = w * h;
1975
1976 Mat max;
1977 max.create(w, h, elemsize, opt.workspace_allocator);
1978 if (max.empty())
1979 return -100;
1980 max.fill(-FLT_MAX);
1981 for (int q = 0; q < channels; q++)
1982 {
1983 float* ptr = bottom_top_blob.channel(q);
1984 float* maxptr = max;
1985
1986 int i = 0;
1987#if __SSE2__
1988#if __AVX__
1989#if __AVX512F__
1990 for (; i + 15 < size; i += 16)
1991 {
1992 __m512 _p = _mm512_loadu_ps(ptr);
1993 __m512 _max = _mm512_load_ps(maxptr);
1994 _max = _mm512_max_ps(_max, _p);
1995 _mm512_store_ps(maxptr, _max);
1996
1997 ptr += 16;
1998 maxptr += 16;
1999 }
2000#endif // __AVX512F__
2001 for (; i + 7 < size; i += 8)
2002 {
2003 __m256 _p = _mm256_loadu_ps(ptr);
2004 __m256 _max = _mm256_load_ps(maxptr);
2005 _max = _mm256_max_ps(_max, _p);
2006 _mm256_store_ps(maxptr, _max);
2007
2008 ptr += 8;
2009 maxptr += 8;
2010 }
2011#endif // __AVX__
2012 for (; i + 3 < size; i += 4)
2013 {
2014 __m128 _p = _mm_load_ps(ptr);
2015 __m128 _max = _mm_load_ps(maxptr);
2016 _max = _mm_max_ps(_max, _p);
2017 _mm_store_ps(maxptr, _max);
2018
2019 ptr += 4;
2020 maxptr += 4;
2021 }
2022#endif // __SSE2__
2023 for (; i < size; i++)
2024 {
2025 *maxptr = std::max(*maxptr, *ptr);
2026
2027 ptr++;
2028 maxptr++;
2029 }
2030 }
2031
2032 #pragma omp parallel for num_threads(opt.num_threads)
2033 for (int q = 0; q < channels; q++)
2034 {
2035 float* ptr = bottom_top_blob.channel(q);
2036 float* maxptr = max;
2037
2038 int i = 0;
2039#if __SSE2__
2040#if __AVX__
2041#if __AVX512F__
2042 for (; i + 15 < size; i += 16)
2043 {
2044 __m512 _p = _mm512_loadu_ps(ptr);
2045 __m512 _max = _mm512_load_ps(maxptr);
2046 _p = exp512_ps(_mm512_sub_ps(_p, _max));
2047 _mm512_storeu_ps(ptr, _p);
2048
2049 ptr += 16;
2050 maxptr += 16;
2051 }
2052#endif // __AVX512F__
2053 for (; i + 7 < size; i += 8)
2054 {
2055 __m256 _p = _mm256_loadu_ps(ptr);
2056 __m256 _max = _mm256_load_ps(maxptr);
2057 _p = exp256_ps(_mm256_sub_ps(_p, _max));
2058 _mm256_storeu_ps(ptr, _p);
2059
2060 ptr += 8;
2061 maxptr += 8;
2062 }
2063#endif // __AVX__
2064 for (; i + 3 < size; i += 4)
2065 {
2066 __m128 _p = _mm_load_ps(ptr);
2067 __m128 _max = _mm_load_ps(maxptr);
2068 _p = exp_ps(_mm_sub_ps(_p, _max));
2069 _mm_store_ps(ptr, _p);
2070
2071 ptr += 4;
2072 maxptr += 4;
2073 }
2074#endif // __SSE2__
2075 for (; i < size; i++)
2076 {
2077 *ptr = expf(*ptr - *maxptr);
2078
2079 ptr++;
2080 maxptr++;
2081 }
2082 }
2083
2084 Mat sum;
2085 sum.create(w, h, elemsize, opt.workspace_allocator);
2086 if (sum.empty())
2087 return -100;
2088 sum.fill(0.f);
2089 for (int q = 0; q < channels; q++)
2090 {
2091 float* ptr = bottom_top_blob.channel(q);
2092 float* sumptr = sum;
2093
2094 int i = 0;
2095#if __SSE2__
2096#if __AVX__
2097#if __AVX512F__
2098 for (; i + 15 < size; i += 16)
2099 {
2100 __m512 _p = _mm512_loadu_ps(ptr);
2101 __m512 _sum = _mm512_load_ps(sumptr);
2102 _sum = _mm512_add_ps(_sum, _p);
2103 _mm512_store_ps(sumptr, _sum);
2104
2105 ptr += 16;
2106 sumptr += 16;
2107 }
2108#endif // __AVX512F__
2109 for (; i + 7 < size; i += 8)
2110 {
2111 __m256 _p = _mm256_loadu_ps(ptr);
2112 __m256 _sum = _mm256_load_ps(sumptr);
2113 _sum = _mm256_add_ps(_sum, _p);
2114 _mm256_store_ps(sumptr, _sum);
2115
2116 ptr += 8;
2117 sumptr += 8;
2118 }
2119#endif // __AVX__
2120 for (; i + 3 < size; i += 4)
2121 {
2122 __m128 _p = _mm_load_ps(ptr);
2123 __m128 _sum = _mm_load_ps(sumptr);
2124 _sum = _mm_add_ps(_sum, _p);
2125 _mm_store_ps(sumptr, _sum);
2126
2127 ptr += 4;
2128 sumptr += 4;
2129 }
2130#endif // __SSE2__
2131 for (; i < size; i++)
2132 {
2133 *sumptr += *ptr;
2134
2135 ptr++;
2136 sumptr++;
2137 }
2138 }
2139
2140 #pragma omp parallel for num_threads(opt.num_threads)
2141 for (int q = 0; q < channels; q++)
2142 {
2143 float* ptr = bottom_top_blob.channel(q);
2144 float* sumptr = sum;
2145
2146 int i = 0;
2147#if __SSE2__
2148#if __AVX__
2149#if __AVX512F__
2150 for (; i + 15 < size; i += 16)
2151 {
2152 __m512 _p = _mm512_loadu_ps(ptr);
2153 __m512 _sum = _mm512_load_ps(sumptr);
2154 _p = _mm512_div_ps(_p, _sum);
2155 _mm512_storeu_ps(ptr, _p);
2156
2157 ptr += 16;
2158 sumptr += 16;
2159 }
2160#endif // __AVX512F__
2161 for (; i + 7 < size; i += 8)
2162 {
2163 __m256 _p = _mm256_loadu_ps(ptr);
2164 __m256 _sum = _mm256_load_ps(sumptr);
2165 _p = _mm256_div_ps(_p, _sum);
2166 _mm256_storeu_ps(ptr, _p);
2167
2168 ptr += 8;
2169 sumptr += 8;
2170 }
2171#endif // __AVX__
2172 for (; i + 3 < size; i += 4)
2173 {
2174 __m128 _p = _mm_load_ps(ptr);
2175 __m128 _sum = _mm_load_ps(sumptr);
2176 _p = _mm_div_ps(_p, _sum);
2177 _mm_store_ps(ptr, _p);
2178
2179 ptr += 4;
2180 sumptr += 4;
2181 }
2182#endif // __SSE2__
2183 for (; i < size; i++)
2184 {
2185 *ptr /= *sumptr;
2186
2187 ptr++;
2188 sumptr++;
2189 }
2190 }
2191 }
2192
2193 if (dims == 3 && positive_axis == 1)
2194 {
2195 int w = bottom_top_blob.w;
2196 int h = bottom_top_blob.h;
2197 int channels = bottom_top_blob.c;
2198
2199 Mat max;
2200 max.create(w, channels, elemsize, opt.workspace_allocator);
2201 if (max.empty())
2202 return -100;
2203 max.fill(-FLT_MAX);
2204
2205 #pragma omp parallel for num_threads(opt.num_threads)
2206 for (int q = 0; q < channels; q++)
2207 {
2208 const float* ptr = bottom_top_blob.channel(q);
2209 float* maxptr = max.row(q);
2210
2211 for (int i = 0; i < h; i++)
2212 {
2213 int j = 0;
2214#if __SSE2__
2215#if __AVX__
2216#if __AVX512F__
2217 for (; j + 15 < w; j += 16)
2218 {
2219 __m512 _p = _mm512_loadu_ps(ptr + j);
2220 __m512 _max = _mm512_loadu_ps(maxptr + j);
2221 _max = _mm512_max_ps(_max, _p);
2222 _mm512_storeu_ps(maxptr + j, _max);
2223 }
2224#endif // __AVX512F__
2225 for (; j + 7 < w; j += 8)
2226 {
2227 __m256 _p = _mm256_loadu_ps(ptr + j);
2228 __m256 _max = _mm256_loadu_ps(maxptr + j);
2229 _max = _mm256_max_ps(_max, _p);
2230 _mm256_storeu_ps(maxptr + j, _max);
2231 }
2232#endif // __AVX__
2233 for (; j + 3 < w; j += 4)
2234 {
2235 __m128 _p = _mm_loadu_ps(ptr + j);
2236 __m128 _max = _mm_loadu_ps(maxptr + j);
2237 _max = _mm_max_ps(_max, _p);
2238 _mm_storeu_ps(maxptr + j, _max);
2239 }
2240#endif // __SSE2__
2241 for (; j < w; j++)
2242 {
2243 maxptr[j] = std::max(maxptr[j], ptr[j]);
2244 }
2245
2246 ptr += w;
2247 }
2248 }
2249
2250 Mat sum;
2251 sum.create(w, channels, elemsize, opt.workspace_allocator);
2252 if (sum.empty())
2253 return -100;
2254 sum.fill(0.f);
2255
2256 #pragma omp parallel for num_threads(opt.num_threads)
2257 for (int q = 0; q < channels; q++)
2258 {
2259 float* ptr = bottom_top_blob.channel(q);
2260 float* maxptr = max.row(q);
2261 float* sumptr = sum.row(q);
2262
2263 for (int i = 0; i < h; i++)
2264 {
2265 int j = 0;
2266#if __SSE2__
2267#if __AVX__
2268#if __AVX512F__
2269 for (; j + 15 < w; j += 16)
2270 {
2271 __m512 _p = _mm512_loadu_ps(ptr + j);
2272 __m512 _max = _mm512_loadu_ps(maxptr + j);
2273 __m512 _sum = _mm512_loadu_ps(sumptr + j);
2274 _p = exp512_ps(_mm512_sub_ps(_p, _max));
2275 _sum = _mm512_add_ps(_sum, _p);
2276 _mm512_storeu_ps(ptr + j, _p);
2277 _mm512_storeu_ps(sumptr + j, _sum);
2278 }
2279#endif // __AVX512F__
2280 for (; j + 7 < w; j += 8)
2281 {
2282 __m256 _p = _mm256_loadu_ps(ptr + j);
2283 __m256 _max = _mm256_loadu_ps(maxptr + j);
2284 __m256 _sum = _mm256_loadu_ps(sumptr + j);
2285 _p = exp256_ps(_mm256_sub_ps(_p, _max));
2286 _sum = _mm256_add_ps(_sum, _p);
2287 _mm256_storeu_ps(ptr + j, _p);
2288 _mm256_storeu_ps(sumptr + j, _sum);
2289 }
2290#endif // __AVX__
2291 for (; j + 3 < w; j += 4)
2292 {
2293 __m128 _p = _mm_loadu_ps(ptr + j);
2294 __m128 _max = _mm_loadu_ps(maxptr + j);
2295 __m128 _sum = _mm_loadu_ps(sumptr + j);
2296 _p = exp_ps(_mm_sub_ps(_p, _max));
2297 _sum = _mm_add_ps(_sum, _p);
2298 _mm_storeu_ps(ptr + j, _p);
2299 _mm_storeu_ps(sumptr + j, _sum);
2300 }
2301#endif // __SSE2__
2302 for (; j < w; j++)
2303 {
2304 ptr[j] = expf(ptr[j] - maxptr[j]);
2305 sumptr[j] += ptr[j];
2306 }
2307
2308 ptr += w;
2309 }
2310 }
2311
2312 #pragma omp parallel for num_threads(opt.num_threads)
2313 for (int q = 0; q < channels; q++)
2314 {
2315 float* ptr = bottom_top_blob.channel(q);
2316 float* sumptr = sum.row(q);
2317
2318 for (int i = 0; i < h; i++)
2319 {
2320 int j = 0;
2321#if __SSE2__
2322#if __AVX__
2323#if __AVX512F__
2324 for (; j + 15 < w; j += 16)
2325 {
2326 __m512 _p = _mm512_loadu_ps(ptr + j);
2327 __m512 _sum = _mm512_loadu_ps(sumptr + j);
2328 _p = _mm512_div_ps(_p, _sum);
2329 _mm512_storeu_ps(ptr + j, _p);
2330 }
2331#endif // __AVX512F__
2332 for (; j + 7 < w; j += 8)
2333 {
2334 __m256 _p = _mm256_loadu_ps(ptr + j);
2335 __m256 _sum = _mm256_loadu_ps(sumptr + j);
2336 _p = _mm256_div_ps(_p, _sum);
2337 _mm256_storeu_ps(ptr + j, _p);
2338 }
2339#endif // __AVX__
2340 for (; j + 3 < w; j += 4)
2341 {
2342 __m128 _p = _mm_loadu_ps(ptr + j);
2343 __m128 _sum = _mm_loadu_ps(sumptr + j);
2344 _p = _mm_div_ps(_p, _sum);
2345 _mm_storeu_ps(ptr + j, _p);
2346 }
2347#endif // __SSE2__
2348 for (; j < w; j++)
2349 {
2350 ptr[j] /= sumptr[j];
2351 }
2352
2353 ptr += w;
2354 }
2355 }
2356 }
2357
2358 if (dims == 3 && positive_axis == 2)
2359 {
2360 int w = bottom_top_blob.w;
2361 int h = bottom_top_blob.h;
2362 int channels = bottom_top_blob.c;
2363
2364 #pragma omp parallel for num_threads(opt.num_threads)
2365 for (int q = 0; q < channels; q++)
2366 {
2367 float* ptr = bottom_top_blob.channel(q);
2368
2369 for (int i = 0; i < h; i++)
2370 {
2371 float max = -FLT_MAX;
2372 {
2373 int j = 0;
2374#if __SSE2__
2375#if __AVX__
2376#if __AVX512F__
2377 __m512 _max_avx512 = _mm512_set1_ps(-FLT_MAX);
2378 for (; j + 15 < w; j += 16)
2379 {
2380 __m512 _p = _mm512_loadu_ps(ptr + j);
2381 _max_avx512 = _mm512_max_ps(_max_avx512, _p);
2382 }
2383 max = std::max(max, _mm512_comp_reduce_max_ps(_max_avx512));
2384#endif // __AVX512F__
2385 __m256 _max_avx = _mm256_set1_ps(-FLT_MAX);
2386 for (; j + 7 < w; j += 8)
2387 {
2388 __m256 _p = _mm256_loadu_ps(ptr + j);
2389 _max_avx = _mm256_max_ps(_max_avx, _p);
2390 }
2391 max = std::max(max, _mm256_reduce_max_ps(_max_avx));
2392#endif // __AVX__
2393 __m128 _max = _mm_set1_ps(-FLT_MAX);
2394 for (; j + 3 < w; j += 4)
2395 {
2396 __m128 _p = _mm_loadu_ps(ptr + j);
2397 _max = _mm_max_ps(_max, _p);
2398 }
2399 max = std::max(max, _mm_reduce_max_ps(_max));
2400#endif // __SSE2__
2401 for (; j < w; j++)
2402 {
2403 max = std::max(max, ptr[j]);
2404 }
2405 }
2406
2407 float sum = 0.f;
2408 {
2409 int j = 0;
2410#if __SSE2__
2411#if __AVX__
2412#if __AVX512F__
2413 __m512 _sum_avx512 = _mm512_setzero_ps();
2414 __m512 _max_avx512 = _mm512_set1_ps(max);
2415 for (; j + 15 < w; j += 16)
2416 {
2417 __m512 _p = _mm512_loadu_ps(ptr + j);
2418 _p = exp512_ps(_mm512_sub_ps(_p, _max_avx512));
2419 _mm512_storeu_ps(ptr + j, _p);
2420 _sum_avx512 = _mm512_add_ps(_sum_avx512, _p);
2421 }
2422 sum += _mm512_comp_reduce_add_ps(_sum_avx512);
2423#endif // __AVX512F__
2424 __m256 _sum_avx = _mm256_setzero_ps();
2425 __m256 _max_avx = _mm256_set1_ps(max);
2426 for (; j + 7 < w; j += 8)
2427 {
2428 __m256 _p = _mm256_loadu_ps(ptr + j);
2429 _p = exp256_ps(_mm256_sub_ps(_p, _max_avx));
2430 _mm256_storeu_ps(ptr + j, _p);
2431 _sum_avx = _mm256_add_ps(_sum_avx, _p);
2432 }
2433 sum += _mm256_reduce_add_ps(_sum_avx);
2434#endif // __AVX__
2435 __m128 _sum = _mm_setzero_ps();
2436 __m128 _max = _mm_set1_ps(max);
2437 for (; j + 3 < w; j += 4)
2438 {
2439 __m128 _p = _mm_loadu_ps(ptr + j);
2440 _p = exp_ps(_mm_sub_ps(_p, _max));
2441 _mm_storeu_ps(ptr + j, _p);
2442 _sum = _mm_add_ps(_sum, _p);
2443 }
2444 sum += _mm_reduce_add_ps(_sum);
2445#endif // __SSE2__
2446 for (; j < w; j++)
2447 {
2448 ptr[j] = expf(ptr[j] - max);
2449 sum += ptr[j];
2450 }
2451 }
2452
2453 {
2454 int j = 0;
2455#if __SSE2__
2456#if __AVX__
2457#if __AVX512F__
2458 __m512 _sum_avx512 = _mm512_set1_ps(sum);
2459 for (; j + 15 < w; j += 16)
2460 {
2461 __m512 _p = _mm512_loadu_ps(ptr + j);
2462 _p = _mm512_div_ps(_p, _sum_avx512);
2463 _mm512_storeu_ps(ptr + j, _p);
2464 }
2465#endif // __AVX512F__
2466 __m256 _sum_avx = _mm256_set1_ps(sum);
2467 for (; j + 7 < w; j += 8)
2468 {
2469 __m256 _p = _mm256_loadu_ps(ptr + j);
2470 _p = _mm256_div_ps(_p, _sum_avx);
2471 _mm256_storeu_ps(ptr + j, _p);
2472 }
2473#endif // __AVX__
2474 __m128 _sum = _mm_set1_ps(sum);
2475 for (; j + 3 < w; j += 4)
2476 {
2477 __m128 _p = _mm_loadu_ps(ptr + j);
2478 _p = _mm_div_ps(_p, _sum);
2479 _mm_storeu_ps(ptr + j, _p);
2480 }
2481#endif // __SSE2__
2482 for (; j < w; j++)
2483 {
2484 ptr[j] /= sum;
2485 }
2486 }
2487
2488 ptr += w;
2489 }
2490 }
2491 }
2492
2493 return 0;
2494}
2495
2496} // namespace ncnn