| 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_pack4to1_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 = 4; |
| 33 | const int out_elempack = 1; |
| 34 | const int wstep = out_elempack * elempack; |
| 35 | |
| 36 | #pragma omp parallel for num_threads(opt.num_threads) |
| 37 | for (int h_col = 0; h_col < outh; h_col++) |
| 38 | { |
| 39 | for (int w_col = 0; w_col < outw; w_col++) |
| 40 | { |
| 41 | int h_in = h_col * stride_h - pad_top; |
| 42 | int w_in = w_col * stride_w - pad_left; |
| 43 | for (int oc = 0; oc < outch; oc++) |
| 44 | { |
| 45 | const float* kptr = weight_data_packed.channel(oc); |
| 46 | float* outptr = top_blob.channel(oc); |
| 47 | float _sum = 0.f; |
| 48 | if (bias_data_ptr) |
| 49 | _sum = *(bias_data_ptr + oc); |
| 50 | for (int i = 0; i < kernel_h; i++) |
| 51 | { |
| 52 | for (int j = 0; j < kernel_w; j++) |
| 53 | { |
| 54 | float offset_h = 0.f; |
| 55 | float offset_w = 0.f; |
| 56 | float mask_ = 1.f; |
| 57 | if (offset_not_pack) |
| 58 | { |
| 59 | offset_h = offset.channel((i * kernel_w + j) * 2).row(h_col)[w_col]; |
| 60 | offset_w = offset.channel((i * kernel_w + j) * 2 + 1).row(h_col)[w_col]; |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | const int y_c = (i * kernel_w + j) * 2; |
| 65 | const int x_c = (i * kernel_w + j) * 2 + 1; |
| 66 | offset_h = offset.channel(y_c / offset.elempack).row(h_col)[w_col * offset.elempack + y_c % offset.elempack]; |
| 67 | offset_w = offset.channel(x_c / offset.elempack).row(h_col)[w_col * offset.elempack + x_c % offset.elempack]; |
| 68 | } |
| 69 | if (has_mask) |
| 70 | { |
| 71 | const Mat& mask = bottom_blobs[2]; |
| 72 | if (mask_not_pack) |
| 73 | { |
| 74 | mask_ = mask.channel(i * kernel_w + j).row(h_col)[w_col]; |
| 75 | } |
| 76 | else |
| 77 | { |
| 78 | const int m_c = i * kernel_w + j; |
| 79 | mask_ = mask.channel(m_c / mask.elempack).row(h_col)[w_col * mask.elempack + m_c % mask.elempack]; |
| 80 | } |
| 81 | } |
| 82 | const float h_im = h_in + i * dilation_h + offset_h; |
| 83 | const float w_im = w_in + j * dilation_w + offset_w; |
| 84 | |
| 85 | // Bilinear |
| 86 | const bool cond = h_im > -1 && w_im > -1 && h_im < h && w_im < w; |
| 87 | float w1 = 0.f; |
| 88 | float w2 = 0.f; |
| 89 | float w3 = 0.f; |
| 90 | float w4 = 0.f; |
| 91 | bool v1_cond = false; |
| 92 | bool v2_cond = false; |
| 93 | bool v3_cond = false; |
| 94 | bool v4_cond = false; |
| 95 | int v1_pos = 0; |
| 96 | int v2_pos = 0; |
| 97 | int v3_pos = 0; |
| 98 | int v4_pos = 0; |
| 99 | if (cond) |
| 100 | { |
| 101 | int h_low = (int)floorf(h_im); |
| 102 | int w_low = (int)floorf(w_im); |
| 103 | int h_high = h_low + 1; |
| 104 | int w_high = w_low + 1; |
| 105 | |
| 106 | float lh = h_im - h_low; |
| 107 | float lw = w_im - w_low; |
| 108 | float hh = 1 - lh; |
| 109 | float hw = 1 - lw; |
| 110 | |
| 111 | v1_cond = (h_low >= 0 && w_low >= 0); |
| 112 | v2_cond = (h_low >= 0 && w_high <= w - 1); |
| 113 | v3_cond = (h_high <= h - 1 && w_low >= 0); |
| 114 | v4_cond = (h_high <= h - 1 && w_high <= w - 1); |
| 115 | if (v1_cond) |
| 116 | v1_pos = h_low * w + w_low; |
| 117 | if (v2_cond) |
| 118 | v2_pos = h_low * w + w_high; |
| 119 | if (v3_cond) |
| 120 | v3_pos = h_high * w + w_low; |
| 121 | if (v4_cond) |
| 122 | v4_pos = h_high * w + w_high; |
| 123 | |
| 124 | w1 = hh * hw; |
| 125 | w2 = hh * lw; |
| 126 | w3 = lh * hw; |
| 127 | w4 = lh * lw; |
| 128 | } |
| 129 | |
| 130 | for (int ic = 0; ic < inch; ic++) |
| 131 | { |
| 132 | const float* data_im_ptr = bottom_blob.channel(ic); |
| 133 | float _val_channel0 = 0.f; |
| 134 | float _val_channel1 = _val_channel0; |
| 135 | float _val_channel2 = _val_channel0; |
| 136 | float _val_channel3 = _val_channel0; |
| 137 | if (cond) |
| 138 | { |
| 139 | float _v1_channel0 = _val_channel0; |
| 140 | float _v1_channel1 = _val_channel0; |
| 141 | float _v1_channel2 = _val_channel0; |
| 142 | float _v1_channel3 = _val_channel0; |
| 143 | float _v2_channel0 = _val_channel0; |
| 144 | float _v2_channel1 = _val_channel0; |
| 145 | float _v2_channel2 = _val_channel0; |
| 146 | float _v2_channel3 = _val_channel0; |
| 147 | float _v3_channel0 = _val_channel0; |
| 148 | float _v3_channel1 = _val_channel0; |
| 149 | float _v3_channel2 = _val_channel0; |
| 150 | float _v3_channel3 = _val_channel0; |
| 151 | float _v4_channel0 = _val_channel0; |
| 152 | float _v4_channel1 = _val_channel0; |
| 153 | float _v4_channel2 = _val_channel0; |
| 154 | float _v4_channel3 = _val_channel0; |
| 155 | if (v1_cond) |
| 156 | { |
| 157 | _v1_channel0 = *(data_im_ptr + v1_pos * elempack); |
| 158 | _v1_channel1 = *(data_im_ptr + v1_pos * elempack + 1); |
| 159 | _v1_channel2 = *(data_im_ptr + v1_pos * elempack + 2); |
| 160 | _v1_channel3 = *(data_im_ptr + v1_pos * elempack + 3); |
| 161 | } |
| 162 | if (v2_cond) |
| 163 | { |
| 164 | _v2_channel0 = *(data_im_ptr + v2_pos * elempack); |
| 165 | _v2_channel1 = *(data_im_ptr + v2_pos * elempack + 1); |
| 166 | _v2_channel2 = *(data_im_ptr + v2_pos * elempack + 2); |
| 167 | _v2_channel3 = *(data_im_ptr + v2_pos * elempack + 3); |
| 168 | } |
| 169 | if (v3_cond) |
| 170 | { |
| 171 | _v3_channel0 = *(data_im_ptr + v3_pos * elempack); |
| 172 | _v3_channel1 = *(data_im_ptr + v3_pos * elempack + 1); |
| 173 | _v3_channel2 = *(data_im_ptr + v3_pos * elempack + 2); |
| 174 | _v3_channel3 = *(data_im_ptr + v3_pos * elempack + 3); |
| 175 | } |
| 176 | if (v4_cond) |
| 177 | { |
| 178 | _v4_channel0 = *(data_im_ptr + v4_pos * elempack); |
| 179 | _v4_channel1 = *(data_im_ptr + v4_pos * elempack + 1); |
| 180 | _v4_channel2 = *(data_im_ptr + v4_pos * elempack + 2); |
| 181 | _v4_channel3 = *(data_im_ptr + v4_pos * elempack + 3); |
| 182 | } |
| 183 | _val_channel0 = w1 * _v1_channel0 + w2 * _v2_channel0 + w3 * _v3_channel0 + w4 * _v4_channel0; |
| 184 | _val_channel1 = w1 * _v1_channel1 + w2 * _v2_channel1 + w3 * _v3_channel1 + w4 * _v4_channel1; |
| 185 | _val_channel2 = w1 * _v1_channel2 + w2 * _v2_channel2 + w3 * _v3_channel2 + w4 * _v4_channel2; |
| 186 | _val_channel3 = w1 * _v1_channel3 + w2 * _v2_channel3 + w3 * _v3_channel3 + w4 * _v4_channel3; |
| 187 | } |
| 188 | if (has_mask) |
| 189 | { |
| 190 | _val_channel0 *= mask_; |
| 191 | _val_channel1 *= mask_; |
| 192 | _val_channel2 *= mask_; |
| 193 | _val_channel3 *= mask_; |
| 194 | } |
| 195 | float _conv_w0 = *(kptr); |
| 196 | float _conv_w1 = *(kptr + out_elempack); // 1 * out_elempack |
| 197 | float _conv_w2 = *(kptr + 2); // 2 * out_elempack |
| 198 | float _conv_w3 = *(kptr + 3); // 3 * out_elempack |
| 199 | _sum += (_val_channel0 * _conv_w0 + _val_channel1 * _conv_w1 + _val_channel2 * _conv_w2 + _val_channel3 * _conv_w3); |
| 200 | kptr += wstep; |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | _sum = activation_ss(_sum, activation_type, activation_params); |
| 205 | *(outptr + h_col * outw + w_col) = _sum; |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | } |