| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2019 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 "convolution_vulkan.h" |
| 16 | |
| 17 | #include "layer_shader_type.h" |
| 18 | #include "layer_type.h" |
| 19 | |
| 20 | namespace ncnn { |
| 21 | |
| 22 | Convolution_vulkan::Convolution_vulkan() |
| 23 | { |
| 24 | support_vulkan = true; |
| 25 | support_image_storage = true; |
| 26 | |
| 27 | padding = 0; |
| 28 | |
| 29 | pipeline_convolution = 0; |
| 30 | pipeline_convolution_1x1s1d1 = 0; |
| 31 | |
| 32 | pipeline_convolution_gemm = 0; |
| 33 | |
| 34 | pipeline_convolution_3x3s1d1_winograd23_transform_input = 0; |
| 35 | pipeline_convolution_3x3s1d1_winograd23_gemm = 0; |
| 36 | pipeline_convolution_3x3s1d1_winograd23_transform_output = 0; |
| 37 | |
| 38 | pipeline_convolution_3x3s1d1_winograd43_transform_input = 0; |
| 39 | pipeline_convolution_3x3s1d1_winograd43_gemm = 0; |
| 40 | pipeline_convolution_3x3s1d1_winograd43_transform_output = 0; |
| 41 | |
| 42 | reshape_1x1xw = 0; |
| 43 | reshape_w = 0; |
| 44 | } |
| 45 | |
| 46 | int Convolution_vulkan::load_param(const ParamDict& pd) |
| 47 | { |
| 48 | int ret = Convolution::load_param(pd); |
| 49 | |
| 50 | if (dynamic_weight) |
| 51 | { |
| 52 | support_vulkan = false; |
| 53 | support_image_storage = false; |
| 54 | } |
| 55 | |
| 56 | return ret; |
| 57 | } |
| 58 | |
| 59 | int Convolution_vulkan::create_pipeline(const Option& _opt) |
| 60 | { |
| 61 | Option opt = _opt; |
| 62 | const Mat& shape = bottom_shapes.empty() ? Mat() : bottom_shapes[0]; |
| 63 | const Mat& out_shape = top_shapes.empty() ? Mat() : top_shapes[0]; |
| 64 | |
| 65 | const int maxk = kernel_w * kernel_h; |
| 66 | int num_input = weight_data_size / maxk / num_output; |
| 67 | |
| 68 | // the shape after padding |
| 69 | Mat shape_bordered; |
| 70 | if (shape.dims != 0) |
| 71 | { |
| 72 | if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0) |
| 73 | { |
| 74 | shape_bordered = Mat(shape.w + pad_left + pad_right, shape.h + pad_top + pad_bottom, shape.c, (void*)0); |
| 75 | } |
| 76 | else if ((pad_left == -233 && pad_right == -233 && pad_top == -233 && pad_bottom == -233) |
| 77 | || (pad_left == -234 && pad_right == -234 && pad_top == -234 && pad_bottom == -234)) |
| 78 | { |
| 79 | const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; |
| 80 | const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1; |
| 81 | |
| 82 | int wpad = kernel_extent_w + (shape.w - 1) / stride_w * stride_w - shape.w; |
| 83 | int hpad = kernel_extent_h + (shape.h - 1) / stride_h * stride_h - shape.h; |
| 84 | if (wpad > 0 || hpad > 0) |
| 85 | { |
| 86 | shape_bordered = Mat(shape.w + wpad, shape.h + hpad, shape.c, (void*)0); |
| 87 | } |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | shape_bordered = shape; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | int elempack = opt.use_shader_pack8 && num_input % 8 == 0 ? 8 : num_input % 4 == 0 ? 4 : 1; |
| 96 | int out_elempack = opt.use_shader_pack8 && num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1; |
| 97 | |
| 98 | size_t elemsize; |
| 99 | size_t out_elemsize; |
| 100 | if (opt.use_fp16_storage) |
| 101 | { |
| 102 | elemsize = elempack * 2u; |
| 103 | out_elemsize = out_elempack * 2u; |
| 104 | } |
| 105 | else if (opt.use_fp16_packed) |
| 106 | { |
| 107 | elemsize = elempack == 1 ? 4u : elempack * 2u; |
| 108 | out_elemsize = out_elempack == 1 ? 4u : out_elempack * 2u; |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | elemsize = elempack * 4u; |
| 113 | out_elemsize = out_elempack * 4u; |
| 114 | } |
| 115 | |
| 116 | Mat shape_bordered_packed; |
| 117 | if (shape_bordered.dims == 3) shape_bordered_packed = Mat(shape_bordered.w, shape_bordered.h, num_input / elempack, (void*)0, elemsize, elempack); |
| 118 | |
| 119 | Mat out_shape_packed; |
| 120 | if (out_shape.dims == 3) out_shape_packed = Mat(out_shape.w, out_shape.h, num_output / out_elempack, (void*)0, out_elemsize, out_elempack); |
| 121 | |
| 122 | // fc |
| 123 | if (kernel_w == 1 && kernel_h == 1) |
| 124 | { |
| 125 | { |
| 126 | reshape_1x1xw = ncnn::create_layer_vulkan(ncnn::LayerType::Reshape); |
| 127 | reshape_1x1xw->vkdev = vkdev; |
| 128 | |
| 129 | reshape_1x1xw->bottom_shapes.resize(1); |
| 130 | reshape_1x1xw->bottom_shapes[0] = Mat(num_input, (void*)0); |
| 131 | reshape_1x1xw->top_shapes.resize(1); |
| 132 | reshape_1x1xw->top_shapes[0] = Mat(1, 1, num_input, (void*)0); |
| 133 | |
| 134 | ncnn::ParamDict pd; |
| 135 | pd.set(0, 1); // w |
| 136 | pd.set(1, 1); // h |
| 137 | pd.set(2, num_input); // c |
| 138 | |
| 139 | reshape_1x1xw->load_param(pd); |
| 140 | |
| 141 | reshape_1x1xw->create_pipeline(opt); |
| 142 | } |
| 143 | |
| 144 | { |
| 145 | reshape_w = ncnn::create_layer_vulkan(ncnn::LayerType::Reshape); |
| 146 | reshape_w->vkdev = vkdev; |
| 147 | |
| 148 | reshape_w->bottom_shapes.resize(1); |
| 149 | reshape_w->bottom_shapes[0] = Mat(1, 1, num_output, (void*)0); |
| 150 | reshape_w->top_shapes.resize(1); |
| 151 | reshape_w->top_shapes[0] = Mat(num_output, (void*)0); |
| 152 | |
| 153 | ncnn::ParamDict pd; |
| 154 | pd.set(0, num_output); // w |
| 155 | |
| 156 | reshape_w->load_param(pd); |
| 157 | |
| 158 | reshape_w->create_pipeline(opt); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | bool is_conv1x1s1d1 = kernel_w == 1 && kernel_h == 1 && stride_w == 1 && stride_h == 1 && dilation_w == 1 && dilation_h == 1; |
| 163 | bool is_conv3x3s1d1 = kernel_w == 3 && kernel_h == 3 && stride_w == 1 && stride_h == 1 && dilation_w == 1 && dilation_h == 1; |
| 164 | |
| 165 | { |
| 166 | padding = ncnn::create_layer_vulkan(ncnn::LayerType::Padding); |
| 167 | padding->vkdev = vkdev; |
| 168 | |
| 169 | padding->bottom_shapes.resize(1); |
| 170 | padding->bottom_shapes[0] = shape; |
| 171 | padding->top_shapes.resize(1); |
| 172 | padding->top_shapes[0] = shape_bordered; |
| 173 | |
| 174 | ncnn::ParamDict pd; |
| 175 | pd.set(0, pad_top); |
| 176 | pd.set(1, pad_bottom); |
| 177 | pd.set(2, pad_left); |
| 178 | pd.set(3, pad_right); |
| 179 | pd.set(4, 0); |
| 180 | pd.set(5, pad_value); |
| 181 | |
| 182 | padding->load_param(pd); |
| 183 | |
| 184 | padding->create_pipeline(opt); |
| 185 | } |
| 186 | |
| 187 | if (opt.use_winograd_convolution && (opt.use_winograd23_convolution || opt.use_winograd43_convolution) && is_conv3x3s1d1 && num_input >= 16 && num_output >= 16) |
| 188 | { |
| 189 | bool use_cooperative_matrix_16_8_8 = vkdev->info.support_cooperative_matrix_16_8_8() && opt.use_cooperative_matrix && !opt.use_image_storage && !opt.use_shader_pack8 && opt.use_fp16_storage && num_input % 8 == 0 && num_output % 8 == 0; |
| 190 | bool use_cooperative_matrix_16_16_16 = vkdev->info.support_cooperative_matrix_16_16_16() && opt.use_cooperative_matrix && !opt.use_image_storage && !opt.use_shader_pack8 && opt.use_fp16_storage && num_input % 16 == 0 && num_output % 16 == 0; |
| 191 | |
| 192 | // winograd43 transform kernel |
| 193 | if (opt.use_winograd43_convolution) |
| 194 | { |
| 195 | Mat weight_data_tm; |
| 196 | weight_data_tm.create(6 * 6, num_input, num_output); |
| 197 | |
| 198 | const float sq2 = 1.41421356237f; |
| 199 | const float ktm[6][3] = { |
| 200 | {1.0f, 0.0f, 0.0f}, |
| 201 | {-2.0f / 3, -sq2 / 3, -1.0f / 3}, |
| 202 | {-2.0f / 3, sq2 / 3, -1.0f / 3}, |
| 203 | {1.0f / 6, sq2 / 6, 1.0f / 3}, |
| 204 | {1.0f / 6, -sq2 / 6, 1.0f / 3}, |
| 205 | {0.0f, 0.0f, 1.0f} |
| 206 | }; |
| 207 | |
| 208 | #pragma omp parallel for num_threads(opt.num_threads) |
| 209 | for (int p = 0; p < num_output; p++) |
| 210 | { |
| 211 | for (int q = 0; q < num_input; q++) |
| 212 | { |
| 213 | const float* kernel0 = (const float*)weight_data + p * num_input * 9 + q * 9; |
| 214 | float* kernel_tm0 = weight_data_tm.channel(p).row(q); |
| 215 | |
| 216 | // transform kernel |
| 217 | const float* k0 = kernel0; |
| 218 | const float* k1 = kernel0 + 3; |
| 219 | const float* k2 = kernel0 + 6; |
| 220 | |
| 221 | // h |
| 222 | float tmp[6][3]; |
| 223 | for (int i = 0; i < 6; i++) |
| 224 | { |
| 225 | tmp[i][0] = k0[0] * ktm[i][0] + k0[1] * ktm[i][1] + k0[2] * ktm[i][2]; |
| 226 | tmp[i][1] = k1[0] * ktm[i][0] + k1[1] * ktm[i][1] + k1[2] * ktm[i][2]; |
| 227 | tmp[i][2] = k2[0] * ktm[i][0] + k2[1] * ktm[i][1] + k2[2] * ktm[i][2]; |
| 228 | } |
| 229 | |
| 230 | // U |
| 231 | for (int j = 0; j < 6; j++) |
| 232 | { |
| 233 | float* tmpp = &tmp[j][0]; |
| 234 | |
| 235 | for (int i = 0; i < 6; i++) |
| 236 | { |
| 237 | kernel_tm0[j * 6 + i] = tmpp[0] * ktm[i][0] + tmpp[1] * ktm[i][1] + tmpp[2] * ktm[i][2]; |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | if (use_cooperative_matrix_16_8_8) |
| 244 | { |
| 245 | // src = 36-inch-outch |
| 246 | // dst = 8b-8a-inch/8a-outch/8b-36 |
| 247 | weight_winograd43_data_packed.create(num_input / 8, num_output / 8, 36, (size_t)4 * 8 * 8, 8 * 8); |
| 248 | |
| 249 | for (int k = 0; k < 36; k++) |
| 250 | { |
| 251 | float* g00 = weight_winograd43_data_packed.channel(k); |
| 252 | |
| 253 | for (int q = 0; q + (8 - 1) < num_output; q += 8) |
| 254 | { |
| 255 | for (int p = 0; p + (8 - 1) < num_input; p += 8) |
| 256 | { |
| 257 | for (int i = 0; i < 8; i++) |
| 258 | { |
| 259 | for (int j = 0; j < 8; j++) |
| 260 | { |
| 261 | const float* k00 = weight_data_tm.channel(q + j).row(p + i); |
| 262 | g00[0] = k00[k]; |
| 263 | g00++; |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | else if (use_cooperative_matrix_16_16_16) |
| 271 | { |
| 272 | // src = 36-inch-outch |
| 273 | // dst = 16b-16a-inch/16a-outch/16b-36 |
| 274 | weight_winograd43_data_packed.create(num_input / 16, num_output / 16, 36, (size_t)4 * 16 * 16, 16 * 16); |
| 275 | |
| 276 | for (int k = 0; k < 36; k++) |
| 277 | { |
| 278 | float* g00 = weight_winograd43_data_packed.channel(k); |
| 279 | |
| 280 | for (int q = 0; q + (16 - 1) < num_output; q += 16) |
| 281 | { |
| 282 | for (int p = 0; p + (16 - 1) < num_input; p += 16) |
| 283 | { |
| 284 | for (int i = 0; i < 16; i++) |
| 285 | { |
| 286 | for (int j = 0; j < 16; j++) |
| 287 | { |
| 288 | const float* k00 = weight_data_tm.channel(q + j).row(p + i); |
| 289 | g00[0] = k00[k]; |
| 290 | g00++; |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | else |
| 298 | { |
| 299 | // src = 36-inch-outch |
| 300 | // dst = 8a-8b-inch/8a-outch/8b-36 |
| 301 | weight_winograd43_data_packed.create(num_input / elempack, num_output / out_elempack, 36, (size_t)4 * elempack * out_elempack, elempack * out_elempack); |
| 302 | |
| 303 | for (int k = 0; k < 36; k++) |
| 304 | { |
| 305 | float* g00 = weight_winograd43_data_packed.channel(k); |
| 306 | |
| 307 | for (int q = 0; q + (out_elempack - 1) < num_output; q += out_elempack) |
| 308 | { |
| 309 | for (int p = 0; p + (elempack - 1) < num_input; p += elempack) |
| 310 | { |
| 311 | for (int i = 0; i < out_elempack; i++) |
| 312 | { |
| 313 | const Mat k0 = weight_data_tm.channel(q + i); |
| 314 | |
| 315 | for (int j = 0; j < elempack; j++) |
| 316 | { |
| 317 | const float* k00 = k0.row(p + j); |
| 318 | g00[0] = k00[k]; |
| 319 | g00++; |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | // winograd43 |
| 329 | if (opt.use_winograd43_convolution) |
| 330 | { |
| 331 | int block_x = 0; |
| 332 | int block_y = 0; |
| 333 | Mat shape_winograd_input_transformed; |
| 334 | Mat shape_winograd_gemm; |
| 335 | Mat shape_winograd_input_transformed_packed; |
| 336 | Mat shape_winograd_gemm_packed; |
| 337 | |
| 338 | if (out_shape.dims != 0) |
| 339 | { |
| 340 | int block_x = (out_shape.w + 3) / 4; |
| 341 | int block_y = (out_shape.h + 3) / 4; |
| 342 | |
| 343 | shape_winograd_input_transformed = Mat(block_x * block_y, shape.c, 36, (void*)0); |
| 344 | shape_winograd_gemm = Mat(block_x * block_y, out_shape.c, 36, (void*)0); |
| 345 | } |
| 346 | |
| 347 | if (shape_winograd_input_transformed.dims == 3) shape_winograd_input_transformed_packed = Mat(shape_winograd_input_transformed.w, shape_winograd_input_transformed.h / elempack, 36, (void*)0, elemsize, elempack); |
| 348 | |
| 349 | if (shape_winograd_gemm.dims == 3) shape_winograd_gemm_packed = Mat(shape_winograd_gemm.w, shape_winograd_gemm.h / out_elempack, 36, (void*)0, out_elemsize, out_elempack); |
| 350 | |
| 351 | // check blob shape |
| 352 | if (!vkdev->shape_support_image_storage(shape_winograd_input_transformed_packed) || !vkdev->shape_support_image_storage(shape_winograd_gemm_packed)) |
| 353 | { |
| 354 | support_image_storage = false; |
| 355 | opt.use_image_storage = false; |
| 356 | } |
| 357 | |
| 358 | Mat weight_data_packed_tm(num_input / elempack, num_output / out_elempack, 36, (size_t)4 * elempack * out_elempack, elempack * out_elempack); |
| 359 | if (!vkdev->shape_support_image_storage(weight_data_packed_tm)) |
| 360 | { |
| 361 | support_image_storage = false; |
| 362 | opt.use_image_storage = false; |
| 363 | } |
| 364 | |
| 365 | if (vkdev->info.vendor_id() == 0x5143 && vkdev->info.api_version() < VK_MAKE_VERSION(1, 0, 66)) |
| 366 | { |
| 367 | // FIXME workaround qcom adreno image shader produce wrong result on old drivers |
| 368 | support_image_storage = false; |
| 369 | opt.use_image_storage = false; |
| 370 | } |
| 371 | |
| 372 | { |
| 373 | std::vector<vk_specialization_type> specializations(0 + 7); |
| 374 | specializations[0 + 0].i = shape_bordered_packed.w; |
| 375 | specializations[0 + 1].i = shape_bordered_packed.h; |
| 376 | specializations[0 + 2].i = shape_bordered_packed.c; |
| 377 | specializations[0 + 3].i = shape_bordered_packed.cstep; |
| 378 | specializations[0 + 4].i = shape_winograd_input_transformed_packed.cstep; |
| 379 | specializations[0 + 5].i = block_x; |
| 380 | specializations[0 + 6].i = block_y; |
| 381 | |
| 382 | int shader_type_index = -1; |
| 383 | if (elempack == 1) shader_type_index = LayerShaderType::convolution_3x3s1d1_winograd43_transform_input; |
| 384 | if (elempack == 4) shader_type_index = LayerShaderType::convolution_pack4_3x3s1d1_winograd43_transform_input; |
| 385 | if (elempack == 8) shader_type_index = LayerShaderType::convolution_pack8_3x3s1d1_winograd43_transform_input; |
| 386 | |
| 387 | pipeline_convolution_3x3s1d1_winograd43_transform_input = new Pipeline(vkdev); |
| 388 | pipeline_convolution_3x3s1d1_winograd43_transform_input->set_local_size_xyz(4, 4, 1); |
| 389 | pipeline_convolution_3x3s1d1_winograd43_transform_input->create(shader_type_index, opt, specializations); |
| 390 | } |
| 391 | |
| 392 | { |
| 393 | std::vector<vk_specialization_type> specializations(1 + 5); |
| 394 | specializations[0].i = 36; |
| 395 | specializations[1 + 0].i = shape_winograd_input_transformed_packed.h; |
| 396 | specializations[1 + 1].i = shape_winograd_input_transformed_packed.cstep; |
| 397 | specializations[1 + 2].i = shape_winograd_gemm_packed.w; |
| 398 | specializations[1 + 3].i = shape_winograd_gemm_packed.h; |
| 399 | specializations[1 + 4].i = shape_winograd_gemm_packed.cstep; |
| 400 | |
| 401 | int shader_type_index = -1; |
| 402 | if (elempack == 1 && out_elempack == 1) shader_type_index = LayerShaderType::convolution_3x3s1d1_winograd_gemm; |
| 403 | if (elempack == 4 && out_elempack == 4) shader_type_index = LayerShaderType::convolution_pack4_3x3s1d1_winograd_gemm; |
| 404 | if (elempack == 1 && out_elempack == 4) shader_type_index = LayerShaderType::convolution_pack1to4_3x3s1d1_winograd_gemm; |
| 405 | if (elempack == 4 && out_elempack == 1) shader_type_index = LayerShaderType::convolution_pack4to1_3x3s1d1_winograd_gemm; |
| 406 | if (elempack == 8 && out_elempack == 8) shader_type_index = LayerShaderType::convolution_pack8_3x3s1d1_winograd_gemm; |
| 407 | if (elempack == 1 && out_elempack == 8) shader_type_index = LayerShaderType::convolution_pack1to8_3x3s1d1_winograd_gemm; |
| 408 | if (elempack == 8 && out_elempack == 1) shader_type_index = LayerShaderType::convolution_pack8to1_3x3s1d1_winograd_gemm; |
| 409 | if (elempack == 4 && out_elempack == 8) shader_type_index = LayerShaderType::convolution_pack4to8_3x3s1d1_winograd_gemm; |
| 410 | if (elempack == 8 && out_elempack == 4) shader_type_index = LayerShaderType::convolution_pack8to4_3x3s1d1_winograd_gemm; |
| 411 | |
| 412 | if (use_cooperative_matrix_16_8_8) |
| 413 | { |
| 414 | if (vkdev->info.support_VK_KHR_cooperative_matrix()) |
| 415 | shader_type_index = LayerShaderType::convolution_pack4_3x3s1d1_winograd_gemm_khr_cm_16_8_8; |
| 416 | else |
| 417 | shader_type_index = LayerShaderType::convolution_pack4_3x3s1d1_winograd_gemm_nv_cm_16_8_8; |
| 418 | } |
| 419 | else if (use_cooperative_matrix_16_16_16) |
| 420 | { |
| 421 | if (vkdev->info.support_VK_KHR_cooperative_matrix()) |
| 422 | shader_type_index = LayerShaderType::convolution_pack4_3x3s1d1_winograd_gemm_khr_cm_16_16_16; |
| 423 | else |
| 424 | shader_type_index = LayerShaderType::convolution_pack4_3x3s1d1_winograd_gemm_nv_cm_16_16_16; |
| 425 | } |
| 426 | |
| 427 | pipeline_convolution_3x3s1d1_winograd43_gemm = new Pipeline(vkdev); |
| 428 | if (use_cooperative_matrix_16_8_8) |
| 429 | { |
| 430 | pipeline_convolution_3x3s1d1_winograd43_gemm->set_local_size_xyz(32, 1, 1); |
| 431 | } |
| 432 | else if (use_cooperative_matrix_16_16_16) |
| 433 | { |
| 434 | pipeline_convolution_3x3s1d1_winograd43_gemm->set_local_size_xyz(32, 1, 1); |
| 435 | } |
| 436 | else if (opt.use_shader_local_memory) |
| 437 | { |
| 438 | pipeline_convolution_3x3s1d1_winograd43_gemm->set_local_size_xyz(8, 8, 1); |
| 439 | } |
| 440 | else |
| 441 | { |
| 442 | pipeline_convolution_3x3s1d1_winograd43_gemm->set_local_size_xyz(4, std::min(4, num_output / out_elempack), 4); |
| 443 | } |
| 444 | pipeline_convolution_3x3s1d1_winograd43_gemm->create(shader_type_index, opt, specializations); |
| 445 | } |
| 446 | |
| 447 | { |
| 448 | std::vector<vk_specialization_type> specializations(4 + 7); |
| 449 | specializations[0].i = bias_term; |
| 450 | specializations[1].i = activation_type; |
| 451 | specializations[2].f = activation_params.w >= 1 ? activation_params[0] : 0.f; |
| 452 | specializations[3].f = activation_params.w == 2 ? activation_params[1] : 0.f; |
| 453 | specializations[4 + 0].i = shape_winograd_gemm_packed.h; |
| 454 | specializations[4 + 1].i = shape_winograd_gemm_packed.cstep; |
| 455 | specializations[4 + 2].i = block_x; |
| 456 | specializations[4 + 3].i = block_y; |
| 457 | specializations[4 + 4].i = out_shape_packed.w; |
| 458 | specializations[4 + 5].i = out_shape_packed.h; |
| 459 | specializations[4 + 6].i = out_shape_packed.cstep; |
| 460 | |
| 461 | int shader_type_index = -1; |
| 462 | if (out_elempack == 1) shader_type_index = LayerShaderType::convolution_3x3s1d1_winograd43_transform_output; |
| 463 | if (out_elempack == 4) shader_type_index = LayerShaderType::convolution_pack4_3x3s1d1_winograd43_transform_output; |
| 464 | if (out_elempack == 8) shader_type_index = LayerShaderType::convolution_pack8_3x3s1d1_winograd43_transform_output; |
| 465 | |
| 466 | pipeline_convolution_3x3s1d1_winograd43_transform_output = new Pipeline(vkdev); |
| 467 | pipeline_convolution_3x3s1d1_winograd43_transform_output->set_local_size_xyz(4, 4, 1); |
| 468 | pipeline_convolution_3x3s1d1_winograd43_transform_output->create(shader_type_index, opt, specializations); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | // winograd23 transform kernel |
| 473 | if (opt.use_winograd23_convolution) |
| 474 | { |
| 475 | Mat weight_data_tm; |
| 476 | weight_data_tm.create(4 * 4, num_input, num_output); |
| 477 | |
| 478 | // G |
| 479 | const float ktm[4][3] = { |
| 480 | {1.0f, 0.0f, 0.0f}, |
| 481 | {1.0f / 2, 1.0f / 2, 1.0f / 2}, |
| 482 | {1.0f / 2, -1.0f / 2, 1.0f / 2}, |
| 483 | {0.0f, 0.0f, 1.0f} |
| 484 | }; |
| 485 | |
| 486 | #pragma omp parallel for num_threads(opt.num_threads) |
| 487 | for (int p = 0; p < num_output; p++) |
| 488 | { |
| 489 | for (int q = 0; q < num_input; q++) |
| 490 | { |
| 491 | const float* kernel0 = (const float*)weight_data + p * num_input * 9 + q * 9; |
| 492 | float* kernel_tm0 = weight_data_tm.channel(p).row(q); |
| 493 | |
| 494 | // transform kernel |
| 495 | const float* k0 = kernel0; |
| 496 | const float* k1 = kernel0 + 3; |
| 497 | const float* k2 = kernel0 + 6; |
| 498 | |
| 499 | // h |
| 500 | float tmp[4][3]; |
| 501 | for (int i = 0; i < 4; i++) |
| 502 | { |
| 503 | tmp[i][0] = k0[0] * ktm[i][0] + k0[1] * ktm[i][1] + k0[2] * ktm[i][2]; |
| 504 | tmp[i][1] = k1[0] * ktm[i][0] + k1[1] * ktm[i][1] + k1[2] * ktm[i][2]; |
| 505 | tmp[i][2] = k2[0] * ktm[i][0] + k2[1] * ktm[i][1] + k2[2] * ktm[i][2]; |
| 506 | } |
| 507 | |
| 508 | // U |
| 509 | for (int j = 0; j < 4; j++) |
| 510 | { |
| 511 | float* tmpp = &tmp[j][0]; |
| 512 | |
| 513 | for (int i = 0; i < 4; i++) |
| 514 | { |
| 515 | kernel_tm0[j * 4 + i] = tmpp[0] * ktm[i][0] + tmpp[1] * ktm[i][1] + tmpp[2] * ktm[i][2]; |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | if (use_cooperative_matrix_16_8_8) |
| 522 | { |
| 523 | // src = 16-inch-outch |
| 524 | // dst = 8b-8a-inch/8a-outch/8b-16 |
| 525 | weight_winograd23_data_packed.create(num_input / 8, num_output / 8, 16, (size_t)4 * 8 * 8, 8 * 8); |
| 526 | |
| 527 | for (int k = 0; k < 16; k++) |
| 528 | { |
| 529 | float* g00 = weight_winograd23_data_packed.channel(k); |
| 530 | |
| 531 | for (int q = 0; q + (8 - 1) < num_output; q += 8) |
| 532 | { |
| 533 | for (int p = 0; p + (8 - 1) < num_input; p += 8) |
| 534 | { |
| 535 | for (int i = 0; i < 8; i++) |
| 536 | { |
| 537 | for (int j = 0; j < 8; j++) |
| 538 | { |
| 539 | const float* k00 = weight_data_tm.channel(q + j).row(p + i); |
| 540 | g00[0] = k00[k]; |
| 541 | g00++; |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | } |
| 548 | else if (use_cooperative_matrix_16_16_16) |
| 549 | { |
| 550 | // src = 16-inch-outch |
| 551 | // dst = 16b-16a-inch/16a-outch/16b-16 |
| 552 | weight_winograd23_data_packed.create(num_input / 16, num_output / 16, 16, (size_t)4 * 16 * 16, 16 * 16); |
| 553 | |
| 554 | for (int k = 0; k < 16; k++) |
| 555 | { |
| 556 | float* g00 = weight_winograd23_data_packed.channel(k); |
| 557 | |
| 558 | for (int q = 0; q + (16 - 1) < num_output; q += 16) |
| 559 | { |
| 560 | for (int p = 0; p + (16 - 1) < num_input; p += 16) |
| 561 | { |
| 562 | for (int i = 0; i < 16; i++) |
| 563 | { |
| 564 | for (int j = 0; j < 16; j++) |
| 565 | { |
| 566 | const float* k00 = weight_data_tm.channel(q + j).row(p + i); |
| 567 | g00[0] = k00[k]; |
| 568 | g00++; |
| 569 | } |
| 570 | } |
| 571 | } |
| 572 | } |
| 573 | } |
| 574 | } |
| 575 | else |
| 576 | { |
| 577 | // src = 16-inch-outch |
| 578 | // dst = 8a-8b-inch/8a-outch/8b-16 |
| 579 | weight_winograd23_data_packed.create(num_input / elempack, num_output / out_elempack, 16, (size_t)4 * elempack * out_elempack, elempack * out_elempack); |
| 580 | |
| 581 | for (int k = 0; k < 16; k++) |
| 582 | { |
| 583 | float* g00 = weight_winograd23_data_packed.channel(k); |
| 584 | |
| 585 | for (int q = 0; q + (out_elempack - 1) < num_output; q += out_elempack) |
| 586 | { |
| 587 | for (int p = 0; p + (elempack - 1) < num_input; p += elempack) |
| 588 | { |
| 589 | for (int i = 0; i < out_elempack; i++) |
| 590 | { |
| 591 | const Mat k0 = weight_data_tm.channel(q + i); |
| 592 | |
| 593 | for (int j = 0; j < elempack; j++) |
| 594 | { |
| 595 | const float* k00 = k0.row(p + j); |
| 596 | g00[0] = k00[k]; |
| 597 | g00++; |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | // winograd23 |
| 607 | if (opt.use_winograd23_convolution) |
| 608 | { |
| 609 | int block_x = 0; |
| 610 | int block_y = 0; |
| 611 | Mat shape_winograd_input_transformed; |
| 612 | Mat shape_winograd_gemm; |
| 613 | Mat shape_winograd_input_transformed_packed; |
| 614 | Mat shape_winograd_gemm_packed; |
| 615 | |
| 616 | if (out_shape.dims != 0) |
| 617 | { |
| 618 | int block_x = (out_shape.w + 1) / 2; |
| 619 | int block_y = (out_shape.h + 1) / 2; |
| 620 | |
| 621 | shape_winograd_input_transformed = Mat(block_x * block_y, shape.c, 16, (void*)0); |
| 622 | shape_winograd_gemm = Mat(block_x * block_y, out_shape.c, 16, (void*)0); |
| 623 | } |
| 624 | |
| 625 | if (shape_winograd_input_transformed.dims == 3) shape_winograd_input_transformed_packed = Mat(shape_winograd_input_transformed.w, shape_winograd_input_transformed.h / elempack, 16, (void*)0, elemsize, elempack); |
| 626 | |
| 627 | if (shape_winograd_gemm.dims == 3) shape_winograd_gemm_packed = Mat(shape_winograd_gemm.w, shape_winograd_gemm.h / out_elempack, 16, (void*)0, out_elemsize, out_elempack); |
| 628 | |
| 629 | // check blob shape |
| 630 | if (!vkdev->shape_support_image_storage(shape_winograd_input_transformed_packed) || !vkdev->shape_support_image_storage(shape_winograd_gemm_packed)) |
| 631 | { |
| 632 | support_image_storage = false; |
| 633 | opt.use_image_storage = false; |
| 634 | } |
| 635 | |
| 636 | Mat weight_data_packed_tm(num_input / elempack, num_output / out_elempack, 16, (size_t)4 * elempack * out_elempack, elempack * out_elempack); |
| 637 | if (!vkdev->shape_support_image_storage(weight_data_packed_tm)) |
| 638 | { |
| 639 | support_image_storage = false; |
| 640 | opt.use_image_storage = false; |
| 641 | } |
| 642 | |
| 643 | if (vkdev->info.vendor_id() == 0x5143 && vkdev->info.api_version() < VK_MAKE_VERSION(1, 0, 66)) |
| 644 | { |
| 645 | // FIXME workaround qcom adreno image shader produce wrong result on old drivers |
| 646 | support_image_storage = false; |
| 647 | opt.use_image_storage = false; |
| 648 | } |
| 649 | |
| 650 | { |
| 651 | std::vector<vk_specialization_type> specializations(0 + 7); |
| 652 | specializations[0 + 0].i = shape_bordered_packed.w; |
| 653 | specializations[0 + 1].i = shape_bordered_packed.h; |
| 654 | specializations[0 + 2].i = shape_bordered_packed.c; |
| 655 | specializations[0 + 3].i = shape_bordered_packed.cstep; |
| 656 | specializations[0 + 4].i = shape_winograd_input_transformed_packed.cstep; |
| 657 | specializations[0 + 5].i = block_x; |
| 658 | specializations[0 + 6].i = block_y; |
| 659 | |
| 660 | int shader_type_index = -1; |
| 661 | if (elempack == 1) shader_type_index = LayerShaderType::convolution_3x3s1d1_winograd23_transform_input; |
| 662 | if (elempack == 4) shader_type_index = LayerShaderType::convolution_pack4_3x3s1d1_winograd23_transform_input; |
| 663 | if (elempack == 8) shader_type_index = LayerShaderType::convolution_pack8_3x3s1d1_winograd23_transform_input; |
| 664 | |
| 665 | pipeline_convolution_3x3s1d1_winograd23_transform_input = new Pipeline(vkdev); |
| 666 | pipeline_convolution_3x3s1d1_winograd23_transform_input->set_local_size_xyz(8, 8, 1); |
| 667 | pipeline_convolution_3x3s1d1_winograd23_transform_input->create(shader_type_index, opt, specializations); |
| 668 | } |
| 669 | |
| 670 | { |
| 671 | std::vector<vk_specialization_type> specializations(1 + 5); |
| 672 | specializations[0].i = 16; |
| 673 | specializations[1 + 0].i = shape_winograd_input_transformed_packed.h; |
| 674 | specializations[1 + 1].i = shape_winograd_input_transformed_packed.cstep; |
| 675 | specializations[1 + 2].i = shape_winograd_gemm_packed.w; |
| 676 | specializations[1 + 3].i = shape_winograd_gemm_packed.h; |
| 677 | specializations[1 + 4].i = shape_winograd_gemm_packed.cstep; |
| 678 | |
| 679 | int shader_type_index = -1; |
| 680 | if (elempack == 1 && out_elempack == 1) shader_type_index = LayerShaderType::convolution_3x3s1d1_winograd_gemm; |
| 681 | if (elempack == 4 && out_elempack == 4) shader_type_index = LayerShaderType::convolution_pack4_3x3s1d1_winograd_gemm; |
| 682 | if (elempack == 1 && out_elempack == 4) shader_type_index = LayerShaderType::convolution_pack1to4_3x3s1d1_winograd_gemm; |
| 683 | if (elempack == 4 && out_elempack == 1) shader_type_index = LayerShaderType::convolution_pack4to1_3x3s1d1_winograd_gemm; |
| 684 | if (elempack == 8 && out_elempack == 8) shader_type_index = LayerShaderType::convolution_pack8_3x3s1d1_winograd_gemm; |
| 685 | if (elempack == 1 && out_elempack == 8) shader_type_index = LayerShaderType::convolution_pack1to8_3x3s1d1_winograd_gemm; |
| 686 | if (elempack == 8 && out_elempack == 1) shader_type_index = LayerShaderType::convolution_pack8to1_3x3s1d1_winograd_gemm; |
| 687 | if (elempack == 4 && out_elempack == 8) shader_type_index = LayerShaderType::convolution_pack4to8_3x3s1d1_winograd_gemm; |
| 688 | if (elempack == 8 && out_elempack == 4) shader_type_index = LayerShaderType::convolution_pack8to4_3x3s1d1_winograd_gemm; |
| 689 | |
| 690 | if (use_cooperative_matrix_16_8_8) |
| 691 | { |
| 692 | if (vkdev->info.support_VK_KHR_cooperative_matrix()) |
| 693 | shader_type_index = LayerShaderType::convolution_pack4_3x3s1d1_winograd_gemm_khr_cm_16_8_8; |
| 694 | else |
| 695 | shader_type_index = LayerShaderType::convolution_pack4_3x3s1d1_winograd_gemm_nv_cm_16_8_8; |
| 696 | } |
| 697 | else if (use_cooperative_matrix_16_16_16) |
| 698 | { |
| 699 | if (vkdev->info.support_VK_KHR_cooperative_matrix()) |
| 700 | shader_type_index = LayerShaderType::convolution_pack4_3x3s1d1_winograd_gemm_khr_cm_16_16_16; |
| 701 | else |
| 702 | shader_type_index = LayerShaderType::convolution_pack4_3x3s1d1_winograd_gemm_nv_cm_16_16_16; |
| 703 | } |
| 704 | |
| 705 | pipeline_convolution_3x3s1d1_winograd23_gemm = new Pipeline(vkdev); |
| 706 | if (use_cooperative_matrix_16_8_8) |
| 707 | { |
| 708 | pipeline_convolution_3x3s1d1_winograd23_gemm->set_local_size_xyz(32, 1, 1); |
| 709 | } |
| 710 | else if (use_cooperative_matrix_16_16_16) |
| 711 | { |
| 712 | pipeline_convolution_3x3s1d1_winograd23_gemm->set_local_size_xyz(32, 1, 1); |
| 713 | } |
| 714 | else if (opt.use_shader_local_memory) |
| 715 | { |
| 716 | pipeline_convolution_3x3s1d1_winograd23_gemm->set_local_size_xyz(8, 8, 1); |
| 717 | } |
| 718 | else |
| 719 | { |
| 720 | pipeline_convolution_3x3s1d1_winograd23_gemm->set_local_size_xyz(4, std::min(4, num_output / out_elempack), 4); |
| 721 | } |
| 722 | pipeline_convolution_3x3s1d1_winograd23_gemm->create(shader_type_index, opt, specializations); |
| 723 | } |
| 724 | |
| 725 | { |
| 726 | std::vector<vk_specialization_type> specializations(4 + 7); |
| 727 | specializations[0].i = bias_term; |
| 728 | specializations[1].i = activation_type; |
| 729 | specializations[2].f = activation_params.w >= 1 ? activation_params[0] : 0.f; |
| 730 | specializations[3].f = activation_params.w == 2 ? activation_params[1] : 0.f; |
| 731 | specializations[4 + 0].i = shape_winograd_gemm_packed.h; |
| 732 | specializations[4 + 1].i = shape_winograd_gemm_packed.cstep; |
| 733 | specializations[4 + 2].i = block_x; |
| 734 | specializations[4 + 3].i = block_y; |
| 735 | specializations[4 + 4].i = out_shape_packed.w; |
| 736 | specializations[4 + 5].i = out_shape_packed.h; |
| 737 | specializations[4 + 6].i = out_shape_packed.cstep; |
| 738 | |
| 739 | int shader_type_index = -1; |
| 740 | if (out_elempack == 1) shader_type_index = LayerShaderType::convolution_3x3s1d1_winograd23_transform_output; |
| 741 | if (out_elempack == 4) shader_type_index = LayerShaderType::convolution_pack4_3x3s1d1_winograd23_transform_output; |
| 742 | if (out_elempack == 8) shader_type_index = LayerShaderType::convolution_pack8_3x3s1d1_winograd23_transform_output; |
| 743 | |
| 744 | pipeline_convolution_3x3s1d1_winograd23_transform_output = new Pipeline(vkdev); |
| 745 | pipeline_convolution_3x3s1d1_winograd23_transform_output->set_local_size_xyz(8, 8, 1); |
| 746 | pipeline_convolution_3x3s1d1_winograd23_transform_output->create(shader_type_index, opt, specializations); |
| 747 | } |
| 748 | } |
| 749 | } |
| 750 | else |
| 751 | { |
| 752 | // src = kw-kh-inch-outch |
| 753 | // dst = pa-pb-kw-kh-inch/pa-outch/pb |
| 754 | if (opt.use_sgemm_convolution && !is_conv1x1s1d1 && num_input >= 16 && num_output >= 16) |
| 755 | { |
| 756 | bool use_cooperative_matrix_16_8_8 = vkdev->info.support_cooperative_matrix_16_8_8() && opt.use_cooperative_matrix && !opt.use_image_storage && !opt.use_shader_pack8 && opt.use_fp16_storage && num_input % 8 == 0 && num_output % 8 == 0; |
| 757 | bool use_cooperative_matrix_16_16_16 = vkdev->info.support_cooperative_matrix_16_16_16() && opt.use_cooperative_matrix && !opt.use_image_storage && !opt.use_shader_pack8 && opt.use_fp16_storage && num_input % 16 == 0 && num_output % 16 == 0; |
| 758 | |
| 759 | if (use_cooperative_matrix_16_8_8) |
| 760 | { |
| 761 | // dst = 8b-8a-maxk-inch/8a-outch/8b |
| 762 | Mat weight_data_r2 = weight_data.reshape(maxk, num_input, num_output); |
| 763 | |
| 764 | weight_data_packed.create(maxk * num_input / 8, num_output / 8, (size_t)4 * 8 * 8, 8 * 8); |
| 765 | |
| 766 | for (int q = 0; q + 7 < num_output; q += 8) |
| 767 | { |
| 768 | float* g00 = weight_data_packed.row(q / 8); |
| 769 | |
| 770 | for (int p = 0; p + 7 < num_input; p += 8) |
| 771 | { |
| 772 | for (int k = 0; k < maxk; k++) |
| 773 | { |
| 774 | for (int i = 0; i < 8; i++) |
| 775 | { |
| 776 | for (int j = 0; j < 8; j++) |
| 777 | { |
| 778 | const float* k00 = weight_data_r2.channel(q + j).row(p + i); |
| 779 | g00[0] = k00[k]; |
| 780 | g00++; |
| 781 | } |
| 782 | } |
| 783 | } |
| 784 | } |
| 785 | } |
| 786 | } |
| 787 | else if (use_cooperative_matrix_16_16_16) |
| 788 | { |
| 789 | // dst = 16b-16a-maxk-inch/16a-outch/16b |
| 790 | Mat weight_data_r2 = weight_data.reshape(maxk, num_input, num_output); |
| 791 | |
| 792 | weight_data_packed.create(maxk * num_input / 16, num_output / 16, (size_t)4 * 16 * 16, 16 * 16); |
| 793 | |
| 794 | for (int q = 0; q + 15 < num_output; q += 16) |
| 795 | { |
| 796 | float* g00 = weight_data_packed.row(q / 16); |
| 797 | |
| 798 | for (int p = 0; p + 15 < num_input; p += 16) |
| 799 | { |
| 800 | for (int k = 0; k < maxk; k++) |
| 801 | { |
| 802 | for (int i = 0; i < 16; i++) |
| 803 | { |
| 804 | for (int j = 0; j < 16; j++) |
| 805 | { |
| 806 | const float* k00 = weight_data_r2.channel(q + j).row(p + i); |
| 807 | g00[0] = k00[k]; |
| 808 | g00++; |
| 809 | } |
| 810 | } |
| 811 | } |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | else |
| 816 | { |
| 817 | Mat weight_data_r2 = weight_data.reshape(maxk, num_input, num_output); |
| 818 | |
| 819 | weight_data_packed.create(maxk * num_input / elempack, num_output / out_elempack, (size_t)4 * elempack * out_elempack, elempack * out_elempack); |
| 820 | |
| 821 | for (int q = 0; q + (out_elempack - 1) < num_output; q += out_elempack) |
| 822 | { |
| 823 | float* g00 = weight_data_packed.row(q / out_elempack); |
| 824 | |
| 825 | for (int p = 0; p + (elempack - 1) < num_input; p += elempack) |
| 826 | { |
| 827 | for (int k = 0; k < maxk; k++) |
| 828 | { |
| 829 | for (int i = 0; i < out_elempack; i++) |
| 830 | { |
| 831 | const Mat k0 = weight_data_r2.channel(q + i); |
| 832 | |
| 833 | for (int j = 0; j < elempack; j++) |
| 834 | { |
| 835 | const float* k00 = k0.row(p + j); |
| 836 | g00[0] = k00[k]; |
| 837 | g00++; |
| 838 | } |
| 839 | } |
| 840 | } |
| 841 | } |
| 842 | } |
| 843 | } |
| 844 | } |
| 845 | else |
| 846 | { |
| 847 | bool use_cooperative_matrix_16_8_8 = vkdev->info.support_cooperative_matrix_16_8_8() && opt.use_cooperative_matrix && is_conv1x1s1d1 && !opt.use_image_storage && !opt.use_shader_pack8 && opt.use_fp16_storage && num_input % 8 == 0 && num_output % 8 == 0; |
| 848 | bool use_cooperative_matrix_16_16_16 = vkdev->info.support_cooperative_matrix_16_16_16() && opt.use_cooperative_matrix && is_conv1x1s1d1 && !opt.use_image_storage && !opt.use_shader_pack8 && opt.use_fp16_storage && num_input % 16 == 0 && num_output % 16 == 0; |
| 849 | |
| 850 | if (use_cooperative_matrix_16_8_8) |
| 851 | { |
| 852 | // dst = 8b-8a-inch/8a-outch/8b |
| 853 | Mat weight_data_r2 = weight_data.reshape(maxk, num_input, num_output); |
| 854 | |
| 855 | weight_data_packed.create(maxk, num_input / 8, num_output / 8, (size_t)4 * 8 * 8, 8 * 8); |
| 856 | |
| 857 | for (int q = 0; q + 7 < num_output; q += 8) |
| 858 | { |
| 859 | float* g00 = weight_data_packed.channel(q / 8); |
| 860 | |
| 861 | for (int p = 0; p + 7 < num_input; p += 8) |
| 862 | { |
| 863 | for (int k = 0; k < maxk; k++) |
| 864 | { |
| 865 | for (int i = 0; i < 8; i++) |
| 866 | { |
| 867 | for (int j = 0; j < 8; j++) |
| 868 | { |
| 869 | const float* k00 = weight_data_r2.channel(q + j).row(p + i); |
| 870 | g00[0] = k00[k]; |
| 871 | g00++; |
| 872 | } |
| 873 | } |
| 874 | } |
| 875 | } |
| 876 | } |
| 877 | } |
| 878 | else if (use_cooperative_matrix_16_16_16) |
| 879 | { |
| 880 | // dst = 16b-16a-inch/16a-outch/16b |
| 881 | Mat weight_data_r2 = weight_data.reshape(maxk, num_input, num_output); |
| 882 | |
| 883 | weight_data_packed.create(maxk, num_input / 16, num_output / 16, (size_t)4 * 16 * 16, 16 * 16); |
| 884 | |
| 885 | for (int q = 0; q + 15 < num_output; q += 16) |
| 886 | { |
| 887 | float* g00 = weight_data_packed.channel(q / 16); |
| 888 | |
| 889 | for (int p = 0; p + 15 < num_input; p += 16) |
| 890 | { |
| 891 | for (int k = 0; k < maxk; k++) |
| 892 | { |
| 893 | for (int i = 0; i < 16; i++) |
| 894 | { |
| 895 | for (int j = 0; j < 16; j++) |
| 896 | { |
| 897 | const float* k00 = weight_data_r2.channel(q + j).row(p + i); |
| 898 | g00[0] = k00[k]; |
| 899 | g00++; |
| 900 | } |
| 901 | } |
| 902 | } |
| 903 | } |
| 904 | } |
| 905 | } |
| 906 | else |
| 907 | { |
| 908 | Mat weight_data_r2 = weight_data.reshape(maxk, num_input, num_output); |
| 909 | |
| 910 | weight_data_packed.create(maxk, num_input / elempack, num_output / out_elempack, (size_t)4 * elempack * out_elempack, elempack * out_elempack); |
| 911 | |
| 912 | for (int q = 0; q + (out_elempack - 1) < num_output; q += out_elempack) |
| 913 | { |
| 914 | float* g00 = weight_data_packed.channel(q / out_elempack); |
| 915 | |
| 916 | for (int p = 0; p + (elempack - 1) < num_input; p += elempack) |
| 917 | { |
| 918 | for (int k = 0; k < maxk; k++) |
| 919 | { |
| 920 | for (int i = 0; i < out_elempack; i++) |
| 921 | { |
| 922 | const Mat k0 = weight_data_r2.channel(q + i); |
| 923 | |
| 924 | for (int j = 0; j < elempack; j++) |
| 925 | { |
| 926 | const float* k00 = k0.row(p + j); |
| 927 | g00[0] = k00[k]; |
| 928 | g00++; |
| 929 | } |
| 930 | } |
| 931 | } |
| 932 | } |
| 933 | } |
| 934 | } |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | if (bias_term) |
| 939 | { |
| 940 | convert_packing(bias_data, bias_data_packed, out_elempack, opt); |
| 941 | } |
| 942 | |
| 943 | if (opt.use_winograd_convolution && (opt.use_winograd23_convolution || opt.use_winograd43_convolution) && is_conv3x3s1d1 && num_input >= 16 && num_output >= 16) |
| 944 | { |
| 945 | // pass |
| 946 | } |
| 947 | else if (opt.use_sgemm_convolution && !is_conv1x1s1d1 && num_input >= 16 && num_output >= 16) |
| 948 | { |
| 949 | bool use_cooperative_matrix_16_8_8 = vkdev->info.support_cooperative_matrix_16_8_8() && opt.use_cooperative_matrix && !opt.use_image_storage && !opt.use_shader_pack8 && opt.use_fp16_storage && num_input % 8 == 0 && num_output % 8 == 0; |
| 950 | bool use_cooperative_matrix_16_16_16 = vkdev->info.support_cooperative_matrix_16_16_16() && opt.use_cooperative_matrix && !opt.use_image_storage && !opt.use_shader_pack8 && opt.use_fp16_storage && num_input % 16 == 0 && num_output % 16 == 0; |
| 951 | |
| 952 | // check blob shape |
| 953 | if (!vkdev->shape_support_image_storage(shape_bordered_packed) || !vkdev->shape_support_image_storage(out_shape_packed)) |
| 954 | { |
| 955 | support_image_storage = false; |
| 956 | opt.use_image_storage = false; |
| 957 | } |
| 958 | |
| 959 | // check weight shape |
| 960 | Mat weight_data_packed_shape(maxk, num_input / elempack, num_output / out_elempack, (void*)0, (size_t)4 * elempack * out_elempack, elempack * out_elempack); |
| 961 | if (!vkdev->shape_support_image_storage(weight_data_packed_shape)) |
| 962 | { |
| 963 | support_image_storage = false; |
| 964 | opt.use_image_storage = false; |
| 965 | } |
| 966 | |
| 967 | std::vector<vk_specialization_type> specializations(10 + 8); |
| 968 | specializations[0].i = kernel_w; |
| 969 | specializations[1].i = kernel_h; |
| 970 | specializations[2].i = dilation_w; |
| 971 | specializations[3].i = dilation_h; |
| 972 | specializations[4].i = stride_w; |
| 973 | specializations[5].i = stride_h; |
| 974 | specializations[6].i = bias_term; |
| 975 | specializations[7].i = activation_type; |
| 976 | specializations[8].f = activation_params.w >= 1 ? activation_params[0] : 0.f; |
| 977 | specializations[9].f = activation_params.w == 2 ? activation_params[1] : 0.f; |
| 978 | specializations[10 + 0].i = shape_bordered_packed.w; |
| 979 | specializations[10 + 1].i = shape_bordered_packed.h; |
| 980 | specializations[10 + 2].i = shape_bordered_packed.c; |
| 981 | specializations[10 + 3].i = shape_bordered_packed.cstep; |
| 982 | specializations[10 + 4].i = out_shape_packed.w; |
| 983 | specializations[10 + 5].i = out_shape_packed.h; |
| 984 | specializations[10 + 6].i = out_shape_packed.c; |
| 985 | specializations[10 + 7].i = out_shape_packed.cstep; |
| 986 | |
| 987 | Mat local_size_xyz(16, std::min(4, num_output / out_elempack), 1, (void*)0); |
| 988 | if (out_shape_packed.dims != 0) |
| 989 | { |
| 990 | local_size_xyz.w = std::min(16, out_shape_packed.w * out_shape_packed.h); |
| 991 | local_size_xyz.h = std::min(4, out_shape_packed.c); |
| 992 | } |
| 993 | |
| 994 | int shader_type_index = -1; |
| 995 | if (elempack == 1 && out_elempack == 1) shader_type_index = LayerShaderType::convolution_gemm; |
| 996 | if (elempack == 4 && out_elempack == 4) shader_type_index = LayerShaderType::convolution_pack4_gemm; |
| 997 | if (elempack == 1 && out_elempack == 4) shader_type_index = LayerShaderType::convolution_pack1to4_gemm; |
| 998 | if (elempack == 4 && out_elempack == 1) shader_type_index = LayerShaderType::convolution_pack4to1_gemm; |
| 999 | if (elempack == 8 && out_elempack == 8) shader_type_index = LayerShaderType::convolution_pack8_gemm; |
| 1000 | if (elempack == 1 && out_elempack == 8) shader_type_index = LayerShaderType::convolution_pack1to8_gemm; |
| 1001 | if (elempack == 8 && out_elempack == 1) shader_type_index = LayerShaderType::convolution_pack8to1_gemm; |
| 1002 | if (elempack == 4 && out_elempack == 8) shader_type_index = LayerShaderType::convolution_pack4to8_gemm; |
| 1003 | if (elempack == 8 && out_elempack == 4) shader_type_index = LayerShaderType::convolution_pack8to4_gemm; |
| 1004 | |
| 1005 | if (use_cooperative_matrix_16_8_8) |
| 1006 | { |
| 1007 | if (vkdev->info.support_VK_KHR_cooperative_matrix()) |
| 1008 | shader_type_index = LayerShaderType::convolution_pack4_gemm_khr_cm_16_8_8; |
| 1009 | else |
| 1010 | shader_type_index = LayerShaderType::convolution_pack4_gemm_nv_cm_16_8_8; |
| 1011 | } |
| 1012 | else if (use_cooperative_matrix_16_16_16) |
| 1013 | { |
| 1014 | if (vkdev->info.support_VK_KHR_cooperative_matrix()) |
| 1015 | shader_type_index = LayerShaderType::convolution_pack4_gemm_khr_cm_16_16_16; |
| 1016 | else |
| 1017 | shader_type_index = LayerShaderType::convolution_pack4_gemm_nv_cm_16_16_16; |
| 1018 | } |
| 1019 | |
| 1020 | pipeline_convolution_gemm = new Pipeline(vkdev); |
| 1021 | if (use_cooperative_matrix_16_8_8) |
| 1022 | { |
| 1023 | pipeline_convolution_gemm->set_local_size_xyz(32, 1, 1); // 16_8_8 |
| 1024 | } |
| 1025 | else if (use_cooperative_matrix_16_16_16) |
| 1026 | { |
| 1027 | pipeline_convolution_gemm->set_local_size_xyz(32, 1, 1); // 16_16_16 |
| 1028 | } |
| 1029 | else if (opt.use_shader_local_memory) |
| 1030 | { |
| 1031 | pipeline_convolution_gemm->set_local_size_xyz(8, 8, 1); |
| 1032 | } |
| 1033 | else |
| 1034 | { |
| 1035 | pipeline_convolution_gemm->set_optimal_local_size_xyz(local_size_xyz); |
| 1036 | } |
| 1037 | pipeline_convolution_gemm->create(shader_type_index, opt, specializations); |
| 1038 | } |
| 1039 | else if (is_conv1x1s1d1) |
| 1040 | { |
| 1041 | bool use_cooperative_matrix_16_8_8 = vkdev->info.support_cooperative_matrix_16_8_8() && opt.use_cooperative_matrix && !opt.use_image_storage && !opt.use_shader_pack8 && opt.use_fp16_storage && num_input % 8 == 0 && num_output % 8 == 0; |
| 1042 | bool use_cooperative_matrix_16_16_16 = vkdev->info.support_cooperative_matrix_16_16_16() && opt.use_cooperative_matrix && !opt.use_image_storage && !opt.use_shader_pack8 && opt.use_fp16_storage && num_input % 16 == 0 && num_output % 16 == 0; |
| 1043 | |
| 1044 | std::vector<vk_specialization_type> specializations(4 + 8); |
| 1045 | specializations[0].i = bias_term; |
| 1046 | specializations[1].i = activation_type; |
| 1047 | specializations[2].f = activation_params.w >= 1 ? activation_params[0] : 0.f; |
| 1048 | specializations[3].f = activation_params.w == 2 ? activation_params[1] : 0.f; |
| 1049 | specializations[4 + 0].i = shape_bordered_packed.w; |
| 1050 | specializations[4 + 1].i = shape_bordered_packed.h; |
| 1051 | specializations[4 + 2].i = shape_bordered_packed.c; |
| 1052 | specializations[4 + 3].i = shape_bordered_packed.cstep; |
| 1053 | specializations[4 + 4].i = out_shape_packed.w; |
| 1054 | specializations[4 + 5].i = out_shape_packed.h; |
| 1055 | specializations[4 + 6].i = out_shape_packed.c; |
| 1056 | specializations[4 + 7].i = out_shape_packed.cstep; |
| 1057 | |
| 1058 | int shader_type_index = -1; |
| 1059 | if (elempack == 1 && out_elempack == 1) shader_type_index = LayerShaderType::convolution_1x1s1d1; |
| 1060 | if (elempack == 4 && out_elempack == 4) shader_type_index = LayerShaderType::convolution_pack4_1x1s1d1; |
| 1061 | if (elempack == 1 && out_elempack == 4) shader_type_index = LayerShaderType::convolution_pack1to4_1x1s1d1; |
| 1062 | if (elempack == 4 && out_elempack == 1) shader_type_index = LayerShaderType::convolution_pack4to1_1x1s1d1; |
| 1063 | if (elempack == 8 && out_elempack == 8) shader_type_index = LayerShaderType::convolution_pack8_1x1s1d1; |
| 1064 | if (elempack == 1 && out_elempack == 8) shader_type_index = LayerShaderType::convolution_pack1to8_1x1s1d1; |
| 1065 | if (elempack == 8 && out_elempack == 1) shader_type_index = LayerShaderType::convolution_pack8to1_1x1s1d1; |
| 1066 | if (elempack == 4 && out_elempack == 8) shader_type_index = LayerShaderType::convolution_pack4to8_1x1s1d1; |
| 1067 | if (elempack == 8 && out_elempack == 4) shader_type_index = LayerShaderType::convolution_pack8to4_1x1s1d1; |
| 1068 | |
| 1069 | if (use_cooperative_matrix_16_8_8) |
| 1070 | { |
| 1071 | if (vkdev->info.support_VK_KHR_cooperative_matrix()) |
| 1072 | shader_type_index = LayerShaderType::convolution_pack4_1x1s1d1_khr_cm_16_8_8; |
| 1073 | else |
| 1074 | shader_type_index = LayerShaderType::convolution_pack4_1x1s1d1_nv_cm_16_8_8; |
| 1075 | } |
| 1076 | else if (use_cooperative_matrix_16_16_16) |
| 1077 | { |
| 1078 | if (vkdev->info.support_VK_KHR_cooperative_matrix()) |
| 1079 | shader_type_index = LayerShaderType::convolution_pack4_1x1s1d1_khr_cm_16_16_16; |
| 1080 | else |
| 1081 | shader_type_index = LayerShaderType::convolution_pack4_1x1s1d1_nv_cm_16_16_16; |
| 1082 | } |
| 1083 | |
| 1084 | pipeline_convolution_1x1s1d1 = new Pipeline(vkdev); |
| 1085 | if (use_cooperative_matrix_16_8_8) |
| 1086 | { |
| 1087 | pipeline_convolution_1x1s1d1->set_local_size_xyz(32, 1, 1); // 16_8_8 |
| 1088 | } |
| 1089 | else if (use_cooperative_matrix_16_16_16) |
| 1090 | { |
| 1091 | pipeline_convolution_1x1s1d1->set_local_size_xyz(32, 1, 1); // 16_16_16 |
| 1092 | } |
| 1093 | else if (opt.use_shader_local_memory) |
| 1094 | { |
| 1095 | pipeline_convolution_1x1s1d1->set_local_size_xyz(8, 8, 1); |
| 1096 | } |
| 1097 | else |
| 1098 | { |
| 1099 | pipeline_convolution_1x1s1d1->set_local_size_xyz(8, std::min(8, num_output / out_elempack), 1); |
| 1100 | } |
| 1101 | pipeline_convolution_1x1s1d1->create(shader_type_index, opt, specializations); |
| 1102 | } |
| 1103 | else |
| 1104 | { |
| 1105 | std::vector<vk_specialization_type> specializations(10 + 10); |
| 1106 | specializations[0].i = kernel_w; |
| 1107 | specializations[1].i = kernel_h; |
| 1108 | specializations[2].i = dilation_w; |
| 1109 | specializations[3].i = dilation_h; |
| 1110 | specializations[4].i = stride_w; |
| 1111 | specializations[5].i = stride_h; |
| 1112 | specializations[6].i = bias_term; |
| 1113 | specializations[7].i = activation_type; |
| 1114 | specializations[8].f = activation_params.w >= 1 ? activation_params[0] : 0.f; |
| 1115 | specializations[9].f = activation_params.w == 2 ? activation_params[1] : 0.f; |
| 1116 | specializations[10 + 0].i = shape_bordered_packed.dims; |
| 1117 | specializations[10 + 1].i = shape_bordered_packed.w; |
| 1118 | specializations[10 + 2].i = shape_bordered_packed.h; |
| 1119 | specializations[10 + 3].i = shape_bordered_packed.c; |
| 1120 | specializations[10 + 4].i = shape_bordered_packed.cstep; |
| 1121 | specializations[10 + 5].i = out_shape_packed.dims; |
| 1122 | specializations[10 + 6].i = out_shape_packed.w; |
| 1123 | specializations[10 + 7].i = out_shape_packed.h; |
| 1124 | specializations[10 + 8].i = out_shape_packed.c; |
| 1125 | specializations[10 + 9].i = out_shape_packed.cstep; |
| 1126 | |
| 1127 | Mat local_size_xyz(8, 8, std::min(4, (num_output / out_elempack + 1) / 2), (void*)0); |
| 1128 | if (out_shape_packed.dims != 0) |
| 1129 | { |
| 1130 | local_size_xyz.w = std::min(8, out_shape_packed.w); |
| 1131 | local_size_xyz.h = std::min(8, out_shape_packed.h); |
| 1132 | local_size_xyz.c = std::min(4, (out_shape_packed.c + 1) / 2); |
| 1133 | } |
| 1134 | |
| 1135 | int shader_type_index = -1; |
| 1136 | if (elempack == 1 && out_elempack == 1) shader_type_index = LayerShaderType::convolution; |
| 1137 | if (elempack == 4 && out_elempack == 4) shader_type_index = LayerShaderType::convolution_pack4; |
| 1138 | if (elempack == 1 && out_elempack == 4) shader_type_index = LayerShaderType::convolution_pack1to4; |
| 1139 | if (elempack == 4 && out_elempack == 1) shader_type_index = LayerShaderType::convolution_pack4to1; |
| 1140 | if (elempack == 8 && out_elempack == 8) shader_type_index = LayerShaderType::convolution_pack8; |
| 1141 | if (elempack == 1 && out_elempack == 8) shader_type_index = LayerShaderType::convolution_pack1to8; |
| 1142 | if (elempack == 8 && out_elempack == 1) shader_type_index = LayerShaderType::convolution_pack8to1; |
| 1143 | if (elempack == 4 && out_elempack == 8) shader_type_index = LayerShaderType::convolution_pack4to8; |
| 1144 | if (elempack == 8 && out_elempack == 4) shader_type_index = LayerShaderType::convolution_pack8to4; |
| 1145 | |
| 1146 | pipeline_convolution = new Pipeline(vkdev); |
| 1147 | pipeline_convolution->set_optimal_local_size_xyz(local_size_xyz); |
| 1148 | pipeline_convolution->create(shader_type_index, opt, specializations); |
| 1149 | } |
| 1150 | |
| 1151 | if (opt.lightmode) |
| 1152 | { |
| 1153 | weight_data.release(); |
| 1154 | bias_data.release(); |
| 1155 | } |
| 1156 | |
| 1157 | return 0; |
| 1158 | } |
| 1159 | |
| 1160 | int Convolution_vulkan::destroy_pipeline(const Option& opt) |
| 1161 | { |
| 1162 | if (padding) |
| 1163 | { |
| 1164 | padding->destroy_pipeline(opt); |
| 1165 | delete padding; |
| 1166 | padding = 0; |
| 1167 | } |
| 1168 | |
| 1169 | delete pipeline_convolution; |
| 1170 | pipeline_convolution = 0; |
| 1171 | |
| 1172 | delete pipeline_convolution_1x1s1d1; |
| 1173 | pipeline_convolution_1x1s1d1 = 0; |
| 1174 | |
| 1175 | delete pipeline_convolution_gemm; |
| 1176 | pipeline_convolution_gemm = 0; |
| 1177 | |
| 1178 | delete pipeline_convolution_3x3s1d1_winograd23_transform_input; |
| 1179 | delete pipeline_convolution_3x3s1d1_winograd23_gemm; |
| 1180 | delete pipeline_convolution_3x3s1d1_winograd23_transform_output; |
| 1181 | pipeline_convolution_3x3s1d1_winograd23_transform_input = 0; |
| 1182 | pipeline_convolution_3x3s1d1_winograd23_gemm = 0; |
| 1183 | pipeline_convolution_3x3s1d1_winograd23_transform_output = 0; |
| 1184 | |
| 1185 | delete pipeline_convolution_3x3s1d1_winograd43_transform_input; |
| 1186 | delete pipeline_convolution_3x3s1d1_winograd43_gemm; |
| 1187 | delete pipeline_convolution_3x3s1d1_winograd43_transform_output; |
| 1188 | pipeline_convolution_3x3s1d1_winograd43_transform_input = 0; |
| 1189 | pipeline_convolution_3x3s1d1_winograd43_gemm = 0; |
| 1190 | pipeline_convolution_3x3s1d1_winograd43_transform_output = 0; |
| 1191 | |
| 1192 | // fc |
| 1193 | if (reshape_1x1xw) |
| 1194 | { |
| 1195 | reshape_1x1xw->destroy_pipeline(opt); |
| 1196 | delete reshape_1x1xw; |
| 1197 | reshape_1x1xw = 0; |
| 1198 | } |
| 1199 | |
| 1200 | if (reshape_w) |
| 1201 | { |
| 1202 | reshape_w->destroy_pipeline(opt); |
| 1203 | delete reshape_w; |
| 1204 | reshape_w = 0; |
| 1205 | } |
| 1206 | |
| 1207 | return 0; |
| 1208 | } |
| 1209 | |
| 1210 | int Convolution_vulkan::upload_model(VkTransfer& cmd, const Option& opt) |
| 1211 | { |
| 1212 | if (padding) |
| 1213 | { |
| 1214 | padding->upload_model(cmd, opt); |
| 1215 | } |
| 1216 | |
| 1217 | const int maxk = kernel_w * kernel_h; |
| 1218 | int num_input = weight_data_size / maxk / num_output; |
| 1219 | |
| 1220 | bool is_conv3x3s1d1 = kernel_w == 3 && kernel_h == 3 && stride_w == 1 && stride_h == 1 && dilation_w == 1 && dilation_h == 1; |
| 1221 | |
| 1222 | if (opt.use_winograd_convolution && (opt.use_winograd23_convolution || opt.use_winograd43_convolution) && is_conv3x3s1d1 && num_input >= 16 && num_output >= 16) |
| 1223 | { |
| 1224 | // winograd43 |
| 1225 | if (opt.use_winograd43_convolution) |
| 1226 | { |
| 1227 | if (support_image_storage && opt.use_image_storage) |
| 1228 | { |
| 1229 | cmd.record_upload(weight_winograd43_data_packed, weight_data_gpu_tm_winograd43_image, opt); |
| 1230 | } |
| 1231 | else |
| 1232 | { |
| 1233 | cmd.record_upload(weight_winograd43_data_packed, weight_data_gpu_tm_winograd43, opt); |
| 1234 | } |
| 1235 | |
| 1236 | weight_winograd43_data_packed.release(); |
| 1237 | } |
| 1238 | |
| 1239 | // winograd23 |
| 1240 | if (opt.use_winograd23_convolution) |
| 1241 | { |
| 1242 | if (support_image_storage && opt.use_image_storage) |
| 1243 | { |
| 1244 | cmd.record_upload(weight_winograd23_data_packed, weight_data_gpu_tm_winograd23_image, opt); |
| 1245 | } |
| 1246 | else |
| 1247 | { |
| 1248 | cmd.record_upload(weight_winograd23_data_packed, weight_data_gpu_tm_winograd23, opt); |
| 1249 | } |
| 1250 | |
| 1251 | weight_winograd23_data_packed.release(); |
| 1252 | } |
| 1253 | } |
| 1254 | else |
| 1255 | { |
| 1256 | if (support_image_storage && opt.use_image_storage) |
| 1257 | { |
| 1258 | cmd.record_upload(weight_data_packed, weight_data_gpu_image, opt); |
| 1259 | } |
| 1260 | else |
| 1261 | { |
| 1262 | cmd.record_upload(weight_data_packed, weight_data_gpu, opt); |
| 1263 | } |
| 1264 | |
| 1265 | weight_data_packed.release(); |
| 1266 | } |
| 1267 | |
| 1268 | if (bias_term) |
| 1269 | { |
| 1270 | if (support_image_storage && opt.use_image_storage) |
| 1271 | { |
| 1272 | cmd.record_upload(bias_data_packed, bias_data_gpu_image, opt); |
| 1273 | } |
| 1274 | else |
| 1275 | { |
| 1276 | cmd.record_upload(bias_data_packed, bias_data_gpu, opt); |
| 1277 | } |
| 1278 | |
| 1279 | bias_data_packed.release(); |
| 1280 | } |
| 1281 | |
| 1282 | return 0; |
| 1283 | } |
| 1284 | |
| 1285 | int Convolution_vulkan::forward(const VkMat& bottom_blob, VkMat& top_blob, VkCompute& cmd, const Option& opt) const |
| 1286 | { |
| 1287 | int w = bottom_blob.w; |
| 1288 | int h = bottom_blob.h; |
| 1289 | int channels = bottom_blob.c; |
| 1290 | size_t elemsize = bottom_blob.elemsize; |
| 1291 | int elempack = bottom_blob.elempack; |
| 1292 | |
| 1293 | // flattened blob, implement as InnerProduct |
| 1294 | if (bottom_blob.dims == 1 && kernel_w == 1 && kernel_h == 1) |
| 1295 | { |
| 1296 | int num_input = weight_data_size / num_output; |
| 1297 | if (bottom_blob.w * bottom_blob.elempack == num_input) |
| 1298 | { |
| 1299 | VkMat bottom_blob_1x1xw; |
| 1300 | { |
| 1301 | Option opt_reshape = opt; |
| 1302 | opt_reshape.blob_vkallocator = opt.workspace_vkallocator; |
| 1303 | reshape_1x1xw->forward(bottom_blob, bottom_blob_1x1xw, cmd, opt_reshape); |
| 1304 | } |
| 1305 | |
| 1306 | if (bottom_blob_1x1xw.empty()) |
| 1307 | return -100; |
| 1308 | |
| 1309 | VkMat top_blob_1x1xw; |
| 1310 | int ret = forward(bottom_blob_1x1xw, top_blob_1x1xw, cmd, opt); |
| 1311 | if (ret != 0) |
| 1312 | return ret; |
| 1313 | |
| 1314 | return reshape_w->forward(top_blob_1x1xw, top_blob, cmd, opt); |
| 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; |
| 1319 | const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1; |
| 1320 | |
| 1321 | VkMat bottom_blob_bordered = bottom_blob; |
| 1322 | if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0) |
| 1323 | { |
| 1324 | Option opt_pad = opt; |
| 1325 | opt_pad.blob_vkallocator = opt.workspace_vkallocator; |
| 1326 | |
| 1327 | padding->forward(bottom_blob, bottom_blob_bordered, cmd, opt_pad); |
| 1328 | } |
| 1329 | else if (pad_left == -233 && pad_right == -233 && pad_top == -233 && pad_bottom == -233) |
| 1330 | { |
| 1331 | int wpad = kernel_extent_w + (w - 1) / stride_w * stride_w - w; |
| 1332 | int hpad = kernel_extent_h + (h - 1) / stride_h * stride_h - h; |
| 1333 | if (wpad > 0 || hpad > 0) |
| 1334 | { |
| 1335 | Option opt_pad = opt; |
| 1336 | opt_pad.blob_vkallocator = opt.workspace_vkallocator; |
| 1337 | |
| 1338 | VkMat padding_param_blob(6, (size_t)4u, 1, opt.staging_vkallocator); |
| 1339 | int* padding_params = padding_param_blob.mapped(); |
| 1340 | |
| 1341 | padding_params[0] = hpad / 2; |
| 1342 | padding_params[1] = hpad - hpad / 2; |
| 1343 | padding_params[2] = wpad / 2; |
| 1344 | padding_params[3] = wpad - wpad / 2; |
| 1345 | padding_params[4] = 0; |
| 1346 | padding_params[5] = 0; |
| 1347 | std::vector<VkMat> padding_inputs(2); |
| 1348 | padding_inputs[0] = bottom_blob; |
| 1349 | padding_inputs[1] = padding_param_blob; |
| 1350 | |
| 1351 | std::vector<VkMat> padding_outputs(1); |
| 1352 | padding->forward(padding_inputs, padding_outputs, cmd, opt_pad); |
| 1353 | bottom_blob_bordered = padding_outputs[0]; |
| 1354 | } |
| 1355 | } |
| 1356 | else if (pad_left == -234 && pad_right == -234 && pad_top == -234 && pad_bottom == -234) |
| 1357 | { |
| 1358 | int wpad = kernel_extent_w + (w - 1) / stride_w * stride_w - w; |
| 1359 | int hpad = kernel_extent_h + (h - 1) / stride_h * stride_h - h; |
| 1360 | if (wpad > 0 || hpad > 0) |
| 1361 | { |
| 1362 | Option opt_pad = opt; |
| 1363 | opt_pad.blob_vkallocator = opt.workspace_vkallocator; |
| 1364 | |
| 1365 | VkMat padding_param_blob(6, (size_t)4u, 1, opt.staging_vkallocator); |
| 1366 | int* padding_params = padding_param_blob.mapped(); |
| 1367 | |
| 1368 | padding_params[0] = hpad - hpad / 2; |
| 1369 | padding_params[1] = hpad / 2; |
| 1370 | padding_params[2] = wpad - wpad / 2; |
| 1371 | padding_params[3] = wpad / 2; |
| 1372 | padding_params[4] = 0; |
| 1373 | padding_params[5] = 0; |
| 1374 | |
| 1375 | std::vector<VkMat> padding_inputs(2); |
| 1376 | padding_inputs[0] = bottom_blob; |
| 1377 | padding_inputs[1] = padding_param_blob; |
| 1378 | |
| 1379 | std::vector<VkMat> padding_outputs(1); |
| 1380 | padding->forward(padding_inputs, padding_outputs, cmd, opt_pad); |
| 1381 | bottom_blob_bordered = padding_outputs[0]; |
| 1382 | } |
| 1383 | } |
| 1384 | |
| 1385 | w = bottom_blob_bordered.w; |
| 1386 | h = bottom_blob_bordered.h; |
| 1387 | |
| 1388 | int outw = (w - kernel_extent_w) / stride_w + 1; |
| 1389 | int outh = (h - kernel_extent_h) / stride_h + 1; |
| 1390 | int out_elempack = opt.use_shader_pack8 && num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1; |
| 1391 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 1392 | |
| 1393 | if (opt.use_fp16_packed && !opt.use_fp16_storage) |
| 1394 | { |
| 1395 | if (out_elempack == 8) out_elemsize = 8 * 2u; |
| 1396 | if (out_elempack == 4) out_elemsize = 4 * 2u; |
| 1397 | if (out_elempack == 1) out_elemsize = 4u; |
| 1398 | } |
| 1399 | |
| 1400 | bool is_conv1x1s1d1 = kernel_w == 1 && kernel_h == 1 && stride_w == 1 && stride_h == 1 && dilation_w == 1 && dilation_h == 1; |
| 1401 | bool is_conv3x3s1d1 = kernel_w == 3 && kernel_h == 3 && stride_w == 1 && stride_h == 1 && dilation_w == 1 && dilation_h == 1; |
| 1402 | |
| 1403 | if (opt.use_winograd_convolution && (opt.use_winograd23_convolution || opt.use_winograd43_convolution) && is_conv3x3s1d1 && channels * elempack >= 16 && num_output >= 16) |
| 1404 | { |
| 1405 | bool use_cooperative_matrix_16_8_8 = vkdev->info.support_cooperative_matrix_16_8_8() && opt.use_cooperative_matrix && !opt.use_image_storage && !opt.use_shader_pack8 && opt.use_fp16_storage && channels * elempack % 8 == 0 && num_output % 8 == 0; |
| 1406 | bool use_cooperative_matrix_16_16_16 = vkdev->info.support_cooperative_matrix_16_16_16() && opt.use_cooperative_matrix && !opt.use_image_storage && !opt.use_shader_pack8 && opt.use_fp16_storage && channels * elempack % 16 == 0 && num_output % 16 == 0; |
| 1407 | |
| 1408 | bool pre_winograd43 = opt.use_winograd43_convolution; |
| 1409 | if (opt.use_winograd23_convolution) |
| 1410 | { |
| 1411 | if (vkdev->info.type() == 0 && ((w <= 18 && h <= 18) || ((w >= 23 && w <= 24) && (h >= 23 && h <= 24)))) |
| 1412 | pre_winograd43 = false; |
| 1413 | if (vkdev->info.type() != 0 && (w <= 12 && h <= 12)) |
| 1414 | pre_winograd43 = false; |
| 1415 | |
| 1416 | if (use_cooperative_matrix_16_8_8 && (w <= 18 && h <= 18)) |
| 1417 | pre_winograd43 = false; |
| 1418 | else if (use_cooperative_matrix_16_16_16 && (w <= 18 && h <= 18)) |
| 1419 | pre_winograd43 = false; |
| 1420 | } |
| 1421 | |
| 1422 | if (pre_winograd43) |
| 1423 | { |
| 1424 | // winograd43 |
| 1425 | int block_x = (outw + 3) / 4; |
| 1426 | int block_y = (outh + 3) / 4; |
| 1427 | |
| 1428 | // transform input |
| 1429 | VkMat bottom_tm_blob; |
| 1430 | { |
| 1431 | bottom_tm_blob.create(block_x * block_y, channels, 36, elemsize, elempack, opt.workspace_vkallocator); |
| 1432 | if (bottom_tm_blob.empty()) |
| 1433 | return -100; |
| 1434 | |
| 1435 | std::vector<VkMat> bindings(2); |
| 1436 | bindings[0] = bottom_blob_bordered; |
| 1437 | bindings[1] = bottom_tm_blob; |
| 1438 | |
| 1439 | std::vector<vk_constant_type> constants(7); |
| 1440 | constants[0].i = bottom_blob_bordered.w; |
| 1441 | constants[1].i = bottom_blob_bordered.h; |
| 1442 | constants[2].i = bottom_blob_bordered.c; |
| 1443 | constants[3].i = bottom_blob_bordered.cstep; |
| 1444 | constants[4].i = bottom_tm_blob.cstep; |
| 1445 | constants[5].i = block_x; |
| 1446 | constants[6].i = block_y; |
| 1447 | |
| 1448 | VkMat dispatcher; |
| 1449 | dispatcher.w = block_x; |
| 1450 | dispatcher.h = block_y; |
| 1451 | dispatcher.c = bottom_tm_blob.h; |
| 1452 | |
| 1453 | cmd.record_pipeline(pipeline_convolution_3x3s1d1_winograd43_transform_input, bindings, constants, dispatcher); |
| 1454 | } |
| 1455 | |
| 1456 | // gemm |
| 1457 | VkMat top_tm_blob; |
| 1458 | { |
| 1459 | top_tm_blob.create(block_x * block_y, num_output / out_elempack, 36, out_elemsize, out_elempack, opt.workspace_vkallocator); |
| 1460 | if (top_tm_blob.empty()) |
| 1461 | return -100; |
| 1462 | |
| 1463 | std::vector<VkMat> bindings(3); |
| 1464 | bindings[0] = bottom_tm_blob; |
| 1465 | bindings[1] = top_tm_blob; |
| 1466 | bindings[2] = weight_data_gpu_tm_winograd43; |
| 1467 | |
| 1468 | std::vector<vk_constant_type> constants(5); |
| 1469 | constants[0].i = bottom_tm_blob.h; |
| 1470 | constants[1].i = bottom_tm_blob.cstep; |
| 1471 | constants[2].i = top_tm_blob.w; |
| 1472 | constants[3].i = top_tm_blob.h; |
| 1473 | constants[4].i = top_tm_blob.cstep; |
| 1474 | |
| 1475 | VkMat dispatcher; |
| 1476 | dispatcher.w = (top_tm_blob.w + 3) / 4; |
| 1477 | dispatcher.h = top_tm_blob.h; |
| 1478 | dispatcher.c = 36; |
| 1479 | |
| 1480 | if (use_cooperative_matrix_16_8_8) |
| 1481 | { |
| 1482 | dispatcher.w = ((top_tm_blob.w + 15) / 16 + 1) / 2 * 32; |
| 1483 | dispatcher.h = ((top_tm_blob.h + 1) / 2 + 3) / 4; |
| 1484 | dispatcher.c = 36; |
| 1485 | } |
| 1486 | else if (use_cooperative_matrix_16_16_16) |
| 1487 | { |
| 1488 | dispatcher.w = ((top_tm_blob.w + 15) / 16 + 1) / 2 * 32; |
| 1489 | dispatcher.h = ((top_tm_blob.h + 3) / 4 + 1) / 2; |
| 1490 | dispatcher.c = 36; |
| 1491 | } |
| 1492 | |
| 1493 | cmd.record_pipeline(pipeline_convolution_3x3s1d1_winograd43_gemm, bindings, constants, dispatcher); |
| 1494 | } |
| 1495 | |
| 1496 | // transform output |
| 1497 | { |
| 1498 | top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_vkallocator); |
| 1499 | if (top_blob.empty()) |
| 1500 | return -100; |
| 1501 | |
| 1502 | std::vector<VkMat> bindings(3); |
| 1503 | bindings[0] = top_tm_blob; |
| 1504 | bindings[1] = top_blob; |
| 1505 | bindings[2] = bias_data_gpu; |
| 1506 | |
| 1507 | std::vector<vk_constant_type> constants(7); |
| 1508 | constants[0].i = top_tm_blob.h; |
| 1509 | constants[1].i = top_tm_blob.cstep; |
| 1510 | constants[2].i = block_x; |
| 1511 | constants[3].i = block_y; |
| 1512 | constants[4].i = top_blob.w; |
| 1513 | constants[5].i = top_blob.h; |
| 1514 | constants[6].i = top_blob.cstep; |
| 1515 | |
| 1516 | VkMat dispatcher; |
| 1517 | dispatcher.w = block_x; |
| 1518 | dispatcher.h = block_y; |
| 1519 | dispatcher.c = top_blob.c; |
| 1520 | |
| 1521 | cmd.record_pipeline(pipeline_convolution_3x3s1d1_winograd43_transform_output, bindings, constants, dispatcher); |
| 1522 | } |
| 1523 | } |
| 1524 | else |
| 1525 | { |
| 1526 | // winograd23 |
| 1527 | int block_x = (outw + 1) / 2; |
| 1528 | int block_y = (outh + 1) / 2; |
| 1529 | |
| 1530 | // transform input |
| 1531 | VkMat bottom_tm_blob; |
| 1532 | { |
| 1533 | bottom_tm_blob.create(block_x * block_y, channels, 16, elemsize, elempack, opt.workspace_vkallocator); |
| 1534 | if (bottom_tm_blob.empty()) |
| 1535 | return -100; |
| 1536 | |
| 1537 | std::vector<VkMat> bindings(2); |
| 1538 | bindings[0] = bottom_blob_bordered; |
| 1539 | bindings[1] = bottom_tm_blob; |
| 1540 | |
| 1541 | std::vector<vk_constant_type> constants(7); |
| 1542 | constants[0].i = bottom_blob_bordered.w; |
| 1543 | constants[1].i = bottom_blob_bordered.h; |
| 1544 | constants[2].i = bottom_blob_bordered.c; |
| 1545 | constants[3].i = bottom_blob_bordered.cstep; |
| 1546 | constants[4].i = bottom_tm_blob.cstep; |
| 1547 | constants[5].i = block_x; |
| 1548 | constants[6].i = block_y; |
| 1549 | |
| 1550 | VkMat dispatcher; |
| 1551 | dispatcher.w = block_x; |
| 1552 | dispatcher.h = block_y; |
| 1553 | dispatcher.c = bottom_tm_blob.h; |
| 1554 | |
| 1555 | cmd.record_pipeline(pipeline_convolution_3x3s1d1_winograd23_transform_input, bindings, constants, dispatcher); |
| 1556 | } |
| 1557 | |
| 1558 | // gemm |
| 1559 | VkMat top_tm_blob; |
| 1560 | { |
| 1561 | top_tm_blob.create(block_x * block_y, num_output / out_elempack, 16, out_elemsize, out_elempack, opt.workspace_vkallocator); |
| 1562 | if (top_tm_blob.empty()) |
| 1563 | return -100; |
| 1564 | |
| 1565 | std::vector<VkMat> bindings(3); |
| 1566 | bindings[0] = bottom_tm_blob; |
| 1567 | bindings[1] = top_tm_blob; |
| 1568 | bindings[2] = weight_data_gpu_tm_winograd23; |
| 1569 | |
| 1570 | std::vector<vk_constant_type> constants(5); |
| 1571 | constants[0].i = bottom_tm_blob.h; |
| 1572 | constants[1].i = bottom_tm_blob.cstep; |
| 1573 | constants[2].i = top_tm_blob.w; |
| 1574 | constants[3].i = top_tm_blob.h; |
| 1575 | constants[4].i = top_tm_blob.cstep; |
| 1576 | |
| 1577 | VkMat dispatcher; |
| 1578 | dispatcher.w = (top_tm_blob.w + 3) / 4; |
| 1579 | dispatcher.h = top_tm_blob.h; |
| 1580 | dispatcher.c = 16; |
| 1581 | |
| 1582 | if (use_cooperative_matrix_16_8_8) |
| 1583 | { |
| 1584 | dispatcher.w = ((top_tm_blob.w + 15) / 16 + 1) / 2 * 32; |
| 1585 | dispatcher.h = ((top_tm_blob.h + 1) / 2 + 3) / 4; |
| 1586 | dispatcher.c = 16; |
| 1587 | } |
| 1588 | else if (use_cooperative_matrix_16_16_16) |
| 1589 | { |
| 1590 | dispatcher.w = ((top_tm_blob.w + 15) / 16 + 1) / 2 * 32; |
| 1591 | dispatcher.h = ((top_tm_blob.h + 3) / 4 + 1) / 2; |
| 1592 | dispatcher.c = 16; |
| 1593 | } |
| 1594 | |
| 1595 | cmd.record_pipeline(pipeline_convolution_3x3s1d1_winograd23_gemm, bindings, constants, dispatcher); |
| 1596 | } |
| 1597 | |
| 1598 | // transform output |
| 1599 | { |
| 1600 | top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_vkallocator); |
| 1601 | if (top_blob.empty()) |
| 1602 | return -100; |
| 1603 | |
| 1604 | std::vector<VkMat> bindings(3); |
| 1605 | bindings[0] = top_tm_blob; |
| 1606 | bindings[1] = top_blob; |
| 1607 | bindings[2] = bias_data_gpu; |
| 1608 | |
| 1609 | std::vector<vk_constant_type> constants(7); |
| 1610 | constants[0].i = top_tm_blob.h; |
| 1611 | constants[1].i = top_tm_blob.cstep; |
| 1612 | constants[2].i = block_x; |
| 1613 | constants[3].i = block_y; |
| 1614 | constants[4].i = top_blob.w; |
| 1615 | constants[5].i = top_blob.h; |
| 1616 | constants[6].i = top_blob.cstep; |
| 1617 | |
| 1618 | VkMat dispatcher; |
| 1619 | dispatcher.w = block_x; |
| 1620 | dispatcher.h = block_y; |
| 1621 | dispatcher.c = top_blob.c; |
| 1622 | |
| 1623 | cmd.record_pipeline(pipeline_convolution_3x3s1d1_winograd23_transform_output, bindings, constants, dispatcher); |
| 1624 | } |
| 1625 | } |
| 1626 | |
| 1627 | return 0; |
| 1628 | } |
| 1629 | if (opt.use_sgemm_convolution && !is_conv1x1s1d1 && channels * elempack >= 16 && num_output >= 16) |
| 1630 | { |
| 1631 | bool use_cooperative_matrix_16_8_8 = vkdev->info.support_cooperative_matrix_16_8_8() && opt.use_cooperative_matrix && !opt.use_image_storage && !opt.use_shader_pack8 && opt.use_fp16_storage && channels * elempack % 8 == 0 && num_output % 8 == 0; |
| 1632 | bool use_cooperative_matrix_16_16_16 = vkdev->info.support_cooperative_matrix_16_16_16() && opt.use_cooperative_matrix && !opt.use_image_storage && !opt.use_shader_pack8 && opt.use_fp16_storage && channels * elempack % 16 == 0 && num_output % 16 == 0; |
| 1633 | |
| 1634 | // gemm |
| 1635 | top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_vkallocator); |
| 1636 | if (top_blob.empty()) |
| 1637 | return -100; |
| 1638 | |
| 1639 | std::vector<VkMat> bindings(4); |
| 1640 | bindings[0] = bottom_blob_bordered; |
| 1641 | bindings[1] = top_blob; |
| 1642 | bindings[2] = weight_data_gpu; |
| 1643 | bindings[3] = bias_data_gpu; |
| 1644 | |
| 1645 | std::vector<vk_constant_type> constants(8); |
| 1646 | constants[0].i = bottom_blob_bordered.w; |
| 1647 | constants[1].i = bottom_blob_bordered.h; |
| 1648 | constants[2].i = bottom_blob_bordered.c; |
| 1649 | constants[3].i = bottom_blob_bordered.cstep; |
| 1650 | constants[4].i = top_blob.w; |
| 1651 | constants[5].i = top_blob.h; |
| 1652 | constants[6].i = top_blob.c; |
| 1653 | constants[7].i = top_blob.cstep; |
| 1654 | |
| 1655 | VkMat dispatcher; |
| 1656 | dispatcher.w = (top_blob.w * top_blob.h + 3) / 4; |
| 1657 | dispatcher.h = top_blob.c; |
| 1658 | dispatcher.c = 1; |
| 1659 | |
| 1660 | if (use_cooperative_matrix_16_8_8) |
| 1661 | { |
| 1662 | dispatcher.w = ((top_blob.w * top_blob.h + 15) / 16 + 1) / 2 * 32; |
| 1663 | dispatcher.h = ((top_blob.c + 1) / 2 + 3) / 4; |
| 1664 | dispatcher.c = 1; |
| 1665 | } |
| 1666 | else if (use_cooperative_matrix_16_16_16) |
| 1667 | { |
| 1668 | dispatcher.w = ((top_blob.w * top_blob.h + 15) / 16 + 1) / 2 * 32; |
| 1669 | dispatcher.h = ((top_blob.c + 3) / 4 + 1) / 2; |
| 1670 | dispatcher.c = 1; |
| 1671 | } |
| 1672 | |
| 1673 | cmd.record_pipeline(pipeline_convolution_gemm, bindings, constants, dispatcher); |
| 1674 | |
| 1675 | return 0; |
| 1676 | } |
| 1677 | if (is_conv1x1s1d1) |
| 1678 | { |
| 1679 | bool use_cooperative_matrix_16_8_8 = vkdev->info.support_cooperative_matrix_16_8_8() && opt.use_cooperative_matrix && !opt.use_image_storage && !opt.use_shader_pack8 && opt.use_fp16_storage && channels * elempack % 8 == 0 && num_output % 8 == 0; |
| 1680 | bool use_cooperative_matrix_16_16_16 = vkdev->info.support_cooperative_matrix_16_16_16() && opt.use_cooperative_matrix && !opt.use_image_storage && !opt.use_shader_pack8 && opt.use_fp16_storage && channels * elempack % 16 == 0 && num_output % 16 == 0; |
| 1681 | |
| 1682 | top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_vkallocator); |
| 1683 | if (top_blob.empty()) |
| 1684 | return -100; |
| 1685 | |
| 1686 | std::vector<VkMat> bindings(4); |
| 1687 | bindings[0] = bottom_blob_bordered; |
| 1688 | bindings[1] = top_blob; |
| 1689 | bindings[2] = weight_data_gpu; |
| 1690 | bindings[3] = bias_data_gpu; |
| 1691 | |
| 1692 | std::vector<vk_constant_type> constants(8); |
| 1693 | constants[0].i = bottom_blob_bordered.w; |
| 1694 | constants[1].i = bottom_blob_bordered.h; |
| 1695 | constants[2].i = bottom_blob_bordered.c; |
| 1696 | constants[3].i = bottom_blob_bordered.cstep; |
| 1697 | constants[4].i = top_blob.w; |
| 1698 | constants[5].i = top_blob.h; |
| 1699 | constants[6].i = top_blob.c; |
| 1700 | constants[7].i = top_blob.cstep; |
| 1701 | |
| 1702 | VkMat dispatcher; |
| 1703 | dispatcher.w = (top_blob.w * top_blob.h + 3) / 4; |
| 1704 | dispatcher.h = top_blob.c; |
| 1705 | dispatcher.c = 1; |
| 1706 | |
| 1707 | if (use_cooperative_matrix_16_8_8) |
| 1708 | { |
| 1709 | dispatcher.w = ((top_blob.w * top_blob.h + 15) / 16 + 1) / 2 * 32; |
| 1710 | dispatcher.h = ((top_blob.c + 1) / 2 + 3) / 4; |
| 1711 | dispatcher.c = 1; |
| 1712 | } |
| 1713 | else if (use_cooperative_matrix_16_16_16) |
| 1714 | { |
| 1715 | dispatcher.w = ((top_blob.w * top_blob.h + 15) / 16 + 1) / 2 * 32; |
| 1716 | dispatcher.h = ((top_blob.c + 3) / 4 + 1) / 2; |
| 1717 | dispatcher.c = 1; |
| 1718 | } |
| 1719 | |
| 1720 | cmd.record_pipeline(pipeline_convolution_1x1s1d1, bindings, constants, dispatcher); |
| 1721 | |
| 1722 | return 0; |
| 1723 | } |
| 1724 | |
| 1725 | top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_vkallocator); |
| 1726 | if (top_blob.empty()) |
| 1727 | return -100; |
| 1728 | |
| 1729 | std::vector<VkMat> bindings(4); |
| 1730 | bindings[0] = bottom_blob_bordered; |
| 1731 | bindings[1] = top_blob; |
| 1732 | bindings[2] = weight_data_gpu; |
| 1733 | bindings[3] = bias_data_gpu; |
| 1734 | |
| 1735 | std::vector<vk_constant_type> constants(10); |
| 1736 | constants[0].i = bottom_blob_bordered.dims; |
| 1737 | constants[1].i = bottom_blob_bordered.w; |
| 1738 | constants[2].i = bottom_blob_bordered.h; |
| 1739 | constants[3].i = bottom_blob_bordered.c; |
| 1740 | constants[4].i = bottom_blob_bordered.cstep; |
| 1741 | constants[5].i = top_blob.dims; |
| 1742 | constants[6].i = top_blob.w; |
| 1743 | constants[7].i = top_blob.h; |
| 1744 | constants[8].i = top_blob.c; |
| 1745 | constants[9].i = top_blob.cstep; |
| 1746 | |
| 1747 | VkMat dispatcher; |
| 1748 | dispatcher.w = (top_blob.w + 1) / 2; |
| 1749 | dispatcher.h = (top_blob.h + 1) / 2; |
| 1750 | dispatcher.c = (top_blob.c + 1) / 2; |
| 1751 | |
| 1752 | cmd.record_pipeline(pipeline_convolution, bindings, constants, dispatcher); |
| 1753 | |
| 1754 | return 0; |
| 1755 | } |
| 1756 | |
| 1757 | int Convolution_vulkan::forward(const VkImageMat& bottom_blob, VkImageMat& top_blob, VkCompute& cmd, const Option& opt) const |
| 1758 | { |
| 1759 | int w = bottom_blob.w; |
| 1760 | int h = bottom_blob.h; |
| 1761 | int channels = bottom_blob.c; |
| 1762 | size_t elemsize = bottom_blob.elemsize; |
| 1763 | int elempack = bottom_blob.elempack; |
| 1764 | |
| 1765 | // flattened blob, implement as InnerProduct |
| 1766 | if (bottom_blob.dims == 1 && kernel_w == 1 && kernel_h == 1) |
| 1767 | { |
| 1768 | int num_input = weight_data_size / num_output; |
| 1769 | if (bottom_blob.w * bottom_blob.elempack == num_input) |
| 1770 | { |
| 1771 | VkImageMat bottom_blob_1x1xw; |
| 1772 | { |
| 1773 | Option opt_reshape = opt; |
| 1774 | opt_reshape.blob_vkallocator = opt.workspace_vkallocator; |
| 1775 | reshape_1x1xw->forward(bottom_blob, bottom_blob_1x1xw, cmd, opt_reshape); |
| 1776 | } |
| 1777 | |
| 1778 | if (bottom_blob_1x1xw.empty()) |
| 1779 | return -100; |
| 1780 | |
| 1781 | VkImageMat top_blob_1x1xw; |
| 1782 | int ret = forward(bottom_blob_1x1xw, top_blob_1x1xw, cmd, opt); |
| 1783 | if (ret != 0) |
| 1784 | return ret; |
| 1785 | |
| 1786 | return reshape_w->forward(top_blob_1x1xw, top_blob, cmd, opt); |
| 1787 | } |
| 1788 | } |
| 1789 | |
| 1790 | const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; |
| 1791 | const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1; |
| 1792 | |
| 1793 | VkImageMat bottom_blob_bordered = bottom_blob; |
| 1794 | if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0) |
| 1795 | { |
| 1796 | Option opt_pad = opt; |
| 1797 | opt_pad.blob_vkallocator = opt.workspace_vkallocator; |
| 1798 | |
| 1799 | padding->forward(bottom_blob, bottom_blob_bordered, cmd, opt_pad); |
| 1800 | } |
| 1801 | else if (pad_left == -233 && pad_right == -233 && pad_top == -233 && pad_bottom == -233) |
| 1802 | { |
| 1803 | int wpad = kernel_extent_w + (w - 1) / stride_w * stride_w - w; |
| 1804 | int hpad = kernel_extent_h + (h - 1) / stride_h * stride_h - h; |
| 1805 | if (wpad > 0 || hpad > 0) |
| 1806 | { |
| 1807 | Option opt_pad = opt; |
| 1808 | opt_pad.blob_vkallocator = opt.workspace_vkallocator; |
| 1809 | |
| 1810 | VkImageMat padding_param_blob(6, (size_t)4u, 1, opt.staging_vkallocator); |
| 1811 | int* padding_params = padding_param_blob.mapped(); |
| 1812 | |
| 1813 | padding_params[0] = hpad / 2; |
| 1814 | padding_params[1] = hpad - hpad / 2; |
| 1815 | padding_params[2] = wpad / 2; |
| 1816 | padding_params[3] = wpad - wpad / 2; |
| 1817 | padding_params[4] = 0; |
| 1818 | padding_params[5] = 0; |
| 1819 | |
| 1820 | std::vector<VkImageMat> padding_inputs(2); |
| 1821 | padding_inputs[0] = bottom_blob; |
| 1822 | padding_inputs[1] = padding_param_blob; |
| 1823 | |
| 1824 | std::vector<VkImageMat> padding_outputs(1); |
| 1825 | padding->forward(padding_inputs, padding_outputs, cmd, opt_pad); |
| 1826 | bottom_blob_bordered = padding_outputs[0]; |
| 1827 | } |
| 1828 | } |
| 1829 | else if (pad_left == -234 && pad_right == -234 && pad_top == -234 && pad_bottom == -234) |
| 1830 | { |
| 1831 | int wpad = kernel_extent_w + (w - 1) / stride_w * stride_w - w; |
| 1832 | int hpad = kernel_extent_h + (h - 1) / stride_h * stride_h - h; |
| 1833 | if (wpad > 0 || hpad > 0) |
| 1834 | { |
| 1835 | Option opt_pad = opt; |
| 1836 | opt_pad.blob_vkallocator = opt.workspace_vkallocator; |
| 1837 | |
| 1838 | VkImageMat padding_param_blob(6, (size_t)4u, 1, opt.staging_vkallocator); |
| 1839 | int* padding_params = padding_param_blob.mapped(); |
| 1840 | |
| 1841 | padding_params[0] = hpad - hpad / 2; |
| 1842 | padding_params[1] = hpad / 2; |
| 1843 | padding_params[2] = wpad - wpad / 2; |
| 1844 | padding_params[3] = wpad / 2; |
| 1845 | padding_params[4] = 0; |
| 1846 | padding_params[5] = 0; |
| 1847 | |
| 1848 | std::vector<VkImageMat> padding_inputs(2); |
| 1849 | padding_inputs[0] = bottom_blob; |
| 1850 | padding_inputs[1] = padding_param_blob; |
| 1851 | |
| 1852 | std::vector<VkImageMat> padding_outputs(1); |
| 1853 | padding->forward(padding_inputs, padding_outputs, cmd, opt_pad); |
| 1854 | bottom_blob_bordered = padding_outputs[0]; |
| 1855 | } |
| 1856 | } |
| 1857 | |
| 1858 | w = bottom_blob_bordered.w; |
| 1859 | h = bottom_blob_bordered.h; |
| 1860 | |
| 1861 | int outw = (w - kernel_extent_w) / stride_w + 1; |
| 1862 | int outh = (h - kernel_extent_h) / stride_h + 1; |
| 1863 | int out_elempack = opt.use_shader_pack8 && num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1; |
| 1864 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 1865 | |
| 1866 | if (opt.use_fp16_packed && !opt.use_fp16_storage) |
| 1867 | { |
| 1868 | if (out_elempack == 8) out_elemsize = 8 * 2u; |
| 1869 | if (out_elempack == 4) out_elemsize = 4 * 2u; |
| 1870 | if (out_elempack == 1) out_elemsize = 4u; |
| 1871 | } |
| 1872 | |
| 1873 | bool is_conv1x1s1d1 = kernel_w == 1 && kernel_h == 1 && stride_w == 1 && stride_h == 1 && dilation_w == 1 && dilation_h == 1; |
| 1874 | bool is_conv3x3s1d1 = kernel_w == 3 && kernel_h == 3 && stride_w == 1 && stride_h == 1 && dilation_w == 1 && dilation_h == 1; |
| 1875 | |
| 1876 | if (opt.use_winograd_convolution && (opt.use_winograd23_convolution || opt.use_winograd43_convolution) && is_conv3x3s1d1 && channels * elempack >= 16 && num_output >= 16) |
| 1877 | { |
| 1878 | bool pre_winograd43 = opt.use_winograd43_convolution; |
| 1879 | if (opt.use_winograd23_convolution) |
| 1880 | { |
| 1881 | if (vkdev->info.type() == 0 && ((w <= 18 && h <= 18) || ((w >= 23 && w <= 24) && (h >= 23 && h <= 24)))) |
| 1882 | pre_winograd43 = false; |
| 1883 | if (vkdev->info.type() != 0 && (w <= 12 && h <= 12)) |
| 1884 | pre_winograd43 = false; |
| 1885 | } |
| 1886 | |
| 1887 | if (pre_winograd43) |
| 1888 | { |
| 1889 | // winograd43 |
| 1890 | int block_x = (outw + 3) / 4; |
| 1891 | int block_y = (outh + 3) / 4; |
| 1892 | |
| 1893 | // transform input |
| 1894 | VkImageMat bottom_tm_blob; |
| 1895 | { |
| 1896 | bottom_tm_blob.create(block_x * block_y, channels, 36, elemsize, elempack, opt.workspace_vkallocator); |
| 1897 | if (bottom_tm_blob.empty()) |
| 1898 | return -100; |
| 1899 | |
| 1900 | std::vector<VkImageMat> bindings(2); |
| 1901 | bindings[0] = bottom_blob_bordered; |
| 1902 | bindings[1] = bottom_tm_blob; |
| 1903 | |
| 1904 | std::vector<vk_constant_type> constants(7); |
| 1905 | constants[0].i = bottom_blob_bordered.w; |
| 1906 | constants[1].i = bottom_blob_bordered.h; |
| 1907 | constants[2].i = bottom_blob_bordered.c; |
| 1908 | constants[3].i = 0; //bottom_blob_bordered.cstep; |
| 1909 | constants[4].i = 0; //bottom_tm_blob.cstep; |
| 1910 | constants[5].i = block_x; |
| 1911 | constants[6].i = block_y; |
| 1912 | |
| 1913 | VkImageMat dispatcher; |
| 1914 | dispatcher.w = block_x; |
| 1915 | dispatcher.h = block_y; |
| 1916 | dispatcher.c = bottom_tm_blob.h; |
| 1917 | |
| 1918 | cmd.record_pipeline(pipeline_convolution_3x3s1d1_winograd43_transform_input, bindings, constants, dispatcher); |
| 1919 | } |
| 1920 | |
| 1921 | // gemm |
| 1922 | VkImageMat top_tm_blob; |
| 1923 | { |
| 1924 | top_tm_blob.create(block_x * block_y, num_output / out_elempack, 36, out_elemsize, out_elempack, opt.workspace_vkallocator); |
| 1925 | if (top_tm_blob.empty()) |
| 1926 | return -100; |
| 1927 | |
| 1928 | std::vector<VkImageMat> bindings(3); |
| 1929 | bindings[0] = bottom_tm_blob; |
| 1930 | bindings[1] = top_tm_blob; |
| 1931 | bindings[2] = weight_data_gpu_tm_winograd43_image; |
| 1932 | |
| 1933 | std::vector<vk_constant_type> constants(5); |
| 1934 | constants[0].i = bottom_tm_blob.h; |
| 1935 | constants[1].i = 0; //bottom_tm_blob.cstep; |
| 1936 | constants[2].i = top_tm_blob.w; |
| 1937 | constants[3].i = top_tm_blob.h; |
| 1938 | constants[4].i = 0; //top_tm_blob.cstep; |
| 1939 | |
| 1940 | VkImageMat dispatcher; |
| 1941 | dispatcher.w = (top_tm_blob.w + 3) / 4; |
| 1942 | dispatcher.h = top_tm_blob.h; |
| 1943 | dispatcher.c = 36; |
| 1944 | |
| 1945 | cmd.record_pipeline(pipeline_convolution_3x3s1d1_winograd43_gemm, bindings, constants, dispatcher); |
| 1946 | } |
| 1947 | |
| 1948 | // transform output |
| 1949 | { |
| 1950 | top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_vkallocator); |
| 1951 | if (top_blob.empty()) |
| 1952 | return -100; |
| 1953 | |
| 1954 | std::vector<VkImageMat> bindings(3); |
| 1955 | bindings[0] = top_tm_blob; |
| 1956 | bindings[1] = top_blob; |
| 1957 | bindings[2] = bias_data_gpu_image; |
| 1958 | |
| 1959 | std::vector<vk_constant_type> constants(7); |
| 1960 | constants[0].i = top_tm_blob.h; |
| 1961 | constants[1].i = 0; //top_tm_blob.cstep; |
| 1962 | constants[2].i = block_x; |
| 1963 | constants[3].i = block_y; |
| 1964 | constants[4].i = top_blob.w; |
| 1965 | constants[5].i = top_blob.h; |
| 1966 | constants[6].i = 0; //top_blob.cstep; |
| 1967 | |
| 1968 | VkImageMat dispatcher; |
| 1969 | dispatcher.w = block_x; |
| 1970 | dispatcher.h = block_y; |
| 1971 | dispatcher.c = top_blob.c; |
| 1972 | |
| 1973 | cmd.record_pipeline(pipeline_convolution_3x3s1d1_winograd43_transform_output, bindings, constants, dispatcher); |
| 1974 | } |
| 1975 | } |
| 1976 | else |
| 1977 | { |
| 1978 | // winograd23 |
| 1979 | int block_x = (outw + 1) / 2; |
| 1980 | int block_y = (outh + 1) / 2; |
| 1981 | |
| 1982 | // transform input |
| 1983 | VkImageMat bottom_tm_blob; |
| 1984 | { |
| 1985 | bottom_tm_blob.create(block_x * block_y, channels, 16, elemsize, elempack, opt.workspace_vkallocator); |
| 1986 | if (bottom_tm_blob.empty()) |
| 1987 | return -100; |
| 1988 | |
| 1989 | std::vector<VkImageMat> bindings(2); |
| 1990 | bindings[0] = bottom_blob_bordered; |
| 1991 | bindings[1] = bottom_tm_blob; |
| 1992 | |
| 1993 | std::vector<vk_constant_type> constants(7); |
| 1994 | constants[0].i = bottom_blob_bordered.w; |
| 1995 | constants[1].i = bottom_blob_bordered.h; |
| 1996 | constants[2].i = bottom_blob_bordered.c; |
| 1997 | constants[3].i = 0; //bottom_blob_bordered.cstep; |
| 1998 | constants[4].i = 0; //bottom_tm_blob.cstep; |
| 1999 | constants[5].i = block_x; |
| 2000 | constants[6].i = block_y; |
| 2001 | |
| 2002 | VkImageMat dispatcher; |
| 2003 | dispatcher.w = block_x; |
| 2004 | dispatcher.h = block_y; |
| 2005 | dispatcher.c = bottom_tm_blob.h; |
| 2006 | |
| 2007 | cmd.record_pipeline(pipeline_convolution_3x3s1d1_winograd23_transform_input, bindings, constants, dispatcher); |
| 2008 | } |
| 2009 | |
| 2010 | // gemm |
| 2011 | VkImageMat top_tm_blob; |
| 2012 | { |
| 2013 | top_tm_blob.create(block_x * block_y, num_output / out_elempack, 16, out_elemsize, out_elempack, opt.workspace_vkallocator); |
| 2014 | if (top_tm_blob.empty()) |
| 2015 | return -100; |
| 2016 | |
| 2017 | std::vector<VkImageMat> bindings(3); |
| 2018 | bindings[0] = bottom_tm_blob; |
| 2019 | bindings[1] = top_tm_blob; |
| 2020 | bindings[2] = weight_data_gpu_tm_winograd23_image; |
| 2021 | |
| 2022 | std::vector<vk_constant_type> constants(5); |
| 2023 | constants[0].i = bottom_tm_blob.h; |
| 2024 | constants[1].i = 0; //bottom_tm_blob.cstep; |
| 2025 | constants[2].i = top_tm_blob.w; |
| 2026 | constants[3].i = top_tm_blob.h; |
| 2027 | constants[4].i = 0; //top_tm_blob.cstep; |
| 2028 | |
| 2029 | VkImageMat dispatcher; |
| 2030 | dispatcher.w = (top_tm_blob.w + 3) / 4; |
| 2031 | dispatcher.h = top_tm_blob.h; |
| 2032 | dispatcher.c = 16; |
| 2033 | |
| 2034 | cmd.record_pipeline(pipeline_convolution_3x3s1d1_winograd23_gemm, bindings, constants, dispatcher); |
| 2035 | } |
| 2036 | |
| 2037 | // transform output |
| 2038 | { |
| 2039 | top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_vkallocator); |
| 2040 | if (top_blob.empty()) |
| 2041 | return -100; |
| 2042 | |
| 2043 | std::vector<VkImageMat> bindings(3); |
| 2044 | bindings[0] = top_tm_blob; |
| 2045 | bindings[1] = top_blob; |
| 2046 | bindings[2] = bias_data_gpu_image; |
| 2047 | |
| 2048 | std::vector<vk_constant_type> constants(7); |
| 2049 | constants[0].i = top_tm_blob.h; |
| 2050 | constants[1].i = 0; //top_tm_blob.cstep; |
| 2051 | constants[2].i = block_x; |
| 2052 | constants[3].i = block_y; |
| 2053 | constants[4].i = top_blob.w; |
| 2054 | constants[5].i = top_blob.h; |
| 2055 | constants[6].i = 0; //top_blob.cstep; |
| 2056 | |
| 2057 | VkImageMat dispatcher; |
| 2058 | dispatcher.w = block_x; |
| 2059 | dispatcher.h = block_y; |
| 2060 | dispatcher.c = top_blob.c; |
| 2061 | |
| 2062 | cmd.record_pipeline(pipeline_convolution_3x3s1d1_winograd23_transform_output, bindings, constants, dispatcher); |
| 2063 | } |
| 2064 | } |
| 2065 | |
| 2066 | return 0; |
| 2067 | } |
| 2068 | if (opt.use_sgemm_convolution && !is_conv1x1s1d1 && channels * elempack >= 16 && num_output >= 16) |
| 2069 | { |
| 2070 | // gemm |
| 2071 | top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_vkallocator); |
| 2072 | if (top_blob.empty()) |
| 2073 | return -100; |
| 2074 | |
| 2075 | std::vector<VkImageMat> bindings(4); |
| 2076 | bindings[0] = bottom_blob_bordered; |
| 2077 | bindings[1] = top_blob; |
| 2078 | bindings[2] = weight_data_gpu_image; |
| 2079 | bindings[3] = bias_data_gpu_image; |
| 2080 | |
| 2081 | std::vector<vk_constant_type> constants(8); |
| 2082 | constants[0].i = bottom_blob_bordered.w; |
| 2083 | constants[1].i = bottom_blob_bordered.h; |
| 2084 | constants[2].i = bottom_blob_bordered.c; |
| 2085 | constants[3].i = 0; // bottom_blob_bordered.cstep; |
| 2086 | constants[4].i = top_blob.w; |
| 2087 | constants[5].i = top_blob.h; |
| 2088 | constants[6].i = top_blob.c; |
| 2089 | constants[7].i = 0; // top_blob.cstep; |
| 2090 | |
| 2091 | VkImageMat dispatcher; |
| 2092 | dispatcher.w = (top_blob.w * top_blob.h + 3) / 4; |
| 2093 | dispatcher.h = top_blob.c; |
| 2094 | dispatcher.c = 1; |
| 2095 | |
| 2096 | cmd.record_pipeline(pipeline_convolution_gemm, bindings, constants, dispatcher); |
| 2097 | |
| 2098 | return 0; |
| 2099 | } |
| 2100 | if (is_conv1x1s1d1) |
| 2101 | { |
| 2102 | top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_vkallocator); |
| 2103 | if (top_blob.empty()) |
| 2104 | return -100; |
| 2105 | |
| 2106 | std::vector<VkImageMat> bindings(4); |
| 2107 | bindings[0] = bottom_blob_bordered; |
| 2108 | bindings[1] = top_blob; |
| 2109 | bindings[2] = weight_data_gpu_image; |
| 2110 | bindings[3] = bias_data_gpu_image; |
| 2111 | |
| 2112 | std::vector<vk_constant_type> constants(8); |
| 2113 | constants[0].i = bottom_blob_bordered.w; |
| 2114 | constants[1].i = bottom_blob_bordered.h; |
| 2115 | constants[2].i = bottom_blob_bordered.c; |
| 2116 | constants[3].i = 0; // bottom_blob_bordered.cstep; |
| 2117 | constants[4].i = top_blob.w; |
| 2118 | constants[5].i = top_blob.h; |
| 2119 | constants[6].i = top_blob.c; |
| 2120 | constants[7].i = 0; // top_blob.cstep; |
| 2121 | |
| 2122 | VkImageMat dispatcher; |
| 2123 | dispatcher.w = (top_blob.w * top_blob.h + 3) / 4; |
| 2124 | dispatcher.h = top_blob.c; |
| 2125 | dispatcher.c = 1; |
| 2126 | |
| 2127 | cmd.record_pipeline(pipeline_convolution_1x1s1d1, bindings, constants, dispatcher); |
| 2128 | |
| 2129 | return 0; |
| 2130 | } |
| 2131 | |
| 2132 | top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_vkallocator); |
| 2133 | if (top_blob.empty()) |
| 2134 | return -100; |
| 2135 | |
| 2136 | std::vector<VkImageMat> bindings(4); |
| 2137 | bindings[0] = bottom_blob_bordered; |
| 2138 | bindings[1] = top_blob; |
| 2139 | bindings[2] = weight_data_gpu_image; |
| 2140 | bindings[3] = bias_data_gpu_image; |
| 2141 | |
| 2142 | std::vector<vk_constant_type> constants(10); |
| 2143 | constants[0].i = bottom_blob_bordered.dims; |
| 2144 | constants[1].i = bottom_blob_bordered.w; |
| 2145 | constants[2].i = bottom_blob_bordered.h; |
| 2146 | constants[3].i = bottom_blob_bordered.c; |
| 2147 | constants[4].i = 0; //bottom_blob_bordered.cstep; |
| 2148 | constants[5].i = top_blob.dims; |
| 2149 | constants[6].i = top_blob.w; |
| 2150 | constants[7].i = top_blob.h; |
| 2151 | constants[8].i = top_blob.c; |
| 2152 | constants[9].i = 0; //top_blob.cstep; |
| 2153 | |
| 2154 | VkImageMat dispatcher; |
| 2155 | dispatcher.w = (top_blob.w + 1) / 2; |
| 2156 | dispatcher.h = (top_blob.h + 1) / 2; |
| 2157 | dispatcher.c = (top_blob.c + 1) / 2; |
| 2158 | |
| 2159 | cmd.record_pipeline(pipeline_convolution, bindings, constants, dispatcher); |
| 2160 | |
| 2161 | return 0; |
| 2162 | } |
| 2163 | |
| 2164 | } // namespace ncnn |