Elffuss BraidHub

kikocis/ncnn public

master · 68.1 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) 2021 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 "requantize_arm.h"
17
18#if __ARM_NEON
19#include <arm_neon.h>
20#endif // __ARM_NEON
21
22#include "arm_activation.h"
23#include "arm_usability.h"
24
25namespace ncnn {
26
27#if __ARM_NEON
28#include "requantize_leakyrelu_pack4.h"
29#include "requantize_leakyrelu_pack8.h"
30#include "requantize_relu_pack4.h"
31#include "requantize_relu_pack8.h"
32#endif // __ARM_NEON
33
34Requantize_arm::Requantize_arm()
35{
36#if __ARM_NEON
37 support_packing = true;
38#endif // __ARM_NEON
39}
40
41int Requantize_arm::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
42{
43 int dims = bottom_blob.dims;
44 int elempack = bottom_blob.elempack;
45
46#if __ARM_NEON
47 if (elempack == 8)
48 {
49 if (dims == 1)
50 {
51 int w = bottom_blob.w;
52
53 top_blob.create(w, (size_t)8u, 8, opt.blob_allocator);
54 if (top_blob.empty())
55 return -100;
56
57 if (scale_in_data_size == 1 && scale_out_data_size == 1)
58 {
59 float32x4_t _scale_in = vdupq_n_f32(scale_in_data[0]);
60 float32x4_t _scale_out = vdupq_n_f32(scale_out_data[0]);
61
62 if (bias_data_size == 0)
63 {
64 #pragma omp parallel for num_threads(opt.num_threads)
65 for (int i = 0; i < w; i++)
66 {
67 const int* intptr = (const int*)bottom_blob + i * 8;
68 signed char* ptr = (signed char*)top_blob + i * 8;
69
70 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
71 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32((intptr + 4)));
72 _v0 = vmulq_f32(_v0, _scale_in);
73 _v1 = vmulq_f32(_v1, _scale_in);
74 _v0 = activation_ps(_v0, activation_type, activation_params);
75 _v1 = activation_ps(_v1, activation_type, activation_params);
76 _v0 = vmulq_f32(_v0, _scale_out);
77 _v1 = vmulq_f32(_v1, _scale_out);
78 vst1_s8(ptr, float2int8(_v0, _v1));
79 }
80 }
81 else if (bias_data_size == 1)
82 {
83 float32x4_t _bias = vdupq_n_f32(bias_data[0]);
84
85 #pragma omp parallel for num_threads(opt.num_threads)
86 for (int i = 0; i < w; i++)
87 {
88 const int* intptr = (const int*)bottom_blob + i * 8;
89 signed char* ptr = (signed char*)top_blob + i * 8;
90
91 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
92 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32((intptr + 4)));
93 _v0 = vmlaq_f32(_bias, _v0, _scale_in);
94 _v1 = vmlaq_f32(_bias, _v1, _scale_in);
95 _v0 = activation_ps(_v0, activation_type, activation_params);
96 _v1 = activation_ps(_v1, activation_type, activation_params);
97 _v0 = vmulq_f32(_v0, _scale_out);
98 _v1 = vmulq_f32(_v1, _scale_out);
99 vst1_s8(ptr, float2int8(_v0, _v1));
100 }
101 }
102 else
103 {
104 #pragma omp parallel for num_threads(opt.num_threads)
105 for (int i = 0; i < w; i++)
106 {
107 const int* intptr = (const int*)bottom_blob + i * 8;
108 signed char* ptr = (signed char*)top_blob + i * 8;
109
110 float32x4_t _bias0 = bias_data_size == 1 ? vdupq_n_f32(bias_data[0]) : vld1q_f32((const float*)bias_data + i * 8);
111 float32x4_t _bias1 = bias_data_size == 1 ? vdupq_n_f32(bias_data[0]) : vld1q_f32((const float*)bias_data + i * 8 + 4);
112 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
113 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32((intptr + 4)));
114 _v0 = vmlaq_f32(_bias0, _v0, _scale_in);
115 _v1 = vmlaq_f32(_bias1, _v1, _scale_in);
116 _v0 = activation_ps(_v0, activation_type, activation_params);
117 _v1 = activation_ps(_v1, activation_type, activation_params);
118 _v0 = vmulq_f32(_v0, _scale_out);
119 _v1 = vmulq_f32(_v1, _scale_out);
120 vst1_s8(ptr, float2int8(_v0, _v1));
121 }
122 }
123 }
124 else if (scale_in_data_size == 1 && scale_out_data_size > 1)
125 {
126 float32x4_t _scale_in = vdupq_n_f32(scale_in_data[0]);
127
128 if (bias_data_size == 0)
129 {
130 #pragma omp parallel for num_threads(opt.num_threads)
131 for (int i = 0; i < w; i++)
132 {
133 const int* intptr = (const int*)bottom_blob + i * 8;
134 signed char* ptr = (signed char*)top_blob + i * 8;
135
136 float32x4_t _scale_out0 = scale_out_data_size == 1 ? vdupq_n_f32(scale_out_data[0]) : vld1q_f32((const float*)scale_out_data + i * 8);
137 float32x4_t _scale_out1 = scale_out_data_size == 1 ? vdupq_n_f32(scale_out_data[0]) : vld1q_f32((const float*)scale_out_data + i * 8 + 4);
138 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
139 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32((intptr + 4)));
140 _v0 = vmulq_f32(_v0, _scale_in);
141 _v1 = vmulq_f32(_v1, _scale_in);
142 _v0 = activation_ps(_v0, activation_type, activation_params);
143 _v1 = activation_ps(_v1, activation_type, activation_params);
144 _v0 = vmulq_f32(_v0, _scale_out0);
145 _v1 = vmulq_f32(_v1, _scale_out1);
146 vst1_s8(ptr, float2int8(_v0, _v1));
147 }
148 }
149 else if (bias_data_size == 1)
150 {
151 float32x4_t _bias = vdupq_n_f32(bias_data[0]);
152
153 #pragma omp parallel for num_threads(opt.num_threads)
154 for (int i = 0; i < w; i++)
155 {
156 const int* intptr = (const int*)bottom_blob + i * 8;
157 signed char* ptr = (signed char*)top_blob + i * 8;
158
159 float32x4_t _scale_out0 = scale_out_data_size == 1 ? vdupq_n_f32(scale_out_data[0]) : vld1q_f32((const float*)scale_out_data + i * 8);
160 float32x4_t _scale_out1 = scale_out_data_size == 1 ? vdupq_n_f32(scale_out_data[0]) : vld1q_f32((const float*)scale_out_data + i * 8 + 4);
161 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
162 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32((intptr + 4)));
163 _v0 = vmlaq_f32(_bias, _v0, _scale_in);
164 _v1 = vmlaq_f32(_bias, _v1, _scale_in);
165 _v0 = activation_ps(_v0, activation_type, activation_params);
166 _v1 = activation_ps(_v1, activation_type, activation_params);
167 _v0 = vmulq_f32(_v0, _scale_out0);
168 _v1 = vmulq_f32(_v1, _scale_out1);
169 vst1_s8(ptr, float2int8(_v0, _v1));
170 }
171 }
172 else
173 {
174 #pragma omp parallel for num_threads(opt.num_threads)
175 for (int i = 0; i < w; i++)
176 {
177 const int* intptr = (const int*)bottom_blob + i * 8;
178 signed char* ptr = (signed char*)top_blob + i * 8;
179
180 float32x4_t _scale_out0 = scale_out_data_size == 1 ? vdupq_n_f32(scale_out_data[0]) : vld1q_f32((const float*)scale_out_data + i * 8);
181 float32x4_t _scale_out1 = scale_out_data_size == 1 ? vdupq_n_f32(scale_out_data[0]) : vld1q_f32((const float*)scale_out_data + i * 8 + 4);
182 float32x4_t _bias0 = bias_data_size == 1 ? vdupq_n_f32(bias_data[0]) : vld1q_f32((const float*)bias_data + i * 8);
183 float32x4_t _bias1 = bias_data_size == 1 ? vdupq_n_f32(bias_data[0]) : vld1q_f32((const float*)bias_data + i * 8 + 4);
184 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
185 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32((intptr + 4)));
186 _v0 = vmlaq_f32(_bias0, _v0, _scale_in);
187 _v1 = vmlaq_f32(_bias1, _v1, _scale_in);
188 _v0 = activation_ps(_v0, activation_type, activation_params);
189 _v1 = activation_ps(_v1, activation_type, activation_params);
190 _v0 = vmulq_f32(_v0, _scale_out0);
191 _v1 = vmulq_f32(_v1, _scale_out1);
192 vst1_s8(ptr, float2int8(_v0, _v1));
193 }
194 }
195 }
196 else if (scale_in_data_size > 1 && scale_out_data_size == 1)
197 {
198 float32x4_t _scale_out = vdupq_n_f32(scale_out_data[0]);
199
200 if (bias_data_size == 0)
201 {
202 #pragma omp parallel for num_threads(opt.num_threads)
203 for (int i = 0; i < w; i++)
204 {
205 const int* intptr = (const int*)bottom_blob + i * 8;
206 signed char* ptr = (signed char*)top_blob + i * 8;
207
208 float32x4_t _scale_in0 = scale_in_data_size == 1 ? vdupq_n_f32(scale_in_data[0]) : vld1q_f32((const float*)scale_in_data + i * 8);
209 float32x4_t _scale_in1 = scale_in_data_size == 1 ? vdupq_n_f32(scale_in_data[0]) : vld1q_f32((const float*)scale_in_data + i * 8 + 4);
210 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
211 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32((intptr + 4)));
212 _v0 = vmulq_f32(_v0, _scale_in0);
213 _v1 = vmulq_f32(_v1, _scale_in1);
214 _v0 = activation_ps(_v0, activation_type, activation_params);
215 _v1 = activation_ps(_v1, activation_type, activation_params);
216 _v0 = vmulq_f32(_v0, _scale_out);
217 _v1 = vmulq_f32(_v1, _scale_out);
218 vst1_s8(ptr, float2int8(_v0, _v1));
219 }
220 }
221 else if (bias_data_size == 1)
222 {
223 float32x4_t _bias = vdupq_n_f32(bias_data[0]);
224
225 #pragma omp parallel for num_threads(opt.num_threads)
226 for (int i = 0; i < w; i++)
227 {
228 const int* intptr = (const int*)bottom_blob + i * 8;
229 signed char* ptr = (signed char*)top_blob + i * 8;
230
231 float32x4_t _scale_in0 = scale_in_data_size == 1 ? vdupq_n_f32(scale_in_data[0]) : vld1q_f32((const float*)scale_in_data + i * 8);
232 float32x4_t _scale_in1 = scale_in_data_size == 1 ? vdupq_n_f32(scale_in_data[0]) : vld1q_f32((const float*)scale_in_data + i * 8 + 4);
233 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
234 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32((intptr + 4)));
235 _v0 = vmlaq_f32(_bias, _v0, _scale_in0);
236 _v1 = vmlaq_f32(_bias, _v1, _scale_in1);
237 _v0 = activation_ps(_v0, activation_type, activation_params);
238 _v1 = activation_ps(_v1, activation_type, activation_params);
239 _v0 = vmulq_f32(_v0, _scale_out);
240 _v1 = vmulq_f32(_v1, _scale_out);
241 vst1_s8(ptr, float2int8(_v0, _v1));
242 }
243 }
244 else
245 {
246 #pragma omp parallel for num_threads(opt.num_threads)
247 for (int i = 0; i < w; i++)
248 {
249 const int* intptr = (const int*)bottom_blob + i * 8;
250 signed char* ptr = (signed char*)top_blob + i * 8;
251
252 float32x4_t _scale_in0 = scale_in_data_size == 1 ? vdupq_n_f32(scale_in_data[0]) : vld1q_f32((const float*)scale_in_data + i * 8);
253 float32x4_t _scale_in1 = scale_in_data_size == 1 ? vdupq_n_f32(scale_in_data[0]) : vld1q_f32((const float*)scale_in_data + i * 8 + 4);
254 float32x4_t _bias0 = bias_data_size == 1 ? vdupq_n_f32(bias_data[0]) : vld1q_f32((const float*)bias_data + i * 8);
255 float32x4_t _bias1 = bias_data_size == 1 ? vdupq_n_f32(bias_data[0]) : vld1q_f32((const float*)bias_data + i * 8 + 4);
256 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
257 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32((intptr + 4)));
258 _v0 = vmlaq_f32(_bias0, _v0, _scale_in0);
259 _v1 = vmlaq_f32(_bias1, _v1, _scale_in1);
260 _v0 = activation_ps(_v0, activation_type, activation_params);
261 _v1 = activation_ps(_v1, activation_type, activation_params);
262 _v0 = vmulq_f32(_v0, _scale_out);
263 _v1 = vmulq_f32(_v1, _scale_out);
264 vst1_s8(ptr, float2int8(_v0, _v1));
265 }
266 }
267 }
268 else // if (scale_in_data_size > 1 && scale_out_data_size > 1)
269 {
270 if (bias_data_size == 0)
271 {
272 #pragma omp parallel for num_threads(opt.num_threads)
273 for (int i = 0; i < w; i++)
274 {
275 const int* intptr = (const int*)bottom_blob + i * 8;
276 signed char* ptr = (signed char*)top_blob + i * 8;
277
278 float32x4_t _scale_in0 = scale_in_data_size == 1 ? vdupq_n_f32(scale_in_data[0]) : vld1q_f32((const float*)scale_in_data + i * 8);
279 float32x4_t _scale_in1 = scale_in_data_size == 1 ? vdupq_n_f32(scale_in_data[0]) : vld1q_f32((const float*)scale_in_data + i * 8 + 4);
280 float32x4_t _scale_out0 = scale_out_data_size == 1 ? vdupq_n_f32(scale_out_data[0]) : vld1q_f32((const float*)scale_out_data + i * 8);
281 float32x4_t _scale_out1 = scale_out_data_size == 1 ? vdupq_n_f32(scale_out_data[0]) : vld1q_f32((const float*)scale_out_data + i * 8 + 4);
282 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
283 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32((intptr + 4)));
284 _v0 = vmulq_f32(_v0, _scale_in0);
285 _v1 = vmulq_f32(_v1, _scale_in1);
286 _v0 = activation_ps(_v0, activation_type, activation_params);
287 _v1 = activation_ps(_v1, activation_type, activation_params);
288 _v0 = vmulq_f32(_v0, _scale_out0);
289 _v1 = vmulq_f32(_v1, _scale_out1);
290 vst1_s8(ptr, float2int8(_v0, _v1));
291 }
292 }
293 else if (bias_data_size == 1)
294 {
295 float32x4_t _bias = vdupq_n_f32(bias_data[0]);
296
297 #pragma omp parallel for num_threads(opt.num_threads)
298 for (int i = 0; i < w; i++)
299 {
300 const int* intptr = (const int*)bottom_blob + i * 8;
301 signed char* ptr = (signed char*)top_blob + i * 8;
302
303 float32x4_t _scale_in0 = scale_in_data_size == 1 ? vdupq_n_f32(scale_in_data[0]) : vld1q_f32((const float*)scale_in_data + i * 8);
304 float32x4_t _scale_in1 = scale_in_data_size == 1 ? vdupq_n_f32(scale_in_data[0]) : vld1q_f32((const float*)scale_in_data + i * 8 + 4);
305 float32x4_t _scale_out0 = scale_out_data_size == 1 ? vdupq_n_f32(scale_out_data[0]) : vld1q_f32((const float*)scale_out_data + i * 8);
306 float32x4_t _scale_out1 = scale_out_data_size == 1 ? vdupq_n_f32(scale_out_data[0]) : vld1q_f32((const float*)scale_out_data + i * 8 + 4);
307 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
308 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32((intptr + 4)));
309 _v0 = vmlaq_f32(_bias, _v0, _scale_in0);
310 _v1 = vmlaq_f32(_bias, _v1, _scale_in1);
311 _v0 = activation_ps(_v0, activation_type, activation_params);
312 _v1 = activation_ps(_v1, activation_type, activation_params);
313 _v0 = vmulq_f32(_v0, _scale_out0);
314 _v1 = vmulq_f32(_v1, _scale_out1);
315 vst1_s8(ptr, float2int8(_v0, _v1));
316 }
317 }
318 else
319 {
320 #pragma omp parallel for num_threads(opt.num_threads)
321 for (int i = 0; i < w; i++)
322 {
323 const int* intptr = (const int*)bottom_blob + i * 8;
324 signed char* ptr = (signed char*)top_blob + i * 8;
325
326 float32x4_t _scale_in0 = scale_in_data_size == 1 ? vdupq_n_f32(scale_in_data[0]) : vld1q_f32((const float*)scale_in_data + i * 8);
327 float32x4_t _scale_in1 = scale_in_data_size == 1 ? vdupq_n_f32(scale_in_data[0]) : vld1q_f32((const float*)scale_in_data + i * 8 + 4);
328 float32x4_t _scale_out0 = scale_out_data_size == 1 ? vdupq_n_f32(scale_out_data[0]) : vld1q_f32((const float*)scale_out_data + i * 8);
329 float32x4_t _scale_out1 = scale_out_data_size == 1 ? vdupq_n_f32(scale_out_data[0]) : vld1q_f32((const float*)scale_out_data + i * 8 + 4);
330 float32x4_t _bias0 = bias_data_size == 1 ? vdupq_n_f32(bias_data[0]) : vld1q_f32((const float*)bias_data + i * 8);
331 float32x4_t _bias1 = bias_data_size == 1 ? vdupq_n_f32(bias_data[0]) : vld1q_f32((const float*)bias_data + i * 8 + 4);
332 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
333 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32((intptr + 4)));
334 _v0 = vmlaq_f32(_bias0, _v0, _scale_in0);
335 _v1 = vmlaq_f32(_bias1, _v1, _scale_in1);
336 _v0 = activation_ps(_v0, activation_type, activation_params);
337 _v1 = activation_ps(_v1, activation_type, activation_params);
338 _v0 = vmulq_f32(_v0, _scale_out0);
339 _v1 = vmulq_f32(_v1, _scale_out1);
340 vst1_s8(ptr, float2int8(_v0, _v1));
341 }
342 }
343 }
344 }
345
346 if (dims == 2)
347 {
348 int w = bottom_blob.w;
349 int h = bottom_blob.h;
350
351 top_blob.create(w, h, (size_t)8u, 8, opt.blob_allocator);
352 if (top_blob.empty())
353 return -100;
354
355 if (bias_data_size == 0)
356 {
357 #pragma omp parallel for num_threads(opt.num_threads)
358 for (int i = 0; i < h; i++)
359 {
360 const int* intptr = bottom_blob.row<const int>(i);
361 signed char* ptr = top_blob.row<signed char>(i);
362
363 float32x4_t _scale_in0 = scale_in_data_size == 1 ? vdupq_n_f32(scale_in_data[0]) : vld1q_f32((const float*)scale_in_data + i * 8);
364 float32x4_t _scale_in1 = scale_in_data_size == 1 ? vdupq_n_f32(scale_in_data[0]) : vld1q_f32((const float*)scale_in_data + i * 8 + 4);
365 float32x4_t _scale_out0 = scale_out_data_size == 1 ? vdupq_n_f32(scale_out_data[0]) : vld1q_f32((const float*)scale_out_data + i * 8);
366 float32x4_t _scale_out1 = scale_out_data_size == 1 ? vdupq_n_f32(scale_out_data[0]) : vld1q_f32((const float*)scale_out_data + i * 8 + 4);
367
368 for (int j = 0; j < w; j++)
369 {
370 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
371 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32((intptr + 4)));
372 _v0 = vmulq_f32(_v0, _scale_in0);
373 _v1 = vmulq_f32(_v1, _scale_in1);
374 _v0 = activation_ps(_v0, activation_type, activation_params);
375 _v1 = activation_ps(_v1, activation_type, activation_params);
376 _v0 = vmulq_f32(_v0, _scale_out0);
377 _v1 = vmulq_f32(_v1, _scale_out1);
378 vst1_s8(ptr, float2int8(_v0, _v1));
379
380 intptr += 8;
381 ptr += 8;
382 }
383 }
384 }
385 else
386 {
387 #pragma omp parallel for num_threads(opt.num_threads)
388 for (int i = 0; i < h; i++)
389 {
390 const int* intptr = bottom_blob.row<const int>(i);
391 signed char* ptr = top_blob.row<signed char>(i);
392
393 float32x4_t _scale_in0 = scale_in_data_size == 1 ? vdupq_n_f32(scale_in_data[0]) : vld1q_f32((const float*)scale_in_data + i * 8);
394 float32x4_t _scale_in1 = scale_in_data_size == 1 ? vdupq_n_f32(scale_in_data[0]) : vld1q_f32((const float*)scale_in_data + i * 8 + 4);
395 float32x4_t _scale_out0 = scale_out_data_size == 1 ? vdupq_n_f32(scale_out_data[0]) : vld1q_f32((const float*)scale_out_data + i * 8);
396 float32x4_t _scale_out1 = scale_out_data_size == 1 ? vdupq_n_f32(scale_out_data[0]) : vld1q_f32((const float*)scale_out_data + i * 8 + 4);
397 float32x4_t _bias0 = bias_data_size == 1 ? vdupq_n_f32(bias_data[0]) : vld1q_f32((const float*)bias_data + i * 8);
398 float32x4_t _bias1 = bias_data_size == 1 ? vdupq_n_f32(bias_data[0]) : vld1q_f32((const float*)bias_data + i * 8 + 4);
399
400 for (int j = 0; j < w; j++)
401 {
402 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
403 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32((intptr + 4)));
404 _v0 = vmlaq_f32(_bias0, _v0, _scale_in0);
405 _v1 = vmlaq_f32(_bias1, _v1, _scale_in1);
406 _v0 = activation_ps(_v0, activation_type, activation_params);
407 _v1 = activation_ps(_v1, activation_type, activation_params);
408 _v0 = vmulq_f32(_v0, _scale_out0);
409 _v1 = vmulq_f32(_v1, _scale_out1);
410 vst1_s8(ptr, float2int8(_v0, _v1));
411
412 intptr += 8;
413 ptr += 8;
414 }
415 }
416 }
417 }
418
419 if (dims == 3)
420 {
421 int w = bottom_blob.w;
422 int h = bottom_blob.h;
423 int channels = bottom_blob.c;
424 int size = w * h;
425
426 top_blob.create(w, h, channels, (size_t)8u, 8, opt.blob_allocator);
427 if (top_blob.empty())
428 return -100;
429
430 if (activation_type == 1)
431 {
432 requantize_relu_pack8_neon(bottom_blob, top_blob, scale_in_data, scale_out_data, bias_data, opt);
433 return 0;
434 }
435
436 if (activation_type == 2 && activation_params[0] > 0.f)
437 {
438 requantize_leakyrelu_pack8_neon(bottom_blob, top_blob, scale_in_data, scale_out_data, bias_data, activation_params[0], opt);
439 return 0;
440 }
441
442 if (bias_data_size == 0)
443 {
444 #pragma omp parallel for num_threads(opt.num_threads)
445 for (int q = 0; q < channels; q++)
446 {
447 const int* intptr = bottom_blob.channel(q);
448 signed char* ptr = top_blob.channel(q);
449
450 float32x4_t _scale_in0 = scale_in_data_size == 1 ? vdupq_n_f32(scale_in_data[0]) : vld1q_f32((const float*)scale_in_data + q * 8);
451 float32x4_t _scale_in1 = scale_in_data_size == 1 ? vdupq_n_f32(scale_in_data[0]) : vld1q_f32((const float*)scale_in_data + q * 8 + 4);
452 float32x4_t _scale_out0 = scale_out_data_size == 1 ? vdupq_n_f32(scale_out_data[0]) : vld1q_f32((const float*)scale_out_data + q * 8);
453 float32x4_t _scale_out1 = scale_out_data_size == 1 ? vdupq_n_f32(scale_out_data[0]) : vld1q_f32((const float*)scale_out_data + q * 8 + 4);
454
455 for (int i = 0; i < size; i++)
456 {
457 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
458 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32((intptr + 4)));
459 _v0 = vmulq_f32(_v0, _scale_in0);
460 _v1 = vmulq_f32(_v1, _scale_in1);
461 _v0 = activation_ps(_v0, activation_type, activation_params);
462 _v1 = activation_ps(_v1, activation_type, activation_params);
463 _v0 = vmulq_f32(_v0, _scale_out0);
464 _v1 = vmulq_f32(_v1, _scale_out1);
465 vst1_s8(ptr, float2int8(_v0, _v1));
466
467 intptr += 8;
468 ptr += 8;
469 }
470 }
471 }
472 else
473 {
474 #pragma omp parallel for num_threads(opt.num_threads)
475 for (int q = 0; q < channels; q++)
476 {
477 const int* intptr = bottom_blob.channel(q);
478 signed char* ptr = top_blob.channel(q);
479
480 float32x4_t _scale_in0 = scale_in_data_size == 1 ? vdupq_n_f32(scale_in_data[0]) : vld1q_f32((const float*)scale_in_data + q * 8);
481 float32x4_t _scale_in1 = scale_in_data_size == 1 ? vdupq_n_f32(scale_in_data[0]) : vld1q_f32((const float*)scale_in_data + q * 8 + 4);
482 float32x4_t _scale_out0 = scale_out_data_size == 1 ? vdupq_n_f32(scale_out_data[0]) : vld1q_f32((const float*)scale_out_data + q * 8);
483 float32x4_t _scale_out1 = scale_out_data_size == 1 ? vdupq_n_f32(scale_out_data[0]) : vld1q_f32((const float*)scale_out_data + q * 8 + 4);
484 float32x4_t _bias0 = bias_data_size == 1 ? vdupq_n_f32(bias_data[0]) : vld1q_f32((const float*)bias_data + q * 8);
485 float32x4_t _bias1 = bias_data_size == 1 ? vdupq_n_f32(bias_data[0]) : vld1q_f32((const float*)bias_data + q * 8 + 4);
486
487 for (int i = 0; i < size; i++)
488 {
489 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
490 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32((intptr + 4)));
491 _v0 = vmlaq_f32(_bias0, _v0, _scale_in0);
492 _v1 = vmlaq_f32(_bias1, _v1, _scale_in1);
493 _v0 = activation_ps(_v0, activation_type, activation_params);
494 _v1 = activation_ps(_v1, activation_type, activation_params);
495 _v0 = vmulq_f32(_v0, _scale_out0);
496 _v1 = vmulq_f32(_v1, _scale_out1);
497 vst1_s8(ptr, float2int8(_v0, _v1));
498
499 intptr += 8;
500 ptr += 8;
501 }
502 }
503 }
504 }
505
506 return 0;
507 }
508
509 if (elempack == 4)
510 {
511 if (dims == 1)
512 {
513 int w = bottom_blob.w;
514 int out_elempack = opt.use_packing_layout && w * elempack % 8 == 0 ? 8 : 1;
515 int outw = w * elempack / out_elempack;
516
517 top_blob.create(outw, (size_t)out_elempack, out_elempack, opt.blob_allocator);
518 if (top_blob.empty())
519 return -100;
520
521 if (scale_in_data_size == 1 && scale_out_data_size == 1)
522 {
523 float32x4_t _scale_in = vdupq_n_f32(scale_in_data[0]);
524 float32x4_t _scale_out = vdupq_n_f32(scale_out_data[0]);
525
526 if (bias_data_size == 0)
527 {
528 #pragma omp parallel for num_threads(opt.num_threads)
529 for (int i = 0; i < w; i++)
530 {
531 const int* intptr = (const int*)bottom_blob + i * 4;
532 signed char* ptr = (signed char*)top_blob + i * 4;
533
534 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
535 _v = vmulq_f32(_v, _scale_in);
536 _v = activation_ps(_v, activation_type, activation_params);
537 _v = vmulq_f32(_v, _scale_out);
538 int8x8_t v = float2int8(_v, _v);
539 ptr[0] = vget_lane_s8(v, 0);
540 ptr[1] = vget_lane_s8(v, 1);
541 ptr[2] = vget_lane_s8(v, 2);
542 ptr[3] = vget_lane_s8(v, 3);
543 }
544 }
545 else if (bias_data_size == 1)
546 {
547 float32x4_t _bias = vdupq_n_f32(bias_data[0]);
548
549 #pragma omp parallel for num_threads(opt.num_threads)
550 for (int i = 0; i < w; i++)
551 {
552 const int* intptr = (const int*)bottom_blob + i * 4;
553 signed char* ptr = (signed char*)top_blob + i * 4;
554
555 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
556 _v = vmlaq_f32(_bias, _v, _scale_in);
557 _v = activation_ps(_v, activation_type, activation_params);
558 _v = vmulq_f32(_v, _scale_out);
559 int8x8_t v = float2int8(_v, _v);
560 ptr[0] = vget_lane_s8(v, 0);
561 ptr[1] = vget_lane_s8(v, 1);
562 ptr[2] = vget_lane_s8(v, 2);
563 ptr[3] = vget_lane_s8(v, 3);
564 }
565 }
566 else
567 {
568 #pragma omp parallel for num_threads(opt.num_threads)
569 for (int i = 0; i < w; i++)
570 {
571 const int* intptr = (const int*)bottom_blob + i * 4;
572 signed char* ptr = (signed char*)top_blob + i * 4;
573
574 float32x4_t _bias = vld1q_f32((const float*)bias_data + i * 4);
575 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
576 _v = vmlaq_f32(_bias, _v, _scale_in);
577 _v = activation_ps(_v, activation_type, activation_params);
578 _v = vmulq_f32(_v, _scale_out);
579 int8x8_t v = float2int8(_v, _v);
580 ptr[0] = vget_lane_s8(v, 0);
581 ptr[1] = vget_lane_s8(v, 1);
582 ptr[2] = vget_lane_s8(v, 2);
583 ptr[3] = vget_lane_s8(v, 3);
584 }
585 }
586 }
587 else if (scale_in_data_size == 1 && scale_out_data_size > 1)
588 {
589 float32x4_t _scale_in = vdupq_n_f32(scale_in_data[0]);
590
591 if (bias_data_size == 0)
592 {
593 #pragma omp parallel for num_threads(opt.num_threads)
594 for (int i = 0; i < w; i++)
595 {
596 const int* intptr = (const int*)bottom_blob + i * 4;
597 signed char* ptr = (signed char*)top_blob + i * 4;
598
599 float32x4_t _scale_out = vld1q_f32((const float*)scale_out_data + i * 4);
600 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
601 _v = vmulq_f32(_v, _scale_in);
602 _v = activation_ps(_v, activation_type, activation_params);
603 _v = vmulq_f32(_v, _scale_out);
604 int8x8_t v = float2int8(_v, _v);
605 ptr[0] = vget_lane_s8(v, 0);
606 ptr[1] = vget_lane_s8(v, 1);
607 ptr[2] = vget_lane_s8(v, 2);
608 ptr[3] = vget_lane_s8(v, 3);
609 }
610 }
611 else if (bias_data_size == 1)
612 {
613 float32x4_t _bias = vdupq_n_f32(bias_data[0]);
614
615 #pragma omp parallel for num_threads(opt.num_threads)
616 for (int i = 0; i < w; i++)
617 {
618 const int* intptr = (const int*)bottom_blob + i * 4;
619 signed char* ptr = (signed char*)top_blob + i * 4;
620
621 float32x4_t _scale_out = vld1q_f32((const float*)scale_out_data + i * 4);
622 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
623 _v = vmlaq_f32(_bias, _v, _scale_in);
624 _v = activation_ps(_v, activation_type, activation_params);
625 _v = vmulq_f32(_v, _scale_out);
626 int8x8_t v = float2int8(_v, _v);
627 ptr[0] = vget_lane_s8(v, 0);
628 ptr[1] = vget_lane_s8(v, 1);
629 ptr[2] = vget_lane_s8(v, 2);
630 ptr[3] = vget_lane_s8(v, 3);
631 }
632 }
633 else
634 {
635 #pragma omp parallel for num_threads(opt.num_threads)
636 for (int i = 0; i < w; i++)
637 {
638 const int* intptr = (const int*)bottom_blob + i * 4;
639 signed char* ptr = (signed char*)top_blob + i * 4;
640
641 float32x4_t _scale_out = vld1q_f32((const float*)scale_out_data + i * 4);
642 float32x4_t _bias = vld1q_f32((const float*)bias_data + i * 4);
643 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
644 _v = vmlaq_f32(_bias, _v, _scale_in);
645 _v = activation_ps(_v, activation_type, activation_params);
646 _v = vmulq_f32(_v, _scale_out);
647 int8x8_t v = float2int8(_v, _v);
648 ptr[0] = vget_lane_s8(v, 0);
649 ptr[1] = vget_lane_s8(v, 1);
650 ptr[2] = vget_lane_s8(v, 2);
651 ptr[3] = vget_lane_s8(v, 3);
652 }
653 }
654 }
655 else if (scale_in_data_size > 1 && scale_out_data_size == 1)
656 {
657 float32x4_t _scale_out = vdupq_n_f32(scale_out_data[0]);
658
659 if (bias_data_size == 0)
660 {
661 #pragma omp parallel for num_threads(opt.num_threads)
662 for (int i = 0; i < w; i++)
663 {
664 const int* intptr = (const int*)bottom_blob + i * 4;
665 signed char* ptr = (signed char*)top_blob + i * 4;
666
667 float32x4_t _scale_in = vld1q_f32((const float*)scale_in_data + i * 4);
668 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
669 _v = vmulq_f32(_v, _scale_in);
670 _v = activation_ps(_v, activation_type, activation_params);
671 _v = vmulq_f32(_v, _scale_out);
672 int8x8_t v = float2int8(_v, _v);
673 ptr[0] = vget_lane_s8(v, 0);
674 ptr[1] = vget_lane_s8(v, 1);
675 ptr[2] = vget_lane_s8(v, 2);
676 ptr[3] = vget_lane_s8(v, 3);
677 }
678 }
679 else if (bias_data_size == 1)
680 {
681 float32x4_t _bias = vdupq_n_f32(bias_data[0]);
682
683 #pragma omp parallel for num_threads(opt.num_threads)
684 for (int i = 0; i < w; i++)
685 {
686 const int* intptr = (const int*)bottom_blob + i * 4;
687 signed char* ptr = (signed char*)top_blob + i * 4;
688
689 float32x4_t _scale_in = vld1q_f32((const float*)scale_in_data + i * 4);
690 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
691 _v = vmlaq_f32(_bias, _v, _scale_in);
692 _v = activation_ps(_v, activation_type, activation_params);
693 _v = vmulq_f32(_v, _scale_out);
694 int8x8_t v = float2int8(_v, _v);
695 ptr[0] = vget_lane_s8(v, 0);
696 ptr[1] = vget_lane_s8(v, 1);
697 ptr[2] = vget_lane_s8(v, 2);
698 ptr[3] = vget_lane_s8(v, 3);
699 }
700 }
701 else
702 {
703 #pragma omp parallel for num_threads(opt.num_threads)
704 for (int i = 0; i < w; i++)
705 {
706 const int* intptr = (const int*)bottom_blob + i * 4;
707 signed char* ptr = (signed char*)top_blob + i * 4;
708
709 float32x4_t _scale_in = vld1q_f32((const float*)scale_in_data + i * 4);
710 float32x4_t _bias = vld1q_f32((const float*)bias_data + i * 4);
711 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
712 _v = vmlaq_f32(_bias, _v, _scale_in);
713 _v = activation_ps(_v, activation_type, activation_params);
714 _v = vmulq_f32(_v, _scale_out);
715 int8x8_t v = float2int8(_v, _v);
716 ptr[0] = vget_lane_s8(v, 0);
717 ptr[1] = vget_lane_s8(v, 1);
718 ptr[2] = vget_lane_s8(v, 2);
719 ptr[3] = vget_lane_s8(v, 3);
720 }
721 }
722 }
723 else // if (scale_in_data_size > 1 && scale_out_data_size > 1)
724 {
725 if (bias_data_size == 0)
726 {
727 #pragma omp parallel for num_threads(opt.num_threads)
728 for (int i = 0; i < w; i++)
729 {
730 const int* intptr = (const int*)bottom_blob + i * 4;
731 signed char* ptr = (signed char*)top_blob + i * 4;
732
733 float32x4_t _scale_in = vld1q_f32((const float*)scale_in_data + i * 4);
734 float32x4_t _scale_out = vld1q_f32((const float*)scale_out_data + i * 4);
735 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
736 _v = vmulq_f32(_v, _scale_in);
737 _v = activation_ps(_v, activation_type, activation_params);
738 _v = vmulq_f32(_v, _scale_out);
739 int8x8_t v = float2int8(_v, _v);
740 ptr[0] = vget_lane_s8(v, 0);
741 ptr[1] = vget_lane_s8(v, 1);
742 ptr[2] = vget_lane_s8(v, 2);
743 ptr[3] = vget_lane_s8(v, 3);
744 }
745 }
746 else if (bias_data_size == 1)
747 {
748 float32x4_t _bias = vdupq_n_f32(bias_data[0]);
749
750 #pragma omp parallel for num_threads(opt.num_threads)
751 for (int i = 0; i < w; i++)
752 {
753 const int* intptr = (const int*)bottom_blob + i * 4;
754 signed char* ptr = (signed char*)top_blob + i * 4;
755
756 float32x4_t _scale_in = vld1q_f32((const float*)scale_in_data + i * 4);
757 float32x4_t _scale_out = vld1q_f32((const float*)scale_out_data + i * 4);
758 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
759 _v = vmlaq_f32(_bias, _v, _scale_in);
760 _v = activation_ps(_v, activation_type, activation_params);
761 _v = vmulq_f32(_v, _scale_out);
762 int8x8_t v = float2int8(_v, _v);
763 ptr[0] = vget_lane_s8(v, 0);
764 ptr[1] = vget_lane_s8(v, 1);
765 ptr[2] = vget_lane_s8(v, 2);
766 ptr[3] = vget_lane_s8(v, 3);
767 }
768 }
769 else
770 {
771 #pragma omp parallel for num_threads(opt.num_threads)
772 for (int i = 0; i < w; i++)
773 {
774 const int* intptr = (const int*)bottom_blob + i * 4;
775 signed char* ptr = (signed char*)top_blob + i * 4;
776
777 float32x4_t _scale_in = vld1q_f32((const float*)scale_in_data + i * 4);
778 float32x4_t _scale_out = vld1q_f32((const float*)scale_out_data + i * 4);
779 float32x4_t _bias = vld1q_f32((const float*)bias_data + i * 4);
780 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
781 _v = vmlaq_f32(_bias, _v, _scale_in);
782 _v = activation_ps(_v, activation_type, activation_params);
783 _v = vmulq_f32(_v, _scale_out);
784 int8x8_t v = float2int8(_v, _v);
785 ptr[0] = vget_lane_s8(v, 0);
786 ptr[1] = vget_lane_s8(v, 1);
787 ptr[2] = vget_lane_s8(v, 2);
788 ptr[3] = vget_lane_s8(v, 3);
789 }
790 }
791 }
792 }
793
794 if (dims == 2)
795 {
796 int w = bottom_blob.w;
797 int h = bottom_blob.h;
798 int out_elempack = opt.use_packing_layout && h * elempack % 8 == 0 ? 8 : 1;
799 int outh = h * elempack / out_elempack;
800
801 top_blob.create(w, outh, (size_t)out_elempack, out_elempack, opt.blob_allocator);
802 if (top_blob.empty())
803 return -100;
804
805 if (out_elempack == 8)
806 {
807 if (bias_data_size == 0)
808 {
809 #pragma omp parallel for num_threads(opt.num_threads)
810 for (int i = 0; i < outh; i++)
811 {
812 const int* intptr0 = bottom_blob.row<const int>(i * 2);
813 const int* intptr1 = bottom_blob.row<const int>(i * 2 + 1);
814 signed char* ptr = top_blob.row<signed char>(i);
815
816 float32x4_t _scale_in0 = scale_in_data_size == 1 ? vdupq_n_f32(scale_in_data[0]) : vld1q_f32((const float*)scale_in_data + i * 8);
817 float32x4_t _scale_in1 = scale_in_data_size == 1 ? vdupq_n_f32(scale_in_data[0]) : vld1q_f32((const float*)scale_in_data + i * 8 + 4);
818 float32x4_t _scale_out0 = scale_out_data_size == 1 ? vdupq_n_f32(scale_out_data[0]) : vld1q_f32((const float*)scale_out_data + i * 8);
819 float32x4_t _scale_out1 = scale_out_data_size == 1 ? vdupq_n_f32(scale_out_data[0]) : vld1q_f32((const float*)scale_out_data + i * 8 + 4);
820
821 for (int j = 0; j < w; j++)
822 {
823 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr0));
824 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32(intptr1));
825 _v0 = vmulq_f32(_v0, _scale_in0);
826 _v1 = vmulq_f32(_v1, _scale_in1);
827 _v0 = activation_ps(_v0, activation_type, activation_params);
828 _v1 = activation_ps(_v1, activation_type, activation_params);
829 _v0 = vmulq_f32(_v0, _scale_out0);
830 _v1 = vmulq_f32(_v1, _scale_out1);
831 vst1_s8(ptr, float2int8(_v0, _v1));
832
833 intptr0 += 4;
834 intptr1 += 4;
835 ptr += 8;
836 }
837 }
838 }
839 else
840 {
841 #pragma omp parallel for num_threads(opt.num_threads)
842 for (int i = 0; i < outh; i++)
843 {
844 const int* intptr0 = bottom_blob.row<const int>(i * 2);
845 const int* intptr1 = bottom_blob.row<const int>(i * 2 + 1);
846 signed char* ptr = top_blob.row<signed char>(i);
847
848 float32x4_t _scale_in0 = scale_in_data_size == 1 ? vdupq_n_f32(scale_in_data[0]) : vld1q_f32((const float*)scale_in_data + i * 8);
849 float32x4_t _scale_in1 = scale_in_data_size == 1 ? vdupq_n_f32(scale_in_data[0]) : vld1q_f32((const float*)scale_in_data + i * 8 + 4);
850 float32x4_t _scale_out0 = scale_out_data_size == 1 ? vdupq_n_f32(scale_out_data[0]) : vld1q_f32((const float*)scale_out_data + i * 8);
851 float32x4_t _scale_out1 = scale_out_data_size == 1 ? vdupq_n_f32(scale_out_data[0]) : vld1q_f32((const float*)scale_out_data + i * 8 + 4);
852 float32x4_t _bias0 = bias_data_size == 1 ? vdupq_n_f32(bias_data[0]) : vld1q_f32((const float*)bias_data + i * 8);
853 float32x4_t _bias1 = bias_data_size == 1 ? vdupq_n_f32(bias_data[0]) : vld1q_f32((const float*)bias_data + i * 8 + 4);
854
855 for (int j = 0; j < w; j++)
856 {
857 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr0));
858 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32(intptr1));
859 _v0 = vmlaq_f32(_bias0, _v0, _scale_in0);
860 _v1 = vmlaq_f32(_bias1, _v1, _scale_in1);
861 _v0 = activation_ps(_v0, activation_type, activation_params);
862 _v1 = activation_ps(_v1, activation_type, activation_params);
863 _v0 = vmulq_f32(_v0, _scale_out0);
864 _v1 = vmulq_f32(_v1, _scale_out1);
865 vst1_s8(ptr, float2int8(_v0, _v1));
866
867 intptr0 += 4;
868 intptr1 += 4;
869 ptr += 8;
870 }
871 }
872 }
873 }
874 if (out_elempack == 1)
875 {
876 if (bias_data_size == 0)
877 {
878 #pragma omp parallel for num_threads(opt.num_threads)
879 for (int i = 0; i < h; i++)
880 {
881 const int* intptr = bottom_blob.row<const int>(i);
882 signed char* ptr0 = top_blob.row<signed char>(i * 4);
883 signed char* ptr1 = top_blob.row<signed char>(i * 4 + 1);
884 signed char* ptr2 = top_blob.row<signed char>(i * 4 + 2);
885 signed char* ptr3 = top_blob.row<signed char>(i * 4 + 3);
886
887 float32x4_t _scale_in = scale_in_data_size == 1 ? vdupq_n_f32(scale_in_data[0]) : vld1q_f32((const float*)scale_in_data + i * 4);
888 float32x4_t _scale_out = scale_out_data_size == 1 ? vdupq_n_f32(scale_out_data[0]) : vld1q_f32((const float*)scale_out_data + i * 4);
889
890 for (int j = 0; j < w; j++)
891 {
892 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
893 _v = vmulq_f32(_v, _scale_in);
894 _v = activation_ps(_v, activation_type, activation_params);
895 _v = vmulq_f32(_v, _scale_out);
896 int8x8_t v = float2int8(_v, _v);
897 ptr0[0] = vget_lane_s8(v, 0);
898 ptr1[0] = vget_lane_s8(v, 1);
899 ptr2[0] = vget_lane_s8(v, 2);
900 ptr3[0] = vget_lane_s8(v, 3);
901
902 intptr += 4;
903 ptr0 += 1;
904 ptr1 += 1;
905 ptr2 += 1;
906 ptr3 += 1;
907 }
908 }
909 }
910 else
911 {
912 #pragma omp parallel for num_threads(opt.num_threads)
913 for (int i = 0; i < h; i++)
914 {
915 const int* intptr = bottom_blob.row<const int>(i);
916 signed char* ptr0 = top_blob.row<signed char>(i * 4);
917 signed char* ptr1 = top_blob.row<signed char>(i * 4 + 1);
918 signed char* ptr2 = top_blob.row<signed char>(i * 4 + 2);
919 signed char* ptr3 = top_blob.row<signed char>(i * 4 + 3);
920
921 float32x4_t _scale_in = scale_in_data_size == 1 ? vdupq_n_f32(scale_in_data[0]) : vld1q_f32((const float*)scale_in_data + i * 4);
922 float32x4_t _scale_out = scale_out_data_size == 1 ? vdupq_n_f32(scale_out_data[0]) : vld1q_f32((const float*)scale_out_data + i * 4);
923 float32x4_t _bias = bias_data_size == 1 ? vdupq_n_f32(bias_data[0]) : vld1q_f32((const float*)bias_data + i * 4);
924
925 for (int j = 0; j < w; j++)
926 {
927 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
928 _v = vmlaq_f32(_bias, _v, _scale_in);
929 _v = activation_ps(_v, activation_type, activation_params);
930 _v = vmulq_f32(_v, _scale_out);
931 int8x8_t v = float2int8(_v, _v);
932 ptr0[0] = vget_lane_s8(v, 0);
933 ptr1[0] = vget_lane_s8(v, 1);
934 ptr2[0] = vget_lane_s8(v, 2);
935 ptr3[0] = vget_lane_s8(v, 3);
936
937 intptr += 4;
938 ptr0 += 1;
939 ptr1 += 1;
940 ptr2 += 1;
941 ptr3 += 1;
942 }
943 }
944 }
945 }
946 }
947
948 if (dims == 3)
949 {
950 int w = bottom_blob.w;
951 int h = bottom_blob.h;
952 int channels = bottom_blob.c;
953 int size = w * h;
954 int out_elempack = opt.use_packing_layout && channels * elempack % 8 == 0 ? 8 : 1;
955 int outc = channels * elempack / out_elempack;
956
957 top_blob.create(w, h, outc, (size_t)out_elempack, out_elempack, opt.blob_allocator);
958 if (top_blob.empty())
959 return -100;
960
961 if (activation_type == 1)
962 {
963 requantize_relu_pack4_neon(bottom_blob, top_blob, scale_in_data, scale_out_data, bias_data, opt);
964 return 0;
965 }
966
967 if (activation_type == 2 && activation_params[0] > 0.f)
968 {
969 requantize_leakyrelu_pack4_neon(bottom_blob, top_blob, scale_in_data, scale_out_data, bias_data, activation_params[0], opt);
970 return 0;
971 }
972
973 if (out_elempack == 8)
974 {
975 if (bias_data_size == 0)
976 {
977 #pragma omp parallel for num_threads(opt.num_threads)
978 for (int q = 0; q < outc; q++)
979 {
980 const int* intptr0 = bottom_blob.channel(q * 2);
981 const int* intptr1 = bottom_blob.channel(q * 2 + 1);
982 signed char* ptr = top_blob.channel(q);
983
984 float32x4_t _scale_in0 = scale_in_data_size == 1 ? vdupq_n_f32(scale_in_data[0]) : vld1q_f32((const float*)scale_in_data + q * 8);
985 float32x4_t _scale_in1 = scale_in_data_size == 1 ? vdupq_n_f32(scale_in_data[0]) : vld1q_f32((const float*)scale_in_data + q * 8 + 4);
986 float32x4_t _scale_out0 = scale_out_data_size == 1 ? vdupq_n_f32(scale_out_data[0]) : vld1q_f32((const float*)scale_out_data + q * 8);
987 float32x4_t _scale_out1 = scale_out_data_size == 1 ? vdupq_n_f32(scale_out_data[0]) : vld1q_f32((const float*)scale_out_data + q * 8 + 4);
988
989 for (int i = 0; i < size; i++)
990 {
991 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr0));
992 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32(intptr1));
993 _v0 = vmulq_f32(_v0, _scale_in0);
994 _v1 = vmulq_f32(_v1, _scale_in1);
995 _v0 = activation_ps(_v0, activation_type, activation_params);
996 _v1 = activation_ps(_v1, activation_type, activation_params);
997 _v0 = vmulq_f32(_v0, _scale_out0);
998 _v1 = vmulq_f32(_v1, _scale_out1);
999 vst1_s8(ptr, float2int8(_v0, _v1));
1000
1001 intptr0 += 4;
1002 intptr1 += 4;
1003 ptr += 8;
1004 }
1005 }
1006 }
1007 else
1008 {
1009 #pragma omp parallel for num_threads(opt.num_threads)
1010 for (int q = 0; q < outc; q++)
1011 {
1012 const int* intptr0 = bottom_blob.channel(q * 2);
1013 const int* intptr1 = bottom_blob.channel(q * 2 + 1);
1014 signed char* ptr = top_blob.channel(q);
1015
1016 float32x4_t _scale_in0 = scale_in_data_size == 1 ? vdupq_n_f32(scale_in_data[0]) : vld1q_f32((const float*)scale_in_data + q * 8);
1017 float32x4_t _scale_in1 = scale_in_data_size == 1 ? vdupq_n_f32(scale_in_data[0]) : vld1q_f32((const float*)scale_in_data + q * 8 + 4);
1018 float32x4_t _scale_out0 = scale_out_data_size == 1 ? vdupq_n_f32(scale_out_data[0]) : vld1q_f32((const float*)scale_out_data + q * 8);
1019 float32x4_t _scale_out1 = scale_out_data_size == 1 ? vdupq_n_f32(scale_out_data[0]) : vld1q_f32((const float*)scale_out_data + q * 8 + 4);
1020 float32x4_t _bias0 = bias_data_size == 1 ? vdupq_n_f32(bias_data[0]) : vld1q_f32((const float*)bias_data + q * 8);
1021 float32x4_t _bias1 = bias_data_size == 1 ? vdupq_n_f32(bias_data[0]) : vld1q_f32((const float*)bias_data + q * 8 + 4);
1022
1023 for (int i = 0; i < size; i++)
1024 {
1025 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr0));
1026 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32(intptr1));
1027 _v0 = vmlaq_f32(_bias0, _v0, _scale_in0);
1028 _v1 = vmlaq_f32(_bias1, _v1, _scale_in1);
1029 _v0 = activation_ps(_v0, activation_type, activation_params);
1030 _v1 = activation_ps(_v1, activation_type, activation_params);
1031 _v0 = vmulq_f32(_v0, _scale_out0);
1032 _v1 = vmulq_f32(_v1, _scale_out1);
1033 vst1_s8(ptr, float2int8(_v0, _v1));
1034
1035 intptr0 += 4;
1036 intptr1 += 4;
1037 ptr += 8;
1038 }
1039 }
1040 }
1041 }
1042 if (out_elempack == 1)
1043 {
1044 if (bias_data_size == 0)
1045 {
1046 #pragma omp parallel for num_threads(opt.num_threads)
1047 for (int q = 0; q < channels; q++)
1048 {
1049 const int* intptr = bottom_blob.channel(q);
1050 signed char* ptr0 = top_blob.channel(q * 4);
1051 signed char* ptr1 = top_blob.channel(q * 4 + 1);
1052 signed char* ptr2 = top_blob.channel(q * 4 + 2);
1053 signed char* ptr3 = top_blob.channel(q * 4 + 3);
1054
1055 float32x4_t _scale_in = scale_in_data_size == 1 ? vdupq_n_f32(scale_in_data[0]) : vld1q_f32((const float*)scale_in_data + q * 4);
1056 float32x4_t _scale_out = scale_out_data_size == 1 ? vdupq_n_f32(scale_out_data[0]) : vld1q_f32((const float*)scale_out_data + q * 4);
1057
1058 for (int i = 0; i < size; i++)
1059 {
1060 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
1061 _v = vmulq_f32(_v, _scale_in);
1062 _v = activation_ps(_v, activation_type, activation_params);
1063 _v = vmulq_f32(_v, _scale_out);
1064 int8x8_t v = float2int8(_v, _v);
1065 ptr0[0] = vget_lane_s8(v, 0);
1066 ptr1[0] = vget_lane_s8(v, 1);
1067 ptr2[0] = vget_lane_s8(v, 2);
1068 ptr3[0] = vget_lane_s8(v, 3);
1069
1070 intptr += 4;
1071 ptr0 += 1;
1072 ptr1 += 1;
1073 ptr2 += 1;
1074 ptr3 += 1;
1075 }
1076 }
1077 }
1078 else
1079 {
1080 #pragma omp parallel for num_threads(opt.num_threads)
1081 for (int q = 0; q < channels; q++)
1082 {
1083 const int* intptr = bottom_blob.channel(q);
1084 signed char* ptr0 = top_blob.channel(q * 4);
1085 signed char* ptr1 = top_blob.channel(q * 4 + 1);
1086 signed char* ptr2 = top_blob.channel(q * 4 + 2);
1087 signed char* ptr3 = top_blob.channel(q * 4 + 3);
1088
1089 float32x4_t _scale_in = scale_in_data_size == 1 ? vdupq_n_f32(scale_in_data[0]) : vld1q_f32((const float*)scale_in_data + q * 4);
1090 float32x4_t _scale_out = scale_out_data_size == 1 ? vdupq_n_f32(scale_out_data[0]) : vld1q_f32((const float*)scale_out_data + q * 4);
1091 float32x4_t _bias = bias_data_size == 1 ? vdupq_n_f32(bias_data[0]) : vld1q_f32((const float*)bias_data + q * 4);
1092
1093 for (int i = 0; i < size; i++)
1094 {
1095 float32x4_t _v = vcvtq_f32_s32(vld1q_s32(intptr));
1096 _v = vmlaq_f32(_bias, _v, _scale_in);
1097 _v = activation_ps(_v, activation_type, activation_params);
1098 _v = vmulq_f32(_v, _scale_out);
1099 int8x8_t v = float2int8(_v, _v);
1100 ptr0[0] = vget_lane_s8(v, 0);
1101 ptr1[0] = vget_lane_s8(v, 1);
1102 ptr2[0] = vget_lane_s8(v, 2);
1103 ptr3[0] = vget_lane_s8(v, 3);
1104
1105 intptr += 4;
1106 ptr0 += 1;
1107 ptr1 += 1;
1108 ptr2 += 1;
1109 ptr3 += 1;
1110 }
1111 }
1112 }
1113 }
1114 }
1115
1116 return 0;
1117 }
1118#endif // __ARM_NEON
1119
1120 if (dims == 1)
1121 {
1122 int w = bottom_blob.w;
1123
1124 top_blob.create(w, (size_t)1u, opt.blob_allocator);
1125 if (top_blob.empty())
1126 return -100;
1127
1128 const int* intptr = bottom_blob;
1129 signed char* ptr = top_blob;
1130
1131 if (scale_in_data_size == 1 && scale_out_data_size == 1)
1132 {
1133 const float scale_in = scale_in_data[0];
1134 const float scale_out = scale_out_data[0];
1135
1136 if (bias_data_size == 0)
1137 {
1138 #pragma omp parallel for num_threads(opt.num_threads)
1139 for (int i = 0; i < w; i++)
1140 {
1141 float v = intptr[i] * scale_in;
1142 ptr[i] = float2int8(activation_ss(v, activation_type, activation_params) * scale_out);
1143 }
1144 }
1145 else if (bias_data_size == 1)
1146 {
1147 const float bias = bias_data[0];
1148
1149 #pragma omp parallel for num_threads(opt.num_threads)
1150 for (int i = 0; i < w; i++)
1151 {
1152 float v = intptr[i] * scale_in + bias;
1153 ptr[i] = float2int8(activation_ss(v, activation_type, activation_params) * scale_out);
1154 }
1155 }
1156 else
1157 {
1158 #pragma omp parallel for num_threads(opt.num_threads)
1159 for (int i = 0; i < w; i++)
1160 {
1161 float v = intptr[i] * scale_in + bias_data[i];
1162 ptr[i] = float2int8(activation_ss(v, activation_type, activation_params) * scale_out);
1163 }
1164 }
1165 }
1166 else if (scale_in_data_size == 1 && scale_out_data_size > 1)
1167 {
1168 const float scale_in = scale_in_data[0];
1169
1170 if (bias_data_size == 0)
1171 {
1172 #pragma omp parallel for num_threads(opt.num_threads)
1173 for (int i = 0; i < w; i++)
1174 {
1175 float v = intptr[i] * scale_in;
1176 ptr[i] = float2int8(activation_ss(v, activation_type, activation_params) * scale_out_data[i]);
1177 }
1178 }
1179 else if (bias_data_size == 1)
1180 {
1181 const float bias = bias_data[0];
1182
1183 #pragma omp parallel for num_threads(opt.num_threads)
1184 for (int i = 0; i < w; i++)
1185 {
1186 float v = intptr[i] * scale_in + bias;
1187 ptr[i] = float2int8(activation_ss(v, activation_type, activation_params) * scale_out_data[i]);
1188 }
1189 }
1190 else
1191 {
1192 #pragma omp parallel for num_threads(opt.num_threads)
1193 for (int i = 0; i < w; i++)
1194 {
1195 float v = intptr[i] * scale_in + bias_data[i];
1196 ptr[i] = float2int8(activation_ss(v, activation_type, activation_params) * scale_out_data[i]);
1197 }
1198 }
1199 }
1200 else if (scale_in_data_size > 1 && scale_out_data_size == 1)
1201 {
1202 const float scale_out = scale_out_data[0];
1203
1204 if (bias_data_size == 0)
1205 {
1206 #pragma omp parallel for num_threads(opt.num_threads)
1207 for (int i = 0; i < w; i++)
1208 {
1209 float v = intptr[i] * scale_in_data[i];
1210 ptr[i] = float2int8(activation_ss(v, activation_type, activation_params) * scale_out);
1211 }
1212 }
1213 else if (bias_data_size == 1)
1214 {
1215 const float bias = bias_data[0];
1216
1217 #pragma omp parallel for num_threads(opt.num_threads)
1218 for (int i = 0; i < w; i++)
1219 {
1220 float v = intptr[i] * scale_in_data[i] + bias;
1221 ptr[i] = float2int8(activation_ss(v, activation_type, activation_params) * scale_out);
1222 }
1223 }
1224 else
1225 {
1226 #pragma omp parallel for num_threads(opt.num_threads)
1227 for (int i = 0; i < w; i++)
1228 {
1229 float v = intptr[i] * scale_in_data[i] + bias_data[i];
1230 ptr[i] = float2int8(activation_ss(v, activation_type, activation_params) * scale_out);
1231 }
1232 }
1233 }
1234 else // if (scale_in_data_size > 1 && scale_out_data_size > 1)
1235 {
1236 if (bias_data_size == 0)
1237 {
1238 #pragma omp parallel for num_threads(opt.num_threads)
1239 for (int i = 0; i < w; i++)
1240 {
1241 float v = intptr[i] * scale_in_data[i];
1242 ptr[i] = float2int8(activation_ss(v, activation_type, activation_params) * scale_out_data[i]);
1243 }
1244 }
1245 else if (bias_data_size == 1)
1246 {
1247 const float bias = bias_data[0];
1248
1249 #pragma omp parallel for num_threads(opt.num_threads)
1250 for (int i = 0; i < w; i++)
1251 {
1252 float v = intptr[i] * scale_in_data[i] + bias;
1253 ptr[i] = float2int8(activation_ss(v, activation_type, activation_params) * scale_out_data[i]);
1254 }
1255 }
1256 else
1257 {
1258 #pragma omp parallel for num_threads(opt.num_threads)
1259 for (int i = 0; i < w; i++)
1260 {
1261 float v = intptr[i] * scale_in_data[i] + bias_data[i];
1262 ptr[i] = float2int8(activation_ss(v, activation_type, activation_params) * scale_out_data[i]);
1263 }
1264 }
1265 }
1266 }
1267
1268 if (dims == 2)
1269 {
1270 int w = bottom_blob.w;
1271 int h = bottom_blob.h;
1272
1273 top_blob.create(w, h, (size_t)1u, opt.blob_allocator);
1274 if (top_blob.empty())
1275 return -100;
1276
1277 if (bias_data_size == 0)
1278 {
1279 #pragma omp parallel for num_threads(opt.num_threads)
1280 for (int i = 0; i < h; i++)
1281 {
1282 const int* intptr = bottom_blob.row<const int>(i);
1283 signed char* ptr = top_blob.row<signed char>(i);
1284
1285 const float scale_in = scale_in_data_size == 1 ? scale_in_data[0] : scale_in_data[i];
1286 const float scale_out = scale_out_data_size == 1 ? scale_out_data[0] : scale_out_data[i];
1287
1288 for (int j = 0; j < w; j++)
1289 {
1290 float v = intptr[j] * scale_in;
1291 ptr[j] = float2int8(activation_ss(v, activation_type, activation_params) * scale_out);
1292 }
1293 }
1294 }
1295 else
1296 {
1297 #pragma omp parallel for num_threads(opt.num_threads)
1298 for (int i = 0; i < h; i++)
1299 {
1300 const int* intptr = bottom_blob.row<const int>(i);
1301 signed char* ptr = top_blob.row<signed char>(i);
1302
1303 const float scale_in = scale_in_data_size == 1 ? scale_in_data[0] : scale_in_data[i];
1304 const float scale_out = scale_out_data_size == 1 ? scale_out_data[0] : scale_out_data[i];
1305 const float bias = bias_data_size == 1 ? bias_data[0] : bias_data[i];
1306
1307 for (int j = 0; j < w; j++)
1308 {
1309 float v = intptr[j] * scale_in + bias;
1310 ptr[j] = float2int8(activation_ss(v, activation_type, activation_params) * scale_out);
1311 }
1312 }
1313 }
1314 }
1315
1316 if (dims == 3)
1317 {
1318 int w = bottom_blob.w;
1319 int h = bottom_blob.h;
1320 int channels = bottom_blob.c;
1321 int size = w * h;
1322
1323 top_blob.create(w, h, channels, (size_t)1u, opt.blob_allocator);
1324 if (top_blob.empty())
1325 return -100;
1326
1327 if (bias_data_size == 0)
1328 {
1329 #pragma omp parallel for num_threads(opt.num_threads)
1330 for (int q = 0; q < channels; q++)
1331 {
1332 const int* intptr = bottom_blob.channel(q);
1333 signed char* ptr = top_blob.channel(q);
1334
1335 const float scale_in = scale_in_data_size == 1 ? scale_in_data[0] : scale_in_data[q];
1336 const float scale_out = scale_out_data_size == 1 ? scale_out_data[0] : scale_out_data[q];
1337
1338 for (int i = 0; i < size; i++)
1339 {
1340 float v = intptr[i] * scale_in;
1341 ptr[i] = float2int8(activation_ss(v, activation_type, activation_params) * scale_out);
1342 }
1343 }
1344 }
1345 else
1346 {
1347 #pragma omp parallel for num_threads(opt.num_threads)
1348 for (int q = 0; q < channels; q++)
1349 {
1350 const int* intptr = bottom_blob.channel(q);
1351 signed char* ptr = top_blob.channel(q);
1352
1353 const float scale_in = scale_in_data_size == 1 ? scale_in_data[0] : scale_in_data[q];
1354 const float scale_out = scale_out_data_size == 1 ? scale_out_data[0] : scale_out_data[q];
1355 const float bias = bias_data_size == 1 ? bias_data[0] : bias_data[q];
1356
1357 for (int i = 0; i < size; i++)
1358 {
1359 float v = intptr[i] * scale_in + bias;
1360 ptr[i] = float2int8(activation_ss(v, activation_type, activation_params) * scale_out);
1361 }
1362 }
1363 }
1364 }
1365
1366 return 0;
1367}
1368
1369} // namespace ncnn