Elffuss BraidHub

kikocis/ncnn public

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