Elffuss BraidHub

kikocis/ncnn public

master · 26.7 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2021 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_x86.h"
16
17#if __SSE2__
18#include <emmintrin.h>
19#if __AVX__
20#include <immintrin.h>
21#endif // __AVX__
22#endif // __SSE2__
23
24#include "x86_usability.h"
25
26namespace ncnn {
27
28Quantize_x86::Quantize_x86()
29{
30#if __SSE2__
31 support_packing = true;
32#endif // __SSE2__
33}
34
35int Quantize_x86::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
36{
37 int dims = bottom_blob.dims;
38 int elempack = bottom_blob.elempack;
39
40#if __SSE2__
41#if __AVX__
42#if __AVX512F__
43 if (elempack == 16)
44 {
45 Mat tmp;
46 convert_packing(bottom_blob, tmp, 8, opt);
47
48 forward(tmp, top_blob, opt);
49
50 return 0;
51 }
52#endif // __AVX512F__
53
54 if (elempack == 8)
55 {
56 if (dims == 1)
57 {
58 int w = bottom_blob.w;
59
60 top_blob.create(w, (size_t)8u, 8, opt.blob_allocator);
61 if (top_blob.empty())
62 return -100;
63
64 if (scale_data_size == 1)
65 {
66 __m256 _scale = _mm256_set1_ps(scale_data[0]);
67
68 #pragma omp parallel for num_threads(opt.num_threads)
69 for (int i = 0; i < w; i++)
70 {
71 const float* ptr = (const float*)bottom_blob + i * 8;
72 signed char* outptr = (signed char*)top_blob + i * 8;
73
74 __m256 _v = _mm256_loadu_ps(ptr);
75 _v = _mm256_mul_ps(_v, _scale);
76 *(int64_t*)outptr = float2int8_avx(_v);
77 }
78 }
79 else
80 {
81 #pragma omp parallel for num_threads(opt.num_threads)
82 for (int i = 0; i < w; i++)
83 {
84 const float* ptr = (const float*)bottom_blob + i * 8;
85 signed char* outptr = (signed char*)top_blob + i * 8;
86
87 __m256 _v = _mm256_loadu_ps(ptr);
88 __m256 _scale = _mm256_loadu_ps((const float*)scale_data + i * 8);
89 _v = _mm256_mul_ps(_v, _scale);
90 *(int64_t*)outptr = float2int8_avx(_v);
91 }
92 }
93 }
94
95 if (dims == 2)
96 {
97 int w = bottom_blob.w;
98 int h = bottom_blob.h;
99
100 top_blob.create(w, h, (size_t)8u, 8, opt.blob_allocator);
101 if (top_blob.empty())
102 return -100;
103
104 if (scale_data_size == 1)
105 {
106 __m256 _scale = _mm256_set1_ps(scale_data[0]);
107
108 #pragma omp parallel for num_threads(opt.num_threads)
109 for (int i = 0; i < h; i++)
110 {
111 const float* ptr = bottom_blob.row(i);
112 signed char* outptr = top_blob.row<signed char>(i);
113
114 int j = 0;
115 for (; j + 1 < w; j += 2)
116 {
117 __m256 _v0 = _mm256_loadu_ps(ptr);
118 __m256 _v1 = _mm256_loadu_ps(ptr + 8);
119 _v0 = _mm256_mul_ps(_v0, _scale);
120 _v1 = _mm256_mul_ps(_v1, _scale);
121 __m128i _v = float2int8_avx(_v0, _v1);
122 _mm_storeu_si128((__m128i*)outptr, _v);
123
124 ptr += 16;
125 outptr += 16;
126 }
127 for (; j < w; j++)
128 {
129 __m256 _v = _mm256_loadu_ps(ptr);
130 _v = _mm256_mul_ps(_v, _scale);
131 *(int64_t*)outptr = float2int8_avx(_v);
132
133 ptr += 8;
134 outptr += 8;
135 }
136 }
137 }
138 else
139 {
140 #pragma omp parallel for num_threads(opt.num_threads)
141 for (int i = 0; i < h; i++)
142 {
143 const float* ptr = bottom_blob.row(i);
144 signed char* outptr = top_blob.row<signed char>(i);
145
146 __m256 _scale = _mm256_loadu_ps((const float*)scale_data + i * 8);
147
148 int j = 0;
149 for (; j + 1 < w; j += 2)
150 {
151 __m256 _v0 = _mm256_loadu_ps(ptr);
152 __m256 _v1 = _mm256_loadu_ps(ptr + 8);
153 _v0 = _mm256_mul_ps(_v0, _scale);
154 _v1 = _mm256_mul_ps(_v1, _scale);
155 __m128i _v = float2int8_avx(_v0, _v1);
156 _mm_storeu_si128((__m128i*)outptr, _v);
157
158 ptr += 16;
159 outptr += 16;
160 }
161 for (; j < w; j++)
162 {
163 __m256 _v = _mm256_loadu_ps(ptr);
164 _v = _mm256_mul_ps(_v, _scale);
165 *(int64_t*)outptr = float2int8_avx(_v);
166
167 ptr += 8;
168 outptr += 8;
169 }
170 }
171 }
172 }
173
174 if (dims == 3)
175 {
176 int w = bottom_blob.w;
177 int h = bottom_blob.h;
178 int channels = bottom_blob.c;
179 int size = w * h;
180
181 top_blob.create(w, h, channels, (size_t)8u, 8, opt.blob_allocator);
182 if (top_blob.empty())
183 return -100;
184
185 if (scale_data_size == 1)
186 {
187 __m256 _scale = _mm256_set1_ps(scale_data[0]);
188
189 #pragma omp parallel for num_threads(opt.num_threads)
190 for (int q = 0; q < channels; q++)
191 {
192 const float* ptr = bottom_blob.channel(q);
193 signed char* outptr = top_blob.channel(q);
194
195 int i = 0;
196 for (; i + 1 < size; i += 2)
197 {
198 __m256 _v0 = _mm256_loadu_ps(ptr);
199 __m256 _v1 = _mm256_loadu_ps(ptr + 8);
200 _v0 = _mm256_mul_ps(_v0, _scale);
201 _v1 = _mm256_mul_ps(_v1, _scale);
202 __m128i _v = float2int8_avx(_v0, _v1);
203 _mm_storeu_si128((__m128i*)outptr, _v);
204
205 ptr += 16;
206 outptr += 16;
207 }
208 for (; i < size; i++)
209 {
210 __m256 _v = _mm256_loadu_ps(ptr);
211 _v = _mm256_mul_ps(_v, _scale);
212 *(int64_t*)outptr = float2int8_avx(_v);
213
214 ptr += 8;
215 outptr += 8;
216 }
217 }
218 }
219 else
220 {
221 #pragma omp parallel for num_threads(opt.num_threads)
222 for (int q = 0; q < channels; q++)
223 {
224 const float* ptr = bottom_blob.channel(q);
225 signed char* outptr = top_blob.channel(q);
226
227 __m256 _scale = _mm256_loadu_ps((const float*)scale_data + q * 8);
228
229 int i = 0;
230 for (; i + 1 < size; i += 2)
231 {
232 __m256 _v0 = _mm256_loadu_ps(ptr);
233 __m256 _v1 = _mm256_loadu_ps(ptr + 8);
234 _v0 = _mm256_mul_ps(_v0, _scale);
235 _v1 = _mm256_mul_ps(_v1, _scale);
236 __m128i _v = float2int8_avx(_v0, _v1);
237 _mm_storeu_si128((__m128i*)outptr, _v);
238
239 ptr += 16;
240 outptr += 16;
241 }
242 for (; i < size; i++)
243 {
244 __m256 _v = _mm256_loadu_ps(ptr);
245 _v = _mm256_mul_ps(_v, _scale);
246 *(int64_t*)outptr = float2int8_avx(_v);
247
248 ptr += 8;
249 outptr += 8;
250 }
251 }
252 }
253 }
254
255 return 0;
256 }
257#endif // __AVX__
258
259 if (elempack == 4)
260 {
261 if (dims == 1)
262 {
263 int w = bottom_blob.w;
264 int out_elempack = opt.use_packing_layout && w * elempack % 8 == 0 ? 8 : 1;
265 int outw = w * elempack / out_elempack;
266
267 top_blob.create(outw, (size_t)out_elempack, out_elempack, opt.blob_allocator);
268 if (top_blob.empty())
269 return -100;
270
271 if (scale_data_size == 1)
272 {
273 const float scale = scale_data[0];
274
275 #pragma omp parallel for num_threads(opt.num_threads)
276 for (int i = 0; i < w; i++)
277 {
278 const float* ptr0 = (const float*)bottom_blob + i * 4;
279 signed char* outptr = (signed char*)top_blob + i * 4;
280
281 outptr[0] = float2int8(ptr0[0] * scale);
282 outptr[1] = float2int8(ptr0[1] * scale);
283 outptr[2] = float2int8(ptr0[2] * scale);
284 outptr[3] = float2int8(ptr0[3] * scale);
285 }
286 }
287 else
288 {
289 #pragma omp parallel for num_threads(opt.num_threads)
290 for (int i = 0; i < w; i++)
291 {
292 const float* ptr0 = (const float*)bottom_blob + i * 4;
293 signed char* outptr = (signed char*)top_blob + i * 4;
294
295 outptr[0] = float2int8(ptr0[0] * scale_data[i * 4]);
296 outptr[1] = float2int8(ptr0[1] * scale_data[i * 4 + 1]);
297 outptr[2] = float2int8(ptr0[2] * scale_data[i * 4 + 2]);
298 outptr[3] = float2int8(ptr0[3] * scale_data[i * 4 + 3]);
299 }
300 }
301 }
302
303 if (dims == 2)
304 {
305 int w = bottom_blob.w;
306 int h = bottom_blob.h;
307 int out_elempack = opt.use_packing_layout && h * elempack % 8 == 0 ? 8 : 1;
308 int outh = h * elempack / out_elempack;
309
310 top_blob.create(w, outh, (size_t)out_elempack, out_elempack, opt.blob_allocator);
311 if (top_blob.empty())
312 return -100;
313
314 if (out_elempack == 8)
315 {
316 if (scale_data_size == 1)
317 {
318 __m128 _scale = _mm_set1_ps(scale_data[0]);
319
320 #pragma omp parallel for num_threads(opt.num_threads)
321 for (int i = 0; i < outh; i++)
322 {
323 const float* ptr0 = bottom_blob.row(i * 2);
324 const float* ptr1 = bottom_blob.row(i * 2 + 1);
325 signed char* outptr = top_blob.row<signed char>(i);
326
327 int j = 0;
328 for (; j + 1 < w; j += 2)
329 {
330 __m128 _v0 = _mm_loadu_ps(ptr0);
331 __m128 _v1 = _mm_loadu_ps(ptr1);
332 __m128 _v2 = _mm_loadu_ps(ptr0 + 4);
333 __m128 _v3 = _mm_loadu_ps(ptr1 + 4);
334 _v0 = _mm_mul_ps(_v0, _scale);
335 _v1 = _mm_mul_ps(_v1, _scale);
336 _v2 = _mm_mul_ps(_v2, _scale);
337 _v3 = _mm_mul_ps(_v3, _scale);
338 __m128i _v = float2int8_sse(_v0, _v1, _v2, _v3);
339 _mm_storeu_si128((__m128i*)outptr, _v);
340
341 ptr0 += 8;
342 ptr1 += 8;
343 outptr += 16;
344 }
345 for (; j < w; j++)
346 {
347 __m128 _vlow = _mm_loadu_ps(ptr0);
348 __m128 _vhigh = _mm_loadu_ps(ptr1);
349 _vlow = _mm_mul_ps(_vlow, _scale);
350 _vhigh = _mm_mul_ps(_vhigh, _scale);
351 *(int64_t*)outptr = float2int8_sse(_vlow, _vhigh);
352
353 ptr0 += 4;
354 ptr1 += 4;
355 outptr += 8;
356 }
357 }
358 }
359 else
360 {
361 #pragma omp parallel for num_threads(opt.num_threads)
362 for (int i = 0; i < outh; i++)
363 {
364 const float* ptr0 = bottom_blob.row(i * 2);
365 const float* ptr1 = bottom_blob.row(i * 2 + 1);
366 signed char* outptr = top_blob.row<signed char>(i);
367
368 __m128 _scale0 = _mm_loadu_ps((const float*)scale_data + i * 8);
369 __m128 _scale1 = _mm_loadu_ps((const float*)scale_data + i * 8 + 4);
370
371 int j = 0;
372 for (; j + 1 < w; j += 2)
373 {
374 __m128 _v0 = _mm_loadu_ps(ptr0);
375 __m128 _v1 = _mm_loadu_ps(ptr1);
376 __m128 _v2 = _mm_loadu_ps(ptr0 + 4);
377 __m128 _v3 = _mm_loadu_ps(ptr1 + 4);
378 _v0 = _mm_mul_ps(_v0, _scale0);
379 _v1 = _mm_mul_ps(_v1, _scale1);
380 _v2 = _mm_mul_ps(_v2, _scale0);
381 _v3 = _mm_mul_ps(_v3, _scale1);
382 __m128i _v = float2int8_sse(_v0, _v1, _v2, _v3);
383 _mm_storeu_si128((__m128i*)outptr, _v);
384
385 ptr0 += 8;
386 ptr1 += 8;
387 outptr += 16;
388 }
389 for (; j < w; j++)
390 {
391 __m128 _vlow = _mm_loadu_ps(ptr0);
392 __m128 _vhigh = _mm_loadu_ps(ptr1);
393 _vlow = _mm_mul_ps(_vlow, _scale0);
394 _vhigh = _mm_mul_ps(_vhigh, _scale1);
395 *(int64_t*)outptr = float2int8_sse(_vlow, _vhigh);
396
397 ptr0 += 4;
398 ptr1 += 4;
399 outptr += 8;
400 }
401 }
402 }
403 }
404 if (out_elempack == 1)
405 {
406 if (scale_data_size == 1)
407 {
408 const float scale = scale_data[0];
409
410 #pragma omp parallel for num_threads(opt.num_threads)
411 for (int i = 0; i < h; i++)
412 {
413 const float* ptr0 = bottom_blob.row(i);
414 signed char* outptr0 = top_blob.row<signed char>(i * 4);
415 signed char* outptr1 = top_blob.row<signed char>(i * 4 + 1);
416 signed char* outptr2 = top_blob.row<signed char>(i * 4 + 2);
417 signed char* outptr3 = top_blob.row<signed char>(i * 4 + 3);
418
419 for (int j = 0; j < w; j++)
420 {
421 outptr0[0] = float2int8(ptr0[0] * scale);
422 outptr1[0] = float2int8(ptr0[1] * scale);
423 outptr2[0] = float2int8(ptr0[2] * scale);
424 outptr3[0] = float2int8(ptr0[3] * scale);
425
426 ptr0 += 4;
427 outptr0 += 1;
428 outptr1 += 1;
429 outptr2 += 1;
430 outptr3 += 1;
431 }
432 }
433 }
434 else
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 * 4);
441 signed char* outptr1 = top_blob.row<signed char>(i * 4 + 1);
442 signed char* outptr2 = top_blob.row<signed char>(i * 4 + 2);
443 signed char* outptr3 = top_blob.row<signed char>(i * 4 + 3);
444
445 const float s0 = scale_data[i * 4];
446 const float s1 = scale_data[i * 4 + 1];
447 const float s2 = scale_data[i * 4 + 2];
448 const float s3 = scale_data[i * 4 + 3];
449
450 for (int j = 0; j < w; j++)
451 {
452 outptr0[0] = float2int8(ptr0[0] * s0);
453 outptr1[0] = float2int8(ptr0[1] * s1);
454 outptr2[0] = float2int8(ptr0[2] * s2);
455 outptr3[0] = float2int8(ptr0[3] * s3);
456
457 ptr0 += 4;
458 outptr0 += 1;
459 outptr1 += 1;
460 outptr2 += 1;
461 outptr3 += 1;
462 }
463 }
464 }
465 }
466 }
467
468 if (dims == 3)
469 {
470 int w = bottom_blob.w;
471 int h = bottom_blob.h;
472 int channels = bottom_blob.c;
473 int size = w * h;
474 int out_elempack = opt.use_packing_layout && channels * elempack % 8 == 0 ? 8 : 1;
475 int outc = channels * elempack / out_elempack;
476
477 top_blob.create(w, h, outc, (size_t)out_elempack, out_elempack, opt.blob_allocator);
478 if (top_blob.empty())
479 return -100;
480
481 if (out_elempack == 8)
482 {
483 if (scale_data_size == 1)
484 {
485 __m128 _scale = _mm_set1_ps(scale_data[0]);
486
487 #pragma omp parallel for num_threads(opt.num_threads)
488 for (int q = 0; q < outc; q++)
489 {
490 const float* ptr0 = bottom_blob.channel(q * 2);
491 const float* ptr1 = bottom_blob.channel(q * 2 + 1);
492 signed char* outptr = top_blob.channel(q);
493
494 int i = 0;
495 for (; i + 1 < size; i += 2)
496 {
497 __m128 _v0 = _mm_loadu_ps(ptr0);
498 __m128 _v1 = _mm_loadu_ps(ptr1);
499 __m128 _v2 = _mm_loadu_ps(ptr0 + 4);
500 __m128 _v3 = _mm_loadu_ps(ptr1 + 4);
501 _v0 = _mm_mul_ps(_v0, _scale);
502 _v1 = _mm_mul_ps(_v1, _scale);
503 _v2 = _mm_mul_ps(_v2, _scale);
504 _v3 = _mm_mul_ps(_v3, _scale);
505 __m128i _v = float2int8_sse(_v0, _v1, _v2, _v3);
506 _mm_storeu_si128((__m128i*)outptr, _v);
507
508 ptr0 += 8;
509 ptr1 += 8;
510 outptr += 16;
511 }
512 for (; i < size; i++)
513 {
514 __m128 _vlow = _mm_loadu_ps(ptr0);
515 __m128 _vhigh = _mm_loadu_ps(ptr1);
516 _vlow = _mm_mul_ps(_vlow, _scale);
517 _vhigh = _mm_mul_ps(_vhigh, _scale);
518 *(int64_t*)outptr = float2int8_sse(_vlow, _vhigh);
519
520 ptr0 += 4;
521 ptr1 += 4;
522 outptr += 8;
523 }
524 }
525 }
526 else
527 {
528 #pragma omp parallel for num_threads(opt.num_threads)
529 for (int q = 0; q < outc; q++)
530 {
531 const float* ptr0 = bottom_blob.channel(q * 2);
532 const float* ptr1 = bottom_blob.channel(q * 2 + 1);
533 signed char* outptr = top_blob.channel(q);
534
535 __m128 _scale0 = _mm_loadu_ps((const float*)scale_data + q * 8);
536 __m128 _scale1 = _mm_loadu_ps((const float*)scale_data + q * 8 + 4);
537
538 int i = 0;
539 for (; i + 1 < size; i += 2)
540 {
541 __m128 _v0 = _mm_loadu_ps(ptr0);
542 __m128 _v1 = _mm_loadu_ps(ptr1);
543 __m128 _v2 = _mm_loadu_ps(ptr0 + 4);
544 __m128 _v3 = _mm_loadu_ps(ptr1 + 4);
545 _v0 = _mm_mul_ps(_v0, _scale0);
546 _v1 = _mm_mul_ps(_v1, _scale1);
547 _v2 = _mm_mul_ps(_v2, _scale0);
548 _v3 = _mm_mul_ps(_v3, _scale1);
549 __m128i _v = float2int8_sse(_v0, _v1, _v2, _v3);
550 _mm_storeu_si128((__m128i*)outptr, _v);
551
552 ptr0 += 8;
553 ptr1 += 8;
554 outptr += 16;
555 }
556 for (; i < size; i++)
557 {
558 __m128 _vlow = _mm_loadu_ps(ptr0);
559 __m128 _vhigh = _mm_loadu_ps(ptr1);
560 _vlow = _mm_mul_ps(_vlow, _scale0);
561 _vhigh = _mm_mul_ps(_vhigh, _scale1);
562 *(int64_t*)outptr = float2int8_sse(_vlow, _vhigh);
563
564 ptr0 += 4;
565 ptr1 += 4;
566 outptr += 8;
567 }
568 }
569 }
570 }
571 if (out_elempack == 1)
572 {
573 if (scale_data_size == 1)
574 {
575 const float scale = scale_data[0];
576
577 #pragma omp parallel for num_threads(opt.num_threads)
578 for (int q = 0; q < channels; q++)
579 {
580 const float* ptr0 = bottom_blob.channel(q);
581 signed char* outptr0 = top_blob.channel(q * 4);
582 signed char* outptr1 = top_blob.channel(q * 4 + 1);
583 signed char* outptr2 = top_blob.channel(q * 4 + 2);
584 signed char* outptr3 = top_blob.channel(q * 4 + 3);
585
586 for (int i = 0; i < size; i++)
587 {
588 outptr0[0] = float2int8(ptr0[0] * scale);
589 outptr1[0] = float2int8(ptr0[1] * scale);
590 outptr2[0] = float2int8(ptr0[2] * scale);
591 outptr3[0] = float2int8(ptr0[3] * scale);
592
593 ptr0 += 4;
594 outptr0 += 1;
595 outptr1 += 1;
596 outptr2 += 1;
597 outptr3 += 1;
598 }
599 }
600 }
601 else
602 {
603 #pragma omp parallel for num_threads(opt.num_threads)
604 for (int q = 0; q < channels; q++)
605 {
606 const float* ptr0 = bottom_blob.channel(q);
607 signed char* outptr0 = top_blob.channel(q * 4);
608 signed char* outptr1 = top_blob.channel(q * 4 + 1);
609 signed char* outptr2 = top_blob.channel(q * 4 + 2);
610 signed char* outptr3 = top_blob.channel(q * 4 + 3);
611
612 const float s0 = scale_data[q * 4];
613 const float s1 = scale_data[q * 4 + 1];
614 const float s2 = scale_data[q * 4 + 2];
615 const float s3 = scale_data[q * 4 + 3];
616
617 for (int i = 0; i < size; i++)
618 {
619 outptr0[0] = float2int8(ptr0[0] * s0);
620 outptr1[0] = float2int8(ptr0[1] * s1);
621 outptr2[0] = float2int8(ptr0[2] * s2);
622 outptr3[0] = float2int8(ptr0[3] * s3);
623
624 ptr0 += 4;
625 outptr0 += 1;
626 outptr1 += 1;
627 outptr2 += 1;
628 outptr3 += 1;
629 }
630 }
631 }
632 }
633 }
634
635 return 0;
636 }
637#endif // __SSE2__
638
639 if (dims == 1)
640 {
641 int w = bottom_blob.w;
642
643 top_blob.create(w, (size_t)1u, opt.blob_allocator);
644 if (top_blob.empty())
645 return -100;
646
647 const float* ptr = bottom_blob;
648 signed char* outptr = top_blob;
649
650 if (scale_data_size == 1)
651 {
652 const float scale = scale_data[0];
653
654 #pragma omp parallel for num_threads(opt.num_threads)
655 for (int i = 0; i < w; i++)
656 {
657 outptr[i] = float2int8(ptr[i] * scale);
658 }
659 }
660 else
661 {
662 #pragma omp parallel for num_threads(opt.num_threads)
663 for (int i = 0; i < w; i++)
664 {
665 outptr[i] = float2int8(ptr[i] * scale_data[i]);
666 }
667 }
668 }
669
670 if (dims == 2)
671 {
672 int w = bottom_blob.w;
673 int h = bottom_blob.h;
674
675 top_blob.create(w, h, (size_t)1u, opt.blob_allocator);
676 if (top_blob.empty())
677 return -100;
678
679 #pragma omp parallel for num_threads(opt.num_threads)
680 for (int i = 0; i < h; i++)
681 {
682 const float* ptr0 = bottom_blob.row(i);
683 signed char* outptr0 = top_blob.row<signed char>(i);
684
685 const float scale = scale_data_size == 1 ? scale_data[0] : scale_data[i];
686
687 for (int j = 0; j < w; j++)
688 {
689 *outptr0++ = float2int8(*ptr0++ * scale);
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
701 top_blob.create(w, h, channels, (size_t)1u, opt.blob_allocator);
702 if (top_blob.empty())
703 return -100;
704
705 #pragma omp parallel for num_threads(opt.num_threads)
706 for (int q = 0; q < channels; q++)
707 {
708 const float* ptr = bottom_blob.channel(q);
709 signed char* outptr = top_blob.channel(q);
710
711 const float scale = scale_data_size == 1 ? scale_data[0] : scale_data[q];
712
713 for (int i = 0; i < size; i++)
714 {
715 *outptr++ = float2int8(*ptr++ * scale);
716 }
717 }
718 }
719
720 return 0;
721}
722
723} // namespace ncnn