Elffuss BraidHub

kikocis/ncnn public

master · 60.3 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2019 BUG1989. All rights reserved.
4// Copyright (C) 2018 THL A29 Limited, a Tencent company. All rights reserved.
5//
6// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
7// in compliance with the License. You may obtain a copy of the License at
8//
9// https://opensource.org/licenses/BSD-3-Clause
10//
11// Unless required by applicable law or agreed to in writing, software distributed
12// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
13// CONDITIONS OF ANY KIND, either express or implied. See the License for the
14// specific language governing permissions and limitations under the License.
15
16#include "dequantize_arm.h"
17
18#if __ARM_NEON
19#include <arm_neon.h>
20#endif // __ARM_NEON
21
22#include "arm_usability.h"
23#include "cpu.h"
24
25namespace ncnn {
26
27Dequantize_arm::Dequantize_arm()
28{
29#if __ARM_NEON
30 support_packing = true;
31#if NCNN_ARM82
32 support_fp16_storage = cpu_support_arm_asimdhp();
33#endif
34#endif // __ARM_NEON
35
36#if NCNN_BF16
37 support_bf16_storage = true;
38#endif
39}
40
41int Dequantize_arm::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
42{
43 // assert bottom_blob.elembits() == 32
44
45#if NCNN_ARM82
46 if (support_fp16_storage && opt.use_fp16_storage)
47 {
48 if (opt.use_fp16_arithmetic)
49 return forward_fp16sa(bottom_blob, top_blob, opt);
50 else
51 return forward_fp16s(bottom_blob, top_blob, opt);
52 }
53#endif
54
55#if NCNN_BF16
56 if (opt.use_bf16_storage)
57 return forward_bf16s(bottom_blob, top_blob, opt);
58#endif
59
60 int dims = bottom_blob.dims;
61 int elempack = bottom_blob.elempack;
62
63#if __ARM_NEON
64 if (elempack == 8)
65 {
66 if (dims == 1)
67 {
68 int w = bottom_blob.w;
69 int outw = w * 2;
70
71 top_blob.create(outw, (size_t)16u, 4, opt.blob_allocator);
72 if (top_blob.empty())
73 return -100;
74
75 if (scale_data_size == 1)
76 {
77 float32x4_t _scale = vdupq_n_f32(scale_data[0]);
78
79 if (bias_data_size == 0)
80 {
81 #pragma omp parallel for num_threads(opt.num_threads)
82 for (int i = 0; i < outw; i++)
83 {
84 const int* intptr = (const int*)bottom_blob + i * 4;
85 float* ptr = (float*)top_blob + i * 4;
86
87 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
88 _v = vmulq_f32(_v, _scale);
89 vst1q_f32(ptr, _v);
90 }
91 }
92 else if (bias_data_size == 1)
93 {
94 float32x4_t _bias = vdupq_n_f32(bias_data[0]);
95
96 #pragma omp parallel for num_threads(opt.num_threads)
97 for (int i = 0; i < outw; i++)
98 {
99 const int* intptr = (const int*)bottom_blob + i * 4;
100 float* ptr = (float*)top_blob + i * 4;
101
102 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
103#if __aarch64__
104 _v = vfmaq_f32(_bias, _v, _scale);
105#else
106 _v = vmlaq_f32(_bias, _v, _scale);
107#endif
108 vst1q_f32(ptr, _v);
109 }
110 }
111 else
112 {
113 #pragma omp parallel for num_threads(opt.num_threads)
114 for (int i = 0; i < outw; i++)
115 {
116 const int* intptr = (const int*)bottom_blob + i * 4;
117 float* ptr = (float*)top_blob + i * 4;
118
119 float32x4_t _bias = vld1q_f32((const float*)bias_data + i * 4);
120 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
121#if __aarch64__
122 _v = vfmaq_f32(_bias, _v, _scale);
123#else
124 _v = vmlaq_f32(_bias, _v, _scale);
125#endif
126 vst1q_f32(ptr, _v);
127 }
128 }
129 }
130 else
131 {
132 if (bias_data_size == 0)
133 {
134 #pragma omp parallel for num_threads(opt.num_threads)
135 for (int i = 0; i < outw; i++)
136 {
137 const int* intptr = (const int*)bottom_blob + i * 4;
138 float* ptr = (float*)top_blob + i * 4;
139
140 float32x4_t _scale = vld1q_f32((const float*)scale_data + i * 4);
141 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
142 _v = vmulq_f32(_v, _scale);
143 vst1q_f32(ptr, _v);
144 }
145 }
146 else if (bias_data_size == 1)
147 {
148 float32x4_t _bias = vdupq_n_f32(bias_data[0]);
149
150 #pragma omp parallel for num_threads(opt.num_threads)
151 for (int i = 0; i < outw; i++)
152 {
153 const int* intptr = (const int*)bottom_blob + i * 4;
154 float* ptr = (float*)top_blob + i * 4;
155
156 float32x4_t _scale = vld1q_f32((const float*)scale_data + i * 4);
157 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
158#if __aarch64__
159 _v = vfmaq_f32(_bias, _v, _scale);
160#else
161 _v = vmlaq_f32(_bias, _v, _scale);
162#endif
163 vst1q_f32(ptr, _v);
164 }
165 }
166 else
167 {
168 #pragma omp parallel for num_threads(opt.num_threads)
169 for (int i = 0; i < outw; i++)
170 {
171 const int* intptr = (const int*)bottom_blob + i * 4;
172 float* ptr = (float*)top_blob + i * 4;
173
174 float32x4_t _scale = vld1q_f32((const float*)scale_data + i * 4);
175 float32x4_t _bias = vld1q_f32((const float*)bias_data + i * 4);
176 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
177#if __aarch64__
178 _v = vfmaq_f32(_bias, _v, _scale);
179#else
180 _v = vmlaq_f32(_bias, _v, _scale);
181#endif
182 vst1q_f32(ptr, _v);
183 }
184 }
185 }
186 }
187
188 if (dims == 2)
189 {
190 int w = bottom_blob.w;
191 int h = bottom_blob.h;
192 int outh = h * 2;
193
194 top_blob.create(w, outh, (size_t)16u, 4, opt.blob_allocator);
195 if (top_blob.empty())
196 return -100;
197
198 if (bias_data_size == 0)
199 {
200 #pragma omp parallel for num_threads(opt.num_threads)
201 for (int i = 0; i < h; i++)
202 {
203 const int* intptr = bottom_blob.row<const int>(i);
204 float* ptr0 = top_blob.row(i * 2);
205 float* ptr1 = top_blob.row(i * 2 + 1);
206
207 float32x4_t _scale0 = scale_data_size == 1 ? vdupq_n_f32(scale_data[0]) : vld1q_f32((const float*)scale_data + i * 8);
208 float32x4_t _scale1 = scale_data_size == 1 ? vdupq_n_f32(scale_data[0]) : vld1q_f32((const float*)scale_data + i * 8 + 4);
209
210 for (int j = 0; j < w; j++)
211 {
212 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
213 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32(intptr + 4));
214 _v0 = vmulq_f32(_v0, _scale0);
215 _v1 = vmulq_f32(_v1, _scale1);
216 vst1q_f32(ptr0, _v0);
217 vst1q_f32(ptr1, _v1);
218
219 intptr += 8;
220 ptr0 += 4;
221 ptr1 += 4;
222 }
223 }
224 }
225 else
226 {
227 #pragma omp parallel for num_threads(opt.num_threads)
228 for (int i = 0; i < h; i++)
229 {
230 const int* intptr = bottom_blob.row<const int>(i);
231 float* ptr0 = top_blob.row(i * 2);
232 float* ptr1 = top_blob.row(i * 2 + 1);
233
234 float32x4_t _scale0 = scale_data_size == 1 ? vdupq_n_f32(scale_data[0]) : vld1q_f32((const float*)scale_data + i * 8);
235 float32x4_t _scale1 = scale_data_size == 1 ? vdupq_n_f32(scale_data[0]) : vld1q_f32((const float*)scale_data + i * 8 + 4);
236 float32x4_t _bias0 = bias_data_size == 1 ? vdupq_n_f32(bias_data[0]) : vld1q_f32((const float*)bias_data + i * 8);
237 float32x4_t _bias1 = bias_data_size == 1 ? vdupq_n_f32(bias_data[0]) : vld1q_f32((const float*)bias_data + i * 8 + 4);
238
239 for (int j = 0; j < w; j++)
240 {
241 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
242 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32(intptr + 4));
243#if __aarch64__
244 _v0 = vfmaq_f32(_bias0, _v0, _scale0);
245 _v1 = vfmaq_f32(_bias1, _v1, _scale1);
246#else
247 _v0 = vmlaq_f32(_bias0, _v0, _scale0);
248 _v1 = vmlaq_f32(_bias1, _v1, _scale1);
249#endif
250 vst1q_f32(ptr0, _v0);
251 vst1q_f32(ptr1, _v1);
252
253 intptr += 8;
254 ptr0 += 4;
255 ptr1 += 4;
256 }
257 }
258 }
259 }
260
261 if (dims == 3)
262 {
263 int w = bottom_blob.w;
264 int h = bottom_blob.h;
265 int channels = bottom_blob.c;
266 int size = w * h;
267 int outc = channels * 2;
268
269 top_blob.create(w, h, outc, (size_t)16u, 4, opt.blob_allocator);
270 if (top_blob.empty())
271 return -100;
272
273 if (bias_data_size == 0)
274 {
275 #pragma omp parallel for num_threads(opt.num_threads)
276 for (int q = 0; q < channels; q++)
277 {
278 const int* intptr = bottom_blob.channel(q);
279 float* ptr0 = top_blob.channel(q * 2);
280 float* ptr1 = top_blob.channel(q * 2 + 1);
281
282 float32x4_t _scale0 = scale_data_size == 1 ? vdupq_n_f32(scale_data[0]) : vld1q_f32((const float*)scale_data + q * 8);
283 float32x4_t _scale1 = scale_data_size == 1 ? vdupq_n_f32(scale_data[0]) : vld1q_f32((const float*)scale_data + q * 8 + 4);
284
285 int i = 0;
286 for (; i + 1 < size; i += 2)
287 {
288 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
289 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32(intptr + 4));
290 float32x4_t _v2 = vcvtq_f32_s32(vld1q_s32(intptr + 8));
291 float32x4_t _v3 = vcvtq_f32_s32(vld1q_s32(intptr + 12));
292 _v0 = vmulq_f32(_v0, _scale0);
293 _v1 = vmulq_f32(_v1, _scale1);
294 _v2 = vmulq_f32(_v2, _scale0);
295 _v3 = vmulq_f32(_v3, _scale1);
296 vst1q_f32(ptr0, _v0);
297 vst1q_f32(ptr0 + 4, _v2);
298 vst1q_f32(ptr1, _v1);
299 vst1q_f32(ptr1 + 4, _v3);
300
301 intptr += 16;
302 ptr0 += 8;
303 ptr1 += 8;
304 }
305 for (; i < size; i++)
306 {
307 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
308 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32(intptr + 4));
309 _v0 = vmulq_f32(_v0, _scale0);
310 _v1 = vmulq_f32(_v1, _scale1);
311 vst1q_f32(ptr0, _v0);
312 vst1q_f32(ptr1, _v1);
313
314 intptr += 8;
315 ptr0 += 4;
316 ptr1 += 4;
317 }
318 }
319 }
320 else
321 {
322 #pragma omp parallel for num_threads(opt.num_threads)
323 for (int q = 0; q < channels; q++)
324 {
325 const int* intptr = bottom_blob.channel(q);
326 float* ptr0 = top_blob.channel(q * 2);
327 float* ptr1 = top_blob.channel(q * 2 + 1);
328
329 float32x4_t _scale0 = scale_data_size == 1 ? vdupq_n_f32(scale_data[0]) : vld1q_f32((const float*)scale_data + q * 8);
330 float32x4_t _scale1 = scale_data_size == 1 ? vdupq_n_f32(scale_data[0]) : vld1q_f32((const float*)scale_data + q * 8 + 4);
331 float32x4_t _bias0 = bias_data_size == 1 ? vdupq_n_f32(bias_data[0]) : vld1q_f32((const float*)bias_data + q * 8);
332 float32x4_t _bias1 = bias_data_size == 1 ? vdupq_n_f32(bias_data[0]) : vld1q_f32((const float*)bias_data + q * 8 + 4);
333
334 int i = 0;
335 for (; i + 1 < size; i += 2)
336 {
337 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
338 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32(intptr + 4));
339 float32x4_t _v2 = vcvtq_f32_s32(vld1q_s32(intptr + 8));
340 float32x4_t _v3 = vcvtq_f32_s32(vld1q_s32(intptr + 12));
341#if __aarch64__
342 _v0 = vfmaq_f32(_bias0, _v0, _scale0);
343 _v1 = vfmaq_f32(_bias1, _v1, _scale1);
344 _v2 = vfmaq_f32(_bias0, _v2, _scale0);
345 _v3 = vfmaq_f32(_bias1, _v3, _scale1);
346#else
347 _v0 = vmlaq_f32(_bias0, _v0, _scale0);
348 _v1 = vmlaq_f32(_bias1, _v1, _scale1);
349 _v2 = vmlaq_f32(_bias0, _v2, _scale0);
350 _v3 = vmlaq_f32(_bias1, _v3, _scale1);
351#endif
352 vst1q_f32(ptr0, _v0);
353 vst1q_f32(ptr0 + 4, _v2);
354 vst1q_f32(ptr1, _v1);
355 vst1q_f32(ptr1 + 4, _v3);
356
357 intptr += 16;
358 ptr0 += 8;
359 ptr1 += 8;
360 }
361 for (; i < size; i++)
362 {
363 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
364 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32(intptr + 4));
365#if __aarch64__
366 _v0 = vfmaq_f32(_bias0, _v0, _scale0);
367 _v1 = vfmaq_f32(_bias1, _v1, _scale1);
368#else
369 _v0 = vmlaq_f32(_bias0, _v0, _scale0);
370 _v1 = vmlaq_f32(_bias1, _v1, _scale1);
371#endif
372 vst1q_f32(ptr0, _v0);
373 vst1q_f32(ptr1, _v1);
374
375 intptr += 8;
376 ptr0 += 4;
377 ptr1 += 4;
378 }
379 }
380 }
381 }
382
383 return 0;
384 }
385
386 if (elempack == 4)
387 {
388 if (dims == 1)
389 {
390 int w = bottom_blob.w;
391
392 top_blob.create(w, (size_t)16u, elempack, opt.blob_allocator);
393 if (top_blob.empty())
394 return -100;
395
396 if (scale_data_size == 1)
397 {
398 float32x4_t _scale = vdupq_n_f32(scale_data[0]);
399
400 if (bias_data_size == 0)
401 {
402 #pragma omp parallel for num_threads(opt.num_threads)
403 for (int i = 0; i < w; i++)
404 {
405 const int* intptr = (const int*)bottom_blob + i * 4;
406 float* ptr = (float*)top_blob + i * 4;
407
408 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
409 _v = vmulq_f32(_v, _scale);
410 vst1q_f32(ptr, _v);
411 }
412 }
413 else if (bias_data_size == 1)
414 {
415 float32x4_t _bias = vdupq_n_f32(bias_data[0]);
416
417 #pragma omp parallel for num_threads(opt.num_threads)
418 for (int i = 0; i < w; i++)
419 {
420 const int* intptr = (const int*)bottom_blob + i * 4;
421 float* ptr = (float*)top_blob + i * 4;
422
423 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
424#if __aarch64__
425 _v = vfmaq_f32(_bias, _v, _scale);
426#else
427 _v = vmlaq_f32(_bias, _v, _scale);
428#endif
429 vst1q_f32(ptr, _v);
430 }
431 }
432 else
433 {
434 #pragma omp parallel for num_threads(opt.num_threads)
435 for (int i = 0; i < w; i++)
436 {
437 const int* intptr = (const int*)bottom_blob + i * 4;
438 float* ptr = (float*)top_blob + i * 4;
439
440 float32x4_t _bias = vld1q_f32((const float*)bias_data + i * 4);
441 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
442#if __aarch64__
443 _v = vfmaq_f32(_bias, _v, _scale);
444#else
445 _v = vmlaq_f32(_bias, _v, _scale);
446#endif
447 vst1q_f32(ptr, _v);
448 }
449 }
450 }
451 else
452 {
453 if (bias_data_size == 0)
454 {
455 #pragma omp parallel for num_threads(opt.num_threads)
456 for (int i = 0; i < w; i++)
457 {
458 const int* intptr = (const int*)bottom_blob + i * 4;
459 float* ptr = (float*)top_blob + i * 4;
460
461 float32x4_t _scale = vld1q_f32((const float*)scale_data + i * 4);
462 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
463 _v = vmulq_f32(_v, _scale);
464 vst1q_f32(ptr, _v);
465 }
466 }
467 else if (bias_data_size == 1)
468 {
469 float32x4_t _bias = vdupq_n_f32(bias_data[0]);
470
471 #pragma omp parallel for num_threads(opt.num_threads)
472 for (int i = 0; i < w; i++)
473 {
474 const int* intptr = (const int*)bottom_blob + i * 4;
475 float* ptr = (float*)top_blob + i * 4;
476
477 float32x4_t _scale = vld1q_f32((const float*)scale_data + i * 4);
478 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
479#if __aarch64__
480 _v = vfmaq_f32(_bias, _v, _scale);
481#else
482 _v = vmlaq_f32(_bias, _v, _scale);
483#endif
484 vst1q_f32(ptr, _v);
485 }
486 }
487 else
488 {
489 #pragma omp parallel for num_threads(opt.num_threads)
490 for (int i = 0; i < w; i++)
491 {
492 const int* intptr = (const int*)bottom_blob + i * 4;
493 float* ptr = (float*)top_blob + i * 4;
494
495 float32x4_t _scale = vld1q_f32((const float*)scale_data + i * 4);
496 float32x4_t _bias = vld1q_f32((const float*)bias_data + i * 4);
497 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
498#if __aarch64__
499 _v = vfmaq_f32(_bias, _v, _scale);
500#else
501 _v = vmlaq_f32(_bias, _v, _scale);
502#endif
503 vst1q_f32(ptr, _v);
504 }
505 }
506 }
507 }
508
509 if (dims == 2)
510 {
511 int w = bottom_blob.w;
512 int h = bottom_blob.h;
513
514 top_blob.create(w, h, (size_t)16u, elempack, opt.blob_allocator);
515 if (top_blob.empty())
516 return -100;
517
518 if (bias_data_size == 0)
519 {
520 #pragma omp parallel for num_threads(opt.num_threads)
521 for (int i = 0; i < h; i++)
522 {
523 const int* intptr = bottom_blob.row<const int>(i);
524 float* ptr = top_blob.row(i);
525
526 float32x4_t _scale = scale_data_size == 1 ? vdupq_n_f32(scale_data[0]) : vld1q_f32((const float*)scale_data + i * 4);
527
528 for (int j = 0; j < w; j++)
529 {
530 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
531 _v = vmulq_f32(_v, _scale);
532 vst1q_f32(ptr, _v);
533
534 intptr += 4;
535 ptr += 4;
536 }
537 }
538 }
539 else
540 {
541 #pragma omp parallel for num_threads(opt.num_threads)
542 for (int i = 0; i < h; i++)
543 {
544 const int* intptr = bottom_blob.row<const int>(i);
545 float* ptr = top_blob.row(i);
546
547 float32x4_t _scale = scale_data_size == 1 ? vdupq_n_f32(scale_data[0]) : vld1q_f32((const float*)scale_data + i * 4);
548 float32x4_t _bias = bias_data_size == 1 ? vdupq_n_f32(bias_data[0]) : vld1q_f32((const float*)bias_data + i * 4);
549
550 for (int j = 0; j < w; j++)
551 {
552 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
553#if __aarch64__
554 _v = vfmaq_f32(_bias, _v, _scale);
555#else
556 _v = vmlaq_f32(_bias, _v, _scale);
557#endif
558 vst1q_f32(ptr, _v);
559
560 intptr += 4;
561 ptr += 4;
562 }
563 }
564 }
565 }
566
567 if (dims == 3)
568 {
569 int w = bottom_blob.w;
570 int h = bottom_blob.h;
571 int channels = bottom_blob.c;
572 int size = w * h;
573
574 top_blob.create(w, h, channels, (size_t)16u, elempack, opt.blob_allocator);
575 if (top_blob.empty())
576 return -100;
577
578 if (bias_data_size == 0)
579 {
580 #pragma omp parallel for num_threads(opt.num_threads)
581 for (int q = 0; q < channels; q++)
582 {
583 const int* intptr = bottom_blob.channel(q);
584 float* ptr = top_blob.channel(q);
585
586 float32x4_t _scale = scale_data_size == 1 ? vdupq_n_f32(scale_data[0]) : vld1q_f32((const float*)scale_data + q * 4);
587
588 int i = 0;
589 for (; i + 1 < size; i += 2)
590 {
591 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
592 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32(intptr + 4));
593 _v0 = vmulq_f32(_v0, _scale);
594 _v1 = vmulq_f32(_v1, _scale);
595 vst1q_f32(ptr, _v0);
596 vst1q_f32(ptr + 4, _v1);
597
598 intptr += 8;
599 ptr += 8;
600 }
601 for (; i < size; i++)
602 {
603 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
604 _v = vmulq_f32(_v, _scale);
605 vst1q_f32(ptr, _v);
606
607 intptr += 4;
608 ptr += 4;
609 }
610 }
611 }
612 else
613 {
614 #pragma omp parallel for num_threads(opt.num_threads)
615 for (int q = 0; q < channels; q++)
616 {
617 const int* intptr = bottom_blob.channel(q);
618 float* ptr = top_blob.channel(q);
619
620 float32x4_t _scale = scale_data_size == 1 ? vdupq_n_f32(scale_data[0]) : vld1q_f32((const float*)scale_data + q * 4);
621 float32x4_t _bias = bias_data_size == 1 ? vdupq_n_f32(bias_data[0]) : vld1q_f32((const float*)bias_data + q * 4);
622
623 int i = 0;
624 for (; i + 1 < size; i += 2)
625 {
626 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
627 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32(intptr + 4));
628#if __aarch64__
629 _v0 = vfmaq_f32(_bias, _v0, _scale);
630 _v1 = vfmaq_f32(_bias, _v1, _scale);
631#else
632 _v0 = vmlaq_f32(_bias, _v0, _scale);
633 _v1 = vmlaq_f32(_bias, _v1, _scale);
634#endif
635 vst1q_f32(ptr, _v0);
636 vst1q_f32(ptr + 4, _v1);
637
638 intptr += 8;
639 ptr += 8;
640 }
641 for (; i < size; i++)
642 {
643 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
644#if __aarch64__
645 _v = vfmaq_f32(_bias, _v, _scale);
646#else
647 _v = vmlaq_f32(_bias, _v, _scale);
648#endif
649 vst1q_f32(ptr, _v);
650
651 intptr += 4;
652 ptr += 4;
653 }
654 }
655 }
656 }
657
658 return 0;
659 }
660#endif // __ARM_NEON
661
662 if (dims == 1)
663 {
664 int w = bottom_blob.w;
665
666 top_blob.create(w, (size_t)4u, opt.blob_allocator);
667 if (top_blob.empty())
668 return -100;
669
670 const int* intptr = bottom_blob;
671 float* ptr = top_blob;
672
673 if (scale_data_size == 1)
674 {
675 const float scale = scale_data[0];
676
677 if (bias_data_size == 0)
678 {
679 #pragma omp parallel for num_threads(opt.num_threads)
680 for (int i = 0; i < w; i++)
681 {
682 ptr[i] = intptr[i] * scale;
683 }
684 }
685 else if (bias_data_size == 1)
686 {
687 const float bias = bias_data[0];
688
689 #pragma omp parallel for num_threads(opt.num_threads)
690 for (int i = 0; i < w; i++)
691 {
692 ptr[i] = intptr[i] * scale + bias;
693 }
694 }
695 else
696 {
697 #pragma omp parallel for num_threads(opt.num_threads)
698 for (int i = 0; i < w; i++)
699 {
700 ptr[i] = intptr[i] * scale + bias_data[i];
701 }
702 }
703 }
704 else
705 {
706 if (bias_data_size == 0)
707 {
708 #pragma omp parallel for num_threads(opt.num_threads)
709 for (int i = 0; i < w; i++)
710 {
711 ptr[i] = intptr[i] * scale_data[i];
712 }
713 }
714 else if (bias_data_size == 1)
715 {
716 const float bias = bias_data[0];
717
718 #pragma omp parallel for num_threads(opt.num_threads)
719 for (int i = 0; i < w; i++)
720 {
721 ptr[i] = intptr[i] * scale_data[i] + bias;
722 }
723 }
724 else
725 {
726 #pragma omp parallel for num_threads(opt.num_threads)
727 for (int i = 0; i < w; i++)
728 {
729 ptr[i] = intptr[i] * scale_data[i] + bias_data[i];
730 }
731 }
732 }
733 }
734
735 if (dims == 2)
736 {
737 int w = bottom_blob.w;
738 int h = bottom_blob.h;
739
740 top_blob.create(w, h, (size_t)4u, opt.blob_allocator);
741 if (top_blob.empty())
742 return -100;
743
744 if (bias_data_size == 0)
745 {
746 #pragma omp parallel for num_threads(opt.num_threads)
747 for (int i = 0; i < h; i++)
748 {
749 const int* intptr = bottom_blob.row<const int>(i);
750 float* ptr = top_blob.row(i);
751
752 const float scale = scale_data_size == 1 ? scale_data[0] : scale_data[i];
753
754 int j = 0;
755#if __ARM_NEON
756 float32x4_t _scale = vdupq_n_f32(scale);
757 for (; j + 3 < w; j += 4)
758 {
759 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
760 _v = vmulq_f32(_v, _scale);
761 vst1q_f32(ptr, _v);
762
763 intptr += 4;
764 ptr += 4;
765 }
766#endif // __ARM_NEON
767 for (; j < w; j++)
768 {
769 *ptr++ = *intptr++ * scale;
770 }
771 }
772 }
773 else
774 {
775 #pragma omp parallel for num_threads(opt.num_threads)
776 for (int i = 0; i < h; i++)
777 {
778 const int* intptr = bottom_blob.row<const int>(i);
779 float* ptr = top_blob.row(i);
780
781 const float scale = scale_data_size == 1 ? scale_data[0] : scale_data[i];
782 const float bias = bias_data_size == 1 ? bias_data[0] : bias_data[i];
783
784 int j = 0;
785#if __ARM_NEON
786 float32x4_t _scale = vdupq_n_f32(scale);
787 float32x4_t _bias = vdupq_n_f32(bias);
788 for (; j + 3 < w; j += 4)
789 {
790 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
791#if __aarch64__
792 _v = vfmaq_f32(_bias, _v, _scale);
793#else
794 _v = vmlaq_f32(_bias, _v, _scale);
795#endif
796 vst1q_f32(ptr, _v);
797
798 intptr += 4;
799 ptr += 4;
800 }
801#endif // __ARM_NEON
802 for (; j < w; j++)
803 {
804 *ptr++ = *intptr++ * scale + bias;
805 }
806 }
807 }
808 }
809
810 if (dims == 3)
811 {
812 int w = bottom_blob.w;
813 int h = bottom_blob.h;
814 int channels = bottom_blob.c;
815 int size = w * h;
816
817 top_blob.create(w, h, channels, (size_t)4u, opt.blob_allocator);
818 if (top_blob.empty())
819 return -100;
820
821 if (bias_data_size == 0)
822 {
823 #pragma omp parallel for num_threads(opt.num_threads)
824 for (int q = 0; q < channels; q++)
825 {
826 const int* intptr = bottom_blob.channel(q);
827 float* ptr = top_blob.channel(q);
828
829 const float scale = scale_data_size == 1 ? scale_data[0] : scale_data[q];
830
831 int i = 0;
832#if __ARM_NEON
833 float32x4_t _scale = vdupq_n_f32(scale);
834 for (; i + 7 < size; i += 8)
835 {
836 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
837 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32(intptr + 4));
838 _v0 = vmulq_f32(_v0, _scale);
839 _v1 = vmulq_f32(_v1, _scale);
840 vst1q_f32(ptr, _v0);
841 vst1q_f32(ptr + 4, _v1);
842
843 intptr += 8;
844 ptr += 8;
845 }
846 for (; i + 3 < size; i += 4)
847 {
848 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
849 _v = vmulq_f32(_v, _scale);
850 vst1q_f32(ptr, _v);
851
852 intptr += 4;
853 ptr += 4;
854 }
855#endif // __ARM_NEON
856 for (; i < size; i++)
857 {
858 *ptr++ = *intptr++ * scale;
859 }
860 }
861 }
862 else
863 {
864 #pragma omp parallel for num_threads(opt.num_threads)
865 for (int q = 0; q < channels; q++)
866 {
867 const int* intptr = bottom_blob.channel(q);
868 float* ptr = top_blob.channel(q);
869
870 const float scale = scale_data_size == 1 ? scale_data[0] : scale_data[q];
871 const float bias = bias_data_size == 1 ? bias_data[0] : bias_data[q];
872
873 int i = 0;
874#if __ARM_NEON
875 float32x4_t _scale = vdupq_n_f32(scale);
876 float32x4_t _bias = vdupq_n_f32(bias);
877 for (; i + 7 < size; i += 8)
878 {
879 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
880 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32(intptr + 4));
881#if __aarch64__
882 _v0 = vfmaq_f32(_bias, _v0, _scale);
883 _v1 = vfmaq_f32(_bias, _v1, _scale);
884#else
885 _v0 = vmlaq_f32(_bias, _v0, _scale);
886 _v1 = vmlaq_f32(_bias, _v1, _scale);
887#endif
888 vst1q_f32(ptr, _v0);
889 vst1q_f32(ptr + 4, _v1);
890
891 intptr += 8;
892 ptr += 8;
893 }
894 for (; i + 3 < size; i += 4)
895 {
896 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
897#if __aarch64__
898 _v = vfmaq_f32(_bias, _v, _scale);
899#else
900 _v = vmlaq_f32(_bias, _v, _scale);
901#endif
902 vst1q_f32(ptr, _v);
903
904 intptr += 4;
905 ptr += 4;
906 }
907#endif // __ARM_NEON
908 for (; i < size; i++)
909 {
910 *ptr++ = *intptr++ * scale + bias;
911 }
912 }
913 }
914 }
915
916 return 0;
917}
918
919#if NCNN_BF16
920int Dequantize_arm::forward_bf16s(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
921{
922 int dims = bottom_blob.dims;
923 int elempack = bottom_blob.elempack;
924
925#if __ARM_NEON
926 if (elempack == 8)
927 {
928 if (dims == 1)
929 {
930 int w = bottom_blob.w;
931 int outw = w * 2;
932
933 top_blob.create(outw, (size_t)8u, 4, opt.blob_allocator);
934 if (top_blob.empty())
935 return -100;
936
937 if (scale_data_size == 1)
938 {
939 float32x4_t _scale = vdupq_n_f32(scale_data[0]);
940
941 if (bias_data_size == 0)
942 {
943 #pragma omp parallel for num_threads(opt.num_threads)
944 for (int i = 0; i < outw; i++)
945 {
946 const int* intptr = (const int*)bottom_blob + i * 4;
947 unsigned short* ptr = (unsigned short*)top_blob + i * 4;
948
949 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
950 _v = vmulq_f32(_v, _scale);
951 vst1_u16(ptr, float2bfloat(_v));
952 }
953 }
954 else if (bias_data_size == 1)
955 {
956 float32x4_t _bias = vdupq_n_f32(bias_data[0]);
957
958 #pragma omp parallel for num_threads(opt.num_threads)
959 for (int i = 0; i < outw; i++)
960 {
961 const int* intptr = (const int*)bottom_blob + i * 4;
962 unsigned short* ptr = (unsigned short*)top_blob + i * 4;
963
964 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
965#if __aarch64__
966 _v = vfmaq_f32(_bias, _v, _scale);
967#else
968 _v = vmlaq_f32(_bias, _v, _scale);
969#endif
970 vst1_u16(ptr, float2bfloat(_v));
971 }
972 }
973 else
974 {
975 #pragma omp parallel for num_threads(opt.num_threads)
976 for (int i = 0; i < outw; i++)
977 {
978 const int* intptr = (const int*)bottom_blob + i * 4;
979 unsigned short* ptr = (unsigned short*)top_blob + i * 4;
980
981 float32x4_t _bias = vld1q_f32((const float*)bias_data + i * 4);
982 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
983#if __aarch64__
984 _v = vfmaq_f32(_bias, _v, _scale);
985#else
986 _v = vmlaq_f32(_bias, _v, _scale);
987#endif
988 vst1_u16(ptr, float2bfloat(_v));
989 }
990 }
991 }
992 else
993 {
994 if (bias_data_size == 0)
995 {
996 #pragma omp parallel for num_threads(opt.num_threads)
997 for (int i = 0; i < outw; i++)
998 {
999 const int* intptr = (const int*)bottom_blob + i * 4;
1000 unsigned short* ptr = (unsigned short*)top_blob + i * 4;
1001
1002 float32x4_t _scale = vld1q_f32((const float*)scale_data + i * 4);
1003 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
1004 _v = vmulq_f32(_v, _scale);
1005 vst1_u16(ptr, float2bfloat(_v));
1006 }
1007 }
1008 else if (bias_data_size == 1)
1009 {
1010 float32x4_t _bias = vdupq_n_f32(bias_data[0]);
1011
1012 #pragma omp parallel for num_threads(opt.num_threads)
1013 for (int i = 0; i < outw; i++)
1014 {
1015 const int* intptr = (const int*)bottom_blob + i * 4;
1016 unsigned short* ptr = (unsigned short*)top_blob + i * 4;
1017
1018 float32x4_t _scale = vld1q_f32((const float*)scale_data + i * 4);
1019 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
1020#if __aarch64__
1021 _v = vfmaq_f32(_bias, _v, _scale);
1022#else
1023 _v = vmlaq_f32(_bias, _v, _scale);
1024#endif
1025 vst1_u16(ptr, float2bfloat(_v));
1026 }
1027 }
1028 else
1029 {
1030 #pragma omp parallel for num_threads(opt.num_threads)
1031 for (int i = 0; i < outw; i++)
1032 {
1033 const int* intptr = (const int*)bottom_blob + i * 4;
1034 unsigned short* ptr = (unsigned short*)top_blob + i * 4;
1035
1036 float32x4_t _scale = vld1q_f32((const float*)scale_data + i * 4);
1037 float32x4_t _bias = vld1q_f32((const float*)bias_data + i * 4);
1038 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
1039#if __aarch64__
1040 _v = vfmaq_f32(_bias, _v, _scale);
1041#else
1042 _v = vmlaq_f32(_bias, _v, _scale);
1043#endif
1044 vst1_u16(ptr, float2bfloat(_v));
1045 }
1046 }
1047 }
1048 }
1049
1050 if (dims == 2)
1051 {
1052 int w = bottom_blob.w;
1053 int h = bottom_blob.h;
1054 int outh = h * 2;
1055
1056 top_blob.create(w, outh, (size_t)8u, 4, opt.blob_allocator);
1057 if (top_blob.empty())
1058 return -100;
1059
1060 if (bias_data_size == 0)
1061 {
1062 #pragma omp parallel for num_threads(opt.num_threads)
1063 for (int i = 0; i < h; i++)
1064 {
1065 const int* intptr = bottom_blob.row<const int>(i);
1066 unsigned short* ptr0 = top_blob.row<unsigned short>(i * 2);
1067 unsigned short* ptr1 = top_blob.row<unsigned short>(i * 2 + 1);
1068
1069 float32x4_t _scale0 = scale_data_size == 1 ? vdupq_n_f32(scale_data[0]) : vld1q_f32((const float*)scale_data + i * 8);
1070 float32x4_t _scale1 = scale_data_size == 1 ? vdupq_n_f32(scale_data[0]) : vld1q_f32((const float*)scale_data + i * 8 + 4);
1071
1072 for (int j = 0; j < w; j++)
1073 {
1074 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
1075 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32(intptr + 4));
1076 _v0 = vmulq_f32(_v0, _scale0);
1077 _v1 = vmulq_f32(_v1, _scale1);
1078 vst1_u16(ptr0, float2bfloat(_v0));
1079 vst1_u16(ptr1, float2bfloat(_v1));
1080
1081 intptr += 8;
1082 ptr0 += 4;
1083 ptr1 += 4;
1084 }
1085 }
1086 }
1087 else
1088 {
1089 #pragma omp parallel for num_threads(opt.num_threads)
1090 for (int i = 0; i < h; i++)
1091 {
1092 const int* intptr = bottom_blob.row<const int>(i);
1093 unsigned short* ptr0 = top_blob.row<unsigned short>(i * 2);
1094 unsigned short* ptr1 = top_blob.row<unsigned short>(i * 2 + 1);
1095
1096 float32x4_t _scale0 = scale_data_size == 1 ? vdupq_n_f32(scale_data[0]) : vld1q_f32((const float*)scale_data + i * 8);
1097 float32x4_t _scale1 = scale_data_size == 1 ? vdupq_n_f32(scale_data[0]) : vld1q_f32((const float*)scale_data + i * 8 + 4);
1098 float32x4_t _bias0 = bias_data_size == 1 ? vdupq_n_f32(bias_data[0]) : vld1q_f32((const float*)bias_data + i * 8);
1099 float32x4_t _bias1 = bias_data_size == 1 ? vdupq_n_f32(bias_data[0]) : vld1q_f32((const float*)bias_data + i * 8 + 4);
1100
1101 for (int j = 0; j < w; j++)
1102 {
1103 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
1104 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32(intptr + 4));
1105#if __aarch64__
1106 _v0 = vfmaq_f32(_bias0, _v0, _scale0);
1107 _v1 = vfmaq_f32(_bias1, _v1, _scale1);
1108#else
1109 _v0 = vmlaq_f32(_bias0, _v0, _scale0);
1110 _v1 = vmlaq_f32(_bias1, _v1, _scale1);
1111#endif
1112 vst1_u16(ptr0, float2bfloat(_v0));
1113 vst1_u16(ptr1, float2bfloat(_v1));
1114
1115 intptr += 8;
1116 ptr0 += 4;
1117 ptr1 += 4;
1118 }
1119 }
1120 }
1121 }
1122
1123 if (dims == 3)
1124 {
1125 int w = bottom_blob.w;
1126 int h = bottom_blob.h;
1127 int channels = bottom_blob.c;
1128 int size = w * h;
1129 int outc = channels * 2;
1130
1131 top_blob.create(w, h, outc, (size_t)8u, 4, opt.blob_allocator);
1132 if (top_blob.empty())
1133 return -100;
1134
1135 if (bias_data_size == 0)
1136 {
1137 #pragma omp parallel for num_threads(opt.num_threads)
1138 for (int q = 0; q < channels; q++)
1139 {
1140 const int* intptr = bottom_blob.channel(q);
1141 unsigned short* ptr0 = top_blob.channel(q * 2);
1142 unsigned short* ptr1 = top_blob.channel(q * 2 + 1);
1143
1144 float32x4_t _scale0 = scale_data_size == 1 ? vdupq_n_f32(scale_data[0]) : vld1q_f32((const float*)scale_data + q * 8);
1145 float32x4_t _scale1 = scale_data_size == 1 ? vdupq_n_f32(scale_data[0]) : vld1q_f32((const float*)scale_data + q * 8 + 4);
1146
1147 for (int i = 0; i < size; i++)
1148 {
1149 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
1150 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32(intptr + 4));
1151 _v0 = vmulq_f32(_v0, _scale0);
1152 _v1 = vmulq_f32(_v1, _scale1);
1153 vst1_u16(ptr0, float2bfloat(_v0));
1154 vst1_u16(ptr1, float2bfloat(_v1));
1155
1156 intptr += 8;
1157 ptr0 += 4;
1158 ptr1 += 4;
1159 }
1160 }
1161 }
1162 else
1163 {
1164 #pragma omp parallel for num_threads(opt.num_threads)
1165 for (int q = 0; q < channels; q++)
1166 {
1167 const int* intptr = bottom_blob.channel(q);
1168 unsigned short* ptr0 = top_blob.channel(q * 2);
1169 unsigned short* ptr1 = top_blob.channel(q * 2 + 1);
1170
1171 float32x4_t _scale0 = scale_data_size == 1 ? vdupq_n_f32(scale_data[0]) : vld1q_f32((const float*)scale_data + q * 8);
1172 float32x4_t _scale1 = scale_data_size == 1 ? vdupq_n_f32(scale_data[0]) : vld1q_f32((const float*)scale_data + q * 8 + 4);
1173 float32x4_t _bias0 = bias_data_size == 1 ? vdupq_n_f32(bias_data[0]) : vld1q_f32((const float*)bias_data + q * 8);
1174 float32x4_t _bias1 = bias_data_size == 1 ? vdupq_n_f32(bias_data[0]) : vld1q_f32((const float*)bias_data + q * 8 + 4);
1175
1176 for (int i = 0; i < size; i++)
1177 {
1178 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
1179 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32(intptr + 4));
1180#if __aarch64__
1181 _v0 = vfmaq_f32(_bias0, _v0, _scale0);
1182 _v1 = vfmaq_f32(_bias1, _v1, _scale1);
1183#else
1184 _v0 = vmlaq_f32(_bias0, _v0, _scale0);
1185 _v1 = vmlaq_f32(_bias1, _v1, _scale1);
1186#endif
1187 vst1_u16(ptr0, float2bfloat(_v0));
1188 vst1_u16(ptr1, float2bfloat(_v1));
1189
1190 intptr += 8;
1191 ptr0 += 4;
1192 ptr1 += 4;
1193 }
1194 }
1195 }
1196 }
1197
1198 return 0;
1199 }
1200
1201 if (elempack == 4)
1202 {
1203 if (dims == 1)
1204 {
1205 int w = bottom_blob.w;
1206
1207 top_blob.create(w, (size_t)8u, elempack, opt.blob_allocator);
1208 if (top_blob.empty())
1209 return -100;
1210
1211 if (scale_data_size == 1)
1212 {
1213 float32x4_t _scale = vdupq_n_f32(scale_data[0]);
1214
1215 if (bias_data_size == 0)
1216 {
1217 #pragma omp parallel for num_threads(opt.num_threads)
1218 for (int i = 0; i < w; i++)
1219 {
1220 const int* intptr = (const int*)bottom_blob + i * 4;
1221 unsigned short* ptr = (unsigned short*)top_blob + i * 4;
1222
1223 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
1224 _v = vmulq_f32(_v, _scale);
1225 vst1_u16(ptr, float2bfloat(_v));
1226 }
1227 }
1228 else if (bias_data_size == 1)
1229 {
1230 float32x4_t _bias = vdupq_n_f32(bias_data[0]);
1231
1232 #pragma omp parallel for num_threads(opt.num_threads)
1233 for (int i = 0; i < w; i++)
1234 {
1235 const int* intptr = (const int*)bottom_blob + i * 4;
1236 unsigned short* ptr = (unsigned short*)top_blob + i * 4;
1237
1238 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
1239#if __aarch64__
1240 _v = vfmaq_f32(_bias, _v, _scale);
1241#else
1242 _v = vmlaq_f32(_bias, _v, _scale);
1243#endif
1244 vst1_u16(ptr, float2bfloat(_v));
1245 }
1246 }
1247 else
1248 {
1249 #pragma omp parallel for num_threads(opt.num_threads)
1250 for (int i = 0; i < w; i++)
1251 {
1252 const int* intptr = (const int*)bottom_blob + i * 4;
1253 unsigned short* ptr = (unsigned short*)top_blob + i * 4;
1254
1255 float32x4_t _bias = vld1q_f32((const float*)bias_data + i * 4);
1256 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
1257#if __aarch64__
1258 _v = vfmaq_f32(_bias, _v, _scale);
1259#else
1260 _v = vmlaq_f32(_bias, _v, _scale);
1261#endif
1262 vst1_u16(ptr, float2bfloat(_v));
1263 }
1264 }
1265 }
1266 else
1267 {
1268 if (bias_data_size == 0)
1269 {
1270 #pragma omp parallel for num_threads(opt.num_threads)
1271 for (int i = 0; i < w; i++)
1272 {
1273 const int* intptr = (const int*)bottom_blob + i * 4;
1274 unsigned short* ptr = (unsigned short*)top_blob + i * 4;
1275
1276 float32x4_t _scale = vld1q_f32((const float*)scale_data + i * 4);
1277 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
1278 _v = vmulq_f32(_v, _scale);
1279 vst1_u16(ptr, float2bfloat(_v));
1280 }
1281 }
1282 else if (bias_data_size == 1)
1283 {
1284 float32x4_t _bias = vdupq_n_f32(bias_data[0]);
1285
1286 #pragma omp parallel for num_threads(opt.num_threads)
1287 for (int i = 0; i < w; i++)
1288 {
1289 const int* intptr = (const int*)bottom_blob + i * 4;
1290 unsigned short* ptr = (unsigned short*)top_blob + i * 4;
1291
1292 float32x4_t _scale = vld1q_f32((const float*)scale_data + i * 4);
1293 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
1294#if __aarch64__
1295 _v = vfmaq_f32(_bias, _v, _scale);
1296#else
1297 _v = vmlaq_f32(_bias, _v, _scale);
1298#endif
1299 vst1_u16(ptr, float2bfloat(_v));
1300 }
1301 }
1302 else
1303 {
1304 #pragma omp parallel for num_threads(opt.num_threads)
1305 for (int i = 0; i < w; i++)
1306 {
1307 const int* intptr = (const int*)bottom_blob + i * 4;
1308 unsigned short* ptr = (unsigned short*)top_blob + i * 4;
1309
1310 float32x4_t _scale = vld1q_f32((const float*)scale_data + i * 4);
1311 float32x4_t _bias = vld1q_f32((const float*)bias_data + i * 4);
1312 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
1313#if __aarch64__
1314 _v = vfmaq_f32(_bias, _v, _scale);
1315#else
1316 _v = vmlaq_f32(_bias, _v, _scale);
1317#endif
1318 vst1_u16(ptr, float2bfloat(_v));
1319 }
1320 }
1321 }
1322 }
1323
1324 if (dims == 2)
1325 {
1326 int w = bottom_blob.w;
1327 int h = bottom_blob.h;
1328
1329 top_blob.create(w, h, (size_t)8u, elempack, opt.blob_allocator);
1330 if (top_blob.empty())
1331 return -100;
1332
1333 if (bias_data_size == 0)
1334 {
1335 #pragma omp parallel for num_threads(opt.num_threads)
1336 for (int i = 0; i < h; i++)
1337 {
1338 const int* intptr = bottom_blob.row<const int>(i);
1339 unsigned short* ptr = top_blob.row<unsigned short>(i);
1340
1341 float32x4_t _scale = scale_data_size == 1 ? vdupq_n_f32(scale_data[0]) : vld1q_f32((const float*)scale_data + i * 4);
1342
1343 for (int j = 0; j < w; j++)
1344 {
1345 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
1346 _v = vmulq_f32(_v, _scale);
1347 vst1_u16(ptr, float2bfloat(_v));
1348
1349 intptr += 4;
1350 ptr += 4;
1351 }
1352 }
1353 }
1354 else
1355 {
1356 #pragma omp parallel for num_threads(opt.num_threads)
1357 for (int i = 0; i < h; i++)
1358 {
1359 const int* intptr = bottom_blob.row<const int>(i);
1360 unsigned short* ptr = top_blob.row<unsigned short>(i);
1361
1362 float32x4_t _scale = scale_data_size == 1 ? vdupq_n_f32(scale_data[0]) : vld1q_f32((const float*)scale_data + i * 4);
1363 float32x4_t _bias = bias_data_size == 1 ? vdupq_n_f32(bias_data[0]) : vld1q_f32((const float*)bias_data + i * 4);
1364
1365 for (int j = 0; j < w; j++)
1366 {
1367 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
1368#if __aarch64__
1369 _v = vfmaq_f32(_bias, _v, _scale);
1370#else
1371 _v = vmlaq_f32(_bias, _v, _scale);
1372#endif
1373 vst1_u16(ptr, float2bfloat(_v));
1374
1375 intptr += 4;
1376 ptr += 4;
1377 }
1378 }
1379 }
1380 }
1381
1382 if (dims == 3)
1383 {
1384 int w = bottom_blob.w;
1385 int h = bottom_blob.h;
1386 int channels = bottom_blob.c;
1387 int size = w * h;
1388
1389 top_blob.create(w, h, channels, (size_t)8u, elempack, opt.blob_allocator);
1390 if (top_blob.empty())
1391 return -100;
1392
1393 if (bias_data_size == 0)
1394 {
1395 #pragma omp parallel for num_threads(opt.num_threads)
1396 for (int q = 0; q < channels; q++)
1397 {
1398 const int* intptr = bottom_blob.channel(q);
1399 unsigned short* ptr = top_blob.channel(q);
1400
1401 float32x4_t _scale = scale_data_size == 1 ? vdupq_n_f32(scale_data[0]) : vld1q_f32((const float*)scale_data + q * 4);
1402
1403 for (int i = 0; i < size; i++)
1404 {
1405 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
1406 _v = vmulq_f32(_v, _scale);
1407 vst1_u16(ptr, float2bfloat(_v));
1408
1409 intptr += 4;
1410 ptr += 4;
1411 }
1412 }
1413 }
1414 else
1415 {
1416 #pragma omp parallel for num_threads(opt.num_threads)
1417 for (int q = 0; q < channels; q++)
1418 {
1419 const int* intptr = bottom_blob.channel(q);
1420 unsigned short* ptr = top_blob.channel(q);
1421
1422 float32x4_t _scale = scale_data_size == 1 ? vdupq_n_f32(scale_data[0]) : vld1q_f32((const float*)scale_data + q * 4);
1423 float32x4_t _bias = bias_data_size == 1 ? vdupq_n_f32(bias_data[0]) : vld1q_f32((const float*)bias_data + q * 4);
1424
1425 for (int i = 0; i < size; i++)
1426 {
1427 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
1428#if __aarch64__
1429 _v = vfmaq_f32(_bias, _v, _scale);
1430#else
1431 _v = vmlaq_f32(_bias, _v, _scale);
1432#endif
1433 vst1_u16(ptr, float2bfloat(_v));
1434
1435 intptr += 4;
1436 ptr += 4;
1437 }
1438 }
1439 }
1440 }
1441
1442 return 0;
1443 }
1444#endif // __ARM_NEON
1445
1446 if (dims == 1)
1447 {
1448 int w = bottom_blob.w;
1449
1450 top_blob.create(w, (size_t)2u, opt.blob_allocator);
1451 if (top_blob.empty())
1452 return -100;
1453
1454 const int* intptr = bottom_blob;
1455 unsigned short* ptr = top_blob;
1456
1457 if (scale_data_size == 1)
1458 {
1459 const float scale = scale_data[0];
1460
1461 if (bias_data_size == 0)
1462 {
1463 #pragma omp parallel for num_threads(opt.num_threads)
1464 for (int i = 0; i < w; i++)
1465 {
1466 ptr[i] = float32_to_bfloat16(intptr[i] * scale);
1467 }
1468 }
1469 else if (bias_data_size == 1)
1470 {
1471 const float bias = bias_data[0];
1472
1473 #pragma omp parallel for num_threads(opt.num_threads)
1474 for (int i = 0; i < w; i++)
1475 {
1476 ptr[i] = float32_to_bfloat16(intptr[i] * scale + bias);
1477 }
1478 }
1479 else
1480 {
1481 #pragma omp parallel for num_threads(opt.num_threads)
1482 for (int i = 0; i < w; i++)
1483 {
1484 ptr[i] = float32_to_bfloat16(intptr[i] * scale + bias_data[i]);
1485 }
1486 }
1487 }
1488 else
1489 {
1490 if (bias_data_size == 0)
1491 {
1492 #pragma omp parallel for num_threads(opt.num_threads)
1493 for (int i = 0; i < w; i++)
1494 {
1495 ptr[i] = float32_to_bfloat16(intptr[i] * scale_data[i]);
1496 }
1497 }
1498 else if (bias_data_size == 1)
1499 {
1500 const float bias = bias_data[0];
1501
1502 #pragma omp parallel for num_threads(opt.num_threads)
1503 for (int i = 0; i < w; i++)
1504 {
1505 ptr[i] = float32_to_bfloat16(intptr[i] * scale_data[i] + bias);
1506 }
1507 }
1508 else
1509 {
1510 #pragma omp parallel for num_threads(opt.num_threads)
1511 for (int i = 0; i < w; i++)
1512 {
1513 ptr[i] = float32_to_bfloat16(intptr[i] * scale_data[i] + bias_data[i]);
1514 }
1515 }
1516 }
1517 }
1518
1519 if (dims == 2)
1520 {
1521 int w = bottom_blob.w;
1522 int h = bottom_blob.h;
1523
1524 top_blob.create(w, h, (size_t)2u, opt.blob_allocator);
1525 if (top_blob.empty())
1526 return -100;
1527
1528 if (bias_data_size == 0)
1529 {
1530 #pragma omp parallel for num_threads(opt.num_threads)
1531 for (int i = 0; i < h; i++)
1532 {
1533 const int* intptr = bottom_blob.row<const int>(i);
1534 unsigned short* ptr = top_blob.row<unsigned short>(i);
1535
1536 const float scale = scale_data_size == 1 ? scale_data[0] : scale_data[i];
1537
1538 int j = 0;
1539#if __ARM_NEON
1540 float32x4_t _scale = vdupq_n_f32(scale);
1541 for (; j + 3 < w; j += 4)
1542 {
1543 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
1544 _v = vmulq_f32(_v, _scale);
1545 vst1_u16(ptr, float2bfloat(_v));
1546
1547 intptr += 4;
1548 ptr += 4;
1549 }
1550#endif // __ARM_NEON
1551 for (; j < w; j++)
1552 {
1553 *ptr++ = float32_to_bfloat16(*intptr++ * scale);
1554 }
1555 }
1556 }
1557 else
1558 {
1559 #pragma omp parallel for num_threads(opt.num_threads)
1560 for (int i = 0; i < h; i++)
1561 {
1562 const int* intptr = bottom_blob.row<const int>(i);
1563 unsigned short* ptr = top_blob.row<unsigned short>(i);
1564
1565 const float scale = scale_data_size == 1 ? scale_data[0] : scale_data[i];
1566 const float bias = bias_data_size == 1 ? bias_data[0] : bias_data[i];
1567
1568 int j = 0;
1569#if __ARM_NEON
1570 float32x4_t _scale = vdupq_n_f32(scale);
1571 float32x4_t _bias = vdupq_n_f32(bias);
1572 for (; j + 3 < w; j += 4)
1573 {
1574 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
1575#if __aarch64__
1576 _v = vfmaq_f32(_bias, _v, _scale);
1577#else
1578 _v = vmlaq_f32(_bias, _v, _scale);
1579#endif
1580 vst1_u16(ptr, float2bfloat(_v));
1581
1582 intptr += 4;
1583 ptr += 4;
1584 }
1585#endif // __ARM_NEON
1586 for (; j < w; j++)
1587 {
1588 *ptr++ = float32_to_bfloat16(*intptr++ * scale + bias);
1589 }
1590 }
1591 }
1592 }
1593
1594 if (dims == 3)
1595 {
1596 int w = bottom_blob.w;
1597 int h = bottom_blob.h;
1598 int channels = bottom_blob.c;
1599 int size = w * h;
1600
1601 top_blob.create(w, h, channels, (size_t)2u, opt.blob_allocator);
1602 if (top_blob.empty())
1603 return -100;
1604
1605 if (bias_data_size == 0)
1606 {
1607 #pragma omp parallel for num_threads(opt.num_threads)
1608 for (int q = 0; q < channels; q++)
1609 {
1610 const int* intptr = bottom_blob.channel(q);
1611 unsigned short* ptr = top_blob.channel(q);
1612
1613 const float scale = scale_data_size == 1 ? scale_data[0] : scale_data[q];
1614
1615 int i = 0;
1616#if __ARM_NEON
1617 float32x4_t _scale = vdupq_n_f32(scale);
1618 for (; i + 3 < size; i += 4)
1619 {
1620 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
1621 _v = vmulq_f32(_v, _scale);
1622 vst1_u16(ptr, float2bfloat(_v));
1623
1624 intptr += 4;
1625 ptr += 4;
1626 }
1627#endif // __ARM_NEON
1628 for (; i < size; i++)
1629 {
1630 *ptr++ = float32_to_bfloat16(*intptr++ * scale);
1631 }
1632 }
1633 }
1634 else
1635 {
1636 #pragma omp parallel for num_threads(opt.num_threads)
1637 for (int q = 0; q < channels; q++)
1638 {
1639 const int* intptr = bottom_blob.channel(q);
1640 unsigned short* ptr = top_blob.channel(q);
1641
1642 const float scale = scale_data_size == 1 ? scale_data[0] : scale_data[q];
1643 const float bias = bias_data_size == 1 ? bias_data[0] : bias_data[q];
1644
1645 int i = 0;
1646#if __ARM_NEON
1647 float32x4_t _scale = vdupq_n_f32(scale);
1648 float32x4_t _bias = vdupq_n_f32(bias);
1649 for (; i + 3 < size; i += 4)
1650 {
1651 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
1652#if __aarch64__
1653 _v = vfmaq_f32(_bias, _v, _scale);
1654#else
1655 _v = vmlaq_f32(_bias, _v, _scale);
1656#endif
1657 vst1_u16(ptr, float2bfloat(_v));
1658
1659 intptr += 4;
1660 ptr += 4;
1661 }
1662#endif // __ARM_NEON
1663 for (; i < size; i++)
1664 {
1665 *ptr++ = float32_to_bfloat16(*intptr++ * scale + bias);
1666 }
1667 }
1668 }
1669 }
1670
1671 return 0;
1672}
1673#endif // NCNN_BF16
1674
1675} // namespace ncnn