Elffuss BraidHub

kikocis/ncnn public

master · 13.2 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_relu_pack4_lsx(const Mat& bottom_blob, Mat& top_blob, const Mat& scale_in_data, const Mat& scale_out_data, const Mat& bias_data, 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(relu(v * scale_in) * scale_out)
29 // int8_relu(v * (scale_in * scale_out))
30
31 // int8(relu(v * scale_in + bias) * scale_out)
32 // int8_relu(v * (scale_in * scale_out) + (bias * scale_out))
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
53 int i = 0;
54 for (; i + 3 < size; i += 4)
55 {
56 __builtin_prefetch(intptr0 + 64);
57 __builtin_prefetch(intptr1 + 64);
58 __m128 _v00 = __lsx_vffint_s_w(__lsx_vld(intptr0, 0));
59 __m128 _v01 = __lsx_vffint_s_w(__lsx_vld(intptr0 + 4, 0));
60 __m128 _v02 = __lsx_vffint_s_w(__lsx_vld(intptr0 + 8, 0));
61 __m128 _v03 = __lsx_vffint_s_w(__lsx_vld(intptr0 + 12, 0));
62 __m128 _v10 = __lsx_vffint_s_w(__lsx_vld(intptr1, 0));
63 __m128 _v11 = __lsx_vffint_s_w(__lsx_vld(intptr1 + 4, 0));
64 __m128 _v12 = __lsx_vffint_s_w(__lsx_vld(intptr1 + 8, 0));
65 __m128 _v13 = __lsx_vffint_s_w(__lsx_vld(intptr1 + 12, 0));
66 _v00 = __lsx_vfmul_s(_v00, _scale0);
67 _v01 = __lsx_vfmul_s(_v01, _scale0);
68 _v02 = __lsx_vfmul_s(_v02, _scale0);
69 _v03 = __lsx_vfmul_s(_v03, _scale0);
70 _v10 = __lsx_vfmul_s(_v10, _scale1);
71 _v11 = __lsx_vfmul_s(_v11, _scale1);
72 _v12 = __lsx_vfmul_s(_v12, _scale1);
73 _v13 = __lsx_vfmul_s(_v13, _scale1);
74 *((int64_t*)ptr) = float2int8relu(_v00, _v10);
75 *((int64_t*)(ptr + 8)) = float2int8relu(_v01, _v11);
76 *((int64_t*)(ptr + 16)) = float2int8relu(_v02, _v12);
77 *((int64_t*)(ptr + 24)) = float2int8relu(_v03, _v13);
78
79 intptr0 += 16;
80 intptr1 += 16;
81 ptr += 32;
82 }
83 for (; i < size; i++)
84 {
85 __builtin_prefetch(intptr0 + 16);
86 __builtin_prefetch(intptr1 + 16);
87 __m128 _v0 = __lsx_vffint_s_w(__lsx_vld(intptr0, 0));
88 __m128 _v1 = __lsx_vffint_s_w(__lsx_vld(intptr1, 0));
89 _v0 = __lsx_vfmul_s(_v0, _scale0);
90 _v1 = __lsx_vfmul_s(_v1, _scale1);
91 *((int64_t*)ptr) = float2int8relu(_v0, _v1);
92
93 intptr0 += 4;
94 intptr1 += 4;
95 ptr += 8;
96 }
97 }
98 }
99 else
100 {
101 #pragma omp parallel for num_threads(opt.num_threads)
102 for (int q = 0; q < outc; q++)
103 {
104 const int* intptr0 = bottom_blob.channel(q * 2);
105 const int* intptr1 = bottom_blob.channel(q * 2 + 1);
106 signed char* ptr = top_blob.channel(q);
107
108 __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);
109 __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);
110 __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);
111 __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);
112 __m128 _bias0 = bias_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(bias_data[0]) : (__m128)__lsx_vld((const float*)bias_data + q * 8, 0);
113 __m128 _bias1 = bias_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(bias_data[0]) : (__m128)__lsx_vld((const float*)bias_data + q * 8 + 4, 0);
114
115 __m128 _scale0 = __lsx_vfmul_s(_scale_in0, _scale_out0);
116 __m128 _scale1 = __lsx_vfmul_s(_scale_in1, _scale_out1);
117 _bias0 = __lsx_vfmul_s(_bias0, _scale_out0);
118 _bias1 = __lsx_vfmul_s(_bias1, _scale_out1);
119
120 int i = 0;
121 for (; i + 3 < size; i += 4)
122 {
123 __builtin_prefetch(intptr0 + 64);
124 __builtin_prefetch(intptr1 + 64);
125 __m128 _v00 = __lsx_vffint_s_w(__lsx_vld(intptr0, 0));
126 __m128 _v01 = __lsx_vffint_s_w(__lsx_vld(intptr0 + 4, 0));
127 __m128 _v02 = __lsx_vffint_s_w(__lsx_vld(intptr0 + 8, 0));
128 __m128 _v03 = __lsx_vffint_s_w(__lsx_vld(intptr0 + 12, 0));
129 __m128 _v10 = __lsx_vffint_s_w(__lsx_vld(intptr1, 0));
130 __m128 _v11 = __lsx_vffint_s_w(__lsx_vld(intptr1 + 4, 0));
131 __m128 _v12 = __lsx_vffint_s_w(__lsx_vld(intptr1 + 8, 0));
132 __m128 _v13 = __lsx_vffint_s_w(__lsx_vld(intptr1 + 12, 0));
133 _v00 = __lsx_vfmadd_s(_scale0, _v00, _bias0);
134 _v01 = __lsx_vfmadd_s(_scale0, _v01, _bias0);
135 _v02 = __lsx_vfmadd_s(_scale0, _v02, _bias0);
136 _v03 = __lsx_vfmadd_s(_scale0, _v03, _bias0);
137 _v10 = __lsx_vfmadd_s(_scale1, _v10, _bias1);
138 _v11 = __lsx_vfmadd_s(_scale1, _v11, _bias1);
139 _v12 = __lsx_vfmadd_s(_scale1, _v12, _bias1);
140 _v13 = __lsx_vfmadd_s(_scale1, _v13, _bias1);
141 *((int64_t*)ptr) = float2int8relu(_v00, _v10);
142 *((int64_t*)(ptr + 8)) = float2int8relu(_v01, _v11);
143 *((int64_t*)(ptr + 16)) = float2int8relu(_v02, _v12);
144 *((int64_t*)(ptr + 24)) = float2int8relu(_v03, _v13);
145
146 intptr0 += 16;
147 intptr1 += 16;
148 ptr += 32;
149 }
150 for (; i + 1 < size; i += 2)
151 {
152 __builtin_prefetch(intptr0 + 32);
153 __builtin_prefetch(intptr1 + 32);
154 __m128 _v00 = __lsx_vffint_s_w(__lsx_vld(intptr0, 0));
155 __m128 _v01 = __lsx_vffint_s_w(__lsx_vld(intptr0 + 4, 0));
156 __m128 _v10 = __lsx_vffint_s_w(__lsx_vld(intptr1, 0));
157 __m128 _v11 = __lsx_vffint_s_w(__lsx_vld(intptr1 + 4, 0));
158 _v00 = __lsx_vfmadd_s(_scale0, _v00, _bias0);
159 _v01 = __lsx_vfmadd_s(_scale0, _v01, _bias0);
160 _v10 = __lsx_vfmadd_s(_scale1, _v10, _bias1);
161 _v11 = __lsx_vfmadd_s(_scale1, _v11, _bias1);
162 *((int64_t*)ptr) = float2int8relu(_v00, _v10);
163 *((int64_t*)(ptr + 8)) = float2int8relu(_v01, _v11);
164
165 intptr0 += 8;
166 intptr1 += 8;
167 ptr += 16;
168 }
169 for (; i < size; i++)
170 {
171 __builtin_prefetch(intptr0 + 16);
172 __builtin_prefetch(intptr1 + 16);
173 __m128 _v0 = __lsx_vffint_s_w(__lsx_vld(intptr0, 0));
174 __m128 _v1 = __lsx_vffint_s_w(__lsx_vld(intptr1, 0));
175 _v0 = __lsx_vfmadd_s(_scale0, _v0, _bias0);
176 _v1 = __lsx_vfmadd_s(_scale1, _v1, _bias1);
177 *((int64_t*)ptr) = float2int8relu(_v0, _v1);
178
179 intptr0 += 4;
180 intptr1 += 4;
181 ptr += 8;
182 }
183 }
184 }
185 }
186 if (out_elempack == 1)
187 {
188 if (bias_data_size == 0)
189 {
190 #pragma omp parallel for num_threads(opt.num_threads)
191 for (int q = 0; q < channels; q++)
192 {
193 const int* intptr = bottom_blob.channel(q);
194 signed char* ptr0 = top_blob.channel(q * 4);
195 signed char* ptr1 = top_blob.channel(q * 4 + 1);
196 signed char* ptr2 = top_blob.channel(q * 4 + 2);
197 signed char* ptr3 = top_blob.channel(q * 4 + 3);
198 signed char* vp;
199
200 __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);
201 __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);
202
203 __m128 _scale = __lsx_vfmul_s(_scale_in, _scale_out);
204
205 int i = 0;
206 for (; i < size; i++)
207 {
208 __builtin_prefetch(intptr + 16);
209 __m128 _v = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
210 _v = __lsx_vfmul_s(_v, _scale);
211 __m128i v = float2int8relu(_v);
212 vp = (signed char*)&v;
213 ptr0[0] = vp[0];
214 ptr1[0] = vp[1];
215 ptr2[0] = vp[2];
216 ptr3[0] = vp[3];
217
218 intptr += 4;
219 ptr0 += 1;
220 ptr1 += 1;
221 ptr2 += 1;
222 ptr3 += 1;
223 }
224 }
225 }
226 else
227 {
228 #pragma omp parallel for num_threads(opt.num_threads)
229 for (int q = 0; q < channels; q++)
230 {
231 const int* intptr = bottom_blob.channel(q);
232 signed char* ptr0 = top_blob.channel(q * 4);
233 signed char* ptr1 = top_blob.channel(q * 4 + 1);
234 signed char* ptr2 = top_blob.channel(q * 4 + 2);
235 signed char* ptr3 = top_blob.channel(q * 4 + 3);
236 signed char* vp;
237
238 __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);
239 __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);
240 __m128 _bias = bias_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(bias_data[0]) : (__m128)__lsx_vld((const float*)bias_data + q * 4, 0);
241
242 __m128 _scale = __lsx_vfmul_s(_scale_in, _scale_out);
243 _bias = __lsx_vfmul_s(_bias, _scale_out);
244
245 int i = 0;
246 for (; i < size; i++)
247 {
248 __builtin_prefetch(intptr + 16);
249 __m128 _v = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
250 _v = __lsx_vfmadd_s(_scale, _v, _bias);
251 __m128i v = float2int8relu(_v);
252 vp = (signed char*)&v;
253 ptr0[0] = vp[0];
254 ptr1[0] = vp[1];
255 ptr2[0] = vp[2];
256 ptr3[0] = vp[3];
257
258 intptr += 4;
259 ptr0 += 1;
260 ptr1 += 1;
261 ptr2 += 1;
262 ptr3 += 1;
263 }
264 }
265 }
266 }
267}