Elffuss BraidHub

kikocis/ncnn public

master · 13.7 KiBRawPermalink
1// yala is pleased to support the open source community by making ncnn available.
2//
3//
4// Copyright (C) 2022 yala <zhaojunchao@loongson.cn>;<junchao82@qq.com>. All rights reserved.
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
15static void requantize_leakyrelu_pack4_lsx(const Mat& bottom_blob, Mat& top_blob, const Mat& scale_in_data, const Mat& scale_out_data, const Mat& bias_data, float slope, const Option& opt)
16{
17 int w = bottom_blob.w;
18 int h = bottom_blob.h;
19 int channels = bottom_blob.c;
20 int size = w * h;
21 int outc = top_blob.c;
22 int out_elempack = top_blob.elempack;
23
24 int scale_in_data_size = scale_in_data.w;
25 int scale_out_data_size = scale_out_data.w;
26 int bias_data_size = bias_data.w;
27
28 // int8(leakyrelu(v * scale_in, slope) * scale_out)
29 // int8_leakyrelu(v * (scale_in * scale_out), slope)
30
31 // int8(leakyrelu(v * scale_in + bias, slope) * scale_out)
32 // int8_leakyrelu(v * (scale_in * scale_out) + (bias * scale_out), slope)
33
34 if (out_elempack == 8)
35 {
36 if (bias_data_size == 0)
37 {
38 #pragma omp parallel for num_threads(opt.num_threads)
39 for (int q = 0; q < outc; q++)
40 {
41 const int* intptr0 = bottom_blob.channel(q * 2);
42 const int* intptr1 = bottom_blob.channel(q * 2 + 1);
43 signed char* ptr = top_blob.channel(q);
44
45 __m128 _scale_in0 = scale_in_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(scale_in_data[0]) : (__m128)__lsx_vld((const float*)scale_in_data + q * 8, 0);
46 __m128 _scale_in1 = scale_in_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(scale_in_data[0]) : (__m128)__lsx_vld((const float*)scale_in_data + q * 8 + 4, 0);
47 __m128 _scale_out0 = scale_out_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(scale_out_data[0]) : (__m128)__lsx_vld((const float*)scale_out_data + q * 8, 0);
48 __m128 _scale_out1 = scale_out_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(scale_out_data[0]) : (__m128)__lsx_vld((const float*)scale_out_data + q * 8 + 4, 0);
49
50 __m128 _scale0 = __lsx_vfmul_s(_scale_in0, _scale_out0);
51 __m128 _scale1 = __lsx_vfmul_s(_scale_in1, _scale_out1);
52 __m128 _slope = (__m128)__lsx_vreplfr2vr_s(slope);
53
54 int i = 0;
55 for (; i + 3 < size; i += 4)
56 {
57 __builtin_prefetch(intptr0 + 64);
58 __builtin_prefetch(intptr1 + 64);
59 __m128 _v00 = __lsx_vffint_s_w(__lsx_vld(intptr0, 0));
60 __m128 _v01 = __lsx_vffint_s_w(__lsx_vld(intptr0 + 4, 0));
61 __m128 _v02 = __lsx_vffint_s_w(__lsx_vld(intptr0 + 8, 0));
62 __m128 _v03 = __lsx_vffint_s_w(__lsx_vld(intptr0 + 12, 0));
63 __m128 _v10 = __lsx_vffint_s_w(__lsx_vld(intptr1, 0));
64 __m128 _v11 = __lsx_vffint_s_w(__lsx_vld(intptr1 + 4, 0));
65 __m128 _v12 = __lsx_vffint_s_w(__lsx_vld(intptr1 + 8, 0));
66 __m128 _v13 = __lsx_vffint_s_w(__lsx_vld(intptr1 + 12, 0));
67 _v00 = __lsx_vfmul_s(_v00, _scale0);
68 _v01 = __lsx_vfmul_s(_v01, _scale0);
69 _v02 = __lsx_vfmul_s(_v02, _scale0);
70 _v03 = __lsx_vfmul_s(_v03, _scale0);
71 _v10 = __lsx_vfmul_s(_v10, _scale1);
72 _v11 = __lsx_vfmul_s(_v11, _scale1);
73 _v12 = __lsx_vfmul_s(_v12, _scale1);
74 _v13 = __lsx_vfmul_s(_v13, _scale1);
75 *((int64_t*)ptr) = float2int8leakyrelu(_v00, _v10, _slope);
76 *((int64_t*)(ptr + 8)) = float2int8leakyrelu(_v01, _v11, _slope);
77 *((int64_t*)(ptr + 16)) = float2int8leakyrelu(_v02, _v12, _slope);
78 *((int64_t*)(ptr + 24)) = float2int8leakyrelu(_v03, _v13, _slope);
79
80 intptr0 += 16;
81 intptr1 += 16;
82 ptr += 32;
83 }
84 for (; i < size; i++)
85 {
86 __builtin_prefetch(intptr0 + 16);
87 __builtin_prefetch(intptr1 + 16);
88 __m128 _v0 = __lsx_vffint_s_w(__lsx_vld(intptr0, 0));
89 __m128 _v1 = __lsx_vffint_s_w(__lsx_vld(intptr1, 0));
90 _v0 = __lsx_vfmul_s(_v0, _scale0);
91 _v1 = __lsx_vfmul_s(_v1, _scale1);
92 *((int64_t*)ptr) = float2int8leakyrelu(_v0, _v1, _slope);
93
94 intptr0 += 4;
95 intptr1 += 4;
96 ptr += 8;
97 }
98 }
99 }
100 else
101 {
102 #pragma omp parallel for num_threads(opt.num_threads)
103 for (int q = 0; q < outc; q++)
104 {
105 const int* intptr0 = bottom_blob.channel(q * 2);
106 const int* intptr1 = bottom_blob.channel(q * 2 + 1);
107 signed char* ptr = top_blob.channel(q);
108
109 __m128 _scale_in0 = scale_in_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(scale_in_data[0]) : (__m128)__lsx_vld((const float*)scale_in_data + q * 8, 0);
110 __m128 _scale_in1 = scale_in_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(scale_in_data[0]) : (__m128)__lsx_vld((const float*)scale_in_data + q * 8 + 4, 0);
111 __m128 _scale_out0 = scale_out_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(scale_out_data[0]) : (__m128)__lsx_vld((const float*)scale_out_data + q * 8, 0);
112 __m128 _scale_out1 = scale_out_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(scale_out_data[0]) : (__m128)__lsx_vld((const float*)scale_out_data + q * 8 + 4, 0);
113 __m128 _bias0 = bias_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(bias_data[0]) : (__m128)__lsx_vld((const float*)bias_data + q * 8, 0);
114 __m128 _bias1 = bias_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(bias_data[0]) : (__m128)__lsx_vld((const float*)bias_data + q * 8 + 4, 0);
115
116 __m128 _scale0 = __lsx_vfmul_s(_scale_in0, _scale_out0);
117 __m128 _scale1 = __lsx_vfmul_s(_scale_in1, _scale_out1);
118 _bias0 = __lsx_vfmul_s(_bias0, _scale_out0);
119 _bias1 = __lsx_vfmul_s(_bias1, _scale_out1);
120 __m128 _slope = (__m128)__lsx_vreplfr2vr_s(slope);
121
122 int i = 0;
123 for (; i + 3 < size; i += 4)
124 {
125 __builtin_prefetch(intptr0 + 64);
126 __builtin_prefetch(intptr1 + 64);
127 __m128 _v00 = __lsx_vffint_s_w(__lsx_vld(intptr0, 0));
128 __m128 _v01 = __lsx_vffint_s_w(__lsx_vld(intptr0 + 4, 0));
129 __m128 _v02 = __lsx_vffint_s_w(__lsx_vld(intptr0 + 8, 0));
130 __m128 _v03 = __lsx_vffint_s_w(__lsx_vld(intptr0 + 12, 0));
131 __m128 _v10 = __lsx_vffint_s_w(__lsx_vld(intptr1, 0));
132 __m128 _v11 = __lsx_vffint_s_w(__lsx_vld(intptr1 + 4, 0));
133 __m128 _v12 = __lsx_vffint_s_w(__lsx_vld(intptr1 + 8, 0));
134 __m128 _v13 = __lsx_vffint_s_w(__lsx_vld(intptr1 + 12, 0));
135 _v00 = __lsx_vfmadd_s(_scale0, _v00, _bias0);
136 _v01 = __lsx_vfmadd_s(_scale0, _v01, _bias0);
137 _v02 = __lsx_vfmadd_s(_scale0, _v02, _bias0);
138 _v03 = __lsx_vfmadd_s(_scale0, _v03, _bias0);
139 _v10 = __lsx_vfmadd_s(_scale1, _v10, _bias1);
140 _v11 = __lsx_vfmadd_s(_scale1, _v11, _bias1);
141 _v12 = __lsx_vfmadd_s(_scale1, _v12, _bias1);
142 _v13 = __lsx_vfmadd_s(_scale1, _v13, _bias1);
143 *((int64_t*)ptr) = float2int8leakyrelu(_v00, _v10, _slope);
144 *((int64_t*)(ptr + 8)) = float2int8leakyrelu(_v01, _v11, _slope);
145 *((int64_t*)(ptr + 16)) = float2int8leakyrelu(_v02, _v12, _slope);
146 *((int64_t*)(ptr + 24)) = float2int8leakyrelu(_v03, _v13, _slope);
147
148 intptr0 += 16;
149 intptr1 += 16;
150 ptr += 32;
151 }
152 for (; i + 1 < size; i += 2)
153 {
154 __builtin_prefetch(intptr0 + 32);
155 __builtin_prefetch(intptr1 + 32);
156 __m128 _v00 = __lsx_vffint_s_w(__lsx_vld(intptr0, 0));
157 __m128 _v01 = __lsx_vffint_s_w(__lsx_vld(intptr0 + 4, 0));
158 __m128 _v10 = __lsx_vffint_s_w(__lsx_vld(intptr1, 0));
159 __m128 _v11 = __lsx_vffint_s_w(__lsx_vld(intptr1 + 4, 0));
160 _v00 = __lsx_vfmadd_s(_scale0, _v00, _bias0);
161 _v01 = __lsx_vfmadd_s(_scale0, _v01, _bias0);
162 _v10 = __lsx_vfmadd_s(_scale1, _v10, _bias1);
163 _v11 = __lsx_vfmadd_s(_scale1, _v11, _bias1);
164 *((int64_t*)ptr) = float2int8leakyrelu(_v00, _v10, _slope);
165 *((int64_t*)(ptr + 8)) = float2int8leakyrelu(_v01, _v11, _slope);
166
167 intptr0 += 8;
168 intptr1 += 8;
169 ptr += 16;
170 }
171 for (; i < size; i++)
172 {
173 __builtin_prefetch(intptr0 + 16);
174 __builtin_prefetch(intptr1 + 16);
175 __m128 _v0 = __lsx_vffint_s_w(__lsx_vld(intptr0, 0));
176 __m128 _v1 = __lsx_vffint_s_w(__lsx_vld(intptr1, 0));
177 _v0 = __lsx_vfmadd_s(_scale0, _v0, _bias0);
178 _v1 = __lsx_vfmadd_s(_scale1, _v1, _bias1);
179 *((int64_t*)ptr) = float2int8leakyrelu(_v0, _v1, _slope);
180
181 intptr0 += 4;
182 intptr1 += 4;
183 ptr += 8;
184 }
185 }
186 }
187 }
188 if (out_elempack == 1)
189 {
190 if (bias_data_size == 0)
191 {
192 #pragma omp parallel for num_threads(opt.num_threads)
193 for (int q = 0; q < channels; q++)
194 {
195 const int* intptr = bottom_blob.channel(q);
196 signed char* ptr0 = top_blob.channel(q * 4);
197 signed char* ptr1 = top_blob.channel(q * 4 + 1);
198 signed char* ptr2 = top_blob.channel(q * 4 + 2);
199 signed char* ptr3 = top_blob.channel(q * 4 + 3);
200 signed char* vp;
201
202 __m128 _scale_in = scale_in_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(scale_in_data[0]) : (__m128)__lsx_vld((const float*)scale_in_data + q * 4, 0);
203 __m128 _scale_out = scale_out_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(scale_out_data[0]) : (__m128)__lsx_vld((const float*)scale_out_data + q * 4, 0);
204
205 __m128 _scale = __lsx_vfmul_s(_scale_in, _scale_out);
206 __m128 _slope = (__m128)__lsx_vreplfr2vr_s(slope);
207
208 int i = 0;
209 for (; i < size; i++)
210 {
211 __builtin_prefetch(intptr + 16);
212 __m128 _v = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
213 _v = __lsx_vfmul_s(_v, _scale);
214 __m128i v = float2int8leakyrelu(_v, _slope);
215 vp = (signed char*)&v;
216 ptr0[0] = vp[0];
217 ptr1[0] = vp[1];
218 ptr2[0] = vp[2];
219 ptr3[0] = vp[3];
220
221 intptr += 4;
222 ptr0 += 1;
223 ptr1 += 1;
224 ptr2 += 1;
225 ptr3 += 1;
226 }
227 }
228 }
229 else
230 {
231 #pragma omp parallel for num_threads(opt.num_threads)
232 for (int q = 0; q < channels; q++)
233 {
234 const int* intptr = bottom_blob.channel(q);
235 signed char* ptr0 = top_blob.channel(q * 4);
236 signed char* ptr1 = top_blob.channel(q * 4 + 1);
237 signed char* ptr2 = top_blob.channel(q * 4 + 2);
238 signed char* ptr3 = top_blob.channel(q * 4 + 3);
239 signed char* vp;
240
241 __m128 _scale_in = scale_in_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(scale_in_data[0]) : (__m128)__lsx_vld((const float*)scale_in_data + q * 4, 0);
242 __m128 _scale_out = scale_out_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(scale_out_data[0]) : (__m128)__lsx_vld((const float*)scale_out_data + q * 4, 0);
243 __m128 _bias = bias_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(bias_data[0]) : (__m128)__lsx_vld((const float*)bias_data + q * 4, 0);
244
245 __m128 _scale = __lsx_vfmul_s(_scale_in, _scale_out);
246 _bias = __lsx_vfmul_s(_bias, _scale_out);
247 __m128 _slope = (__m128)__lsx_vreplfr2vr_s(slope);
248
249 int i = 0;
250 for (; i < size; i++)
251 {
252 __builtin_prefetch(intptr + 16);
253 __m128 _v = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
254 _v = __lsx_vfmadd_s(_scale, _v, _bias);
255 __m128i v = float2int8leakyrelu(_v, _slope);
256 vp = (signed char*)&v;
257 ptr0[0] = vp[0];
258 ptr1[0] = vp[1];
259 ptr2[0] = vp[2];
260 ptr3[0] = vp[3];
261
262 intptr += 4;
263 ptr0 += 1;
264 ptr1 += 1;
265 ptr2 += 1;
266 ptr3 += 1;
267 }
268 }
269 }
270 }
271}