Elffuss BraidHub

kikocis/ncnn public

master · 9.7 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2022 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_msa(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 v4f32 _scale_in0 = scale_in_data_size == 1 ? (v4f32)__msa_fill_w_f32(scale_in_data[0]) : (v4f32)__msa_ld_w((const float*)scale_in_data + q * 8, 0);
41 v4f32 _scale_in1 = scale_in_data_size == 1 ? (v4f32)__msa_fill_w_f32(scale_in_data[0]) : (v4f32)__msa_ld_w((const float*)scale_in_data + q * 8 + 4, 0);
42 v4f32 _scale_out0 = scale_out_data_size == 1 ? (v4f32)__msa_fill_w_f32(scale_out_data[0]) : (v4f32)__msa_ld_w((const float*)scale_out_data + q * 8, 0);
43 v4f32 _scale_out1 = scale_out_data_size == 1 ? (v4f32)__msa_fill_w_f32(scale_out_data[0]) : (v4f32)__msa_ld_w((const float*)scale_out_data + q * 8 + 4, 0);
44
45 v4f32 _scale0 = __msa_fmul_w(_scale_in0, _scale_out0);
46 v4f32 _scale1 = __msa_fmul_w(_scale_in1, _scale_out1);
47 v4f32 _slope = (v4f32)__msa_fill_w_f32(slope);
48
49 int i = 0;
50 for (; i + 3 < size; i += 4)
51 {
52 __builtin_prefetch(intptr + 128);
53 v4f32 _v0 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr, 0));
54 v4f32 _v1 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 4, 0));
55 v4f32 _v2 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 8, 0));
56 v4f32 _v3 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 12, 0));
57 v4f32 _v4 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 16, 0));
58 v4f32 _v5 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 20, 0));
59 v4f32 _v6 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 24, 0));
60 v4f32 _v7 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 28, 0));
61 _v0 = __msa_fmul_w(_v0, _scale0);
62 _v1 = __msa_fmul_w(_v1, _scale1);
63 _v2 = __msa_fmul_w(_v2, _scale0);
64 _v3 = __msa_fmul_w(_v3, _scale1);
65 _v4 = __msa_fmul_w(_v4, _scale0);
66 _v5 = __msa_fmul_w(_v5, _scale1);
67 _v6 = __msa_fmul_w(_v6, _scale0);
68 _v7 = __msa_fmul_w(_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 v4f32 _v0 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr, 0));
81 v4f32 _v1 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 4, 0));
82 v4f32 _v2 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 8, 0));
83 v4f32 _v3 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 12, 0));
84 _v0 = __msa_fmul_w(_v0, _scale0);
85 _v1 = __msa_fmul_w(_v1, _scale1);
86 _v2 = __msa_fmul_w(_v2, _scale0);
87 _v3 = __msa_fmul_w(_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 v4f32 _v0 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr, 0));
98 v4f32 _v1 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 4, 0));
99 _v0 = __msa_fmul_w(_v0, _scale0);
100 _v1 = __msa_fmul_w(_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 v4f32 _scale_in0 = scale_in_data_size == 1 ? (v4f32)__msa_fill_w_f32(scale_in_data[0]) : (v4f32)__msa_ld_w((const float*)scale_in_data + q * 8, 0);
117 v4f32 _scale_in1 = scale_in_data_size == 1 ? (v4f32)__msa_fill_w_f32(scale_in_data[0]) : (v4f32)__msa_ld_w((const float*)scale_in_data + q * 8 + 4, 0);
118 v4f32 _scale_out0 = scale_out_data_size == 1 ? (v4f32)__msa_fill_w_f32(scale_out_data[0]) : (v4f32)__msa_ld_w((const float*)scale_out_data + q * 8, 0);
119 v4f32 _scale_out1 = scale_out_data_size == 1 ? (v4f32)__msa_fill_w_f32(scale_out_data[0]) : (v4f32)__msa_ld_w((const float*)scale_out_data + q * 8 + 4, 0);
120 v4f32 _bias0 = bias_data_size == 1 ? (v4f32)__msa_fill_w_f32(bias_data[0]) : (v4f32)__msa_ld_w((const float*)bias_data + q * 8, 0);
121 v4f32 _bias1 = bias_data_size == 1 ? (v4f32)__msa_fill_w_f32(bias_data[0]) : (v4f32)__msa_ld_w((const float*)bias_data + q * 8 + 4, 0);
122
123 v4f32 _scale0 = __msa_fmul_w(_scale_in0, _scale_out0);
124 v4f32 _scale1 = __msa_fmul_w(_scale_in1, _scale_out1);
125 _bias0 = __msa_fmul_w(_bias0, _scale_out0);
126 _bias1 = __msa_fmul_w(_bias1, _scale_out1);
127 v4f32 _slope = (v4f32)__msa_fill_w_f32(slope);
128
129 int i = 0;
130 for (; i + 3 < size; i += 4)
131 {
132 __builtin_prefetch(intptr + 128);
133 v4f32 _v0 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr, 0));
134 v4f32 _v1 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 4, 0));
135 v4f32 _v2 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 8, 0));
136 v4f32 _v3 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 12, 0));
137 v4f32 _v4 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 16, 0));
138 v4f32 _v5 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 20, 0));
139 v4f32 _v6 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 24, 0));
140 v4f32 _v7 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 28, 0));
141 _v0 = __msa_fmadd_w(_bias0, _v0, _scale0);
142 _v1 = __msa_fmadd_w(_bias1, _v1, _scale1);
143 _v2 = __msa_fmadd_w(_bias0, _v2, _scale0);
144 _v3 = __msa_fmadd_w(_bias1, _v3, _scale1);
145 _v4 = __msa_fmadd_w(_bias0, _v4, _scale0);
146 _v5 = __msa_fmadd_w(_bias1, _v5, _scale1);
147 _v6 = __msa_fmadd_w(_bias0, _v6, _scale0);
148 _v7 = __msa_fmadd_w(_bias1, _v7, _scale1);
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 v4f32 _v0 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr, 0));
161 v4f32 _v1 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 4, 0));
162 v4f32 _v2 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 8, 0));
163 v4f32 _v3 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 12, 0));
164 _v0 = __msa_fmadd_w(_bias0, _v0, _scale0);
165 _v1 = __msa_fmadd_w(_bias1, _v1, _scale1);
166 _v2 = __msa_fmadd_w(_bias0, _v2, _scale0);
167 _v3 = __msa_fmadd_w(_bias1, _v3, _scale1);
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 v4f32 _v0 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr, 0));
178 v4f32 _v1 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr + 4, 0));
179 _v0 = __msa_fmadd_w(_bias0, _v0, _scale0);
180 _v1 = __msa_fmadd_w(_bias1, _v1, _scale1);
181 *((int64_t*)ptr) = float2int8leakyrelu(_v0, _v1, _slope);
182
183 intptr += 8;
184 ptr += 8;
185 }
186 }
187 }
188}