// Tencent is pleased to support the open source community by making ncnn available. // // Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. // // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except // in compliance with the License. You may obtain a copy of the License at // // https://opensource.org/licenses/BSD-3-Clause // // Unless required by applicable law or agreed to in writing, software distributed // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR // CONDITIONS OF ANY KIND, either express or implied. See the License for the // specific language governing permissions and limitations under the License. #include "convolution_arm.h" #include "benchmark.h" #include "cpu.h" #include "layer_type.h" #if __ARM_NEON #include #include "neon_mathfun.h" #include "neon_activation.h" #endif // __ARM_NEON namespace ncnn { #include "convolution_1x1.h" #include "convolution_2x2.h" #include "convolution_3x3.h" #include "convolution_4x4.h" #include "convolution_5x5.h" #include "convolution_7x7.h" #include "convolution_sgemm.h" #include "convolution_sgemm_int8.h" #include "convolution_1x1_int8.h" #include "convolution_3x3_int8.h" #include "convolution_1x1_bf16s.h" #if __ARM_NEON #include "convolution_1x1_pack4.h" #include "convolution_1x1_pack4to1.h" #include "convolution_3x3_pack4.h" #include "convolution_3x3_pack1to4.h" #include "convolution_3x3_pack4to1.h" #include "convolution_5x5_pack4.h" #include "convolution_7x7_pack1to4.h" #include "convolution_1x1_pack4_bf16s.h" #include "convolution_1x1_pack4to1_bf16s.h" #include "convolution_3x3_pack4_bf16s.h" #include "convolution_3x3_pack1to4_bf16s.h" #include "convolution_3x3_pack4to1_bf16s.h" #include "convolution_5x5_pack4_bf16s.h" #include "convolution_7x7_pack1to4_bf16s.h" #endif // __ARM_NEON DEFINE_LAYER_CREATOR(Convolution_arm) Convolution_arm::Convolution_arm() { #if __ARM_NEON support_packing = true; #endif // __ARM_NEON support_bf16_storage = true; activation = 0; convolution_dilation1 = 0; } int Convolution_arm::create_pipeline(const Option& opt) { if (activation_type == 1) { activation = ncnn::create_layer(ncnn::LayerType::ReLU); ncnn::ParamDict pd; activation->load_param(pd); } else if (activation_type == 2) { activation = ncnn::create_layer(ncnn::LayerType::ReLU); ncnn::ParamDict pd; pd.set(0, activation_params[0]);// slope activation->load_param(pd); } else if (activation_type == 3) { activation = ncnn::create_layer(ncnn::LayerType::Clip); ncnn::ParamDict pd; pd.set(0, activation_params[0]);// min pd.set(1, activation_params[1]);// max activation->load_param(pd); } else if (activation_type == 4) { activation = ncnn::create_layer(ncnn::LayerType::Sigmoid); ncnn::ParamDict pd; activation->load_param(pd); } if (activation) { activation->create_pipeline(opt); } if (opt.use_bf16_storage) { return create_pipeline_bf16s(opt); } if (opt.use_int8_inference && weight_data.elemsize == (size_t)1u) { support_packing = false; return create_pipeline_int8_arm(opt); } if (opt.use_packing_layout == false && kernel_w == kernel_h && dilation_w != 1 && dilation_h == dilation_w && stride_w == 1 && stride_h == 1) { convolution_dilation1 = ncnn::create_layer(ncnn::LayerType::Convolution); // set param ncnn::ParamDict pd; pd.set(0, num_output);// num_output pd.set(1, kernel_w); pd.set(11, kernel_h); pd.set(2, 1); pd.set(12, 1); pd.set(3, 1);// stride_w pd.set(13, 1);// stride_h pd.set(4, 0);// pad_w pd.set(14, 0);// pad_h pd.set(5, bias_term); pd.set(6, weight_data_size); convolution_dilation1->load_param(pd); // set weights if (bias_term) { ncnn::Mat weights[2]; weights[0] = weight_data; weights[1] = bias_data; convolution_dilation1->load_model(ModelBinFromMatArray(weights)); } else { ncnn::Mat weights[1]; weights[0] = weight_data; convolution_dilation1->load_model(ModelBinFromMatArray(weights)); } convolution_dilation1->create_pipeline(opt); return 0; } const int maxk = kernel_w * kernel_h; const int num_input = weight_data_size / maxk / num_output; int elempack = (opt.use_packing_layout && num_input % 4 == 0) ? 4 : 1; int out_elempack = (opt.use_packing_layout && num_output % 4 == 0) ? 4 : 1; #if __ARM_NEON // pack4 if (elempack == 4 && out_elempack == 4) { if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) { conv1x1s1_sgemm_transform_kernel_pack4_neon(weight_data, weight_data_pack4, num_input, num_output); } else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) { conv1x1s1_sgemm_transform_kernel_pack4_neon(weight_data, weight_data_pack4, num_input, num_output); } else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) { conv3x3s1_winograd64_transform_kernel_pack4_neon(weight_data, weight_data_pack4, num_input, num_output); } else { // src = kw-kh-inch-outch // dst = 4b-4a-kw-kh-inch/4a-outch/4b Mat weight_data_r2 = weight_data.reshape(maxk, num_input, num_output); weight_data_pack4.create(maxk, num_input/4, num_output/4, (size_t)4*16, 16); for (int q=0; q+3= 16 && num_output >= 16) use_winograd3x3 = true; if (use_winograd3x3) { // conv3x3s1_winograd64_transform_kernel_neon(weight_data, weight_3x3_winograd64_data, num_input, num_output); conv3x3s1_winograd64_transform_kernel_neon5(weight_data, weight_3x3_winograd64_data, num_input, num_output); } } // TODO assume more proper condition if (opt.use_sgemm_convolution && kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) { if (num_input >= 64 && num_output >= 64) use_sgemm1x1 = true; if (use_sgemm1x1) { conv1x1s1_sgemm_transform_kernel_neon(weight_data, weight_1x1_sgemm_data, num_input, num_output); } } if (impl_type > 0 && impl_type < 6 && impl_type != 4) { switch (impl_type) { case 1: // winograd conv3x3s1_winograd64_transform_kernel_neon5(weight_data, weight_3x3_winograd64_data, num_input, num_output); break; case 2: // pointwise conv1x1s1_sgemm_transform_kernel_neon(weight_data, weight_1x1_sgemm_data, num_input, num_output); break; case 3: // im2col conv_im2col_sgemm_transform_kernel_neon(weight_data, weight_sgemm_data, num_input, num_output, maxk); break; // case 4: // // direct // break; case 5: // conv3x3s2 conv3x3s2_transform_kernel_neon(weight_data, weight_3x3s2_data, num_input, num_output); break; } } if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) { conv3x3s2_transform_kernel_neon(weight_data, weight_3x3s2_data, num_input, num_output); } if (opt.use_sgemm_convolution && kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) { conv_im2col_sgemm_transform_kernel_neon(weight_data, weight_sgemm_data, num_input, num_output, maxk); } if (opt.use_sgemm_convolution && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) { conv_im2col_sgemm_transform_kernel_neon(weight_data, weight_sgemm_data, num_input, num_output, maxk); } } return 0; } int Convolution_arm::destroy_pipeline(const Option& opt) { if (activation) { activation->destroy_pipeline(opt); delete activation; activation = 0; } if (convolution_dilation1) { convolution_dilation1->destroy_pipeline(opt); delete convolution_dilation1; convolution_dilation1 = 0; } return 0; } int Convolution_arm::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const { if (bottom_blob.dims != 3) { return Convolution::forward(bottom_blob, top_blob, opt); } if (opt.use_int8_inference && weight_data.elemsize == (size_t)1u) { return forward_int8_arm(bottom_blob, top_blob, opt); } if (opt.use_bf16_storage) return forward_bf16s(bottom_blob, top_blob, opt); int w = bottom_blob.w; int h = bottom_blob.h; int channels = bottom_blob.c; size_t elemsize = bottom_blob.elemsize; int elempack = bottom_blob.elempack; // fprintf(stderr, "Convolution input %d x %d pad = %d %d ksize=%d %d stride=%d %d\n", w, h, pad_w, pad_h, kernel_w, kernel_h, stride_w, stride_h); const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1; Mat bottom_blob_bordered; make_padding(bottom_blob, bottom_blob_bordered, opt); if (bottom_blob_bordered.empty()) return -100; w = bottom_blob_bordered.w; h = bottom_blob_bordered.h; int outw = (w - kernel_extent_w) / stride_w + 1; int outh = (h - kernel_extent_h) / stride_h + 1; int out_elempack = (opt.use_packing_layout && num_output % 4 == 0) ? 4 : 1; size_t out_elemsize = elemsize / elempack * out_elempack; top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); if (top_blob.empty()) return -100; if (opt.use_packing_layout == false && kernel_w == kernel_h && dilation_w != 1 && dilation_h == dilation_w && stride_w == 1 && stride_h == 1) { return forwardDilation_arm(bottom_blob_bordered, top_blob, opt); } const int maxk = kernel_w * kernel_h; // kernel offsets std::vector _space_ofs(maxk); int* space_ofs = &_space_ofs[0]; { int p1 = 0; int p2 = 0; int gap = w * dilation_h - kernel_w * dilation_w; for (int i = 0; i < kernel_h; i++) { for (int j = 0; j < kernel_w; j++) { space_ofs[p1] = p2; p1++; p2 += dilation_w; } p2 += gap; } } #if __ARM_NEON if (elempack == 4 && out_elempack == 4) { if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) { conv1x1s1_sgemm_pack4_neon(bottom_blob_bordered, top_blob, weight_data_pack4, bias_data, opt); if (activation) { activation->forward_inplace(top_blob, opt); } } else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) { conv1x1s2_pack4_neon(bottom_blob_bordered, top_blob, weight_data_pack4, bias_data, opt); if (activation) { activation->forward_inplace(top_blob, opt); } } else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) { conv3x3s1_winograd64_pack4_neon(bottom_blob_bordered, top_blob, weight_data_pack4, bias_data, opt); if (activation) { activation->forward_inplace(top_blob, opt); } } else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) { conv3x3s2_pack4_neon(bottom_blob_bordered, top_blob, weight_data_pack4, bias_data, opt); if (activation) { activation->forward_inplace(top_blob, opt); } } else if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) { conv5x5s1_pack4_neon(bottom_blob_bordered, top_blob, weight_data_pack4, bias_data, opt); if (activation) { activation->forward_inplace(top_blob, opt); } } else if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) { conv5x5s2_pack4_neon(bottom_blob_bordered, top_blob, weight_data_pack4, bias_data, opt); if (activation) { activation->forward_inplace(top_blob, opt); } } else { // num_output #pragma omp parallel for num_threads(opt.num_threads) for (int p=0; pforward_inplace(top_blob, opt); } } else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) { conv3x3s2_pack1to4_neon(bottom_blob_bordered, top_blob, weight_data_pack1to4, bias_data, opt); if (activation) { activation->forward_inplace(top_blob, opt); } } else if (kernel_w == 7 && kernel_h == 7 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) { conv7x7s2_pack1to4_neon(bottom_blob_bordered, top_blob, weight_data_pack1to4, bias_data, opt); if (activation) { activation->forward_inplace(top_blob, opt); } } else { // num_output #pragma omp parallel for num_threads(opt.num_threads) for (int p=0; pforward_inplace(top_blob, opt); } } else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) { conv1x1s2_pack4to1_neon(bottom_blob_bordered, top_blob, weight_data_pack4to1, bias_data, opt); if (activation) { activation->forward_inplace(top_blob, opt); } } else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) { // TODO more proper condition conv3x3s1_winograd64_pack4to1_neon(bottom_blob_bordered, top_blob, weight_data_pack4to1, bias_data, opt); // conv3x3s1_pack4to1_neon(bottom_blob_bordered, top_blob, weight_data_pack4to1, bias_data, opt); if (activation) { activation->forward_inplace(top_blob, opt); } } else { // num_output #pragma omp parallel for num_threads(opt.num_threads) for (int p=0; p 0 && impl_type < 6 && impl_type != 4) { // engineering is magic. switch (impl_type) { case 1: conv3x3s1_winograd64_neon5(bottom_blob_bordered, top_blob, weight_3x3_winograd64_data, bias_data, opt); break; case 2: conv1x1s1_sgemm_neon(bottom_blob_bordered, top_blob, weight_1x1_sgemm_data, bias_data, opt); break; case 3: conv_im2col_sgemm_neon(bottom_blob_bordered, top_blob, weight_sgemm_data, bias_data, kernel_w, kernel_h, stride_w, stride_h, opt); break; // case 4: FIXME fallback to auto path // conv(bottom_blob_bordered, top_blob, weight_data, bias_data, opt); // break; case 5: conv3x3s2_packed_neon(bottom_blob_bordered, top_blob, weight_3x3s2_data, bias_data, opt); break; } if (activation) { activation->forward_inplace(top_blob, opt); } } else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) { if (use_sgemm1x1) { conv1x1s1_sgemm_neon(bottom_blob_bordered, top_blob, weight_1x1_sgemm_data, bias_data, opt); } else { conv1x1s1_neon(bottom_blob_bordered, top_blob, weight_data, bias_data, opt); } if (activation) { activation->forward_inplace(top_blob, opt); } } else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) { if (opt.use_sgemm_convolution) conv_im2col_sgemm_neon(bottom_blob_bordered, top_blob, weight_sgemm_data, bias_data, kernel_w, kernel_h, stride_w, stride_h, opt); else conv1x1s2_neon(bottom_blob_bordered, top_blob, weight_data, bias_data, opt); if (activation) { activation->forward_inplace(top_blob, opt); } } else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) { if (use_winograd3x3 && w <= 120 && h <= 120) { // conv3x3s1_winograd64_neon4(bottom_blob_bordered, top_blob, weight_3x3_winograd64_data, bias_data, opt); conv3x3s1_winograd64_neon5(bottom_blob_bordered, top_blob, weight_3x3_winograd64_data, bias_data, opt); } else { conv3x3s1_neon(bottom_blob_bordered, top_blob, weight_data, bias_data, opt); } if (activation) { activation->forward_inplace(top_blob, opt); } } else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) { if (opt.use_sgemm_convolution && !(outw >=8 && outh >=8)) conv_im2col_sgemm_neon(bottom_blob_bordered, top_blob, weight_sgemm_data, bias_data, kernel_w, kernel_h, stride_w, stride_h, opt); else conv3x3s2_packed_neon(bottom_blob_bordered, top_blob, weight_3x3s2_data, bias_data, opt); if (activation) { activation->forward_inplace(top_blob, opt); } } else if (kernel_w == 4 && kernel_h == 4 && dilation_w == 1 && dilation_h == 1 && stride_w == 4 && stride_h == 4) { conv4x4s4_neon(bottom_blob_bordered, top_blob, weight_data, bias_data, opt); if (activation) { activation->forward_inplace(top_blob, opt); } } else if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) { conv5x5s1_neon(bottom_blob_bordered, top_blob, weight_data, bias_data, opt); if (activation) { activation->forward_inplace(top_blob, opt); } } else if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) { conv5x5s2_neon(bottom_blob_bordered, top_blob, weight_data, bias_data, opt); if (activation) { activation->forward_inplace(top_blob, opt); } } else if (kernel_w == 7 && kernel_h == 7 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) { conv7x7s1_neon(bottom_blob_bordered, top_blob, weight_data, bias_data, opt); if (activation) { activation->forward_inplace(top_blob, opt); } } else if (kernel_w == 7 && kernel_h == 7 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) { conv7x7s2_neon(bottom_blob_bordered, top_blob, weight_data, bias_data, opt); if (activation) { activation->forward_inplace(top_blob, opt); } } else { // num_output #pragma omp parallel for num_threads(opt.num_threads) for (int p=0; p 0.f ? sum : sum * slope; } else if (activation_type == 3) { float min = activation_params[0]; float max = activation_params[1]; if (sum < min) sum = min; if (sum > max) sum = max; } else if (activation_type == 4) { sum = static_cast(1.f / (1.f + exp(-sum))); } outptr[j] = sum; } outptr += outw; } } } } return 0; } int Convolution_arm::create_pipeline_bf16s(const Option& opt) { const int maxk = kernel_w * kernel_h; const int num_input = weight_data_size / maxk / num_output; int elempack = (opt.use_packing_layout && num_input % 4 == 0) ? 4 : 1; int out_elempack = (opt.use_packing_layout && num_output % 4 == 0) ? 4 : 1; #if __ARM_NEON // pack4 if (elempack == 4 && out_elempack == 4) { if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) { conv1x1s1_sgemm_transform_kernel_pack4_bf16s_neon(weight_data, weight_data_pack4_bf16, num_input, num_output); } else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) { conv1x1s1_sgemm_transform_kernel_pack4_bf16s_neon(weight_data, weight_data_pack4_bf16, num_input, num_output); } else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) { conv3x3s1_winograd64_transform_kernel_pack4_neon(weight_data, weight_data_pack4_bf16, num_input, num_output); } else { // src = kw-kh-inch-outch // dst = 4b-4a-kw-kh-inch/4a-outch/4b Mat weight_data_r2 = weight_data.reshape(maxk, num_input, num_output); weight_data_pack4_bf16.create(maxk, num_input/4, num_output/4, (size_t)2*16, 16); for (int q=0; q+3(p/4); for (int k=0; k(p); for (int k=0; k(p/4); for (int k=0; k _space_ofs(maxk); int* space_ofs = &_space_ofs[0]; { int p1 = 0; int p2 = 0; int gap = w * dilation_h - kernel_w * dilation_w; for (int i = 0; i < kernel_h; i++) { for (int j = 0; j < kernel_w; j++) { space_ofs[p1] = p2; p1++; p2 += dilation_w; } p2 += gap; } } #if __ARM_NEON if (elempack == 4 && out_elempack == 4) { if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) { conv1x1s1_sgemm_pack4_bf16s_neon(bottom_blob_bordered, top_blob, weight_data_pack4_bf16, bias_data, opt); if (activation) { activation->forward_inplace(top_blob, opt); } } else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) { conv1x1s2_pack4_bf16s_neon(bottom_blob_bordered, top_blob, weight_data_pack4_bf16, bias_data, opt); if (activation) { activation->forward_inplace(top_blob, opt); } } else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) { conv3x3s1_winograd64_pack4_bf16s_neon(bottom_blob_bordered, top_blob, weight_data_pack4_bf16, bias_data, opt); if (activation) { activation->forward_inplace(top_blob, opt); } } else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) { conv3x3s2_pack4_bf16s_neon(bottom_blob_bordered, top_blob, weight_data_pack4_bf16, bias_data, opt); if (activation) { activation->forward_inplace(top_blob, opt); } } else if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) { conv5x5s1_pack4_bf16s_neon(bottom_blob_bordered, top_blob, weight_data_pack4_bf16, bias_data, opt); if (activation) { activation->forward_inplace(top_blob, opt); } } else if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) { conv5x5s2_pack4_bf16s_neon(bottom_blob_bordered, top_blob, weight_data_pack4_bf16, bias_data, opt); if (activation) { activation->forward_inplace(top_blob, opt); } } else { // num_output #pragma omp parallel for num_threads(opt.num_threads) for (int p=0; p(i*stride_h) + j*stride_w * 4; for (int k = 0; k < maxk; k++) { float32x4_t _val = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16( sptr + space_ofs[k] * 4 ), 16)); float32x4_t _w0 = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16( kptr ), 16)); float32x4_t _w1 = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16( kptr + 4 ), 16)); float32x4_t _w2 = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16( kptr + 8 ), 16)); float32x4_t _w3 = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16( kptr + 12 ), 16)); #if __aarch64__ _sum = vmlaq_laneq_f32(_sum, _w0, _val, 0); _sum = vmlaq_laneq_f32(_sum, _w1, _val, 1); _sum = vmlaq_laneq_f32(_sum, _w2, _val, 2); _sum = vmlaq_laneq_f32(_sum, _w3, _val, 3); #else _sum = vmlaq_lane_f32(_sum, _w0, vget_low_f32(_val), 0); _sum = vmlaq_lane_f32(_sum, _w1, vget_low_f32(_val), 1); _sum = vmlaq_lane_f32(_sum, _w2, vget_high_f32(_val), 0); _sum = vmlaq_lane_f32(_sum, _w3, vget_high_f32(_val), 1); #endif kptr += 16; } } _sum = activation_ps(_sum, activation_type, activation_params); vst1_u16(outptr + j * 4, vshrn_n_u32(vreinterpretq_u32_f32(_sum), 16)); } outptr += outw * 4; } } } } if (elempack == 1 && out_elempack == 4) { if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) { conv3x3s1_pack1to4_bf16s_neon(bottom_blob_bordered, top_blob, weight_data_pack1to4_bf16, bias_data, opt); if (activation) { activation->forward_inplace(top_blob, opt); } } else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) { conv3x3s2_pack1to4_bf16s_neon(bottom_blob_bordered, top_blob, weight_data_pack1to4_bf16, bias_data, opt); if (activation) { activation->forward_inplace(top_blob, opt); } } else if (kernel_w == 7 && kernel_h == 7 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) { conv7x7s2_pack1to4_bf16s_neon(bottom_blob_bordered, top_blob, weight_data_pack1to4_bf16, bias_data, opt); if (activation) { activation->forward_inplace(top_blob, opt); } } else { // num_output #pragma omp parallel for num_threads(opt.num_threads) for (int p=0; p(i*stride_h) + j*stride_w; for (int k = 0; k < maxk; k++) { float32x4_t _val = vdupq_n_f32(bfloat16_to_float32( sptr[ space_ofs[k] ] )); float32x4_t _w = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16( kptr ), 16)); _sum = vmlaq_f32(_sum, _val, _w); kptr += 4; } } _sum = activation_ps(_sum, activation_type, activation_params); vst1_u16(outptr + j * 4, vshrn_n_u32(vreinterpretq_u32_f32(_sum), 16)); } outptr += outw * 4; } } } } if (elempack == 4 && out_elempack == 1) { if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) { conv1x1s1_sgemm_pack4to1_bf16s_neon(bottom_blob_bordered, top_blob, weight_data_pack4to1_bf16, bias_data, opt); if (activation) { activation->forward_inplace(top_blob, opt); } } else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) { conv1x1s2_pack4to1_bf16s_neon(bottom_blob_bordered, top_blob, weight_data_pack4to1_bf16, bias_data, opt); if (activation) { activation->forward_inplace(top_blob, opt); } } else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) { // TODO more proper condition conv3x3s1_winograd64_pack4to1_bf16s_neon(bottom_blob_bordered, top_blob, weight_data_pack4to1_bf16, bias_data, opt); // conv3x3s1_pack4to1_bf16s_neon(bottom_blob_bordered, top_blob, weight_data_pack4to1_bf16, bias_data, opt); if (activation) { activation->forward_inplace(top_blob, opt); } } else { // num_output #pragma omp parallel for num_threads(opt.num_threads) for (int p=0; p(i*stride_h) + j*stride_w * 4; for (int k = 0; k < maxk; k++) { float32x4_t _val = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16( sptr + space_ofs[k] * 4 ), 16)); float32x4_t _w = vreinterpretq_f32_u32(vshll_n_u16(vld1_u16( kptr ), 16)); float32x4_t _s4 = vmulq_f32(_val, _w); #if __aarch64__ sum += vaddvq_f32(_s4); // dot #else float32x2_t _ss = vadd_f32(vget_low_f32(_s4), vget_high_f32(_s4)); _ss = vpadd_f32(_ss, _ss); sum += vget_lane_f32(_ss, 0); #endif kptr += 4; } } sum = activation_ss(sum, activation_type, activation_params); outptr[j] = float32_to_bfloat16(sum); } outptr += outw; } } } } #endif // __ARM_NEON if (elempack == 1 && out_elempack == 1) { if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) { conv1x1s1_sgemm_bf16s_neon(bottom_blob_bordered, top_blob, weight_data_bf16, bias_data, opt); if (activation) { activation->forward_inplace(top_blob, opt); } } else { // num_output #pragma omp parallel for num_threads(opt.num_threads) for (int p=0; p(i*stride_h) + j*stride_w; for (int k = 0; k < maxk; k++) { float val = bfloat16_to_float32(sptr[ space_ofs[k] ]); float w = bfloat16_to_float32(kptr[ k ]); sum += val * w; } kptr += maxk; } if (activation_type == 1) { sum = std::max(sum, 0.f); } else if (activation_type == 2) { float slope = activation_params[0]; sum = sum > 0.f ? sum : sum * slope; } else if (activation_type == 3) { float min = activation_params[0]; float max = activation_params[1]; if (sum < min) sum = min; if (sum > max) sum = max; } else if (activation_type == 4) { sum = static_cast(1.f / (1.f + exp(-sum))); } outptr[j] = float32_to_bfloat16(sum); } outptr += outw; } } } } return 0; } int Convolution_arm::create_pipeline_int8_arm(const Option& opt) { const int maxk = kernel_w * kernel_h; const int num_input = weight_data_size / maxk / num_output; use_winograd3x3_int8 = false; use_sgemm1x1_int8 = false; if (opt.use_winograd_convolution && kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) { use_winograd3x3_int8 = true; // conv3x3s1_winograd23_transform_kernel_int8_neon(weight_data, weight_3x3_winograd23_data_int8, num_input, num_output); conv3x3s1_winograd43_transform_kernel_int8_neon(weight_data, weight_3x3_winograd23_data_int8, num_input, num_output); } if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) { conv3x3s2_transform_kernel_int8_neon(weight_data, weight_3x3s2_data_int8, num_input, num_output); } else if (kernel_w == 1 && kernel_h == 1 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1) { use_sgemm1x1_int8 = true; conv1x1s1_sgemm_transform_kernel_int8_neon(weight_data, weight_1x1s1_sgemm_data_int8, num_input, num_output); } else { conv_im2col_sgemm_transform_kernel_int8_neon(weight_data, weight_sgemm_data_int8, num_input, num_output, maxk); } return 0; } int Convolution_arm::forward_int8_arm(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const { if (dilation_w > 1 || dilation_h > 1) { return Convolution::forward(bottom_blob, top_blob, opt); } int w = bottom_blob.w; int h = bottom_blob.h; // int channels = bottom_blob.c; size_t elemsize = bottom_blob.elemsize; // fprintf(stderr, "Convolution_arm input %d x %d ksize=%d %d stride=%d %d\n", w, h, kernel_w, kernel_h, stride_w, stride_h); const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1; Mat bottom_blob_unbordered = bottom_blob; if (elemsize != 1) { Option opt_g = opt; opt_g.blob_allocator = opt.workspace_allocator; quantize_float32_to_int8(bottom_blob, bottom_blob_unbordered, bottom_blob_int8_scale, opt_g); } Mat bottom_blob_bordered; make_padding(bottom_blob_unbordered, bottom_blob_bordered, opt); if (bottom_blob_bordered.empty()) return -100; w = bottom_blob_bordered.w; h = bottom_blob_bordered.h; int outw = (w - kernel_extent_w) / stride_w + 1; int outh = (h - kernel_extent_h) / stride_h + 1; // int8 size_t out_elemsize = use_int8_requantize ? 1u : 4u; top_blob.create(outw, outh, num_output, out_elemsize, opt.blob_allocator); if (top_blob.empty()) return -100; // int8 if (use_int8_requantize == true) { Mat top_blob_tm; top_blob_tm.create(outw, outh, num_output, (size_t)4u, opt.workspace_allocator); if (top_blob_tm.empty()) return -100; if (use_sgemm1x1_int8) { std::vector requantize_scales; for (int p=0; pforward_inplace(top_blob, opt); } return 0; } else if (use_winograd3x3_int8) { // conv3x3s1_winograd23_int8_neon(bottom_blob_bordered, top_blob_tm, weight_3x3_winograd23_data_int8, opt); conv3x3s1_winograd43_int8_neon(bottom_blob_bordered, top_blob_tm, weight_3x3_winograd23_data_int8, opt); } else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2) { conv3x3s2_packed_int8_neon(bottom_blob_bordered, top_blob_tm, weight_3x3s2_data_int8, opt); } else { conv_im2col_sgemm_int8_neon(bottom_blob_bordered, top_blob_tm, weight_sgemm_data_int8, kernel_w, kernel_h, stride_w, stride_h, opt); } // requantize, reverse scale inplace #pragma omp parallel for num_threads(opt.num_threads) for (int p=0; pforward_inplace(top_blob, opt); } return 0; } int Convolution_arm::forwardDilation_arm(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const { int w = bottom_blob.w; int h = bottom_blob.h; size_t elemsize = bottom_blob.elemsize; const int kernel_size = kernel_w; const int stride = stride_w; const int dilation = dilation_w; const int kernel_extent = dilation * (kernel_size - 1) + 1; int outw = (w - kernel_extent) / stride + 1; int outh = (h - kernel_extent) / stride + 1; top_blob.create(outw, outh, num_output, elemsize, opt.blob_allocator); if (top_blob.empty()) return -100; // Make (dilation * dilation) batches Mat inner_bottom_blob; Mat inner_top_blob; for (int x = 0; x < dilation; x ++) { for (int y = 0; y < dilation; y ++) { int inner_w = (w - y + dilation - 1) / dilation; int inner_h = (h - x + dilation - 1) / dilation; int inner_outw = (inner_w - kernel_size) / stride + 1; int inner_outh = (inner_h - kernel_size) / stride + 1; inner_bottom_blob.create(inner_w, inner_h, bottom_blob.c, elemsize, opt.workspace_allocator); if (inner_bottom_blob.empty()) return -100; inner_top_blob.create(inner_outw, inner_outh, num_output, elemsize, opt.workspace_allocator); if (inner_top_blob.empty()) return -100; #pragma omp parallel for num_threads(opt.num_threads) for (int c = 0; c < bottom_blob.c; c ++) { float *outptr = inner_bottom_blob.channel(c); for (int i = 0; i < inner_h; i ++) { const float *ptr = (const float *) bottom_blob.channel(c) + dilation * i * w + x * w + y; for (int j = 0; j < inner_w; j ++) { outptr[j] = ptr[j*dilation]; } outptr += inner_w; } } Option opt_g = opt; opt_g.blob_allocator = inner_top_blob.allocator; convolution_dilation1->forward(inner_bottom_blob, inner_top_blob, opt_g); #pragma omp parallel for num_threads(opt.num_threads) for (int c = 0; c < num_output; c ++) { float *outptr = (float *) top_blob.channel(c) + x * outw + y; for (int i = 0; i < inner_outh; i ++) { const float *ptr = (const float *) inner_top_blob.channel(c) + i * inner_outw; for (int j = 0; j < inner_outw; j ++) { outptr[j*dilation] = ptr[j]; } outptr += dilation * outw; } } } } if (activation) { activation->forward_inplace(top_blob, opt); } return 0; } } // namespace ncnn