Elffuss BraidHub

kikocis/ncnn public

master · 9.6 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_pack8_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
22 int scale_in_data_size = scale_in_data.w;
23 int scale_out_data_size = scale_out_data.w;
24 int bias_data_size = bias_data.w;
25
26 // int8(leakyrelu(v * scale_in, slope) * scale_out)
27 // int8_leakyrelu(v * (scale_in * scale_out), slope)
28
29 // int8(leakyrelu(v * scale_in + bias, slope) * scale_out)
30 // int8_leakyrelu(v * (scale_in * scale_out) + (bias * scale_out), slope)
31
32 if (bias_data_size == 0)
33 {
34 #pragma omp parallel for num_threads(opt.num_threads)
35 for (int q = 0; q < channels; q++)
36 {
37 const int* intptr = bottom_blob.channel(q);
38 signed char* ptr = top_blob.channel(q);
39
40 __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);
41 __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);
42 __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);
43 __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);
44
45 __m128 _scale0 = __lsx_vfmul_s(_scale_in0, _scale_out0);
46 __m128 _scale1 = __lsx_vfmul_s(_scale_in1, _scale_out1);
47 __m128 _slope = (__m128)__lsx_vreplfr2vr_s(slope);
48
49 int i = 0;
50 for (; i + 3 < size; i += 4)
51 {
52 __builtin_prefetch(intptr + 128);
53 __m128 _v0 = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
54 __m128 _v1 = __lsx_vffint_s_w(__lsx_vld(intptr + 4, 0));
55 __m128 _v2 = __lsx_vffint_s_w(__lsx_vld(intptr + 8, 0));
56 __m128 _v3 = __lsx_vffint_s_w(__lsx_vld(intptr + 12, 0));
57 __m128 _v4 = __lsx_vffint_s_w(__lsx_vld(intptr + 16, 0));
58 __m128 _v5 = __lsx_vffint_s_w(__lsx_vld(intptr + 20, 0));
59 __m128 _v6 = __lsx_vffint_s_w(__lsx_vld(intptr + 24, 0));
60 __m128 _v7 = __lsx_vffint_s_w(__lsx_vld(intptr + 28, 0));
61 _v0 = __lsx_vfmul_s(_v0, _scale0);
62 _v1 = __lsx_vfmul_s(_v1, _scale1);
63 _v2 = __lsx_vfmul_s(_v2, _scale0);
64 _v3 = __lsx_vfmul_s(_v3, _scale1);
65 _v4 = __lsx_vfmul_s(_v4, _scale0);
66 _v5 = __lsx_vfmul_s(_v5, _scale1);
67 _v6 = __lsx_vfmul_s(_v6, _scale0);
68 _v7 = __lsx_vfmul_s(_v7, _scale1);
69 *((int64_t*)ptr) = float2int8leakyrelu(_v0, _v1, _slope);
70 *((int64_t*)(ptr + 8)) = float2int8leakyrelu(_v2, _v3, _slope);
71 *((int64_t*)(ptr + 16)) = float2int8leakyrelu(_v4, _v5, _slope);
72 *((int64_t*)(ptr + 24)) = float2int8leakyrelu(_v6, _v7, _slope);
73
74 intptr += 32;
75 ptr += 32;
76 }
77 for (; i + 1 < size; i += 2)
78 {
79 __builtin_prefetch(intptr + 64);
80 __m128 _v0 = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
81 __m128 _v1 = __lsx_vffint_s_w(__lsx_vld(intptr + 4, 0));
82 __m128 _v2 = __lsx_vffint_s_w(__lsx_vld(intptr + 8, 0));
83 __m128 _v3 = __lsx_vffint_s_w(__lsx_vld(intptr + 12, 0));
84 _v0 = __lsx_vfmul_s(_v0, _scale0);
85 _v1 = __lsx_vfmul_s(_v1, _scale1);
86 _v2 = __lsx_vfmul_s(_v2, _scale0);
87 _v3 = __lsx_vfmul_s(_v3, _scale1);
88 *((int64_t*)ptr) = float2int8leakyrelu(_v0, _v1, _slope);
89 *((int64_t*)(ptr + 8)) = float2int8leakyrelu(_v2, _v3, _slope);
90
91 intptr += 16;
92 ptr += 16;
93 }
94 for (; i < size; i++)
95 {
96 __builtin_prefetch(intptr + 32);
97 __m128 _v0 = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
98 __m128 _v1 = __lsx_vffint_s_w(__lsx_vld(intptr + 4, 0));
99 _v0 = __lsx_vfmul_s(_v0, _scale0);
100 _v1 = __lsx_vfmul_s(_v1, _scale1);
101 *((int64_t*)ptr) = float2int8leakyrelu(_v0, _v1, _slope);
102
103 intptr += 8;
104 ptr += 8;
105 }
106 }
107 }
108 else
109 {
110 #pragma omp parallel for num_threads(opt.num_threads)
111 for (int q = 0; q < channels; q++)
112 {
113 const int* intptr = bottom_blob.channel(q);
114 signed char* ptr = top_blob.channel(q);
115
116 __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);
117 __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);
118 __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);
119 __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);
120 __m128 _bias0 = bias_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(bias_data[0]) : (__m128)__lsx_vld((const float*)bias_data + q * 8, 0);
121 __m128 _bias1 = bias_data_size == 1 ? (__m128)__lsx_vreplfr2vr_s(bias_data[0]) : (__m128)__lsx_vld((const float*)bias_data + q * 8 + 4, 0);
122
123 __m128 _scale0 = __lsx_vfmul_s(_scale_in0, _scale_out0);
124 __m128 _scale1 = __lsx_vfmul_s(_scale_in1, _scale_out1);
125 _bias0 = __lsx_vfmul_s(_bias0, _scale_out0);
126 _bias1 = __lsx_vfmul_s(_bias1, _scale_out1);
127 __m128 _slope = (__m128)__lsx_vreplfr2vr_s(slope);
128
129 int i = 0;
130 for (; i + 3 < size; i += 4)
131 {
132 __builtin_prefetch(intptr + 128);
133 __m128 _v0 = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
134 __m128 _v1 = __lsx_vffint_s_w(__lsx_vld(intptr + 4, 0));
135 __m128 _v2 = __lsx_vffint_s_w(__lsx_vld(intptr + 8, 0));
136 __m128 _v3 = __lsx_vffint_s_w(__lsx_vld(intptr + 12, 0));
137 __m128 _v4 = __lsx_vffint_s_w(__lsx_vld(intptr + 16, 0));
138 __m128 _v5 = __lsx_vffint_s_w(__lsx_vld(intptr + 20, 0));
139 __m128 _v6 = __lsx_vffint_s_w(__lsx_vld(intptr + 24, 0));
140 __m128 _v7 = __lsx_vffint_s_w(__lsx_vld(intptr + 28, 0));
141 _v0 = __lsx_vfmadd_s(_scale0, _v0, _bias0);
142 _v1 = __lsx_vfmadd_s(_scale1, _v1, _bias1);
143 _v2 = __lsx_vfmadd_s(_scale0, _v2, _bias0);
144 _v3 = __lsx_vfmadd_s(_scale1, _v3, _bias1);
145 _v4 = __lsx_vfmadd_s(_scale0, _v4, _bias0);
146 _v5 = __lsx_vfmadd_s(_scale1, _v5, _bias1);
147 _v6 = __lsx_vfmadd_s(_scale0, _v6, _bias0);
148 _v7 = __lsx_vfmadd_s(_scale1, _v7, _bias1);
149 *((int64_t*)ptr) = float2int8leakyrelu(_v0, _v1, _slope);
150 *((int64_t*)(ptr + 8)) = float2int8leakyrelu(_v2, _v3, _slope);
151 *((int64_t*)(ptr + 16)) = float2int8leakyrelu(_v4, _v5, _slope);
152 *((int64_t*)(ptr + 24)) = float2int8leakyrelu(_v6, _v7, _slope);
153
154 intptr += 32;
155 ptr += 32;
156 }
157 for (; i + 1 < size; i += 2)
158 {
159 __builtin_prefetch(intptr + 64);
160 __m128 _v0 = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
161 __m128 _v1 = __lsx_vffint_s_w(__lsx_vld(intptr + 4, 0));
162 __m128 _v2 = __lsx_vffint_s_w(__lsx_vld(intptr + 8, 0));
163 __m128 _v3 = __lsx_vffint_s_w(__lsx_vld(intptr + 12, 0));
164 _v0 = __lsx_vfmadd_s(_scale0, _v0, _bias0);
165 _v1 = __lsx_vfmadd_s(_scale1, _v1, _bias1);
166 _v2 = __lsx_vfmadd_s(_scale0, _v2, _bias0);
167 _v3 = __lsx_vfmadd_s(_scale1, _v3, _bias1);
168 *((int64_t*)ptr) = float2int8leakyrelu(_v0, _v1, _slope);
169 *((int64_t*)(ptr + 8)) = float2int8leakyrelu(_v2, _v3, _slope);
170
171 intptr += 16;
172 ptr += 16;
173 }
174 for (; i < size; i++)
175 {
176 __builtin_prefetch(intptr + 32);
177 __m128 _v0 = __lsx_vffint_s_w(__lsx_vld(intptr, 0));
178 __m128 _v1 = __lsx_vffint_s_w(__lsx_vld(intptr + 4, 0));
179 _v0 = __lsx_vfmadd_s(_scale0, _v0, _bias0);
180 _v1 = __lsx_vfmadd_s(_scale1, _v1, _bias1);
181 *((int64_t*)ptr) = float2int8leakyrelu(_v0, _v1, _slope);
182
183 intptr += 8;
184 ptr += 8;
185 }
186 }
187 }
188}