| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2023 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 "convolution1d_vulkan.h" |
| 16 | |
| 17 | #include "layer_shader_type.h" |
| 18 | #include "layer_type.h" |
| 19 | |
| 20 | namespace ncnn { |
| 21 | |
| 22 | Convolution1D_vulkan::Convolution1D_vulkan() |
| 23 | { |
| 24 | support_vulkan = true; |
| 25 | support_image_storage = true; |
| 26 | |
| 27 | padding = 0; |
| 28 | |
| 29 | pipeline_convolution1d = 0; |
| 30 | } |
| 31 | |
| 32 | int Convolution1D_vulkan::load_param(const ParamDict& pd) |
| 33 | { |
| 34 | int ret = Convolution1D::load_param(pd); |
| 35 | |
| 36 | if (dynamic_weight) |
| 37 | { |
| 38 | support_vulkan = false; |
| 39 | support_image_storage = false; |
| 40 | } |
| 41 | |
| 42 | return ret; |
| 43 | } |
| 44 | |
| 45 | int Convolution1D_vulkan::create_pipeline(const Option& _opt) |
| 46 | { |
| 47 | Option opt = _opt; |
| 48 | |
| 49 | const int maxk = kernel_w; |
| 50 | int num_input = weight_data_size / maxk / num_output; |
| 51 | |
| 52 | int elempack = opt.use_shader_pack8 && num_input % 8 == 0 ? 8 : num_input % 4 == 0 ? 4 : 1; |
| 53 | int out_elempack = opt.use_shader_pack8 && num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1; |
| 54 | |
| 55 | { |
| 56 | padding = ncnn::create_layer_vulkan(ncnn::LayerType::Padding); |
| 57 | padding->vkdev = vkdev; |
| 58 | |
| 59 | ncnn::ParamDict pd; |
| 60 | pd.set(0, 0); |
| 61 | pd.set(1, 0); |
| 62 | pd.set(2, pad_left); |
| 63 | pd.set(3, pad_right); |
| 64 | pd.set(4, 0); |
| 65 | pd.set(5, pad_value); |
| 66 | |
| 67 | padding->load_param(pd); |
| 68 | |
| 69 | padding->create_pipeline(opt); |
| 70 | } |
| 71 | |
| 72 | { |
| 73 | Mat weight_data_r2 = weight_data.reshape(maxk, num_input, num_output); |
| 74 | |
| 75 | weight_data_packed.create(maxk, num_input / elempack, num_output / out_elempack, (size_t)4 * elempack * out_elempack, elempack * out_elempack); |
| 76 | |
| 77 | for (int q = 0; q + (out_elempack - 1) < num_output; q += out_elempack) |
| 78 | { |
| 79 | float* g00 = weight_data_packed.channel(q / out_elempack); |
| 80 | |
| 81 | for (int p = 0; p + (elempack - 1) < num_input; p += elempack) |
| 82 | { |
| 83 | for (int k = 0; k < maxk; k++) |
| 84 | { |
| 85 | for (int i = 0; i < out_elempack; i++) |
| 86 | { |
| 87 | const Mat k0 = weight_data_r2.channel(q + i); |
| 88 | |
| 89 | for (int j = 0; j < elempack; j++) |
| 90 | { |
| 91 | const float* k00 = k0.row(p + j); |
| 92 | g00[0] = k00[k]; |
| 93 | g00++; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | if (bias_term) |
| 102 | { |
| 103 | convert_packing(bias_data, bias_data_packed, out_elempack, opt); |
| 104 | } |
| 105 | |
| 106 | { |
| 107 | std::vector<vk_specialization_type> specializations(7 + 4); |
| 108 | specializations[0].i = kernel_w; |
| 109 | specializations[1].i = dilation_w; |
| 110 | specializations[2].i = stride_w; |
| 111 | specializations[3].i = bias_term; |
| 112 | specializations[4].i = activation_type; |
| 113 | specializations[5].f = activation_params.w >= 1 ? activation_params[0] : 0.f; |
| 114 | specializations[6].f = activation_params.w == 2 ? activation_params[1] : 0.f; |
| 115 | specializations[7 + 0].i = 0; |
| 116 | specializations[7 + 1].i = 0; |
| 117 | specializations[7 + 2].i = 0; |
| 118 | specializations[7 + 3].i = 0; |
| 119 | |
| 120 | int shader_type_index = -1; |
| 121 | if (elempack == 1 && out_elempack == 1) shader_type_index = LayerShaderType::convolution1d; |
| 122 | if (elempack == 4 && out_elempack == 4) shader_type_index = LayerShaderType::convolution1d_pack4; |
| 123 | if (elempack == 1 && out_elempack == 4) shader_type_index = LayerShaderType::convolution1d_pack1to4; |
| 124 | if (elempack == 4 && out_elempack == 1) shader_type_index = LayerShaderType::convolution1d_pack4to1; |
| 125 | if (elempack == 8 && out_elempack == 8) shader_type_index = LayerShaderType::convolution1d_pack8; |
| 126 | if (elempack == 1 && out_elempack == 8) shader_type_index = LayerShaderType::convolution1d_pack1to8; |
| 127 | if (elempack == 8 && out_elempack == 1) shader_type_index = LayerShaderType::convolution1d_pack8to1; |
| 128 | if (elempack == 4 && out_elempack == 8) shader_type_index = LayerShaderType::convolution1d_pack4to8; |
| 129 | if (elempack == 8 && out_elempack == 4) shader_type_index = LayerShaderType::convolution1d_pack8to4; |
| 130 | |
| 131 | pipeline_convolution1d = new Pipeline(vkdev); |
| 132 | pipeline_convolution1d->set_optimal_local_size_xyz(1, 1, 1); |
| 133 | pipeline_convolution1d->create(shader_type_index, opt, specializations); |
| 134 | } |
| 135 | |
| 136 | if (opt.lightmode) |
| 137 | { |
| 138 | weight_data.release(); |
| 139 | bias_data.release(); |
| 140 | } |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | int Convolution1D_vulkan::destroy_pipeline(const Option& opt) |
| 146 | { |
| 147 | if (padding) |
| 148 | { |
| 149 | padding->destroy_pipeline(opt); |
| 150 | delete padding; |
| 151 | padding = 0; |
| 152 | } |
| 153 | |
| 154 | delete pipeline_convolution1d; |
| 155 | pipeline_convolution1d = 0; |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | int Convolution1D_vulkan::upload_model(VkTransfer& cmd, const Option& opt) |
| 161 | { |
| 162 | if (padding) |
| 163 | { |
| 164 | padding->upload_model(cmd, opt); |
| 165 | } |
| 166 | |
| 167 | if (support_image_storage && opt.use_image_storage) |
| 168 | { |
| 169 | cmd.record_upload(weight_data_packed, weight_data_gpu_image, opt); |
| 170 | } |
| 171 | else |
| 172 | { |
| 173 | cmd.record_upload(weight_data_packed, weight_data_gpu, opt); |
| 174 | } |
| 175 | |
| 176 | weight_data_packed.release(); |
| 177 | |
| 178 | if (bias_term) |
| 179 | { |
| 180 | if (support_image_storage && opt.use_image_storage) |
| 181 | { |
| 182 | cmd.record_upload(bias_data_packed, bias_data_gpu_image, opt); |
| 183 | } |
| 184 | else |
| 185 | { |
| 186 | cmd.record_upload(bias_data_packed, bias_data_gpu, opt); |
| 187 | } |
| 188 | |
| 189 | bias_data_packed.release(); |
| 190 | } |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | int Convolution1D_vulkan::forward(const VkMat& bottom_blob, VkMat& top_blob, VkCompute& cmd, const Option& opt) const |
| 196 | { |
| 197 | int w = bottom_blob.w; |
| 198 | size_t elemsize = bottom_blob.elemsize; |
| 199 | int elempack = bottom_blob.elempack; |
| 200 | |
| 201 | const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; |
| 202 | |
| 203 | VkMat bottom_blob_bordered = bottom_blob; |
| 204 | if (pad_left > 0 || pad_right > 0) |
| 205 | { |
| 206 | Option opt_pad = opt; |
| 207 | opt_pad.blob_vkallocator = opt.workspace_vkallocator; |
| 208 | |
| 209 | padding->forward(bottom_blob, bottom_blob_bordered, cmd, opt_pad); |
| 210 | } |
| 211 | else if (pad_left == -233 && pad_right == -233) |
| 212 | { |
| 213 | int wpad = kernel_extent_w + (w - 1) / stride_w * stride_w - w; |
| 214 | if (wpad > 0) |
| 215 | { |
| 216 | Option opt_pad = opt; |
| 217 | opt_pad.blob_vkallocator = opt.workspace_vkallocator; |
| 218 | |
| 219 | VkMat padding_param_blob(6, (size_t)4u, 1, opt.staging_vkallocator); |
| 220 | int* padding_params = padding_param_blob.mapped(); |
| 221 | |
| 222 | padding_params[0] = 0; |
| 223 | padding_params[1] = 0; |
| 224 | padding_params[2] = wpad / 2; |
| 225 | padding_params[3] = wpad - wpad / 2; |
| 226 | padding_params[4] = 0; |
| 227 | padding_params[5] = 0; |
| 228 | std::vector<VkMat> padding_inputs(2); |
| 229 | padding_inputs[0] = bottom_blob; |
| 230 | padding_inputs[1] = padding_param_blob; |
| 231 | |
| 232 | std::vector<VkMat> padding_outputs(1); |
| 233 | padding->forward(padding_inputs, padding_outputs, cmd, opt_pad); |
| 234 | bottom_blob_bordered = padding_outputs[0]; |
| 235 | } |
| 236 | } |
| 237 | else if (pad_left == -234 && pad_right == -234) |
| 238 | { |
| 239 | int wpad = kernel_extent_w + (w - 1) / stride_w * stride_w - w; |
| 240 | if (wpad > 0) |
| 241 | { |
| 242 | Option opt_pad = opt; |
| 243 | opt_pad.blob_vkallocator = opt.workspace_vkallocator; |
| 244 | |
| 245 | VkMat padding_param_blob(6, (size_t)4u, 1, opt.staging_vkallocator); |
| 246 | int* padding_params = padding_param_blob.mapped(); |
| 247 | |
| 248 | padding_params[0] = 0; |
| 249 | padding_params[1] = 0; |
| 250 | padding_params[2] = wpad - wpad / 2; |
| 251 | padding_params[3] = wpad / 2; |
| 252 | padding_params[4] = 0; |
| 253 | padding_params[5] = 0; |
| 254 | |
| 255 | std::vector<VkMat> padding_inputs(2); |
| 256 | padding_inputs[0] = bottom_blob; |
| 257 | padding_inputs[1] = padding_param_blob; |
| 258 | |
| 259 | std::vector<VkMat> padding_outputs(1); |
| 260 | padding->forward(padding_inputs, padding_outputs, cmd, opt_pad); |
| 261 | bottom_blob_bordered = padding_outputs[0]; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | int outw = (bottom_blob_bordered.w - kernel_extent_w) / stride_w + 1; |
| 266 | |
| 267 | int out_elempack = opt.use_shader_pack8 && num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1; |
| 268 | |
| 269 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 270 | |
| 271 | if (opt.use_fp16_packed && !opt.use_fp16_storage) |
| 272 | { |
| 273 | if (out_elempack == 8) out_elemsize = 8 * 2u; |
| 274 | if (out_elempack == 4) out_elemsize = 4 * 2u; |
| 275 | if (out_elempack == 1) out_elemsize = 4u; |
| 276 | } |
| 277 | |
| 278 | top_blob.create(outw, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_vkallocator); |
| 279 | if (top_blob.empty()) |
| 280 | return -100; |
| 281 | |
| 282 | std::vector<VkMat> bindings(4); |
| 283 | bindings[0] = bottom_blob_bordered; |
| 284 | bindings[1] = top_blob; |
| 285 | bindings[2] = weight_data_gpu; |
| 286 | bindings[3] = bias_data_gpu; |
| 287 | |
| 288 | std::vector<vk_constant_type> constants(4); |
| 289 | constants[0].i = bottom_blob_bordered.w; |
| 290 | constants[1].i = bottom_blob_bordered.h; |
| 291 | constants[2].i = top_blob.w; |
| 292 | constants[3].i = top_blob.h; |
| 293 | |
| 294 | VkMat dispatcher; |
| 295 | dispatcher.w = (top_blob.w + 1) / 2; |
| 296 | dispatcher.h = (top_blob.h + 1) / 2; |
| 297 | dispatcher.c = 1; |
| 298 | |
| 299 | cmd.record_pipeline(pipeline_convolution1d, bindings, constants, dispatcher); |
| 300 | |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | int Convolution1D_vulkan::forward(const VkImageMat& bottom_blob, VkImageMat& top_blob, VkCompute& cmd, const Option& opt) const |
| 305 | { |
| 306 | int w = bottom_blob.w; |
| 307 | size_t elemsize = bottom_blob.elemsize; |
| 308 | int elempack = bottom_blob.elempack; |
| 309 | |
| 310 | const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; |
| 311 | |
| 312 | VkImageMat bottom_blob_bordered = bottom_blob; |
| 313 | if (pad_left > 0 || pad_right > 0) |
| 314 | { |
| 315 | Option opt_pad = opt; |
| 316 | opt_pad.blob_vkallocator = opt.workspace_vkallocator; |
| 317 | |
| 318 | padding->forward(bottom_blob, bottom_blob_bordered, cmd, opt_pad); |
| 319 | } |
| 320 | else if (pad_left == -233 && pad_right == -233) |
| 321 | { |
| 322 | int wpad = kernel_extent_w + (w - 1) / stride_w * stride_w - w; |
| 323 | if (wpad > 0) |
| 324 | { |
| 325 | Option opt_pad = opt; |
| 326 | opt_pad.blob_vkallocator = opt.workspace_vkallocator; |
| 327 | |
| 328 | VkImageMat padding_param_blob(6, (size_t)4u, 1, opt.staging_vkallocator); |
| 329 | int* padding_params = padding_param_blob.mapped(); |
| 330 | |
| 331 | padding_params[0] = 0; |
| 332 | padding_params[1] = 0; |
| 333 | padding_params[2] = wpad / 2; |
| 334 | padding_params[3] = wpad - wpad / 2; |
| 335 | padding_params[4] = 0; |
| 336 | padding_params[5] = 0; |
| 337 | std::vector<VkImageMat> padding_inputs(2); |
| 338 | padding_inputs[0] = bottom_blob; |
| 339 | padding_inputs[1] = padding_param_blob; |
| 340 | |
| 341 | std::vector<VkImageMat> padding_outputs(1); |
| 342 | padding->forward(padding_inputs, padding_outputs, cmd, opt_pad); |
| 343 | bottom_blob_bordered = padding_outputs[0]; |
| 344 | } |
| 345 | } |
| 346 | else if (pad_left == -234 && pad_right == -234) |
| 347 | { |
| 348 | int wpad = kernel_extent_w + (w - 1) / stride_w * stride_w - w; |
| 349 | if (wpad > 0) |
| 350 | { |
| 351 | Option opt_pad = opt; |
| 352 | opt_pad.blob_vkallocator = opt.workspace_vkallocator; |
| 353 | |
| 354 | VkImageMat padding_param_blob(6, (size_t)4u, 1, opt.staging_vkallocator); |
| 355 | int* padding_params = padding_param_blob.mapped(); |
| 356 | |
| 357 | padding_params[0] = 0; |
| 358 | padding_params[1] = 0; |
| 359 | padding_params[2] = wpad - wpad / 2; |
| 360 | padding_params[3] = wpad / 2; |
| 361 | padding_params[4] = 0; |
| 362 | padding_params[5] = 0; |
| 363 | |
| 364 | std::vector<VkImageMat> padding_inputs(2); |
| 365 | padding_inputs[0] = bottom_blob; |
| 366 | padding_inputs[1] = padding_param_blob; |
| 367 | |
| 368 | std::vector<VkImageMat> padding_outputs(1); |
| 369 | padding->forward(padding_inputs, padding_outputs, cmd, opt_pad); |
| 370 | bottom_blob_bordered = padding_outputs[0]; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | int outw = (bottom_blob_bordered.w - kernel_extent_w) / stride_w + 1; |
| 375 | |
| 376 | int out_elempack = opt.use_shader_pack8 && num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1; |
| 377 | |
| 378 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 379 | |
| 380 | if (opt.use_fp16_packed && !opt.use_fp16_storage) |
| 381 | { |
| 382 | if (out_elempack == 8) out_elemsize = 8 * 2u; |
| 383 | if (out_elempack == 4) out_elemsize = 4 * 2u; |
| 384 | if (out_elempack == 1) out_elemsize = 4u; |
| 385 | } |
| 386 | |
| 387 | top_blob.create(outw, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_vkallocator); |
| 388 | if (top_blob.empty()) |
| 389 | return -100; |
| 390 | |
| 391 | std::vector<VkImageMat> bindings(4); |
| 392 | bindings[0] = bottom_blob_bordered; |
| 393 | bindings[1] = top_blob; |
| 394 | bindings[2] = weight_data_gpu_image; |
| 395 | bindings[3] = bias_data_gpu_image; |
| 396 | |
| 397 | std::vector<vk_constant_type> constants(4); |
| 398 | constants[0].i = bottom_blob_bordered.w; |
| 399 | constants[1].i = bottom_blob_bordered.h; |
| 400 | constants[2].i = top_blob.w; |
| 401 | constants[3].i = top_blob.h; |
| 402 | |
| 403 | VkImageMat dispatcher; |
| 404 | dispatcher.w = (top_blob.w + 1) / 2; |
| 405 | dispatcher.h = (top_blob.h + 1) / 2; |
| 406 | dispatcher.c = 1; |
| 407 | |
| 408 | cmd.record_pipeline(pipeline_convolution1d, bindings, constants, dispatcher); |
| 409 | |
| 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | } // namespace ncnn |