Elffuss BraidHub

kikocis/ncnn public

master · 9.4 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
15static void requantize_leakyrelu_pack8_neon(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 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);
41 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);
42 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);
43 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);
44
45 float32x4_t _scale0 = vmulq_f32(_scale_in0, _scale_out0);
46 float32x4_t _scale1 = vmulq_f32(_scale_in1, _scale_out1);
47 float32x4_t _slope = vdupq_n_f32(slope);
48
49 int i = 0;
50#if __aarch64__
51 for (; i + 3 < size; i += 4)
52 {
53 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
54 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32(intptr + 4));
55 float32x4_t _v2 = vcvtq_f32_s32(vld1q_s32(intptr + 8));
56 float32x4_t _v3 = vcvtq_f32_s32(vld1q_s32(intptr + 12));
57 float32x4_t _v4 = vcvtq_f32_s32(vld1q_s32(intptr + 16));
58 float32x4_t _v5 = vcvtq_f32_s32(vld1q_s32(intptr + 20));
59 float32x4_t _v6 = vcvtq_f32_s32(vld1q_s32(intptr + 24));
60 float32x4_t _v7 = vcvtq_f32_s32(vld1q_s32(intptr + 28));
61 _v0 = vmulq_f32(_v0, _scale0);
62 _v1 = vmulq_f32(_v1, _scale1);
63 _v2 = vmulq_f32(_v2, _scale0);
64 _v3 = vmulq_f32(_v3, _scale1);
65 _v4 = vmulq_f32(_v4, _scale0);
66 _v5 = vmulq_f32(_v5, _scale1);
67 _v6 = vmulq_f32(_v6, _scale0);
68 _v7 = vmulq_f32(_v7, _scale1);
69 vst1_s8(ptr, float2int8leakyrelu(_v0, _v1, _slope));
70 vst1_s8(ptr + 8, float2int8leakyrelu(_v2, _v3, _slope));
71 vst1_s8(ptr + 16, float2int8leakyrelu(_v4, _v5, _slope));
72 vst1_s8(ptr + 24, float2int8leakyrelu(_v6, _v7, _slope));
73
74 intptr += 32;
75 ptr += 32;
76 }
77#endif // __aarch64__
78 for (; i + 1 < size; i += 2)
79 {
80 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
81 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32(intptr + 4));
82 float32x4_t _v2 = vcvtq_f32_s32(vld1q_s32(intptr + 8));
83 float32x4_t _v3 = vcvtq_f32_s32(vld1q_s32(intptr + 12));
84 _v0 = vmulq_f32(_v0, _scale0);
85 _v1 = vmulq_f32(_v1, _scale1);
86 _v2 = vmulq_f32(_v2, _scale0);
87 _v3 = vmulq_f32(_v3, _scale1);
88 vst1_s8(ptr, float2int8leakyrelu(_v0, _v1, _slope));
89 vst1_s8(ptr + 8, float2int8leakyrelu(_v2, _v3, _slope));
90
91 intptr += 16;
92 ptr += 16;
93 }
94 for (; i < size; i++)
95 {
96 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
97 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32(intptr + 4));
98 _v0 = vmulq_f32(_v0, _scale0);
99 _v1 = vmulq_f32(_v1, _scale1);
100 vst1_s8(ptr, float2int8leakyrelu(_v0, _v1, _slope));
101
102 intptr += 8;
103 ptr += 8;
104 }
105 }
106 }
107 else
108 {
109 #pragma omp parallel for num_threads(opt.num_threads)
110 for (int q = 0; q < channels; q++)
111 {
112 const int* intptr = bottom_blob.channel(q);
113 signed char* ptr = top_blob.channel(q);
114
115 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);
116 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);
117 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);
118 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);
119 float32x4_t _bias0 = bias_data_size == 1 ? vdupq_n_f32(bias_data[0]) : vld1q_f32((const float*)bias_data + q * 8);
120 float32x4_t _bias1 = bias_data_size == 1 ? vdupq_n_f32(bias_data[0]) : vld1q_f32((const float*)bias_data + q * 8 + 4);
121
122 float32x4_t _scale0 = vmulq_f32(_scale_in0, _scale_out0);
123 float32x4_t _scale1 = vmulq_f32(_scale_in1, _scale_out1);
124 _bias0 = vmulq_f32(_bias0, _scale_out0);
125 _bias1 = vmulq_f32(_bias1, _scale_out1);
126 float32x4_t _slope = vdupq_n_f32(slope);
127
128 int i = 0;
129#if __aarch64__
130 for (; i + 3 < size; i += 4)
131 {
132 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
133 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32(intptr + 4));
134 float32x4_t _v2 = vcvtq_f32_s32(vld1q_s32(intptr + 8));
135 float32x4_t _v3 = vcvtq_f32_s32(vld1q_s32(intptr + 12));
136 float32x4_t _v4 = vcvtq_f32_s32(vld1q_s32(intptr + 16));
137 float32x4_t _v5 = vcvtq_f32_s32(vld1q_s32(intptr + 20));
138 float32x4_t _v6 = vcvtq_f32_s32(vld1q_s32(intptr + 24));
139 float32x4_t _v7 = vcvtq_f32_s32(vld1q_s32(intptr + 28));
140
141 _v0 = vfmaq_f32(_bias0, _v0, _scale0);
142 _v1 = vfmaq_f32(_bias1, _v1, _scale1);
143 _v2 = vfmaq_f32(_bias0, _v2, _scale0);
144 _v3 = vfmaq_f32(_bias1, _v3, _scale1);
145 _v4 = vfmaq_f32(_bias0, _v4, _scale0);
146 _v5 = vfmaq_f32(_bias1, _v5, _scale1);
147 _v6 = vfmaq_f32(_bias0, _v6, _scale0);
148 _v7 = vfmaq_f32(_bias1, _v7, _scale1);
149
150 vst1_s8(ptr, float2int8leakyrelu(_v0, _v1, _slope));
151 vst1_s8(ptr + 8, float2int8leakyrelu(_v2, _v3, _slope));
152 vst1_s8(ptr + 16, float2int8leakyrelu(_v4, _v5, _slope));
153 vst1_s8(ptr + 24, float2int8leakyrelu(_v6, _v7, _slope));
154
155 intptr += 32;
156 ptr += 32;
157 }
158#endif // __aarch64__
159 for (; i + 1 < size; i += 2)
160 {
161 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
162 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32(intptr + 4));
163 float32x4_t _v2 = vcvtq_f32_s32(vld1q_s32(intptr + 8));
164 float32x4_t _v3 = vcvtq_f32_s32(vld1q_s32(intptr + 12));
165
166#if __aarch64__
167 _v0 = vfmaq_f32(_bias0, _v0, _scale0);
168 _v1 = vfmaq_f32(_bias1, _v1, _scale1);
169 _v2 = vfmaq_f32(_bias0, _v2, _scale0);
170 _v3 = vfmaq_f32(_bias1, _v3, _scale1);
171#else // __aarch64__
172 _v0 = vmlaq_f32(_bias0, _v0, _scale0);
173 _v1 = vmlaq_f32(_bias1, _v1, _scale1);
174 _v2 = vmlaq_f32(_bias0, _v2, _scale0);
175 _v3 = vmlaq_f32(_bias1, _v3, _scale1);
176#endif // __aarch64__
177
178 vst1_s8(ptr, float2int8leakyrelu(_v0, _v1, _slope));
179 vst1_s8(ptr + 8, float2int8leakyrelu(_v2, _v3, _slope));
180
181 intptr += 16;
182 ptr += 16;
183 }
184 for (; i < size; i++)
185 {
186 float32x4_t _v0 = vcvtq_f32_s32(vld1q_s32(intptr));
187 float32x4_t _v1 = vcvtq_f32_s32(vld1q_s32(intptr + 4));
188#if __aarch64__
189 _v0 = vfmaq_f32(_bias0, _v0, _scale0);
190 _v1 = vfmaq_f32(_bias1, _v1, _scale1);
191#else // __aarch64__
192 _v0 = vmlaq_f32(_bias0, _v0, _scale0);
193 _v1 = vmlaq_f32(_bias1, _v1, _scale1);
194#endif // __aarch64__
195 vst1_s8(ptr, float2int8leakyrelu(_v0, _v1, _slope));
196
197 intptr += 8;
198 ptr += 8;
199 }
200 }
201 }
202}