| 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 | |
| 15 | static void requantize_leakyrelu_pack4_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 | 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 | 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); |
| 46 | 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); |
| 47 | 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); |
| 48 | 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); |
| 49 | |
| 50 | v4f32 _scale0 = __msa_fmul_w(_scale_in0, _scale_out0); |
| 51 | v4f32 _scale1 = __msa_fmul_w(_scale_in1, _scale_out1); |
| 52 | v4f32 _slope = (v4f32)__msa_fill_w_f32(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 | v4f32 _v00 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr0, 0)); |
| 60 | v4f32 _v01 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr0 + 4, 0)); |
| 61 | v4f32 _v02 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr0 + 8, 0)); |
| 62 | v4f32 _v03 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr0 + 12, 0)); |
| 63 | v4f32 _v10 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr1, 0)); |
| 64 | v4f32 _v11 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr1 + 4, 0)); |
| 65 | v4f32 _v12 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr1 + 8, 0)); |
| 66 | v4f32 _v13 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr1 + 12, 0)); |
| 67 | _v00 = __msa_fmul_w(_v00, _scale0); |
| 68 | _v01 = __msa_fmul_w(_v01, _scale0); |
| 69 | _v02 = __msa_fmul_w(_v02, _scale0); |
| 70 | _v03 = __msa_fmul_w(_v03, _scale0); |
| 71 | _v10 = __msa_fmul_w(_v10, _scale1); |
| 72 | _v11 = __msa_fmul_w(_v11, _scale1); |
| 73 | _v12 = __msa_fmul_w(_v12, _scale1); |
| 74 | _v13 = __msa_fmul_w(_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 | v4f32 _v0 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr0, 0)); |
| 89 | v4f32 _v1 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr1, 0)); |
| 90 | _v0 = __msa_fmul_w(_v0, _scale0); |
| 91 | _v1 = __msa_fmul_w(_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 | 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); |
| 110 | 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); |
| 111 | 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); |
| 112 | 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); |
| 113 | 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); |
| 114 | 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); |
| 115 | |
| 116 | v4f32 _scale0 = __msa_fmul_w(_scale_in0, _scale_out0); |
| 117 | v4f32 _scale1 = __msa_fmul_w(_scale_in1, _scale_out1); |
| 118 | _bias0 = __msa_fmul_w(_bias0, _scale_out0); |
| 119 | _bias1 = __msa_fmul_w(_bias1, _scale_out1); |
| 120 | v4f32 _slope = (v4f32)__msa_fill_w_f32(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 | v4f32 _v00 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr0, 0)); |
| 128 | v4f32 _v01 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr0 + 4, 0)); |
| 129 | v4f32 _v02 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr0 + 8, 0)); |
| 130 | v4f32 _v03 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr0 + 12, 0)); |
| 131 | v4f32 _v10 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr1, 0)); |
| 132 | v4f32 _v11 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr1 + 4, 0)); |
| 133 | v4f32 _v12 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr1 + 8, 0)); |
| 134 | v4f32 _v13 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr1 + 12, 0)); |
| 135 | _v00 = __msa_fmadd_w(_bias0, _v00, _scale0); |
| 136 | _v01 = __msa_fmadd_w(_bias0, _v01, _scale0); |
| 137 | _v02 = __msa_fmadd_w(_bias0, _v02, _scale0); |
| 138 | _v03 = __msa_fmadd_w(_bias0, _v03, _scale0); |
| 139 | _v10 = __msa_fmadd_w(_bias1, _v10, _scale1); |
| 140 | _v11 = __msa_fmadd_w(_bias1, _v11, _scale1); |
| 141 | _v12 = __msa_fmadd_w(_bias1, _v12, _scale1); |
| 142 | _v13 = __msa_fmadd_w(_bias1, _v13, _scale1); |
| 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 | v4f32 _v00 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr0, 0)); |
| 157 | v4f32 _v01 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr0 + 4, 0)); |
| 158 | v4f32 _v10 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr1, 0)); |
| 159 | v4f32 _v11 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr1 + 4, 0)); |
| 160 | _v00 = __msa_fmadd_w(_bias0, _v00, _scale0); |
| 161 | _v01 = __msa_fmadd_w(_bias0, _v01, _scale0); |
| 162 | _v10 = __msa_fmadd_w(_bias1, _v10, _scale1); |
| 163 | _v11 = __msa_fmadd_w(_bias1, _v11, _scale1); |
| 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 | v4f32 _v0 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr0, 0)); |
| 176 | v4f32 _v1 = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr1, 0)); |
| 177 | _v0 = __msa_fmadd_w(_bias0, _v0, _scale0); |
| 178 | _v1 = __msa_fmadd_w(_bias1, _v1, _scale1); |
| 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 | |
| 201 | v4f32 _scale_in = scale_in_data_size == 1 ? (v4f32)__msa_fill_w_f32(scale_in_data[0]) : (v4f32)__msa_ld_w((const float*)scale_in_data + q * 4, 0); |
| 202 | v4f32 _scale_out = scale_out_data_size == 1 ? (v4f32)__msa_fill_w_f32(scale_out_data[0]) : (v4f32)__msa_ld_w((const float*)scale_out_data + q * 4, 0); |
| 203 | |
| 204 | v4f32 _scale = __msa_fmul_w(_scale_in, _scale_out); |
| 205 | v4f32 _slope = (v4f32)__msa_fill_w_f32(slope); |
| 206 | |
| 207 | int i = 0; |
| 208 | for (; i < size; i++) |
| 209 | { |
| 210 | __builtin_prefetch(intptr + 16); |
| 211 | v4f32 _v = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr, 0)); |
| 212 | _v = __msa_fmul_w(_v, _scale); |
| 213 | v16i8 v = float2int8leakyrelu(_v, _slope); |
| 214 | ptr0[0] = v[0]; |
| 215 | ptr1[0] = v[1]; |
| 216 | ptr2[0] = v[2]; |
| 217 | ptr3[0] = v[3]; |
| 218 | |
| 219 | intptr += 4; |
| 220 | ptr0 += 1; |
| 221 | ptr1 += 1; |
| 222 | ptr2 += 1; |
| 223 | ptr3 += 1; |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | else |
| 228 | { |
| 229 | #pragma omp parallel for num_threads(opt.num_threads) |
| 230 | for (int q = 0; q < channels; q++) |
| 231 | { |
| 232 | const int* intptr = bottom_blob.channel(q); |
| 233 | signed char* ptr0 = top_blob.channel(q * 4); |
| 234 | signed char* ptr1 = top_blob.channel(q * 4 + 1); |
| 235 | signed char* ptr2 = top_blob.channel(q * 4 + 2); |
| 236 | signed char* ptr3 = top_blob.channel(q * 4 + 3); |
| 237 | |
| 238 | v4f32 _scale_in = scale_in_data_size == 1 ? (v4f32)__msa_fill_w_f32(scale_in_data[0]) : (v4f32)__msa_ld_w((const float*)scale_in_data + q * 4, 0); |
| 239 | v4f32 _scale_out = scale_out_data_size == 1 ? (v4f32)__msa_fill_w_f32(scale_out_data[0]) : (v4f32)__msa_ld_w((const float*)scale_out_data + q * 4, 0); |
| 240 | v4f32 _bias = bias_data_size == 1 ? (v4f32)__msa_fill_w_f32(bias_data[0]) : (v4f32)__msa_ld_w((const float*)bias_data + q * 4, 0); |
| 241 | |
| 242 | v4f32 _scale = __msa_fmul_w(_scale_in, _scale_out); |
| 243 | _bias = __msa_fmul_w(_bias, _scale_out); |
| 244 | v4f32 _slope = (v4f32)__msa_fill_w_f32(slope); |
| 245 | |
| 246 | int i = 0; |
| 247 | for (; i < size; i++) |
| 248 | { |
| 249 | __builtin_prefetch(intptr + 16); |
| 250 | v4f32 _v = (v4f32)__msa_ffint_s_w(__msa_ld_w(intptr, 0)); |
| 251 | _v = __msa_fmadd_w(_bias, _v, _scale); |
| 252 | v16i8 v = float2int8leakyrelu(_v, _slope); |
| 253 | ptr0[0] = v[0]; |
| 254 | ptr1[0] = v[1]; |
| 255 | ptr2[0] = v[2]; |
| 256 | ptr3[0] = v[3]; |
| 257 | |
| 258 | intptr += 4; |
| 259 | ptr0 += 1; |
| 260 | ptr1 += 1; |
| 261 | ptr2 += 1; |
| 262 | ptr3 += 1; |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | } |