Elffuss BraidHub

kikocis/ncnn public

master · 34.9 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 "quantize_arm.h"
17
18#if __ARM_NEON
19#include <arm_neon.h>
20#endif // __ARM_NEON
21
22#include "arm_usability.h"
23
24#include "cpu.h"
25
26namespace ncnn {
27
28Quantize_arm::Quantize_arm()
29{
30#if __ARM_NEON
31 support_packing = true;
32#if NCNN_ARM82
33 support_fp16_storage = cpu_support_arm_asimdhp();
34#endif
35#endif // __ARM_NEON
36
37#if NCNN_BF16
38 support_bf16_storage = true;
39#endif
40}
41
42int Quantize_arm::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
43{
44 int elembits = bottom_blob.elembits();
45
46#if NCNN_ARM82
47 if (support_fp16_storage && opt.use_fp16_storage && elembits == 16)
48 {
49 if (opt.use_fp16_arithmetic)
50 return forward_fp16sa(bottom_blob, top_blob, opt);
51 else
52 return forward_fp16s(bottom_blob, top_blob, opt);
53 }
54#endif
55
56#if NCNN_BF16
57 if (opt.use_bf16_storage && elembits == 16)
58 return forward_bf16s(bottom_blob, top_blob, opt);
59#endif
60
61 int dims = bottom_blob.dims;
62 int elempack = bottom_blob.elempack;
63
64#if __ARM_NEON
65 if (elempack == 4)
66 {
67 if (dims == 1)
68 {
69 int w = bottom_blob.w;
70 int out_elempack = opt.use_packing_layout && w * elempack % 8 == 0 ? 8 : 1;
71 int outw = w * elempack / out_elempack;
72
73 top_blob.create(outw, (size_t)out_elempack, out_elempack, opt.blob_allocator);
74 if (top_blob.empty())
75 return -100;
76
77 if (scale_data_size == 1)
78 {
79 const float scale = scale_data[0];
80
81 #pragma omp parallel for num_threads(opt.num_threads)
82 for (int i = 0; i < w; i++)
83 {
84 const float* ptr0 = (const float*)bottom_blob + i * 4;
85 signed char* outptr = (signed char*)top_blob + i * 4;
86
87 outptr[0] = float2int8(ptr0[0] * scale);
88 outptr[1] = float2int8(ptr0[1] * scale);
89 outptr[2] = float2int8(ptr0[2] * scale);
90 outptr[3] = float2int8(ptr0[3] * scale);
91 }
92 }
93 else
94 {
95 #pragma omp parallel for num_threads(opt.num_threads)
96 for (int i = 0; i < w; i++)
97 {
98 const float* ptr0 = (const float*)bottom_blob + i * 4;
99 signed char* outptr = (signed char*)top_blob + i * 4;
100
101 outptr[0] = float2int8(ptr0[0] * scale_data[i * 4]);
102 outptr[1] = float2int8(ptr0[1] * scale_data[i * 4 + 1]);
103 outptr[2] = float2int8(ptr0[2] * scale_data[i * 4 + 2]);
104 outptr[3] = float2int8(ptr0[3] * scale_data[i * 4 + 3]);
105 }
106 }
107 }
108
109 if (dims == 2)
110 {
111 int w = bottom_blob.w;
112 int h = bottom_blob.h;
113 int out_elempack = opt.use_packing_layout && h * elempack % 8 == 0 ? 8 : 1;
114 int outh = h * elempack / out_elempack;
115
116 top_blob.create(w, outh, (size_t)out_elempack, out_elempack, opt.blob_allocator);
117 if (top_blob.empty())
118 return -100;
119
120 if (out_elempack == 8)
121 {
122 if (scale_data_size == 1)
123 {
124 float32x4_t _scale = vdupq_n_f32(scale_data[0]);
125
126 #pragma omp parallel for num_threads(opt.num_threads)
127 for (int i = 0; i < outh; i++)
128 {
129 const float* ptr0 = bottom_blob.row(i * 2);
130 const float* ptr1 = bottom_blob.row(i * 2 + 1);
131 signed char* outptr = top_blob.row<signed char>(i);
132
133 for (int j = 0; j < w; j++)
134 {
135 float32x4_t _vlow = vld1q_f32(ptr0);
136 float32x4_t _vhigh = vld1q_f32(ptr1);
137 _vlow = vmulq_f32(_vlow, _scale);
138 _vhigh = vmulq_f32(_vhigh, _scale);
139 int8x8_t _v = float2int8(_vlow, _vhigh);
140 vst1_s8(outptr, _v);
141
142 ptr0 += 4;
143 ptr1 += 4;
144 outptr += 8;
145 }
146 }
147 }
148 else
149 {
150 #pragma omp parallel for num_threads(opt.num_threads)
151 for (int i = 0; i < outh; i++)
152 {
153 const float* ptr0 = bottom_blob.row(i * 2);
154 const float* ptr1 = bottom_blob.row(i * 2 + 1);
155 signed char* outptr = top_blob.row<signed char>(i);
156
157 float32x4_t _scale0 = vld1q_f32((const float*)scale_data + i * 8);
158 float32x4_t _scale1 = vld1q_f32((const float*)scale_data + i * 8 + 4);
159
160 for (int j = 0; j < w; j++)
161 {
162 float32x4_t _vlow = vld1q_f32(ptr0);
163 float32x4_t _vhigh = vld1q_f32(ptr1);
164 _vlow = vmulq_f32(_vlow, _scale0);
165 _vhigh = vmulq_f32(_vhigh, _scale1);
166 int8x8_t _v = float2int8(_vlow, _vhigh);
167 vst1_s8(outptr, _v);
168
169 ptr0 += 4;
170 ptr1 += 4;
171 outptr += 8;
172 }
173 }
174 }
175 }
176 if (out_elempack == 1)
177 {
178 if (scale_data_size == 1)
179 {
180 const float scale = scale_data[0];
181
182 #pragma omp parallel for num_threads(opt.num_threads)
183 for (int i = 0; i < h; i++)
184 {
185 const float* ptr0 = bottom_blob.row(i);
186 signed char* outptr0 = top_blob.row<signed char>(i * 4);
187 signed char* outptr1 = top_blob.row<signed char>(i * 4 + 1);
188 signed char* outptr2 = top_blob.row<signed char>(i * 4 + 2);
189 signed char* outptr3 = top_blob.row<signed char>(i * 4 + 3);
190
191 for (int j = 0; j < w; j++)
192 {
193 outptr0[0] = float2int8(ptr0[0] * scale);
194 outptr1[0] = float2int8(ptr0[1] * scale);
195 outptr2[0] = float2int8(ptr0[2] * scale);
196 outptr3[0] = float2int8(ptr0[3] * scale);
197
198 ptr0 += 4;
199 outptr0 += 1;
200 outptr1 += 1;
201 outptr2 += 1;
202 outptr3 += 1;
203 }
204 }
205 }
206 else
207 {
208 #pragma omp parallel for num_threads(opt.num_threads)
209 for (int i = 0; i < h; i++)
210 {
211 const float* ptr0 = bottom_blob.row(i);
212 signed char* outptr0 = top_blob.row<signed char>(i * 4);
213 signed char* outptr1 = top_blob.row<signed char>(i * 4 + 1);
214 signed char* outptr2 = top_blob.row<signed char>(i * 4 + 2);
215 signed char* outptr3 = top_blob.row<signed char>(i * 4 + 3);
216
217 const float s0 = scale_data[i * 4];
218 const float s1 = scale_data[i * 4 + 1];
219 const float s2 = scale_data[i * 4 + 2];
220 const float s3 = scale_data[i * 4 + 3];
221
222 for (int j = 0; j < w; j++)
223 {
224 outptr0[0] = float2int8(ptr0[0] * s0);
225 outptr1[0] = float2int8(ptr0[1] * s1);
226 outptr2[0] = float2int8(ptr0[2] * s2);
227 outptr3[0] = float2int8(ptr0[3] * s3);
228
229 ptr0 += 4;
230 outptr0 += 1;
231 outptr1 += 1;
232 outptr2 += 1;
233 outptr3 += 1;
234 }
235 }
236 }
237 }
238 }
239
240 if (dims == 3)
241 {
242 int w = bottom_blob.w;
243 int h = bottom_blob.h;
244 int channels = bottom_blob.c;
245 int size = w * h;
246 int out_elempack = opt.use_packing_layout && channels * elempack % 8 == 0 ? 8 : 1;
247 int outc = channels * elempack / out_elempack;
248
249 top_blob.create(w, h, outc, (size_t)out_elempack, out_elempack, opt.blob_allocator);
250 if (top_blob.empty())
251 return -100;
252
253 if (out_elempack == 8)
254 {
255 if (scale_data_size == 1)
256 {
257 float32x4_t _scale = vdupq_n_f32(scale_data[0]);
258
259 #pragma omp parallel for num_threads(opt.num_threads)
260 for (int q = 0; q < outc; q++)
261 {
262 const float* ptr0 = bottom_blob.channel(q * 2);
263 const float* ptr1 = bottom_blob.channel(q * 2 + 1);
264 signed char* outptr = top_blob.channel(q);
265
266 int i = 0;
267 for (; i + 1 < size; i += 2)
268 {
269 float32x4_t _v0 = vld1q_f32(ptr0);
270 float32x4_t _v1 = vld1q_f32(ptr0 + 4);
271 float32x4_t _v2 = vld1q_f32(ptr1);
272 float32x4_t _v3 = vld1q_f32(ptr1 + 4);
273 _v0 = vmulq_f32(_v0, _scale);
274 _v1 = vmulq_f32(_v1, _scale);
275 _v2 = vmulq_f32(_v2, _scale);
276 _v3 = vmulq_f32(_v3, _scale);
277 vst1_s8(outptr, float2int8(_v0, _v2));
278 vst1_s8(outptr + 8, float2int8(_v1, _v3));
279
280 ptr0 += 8;
281 ptr1 += 8;
282 outptr += 16;
283 }
284 for (; i < size; i++)
285 {
286 float32x4_t _vlow = vld1q_f32(ptr0);
287 float32x4_t _vhigh = vld1q_f32(ptr1);
288 _vlow = vmulq_f32(_vlow, _scale);
289 _vhigh = vmulq_f32(_vhigh, _scale);
290 int8x8_t _v = float2int8(_vlow, _vhigh);
291 vst1_s8(outptr, _v);
292
293 ptr0 += 4;
294 ptr1 += 4;
295 outptr += 8;
296 }
297 }
298 }
299 else
300 {
301 #pragma omp parallel for num_threads(opt.num_threads)
302 for (int q = 0; q < outc; q++)
303 {
304 const float* ptr0 = bottom_blob.channel(q * 2);
305 const float* ptr1 = bottom_blob.channel(q * 2 + 1);
306 signed char* outptr = top_blob.channel(q);
307
308 float32x4_t _scale0 = vld1q_f32((const float*)scale_data + q * 8);
309 float32x4_t _scale1 = vld1q_f32((const float*)scale_data + q * 8 + 4);
310
311 int i = 0;
312 for (; i < size; i++)
313 {
314 float32x4_t _vlow = vld1q_f32(ptr0);
315 float32x4_t _vhigh = vld1q_f32(ptr1);
316 _vlow = vmulq_f32(_vlow, _scale0);
317 _vhigh = vmulq_f32(_vhigh, _scale1);
318 int8x8_t _v = float2int8(_vlow, _vhigh);
319 vst1_s8(outptr, _v);
320
321 ptr0 += 4;
322 ptr1 += 4;
323 outptr += 8;
324 }
325 }
326 }
327 }
328 if (out_elempack == 1)
329 {
330 if (scale_data_size == 1)
331 {
332 const float scale = scale_data[0];
333
334 #pragma omp parallel for num_threads(opt.num_threads)
335 for (int q = 0; q < channels; q++)
336 {
337 const float* ptr0 = bottom_blob.channel(q);
338 signed char* outptr0 = top_blob.channel(q * 4);
339 signed char* outptr1 = top_blob.channel(q * 4 + 1);
340 signed char* outptr2 = top_blob.channel(q * 4 + 2);
341 signed char* outptr3 = top_blob.channel(q * 4 + 3);
342
343 for (int i = 0; i < size; i++)
344 {
345 outptr0[0] = float2int8(ptr0[0] * scale);
346 outptr1[0] = float2int8(ptr0[1] * scale);
347 outptr2[0] = float2int8(ptr0[2] * scale);
348 outptr3[0] = float2int8(ptr0[3] * scale);
349
350 ptr0 += 4;
351 outptr0 += 1;
352 outptr1 += 1;
353 outptr2 += 1;
354 outptr3 += 1;
355 }
356 }
357 }
358 else
359 {
360 #pragma omp parallel for num_threads(opt.num_threads)
361 for (int q = 0; q < channels; q++)
362 {
363 const float* ptr0 = bottom_blob.channel(q);
364 signed char* outptr0 = top_blob.channel(q * 4);
365 signed char* outptr1 = top_blob.channel(q * 4 + 1);
366 signed char* outptr2 = top_blob.channel(q * 4 + 2);
367 signed char* outptr3 = top_blob.channel(q * 4 + 3);
368
369 const float s0 = scale_data[q * 4];
370 const float s1 = scale_data[q * 4 + 1];
371 const float s2 = scale_data[q * 4 + 2];
372 const float s3 = scale_data[q * 4 + 3];
373
374 for (int i = 0; i < size; i++)
375 {
376 outptr0[0] = float2int8(ptr0[0] * s0);
377 outptr1[0] = float2int8(ptr0[1] * s1);
378 outptr2[0] = float2int8(ptr0[2] * s2);
379 outptr3[0] = float2int8(ptr0[3] * s3);
380
381 ptr0 += 4;
382 outptr0 += 1;
383 outptr1 += 1;
384 outptr2 += 1;
385 outptr3 += 1;
386 }
387 }
388 }
389 }
390 }
391
392 return 0;
393 }
394#endif // __ARM_NEON
395
396 if (dims == 1)
397 {
398 int w = bottom_blob.w;
399
400 top_blob.create(w, (size_t)1u, opt.blob_allocator);
401 if (top_blob.empty())
402 return -100;
403
404 const float* ptr = bottom_blob;
405 signed char* outptr = top_blob;
406
407 if (scale_data_size == 1)
408 {
409 const float scale = scale_data[0];
410
411 #pragma omp parallel for num_threads(opt.num_threads)
412 for (int i = 0; i < w; i++)
413 {
414 outptr[i] = float2int8(ptr[i] * scale);
415 }
416 }
417 else
418 {
419 #pragma omp parallel for num_threads(opt.num_threads)
420 for (int i = 0; i < w; i++)
421 {
422 outptr[i] = float2int8(ptr[i] * scale_data[i]);
423 }
424 }
425 }
426
427 if (dims == 2)
428 {
429 int w = bottom_blob.w;
430 int h = bottom_blob.h;
431
432 top_blob.create(w, h, (size_t)1u, opt.blob_allocator);
433 if (top_blob.empty())
434 return -100;
435
436 #pragma omp parallel for num_threads(opt.num_threads)
437 for (int i = 0; i < h; i++)
438 {
439 const float* ptr0 = bottom_blob.row(i);
440 signed char* outptr0 = top_blob.row<signed char>(i);
441
442 const float scale = scale_data_size == 1 ? scale_data[0] : scale_data[i];
443
444 for (int j = 0; j < w; j++)
445 {
446 *outptr0++ = float2int8(*ptr0++ * scale);
447 }
448 }
449 }
450
451 if (dims == 3)
452 {
453 int w = bottom_blob.w;
454 int h = bottom_blob.h;
455 int channels = bottom_blob.c;
456 int size = w * h;
457
458 top_blob.create(w, h, channels, (size_t)1u, opt.blob_allocator);
459 if (top_blob.empty())
460 return -100;
461
462 #pragma omp parallel for num_threads(opt.num_threads)
463 for (int q = 0; q < channels; q++)
464 {
465 const float* ptr = bottom_blob.channel(q);
466 signed char* outptr = top_blob.channel(q);
467
468 const float scale = scale_data_size == 1 ? scale_data[0] : scale_data[q];
469
470 int i = 0;
471#if __ARM_NEON
472 float32x4_t _scale = vdupq_n_f32(scale);
473 for (; i + 15 < size; i += 16)
474 {
475 float32x4_t _v0 = vld1q_f32(ptr);
476 float32x4_t _v1 = vld1q_f32(ptr + 4);
477 float32x4_t _v2 = vld1q_f32(ptr + 8);
478 float32x4_t _v3 = vld1q_f32(ptr + 12);
479 _v0 = vmulq_f32(_v0, _scale);
480 _v1 = vmulq_f32(_v1, _scale);
481 _v2 = vmulq_f32(_v2, _scale);
482 _v3 = vmulq_f32(_v3, _scale);
483 vst1_s8(outptr, float2int8(_v0, _v1));
484 vst1_s8(outptr + 8, float2int8(_v2, _v3));
485
486 ptr += 16;
487 outptr += 16;
488 }
489 for (; i + 7 < size; i += 8)
490 {
491 float32x4_t _v0 = vld1q_f32(ptr);
492 float32x4_t _v1 = vld1q_f32(ptr + 4);
493 _v0 = vmulq_f32(_v0, _scale);
494 _v1 = vmulq_f32(_v1, _scale);
495 int8x8_t _v = float2int8(_v0, _v1);
496 vst1_s8(outptr, _v);
497
498 ptr += 8;
499 outptr += 8;
500 }
501#endif // __ARM_NEON
502 for (; i < size; i++)
503 {
504 *outptr++ = float2int8(*ptr++ * scale);
505 }
506 }
507 }
508
509 return 0;
510}
511
512#if NCNN_BF16
513int Quantize_arm::forward_bf16s(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
514{
515 int dims = bottom_blob.dims;
516 int elempack = bottom_blob.elempack;
517
518#if __ARM_NEON
519 if (elempack == 4)
520 {
521 if (dims == 1)
522 {
523 int w = bottom_blob.w;
524 int out_elempack = opt.use_packing_layout && w * elempack % 8 == 0 ? 8 : 1;
525 int outw = w * elempack / out_elempack;
526
527 top_blob.create(outw, (size_t)out_elempack, out_elempack, opt.blob_allocator);
528 if (top_blob.empty())
529 return -100;
530
531 if (scale_data_size == 1)
532 {
533 const float scale = scale_data[0];
534
535 #pragma omp parallel for num_threads(opt.num_threads)
536 for (int i = 0; i < w; i++)
537 {
538 const unsigned short* ptr0 = (const unsigned short*)bottom_blob + i * 4;
539 signed char* outptr = (signed char*)top_blob + i * 4;
540
541 outptr[0] = float2int8(bfloat16_to_float32(ptr0[0]) * scale);
542 outptr[1] = float2int8(bfloat16_to_float32(ptr0[1]) * scale);
543 outptr[2] = float2int8(bfloat16_to_float32(ptr0[2]) * scale);
544 outptr[3] = float2int8(bfloat16_to_float32(ptr0[3]) * scale);
545 }
546 }
547 else
548 {
549 #pragma omp parallel for num_threads(opt.num_threads)
550 for (int i = 0; i < w; i++)
551 {
552 const unsigned short* ptr0 = (const unsigned short*)bottom_blob + i * 4;
553 signed char* outptr = (signed char*)top_blob + i * 4;
554
555 outptr[0] = float2int8(bfloat16_to_float32(ptr0[0]) * scale_data[i * 4]);
556 outptr[1] = float2int8(bfloat16_to_float32(ptr0[1]) * scale_data[i * 4 + 1]);
557 outptr[2] = float2int8(bfloat16_to_float32(ptr0[2]) * scale_data[i * 4 + 2]);
558 outptr[3] = float2int8(bfloat16_to_float32(ptr0[3]) * scale_data[i * 4 + 3]);
559 }
560 }
561 }
562
563 if (dims == 2)
564 {
565 int w = bottom_blob.w;
566 int h = bottom_blob.h;
567 int out_elempack = opt.use_packing_layout && h * elempack % 8 == 0 ? 8 : 1;
568 int outh = h * elempack / out_elempack;
569
570 top_blob.create(w, outh, (size_t)out_elempack, out_elempack, opt.blob_allocator);
571 if (top_blob.empty())
572 return -100;
573
574 if (out_elempack == 8)
575 {
576 if (scale_data_size == 1)
577 {
578 float32x4_t _scale = vdupq_n_f32(scale_data[0]);
579
580 #pragma omp parallel for num_threads(opt.num_threads)
581 for (int i = 0; i < outh; i++)
582 {
583 const unsigned short* ptr0 = bottom_blob.row<const unsigned short>(i * 2);
584 const unsigned short* ptr1 = bottom_blob.row<const unsigned short>(i * 2 + 1);
585 signed char* outptr = top_blob.row<signed char>(i);
586
587 for (int j = 0; j < w; j++)
588 {
589 float32x4_t _vlow = bfloat2float(vld1_u16(ptr0));
590 float32x4_t _vhigh = bfloat2float(vld1_u16(ptr1));
591 _vlow = vmulq_f32(_vlow, _scale);
592 _vhigh = vmulq_f32(_vhigh, _scale);
593 int8x8_t _v = float2int8(_vlow, _vhigh);
594 vst1_s8(outptr, _v);
595
596 ptr0 += 4;
597 ptr1 += 4;
598 outptr += 8;
599 }
600 }
601 }
602 else
603 {
604 #pragma omp parallel for num_threads(opt.num_threads)
605 for (int i = 0; i < outh; i++)
606 {
607 const unsigned short* ptr0 = bottom_blob.row<const unsigned short>(i * 2);
608 const unsigned short* ptr1 = bottom_blob.row<const unsigned short>(i * 2 + 1);
609 signed char* outptr = top_blob.row<signed char>(i);
610
611 float32x4_t _scale0 = vld1q_f32((const float*)scale_data + i * 8);
612 float32x4_t _scale1 = vld1q_f32((const float*)scale_data + i * 8 + 4);
613
614 for (int j = 0; j < w; j++)
615 {
616 float32x4_t _vlow = bfloat2float(vld1_u16(ptr0));
617 float32x4_t _vhigh = bfloat2float(vld1_u16(ptr1));
618 _vlow = vmulq_f32(_vlow, _scale0);
619 _vhigh = vmulq_f32(_vhigh, _scale1);
620 int8x8_t _v = float2int8(_vlow, _vhigh);
621 vst1_s8(outptr, _v);
622
623 ptr0 += 4;
624 ptr1 += 4;
625 outptr += 8;
626 }
627 }
628 }
629 }
630 if (out_elempack == 1)
631 {
632 if (scale_data_size == 1)
633 {
634 const float scale = scale_data[0];
635
636 #pragma omp parallel for num_threads(opt.num_threads)
637 for (int i = 0; i < h; i++)
638 {
639 const unsigned short* ptr0 = bottom_blob.row<const unsigned short>(i);
640 signed char* outptr0 = top_blob.row<signed char>(i * 4);
641 signed char* outptr1 = top_blob.row<signed char>(i * 4 + 1);
642 signed char* outptr2 = top_blob.row<signed char>(i * 4 + 2);
643 signed char* outptr3 = top_blob.row<signed char>(i * 4 + 3);
644
645 for (int j = 0; j < w; j++)
646 {
647 outptr0[0] = float2int8(bfloat16_to_float32(ptr0[0]) * scale);
648 outptr1[0] = float2int8(bfloat16_to_float32(ptr0[1]) * scale);
649 outptr2[0] = float2int8(bfloat16_to_float32(ptr0[2]) * scale);
650 outptr3[0] = float2int8(bfloat16_to_float32(ptr0[3]) * scale);
651
652 ptr0 += 4;
653 outptr0 += 1;
654 outptr1 += 1;
655 outptr2 += 1;
656 outptr3 += 1;
657 }
658 }
659 }
660 else
661 {
662 #pragma omp parallel for num_threads(opt.num_threads)
663 for (int i = 0; i < h; i++)
664 {
665 const unsigned short* ptr0 = bottom_blob.row<const unsigned short>(i);
666 signed char* outptr0 = top_blob.row<signed char>(i * 4);
667 signed char* outptr1 = top_blob.row<signed char>(i * 4 + 1);
668 signed char* outptr2 = top_blob.row<signed char>(i * 4 + 2);
669 signed char* outptr3 = top_blob.row<signed char>(i * 4 + 3);
670
671 const float s0 = scale_data[i * 4];
672 const float s1 = scale_data[i * 4 + 1];
673 const float s2 = scale_data[i * 4 + 2];
674 const float s3 = scale_data[i * 4 + 3];
675
676 for (int j = 0; j < w; j++)
677 {
678 outptr0[0] = float2int8(bfloat16_to_float32(ptr0[0]) * s0);
679 outptr1[0] = float2int8(bfloat16_to_float32(ptr0[1]) * s1);
680 outptr2[0] = float2int8(bfloat16_to_float32(ptr0[2]) * s2);
681 outptr3[0] = float2int8(bfloat16_to_float32(ptr0[3]) * s3);
682
683 ptr0 += 4;
684 outptr0 += 1;
685 outptr1 += 1;
686 outptr2 += 1;
687 outptr3 += 1;
688 }
689 }
690 }
691 }
692 }
693
694 if (dims == 3)
695 {
696 int w = bottom_blob.w;
697 int h = bottom_blob.h;
698 int channels = bottom_blob.c;
699 int size = w * h;
700 int out_elempack = opt.use_packing_layout && channels * elempack % 8 == 0 ? 8 : 1;
701 int outc = channels * elempack / out_elempack;
702
703 top_blob.create(w, h, outc, (size_t)out_elempack, out_elempack, opt.blob_allocator);
704 if (top_blob.empty())
705 return -100;
706
707 if (out_elempack == 8)
708 {
709 if (scale_data_size == 1)
710 {
711 float32x4_t _scale = vdupq_n_f32(scale_data[0]);
712
713 #pragma omp parallel for num_threads(opt.num_threads)
714 for (int q = 0; q < outc; q++)
715 {
716 const unsigned short* ptr0 = bottom_blob.channel(q * 2);
717 const unsigned short* ptr1 = bottom_blob.channel(q * 2 + 1);
718 signed char* outptr = top_blob.channel(q);
719
720 for (int i = 0; i < size; i++)
721 {
722 float32x4_t _vlow = bfloat2float(vld1_u16(ptr0));
723 float32x4_t _vhigh = bfloat2float(vld1_u16(ptr1));
724 _vlow = vmulq_f32(_vlow, _scale);
725 _vhigh = vmulq_f32(_vhigh, _scale);
726 int8x8_t _v = float2int8(_vlow, _vhigh);
727 vst1_s8(outptr, _v);
728
729 ptr0 += 4;
730 ptr1 += 4;
731 outptr += 8;
732 }
733 }
734 }
735 else
736 {
737 #pragma omp parallel for num_threads(opt.num_threads)
738 for (int q = 0; q < outc; q++)
739 {
740 const unsigned short* ptr0 = bottom_blob.channel(q * 2);
741 const unsigned short* ptr1 = bottom_blob.channel(q * 2 + 1);
742 signed char* outptr = top_blob.channel(q);
743
744 float32x4_t _scale0 = vld1q_f32((const float*)scale_data + q * 8);
745 float32x4_t _scale1 = vld1q_f32((const float*)scale_data + q * 8 + 4);
746
747 for (int i = 0; i < size; i++)
748 {
749 float32x4_t _vlow = bfloat2float(vld1_u16(ptr0));
750 float32x4_t _vhigh = bfloat2float(vld1_u16(ptr1));
751 _vlow = vmulq_f32(_vlow, _scale0);
752 _vhigh = vmulq_f32(_vhigh, _scale1);
753 int8x8_t _v = float2int8(_vlow, _vhigh);
754 vst1_s8(outptr, _v);
755
756 ptr0 += 4;
757 ptr1 += 4;
758 outptr += 8;
759 }
760 }
761 }
762 }
763 if (out_elempack == 1)
764 {
765 if (scale_data_size == 1)
766 {
767 const float scale = scale_data[0];
768
769 #pragma omp parallel for num_threads(opt.num_threads)
770 for (int q = 0; q < channels; q++)
771 {
772 const unsigned short* ptr0 = bottom_blob.channel(q);
773 signed char* outptr0 = top_blob.channel(q * 4);
774 signed char* outptr1 = top_blob.channel(q * 4 + 1);
775 signed char* outptr2 = top_blob.channel(q * 4 + 2);
776 signed char* outptr3 = top_blob.channel(q * 4 + 3);
777
778 for (int i = 0; i < size; i++)
779 {
780 outptr0[0] = float2int8(bfloat16_to_float32(ptr0[0]) * scale);
781 outptr1[0] = float2int8(bfloat16_to_float32(ptr0[1]) * scale);
782 outptr2[0] = float2int8(bfloat16_to_float32(ptr0[2]) * scale);
783 outptr3[0] = float2int8(bfloat16_to_float32(ptr0[3]) * scale);
784
785 ptr0 += 4;
786 outptr0 += 1;
787 outptr1 += 1;
788 outptr2 += 1;
789 outptr3 += 1;
790 }
791 }
792 }
793 else
794 {
795 #pragma omp parallel for num_threads(opt.num_threads)
796 for (int q = 0; q < channels; q++)
797 {
798 const unsigned short* ptr0 = bottom_blob.channel(q);
799 signed char* outptr0 = top_blob.channel(q * 4);
800 signed char* outptr1 = top_blob.channel(q * 4 + 1);
801 signed char* outptr2 = top_blob.channel(q * 4 + 2);
802 signed char* outptr3 = top_blob.channel(q * 4 + 3);
803
804 const float s0 = scale_data[q * 4];
805 const float s1 = scale_data[q * 4 + 1];
806 const float s2 = scale_data[q * 4 + 2];
807 const float s3 = scale_data[q * 4 + 3];
808
809 for (int i = 0; i < size; i++)
810 {
811 outptr0[0] = float2int8(bfloat16_to_float32(ptr0[0]) * s0);
812 outptr1[0] = float2int8(bfloat16_to_float32(ptr0[1]) * s1);
813 outptr2[0] = float2int8(bfloat16_to_float32(ptr0[2]) * s2);
814 outptr3[0] = float2int8(bfloat16_to_float32(ptr0[3]) * s3);
815
816 ptr0 += 4;
817 outptr0 += 1;
818 outptr1 += 1;
819 outptr2 += 1;
820 outptr3 += 1;
821 }
822 }
823 }
824 }
825 }
826
827 return 0;
828 }
829#endif // __ARM_NEON
830
831 if (dims == 1)
832 {
833 int w = bottom_blob.w;
834
835 top_blob.create(w, (size_t)1u, opt.blob_allocator);
836 if (top_blob.empty())
837 return -100;
838
839 const unsigned short* ptr = bottom_blob;
840 signed char* outptr = top_blob;
841
842 if (scale_data_size == 1)
843 {
844 const float scale = scale_data[0];
845
846 #pragma omp parallel for num_threads(opt.num_threads)
847 for (int i = 0; i < w; i++)
848 {
849 outptr[i] = float2int8(bfloat16_to_float32(ptr[i]) * scale);
850 }
851 }
852 else
853 {
854 #pragma omp parallel for num_threads(opt.num_threads)
855 for (int i = 0; i < w; i++)
856 {
857 outptr[i] = float2int8(bfloat16_to_float32(ptr[i]) * scale_data[i]);
858 }
859 }
860 }
861
862 if (dims == 2)
863 {
864 int w = bottom_blob.w;
865 int h = bottom_blob.h;
866
867 top_blob.create(w, h, (size_t)1u, opt.blob_allocator);
868 if (top_blob.empty())
869 return -100;
870
871 #pragma omp parallel for num_threads(opt.num_threads)
872 for (int i = 0; i < h; i++)
873 {
874 const unsigned short* ptr0 = bottom_blob.row<const unsigned short>(i);
875 signed char* outptr0 = top_blob.row<signed char>(i);
876
877 const float scale = scale_data_size == 1 ? scale_data[0] : scale_data[i];
878
879 for (int j = 0; j < w; j++)
880 {
881 *outptr0++ = float2int8(bfloat16_to_float32(*ptr0++) * scale);
882 }
883 }
884 }
885
886 if (dims == 3)
887 {
888 int w = bottom_blob.w;
889 int h = bottom_blob.h;
890 int channels = bottom_blob.c;
891 int size = w * h;
892
893 top_blob.create(w, h, channels, (size_t)1u, opt.blob_allocator);
894 if (top_blob.empty())
895 return -100;
896
897 #pragma omp parallel for num_threads(opt.num_threads)
898 for (int q = 0; q < channels; q++)
899 {
900 const unsigned short* ptr = bottom_blob.channel(q);
901 signed char* outptr = top_blob.channel(q);
902
903 const float scale = scale_data_size == 1 ? scale_data[0] : scale_data[q];
904
905 for (int i = 0; i < size; i++)
906 {
907 *outptr++ = float2int8(bfloat16_to_float32(*ptr++) * scale);
908 }
909 }
910 }
911
912 return 0;
913}
914#endif // NCNN_BF16
915
916} // namespace ncnn