| 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 deconvolution_pack8to4_avx(const Mat& bottom_blob, Mat& top_blob, const Mat& weight_data_packed, const Mat& bias_data, int kernel_w, int kernel_h, int dilation_w, int dilation_h, int stride_w, int stride_h, int activation_type, const Mat& activation_params, const Option& opt) |
| 16 | { |
| 17 | int outch = top_blob.c; |
| 18 | |
| 19 | const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; |
| 20 | const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1; |
| 21 | |
| 22 | const float* bias_data_ptr = bias_data; |
| 23 | |
| 24 | // num_output |
| 25 | #pragma omp parallel for num_threads(opt.num_threads) |
| 26 | for (int p = 0; p < outch; p++) |
| 27 | { |
| 28 | float* outptr = top_blob.channel(p); |
| 29 | |
| 30 | const int maxk = kernel_w * kernel_h; |
| 31 | |
| 32 | // shadowed variable for less openmp task args |
| 33 | const int w = bottom_blob.w; |
| 34 | const int h = bottom_blob.h; |
| 35 | const int channels = bottom_blob.c; |
| 36 | const int outw = top_blob.w; |
| 37 | const int outh = top_blob.h; |
| 38 | |
| 39 | for (int i = 0; i < outh; i++) |
| 40 | { |
| 41 | for (int j = 0; j < outw; j++) |
| 42 | { |
| 43 | __m128 _sum = _mm_setzero_ps(); |
| 44 | |
| 45 | if (bias_data_ptr) |
| 46 | { |
| 47 | _sum = _mm_loadu_ps(bias_data_ptr + p * 4); |
| 48 | } |
| 49 | |
| 50 | const float* kptr = weight_data_packed.channel(p); |
| 51 | |
| 52 | // channels |
| 53 | for (int q = 0; q < channels; q++) |
| 54 | { |
| 55 | const Mat m = bottom_blob.channel(q); |
| 56 | |
| 57 | for (int y = 0; y < kernel_h; y++) |
| 58 | { |
| 59 | int sys = (i + y * dilation_h - (kernel_extent_h - 1)); |
| 60 | if (sys < 0 || sys % stride_h != 0) |
| 61 | continue; |
| 62 | |
| 63 | int sy = sys / stride_h; |
| 64 | if (sy >= h) |
| 65 | continue; |
| 66 | |
| 67 | for (int x = 0; x < kernel_w; x++) |
| 68 | { |
| 69 | int sxs = (j + x * dilation_w - (kernel_extent_w - 1)); |
| 70 | if (sxs < 0 || sxs % stride_w != 0) |
| 71 | continue; |
| 72 | |
| 73 | int sx = sxs / stride_w; |
| 74 | if (sx >= w) |
| 75 | continue; |
| 76 | |
| 77 | const float* sptr = m.row(sy) + sx * 8; |
| 78 | |
| 79 | int k = (y * kernel_w + x) * 32; |
| 80 | |
| 81 | __m128 _val0 = _mm_broadcast_ss(sptr); |
| 82 | __m128 _val1 = _mm_broadcast_ss(sptr + 1); |
| 83 | __m128 _val2 = _mm_broadcast_ss(sptr + 2); |
| 84 | __m128 _val3 = _mm_broadcast_ss(sptr + 3); |
| 85 | __m128 _val4 = _mm_broadcast_ss(sptr + 4); |
| 86 | __m128 _val5 = _mm_broadcast_ss(sptr + 5); |
| 87 | __m128 _val6 = _mm_broadcast_ss(sptr + 6); |
| 88 | __m128 _val7 = _mm_broadcast_ss(sptr + 7); |
| 89 | __m128 _w0 = _mm_load_ps(kptr + k); |
| 90 | __m128 _w1 = _mm_load_ps(kptr + k + 4); |
| 91 | __m128 _w2 = _mm_load_ps(kptr + k + 8); |
| 92 | __m128 _w3 = _mm_load_ps(kptr + k + 12); |
| 93 | __m128 _w4 = _mm_load_ps(kptr + k + 16); |
| 94 | __m128 _w5 = _mm_load_ps(kptr + k + 20); |
| 95 | __m128 _w6 = _mm_load_ps(kptr + k + 24); |
| 96 | __m128 _w7 = _mm_load_ps(kptr + k + 28); |
| 97 | _sum = _mm_comp_fmadd_ps(_val0, _w0, _sum); |
| 98 | _sum = _mm_comp_fmadd_ps(_val1, _w1, _sum); |
| 99 | _sum = _mm_comp_fmadd_ps(_val2, _w2, _sum); |
| 100 | _sum = _mm_comp_fmadd_ps(_val3, _w3, _sum); |
| 101 | _sum = _mm_comp_fmadd_ps(_val4, _w4, _sum); |
| 102 | _sum = _mm_comp_fmadd_ps(_val5, _w5, _sum); |
| 103 | _sum = _mm_comp_fmadd_ps(_val6, _w6, _sum); |
| 104 | _sum = _mm_comp_fmadd_ps(_val7, _w7, _sum); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | kptr += maxk * 32; |
| 109 | } |
| 110 | |
| 111 | _sum = activation_sse(_sum, activation_type, activation_params); |
| 112 | |
| 113 | _mm_storeu_ps(outptr, _sum); |
| 114 | outptr += 4; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |