15
#include "convolutiondepthwise_vulkan.h"
17
#include "layer_shader_type.h"
18
#include "layer_type.h"
22
ConvolutionDepthWise_vulkan::ConvolutionDepthWise_vulkan()
24
support_vulkan = true;
25
support_image_storage = true;
29
pipeline_convolutiondepthwise = 0;
30
pipeline_convolutiondepthwise_pack4 = 0;
31
pipeline_convolutiondepthwise_pack8 = 0;
33
pipeline_convolutiondepthwise_group = 0;
34
pipeline_convolutiondepthwise_group_pack4 = 0;
35
pipeline_convolutiondepthwise_group_pack1to4 = 0;
36
pipeline_convolutiondepthwise_group_pack4to1 = 0;
37
pipeline_convolutiondepthwise_group_pack8 = 0;
38
pipeline_convolutiondepthwise_group_pack1to8 = 0;
39
pipeline_convolutiondepthwise_group_pack4to8 = 0;
40
pipeline_convolutiondepthwise_group_pack8to4 = 0;
41
pipeline_convolutiondepthwise_group_pack8to1 = 0;
44
int ConvolutionDepthWise_vulkan::load_param(const ParamDict& pd)
46
int ret = ConvolutionDepthWise::load_param(pd);
50
support_vulkan = false;
51
support_image_storage = false;
57
int ConvolutionDepthWise_vulkan::create_pipeline(const Option& _opt)
60
const Mat& shape = bottom_shapes.empty() ? Mat() : bottom_shapes[0];
61
const Mat& out_shape = top_shapes.empty() ? Mat() : top_shapes[0];
67
if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0)
69
shape_bordered = Mat(shape.w + pad_left + pad_right, shape.h + pad_top + pad_bottom, shape.c, (void*)0);
71
else if ((pad_left == -233 && pad_right == -233 && pad_top == -233 && pad_bottom == -233)
72
|| (pad_left == -234 && pad_right == -234 && pad_top == -234 && pad_bottom == -234))
74
const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
75
const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
77
int wpad = kernel_extent_w + (shape.w - 1) / stride_w * stride_w - shape.w;
78
int hpad = kernel_extent_h + (shape.h - 1) / stride_h * stride_h - shape.h;
79
if (wpad > 0 || hpad > 0)
81
shape_bordered = Mat(shape.w + wpad, shape.h + hpad, shape.c, (void*)0);
86
shape_bordered = shape;
90
const int maxk = kernel_w * kernel_h;
91
int channels = (weight_data_size / group) / maxk / (num_output / group) * group;
93
int elempack = opt.use_shader_pack8 && channels % 8 == 0 ? 8 : channels % 4 == 0 ? 4 : 1;
94
int out_elempack = opt.use_shader_pack8 && num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1;
98
if (opt.use_fp16_storage)
100
elemsize = elempack * 2u;
101
out_elemsize = out_elempack * 2u;
103
else if (opt.use_fp16_packed)
105
elemsize = elempack == 1 ? 4u : elempack * 2u;
106
out_elemsize = out_elempack == 1 ? 4u : out_elempack * 2u;
110
elemsize = elempack * 4u;
111
out_elemsize = out_elempack * 4u;
114
Mat shape_bordered_packed;
115
if (shape_bordered.dims == 3) shape_bordered_packed = Mat(shape_bordered.w, shape_bordered.h, shape_bordered.c / elempack, (void*)0, elemsize, elempack);
117
Mat out_shape_packed;
118
if (out_shape.dims == 3) out_shape_packed = Mat(out_shape.w, out_shape.h, out_shape.c / out_elempack, (void*)0, out_elemsize, out_elempack);
121
const int channels_g = channels / group;
122
const int num_output_g = num_output / group;
124
int elempack_g = opt.use_shader_pack8 && channels_g % 8 == 0 ? 8 : channels_g % 4 == 0 ? 4 : 1;
125
int out_elempack_g = opt.use_shader_pack8 && num_output_g % 8 == 0 ? 8 : num_output_g % 4 == 0 ? 4 : 1;
128
size_t out_elemsize_g;
129
if (opt.use_fp16_storage)
131
elemsize_g = elempack_g * 2u;
132
out_elemsize_g = out_elempack_g * 2u;
134
else if (opt.use_fp16_packed)
136
elemsize_g = elempack_g == 1 ? 4u : elempack_g * 2u;
137
out_elemsize_g = out_elempack_g == 1 ? 4u : out_elempack_g * 2u;
141
elemsize_g = elempack_g * 4u;
142
out_elemsize_g = out_elempack_g * 4u;
145
Mat shape_bordered_g_packed;
146
if (shape_bordered.dims == 3) shape_bordered_g_packed = Mat(shape_bordered.w, shape_bordered.h, shape_bordered.c / elempack_g, (void*)0, elemsize_g, elempack_g);
148
Mat out_shape_g_packed;
149
if (out_shape.dims == 3) out_shape_g_packed = Mat(out_shape.w, out_shape.h, out_shape.c / out_elempack_g, (void*)0, out_elemsize_g, out_elempack_g);
152
if (!vkdev->shape_support_image_storage(shape_bordered_packed) || !vkdev->shape_support_image_storage(out_shape_packed))
154
support_image_storage = false;
155
opt.use_image_storage = false;
159
if (channels == group && group == num_output)
161
Mat weight_data_packed(maxk, group / elempack, (void*)0, (size_t)4 * elempack, elempack);
162
if (!vkdev->shape_support_image_storage(weight_data_packed))
164
support_image_storage = false;
165
opt.use_image_storage = false;
171
if (!vkdev->shape_support_image_storage(shape_bordered_g_packed) || !vkdev->shape_support_image_storage(out_shape_g_packed))
173
support_image_storage = false;
174
opt.use_image_storage = false;
177
Mat weight_data_packed_groups(maxk, channels_g / elempack_g, num_output_g / out_elempack_g * group, (size_t)4 * elempack_g * out_elempack_g, elempack_g * out_elempack_g);
178
if (!vkdev->shape_support_image_storage(weight_data_packed_groups))
180
support_image_storage = false;
181
opt.use_image_storage = false;
186
padding = ncnn::create_layer_vulkan(ncnn::LayerType::Padding);
187
padding->vkdev = vkdev;
189
padding->bottom_shapes.resize(1);
190
padding->bottom_shapes[0] = shape;
191
padding->top_shapes.resize(1);
192
padding->top_shapes[0] = shape_bordered;
196
pd.set(1, pad_bottom);
198
pd.set(3, pad_right);
200
pd.set(5, pad_value);
202
padding->load_param(pd);
204
padding->create_pipeline(opt);
207
std::vector<vk_specialization_type> specializations(11 + 10);
208
specializations[0].i = kernel_w;
209
specializations[1].i = kernel_h;
210
specializations[2].i = dilation_w;
211
specializations[3].i = dilation_h;
212
specializations[4].i = stride_w;
213
specializations[5].i = stride_h;
214
specializations[6].i = bias_term;
215
specializations[7].i = group;
216
specializations[8].i = activation_type;
217
specializations[9].f = activation_params.w >= 1 ? activation_params[0] : 0.f;
218
specializations[10].f = activation_params.w == 2 ? activation_params[1] : 0.f;
221
if (channels == group && group == num_output)
223
Mat weight_data_r2 = weight_data.reshape(maxk, group);
224
convert_packing(weight_data_r2, weight_data_packed, elempack, opt);
228
convert_packing(bias_data, bias_data_packed, out_elempack, opt);
231
specializations[11 + 0].i = shape_bordered_packed.dims;
232
specializations[11 + 1].i = shape_bordered_packed.w;
233
specializations[11 + 2].i = shape_bordered_packed.h;
234
specializations[11 + 3].i = shape_bordered_packed.c;
235
specializations[11 + 4].i = shape_bordered_packed.cstep;
236
specializations[11 + 5].i = out_shape_packed.dims;
237
specializations[11 + 6].i = out_shape_packed.w;
238
specializations[11 + 7].i = out_shape_packed.h;
239
specializations[11 + 8].i = out_shape_packed.c;
240
specializations[11 + 9].i = out_shape_packed.cstep;
242
Mat local_size_xyz(8, 8, std::min(4, num_output / out_elempack), (void*)0);
243
if (out_shape_packed.dims != 0)
245
local_size_xyz.w = std::min(8, out_shape_packed.w);
246
local_size_xyz.h = std::min(8, out_shape_packed.h);
247
local_size_xyz.c = std::min(4, out_shape_packed.c);
253
pipeline_convolutiondepthwise = new Pipeline(vkdev);
254
pipeline_convolutiondepthwise->set_optimal_local_size_xyz(local_size_xyz);
255
pipeline_convolutiondepthwise->create(LayerShaderType::convolutiondepthwise, opt, specializations);
261
pipeline_convolutiondepthwise_pack4 = new Pipeline(vkdev);
262
pipeline_convolutiondepthwise_pack4->set_optimal_local_size_xyz(local_size_xyz);
263
pipeline_convolutiondepthwise_pack4->create(LayerShaderType::convolutiondepthwise_pack4, opt, specializations);
269
pipeline_convolutiondepthwise_pack8 = new Pipeline(vkdev);
270
pipeline_convolutiondepthwise_pack8->set_optimal_local_size_xyz(local_size_xyz);
271
pipeline_convolutiondepthwise_pack8->create(LayerShaderType::convolutiondepthwise_pack8, opt, specializations);
276
weight_data.release();
286
Mat weight_data_r2_groups = weight_data.reshape(maxk, channels_g, num_output_g * group);
288
weight_data_packed_groups.create(maxk, channels_g / elempack_g, num_output_g / out_elempack_g * group, (size_t)4 * elempack_g * out_elempack_g, elempack_g * out_elempack_g);
290
for (int g = 0; g < group; g++)
292
const Mat weight_data_r2 = weight_data_r2_groups.channel_range(num_output_g * g, num_output_g);
294
Mat weight_data_packed = weight_data_packed_groups.channel_range(num_output_g / out_elempack_g * g, num_output_g / out_elempack_g);
296
for (int q = 0; q + (out_elempack_g - 1) < num_output_g; q += out_elempack_g)
298
float* g00 = weight_data_packed.channel(q / out_elempack_g);
300
for (int p = 0; p + (elempack_g - 1) < channels_g; p += elempack_g)
302
for (int k = 0; k < maxk; k++)
304
for (int i = 0; i < out_elempack_g; i++)
306
const Mat k0 = weight_data_r2.channel(q + i);
308
for (int j = 0; j < elempack_g; j++)
310
const float* k00 = k0.row(p + j);
325
convert_packing(bias_data, bias_data_packed, out_elempack_g, opt);
328
specializations[11 + 0].i = shape_bordered_g_packed.dims;
329
specializations[11 + 1].i = shape_bordered_g_packed.w;
330
specializations[11 + 2].i = shape_bordered_g_packed.h;
331
specializations[11 + 3].i = shape_bordered_g_packed.c;
332
specializations[11 + 4].i = shape_bordered_g_packed.cstep;
333
specializations[11 + 5].i = out_shape_g_packed.dims;
334
specializations[11 + 6].i = out_shape_g_packed.w;
335
specializations[11 + 7].i = out_shape_g_packed.h;
336
specializations[11 + 8].i = out_shape_g_packed.c;
337
specializations[11 + 9].i = out_shape_g_packed.cstep;
339
Mat local_size_xyz(8, 8, std::min(4, num_output / out_elempack_g), (void*)0);
340
if (out_shape_g_packed.dims != 0)
342
local_size_xyz.w = std::min(8, out_shape_g_packed.w);
343
local_size_xyz.h = std::min(8, out_shape_g_packed.h);
344
local_size_xyz.c = std::min(4, out_shape_g_packed.c);
348
if (elempack_g == 1 && out_elempack_g == 1)
350
pipeline_convolutiondepthwise_group = new Pipeline(vkdev);
351
pipeline_convolutiondepthwise_group->set_optimal_local_size_xyz(local_size_xyz);
352
pipeline_convolutiondepthwise_group->create(LayerShaderType::convolutiondepthwise_group, opt, specializations);
356
if (elempack_g == 4 && out_elempack_g == 4)
358
pipeline_convolutiondepthwise_group_pack4 = new Pipeline(vkdev);
359
pipeline_convolutiondepthwise_group_pack4->set_optimal_local_size_xyz(local_size_xyz);
360
pipeline_convolutiondepthwise_group_pack4->create(LayerShaderType::convolutiondepthwise_group_pack4, opt, specializations);
364
if (elempack_g == 1 && out_elempack_g == 4)
366
pipeline_convolutiondepthwise_group_pack1to4 = new Pipeline(vkdev);
367
pipeline_convolutiondepthwise_group_pack1to4->set_optimal_local_size_xyz(local_size_xyz);
368
pipeline_convolutiondepthwise_group_pack1to4->create(LayerShaderType::convolutiondepthwise_group_pack1to4, opt, specializations);
372
if (elempack_g == 4 && out_elempack_g == 1)
374
pipeline_convolutiondepthwise_group_pack4to1 = new Pipeline(vkdev);
375
pipeline_convolutiondepthwise_group_pack4to1->set_optimal_local_size_xyz(local_size_xyz);
376
pipeline_convolutiondepthwise_group_pack4to1->create(LayerShaderType::convolutiondepthwise_group_pack4to1, opt, specializations);
380
if (elempack_g == 8 && out_elempack_g == 8)
382
pipeline_convolutiondepthwise_group_pack8 = new Pipeline(vkdev);
383
pipeline_convolutiondepthwise_group_pack8->set_optimal_local_size_xyz(local_size_xyz);
384
pipeline_convolutiondepthwise_group_pack8->create(LayerShaderType::convolutiondepthwise_group_pack8, opt, specializations);
388
if (elempack_g == 1 && out_elempack_g == 8)
390
pipeline_convolutiondepthwise_group_pack1to8 = new Pipeline(vkdev);
391
pipeline_convolutiondepthwise_group_pack1to8->set_optimal_local_size_xyz(local_size_xyz);
392
pipeline_convolutiondepthwise_group_pack1to8->create(LayerShaderType::convolutiondepthwise_group_pack1to8, opt, specializations);
396
if (elempack_g == 4 && out_elempack_g == 8)
398
pipeline_convolutiondepthwise_group_pack4to8 = new Pipeline(vkdev);
399
pipeline_convolutiondepthwise_group_pack4to8->set_optimal_local_size_xyz(local_size_xyz);
400
pipeline_convolutiondepthwise_group_pack4to8->create(LayerShaderType::convolutiondepthwise_group_pack4to8, opt, specializations);
404
if (elempack_g == 8 && out_elempack_g == 4)
406
pipeline_convolutiondepthwise_group_pack8to4 = new Pipeline(vkdev);
407
pipeline_convolutiondepthwise_group_pack8to4->set_optimal_local_size_xyz(local_size_xyz);
408
pipeline_convolutiondepthwise_group_pack8to4->create(LayerShaderType::convolutiondepthwise_group_pack8to4, opt, specializations);
412
if (elempack_g == 8 && out_elempack_g == 1)
414
pipeline_convolutiondepthwise_group_pack8to1 = new Pipeline(vkdev);
415
pipeline_convolutiondepthwise_group_pack8to1->set_optimal_local_size_xyz(local_size_xyz);
416
pipeline_convolutiondepthwise_group_pack8to1->create(LayerShaderType::convolutiondepthwise_group_pack8to1, opt, specializations);
421
weight_data.release();
428
int ConvolutionDepthWise_vulkan::destroy_pipeline(const Option& opt)
432
padding->destroy_pipeline(opt);
437
delete pipeline_convolutiondepthwise;
438
pipeline_convolutiondepthwise = 0;
440
delete pipeline_convolutiondepthwise_pack4;
441
pipeline_convolutiondepthwise_pack4 = 0;
443
delete pipeline_convolutiondepthwise_pack8;
444
pipeline_convolutiondepthwise_pack8 = 0;
446
delete pipeline_convolutiondepthwise_group;
447
pipeline_convolutiondepthwise_group = 0;
449
delete pipeline_convolutiondepthwise_group_pack4;
450
pipeline_convolutiondepthwise_group_pack4 = 0;
452
delete pipeline_convolutiondepthwise_group_pack1to4;
453
pipeline_convolutiondepthwise_group_pack1to4 = 0;
455
delete pipeline_convolutiondepthwise_group_pack4to1;
456
pipeline_convolutiondepthwise_group_pack4to1 = 0;
458
delete pipeline_convolutiondepthwise_group_pack8;
459
pipeline_convolutiondepthwise_group_pack8 = 0;
461
delete pipeline_convolutiondepthwise_group_pack1to8;
462
pipeline_convolutiondepthwise_group_pack1to8 = 0;
464
delete pipeline_convolutiondepthwise_group_pack4to8;
465
pipeline_convolutiondepthwise_group_pack4to8 = 0;
467
delete pipeline_convolutiondepthwise_group_pack8to4;
468
pipeline_convolutiondepthwise_group_pack8to4 = 0;
470
delete pipeline_convolutiondepthwise_group_pack8to1;
471
pipeline_convolutiondepthwise_group_pack8to1 = 0;
476
int ConvolutionDepthWise_vulkan::upload_model(VkTransfer& cmd, const Option& opt)
480
padding->upload_model(cmd, opt);
483
const int maxk = kernel_w * kernel_h;
484
int channels = (weight_data_size / group) / maxk / (num_output / group) * group;
487
if (channels == group && group == num_output)
489
if (support_image_storage && opt.use_image_storage)
491
cmd.record_upload(weight_data_packed, weight_data_gpu_image, opt);
495
cmd.record_upload(weight_data_packed, weight_data_gpu, opt);
498
weight_data_packed.release();
502
if (support_image_storage && opt.use_image_storage)
504
cmd.record_upload(bias_data_packed, bias_data_gpu_image, opt);
508
cmd.record_upload(bias_data_packed, bias_data_gpu, opt);
511
bias_data_packed.release();
517
if (support_image_storage && opt.use_image_storage)
519
cmd.record_upload(weight_data_packed_groups, weight_data_gpu_image, opt);
523
cmd.record_upload(weight_data_packed_groups, weight_data_gpu, opt);
526
weight_data_packed_groups.release();
530
if (support_image_storage && opt.use_image_storage)
532
cmd.record_upload(bias_data_packed, bias_data_gpu_image, opt);
536
cmd.record_upload(bias_data_packed, bias_data_gpu, opt);
539
bias_data_packed.release();
545
int ConvolutionDepthWise_vulkan::forward(const VkMat& bottom_blob, VkMat& top_blob, VkCompute& cmd, const Option& opt) const
547
int w = bottom_blob.w;
548
int h = bottom_blob.h;
549
int channels = bottom_blob.c;
550
size_t elemsize = bottom_blob.elemsize;
551
int elempack = bottom_blob.elempack;
553
const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
554
const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
556
VkMat bottom_blob_bordered = bottom_blob;
557
if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0)
559
Option opt_pad = opt;
560
opt_pad.blob_vkallocator = opt.workspace_vkallocator;
562
padding->forward(bottom_blob, bottom_blob_bordered, cmd, opt_pad);
564
else if (pad_left == -233 && pad_right == -233 && pad_top == -233 && pad_bottom == -233)
566
int wpad = kernel_extent_w + (w - 1) / stride_w * stride_w - w;
567
int hpad = kernel_extent_h + (h - 1) / stride_h * stride_h - h;
568
if (wpad > 0 || hpad > 0)
570
Option opt_pad = opt;
571
opt_pad.blob_vkallocator = opt.workspace_vkallocator;
573
VkMat padding_param_blob(6, (size_t)4u, 1, opt.staging_vkallocator);
574
int* padding_params = padding_param_blob.mapped();
576
padding_params[0] = hpad / 2;
577
padding_params[1] = hpad - hpad / 2;
578
padding_params[2] = wpad / 2;
579
padding_params[3] = wpad - wpad / 2;
580
padding_params[4] = 0;
581
padding_params[5] = 0;
583
std::vector<VkMat> padding_inputs(2);
584
padding_inputs[0] = bottom_blob;
585
padding_inputs[1] = padding_param_blob;
587
std::vector<VkMat> padding_outputs(1);
588
padding->forward(padding_inputs, padding_outputs, cmd, opt_pad);
589
bottom_blob_bordered = padding_outputs[0];
592
else if (pad_left == -234 && pad_right == -234 && pad_top == -234 && pad_bottom == -234)
594
int wpad = kernel_extent_w + (w - 1) / stride_w * stride_w - w;
595
int hpad = kernel_extent_h + (h - 1) / stride_h * stride_h - h;
596
if (wpad > 0 || hpad > 0)
598
Option opt_pad = opt;
599
opt_pad.blob_vkallocator = opt.workspace_vkallocator;
601
VkMat padding_param_blob(6, (size_t)4u, 1, opt.staging_vkallocator);
602
int* padding_params = padding_param_blob.mapped();
604
padding_params[0] = hpad - hpad / 2;
605
padding_params[1] = hpad / 2;
606
padding_params[2] = wpad - wpad / 2;
607
padding_params[3] = wpad / 2;
608
padding_params[4] = 0;
609
padding_params[5] = 0;
611
std::vector<VkMat> padding_inputs(2);
612
padding_inputs[0] = bottom_blob;
613
padding_inputs[1] = padding_param_blob;
615
std::vector<VkMat> padding_outputs(1);
616
padding->forward(padding_inputs, padding_outputs, cmd, opt_pad);
617
bottom_blob_bordered = padding_outputs[0];
621
w = bottom_blob_bordered.w;
622
h = bottom_blob_bordered.h;
624
int outw = (w - kernel_extent_w) / stride_w + 1;
625
int outh = (h - kernel_extent_h) / stride_h + 1;
626
int out_elempack = opt.use_shader_pack8 && num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1;
627
size_t out_elemsize = elemsize / elempack * out_elempack;
629
if (opt.use_fp16_packed && !opt.use_fp16_storage)
631
if (out_elempack == 8) out_elemsize = 8 * 2u;
632
if (out_elempack == 4) out_elemsize = 4 * 2u;
633
if (out_elempack == 1) out_elemsize = 4u;
636
top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_vkallocator);
637
if (top_blob.empty())
641
if (channels == group / elempack && group / elempack == num_output / elempack)
643
std::vector<VkMat> bindings(4);
644
bindings[0] = bottom_blob_bordered;
645
bindings[1] = top_blob;
646
bindings[2] = weight_data_gpu;
647
bindings[3] = bias_data_gpu;
649
std::vector<vk_constant_type> constants(10);
650
constants[0].i = bottom_blob_bordered.dims;
651
constants[1].i = bottom_blob_bordered.w;
652
constants[2].i = bottom_blob_bordered.h;
653
constants[3].i = bottom_blob_bordered.c;
654
constants[4].i = bottom_blob_bordered.cstep;
655
constants[5].i = top_blob.dims;
656
constants[6].i = top_blob.w;
657
constants[7].i = top_blob.h;
658
constants[8].i = top_blob.c;
659
constants[9].i = top_blob.cstep;
661
const Pipeline* pipeline = elempack == 8 ? pipeline_convolutiondepthwise_pack8
662
: elempack == 4 ? pipeline_convolutiondepthwise_pack4
663
: pipeline_convolutiondepthwise;
665
cmd.record_pipeline(pipeline, bindings, constants, top_blob);
670
const int channels_g = channels * elempack / group;
671
const int num_output_g = num_output / group;
673
int elempack_g = opt.use_shader_pack8 && channels_g % 8 == 0 ? 8 : channels_g % 4 == 0 ? 4 : 1;
674
int out_elempack_g = opt.use_shader_pack8 && num_output_g % 8 == 0 ? 8 : num_output_g % 4 == 0 ? 4 : 1;
675
size_t out_elemsize_g = elemsize / elempack * out_elempack_g;
677
if (opt.use_fp16_packed && !opt.use_fp16_storage)
679
if (out_elempack_g == 8) out_elemsize_g = 8 * 2u;
680
if (out_elempack_g == 4) out_elemsize_g = 4 * 2u;
681
if (out_elempack_g == 1) out_elemsize_g = 4u;
685
VkMat bottom_blob_bordered_unpacked = bottom_blob_bordered;
686
if (elempack > elempack_g)
688
Option opt_pack1 = opt;
689
opt_pack1.blob_vkallocator = opt.workspace_vkallocator;
691
vkdev->convert_packing(bottom_blob_bordered, bottom_blob_bordered_unpacked, elempack_g, cmd, opt_pack1);
694
VkMat top_blob_unpacked = top_blob;
695
if (out_elempack_g < out_elempack)
697
top_blob_unpacked.create(outw, outh, num_output / out_elempack_g, out_elemsize_g, out_elempack_g, opt.workspace_vkallocator);
698
if (top_blob_unpacked.empty())
702
std::vector<VkMat> bindings(4);
703
bindings[0] = bottom_blob_bordered_unpacked;
704
bindings[1] = top_blob_unpacked;
705
bindings[2] = weight_data_gpu;
706
bindings[3] = bias_data_gpu;
708
std::vector<vk_constant_type> constants(10);
709
constants[0].i = bottom_blob_bordered_unpacked.dims;
710
constants[1].i = bottom_blob_bordered_unpacked.w;
711
constants[2].i = bottom_blob_bordered_unpacked.h;
712
constants[3].i = bottom_blob_bordered_unpacked.c;
713
constants[4].i = bottom_blob_bordered_unpacked.cstep;
714
constants[5].i = top_blob_unpacked.dims;
715
constants[6].i = top_blob_unpacked.w;
716
constants[7].i = top_blob_unpacked.h;
717
constants[8].i = top_blob_unpacked.c;
718
constants[9].i = top_blob_unpacked.cstep;
720
const Pipeline* pipeline = 0;
721
if (elempack_g == 1 && out_elempack_g == 1)
723
pipeline = pipeline_convolutiondepthwise_group;
725
else if (elempack_g == 4 && out_elempack_g == 4)
727
pipeline = pipeline_convolutiondepthwise_group_pack4;
729
else if (elempack_g == 1 && out_elempack_g == 4)
731
pipeline = pipeline_convolutiondepthwise_group_pack1to4;
733
else if (elempack_g == 4 && out_elempack_g == 1)
735
pipeline = pipeline_convolutiondepthwise_group_pack4to1;
737
else if (elempack_g == 8 && out_elempack_g == 8)
739
pipeline = pipeline_convolutiondepthwise_group_pack8;
741
else if (elempack_g == 1 && out_elempack_g == 8)
743
pipeline = pipeline_convolutiondepthwise_group_pack1to8;
745
else if (elempack_g == 4 && out_elempack_g == 8)
747
pipeline = pipeline_convolutiondepthwise_group_pack4to8;
749
else if (elempack_g == 8 && out_elempack_g == 4)
751
pipeline = pipeline_convolutiondepthwise_group_pack8to4;
753
else if (elempack_g == 8 && out_elempack_g == 1)
755
pipeline = pipeline_convolutiondepthwise_group_pack8to1;
758
cmd.record_pipeline(pipeline, bindings, constants, top_blob_unpacked);
761
if (out_elempack_g < out_elempack)
763
vkdev->convert_packing(top_blob_unpacked, top_blob, out_elempack, cmd, opt);
767
top_blob = top_blob_unpacked;
773
int ConvolutionDepthWise_vulkan::forward(const VkImageMat& bottom_blob, VkImageMat& top_blob, VkCompute& cmd, const Option& opt) const
775
int w = bottom_blob.w;
776
int h = bottom_blob.h;
777
int channels = bottom_blob.c;
778
size_t elemsize = bottom_blob.elemsize;
779
int elempack = bottom_blob.elempack;
781
const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
782
const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
784
VkImageMat bottom_blob_bordered = bottom_blob;
785
if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0)
787
Option opt_pad = opt;
788
opt_pad.blob_vkallocator = opt.workspace_vkallocator;
790
padding->forward(bottom_blob, bottom_blob_bordered, cmd, opt_pad);
792
else if (pad_left == -233 && pad_right == -233 && pad_top == -233 && pad_bottom == -233)
794
int wpad = kernel_extent_w + (w - 1) / stride_w * stride_w - w;
795
int hpad = kernel_extent_h + (h - 1) / stride_h * stride_h - h;
796
if (wpad > 0 || hpad > 0)
798
Option opt_pad = opt;
799
opt_pad.blob_vkallocator = opt.workspace_vkallocator;
801
VkImageMat padding_param_blob(6, (size_t)4u, 1, opt.staging_vkallocator);
802
int* padding_params = padding_param_blob.mapped();
804
padding_params[0] = hpad / 2;
805
padding_params[1] = hpad - hpad / 2;
806
padding_params[2] = wpad / 2;
807
padding_params[3] = wpad - wpad / 2;
808
padding_params[4] = 0;
809
padding_params[5] = 0;
811
std::vector<VkImageMat> padding_inputs(2);
812
padding_inputs[0] = bottom_blob;
813
padding_inputs[1] = padding_param_blob;
815
std::vector<VkImageMat> padding_outputs(1);
816
padding->forward(padding_inputs, padding_outputs, cmd, opt_pad);
817
bottom_blob_bordered = padding_outputs[0];
820
else if (pad_left == -234 && pad_right == -234 && pad_top == -234 && pad_bottom == -234)
822
int wpad = kernel_extent_w + (w - 1) / stride_w * stride_w - w;
823
int hpad = kernel_extent_h + (h - 1) / stride_h * stride_h - h;
824
if (wpad > 0 || hpad > 0)
826
Option opt_pad = opt;
827
opt_pad.blob_vkallocator = opt.workspace_vkallocator;
829
VkImageMat padding_param_blob(6, (size_t)4u, 1, opt.staging_vkallocator);
830
int* padding_params = padding_param_blob.mapped();
832
padding_params[0] = hpad - hpad / 2;
833
padding_params[1] = hpad / 2;
834
padding_params[2] = wpad - wpad / 2;
835
padding_params[3] = wpad / 2;
836
padding_params[4] = 0;
837
padding_params[5] = 0;
839
std::vector<VkImageMat> padding_inputs(2);
840
padding_inputs[0] = bottom_blob;
841
padding_inputs[1] = padding_param_blob;
843
std::vector<VkImageMat> padding_outputs(1);
844
padding->forward(padding_inputs, padding_outputs, cmd, opt_pad);
845
bottom_blob_bordered = padding_outputs[0];
849
w = bottom_blob_bordered.w;
850
h = bottom_blob_bordered.h;
852
int outw = (w - kernel_extent_w) / stride_w + 1;
853
int outh = (h - kernel_extent_h) / stride_h + 1;
854
int out_elempack = opt.use_shader_pack8 && num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1;
855
size_t out_elemsize = elemsize / elempack * out_elempack;
857
if (opt.use_fp16_packed && !opt.use_fp16_storage)
859
if (out_elempack == 8) out_elemsize = 8 * 2u;
860
if (out_elempack == 4) out_elemsize = 4 * 2u;
861
if (out_elempack == 1) out_elemsize = 4u;
864
top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_vkallocator);
865
if (top_blob.empty())
869
if (channels == group / elempack && group / elempack == num_output / elempack)
871
std::vector<VkImageMat> bindings(4);
872
bindings[0] = bottom_blob_bordered;
873
bindings[1] = top_blob;
874
bindings[2] = weight_data_gpu_image;
875
bindings[3] = bias_data_gpu_image;
877
std::vector<vk_constant_type> constants(10);
878
constants[0].i = bottom_blob_bordered.dims;
879
constants[1].i = bottom_blob_bordered.w;
880
constants[2].i = bottom_blob_bordered.h;
881
constants[3].i = bottom_blob_bordered.c;
883
constants[5].i = top_blob.dims;
884
constants[6].i = top_blob.w;
885
constants[7].i = top_blob.h;
886
constants[8].i = top_blob.c;
889
const Pipeline* pipeline = elempack == 8 ? pipeline_convolutiondepthwise_pack8
890
: elempack == 4 ? pipeline_convolutiondepthwise_pack4
891
: pipeline_convolutiondepthwise;
893
cmd.record_pipeline(pipeline, bindings, constants, top_blob);
898
const int channels_g = channels * elempack / group;
899
const int num_output_g = num_output / group;
901
int elempack_g = opt.use_shader_pack8 && channels_g % 8 == 0 ? 8 : channels_g % 4 == 0 ? 4 : 1;
902
int out_elempack_g = opt.use_shader_pack8 && num_output_g % 8 == 0 ? 8 : num_output_g % 4 == 0 ? 4 : 1;
903
size_t out_elemsize_g = elemsize / elempack * out_elempack_g;
905
if (opt.use_fp16_packed && !opt.use_fp16_storage)
907
if (out_elempack_g == 8) out_elemsize_g = 8 * 2u;
908
if (out_elempack_g == 4) out_elemsize_g = 4 * 2u;
909
if (out_elempack_g == 1) out_elemsize_g = 4u;
913
VkImageMat bottom_blob_bordered_unpacked = bottom_blob_bordered;
914
if (elempack > elempack_g)
916
Option opt_pack1 = opt;
917
opt_pack1.blob_vkallocator = opt.workspace_vkallocator;
919
vkdev->convert_packing(bottom_blob_bordered, bottom_blob_bordered_unpacked, elempack_g, cmd, opt_pack1);
922
VkImageMat top_blob_unpacked = top_blob;
923
if (out_elempack_g < out_elempack)
925
top_blob_unpacked.create(outw, outh, num_output / out_elempack_g, out_elemsize_g, out_elempack_g, opt.workspace_vkallocator);
926
if (top_blob_unpacked.empty())
930
std::vector<VkImageMat> bindings(4);
931
bindings[0] = bottom_blob_bordered_unpacked;
932
bindings[1] = top_blob_unpacked;
933
bindings[2] = weight_data_gpu_image;
934
bindings[3] = bias_data_gpu_image;
936
std::vector<vk_constant_type> constants(10);
937
constants[0].i = bottom_blob_bordered_unpacked.dims;
938
constants[1].i = bottom_blob_bordered_unpacked.w;
939
constants[2].i = bottom_blob_bordered_unpacked.h;
940
constants[3].i = bottom_blob_bordered_unpacked.c;
942
constants[5].i = top_blob_unpacked.dims;
943
constants[6].i = top_blob_unpacked.w;
944
constants[7].i = top_blob_unpacked.h;
945
constants[8].i = top_blob_unpacked.c;
948
const Pipeline* pipeline = 0;
949
if (elempack_g == 1 && out_elempack_g == 1)
951
pipeline = pipeline_convolutiondepthwise_group;
953
else if (elempack_g == 4 && out_elempack_g == 4)
955
pipeline = pipeline_convolutiondepthwise_group_pack4;
957
else if (elempack_g == 1 && out_elempack_g == 4)
959
pipeline = pipeline_convolutiondepthwise_group_pack1to4;
961
else if (elempack_g == 4 && out_elempack_g == 1)
963
pipeline = pipeline_convolutiondepthwise_group_pack4to1;
965
else if (elempack_g == 8 && out_elempack_g == 8)
967
pipeline = pipeline_convolutiondepthwise_group_pack8;
969
else if (elempack_g == 1 && out_elempack_g == 8)
971
pipeline = pipeline_convolutiondepthwise_group_pack1to8;
973
else if (elempack_g == 4 && out_elempack_g == 8)
975
pipeline = pipeline_convolutiondepthwise_group_pack4to8;
977
else if (elempack_g == 8 && out_elempack_g == 4)
979
pipeline = pipeline_convolutiondepthwise_group_pack8to4;
981
else if (elempack_g == 8 && out_elempack_g == 1)
983
pipeline = pipeline_convolutiondepthwise_group_pack8to1;
986
cmd.record_pipeline(pipeline, bindings, constants, top_blob_unpacked);
989
if (out_elempack_g < out_elempack)
991
vkdev->convert_packing(top_blob_unpacked, top_blob, out_elempack, cmd, opt);
995
top_blob = top_blob_unpacked;