| 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 | #include "deconvolution1d.h" |
| 16 | |
| 17 | #include "fused_activation.h" |
| 18 | |
| 19 | namespace ncnn { |
| 20 | |
| 21 | Deconvolution1D::Deconvolution1D() |
| 22 | { |
| 23 | one_blob_only = true; |
| 24 | support_inplace = false; |
| 25 | } |
| 26 | |
| 27 | int Deconvolution1D::load_param(const ParamDict& pd) |
| 28 | { |
| 29 | num_output = pd.get(0, 0); |
| 30 | kernel_w = pd.get(1, 0); |
| 31 | dilation_w = pd.get(2, 1); |
| 32 | stride_w = pd.get(3, 1); |
| 33 | pad_left = pd.get(4, 0); |
| 34 | pad_right = pd.get(15, pad_left); |
| 35 | output_pad_right = pd.get(18, 0); |
| 36 | output_w = pd.get(20, 0); |
| 37 | bias_term = pd.get(5, 0); |
| 38 | weight_data_size = pd.get(6, 0); |
| 39 | activation_type = pd.get(9, 0); |
| 40 | activation_params = pd.get(10, Mat()); |
| 41 | |
| 42 | dynamic_weight = pd.get(28, 0); |
| 43 | |
| 44 | if (dynamic_weight) |
| 45 | { |
| 46 | one_blob_only = false; |
| 47 | } |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | int Deconvolution1D::load_model(const ModelBin& mb) |
| 53 | { |
| 54 | if (dynamic_weight) |
| 55 | return 0; |
| 56 | |
| 57 | weight_data = mb.load(weight_data_size, 0); |
| 58 | if (weight_data.empty()) |
| 59 | return -100; |
| 60 | |
| 61 | if (bias_term) |
| 62 | { |
| 63 | bias_data = mb.load(num_output, 1); |
| 64 | if (bias_data.empty()) |
| 65 | return -100; |
| 66 | } |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | static int deconvolution1d(const Mat& bottom_blob, Mat& top_blob, const Mat& weight_data, const Mat& bias_data, int kernel_w, int stride_w, int dilation_w, int activation_type, const Mat& activation_params, const Option& opt) |
| 72 | { |
| 73 | const int w = bottom_blob.w; |
| 74 | const int h = bottom_blob.h; |
| 75 | |
| 76 | const int outw = top_blob.w; |
| 77 | const int outh = top_blob.h; |
| 78 | |
| 79 | const int bias_term = bias_data.empty() ? 0 : 1; |
| 80 | |
| 81 | #pragma omp parallel for num_threads(opt.num_threads) |
| 82 | for (int p = 0; p < outh; p++) |
| 83 | { |
| 84 | Mat out = top_blob.row_range(p, 1); |
| 85 | |
| 86 | const float bias = bias_term ? bias_data[p] : 0.f; |
| 87 | |
| 88 | out.fill(bias); |
| 89 | |
| 90 | for (int j = 0; j < w; j++) |
| 91 | { |
| 92 | float* outptr = (float*)out + j * stride_w; |
| 93 | |
| 94 | const float* kptr = (const float*)weight_data + kernel_w * h * p; |
| 95 | |
| 96 | for (int q = 0; q < h; q++) |
| 97 | { |
| 98 | const float val = bottom_blob.row(q)[j]; |
| 99 | |
| 100 | for (int k = 0; k < kernel_w; k++) |
| 101 | { |
| 102 | float w = kptr[k]; |
| 103 | outptr[k * dilation_w] += val * w; |
| 104 | } |
| 105 | |
| 106 | kptr += kernel_w; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | { |
| 111 | float* outptr = out; |
| 112 | |
| 113 | for (int i = 0; i < outw; i++) |
| 114 | { |
| 115 | outptr[i] = activation_ss(outptr[i], activation_type, activation_params); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | int Deconvolution1D::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 124 | { |
| 125 | int w = bottom_blob.w; |
| 126 | size_t elemsize = bottom_blob.elemsize; |
| 127 | |
| 128 | const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; |
| 129 | |
| 130 | int outw = (w - 1) * stride_w + kernel_extent_w + output_pad_right; |
| 131 | |
| 132 | Mat top_blob_bordered; |
| 133 | if (pad_left > 0 || pad_right > 0 || output_w > 0) |
| 134 | { |
| 135 | top_blob_bordered.create(outw, num_output, elemsize, opt.workspace_allocator); |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | top_blob_bordered = top_blob; |
| 140 | top_blob_bordered.create(outw, num_output, elemsize, opt.blob_allocator); |
| 141 | } |
| 142 | if (top_blob_bordered.empty()) |
| 143 | return -100; |
| 144 | |
| 145 | int ret = deconvolution1d(bottom_blob, top_blob_bordered, weight_data, bias_data, kernel_w, stride_w, dilation_w, activation_type, activation_params, opt); |
| 146 | if (ret != 0) |
| 147 | return ret; |
| 148 | |
| 149 | cut_padding(top_blob_bordered, top_blob, opt); |
| 150 | if (top_blob.empty()) |
| 151 | return -100; |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | int Deconvolution1D::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 157 | { |
| 158 | const Mat& bottom_blob = bottom_blobs[0]; |
| 159 | const Mat& _weight_data = bottom_blobs[1]; |
| 160 | Mat& top_blob = top_blobs[0]; |
| 161 | |
| 162 | const int _num_input = bottom_blob.h; |
| 163 | const int _kernel_w = _weight_data.w; |
| 164 | const int _num_output = _weight_data.h * 1; |
| 165 | |
| 166 | Mat weight_data_flattened; |
| 167 | flatten(_weight_data, weight_data_flattened, opt); |
| 168 | if (weight_data_flattened.empty()) |
| 169 | return -100; |
| 170 | |
| 171 | // transpose group-inch/group-outch/group-kw to group-outch/group-inch/group-kw |
| 172 | Mat weight_data_transposed; |
| 173 | { |
| 174 | weight_data_transposed.create(_kernel_w * _num_output * _num_input / 1, 4u, opt.workspace_allocator); |
| 175 | if (weight_data_transposed.empty()) |
| 176 | return -100; |
| 177 | |
| 178 | const int outch_g = _num_output / 1; |
| 179 | const int inch_g = _num_input / 1; |
| 180 | const int maxk = _kernel_w; |
| 181 | |
| 182 | for (int g = 0; g < 1; g++) |
| 183 | { |
| 184 | // reorder weight from inch-outch to outch-inch |
| 185 | float* wg2 = (float*)weight_data_transposed + g * outch_g * inch_g * maxk; |
| 186 | const float* wg = (const float*)weight_data_flattened + g * inch_g * outch_g * maxk; |
| 187 | for (int i = 0; i < outch_g; i++) |
| 188 | { |
| 189 | for (int j = 0; j < inch_g; j++) |
| 190 | { |
| 191 | for (int k = 0; k < maxk; k++) |
| 192 | { |
| 193 | wg2[(i * inch_g + j) * maxk + k] = wg[(j * outch_g + i) * maxk + k]; |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | Mat bias_data_flattened; |
| 201 | if (bias_term) |
| 202 | { |
| 203 | const Mat& _bias_data = bottom_blobs[2]; |
| 204 | flatten(_bias_data, bias_data_flattened, opt); |
| 205 | if (bias_data_flattened.empty()) |
| 206 | return -100; |
| 207 | } |
| 208 | |
| 209 | const int w = bottom_blob.w; |
| 210 | |
| 211 | const int kernel_extent_w = dilation_w * (_kernel_w - 1) + 1; |
| 212 | |
| 213 | int outw = (w - 1) * stride_w + kernel_extent_w + output_pad_right; |
| 214 | |
| 215 | Mat top_blob_bordered; |
| 216 | if (pad_left > 0 || pad_right > 0 || output_w > 0) |
| 217 | { |
| 218 | top_blob_bordered.create(outw, _num_output, 4u, opt.workspace_allocator); |
| 219 | } |
| 220 | else |
| 221 | { |
| 222 | top_blob_bordered = top_blob; |
| 223 | top_blob_bordered.create(outw, _num_output, 4u, opt.blob_allocator); |
| 224 | } |
| 225 | if (top_blob_bordered.empty()) |
| 226 | return -100; |
| 227 | |
| 228 | int ret = deconvolution1d(bottom_blob, top_blob_bordered, weight_data_transposed, bias_data_flattened, _kernel_w, stride_w, dilation_w, activation_type, activation_params, opt); |
| 229 | if (ret != 0) |
| 230 | return ret; |
| 231 | |
| 232 | cut_padding(top_blob_bordered, top_blob, opt); |
| 233 | if (top_blob.empty()) |
| 234 | return -100; |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | void Deconvolution1D::cut_padding(const Mat& top_blob_bordered, Mat& top_blob, const Option& opt) const |
| 240 | { |
| 241 | if (pad_left > 0 || pad_right > 0) |
| 242 | { |
| 243 | copy_cut_border(top_blob_bordered, top_blob, 0, 0, pad_left, pad_right, opt); |
| 244 | } |
| 245 | else if (output_w > 0) |
| 246 | { |
| 247 | int wcut = top_blob_bordered.w - output_w; |
| 248 | |
| 249 | if (pad_left == -233 || pad_right == -233) |
| 250 | { |
| 251 | // onnx padding=SAME_UPPER |
| 252 | copy_cut_border(top_blob_bordered, top_blob, 0, 0, wcut / 2, wcut - wcut / 2, opt); |
| 253 | } |
| 254 | else if (pad_left == -234 || pad_right == -234) |
| 255 | { |
| 256 | // onnx padding=SAME_LOWER |
| 257 | copy_cut_border(top_blob_bordered, top_blob, 0, 0, wcut - wcut / 2, wcut / 2, opt); |
| 258 | } |
| 259 | } |
| 260 | else |
| 261 | { |
| 262 | top_blob = top_blob_bordered; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | } // namespace ncnn |