15
#include "softmax_vulkan.h"
17
#include "layer_shader_type.h"
21
Softmax_vulkan::Softmax_vulkan()
23
support_vulkan = true;
24
support_image_storage = true;
26
pipeline_softmax_reduce_max = 0;
27
pipeline_softmax_exp_sub_max = 0;
28
pipeline_softmax_reduce_sum = 0;
29
pipeline_softmax_div_sum = 0;
31
pipeline_softmax_reduce_max_pack4 = 0;
32
pipeline_softmax_exp_sub_max_pack4 = 0;
33
pipeline_softmax_reduce_sum_pack4 = 0;
34
pipeline_softmax_div_sum_pack4 = 0;
36
pipeline_softmax_reduce_max_pack8 = 0;
37
pipeline_softmax_exp_sub_max_pack8 = 0;
38
pipeline_softmax_reduce_sum_pack8 = 0;
39
pipeline_softmax_div_sum_pack8 = 0;
42
int Softmax_vulkan::create_pipeline(const Option& opt)
44
const Mat& shape = top_shapes.empty() ? Mat() : top_shapes[0];
45
int positive_axis = axis < 0 ? shape.dims + axis : axis;
48
if (shape.dims == 1) elempack = opt.use_shader_pack8 && shape.w % 8 == 0 ? 8 : shape.w % 4 == 0 ? 4 : 1;
49
if (shape.dims == 2) elempack = opt.use_shader_pack8 && shape.h % 8 == 0 ? 8 : shape.h % 4 == 0 ? 4 : 1;
50
if (shape.dims == 3) elempack = opt.use_shader_pack8 && shape.c % 8 == 0 ? 8 : shape.c % 4 == 0 ? 4 : 1;
53
if (opt.use_fp16_storage)
55
elemsize = elempack * 2u;
57
else if (opt.use_fp16_packed)
59
elemsize = elempack == 1 ? 4u : elempack * 2u;
63
elemsize = elempack * 4u;
67
if (shape.dims == 1) shape_packed = Mat(shape.w / elempack, (void*)0, elemsize, elempack);
68
if (shape.dims == 2) shape_packed = Mat(shape.w, shape.h / elempack, (void*)0, elemsize, elempack);
69
if (shape.dims == 3) shape_packed = Mat(shape.w, shape.h, shape.c / elempack, (void*)0, elemsize, elempack);
71
Mat workspace_shape_packed;
74
workspace_shape_packed = Mat(1, (void*)0, elemsize, elempack);
76
else if (shape.dims == 2 && positive_axis == 0)
78
workspace_shape_packed = Mat(shape.w, (void*)0, elemsize, elempack);
80
else if (shape.dims == 2 && positive_axis == 1)
82
workspace_shape_packed = Mat(shape.h / elempack, (void*)0, elemsize, elempack);
84
else if (shape.dims == 3 && positive_axis == 0)
86
workspace_shape_packed = Mat(shape.w, shape.h, (void*)0, elemsize, elempack);
88
else if (shape.dims == 3 && positive_axis == 1)
90
workspace_shape_packed = Mat(shape.w, shape.c / elempack, (void*)0, elemsize, elempack);
92
else if (shape.dims == 3 && positive_axis == 2)
94
workspace_shape_packed = Mat(shape.h, shape.c / elempack, (void*)0, elemsize, elempack);
97
std::vector<vk_specialization_type> specializations(1 + 10);
98
specializations[0].i = axis;
99
specializations[1 + 0].i = shape_packed.dims;
100
specializations[1 + 1].i = shape_packed.w;
101
specializations[1 + 2].i = shape_packed.h;
102
specializations[1 + 3].i = shape_packed.c;
103
specializations[1 + 4].i = shape_packed.cstep;
104
specializations[1 + 5].i = workspace_shape_packed.dims;
105
specializations[1 + 6].i = workspace_shape_packed.w;
106
specializations[1 + 7].i = workspace_shape_packed.h;
107
specializations[1 + 8].i = workspace_shape_packed.c;
108
specializations[1 + 9].i = workspace_shape_packed.cstep;
112
if (workspace_shape_packed.dims == 1)
114
local_size_xyz.w = std::min(64, workspace_shape_packed.w);
115
local_size_xyz.h = 1;
116
local_size_xyz.c = 1;
118
if (workspace_shape_packed.dims == 2)
120
local_size_xyz.w = std::min(8, workspace_shape_packed.w);
121
local_size_xyz.h = std::min(8, workspace_shape_packed.h);
122
local_size_xyz.c = 1;
124
if (workspace_shape_packed.dims != 0)
126
local_size_xyz.w = std::min(4, workspace_shape_packed.w);
127
local_size_xyz.h = std::min(4, workspace_shape_packed.h);
128
local_size_xyz.c = std::min(4, workspace_shape_packed.c);
133
pipeline_softmax_reduce_max = new Pipeline(vkdev);
134
pipeline_softmax_reduce_sum = new Pipeline(vkdev);
136
pipeline_softmax_reduce_max->set_optimal_local_size_xyz(local_size_xyz);
137
pipeline_softmax_reduce_sum->set_optimal_local_size_xyz(local_size_xyz);
139
pipeline_softmax_reduce_max->create(LayerShaderType::softmax_reduce_max, opt, specializations);
140
pipeline_softmax_reduce_sum->create(LayerShaderType::softmax_reduce_sum, opt, specializations);
145
pipeline_softmax_reduce_max_pack4 = new Pipeline(vkdev);
146
pipeline_softmax_reduce_sum_pack4 = new Pipeline(vkdev);
148
pipeline_softmax_reduce_max_pack4->set_optimal_local_size_xyz(local_size_xyz);
149
pipeline_softmax_reduce_sum_pack4->set_optimal_local_size_xyz(local_size_xyz);
151
pipeline_softmax_reduce_max_pack4->create(LayerShaderType::softmax_reduce_max_pack4, opt, specializations);
152
pipeline_softmax_reduce_sum_pack4->create(LayerShaderType::softmax_reduce_sum_pack4, opt, specializations);
156
if (opt.use_shader_pack8)
158
pipeline_softmax_reduce_max_pack8 = new Pipeline(vkdev);
159
pipeline_softmax_reduce_sum_pack8 = new Pipeline(vkdev);
161
pipeline_softmax_reduce_max_pack8->set_optimal_local_size_xyz(local_size_xyz);
162
pipeline_softmax_reduce_sum_pack8->set_optimal_local_size_xyz(local_size_xyz);
164
pipeline_softmax_reduce_max_pack8->create(LayerShaderType::softmax_reduce_max_pack8, opt, specializations);
165
pipeline_softmax_reduce_sum_pack8->create(LayerShaderType::softmax_reduce_sum_pack8, opt, specializations);
171
if (shape_packed.dims == 1)
173
local_size_xyz.w = std::min(64, shape_packed.w);
174
local_size_xyz.h = 1;
175
local_size_xyz.c = 1;
177
if (shape_packed.dims == 2)
179
local_size_xyz.w = std::min(8, shape_packed.w);
180
local_size_xyz.h = std::min(8, shape_packed.h);
181
local_size_xyz.c = 1;
183
if (shape_packed.dims == 3)
185
local_size_xyz.w = std::min(4, shape_packed.w);
186
local_size_xyz.h = std::min(4, shape_packed.h);
187
local_size_xyz.c = std::min(4, shape_packed.c);
192
pipeline_softmax_exp_sub_max = new Pipeline(vkdev);
193
pipeline_softmax_div_sum = new Pipeline(vkdev);
195
pipeline_softmax_exp_sub_max->set_optimal_local_size_xyz(local_size_xyz);
196
pipeline_softmax_div_sum->set_optimal_local_size_xyz(local_size_xyz);
198
pipeline_softmax_exp_sub_max->create(LayerShaderType::softmax_exp_sub_max, opt, specializations);
199
pipeline_softmax_div_sum->create(LayerShaderType::softmax_div_sum, opt, specializations);
204
pipeline_softmax_exp_sub_max_pack4 = new Pipeline(vkdev);
205
pipeline_softmax_div_sum_pack4 = new Pipeline(vkdev);
207
pipeline_softmax_exp_sub_max_pack4->set_optimal_local_size_xyz(local_size_xyz);
208
pipeline_softmax_div_sum_pack4->set_optimal_local_size_xyz(local_size_xyz);
210
pipeline_softmax_exp_sub_max_pack4->create(LayerShaderType::softmax_exp_sub_max_pack4, opt, specializations);
211
pipeline_softmax_div_sum_pack4->create(LayerShaderType::softmax_div_sum_pack4, opt, specializations);
215
if (opt.use_shader_pack8)
217
pipeline_softmax_exp_sub_max_pack8 = new Pipeline(vkdev);
218
pipeline_softmax_div_sum_pack8 = new Pipeline(vkdev);
220
pipeline_softmax_exp_sub_max_pack8->set_optimal_local_size_xyz(local_size_xyz);
221
pipeline_softmax_div_sum_pack8->set_optimal_local_size_xyz(local_size_xyz);
223
pipeline_softmax_exp_sub_max_pack8->create(LayerShaderType::softmax_exp_sub_max_pack8, opt, specializations);
224
pipeline_softmax_div_sum_pack8->create(LayerShaderType::softmax_div_sum_pack8, opt, specializations);
231
int Softmax_vulkan::destroy_pipeline(const Option& )
233
delete pipeline_softmax_reduce_max;
234
pipeline_softmax_reduce_max = 0;
236
delete pipeline_softmax_exp_sub_max;
237
pipeline_softmax_exp_sub_max = 0;
239
delete pipeline_softmax_reduce_sum;
240
pipeline_softmax_reduce_sum = 0;
242
delete pipeline_softmax_div_sum;
243
pipeline_softmax_div_sum = 0;
245
delete pipeline_softmax_reduce_max_pack4;
246
pipeline_softmax_reduce_max_pack4 = 0;
248
delete pipeline_softmax_exp_sub_max_pack4;
249
pipeline_softmax_exp_sub_max_pack4 = 0;
251
delete pipeline_softmax_reduce_sum_pack4;
252
pipeline_softmax_reduce_sum_pack4 = 0;
254
delete pipeline_softmax_div_sum_pack4;
255
pipeline_softmax_div_sum_pack4 = 0;
257
delete pipeline_softmax_reduce_max_pack8;
258
pipeline_softmax_reduce_max_pack8 = 0;
260
delete pipeline_softmax_exp_sub_max_pack8;
261
pipeline_softmax_exp_sub_max_pack8 = 0;
263
delete pipeline_softmax_reduce_sum_pack8;
264
pipeline_softmax_reduce_sum_pack8 = 0;
266
delete pipeline_softmax_div_sum_pack8;
267
pipeline_softmax_div_sum_pack8 = 0;
272
int Softmax_vulkan::forward_inplace(VkMat& bottom_top_blob, VkCompute& cmd, const Option& opt) const
274
int dims = bottom_top_blob.dims;
275
int w = bottom_top_blob.w;
276
int h = bottom_top_blob.h;
277
int channels = bottom_top_blob.c;
278
size_t elemsize = bottom_top_blob.elemsize;
279
int elempack = bottom_top_blob.elempack;
280
int positive_axis = axis < 0 ? dims + axis : axis;
287
max_workspace.create(1, elemsize, elempack, opt.workspace_vkallocator);
288
sum_workspace.create(1, elemsize, elempack, opt.workspace_vkallocator);
290
else if (dims == 2 && positive_axis == 0)
292
max_workspace.create(w, elemsize, elempack, opt.workspace_vkallocator);
293
sum_workspace.create(w, elemsize, elempack, opt.workspace_vkallocator);
295
else if (dims == 2 && positive_axis == 1)
297
max_workspace.create(h, elemsize, elempack, opt.workspace_vkallocator);
298
sum_workspace.create(h, elemsize, elempack, opt.workspace_vkallocator);
300
else if (dims == 3 && positive_axis == 0)
302
max_workspace.create(w, h, elemsize, elempack, opt.workspace_vkallocator);
303
sum_workspace.create(w, h, elemsize, elempack, opt.workspace_vkallocator);
305
else if (dims == 3 && positive_axis == 1)
307
max_workspace.create(w, channels, elemsize, elempack, opt.workspace_vkallocator);
308
sum_workspace.create(w, channels, elemsize, elempack, opt.workspace_vkallocator);
310
else if (dims == 3 && positive_axis == 2)
312
max_workspace.create(h, channels, elemsize, elempack, opt.workspace_vkallocator);
313
sum_workspace.create(h, channels, elemsize, elempack, opt.workspace_vkallocator);
318
std::vector<VkMat> bindings(2);
319
bindings[0] = bottom_top_blob;
320
bindings[1] = max_workspace;
322
std::vector<vk_constant_type> constants(10);
323
constants[0].i = bottom_top_blob.dims;
324
constants[1].i = bottom_top_blob.w;
325
constants[2].i = bottom_top_blob.h;
326
constants[3].i = bottom_top_blob.c;
327
constants[4].i = bottom_top_blob.cstep;
328
constants[5].i = max_workspace.dims;
329
constants[6].i = max_workspace.w;
330
constants[7].i = max_workspace.h;
331
constants[8].i = max_workspace.c;
332
constants[9].i = max_workspace.cstep;
334
const Pipeline* pipeline = elempack == 8 ? pipeline_softmax_reduce_max_pack8
335
: elempack == 4 ? pipeline_softmax_reduce_max_pack4
336
: pipeline_softmax_reduce_max;
338
cmd.record_pipeline(pipeline, bindings, constants, max_workspace);
343
std::vector<VkMat> bindings(2);
344
bindings[0] = bottom_top_blob;
345
bindings[1] = max_workspace;
347
std::vector<vk_constant_type> constants(10);
348
constants[0].i = bottom_top_blob.dims;
349
constants[1].i = bottom_top_blob.w;
350
constants[2].i = bottom_top_blob.h;
351
constants[3].i = bottom_top_blob.c;
352
constants[4].i = bottom_top_blob.cstep;
353
constants[5].i = max_workspace.dims;
354
constants[6].i = max_workspace.w;
355
constants[7].i = max_workspace.h;
356
constants[8].i = max_workspace.c;
357
constants[9].i = max_workspace.cstep;
359
const Pipeline* pipeline = elempack == 8 ? pipeline_softmax_exp_sub_max_pack8
360
: elempack == 4 ? pipeline_softmax_exp_sub_max_pack4
361
: pipeline_softmax_exp_sub_max;
363
cmd.record_pipeline(pipeline, bindings, constants, bottom_top_blob);
368
std::vector<VkMat> bindings(2);
369
bindings[0] = bottom_top_blob;
370
bindings[1] = sum_workspace;
372
std::vector<vk_constant_type> constants(10);
373
constants[0].i = bottom_top_blob.dims;
374
constants[1].i = bottom_top_blob.w;
375
constants[2].i = bottom_top_blob.h;
376
constants[3].i = bottom_top_blob.c;
377
constants[4].i = bottom_top_blob.cstep;
378
constants[5].i = sum_workspace.dims;
379
constants[6].i = sum_workspace.w;
380
constants[7].i = sum_workspace.h;
381
constants[8].i = sum_workspace.c;
382
constants[9].i = sum_workspace.cstep;
384
const Pipeline* pipeline = elempack == 8 ? pipeline_softmax_reduce_sum_pack8
385
: elempack == 4 ? pipeline_softmax_reduce_sum_pack4
386
: pipeline_softmax_reduce_sum;
388
cmd.record_pipeline(pipeline, bindings, constants, sum_workspace);
393
std::vector<VkMat> bindings(2);
394
bindings[0] = bottom_top_blob;
395
bindings[1] = sum_workspace;
397
std::vector<vk_constant_type> constants(10);
398
constants[0].i = bottom_top_blob.dims;
399
constants[1].i = bottom_top_blob.w;
400
constants[2].i = bottom_top_blob.h;
401
constants[3].i = bottom_top_blob.c;
402
constants[4].i = bottom_top_blob.cstep;
403
constants[5].i = sum_workspace.dims;
404
constants[6].i = sum_workspace.w;
405
constants[7].i = sum_workspace.h;
406
constants[8].i = sum_workspace.c;
407
constants[9].i = sum_workspace.cstep;
409
const Pipeline* pipeline = elempack == 8 ? pipeline_softmax_div_sum_pack8
410
: elempack == 4 ? pipeline_softmax_div_sum_pack4
411
: pipeline_softmax_div_sum;
413
cmd.record_pipeline(pipeline, bindings, constants, bottom_top_blob);
419
int Softmax_vulkan::forward_inplace(VkImageMat& bottom_top_blob, VkCompute& cmd, const Option& opt) const
421
int dims = bottom_top_blob.dims;
422
int w = bottom_top_blob.w;
423
int h = bottom_top_blob.h;
424
int channels = bottom_top_blob.c;
425
size_t elemsize = bottom_top_blob.elemsize;
426
int elempack = bottom_top_blob.elempack;
427
int positive_axis = axis < 0 ? dims + axis : axis;
429
VkImageMat max_workspace;
430
VkImageMat sum_workspace;
434
max_workspace.create(1, elemsize, elempack, opt.workspace_vkallocator);
435
sum_workspace.create(1, elemsize, elempack, opt.workspace_vkallocator);
437
else if (dims == 2 && positive_axis == 0)
439
max_workspace.create(w, elemsize, elempack, opt.workspace_vkallocator);
440
sum_workspace.create(w, elemsize, elempack, opt.workspace_vkallocator);
442
else if (dims == 2 && positive_axis == 1)
444
max_workspace.create(h, elemsize, elempack, opt.workspace_vkallocator);
445
sum_workspace.create(h, elemsize, elempack, opt.workspace_vkallocator);
447
else if (dims == 3 && positive_axis == 0)
449
max_workspace.create(w, h, elemsize, elempack, opt.workspace_vkallocator);
450
sum_workspace.create(w, h, elemsize, elempack, opt.workspace_vkallocator);
452
else if (dims == 3 && positive_axis == 1)
454
max_workspace.create(w, channels, elemsize, elempack, opt.workspace_vkallocator);
455
sum_workspace.create(w, channels, elemsize, elempack, opt.workspace_vkallocator);
457
else if (dims == 3 && positive_axis == 2)
459
max_workspace.create(h, channels, elemsize, elempack, opt.workspace_vkallocator);
460
sum_workspace.create(h, channels, elemsize, elempack, opt.workspace_vkallocator);
465
std::vector<VkImageMat> bindings(2);
466
bindings[0] = bottom_top_blob;
467
bindings[1] = max_workspace;
469
std::vector<vk_constant_type> constants(10);
470
constants[0].i = bottom_top_blob.dims;
471
constants[1].i = bottom_top_blob.w;
472
constants[2].i = bottom_top_blob.h;
473
constants[3].i = bottom_top_blob.c;
475
constants[5].i = max_workspace.dims;
476
constants[6].i = max_workspace.w;
477
constants[7].i = max_workspace.h;
478
constants[8].i = max_workspace.c;
481
const Pipeline* pipeline = elempack == 8 ? pipeline_softmax_reduce_max_pack8
482
: elempack == 4 ? pipeline_softmax_reduce_max_pack4
483
: pipeline_softmax_reduce_max;
485
cmd.record_pipeline(pipeline, bindings, constants, max_workspace);
490
std::vector<VkImageMat> bindings(3);
491
bindings[0] = bottom_top_blob;
492
bindings[1] = bottom_top_blob;
493
bindings[2] = max_workspace;
495
std::vector<vk_constant_type> constants(10);
496
constants[0].i = bottom_top_blob.dims;
497
constants[1].i = bottom_top_blob.w;
498
constants[2].i = bottom_top_blob.h;
499
constants[3].i = bottom_top_blob.c;
501
constants[5].i = max_workspace.dims;
502
constants[6].i = max_workspace.w;
503
constants[7].i = max_workspace.h;
504
constants[8].i = max_workspace.c;
507
const Pipeline* pipeline = elempack == 8 ? pipeline_softmax_exp_sub_max_pack8
508
: elempack == 4 ? pipeline_softmax_exp_sub_max_pack4
509
: pipeline_softmax_exp_sub_max;
511
cmd.record_pipeline(pipeline, bindings, constants, bottom_top_blob);
516
std::vector<VkImageMat> bindings(2);
517
bindings[0] = bottom_top_blob;
518
bindings[1] = sum_workspace;
520
std::vector<vk_constant_type> constants(10);
521
constants[0].i = bottom_top_blob.dims;
522
constants[1].i = bottom_top_blob.w;
523
constants[2].i = bottom_top_blob.h;
524
constants[3].i = bottom_top_blob.c;
526
constants[5].i = sum_workspace.dims;
527
constants[6].i = sum_workspace.w;
528
constants[7].i = sum_workspace.h;
529
constants[8].i = sum_workspace.c;
532
const Pipeline* pipeline = elempack == 8 ? pipeline_softmax_reduce_sum_pack8
533
: elempack == 4 ? pipeline_softmax_reduce_sum_pack4
534
: pipeline_softmax_reduce_sum;
536
cmd.record_pipeline(pipeline, bindings, constants, sum_workspace);
541
std::vector<VkImageMat> bindings(3);
542
bindings[0] = bottom_top_blob;
543
bindings[1] = bottom_top_blob;
544
bindings[2] = sum_workspace;
546
std::vector<vk_constant_type> constants(10);
547
constants[0].i = bottom_top_blob.dims;
548
constants[1].i = bottom_top_blob.w;
549
constants[2].i = bottom_top_blob.h;
550
constants[3].i = bottom_top_blob.c;
552
constants[5].i = sum_workspace.dims;
553
constants[6].i = sum_workspace.w;
554
constants[7].i = sum_workspace.h;
555
constants[8].i = sum_workspace.c;
558
const Pipeline* pipeline = elempack == 8 ? pipeline_softmax_div_sum_pack8
559
: elempack == 4 ? pipeline_softmax_div_sum_pack4
560
: pipeline_softmax_div_sum;
562
cmd.record_pipeline(pipeline, bindings, constants, bottom_top_blob);