| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2017 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 | #include "innerproduct_x86.h" |
| 16 | |
| 17 | #if __SSE2__ |
| 18 | #include <emmintrin.h> |
| 19 | #if __AVX__ |
| 20 | #include <immintrin.h> |
| 21 | #endif |
| 22 | #endif // __SSE2__ |
| 23 | |
| 24 | #include "x86_activation.h" |
| 25 | #include "x86_usability.h" |
| 26 | |
| 27 | #include "layer_type.h" |
| 28 | |
| 29 | #include "cpu.h" |
| 30 | |
| 31 | namespace ncnn { |
| 32 | |
| 33 | #include "innerproduct_fp.h" |
| 34 | #include "innerproduct_gemm_fp.h" |
| 35 | |
| 36 | #if NCNN_F16C && __AVX__ |
| 37 | #define NCNN_IMPL_FP16S 1 |
| 38 | #include "innerproduct_fp.h" |
| 39 | #include "innerproduct_gemm_fp.h" |
| 40 | #undef NCNN_IMPL_FP16S |
| 41 | #endif |
| 42 | |
| 43 | InnerProduct_x86::InnerProduct_x86() |
| 44 | { |
| 45 | #if __SSE2__ |
| 46 | support_packing = true; |
| 47 | #endif // __SSE2__ |
| 48 | |
| 49 | flatten = 0; |
| 50 | } |
| 51 | |
| 52 | int InnerProduct_x86::create_pipeline(const Option& opt) |
| 53 | { |
| 54 | // if (opt.use_packing_layout) |
| 55 | { |
| 56 | flatten = ncnn::create_layer_cpu(ncnn::LayerType::Flatten); |
| 57 | |
| 58 | ncnn::ParamDict pd; |
| 59 | |
| 60 | flatten->load_param(pd); |
| 61 | |
| 62 | flatten->create_pipeline(opt); |
| 63 | } |
| 64 | |
| 65 | #if NCNN_INT8 |
| 66 | if (opt.use_int8_inference && weight_data.elemsize == (size_t)1u) |
| 67 | { |
| 68 | return create_pipeline_int8_x86(opt); |
| 69 | } |
| 70 | #endif |
| 71 | |
| 72 | #if NCNN_F16C && __AVX__ |
| 73 | if (cpu_support_x86_f16c() && opt.use_fp16_storage) |
| 74 | { |
| 75 | return create_pipeline_fp16s(opt); |
| 76 | } |
| 77 | #endif |
| 78 | |
| 79 | const int num_input = weight_data_size / num_output; |
| 80 | |
| 81 | innerproduct_transform_kernel_sse(weight_data, weight_data_tm, num_input, num_output, opt); |
| 82 | |
| 83 | if (opt.lightmode) |
| 84 | weight_data.release(); |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | int InnerProduct_x86::destroy_pipeline(const Option& opt) |
| 90 | { |
| 91 | if (flatten) |
| 92 | { |
| 93 | flatten->destroy_pipeline(opt); |
| 94 | delete flatten; |
| 95 | flatten = 0; |
| 96 | } |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | int InnerProduct_x86::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 102 | { |
| 103 | #if NCNN_INT8 |
| 104 | if (opt.use_int8_inference && int8_scale_term) |
| 105 | { |
| 106 | return forward_int8_x86(bottom_blob, top_blob, opt); |
| 107 | } |
| 108 | #endif |
| 109 | |
| 110 | #if NCNN_F16C && __AVX__ |
| 111 | if (cpu_support_x86_f16c() && opt.use_fp16_storage) |
| 112 | { |
| 113 | return forward_fp16s(bottom_blob, top_blob, opt); |
| 114 | } |
| 115 | #endif |
| 116 | |
| 117 | const int num_input = weight_data_size / num_output; |
| 118 | |
| 119 | if (bottom_blob.dims == 2 && bottom_blob.w == num_input) |
| 120 | { |
| 121 | // gemm |
| 122 | int h = bottom_blob.h; |
| 123 | size_t elemsize = bottom_blob.elemsize; |
| 124 | int elempack = bottom_blob.elempack; |
| 125 | |
| 126 | top_blob.create(num_output, h, elemsize, elempack, opt.blob_allocator); |
| 127 | if (top_blob.empty()) |
| 128 | return -100; |
| 129 | |
| 130 | innerproduct_gemm_sse(bottom_blob, top_blob, weight_data_tm, bias_data, activation_type, activation_params, opt); |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | // flatten |
| 136 | Mat bottom_blob_flattened = bottom_blob; |
| 137 | if (bottom_blob.dims != 1) |
| 138 | { |
| 139 | Option opt_flatten = opt; |
| 140 | opt_flatten.blob_allocator = opt.workspace_allocator; |
| 141 | |
| 142 | flatten->forward(bottom_blob, bottom_blob_flattened, opt_flatten); |
| 143 | if (bottom_blob_flattened.empty()) |
| 144 | return -100; |
| 145 | } |
| 146 | |
| 147 | size_t elemsize = bottom_blob_flattened.elemsize; |
| 148 | int elempack = bottom_blob_flattened.elempack; |
| 149 | |
| 150 | int out_elempack = 1; |
| 151 | #if __SSE2__ |
| 152 | if (opt.use_packing_layout) |
| 153 | { |
| 154 | #if __AVX512F__ |
| 155 | out_elempack = num_output % 16 == 0 ? 16 : num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1; |
| 156 | #elif __AVX__ |
| 157 | out_elempack = num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1; |
| 158 | #else |
| 159 | out_elempack = num_output % 4 == 0 ? 4 : 1; |
| 160 | #endif |
| 161 | } |
| 162 | #endif // __SSE2__ |
| 163 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 164 | |
| 165 | top_blob.create(num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 166 | if (top_blob.empty()) |
| 167 | return -100; |
| 168 | |
| 169 | innerproduct_sse(bottom_blob_flattened, top_blob, weight_data_tm, bias_data, activation_type, activation_params, opt); |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | #if NCNN_F16C && __AVX__ |
| 175 | int InnerProduct_x86::create_pipeline_fp16s(const Option& opt) |
| 176 | { |
| 177 | const int num_input = weight_data_size / num_output; |
| 178 | |
| 179 | innerproduct_transform_kernel_fp16s_sse(weight_data, weight_data_tm, num_input, num_output, opt); |
| 180 | |
| 181 | if (opt.lightmode) |
| 182 | weight_data.release(); |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | int InnerProduct_x86::forward_fp16s(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 188 | { |
| 189 | const int num_input = weight_data_size / num_output; |
| 190 | |
| 191 | if (bottom_blob.dims == 2 && bottom_blob.w == num_input) |
| 192 | { |
| 193 | // gemm |
| 194 | int h = bottom_blob.h; |
| 195 | size_t elemsize = bottom_blob.elemsize; |
| 196 | int elempack = bottom_blob.elempack; |
| 197 | |
| 198 | top_blob.create(num_output, h, elemsize, elempack, opt.blob_allocator); |
| 199 | if (top_blob.empty()) |
| 200 | return -100; |
| 201 | |
| 202 | innerproduct_gemm_fp16s_sse(bottom_blob, top_blob, weight_data_tm, bias_data, activation_type, activation_params, opt); |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | // flatten |
| 208 | Mat bottom_blob_flattened = bottom_blob; |
| 209 | if (bottom_blob.dims != 1) |
| 210 | { |
| 211 | Option opt_flatten = opt; |
| 212 | opt_flatten.blob_allocator = opt.workspace_allocator; |
| 213 | |
| 214 | flatten->forward(bottom_blob, bottom_blob_flattened, opt_flatten); |
| 215 | if (bottom_blob_flattened.empty()) |
| 216 | return -100; |
| 217 | } |
| 218 | |
| 219 | size_t elemsize = bottom_blob_flattened.elemsize; |
| 220 | int elempack = bottom_blob_flattened.elempack; |
| 221 | |
| 222 | int out_elempack = 1; |
| 223 | if (opt.use_packing_layout) |
| 224 | { |
| 225 | #if __AVX512F__ |
| 226 | out_elempack = num_output % 16 == 0 ? 16 : num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1; |
| 227 | #else |
| 228 | out_elempack = num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1; |
| 229 | #endif |
| 230 | } |
| 231 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 232 | |
| 233 | top_blob.create(num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 234 | if (top_blob.empty()) |
| 235 | return -100; |
| 236 | |
| 237 | innerproduct_fp16s_sse(bottom_blob_flattened, top_blob, weight_data_tm, bias_data, activation_type, activation_params, opt); |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | #endif // NCNN_F16C && __AVX__ |
| 242 | |
| 243 | #if NCNN_INT8 |
| 244 | int InnerProduct_x86::create_pipeline_int8_x86(const Option& opt) |
| 245 | { |
| 246 | const int num_input = weight_data_size / num_output; |
| 247 | |
| 248 | int out_elempack = 1; |
| 249 | #if __SSE2__ |
| 250 | if (opt.use_packing_layout) |
| 251 | { |
| 252 | out_elempack = num_output % 8 == 0 ? 8 : 1; |
| 253 | } |
| 254 | #endif // __SSE2__ |
| 255 | |
| 256 | // src = inch-outch |
| 257 | // dst = pb-inch-outch/pb |
| 258 | { |
| 259 | Mat weight_data_r2 = weight_data.reshape(num_input, num_output); |
| 260 | |
| 261 | weight_data_tm.create(num_input, num_output / out_elempack, (size_t)out_elempack, out_elempack); |
| 262 | |
| 263 | for (int q = 0; q + (out_elempack - 1) < num_output; q += out_elempack) |
| 264 | { |
| 265 | signed char* g0 = weight_data_tm.row<signed char>(q / out_elempack); |
| 266 | |
| 267 | for (int p = 0; p < num_input; p++) |
| 268 | { |
| 269 | for (int j = 0; j < out_elempack; j++) |
| 270 | { |
| 271 | *g0++ = weight_data_r2.row<signed char>(q + j)[p]; |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | scale_in_data.create(num_output); |
| 278 | for (int p = 0; p < num_output; p++) |
| 279 | { |
| 280 | // dequantize |
| 281 | float scale_in; |
| 282 | if (weight_data_int8_scales[p] == 0) |
| 283 | scale_in = 0; |
| 284 | else |
| 285 | scale_in = 1.f / (bottom_blob_int8_scales[0] * weight_data_int8_scales[p]); |
| 286 | |
| 287 | scale_in_data[p] = scale_in; |
| 288 | } |
| 289 | |
| 290 | if (opt.lightmode) |
| 291 | weight_data.release(); |
| 292 | |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | int InnerProduct_x86::forward_int8_x86(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 297 | { |
| 298 | const int num_input = weight_data_size / num_output; |
| 299 | |
| 300 | int elembits = bottom_blob.elembits(); |
| 301 | |
| 302 | Mat bottom_blob_int8 = bottom_blob; |
| 303 | if (elembits != 8) |
| 304 | { |
| 305 | Option opt_q = opt; |
| 306 | opt_q.blob_allocator = opt.workspace_allocator; |
| 307 | quantize_to_int8(bottom_blob, bottom_blob_int8, bottom_blob_int8_scales, opt_q); |
| 308 | if (bottom_blob_int8.empty()) |
| 309 | return -100; |
| 310 | } |
| 311 | |
| 312 | if (bottom_blob_int8.dims == 2 && bottom_blob_int8.w == num_input) |
| 313 | { |
| 314 | // gemm |
| 315 | Mat bottom_blob_int8_unpacked; |
| 316 | Option opt_unpack = opt; |
| 317 | opt_unpack.blob_allocator = opt.workspace_allocator; |
| 318 | convert_packing(bottom_blob_int8, bottom_blob_int8_unpacked, 1, opt_unpack); |
| 319 | if (bottom_blob_int8_unpacked.empty()) |
| 320 | return -100; |
| 321 | |
| 322 | int h = bottom_blob_int8_unpacked.h; |
| 323 | |
| 324 | int out_elempack = 1; |
| 325 | #if __SSE2__ |
| 326 | if (opt.use_packing_layout) |
| 327 | { |
| 328 | out_elempack = h % 4 == 0 ? 4 : 1; |
| 329 | } |
| 330 | #endif |
| 331 | |
| 332 | int outh = h / out_elempack; |
| 333 | |
| 334 | top_blob.create(num_output, outh, (size_t)(4u * out_elempack), out_elempack, opt.blob_allocator); |
| 335 | if (top_blob.empty()) |
| 336 | return -100; |
| 337 | |
| 338 | int num_output_elempack = 1; |
| 339 | #if __SSE2__ |
| 340 | if (opt.use_packing_layout) |
| 341 | { |
| 342 | num_output_elempack = num_output % 8 == 0 ? 8 : 1; |
| 343 | } |
| 344 | #endif |
| 345 | |
| 346 | #if __SSE2__ |
| 347 | if (num_output_elempack == 8 && out_elempack == 4) |
| 348 | { |
| 349 | #pragma omp parallel for num_threads(opt.num_threads) |
| 350 | for (int j = 0; j < outh; j++) |
| 351 | { |
| 352 | float* outptr = top_blob.row(j); |
| 353 | |
| 354 | for (int p = 0; p < num_output / num_output_elempack; p++) |
| 355 | { |
| 356 | const signed char* kptr = weight_data_tm.row<const signed char>(p); |
| 357 | const signed char* m0 = bottom_blob_int8_unpacked.row<const signed char>(j * 4); |
| 358 | const signed char* m1 = bottom_blob_int8_unpacked.row<const signed char>(j * 4 + 1); |
| 359 | const signed char* m2 = bottom_blob_int8_unpacked.row<const signed char>(j * 4 + 2); |
| 360 | const signed char* m3 = bottom_blob_int8_unpacked.row<const signed char>(j * 4 + 3); |
| 361 | |
| 362 | __m128i _sum00 = _mm_setzero_si128(); |
| 363 | __m128i _sum01 = _mm_setzero_si128(); |
| 364 | __m128i _sum10 = _mm_setzero_si128(); |
| 365 | __m128i _sum11 = _mm_setzero_si128(); |
| 366 | __m128i _sum20 = _mm_setzero_si128(); |
| 367 | __m128i _sum21 = _mm_setzero_si128(); |
| 368 | __m128i _sum30 = _mm_setzero_si128(); |
| 369 | __m128i _sum31 = _mm_setzero_si128(); |
| 370 | |
| 371 | int i = 0; |
| 372 | for (; i < num_input; i++) |
| 373 | { |
| 374 | // TODO use _mm_cvtepi8_epi16 on sse4.1 |
| 375 | __m128i _w = _mm_loadl_epi64((const __m128i*)kptr); |
| 376 | _w = _mm_unpacklo_epi8(_w, _mm_cmpgt_epi8(_mm_setzero_si128(), _w)); |
| 377 | |
| 378 | __m128i _val0 = _mm_set1_epi16((short)m0[0]); |
| 379 | __m128i _val1 = _mm_set1_epi16((short)m1[0]); |
| 380 | __m128i _val2 = _mm_set1_epi16((short)m2[0]); |
| 381 | __m128i _val3 = _mm_set1_epi16((short)m3[0]); |
| 382 | |
| 383 | __m128i _s0l = _mm_mullo_epi16(_val0, _w); |
| 384 | __m128i _s0h = _mm_mulhi_epi16(_val0, _w); |
| 385 | __m128i _s1l = _mm_mullo_epi16(_val1, _w); |
| 386 | __m128i _s1h = _mm_mulhi_epi16(_val1, _w); |
| 387 | __m128i _s2l = _mm_mullo_epi16(_val2, _w); |
| 388 | __m128i _s2h = _mm_mulhi_epi16(_val2, _w); |
| 389 | __m128i _s3l = _mm_mullo_epi16(_val3, _w); |
| 390 | __m128i _s3h = _mm_mulhi_epi16(_val3, _w); |
| 391 | __m128i _s00 = _mm_unpacklo_epi16(_s0l, _s0h); |
| 392 | __m128i _s01 = _mm_unpackhi_epi16(_s0l, _s0h); |
| 393 | __m128i _s10 = _mm_unpacklo_epi16(_s1l, _s1h); |
| 394 | __m128i _s11 = _mm_unpackhi_epi16(_s1l, _s1h); |
| 395 | __m128i _s20 = _mm_unpacklo_epi16(_s2l, _s2h); |
| 396 | __m128i _s21 = _mm_unpackhi_epi16(_s2l, _s2h); |
| 397 | __m128i _s30 = _mm_unpacklo_epi16(_s3l, _s3h); |
| 398 | __m128i _s31 = _mm_unpackhi_epi16(_s3l, _s3h); |
| 399 | |
| 400 | _sum00 = _mm_add_epi32(_sum00, _s00); |
| 401 | _sum01 = _mm_add_epi32(_sum01, _s01); |
| 402 | _sum10 = _mm_add_epi32(_sum10, _s10); |
| 403 | _sum11 = _mm_add_epi32(_sum11, _s11); |
| 404 | _sum20 = _mm_add_epi32(_sum20, _s20); |
| 405 | _sum21 = _mm_add_epi32(_sum21, _s21); |
| 406 | _sum30 = _mm_add_epi32(_sum30, _s30); |
| 407 | _sum31 = _mm_add_epi32(_sum31, _s31); |
| 408 | |
| 409 | m0++; |
| 410 | m1++; |
| 411 | m2++; |
| 412 | m3++; |
| 413 | kptr += 8; |
| 414 | } |
| 415 | |
| 416 | // dequantize and relu |
| 417 | __m128 _scale_in0 = _mm_loadu_ps((const float*)scale_in_data + p * 8); |
| 418 | __m128 _scale_in1 = _mm_loadu_ps((const float*)scale_in_data + p * 8 + 4); |
| 419 | |
| 420 | __m128 _sumfp32_00 = _mm_cvtepi32_ps(_sum00); |
| 421 | __m128 _sumfp32_01 = _mm_cvtepi32_ps(_sum01); |
| 422 | __m128 _sumfp32_10 = _mm_cvtepi32_ps(_sum10); |
| 423 | __m128 _sumfp32_11 = _mm_cvtepi32_ps(_sum11); |
| 424 | __m128 _sumfp32_20 = _mm_cvtepi32_ps(_sum20); |
| 425 | __m128 _sumfp32_21 = _mm_cvtepi32_ps(_sum21); |
| 426 | __m128 _sumfp32_30 = _mm_cvtepi32_ps(_sum30); |
| 427 | __m128 _sumfp32_31 = _mm_cvtepi32_ps(_sum31); |
| 428 | if (bias_term) |
| 429 | { |
| 430 | __m128 _bias0 = _mm_loadu_ps((const float*)bias_data + p * 8); |
| 431 | __m128 _bias1 = _mm_loadu_ps((const float*)bias_data + p * 8 + 4); |
| 432 | _sumfp32_00 = _mm_add_ps(_bias0, _mm_mul_ps(_sumfp32_00, _scale_in0)); |
| 433 | _sumfp32_01 = _mm_add_ps(_bias1, _mm_mul_ps(_sumfp32_01, _scale_in1)); |
| 434 | _sumfp32_10 = _mm_add_ps(_bias0, _mm_mul_ps(_sumfp32_10, _scale_in0)); |
| 435 | _sumfp32_11 = _mm_add_ps(_bias1, _mm_mul_ps(_sumfp32_11, _scale_in1)); |
| 436 | _sumfp32_20 = _mm_add_ps(_bias0, _mm_mul_ps(_sumfp32_20, _scale_in0)); |
| 437 | _sumfp32_21 = _mm_add_ps(_bias1, _mm_mul_ps(_sumfp32_21, _scale_in1)); |
| 438 | _sumfp32_30 = _mm_add_ps(_bias0, _mm_mul_ps(_sumfp32_30, _scale_in0)); |
| 439 | _sumfp32_31 = _mm_add_ps(_bias1, _mm_mul_ps(_sumfp32_31, _scale_in1)); |
| 440 | } |
| 441 | else |
| 442 | { |
| 443 | _sumfp32_00 = _mm_mul_ps(_sumfp32_00, _scale_in0); |
| 444 | _sumfp32_01 = _mm_mul_ps(_sumfp32_01, _scale_in1); |
| 445 | _sumfp32_10 = _mm_mul_ps(_sumfp32_10, _scale_in0); |
| 446 | _sumfp32_11 = _mm_mul_ps(_sumfp32_11, _scale_in1); |
| 447 | _sumfp32_20 = _mm_mul_ps(_sumfp32_20, _scale_in0); |
| 448 | _sumfp32_21 = _mm_mul_ps(_sumfp32_21, _scale_in1); |
| 449 | _sumfp32_30 = _mm_mul_ps(_sumfp32_30, _scale_in0); |
| 450 | _sumfp32_31 = _mm_mul_ps(_sumfp32_31, _scale_in1); |
| 451 | } |
| 452 | |
| 453 | _sumfp32_00 = activation_sse(_sumfp32_00, activation_type, activation_params); |
| 454 | _sumfp32_01 = activation_sse(_sumfp32_01, activation_type, activation_params); |
| 455 | _sumfp32_10 = activation_sse(_sumfp32_10, activation_type, activation_params); |
| 456 | _sumfp32_11 = activation_sse(_sumfp32_11, activation_type, activation_params); |
| 457 | _sumfp32_20 = activation_sse(_sumfp32_20, activation_type, activation_params); |
| 458 | _sumfp32_21 = activation_sse(_sumfp32_21, activation_type, activation_params); |
| 459 | _sumfp32_30 = activation_sse(_sumfp32_30, activation_type, activation_params); |
| 460 | _sumfp32_31 = activation_sse(_sumfp32_31, activation_type, activation_params); |
| 461 | |
| 462 | // transpose 4x8 |
| 463 | _MM_TRANSPOSE4_PS(_sumfp32_00, _sumfp32_10, _sumfp32_20, _sumfp32_30); |
| 464 | _MM_TRANSPOSE4_PS(_sumfp32_01, _sumfp32_11, _sumfp32_21, _sumfp32_31); |
| 465 | |
| 466 | _mm_storeu_ps(outptr, _sumfp32_00); |
| 467 | _mm_storeu_ps(outptr + 4, _sumfp32_10); |
| 468 | _mm_storeu_ps(outptr + 8, _sumfp32_20); |
| 469 | _mm_storeu_ps(outptr + 12, _sumfp32_30); |
| 470 | _mm_storeu_ps(outptr + 16, _sumfp32_01); |
| 471 | _mm_storeu_ps(outptr + 20, _sumfp32_11); |
| 472 | _mm_storeu_ps(outptr + 24, _sumfp32_21); |
| 473 | _mm_storeu_ps(outptr + 28, _sumfp32_31); |
| 474 | |
| 475 | outptr += 32; |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | if (num_output_elempack == 1 && out_elempack == 4) |
| 481 | { |
| 482 | #pragma omp parallel for num_threads(opt.num_threads) |
| 483 | for (int j = 0; j < outh; j++) |
| 484 | { |
| 485 | float* outptr = top_blob.row(j); |
| 486 | |
| 487 | for (int p = 0; p < num_output; p++) |
| 488 | { |
| 489 | const signed char* kptr = weight_data_tm.row<const signed char>(p); |
| 490 | const signed char* m0 = bottom_blob_int8_unpacked.row<const signed char>(j * 4); |
| 491 | const signed char* m1 = bottom_blob_int8_unpacked.row<const signed char>(j * 4 + 1); |
| 492 | const signed char* m2 = bottom_blob_int8_unpacked.row<const signed char>(j * 4 + 2); |
| 493 | const signed char* m3 = bottom_blob_int8_unpacked.row<const signed char>(j * 4 + 3); |
| 494 | |
| 495 | int sum0 = 0; |
| 496 | int sum1 = 0; |
| 497 | int sum2 = 0; |
| 498 | int sum3 = 0; |
| 499 | |
| 500 | int i = 0; |
| 501 | for (; i < num_input; i++) |
| 502 | { |
| 503 | sum0 += *m0++ * kptr[0]; |
| 504 | sum1 += *m1++ * kptr[0]; |
| 505 | sum2 += *m2++ * kptr[0]; |
| 506 | sum3 += *m3++ * kptr[0]; |
| 507 | kptr += 1; |
| 508 | } |
| 509 | |
| 510 | // dequantize and relu |
| 511 | float sumfp32_0 = sum0 * scale_in_data[p]; |
| 512 | float sumfp32_1 = sum1 * scale_in_data[p]; |
| 513 | float sumfp32_2 = sum2 * scale_in_data[p]; |
| 514 | float sumfp32_3 = sum3 * scale_in_data[p]; |
| 515 | |
| 516 | if (bias_term) |
| 517 | { |
| 518 | sumfp32_0 += bias_data[p]; |
| 519 | sumfp32_1 += bias_data[p]; |
| 520 | sumfp32_2 += bias_data[p]; |
| 521 | sumfp32_3 += bias_data[p]; |
| 522 | } |
| 523 | |
| 524 | outptr[0] = activation_ss(sumfp32_0, activation_type, activation_params); |
| 525 | outptr[1] = activation_ss(sumfp32_1, activation_type, activation_params); |
| 526 | outptr[2] = activation_ss(sumfp32_2, activation_type, activation_params); |
| 527 | outptr[3] = activation_ss(sumfp32_3, activation_type, activation_params); |
| 528 | outptr += 4; |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | if (num_output_elempack == 8 && out_elempack == 1) |
| 534 | { |
| 535 | #pragma omp parallel for num_threads(opt.num_threads) |
| 536 | for (int j = 0; j < outh; j++) |
| 537 | { |
| 538 | float* outptr = top_blob.row(j); |
| 539 | |
| 540 | for (int p = 0; p < num_output / num_output_elempack; p++) |
| 541 | { |
| 542 | const signed char* kptr = weight_data_tm.row<const signed char>(p); |
| 543 | const signed char* m = bottom_blob_int8_unpacked.row<const signed char>(j); |
| 544 | |
| 545 | __m128i _sum0 = _mm_setzero_si128(); |
| 546 | __m128i _sum1 = _mm_setzero_si128(); |
| 547 | |
| 548 | int i = 0; |
| 549 | for (; i < num_input; i++) |
| 550 | { |
| 551 | __m128i _val = _mm_set1_epi16((short)m[0]); |
| 552 | |
| 553 | // TODO use _mm_cvtepi8_epi16 on sse4.1 |
| 554 | __m128i _w = _mm_loadl_epi64((const __m128i*)kptr); |
| 555 | _w = _mm_unpacklo_epi8(_w, _mm_cmpgt_epi8(_mm_setzero_si128(), _w)); |
| 556 | |
| 557 | __m128i _sl = _mm_mullo_epi16(_val, _w); |
| 558 | __m128i _sh = _mm_mulhi_epi16(_val, _w); |
| 559 | __m128i _s0 = _mm_unpacklo_epi16(_sl, _sh); |
| 560 | __m128i _s1 = _mm_unpackhi_epi16(_sl, _sh); |
| 561 | |
| 562 | _sum0 = _mm_add_epi32(_sum0, _s0); |
| 563 | _sum1 = _mm_add_epi32(_sum1, _s1); |
| 564 | |
| 565 | m++; |
| 566 | kptr += 8; |
| 567 | } |
| 568 | |
| 569 | // dequantize and relu |
| 570 | __m128 _scale_in0 = _mm_loadu_ps((const float*)scale_in_data + p * 8); |
| 571 | __m128 _scale_in1 = _mm_loadu_ps((const float*)scale_in_data + p * 8 + 4); |
| 572 | |
| 573 | __m128 _sumfp32_0 = _mm_cvtepi32_ps(_sum0); |
| 574 | __m128 _sumfp32_1 = _mm_cvtepi32_ps(_sum1); |
| 575 | |
| 576 | if (bias_term) |
| 577 | { |
| 578 | __m128 _bias0 = _mm_loadu_ps((const float*)bias_data + p * 8); |
| 579 | __m128 _bias1 = _mm_loadu_ps((const float*)bias_data + p * 8 + 4); |
| 580 | _sumfp32_0 = _mm_add_ps(_bias0, _mm_mul_ps(_sumfp32_0, _scale_in0)); |
| 581 | _sumfp32_1 = _mm_add_ps(_bias1, _mm_mul_ps(_sumfp32_1, _scale_in1)); |
| 582 | } |
| 583 | else |
| 584 | { |
| 585 | _sumfp32_0 = _mm_mul_ps(_sumfp32_0, _scale_in0); |
| 586 | _sumfp32_1 = _mm_mul_ps(_sumfp32_1, _scale_in1); |
| 587 | } |
| 588 | |
| 589 | _sumfp32_0 = activation_sse(_sumfp32_0, activation_type, activation_params); |
| 590 | _sumfp32_1 = activation_sse(_sumfp32_1, activation_type, activation_params); |
| 591 | |
| 592 | _mm_storeu_ps(outptr, _sumfp32_0); |
| 593 | _mm_storeu_ps(outptr + 4, _sumfp32_1); |
| 594 | outptr += 8; |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | #endif // __SSE2__ |
| 599 | |
| 600 | if (num_output_elempack == 1 && out_elempack == 1) |
| 601 | { |
| 602 | #pragma omp parallel for num_threads(opt.num_threads) |
| 603 | for (int j = 0; j < outh; j++) |
| 604 | { |
| 605 | float* outptr = top_blob.row(j); |
| 606 | |
| 607 | for (int p = 0; p < num_output; p++) |
| 608 | { |
| 609 | const signed char* kptr = weight_data_tm.row<const signed char>(p); |
| 610 | const signed char* m = bottom_blob_int8_unpacked.row<const signed char>(j); |
| 611 | |
| 612 | int sum = 0; |
| 613 | |
| 614 | int i = 0; |
| 615 | for (; i < num_input; i++) |
| 616 | { |
| 617 | sum += *m++ * *kptr++; |
| 618 | } |
| 619 | |
| 620 | // dequantize and relu |
| 621 | float sumfp32 = sum * scale_in_data[p]; |
| 622 | |
| 623 | if (bias_term) |
| 624 | sumfp32 += bias_data[p]; |
| 625 | |
| 626 | outptr[0] = activation_ss(sumfp32, activation_type, activation_params); |
| 627 | outptr += 1; |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | return 0; |
| 633 | } |
| 634 | |
| 635 | Mat bottom_blob_int8_flattened = bottom_blob_int8; |
| 636 | if (bottom_blob_int8.dims != 1) |
| 637 | { |
| 638 | Option opt_flatten = opt; |
| 639 | opt_flatten.blob_allocator = opt.workspace_allocator; |
| 640 | flatten->forward(bottom_blob_int8, bottom_blob_int8_flattened, opt_flatten); |
| 641 | if (bottom_blob_int8_flattened.empty()) |
| 642 | return -100; |
| 643 | } |
| 644 | |
| 645 | // int elempack = bottom_blob_int8_flattened.elempack; |
| 646 | |
| 647 | int out_elempack = 1; |
| 648 | #if __SSE2__ |
| 649 | if (opt.use_packing_layout) |
| 650 | { |
| 651 | out_elempack = num_output % 8 == 0 ? 8 : 1; |
| 652 | } |
| 653 | #endif // __SSE2__ |
| 654 | // size_t out_elemsize = elemsize / elempack * out_elempack; |
| 655 | |
| 656 | top_blob.create(num_output / out_elempack, (size_t)(4u * out_elempack), out_elempack, opt.blob_allocator); |
| 657 | if (top_blob.empty()) |
| 658 | return -100; |
| 659 | |
| 660 | #if __SSE2__ |
| 661 | if (out_elempack == 8) |
| 662 | { |
| 663 | #pragma omp parallel for num_threads(opt.num_threads) |
| 664 | for (int p = 0; p < num_output / out_elempack; p++) |
| 665 | { |
| 666 | __m128i _sum0 = _mm_setzero_si128(); |
| 667 | __m128i _sum1 = _mm_setzero_si128(); |
| 668 | |
| 669 | const signed char* kptr = weight_data_tm.row<const signed char>(p); |
| 670 | const signed char* sptr = bottom_blob_int8_flattened; |
| 671 | |
| 672 | int i = 0; |
| 673 | for (; i < num_input; i++) |
| 674 | { |
| 675 | __m128i _val = _mm_set1_epi16((short)sptr[0]); |
| 676 | |
| 677 | // TODO use _mm_cvtepi8_epi16 on sse4.1 |
| 678 | __m128i _w = _mm_loadl_epi64((const __m128i*)kptr); |
| 679 | _w = _mm_unpacklo_epi8(_w, _mm_cmpgt_epi8(_mm_setzero_si128(), _w)); |
| 680 | |
| 681 | __m128i _sl = _mm_mullo_epi16(_val, _w); |
| 682 | __m128i _sh = _mm_mulhi_epi16(_val, _w); |
| 683 | __m128i _s0 = _mm_unpacklo_epi16(_sl, _sh); |
| 684 | __m128i _s1 = _mm_unpackhi_epi16(_sl, _sh); |
| 685 | |
| 686 | _sum0 = _mm_add_epi32(_sum0, _s0); |
| 687 | _sum1 = _mm_add_epi32(_sum1, _s1); |
| 688 | |
| 689 | sptr += 1; |
| 690 | kptr += 8; |
| 691 | } |
| 692 | |
| 693 | // dequantize and relu |
| 694 | __m128 _scale_in0 = _mm_loadu_ps((const float*)scale_in_data + p * 8); |
| 695 | __m128 _scale_in1 = _mm_loadu_ps((const float*)scale_in_data + p * 8 + 4); |
| 696 | |
| 697 | __m128 _sumfp32_0 = _mm_cvtepi32_ps(_sum0); |
| 698 | __m128 _sumfp32_1 = _mm_cvtepi32_ps(_sum1); |
| 699 | |
| 700 | if (bias_term) |
| 701 | { |
| 702 | __m128 _bias0 = _mm_loadu_ps((const float*)bias_data + p * 8); |
| 703 | __m128 _bias1 = _mm_loadu_ps((const float*)bias_data + p * 8 + 4); |
| 704 | _sumfp32_0 = _mm_add_ps(_bias0, _mm_mul_ps(_sumfp32_0, _scale_in0)); |
| 705 | _sumfp32_1 = _mm_add_ps(_bias1, _mm_mul_ps(_sumfp32_1, _scale_in1)); |
| 706 | } |
| 707 | else |
| 708 | { |
| 709 | _sumfp32_0 = _mm_mul_ps(_sumfp32_0, _scale_in0); |
| 710 | _sumfp32_1 = _mm_mul_ps(_sumfp32_1, _scale_in1); |
| 711 | } |
| 712 | |
| 713 | _sumfp32_0 = activation_sse(_sumfp32_0, activation_type, activation_params); |
| 714 | _sumfp32_1 = activation_sse(_sumfp32_1, activation_type, activation_params); |
| 715 | |
| 716 | float* outptr = (float*)top_blob + p * 8; |
| 717 | _mm_storeu_ps(outptr, _sumfp32_0); |
| 718 | _mm_storeu_ps(outptr + 4, _sumfp32_1); |
| 719 | } |
| 720 | } |
| 721 | #endif // __SSE2__ |
| 722 | |
| 723 | if (out_elempack == 1) |
| 724 | { |
| 725 | #pragma omp parallel for num_threads(opt.num_threads) |
| 726 | for (int p = 0; p < num_output / out_elempack; p++) |
| 727 | { |
| 728 | int sum = 0; |
| 729 | |
| 730 | const signed char* kptr = weight_data_tm.row<const signed char>(p); |
| 731 | const signed char* sptr = bottom_blob_int8_flattened; |
| 732 | |
| 733 | int i = 0; |
| 734 | for (; i < num_input; i++) |
| 735 | { |
| 736 | signed char val = sptr[0]; |
| 737 | |
| 738 | signed char w = kptr[0]; |
| 739 | |
| 740 | sum += val * w; |
| 741 | |
| 742 | sptr += 1; |
| 743 | kptr += 1; |
| 744 | } |
| 745 | |
| 746 | // dequantize and relu |
| 747 | float sumfp32 = sum * scale_in_data[p]; |
| 748 | |
| 749 | if (bias_term) |
| 750 | sumfp32 += bias_data[p]; |
| 751 | |
| 752 | sumfp32 = activation_ss(sumfp32, activation_type, activation_params); |
| 753 | |
| 754 | top_blob[p] = sumfp32; |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | return 0; |
| 759 | } |
| 760 | #endif // NCNN_INT8 |
| 761 | |
| 762 | } // namespace ncnn |