| 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 deformableconv2d_pack1to4_sse(const std::vector<Mat>& bottom_blobs, 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 pad_left, int pad_top, int activation_type, const Mat& activation_params, const Option& opt) |
| 16 | { |
| 17 | const Mat& bottom_blob = bottom_blobs[0]; |
| 18 | const Mat& offset = bottom_blobs[1]; |
| 19 | const bool has_mask = (bottom_blobs.size() == 3); |
| 20 | const bool offset_not_pack = offset.elempack == 1; |
| 21 | const bool mask_not_pack = has_mask ? bottom_blobs[2].elempack == 1 : true; |
| 22 | |
| 23 | int w = bottom_blob.w; |
| 24 | int h = bottom_blob.h; |
| 25 | int inch = bottom_blob.c; |
| 26 | |
| 27 | int outw = top_blob.w; |
| 28 | int outh = top_blob.h; |
| 29 | int outch = top_blob.c; |
| 30 | |
| 31 | const float* bias_data_ptr = bias_data; |
| 32 | const int elempack = 1; |
| 33 | const int out_elempack = 4; |
| 34 | const int wstep = out_elempack * elempack; |
| 35 | const float zeros[out_elempack] = {0.f, 0.f, 0.f, 0.f}; |
| 36 | const float* zeros_ptr = zeros; |
| 37 | |
| 38 | #pragma omp parallel for num_threads(opt.num_threads) |
| 39 | for (int h_col = 0; h_col < outh; h_col++) |
| 40 | { |
| 41 | for (int w_col = 0; w_col < outw; w_col++) |
| 42 | { |
| 43 | int h_in = h_col * stride_h - pad_top; |
| 44 | int w_in = w_col * stride_w - pad_left; |
| 45 | for (int oc = 0; oc < outch; oc++) |
| 46 | { |
| 47 | const float* kptr = weight_data_packed.channel(oc); |
| 48 | float* outptr = top_blob.channel(oc); |
| 49 | __m128 _sum = _mm_setzero_ps(); |
| 50 | if (bias_data_ptr) |
| 51 | _sum = _mm_loadu_ps(bias_data_ptr + oc * out_elempack); |
| 52 | for (int i = 0; i < kernel_h; i++) |
| 53 | { |
| 54 | for (int j = 0; j < kernel_w; j++) |
| 55 | { |
| 56 | float offset_h = 0.f; |
| 57 | float offset_w = 0.f; |
| 58 | float mask_ = 1.f; |
| 59 | if (offset_not_pack) |
| 60 | { |
| 61 | offset_h = offset.channel((i * kernel_w + j) * 2).row(h_col)[w_col]; |
| 62 | offset_w = offset.channel((i * kernel_w + j) * 2 + 1).row(h_col)[w_col]; |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | const int y_c = (i * kernel_w + j) * 2; |
| 67 | const int x_c = (i * kernel_w + j) * 2 + 1; |
| 68 | offset_h = offset.channel(y_c / offset.elempack).row(h_col)[w_col * offset.elempack + y_c % offset.elempack]; |
| 69 | offset_w = offset.channel(x_c / offset.elempack).row(h_col)[w_col * offset.elempack + x_c % offset.elempack]; |
| 70 | } |
| 71 | if (has_mask) |
| 72 | { |
| 73 | const Mat& mask = bottom_blobs[2]; |
| 74 | if (mask_not_pack) |
| 75 | { |
| 76 | mask_ = mask.channel(i * kernel_w + j).row(h_col)[w_col]; |
| 77 | } |
| 78 | else |
| 79 | { |
| 80 | const int m_c = i * kernel_w + j; |
| 81 | mask_ = mask.channel(m_c / mask.elempack).row(h_col)[w_col * mask.elempack + m_c % mask.elempack]; |
| 82 | } |
| 83 | } |
| 84 | const float h_im = h_in + i * dilation_h + offset_h; |
| 85 | const float w_im = w_in + j * dilation_w + offset_w; |
| 86 | |
| 87 | // Bilinear |
| 88 | const bool cond = h_im > -1 && w_im > -1 && h_im < h && w_im < w; |
| 89 | float w1 = 0.f; |
| 90 | float w2 = 0.f; |
| 91 | float w3 = 0.f; |
| 92 | float w4 = 0.f; |
| 93 | bool v1_cond = false; |
| 94 | bool v2_cond = false; |
| 95 | bool v3_cond = false; |
| 96 | bool v4_cond = false; |
| 97 | int v1_pos = 0; |
| 98 | int v2_pos = 0; |
| 99 | int v3_pos = 0; |
| 100 | int v4_pos = 0; |
| 101 | if (cond) |
| 102 | { |
| 103 | int h_low = (int)floorf(h_im); |
| 104 | int w_low = (int)floorf(w_im); |
| 105 | int h_high = h_low + 1; |
| 106 | int w_high = w_low + 1; |
| 107 | |
| 108 | float lh = h_im - h_low; |
| 109 | float lw = w_im - w_low; |
| 110 | float hh = 1 - lh; |
| 111 | float hw = 1 - lw; |
| 112 | |
| 113 | v1_cond = (h_low >= 0 && w_low >= 0); |
| 114 | v2_cond = (h_low >= 0 && w_high <= w - 1); |
| 115 | v3_cond = (h_high <= h - 1 && w_low >= 0); |
| 116 | v4_cond = (h_high <= h - 1 && w_high <= w - 1); |
| 117 | if (v1_cond) |
| 118 | v1_pos = h_low * w + w_low; |
| 119 | if (v2_cond) |
| 120 | v2_pos = h_low * w + w_high; |
| 121 | if (v3_cond) |
| 122 | v3_pos = h_high * w + w_low; |
| 123 | if (v4_cond) |
| 124 | v4_pos = h_high * w + w_high; |
| 125 | |
| 126 | w1 = hh * hw; |
| 127 | w2 = hh * lw; |
| 128 | w3 = lh * hw; |
| 129 | w4 = lh * lw; |
| 130 | } |
| 131 | const float w1s[out_elempack] = {w1, w1, w1, w1}; |
| 132 | const float* w1_ptr = w1s; |
| 133 | const float w2s[out_elempack] = {w2, w2, w2, w2}; |
| 134 | const float* w2_ptr = w2s; |
| 135 | const float w3s[out_elempack] = {w3, w3, w3, w3}; |
| 136 | const float* w3_ptr = w3s; |
| 137 | const float w4s[out_elempack] = {w4, w4, w4, w4}; |
| 138 | const float* w4_ptr = w4s; |
| 139 | const float masks[out_elempack] = {mask_, mask_, mask_, mask_}; |
| 140 | const float* mask_ptr = masks; |
| 141 | |
| 142 | for (int ic = 0; ic < inch; ic++) |
| 143 | { |
| 144 | const float* data_im_ptr = bottom_blob.channel(ic); |
| 145 | __m128 _val_channel0 = _mm_loadu_ps(zeros_ptr); |
| 146 | if (cond) |
| 147 | { |
| 148 | __m128 _v1_channel0 = _val_channel0; |
| 149 | __m128 _v2_channel0 = _val_channel0; |
| 150 | __m128 _v3_channel0 = _val_channel0; |
| 151 | __m128 _v4_channel0 = _val_channel0; |
| 152 | if (v1_cond) |
| 153 | { |
| 154 | _v1_channel0 = _mm_load1_ps(data_im_ptr + v1_pos); |
| 155 | } |
| 156 | if (v2_cond) |
| 157 | { |
| 158 | _v2_channel0 = _mm_load1_ps(data_im_ptr + v2_pos); |
| 159 | } |
| 160 | if (v3_cond) |
| 161 | { |
| 162 | _v3_channel0 = _mm_load1_ps(data_im_ptr + v3_pos); |
| 163 | } |
| 164 | if (v4_cond) |
| 165 | { |
| 166 | _v4_channel0 = _mm_load1_ps(data_im_ptr + v4_pos); |
| 167 | } |
| 168 | __m128 _w1 = _mm_loadu_ps(w1_ptr); |
| 169 | __m128 _w2 = _mm_loadu_ps(w2_ptr); |
| 170 | __m128 _w3 = _mm_loadu_ps(w3_ptr); |
| 171 | __m128 _w4 = _mm_loadu_ps(w4_ptr); |
| 172 | _val_channel0 = _mm_comp_fmadd_ps(_v1_channel0, _w1, _val_channel0); |
| 173 | _val_channel0 = _mm_comp_fmadd_ps(_v2_channel0, _w2, _val_channel0); |
| 174 | _val_channel0 = _mm_comp_fmadd_ps(_v3_channel0, _w3, _val_channel0); |
| 175 | _val_channel0 = _mm_comp_fmadd_ps(_v4_channel0, _w4, _val_channel0); |
| 176 | } |
| 177 | if (has_mask) |
| 178 | { |
| 179 | __m128 _mask = _mm_loadu_ps(mask_ptr); |
| 180 | _val_channel0 = _mm_mul_ps(_val_channel0, _mask); |
| 181 | } |
| 182 | __m128 _conv_w0 = _mm_load_ps(kptr); |
| 183 | _sum = _mm_comp_fmadd_ps(_val_channel0, _conv_w0, _sum); |
| 184 | kptr += wstep; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | _sum = activation_sse(_sum, activation_type, activation_params); |
| 189 | _mm_storeu_ps(outptr + (h_col * outw + w_col) * out_elempack, _sum); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |