1
// Tencent is pleased to support the open source community by making ncnn available.
3
// Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
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
8
// https://opensource.org/licenses/BSD-3-Clause
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.
15
#include "binaryop_vulkan.h"
17
#include "layer_shader_type.h"
21
BinaryOp_vulkan::BinaryOp_vulkan()
23
support_vulkan = true;
24
support_image_storage = true;
26
pipeline_binaryop = 0;
27
pipeline_binaryop_pack4 = 0;
28
pipeline_binaryop_pack8 = 0;
30
pipeline_binaryop_broadcast[0] = 0;
31
pipeline_binaryop_broadcast[1] = 0;
32
pipeline_binaryop_broadcast_pack4[0] = 0;
33
pipeline_binaryop_broadcast_pack4[1] = 0;
34
pipeline_binaryop_broadcast_pack1to4[0] = 0;
35
pipeline_binaryop_broadcast_pack1to4[1] = 0;
36
pipeline_binaryop_broadcast_pack8[0] = 0;
37
pipeline_binaryop_broadcast_pack8[1] = 0;
38
pipeline_binaryop_broadcast_pack1to8[0] = 0;
39
pipeline_binaryop_broadcast_pack1to8[1] = 0;
42
static int get_reverse_op_type(int op_type)
44
if (op_type == BinaryOp::Operation_SUB) return BinaryOp::Operation_RSUB;
45
if (op_type == BinaryOp::Operation_DIV) return BinaryOp::Operation_RDIV;
46
if (op_type == BinaryOp::Operation_POW) return BinaryOp::Operation_RPOW;
47
if (op_type == BinaryOp::Operation_ATAN2) return BinaryOp::Operation_RATAN2;
48
if (op_type == BinaryOp::Operation_RSUB) return BinaryOp::Operation_SUB;
49
if (op_type == BinaryOp::Operation_RDIV) return BinaryOp::Operation_DIV;
50
if (op_type == BinaryOp::Operation_RPOW) return BinaryOp::Operation_POW;
51
if (op_type == BinaryOp::Operation_RATAN2) return BinaryOp::Operation_ATAN2;
55
int BinaryOp_vulkan::create_pipeline(const Option& opt)
57
const Mat& A_shape = bottom_shapes.empty() ? Mat() : bottom_shapes[0];
58
const Mat& B_shape = with_scalar ? A_shape : bottom_shapes.empty() ? Mat() : bottom_shapes[1];
59
const Mat& out_shape = top_shapes.empty() ? Mat() : top_shapes[0];
62
if (A_shape.dims == 1) A_elempack = opt.use_shader_pack8 && A_shape.w % 8 == 0 ? 8 : A_shape.w % 4 == 0 ? 4 : 1;
63
if (A_shape.dims == 2) A_elempack = opt.use_shader_pack8 && A_shape.h % 8 == 0 ? 8 : A_shape.h % 4 == 0 ? 4 : 1;
64
if (A_shape.dims == 3 || A_shape.dims == 4) A_elempack = opt.use_shader_pack8 && A_shape.c % 8 == 0 ? 8 : A_shape.c % 4 == 0 ? 4 : 1;
67
if (B_shape.dims == 1) B_elempack = opt.use_shader_pack8 && B_shape.w % 8 == 0 ? 8 : B_shape.w % 4 == 0 ? 4 : 1;
68
if (B_shape.dims == 2) B_elempack = opt.use_shader_pack8 && B_shape.h % 8 == 0 ? 8 : B_shape.h % 4 == 0 ? 4 : 1;
69
if (B_shape.dims == 3 || B_shape.dims == 4) B_elempack = opt.use_shader_pack8 && B_shape.c % 8 == 0 ? 8 : B_shape.c % 4 == 0 ? 4 : 1;
72
if (out_shape.dims == 1) out_elempack = opt.use_shader_pack8 && out_shape.w % 8 == 0 ? 8 : out_shape.w % 4 == 0 ? 4 : 1;
73
if (out_shape.dims == 2) out_elempack = opt.use_shader_pack8 && out_shape.h % 8 == 0 ? 8 : out_shape.h % 4 == 0 ? 4 : 1;
74
if (out_shape.dims == 3 || out_shape.dims == 4) out_elempack = opt.use_shader_pack8 && out_shape.c % 8 == 0 ? 8 : out_shape.c % 4 == 0 ? 4 : 1;
79
if (opt.use_fp16_storage)
81
A_elemsize = A_elempack * 2u;
82
B_elemsize = B_elempack * 2u;
83
out_elemsize = out_elempack * 2u;
85
else if (opt.use_fp16_packed)
87
A_elemsize = A_elempack == 1 ? 4u : A_elempack * 2u;
88
B_elemsize = B_elempack == 1 ? 4u : B_elempack * 2u;
89
out_elemsize = out_elempack == 1 ? 4u : out_elempack * 2u;
93
A_elemsize = A_elempack * 4u;
94
B_elemsize = B_elempack * 4u;
95
out_elemsize = out_elempack * 4u;
99
if (A_shape.dims == 1) A_shape_packed = Mat(A_shape.w / A_elempack, (void*)0, A_elemsize, A_elempack);
100
if (A_shape.dims == 2) A_shape_packed = Mat(A_shape.w, A_shape.h / A_elempack, (void*)0, A_elemsize, A_elempack);
101
if (A_shape.dims == 3) A_shape_packed = Mat(A_shape.w, A_shape.h, A_shape.c / A_elempack, (void*)0, A_elemsize, A_elempack);
102
if (A_shape.dims == 4) A_shape_packed = Mat(A_shape.w, A_shape.h, A_shape.d, A_shape.c / A_elempack, (void*)0, A_elemsize, A_elempack);
105
if (B_shape.dims == 1) B_shape_packed = Mat(B_shape.w / B_elempack, (void*)0, B_elemsize, B_elempack);
106
if (B_shape.dims == 2) B_shape_packed = Mat(B_shape.w, B_shape.h / B_elempack, (void*)0, B_elemsize, B_elempack);
107
if (B_shape.dims == 3) B_shape_packed = Mat(B_shape.w, B_shape.h, B_shape.c / B_elempack, (void*)0, B_elemsize, B_elempack);
108
if (B_shape.dims == 4) B_shape_packed = Mat(B_shape.w, B_shape.h, B_shape.d, B_shape.c / B_elempack, (void*)0, B_elemsize, B_elempack);
110
Mat out_shape_packed;
111
if (out_shape.dims == 1) out_shape_packed = Mat(out_shape.w / out_elempack, (void*)0, out_elemsize, out_elempack);
112
if (out_shape.dims == 2) out_shape_packed = Mat(out_shape.w, out_shape.h / out_elempack, (void*)0, out_elemsize, out_elempack);
113
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);
114
if (out_shape.dims == 4) out_shape_packed = Mat(out_shape.w, out_shape.h, out_shape.d, out_shape.c / out_elempack, (void*)0, out_elemsize, out_elempack);
116
bool broadcast = true;
117
if (A_shape.dims == B_shape.dims && A_shape.w == B_shape.w && A_shape.h == B_shape.h && A_shape.d == B_shape.d && A_shape.c == B_shape.c)
123
if (out_shape.dims == 0 || !broadcast)
125
std::vector<vk_specialization_type> specializations(3 + 15);
126
specializations[0].i = op_type;
127
specializations[1].i = with_scalar;
128
specializations[2].f = b;
129
specializations[3 + 0].i = A_shape_packed.dims;
130
specializations[3 + 1].i = A_shape_packed.w;
131
specializations[3 + 2].i = A_shape_packed.h * A_shape_packed.d;
132
specializations[3 + 3].i = A_shape_packed.c;
133
specializations[3 + 4].i = A_shape_packed.cstep;
134
specializations[3 + 5].i = B_shape_packed.dims;
135
specializations[3 + 6].i = B_shape_packed.w;
136
specializations[3 + 7].i = B_shape_packed.h * B_shape_packed.d;
137
specializations[3 + 8].i = B_shape_packed.c;
138
specializations[3 + 9].i = B_shape_packed.cstep;
139
specializations[3 + 10].i = out_shape_packed.dims;
140
specializations[3 + 11].i = out_shape_packed.w;
141
specializations[3 + 12].i = out_shape_packed.h * out_shape_packed.d;
142
specializations[3 + 13].i = out_shape_packed.c;
143
specializations[3 + 14].i = out_shape_packed.cstep;
146
if (out_shape_packed.dims == 1)
148
local_size_xyz.w = std::min(64, out_shape_packed.w);
149
local_size_xyz.h = 1;
150
local_size_xyz.c = 1;
152
if (out_shape_packed.dims == 2)
154
local_size_xyz.w = std::min(8, out_shape_packed.w);
155
local_size_xyz.h = std::min(8, out_shape_packed.h);
156
local_size_xyz.c = 1;
158
if (out_shape_packed.dims == 3)
160
local_size_xyz.w = std::min(4, out_shape_packed.w);
161
local_size_xyz.h = std::min(4, out_shape_packed.h);
162
local_size_xyz.c = std::min(4, out_shape_packed.c);
164
if (out_shape_packed.dims == 4)
166
local_size_xyz.w = std::min(4, out_shape_packed.w);
167
local_size_xyz.h = std::min(4, out_shape_packed.h * out_shape_packed.d);
168
local_size_xyz.c = std::min(4, out_shape_packed.c);
172
if (out_shape.dims == 0 || out_elempack == 1)
174
pipeline_binaryop = new Pipeline(vkdev);
175
pipeline_binaryop->set_optimal_local_size_xyz(local_size_xyz);
176
pipeline_binaryop->create(LayerShaderType::binaryop, opt, specializations);
180
if (out_shape.dims == 0 || out_elempack == 4)
182
pipeline_binaryop_pack4 = new Pipeline(vkdev);
183
pipeline_binaryop_pack4->set_optimal_local_size_xyz(local_size_xyz);
184
pipeline_binaryop_pack4->create(LayerShaderType::binaryop_pack4, opt, specializations);
188
if ((opt.use_shader_pack8 && out_shape.dims == 0) || out_elempack == 8)
190
pipeline_binaryop_pack8 = new Pipeline(vkdev);
191
pipeline_binaryop_pack8->set_optimal_local_size_xyz(local_size_xyz);
192
pipeline_binaryop_pack8->create(LayerShaderType::binaryop_pack8, opt, specializations);
197
if (out_shape.dims == 0 || broadcast)
199
if (A_shape.dims > 0 && B_shape.dims > 0)
201
const bool a_rank_is_lower = A_shape.dims < B_shape.dims;
202
const bool a_rank_is_equal = A_shape.dims == B_shape.dims;
203
const bool a_pack_is_lower = A_elempack < B_elempack;
204
const bool a_pack_is_equal = A_elempack == B_elempack;
205
const bool a_size_is_lower = A_shape.w * A_shape.h * A_shape.d * A_shape.c < B_shape.w * B_shape.h * B_shape.d * B_shape.c;
206
if (a_rank_is_lower || (a_rank_is_equal && a_pack_is_lower) || (a_pack_is_equal && a_size_is_lower))
209
std::swap(A_shape_packed, B_shape_packed);
212
if (B_shape_packed.dims == 1 && ((A_shape_packed.dims == 2 && B_shape_packed.w * B_shape_packed.elempack != A_shape_packed.h * A_shape_packed.elempack) || ((A_shape_packed.dims == 3 || A_shape_packed.dims == 4) && B_shape_packed.w * B_shape_packed.elempack != A_shape_packed.c * A_shape_packed.elempack)))
214
B_shape_packed.dims = out_shape.dims;
215
B_shape_packed.w = B_shape_packed.w * B_shape_packed.elempack;
216
B_shape_packed.elempack = 1;
220
const int op_type_r = get_reverse_op_type(op_type);
222
std::vector<vk_specialization_type> specializations(1 + 18);
223
specializations[0].i = op_type;
224
specializations[1 + 0].i = A_shape_packed.dims;
225
specializations[1 + 1].i = A_shape_packed.w;
226
specializations[1 + 2].i = A_shape_packed.h;
227
specializations[1 + 3].i = A_shape_packed.d;
228
specializations[1 + 4].i = A_shape_packed.c;
229
specializations[1 + 5].i = A_shape_packed.cstep;
230
specializations[1 + 6].i = B_shape_packed.dims;
231
specializations[1 + 7].i = B_shape_packed.w;
232
specializations[1 + 8].i = B_shape_packed.h;
233
specializations[1 + 9].i = B_shape_packed.d;
234
specializations[1 + 10].i = B_shape_packed.c;
235
specializations[1 + 11].i = B_shape_packed.cstep;
236
specializations[1 + 12].i = out_shape_packed.dims;
237
specializations[1 + 13].i = out_shape_packed.w;
238
specializations[1 + 14].i = out_shape_packed.h;
239
specializations[1 + 15].i = out_shape_packed.d;
240
specializations[1 + 16].i = out_shape_packed.c;
241
specializations[1 + 17].i = out_shape_packed.cstep;
243
std::vector<vk_specialization_type> specializations_r(1 + 18);
244
specializations_r[0].i = op_type_r;
245
specializations_r[1 + 0].i = A_shape_packed.dims;
246
specializations_r[1 + 1].i = A_shape_packed.w;
247
specializations_r[1 + 2].i = A_shape_packed.h;
248
specializations_r[1 + 3].i = A_shape_packed.d;
249
specializations_r[1 + 4].i = A_shape_packed.c;
250
specializations_r[1 + 5].i = A_shape_packed.cstep;
251
specializations_r[1 + 6].i = B_shape_packed.dims;
252
specializations_r[1 + 7].i = B_shape_packed.w;
253
specializations_r[1 + 8].i = B_shape_packed.h;
254
specializations_r[1 + 9].i = B_shape_packed.d;
255
specializations_r[1 + 10].i = B_shape_packed.c;
256
specializations_r[1 + 11].i = B_shape_packed.cstep;
257
specializations_r[1 + 12].i = out_shape_packed.dims;
258
specializations_r[1 + 13].i = out_shape_packed.w;
259
specializations_r[1 + 14].i = out_shape_packed.h;
260
specializations_r[1 + 15].i = out_shape_packed.d;
261
specializations_r[1 + 16].i = out_shape_packed.c;
262
specializations_r[1 + 17].i = out_shape_packed.cstep;
265
if (out_shape_packed.dims == 1)
267
local_size_xyz.w = std::min(64, out_shape_packed.w);
268
local_size_xyz.h = 1;
269
local_size_xyz.c = 1;
271
if (out_shape_packed.dims == 2)
273
local_size_xyz.w = std::min(8, out_shape_packed.w);
274
local_size_xyz.h = std::min(8, out_shape_packed.h);
275
local_size_xyz.c = 1;
277
if (out_shape_packed.dims == 3)
279
local_size_xyz.w = std::min(4, out_shape_packed.w);
280
local_size_xyz.h = std::min(4, out_shape_packed.h);
281
local_size_xyz.c = std::min(4, out_shape_packed.c);
283
if (out_shape_packed.dims == 4)
285
local_size_xyz.w = std::min(4, out_shape_packed.w);
286
local_size_xyz.h = std::min(4, out_shape_packed.h * out_shape_packed.d);
287
local_size_xyz.c = std::min(4, out_shape_packed.c);
291
if (out_shape.dims == 0 || (out_elempack == 1))
293
pipeline_binaryop_broadcast[0] = new Pipeline(vkdev);
294
pipeline_binaryop_broadcast[0]->set_optimal_local_size_xyz(local_size_xyz);
295
pipeline_binaryop_broadcast[0]->create(LayerShaderType::binaryop_broadcast, opt, specializations);
297
if (op_type_r != op_type)
300
pipeline_binaryop_broadcast[1] = new Pipeline(vkdev);
301
pipeline_binaryop_broadcast[1]->set_optimal_local_size_xyz(local_size_xyz);
302
pipeline_binaryop_broadcast[1]->create(LayerShaderType::binaryop_broadcast, opt, specializations_r);
307
if (out_shape.dims == 0 || (A_shape_packed.elempack == 4 && B_shape_packed.elempack == 4 && out_elempack == 4))
309
pipeline_binaryop_broadcast_pack4[0] = new Pipeline(vkdev);
310
pipeline_binaryop_broadcast_pack4[0]->set_optimal_local_size_xyz(local_size_xyz);
311
pipeline_binaryop_broadcast_pack4[0]->create(LayerShaderType::binaryop_broadcast_pack4, opt, specializations);
313
if (op_type_r != op_type)
316
pipeline_binaryop_broadcast_pack4[1] = new Pipeline(vkdev);
317
pipeline_binaryop_broadcast_pack4[1]->set_optimal_local_size_xyz(local_size_xyz);
318
pipeline_binaryop_broadcast_pack4[1]->create(LayerShaderType::binaryop_broadcast_pack4, opt, specializations_r);
323
if (out_shape.dims == 0 || ((A_shape_packed.elempack == 1 || B_shape_packed.elempack == 1) && out_elempack == 4))
325
pipeline_binaryop_broadcast_pack1to4[0] = new Pipeline(vkdev);
326
pipeline_binaryop_broadcast_pack1to4[0]->set_optimal_local_size_xyz(local_size_xyz);
327
pipeline_binaryop_broadcast_pack1to4[0]->create(LayerShaderType::binaryop_broadcast_pack1to4, opt, specializations);
329
if (op_type_r != op_type)
332
pipeline_binaryop_broadcast_pack1to4[1] = new Pipeline(vkdev);
333
pipeline_binaryop_broadcast_pack1to4[1]->set_optimal_local_size_xyz(local_size_xyz);
334
pipeline_binaryop_broadcast_pack1to4[1]->create(LayerShaderType::binaryop_broadcast_pack1to4, opt, specializations_r);
339
if ((opt.use_shader_pack8 && out_shape.dims == 0) || (A_shape_packed.elempack == 8 && B_shape_packed.elempack == 8 && out_elempack == 8))
341
pipeline_binaryop_broadcast_pack8[0] = new Pipeline(vkdev);
342
pipeline_binaryop_broadcast_pack8[0]->set_optimal_local_size_xyz(local_size_xyz);
343
pipeline_binaryop_broadcast_pack8[0]->create(LayerShaderType::binaryop_broadcast_pack8, opt, specializations);
345
if (op_type_r != op_type)
348
pipeline_binaryop_broadcast_pack8[1] = new Pipeline(vkdev);
349
pipeline_binaryop_broadcast_pack8[1]->set_optimal_local_size_xyz(local_size_xyz);
350
pipeline_binaryop_broadcast_pack8[1]->create(LayerShaderType::binaryop_broadcast_pack8, opt, specializations_r);
355
if ((opt.use_shader_pack8 && out_shape.dims == 0) || ((A_shape_packed.elempack == 1 || B_shape_packed.elempack == 1) && out_elempack == 8))
357
pipeline_binaryop_broadcast_pack1to8[0] = new Pipeline(vkdev);
358
pipeline_binaryop_broadcast_pack1to8[0]->set_optimal_local_size_xyz(local_size_xyz);
359
pipeline_binaryop_broadcast_pack1to8[0]->create(LayerShaderType::binaryop_broadcast_pack1to8, opt, specializations);
361
if (op_type_r != op_type)
364
pipeline_binaryop_broadcast_pack1to8[1] = new Pipeline(vkdev);
365
pipeline_binaryop_broadcast_pack1to8[1]->set_optimal_local_size_xyz(local_size_xyz);
366
pipeline_binaryop_broadcast_pack1to8[1]->create(LayerShaderType::binaryop_broadcast_pack1to8, opt, specializations_r);
374
int BinaryOp_vulkan::destroy_pipeline(const Option& /*opt*/)
376
delete pipeline_binaryop;
377
pipeline_binaryop = 0;
379
delete pipeline_binaryop_pack4;
380
pipeline_binaryop_pack4 = 0;
382
delete pipeline_binaryop_pack8;
383
pipeline_binaryop_pack8 = 0;
385
delete pipeline_binaryop_broadcast[0];
386
delete pipeline_binaryop_broadcast[1];
387
pipeline_binaryop_broadcast[0] = 0;
388
pipeline_binaryop_broadcast[1] = 0;
390
delete pipeline_binaryop_broadcast_pack4[0];
391
delete pipeline_binaryop_broadcast_pack4[1];
392
pipeline_binaryop_broadcast_pack4[0] = 0;
393
pipeline_binaryop_broadcast_pack4[1] = 0;
395
delete pipeline_binaryop_broadcast_pack1to4[0];
396
delete pipeline_binaryop_broadcast_pack1to4[1];
397
pipeline_binaryop_broadcast_pack1to4[0] = 0;
398
pipeline_binaryop_broadcast_pack1to4[1] = 0;
400
delete pipeline_binaryop_broadcast_pack1to8[0];
401
delete pipeline_binaryop_broadcast_pack1to8[1];
402
pipeline_binaryop_broadcast_pack1to8[0] = 0;
403
pipeline_binaryop_broadcast_pack1to8[1] = 0;
405
delete pipeline_binaryop_broadcast_pack1to8[0];
406
delete pipeline_binaryop_broadcast_pack1to8[1];
407
pipeline_binaryop_broadcast_pack1to8[0] = 0;
408
pipeline_binaryop_broadcast_pack1to8[1] = 0;
413
int BinaryOp_vulkan::forward(const std::vector<VkMat>& bottom_blobs, std::vector<VkMat>& top_blobs, VkCompute& cmd, const Option& opt) const
415
const VkMat& A = bottom_blobs[0];
416
const VkMat& B = bottom_blobs[1];
417
const int outdims = std::max(A.dims, B.dims);
419
const bool a_rank_is_lower = A.dims < B.dims;
420
const bool b_rank_is_lower = B.dims < A.dims;
421
const bool a_rank_is_equal = A.dims == B.dims;
423
VkMat& top_blob = top_blobs[0];
426
top_blob.create_like(B, opt.blob_vkallocator);
428
else if (b_rank_is_lower)
430
top_blob.create_like(A, opt.blob_vkallocator);
434
const int outw = std::max(A.w, B.w);
435
const int outh = std::max(A.h, B.h);
436
const int outd = std::max(A.d, B.d);
437
const int outc = std::max(A.c, B.c);
438
const int out_elempack = std::max(A.elempack, B.elempack);
439
const size_t out_elemsize = std::max(A.elemsize, B.elemsize);
443
top_blob.create(outw, out_elemsize, out_elempack, opt.blob_vkallocator);
447
top_blob.create(outw, outh, out_elemsize, out_elempack, opt.blob_vkallocator);
451
top_blob.create(outw, outh, outc, out_elemsize, out_elempack, opt.blob_vkallocator);
455
top_blob.create(outw, outh, outd, outc, out_elemsize, out_elempack, opt.blob_vkallocator);
458
if (top_blob.empty())
462
if (A.dims == B.dims && A.w == B.w && A.h == B.h && A.d == B.d && A.c == B.c && A.elempack == B.elempack)
464
std::vector<VkMat> bindings(3);
467
bindings[2] = top_blob;
469
std::vector<vk_constant_type> constants(15);
470
constants[0].i = A.dims;
471
constants[1].i = A.w;
472
constants[2].i = A.h * A.d;
473
constants[3].i = A.c;
474
constants[4].i = A.cstep;
475
constants[5].i = B.dims;
476
constants[6].i = B.w;
477
constants[7].i = B.h * B.d;
478
constants[8].i = B.c;
479
constants[9].i = B.cstep;
480
constants[10].i = top_blob.dims;
481
constants[11].i = top_blob.w;
482
constants[12].i = top_blob.h * top_blob.d;
483
constants[13].i = top_blob.c;
484
constants[14].i = top_blob.cstep;
486
const Pipeline* pipeline = top_blob.elempack == 8 ? pipeline_binaryop_pack8
487
: top_blob.elempack == 4 ? pipeline_binaryop_pack4
490
cmd.record_pipeline(pipeline, bindings, constants, top_blob);
495
const bool a_pack_is_lower = A.elempack < B.elempack;
496
const bool a_pack_is_equal = A.elempack == B.elempack;
497
const bool a_size_is_lower = A.w * A.h * A.d * A.c * A.elempack < B.w * B.h * B.d * B.c * B.elempack;
498
if (a_rank_is_lower || (a_rank_is_equal && a_pack_is_lower) || (a_pack_is_equal && a_size_is_lower))
501
if (A.dims == 1 && ((B.dims == 2 && A.w * A.elempack != B.h * B.elempack) || ((B.dims == 3 || B.dims == 4) && A.w * A.elempack != B.c * B.elempack)))
503
vkdev->convert_packing(A, A2, 1, cmd, opt);
504
A2.dims = top_blob.dims;
511
std::vector<VkMat> bindings(3);
514
bindings[2] = top_blob;
516
std::vector<vk_constant_type> constants(18);
517
constants[0].i = B.dims;
518
constants[1].i = B.w;
519
constants[2].i = B.h;
520
constants[3].i = B.d;
521
constants[4].i = B.c;
522
constants[5].i = B.cstep;
523
constants[6].i = A2.dims;
524
constants[7].i = A2.w;
525
constants[8].i = A2.h;
526
constants[9].i = A2.d;
527
constants[10].i = A2.c;
528
constants[11].i = A2.cstep;
529
constants[12].i = top_blob.dims;
530
constants[13].i = top_blob.w;
531
constants[14].i = top_blob.h;
532
constants[15].i = top_blob.d;
533
constants[16].i = top_blob.c;
534
constants[17].i = top_blob.cstep;
536
const int ri = get_reverse_op_type(op_type) == op_type ? 0 : 1;
538
const Pipeline* pipeline = 0;
539
if (A2.elempack == 1 && top_blob.elempack == 1)
541
pipeline = pipeline_binaryop_broadcast[ri];
543
if (A2.elempack == 4 && top_blob.elempack == 4)
545
pipeline = pipeline_binaryop_broadcast_pack4[ri];
547
if (A2.elempack == 1 && top_blob.elempack == 4)
549
pipeline = pipeline_binaryop_broadcast_pack1to4[ri];
551
if (A2.elempack == 8 && top_blob.elempack == 8)
553
pipeline = pipeline_binaryop_broadcast_pack8[ri];
555
if (A2.elempack == 1 && top_blob.elempack == 8)
557
pipeline = pipeline_binaryop_broadcast_pack1to8[ri];
560
cmd.record_pipeline(pipeline, bindings, constants, top_blob);
565
if (B.dims == 1 && ((A.dims == 2 && B.w * B.elempack != A.h * A.elempack) || ((A.dims == 3 || A.dims == 4) && B.w * B.elempack != A.c * A.elempack)))
567
vkdev->convert_packing(B, B2, 1, cmd, opt);
568
B2.dims = top_blob.dims;
575
std::vector<VkMat> bindings(3);
578
bindings[2] = top_blob;
580
std::vector<vk_constant_type> constants(18);
581
constants[0].i = A.dims;
582
constants[1].i = A.w;
583
constants[2].i = A.h;
584
constants[3].i = A.d;
585
constants[4].i = A.c;
586
constants[5].i = A.cstep;
587
constants[6].i = B2.dims;
588
constants[7].i = B2.w;
589
constants[8].i = B2.h;
590
constants[9].i = B2.d;
591
constants[10].i = B2.c;
592
constants[11].i = B2.cstep;
593
constants[12].i = top_blob.dims;
594
constants[13].i = top_blob.w;
595
constants[14].i = top_blob.h;
596
constants[15].i = top_blob.d;
597
constants[16].i = top_blob.c;
598
constants[17].i = top_blob.cstep;
600
const Pipeline* pipeline = 0;
601
if (B2.elempack == 1 && top_blob.elempack == 1)
603
pipeline = pipeline_binaryop_broadcast[0];
605
if (B2.elempack == 4 && top_blob.elempack == 4)
607
pipeline = pipeline_binaryop_broadcast_pack4[0];
609
if (B2.elempack == 1 && top_blob.elempack == 4)
611
pipeline = pipeline_binaryop_broadcast_pack1to4[0];
613
if (B2.elempack == 8 && top_blob.elempack == 8)
615
pipeline = pipeline_binaryop_broadcast_pack8[0];
617
if (B2.elempack == 1 && top_blob.elempack == 8)
619
pipeline = pipeline_binaryop_broadcast_pack1to8[0];
622
cmd.record_pipeline(pipeline, bindings, constants, top_blob);
628
int BinaryOp_vulkan::forward_inplace(VkMat& bottom_top_blob, VkCompute& cmd, const Option& /*opt*/) const
630
int elempack = bottom_top_blob.elempack;
632
std::vector<VkMat> bindings(3);
633
bindings[0] = bottom_top_blob;
634
bindings[1] = bottom_top_blob; // TODO use dummy buffer
635
bindings[2] = bottom_top_blob; // TODO use dummy buffer
637
std::vector<vk_constant_type> constants(15);
638
constants[10].i = bottom_top_blob.dims;
639
constants[11].i = bottom_top_blob.w;
640
constants[12].i = bottom_top_blob.h * bottom_top_blob.d;
641
constants[13].i = bottom_top_blob.c;
642
constants[14].i = bottom_top_blob.cstep;
644
const Pipeline* pipeline = elempack == 8 ? pipeline_binaryop_pack8
645
: elempack == 4 ? pipeline_binaryop_pack4
648
cmd.record_pipeline(pipeline, bindings, constants, bottom_top_blob);
653
int BinaryOp_vulkan::forward(const std::vector<VkImageMat>& bottom_blobs, std::vector<VkImageMat>& top_blobs, VkCompute& cmd, const Option& opt) const
655
const VkImageMat& A = bottom_blobs[0];
656
const VkImageMat& B = bottom_blobs[1];
657
const int outdims = std::max(A.dims, B.dims);
659
const bool a_rank_is_lower = A.dims < B.dims;
660
const bool b_rank_is_lower = B.dims < A.dims;
661
const bool a_rank_is_equal = A.dims == B.dims;
663
VkImageMat& top_blob = top_blobs[0];
666
top_blob.create_like(B, opt.blob_vkallocator);
668
else if (b_rank_is_lower)
670
top_blob.create_like(A, opt.blob_vkallocator);
674
const int outw = std::max(A.w, B.w);
675
const int outh = std::max(A.h, B.h);
676
const int outd = std::max(A.d, B.d);
677
const int outc = std::max(A.c, B.c);
678
const int out_elempack = std::max(A.elempack, B.elempack);
679
const size_t out_elemsize = std::max(A.elemsize, B.elemsize);
683
top_blob.create(outw, out_elemsize, out_elempack, opt.blob_vkallocator);
687
top_blob.create(outw, outh, out_elemsize, out_elempack, opt.blob_vkallocator);
691
top_blob.create(outw, outh, outc, out_elemsize, out_elempack, opt.blob_vkallocator);
695
top_blob.create(outw, outh, outd, outc, out_elemsize, out_elempack, opt.blob_vkallocator);
698
if (top_blob.empty())
702
if (A.dims == B.dims && A.w == B.w && A.h == B.h && A.d == B.d && A.c == B.c && A.elempack == B.elempack)
704
std::vector<VkImageMat> bindings(3);
707
bindings[2] = top_blob;
709
std::vector<vk_constant_type> constants(15);
710
constants[0].i = A.dims;
711
constants[1].i = A.w;
712
constants[2].i = A.h * A.d;
713
constants[3].i = A.c;
714
constants[4].i = 0; //A.cstep;
715
constants[5].i = B.dims;
716
constants[6].i = B.w;
717
constants[7].i = B.h * B.d;
718
constants[8].i = B.c;
719
constants[9].i = 0; //B.cstep;
720
constants[10].i = top_blob.dims;
721
constants[11].i = top_blob.w;
722
constants[12].i = top_blob.h * top_blob.d;
723
constants[13].i = top_blob.c;
724
constants[14].i = 0; //top_blob.cstep;
726
const Pipeline* pipeline = top_blob.elempack == 8 ? pipeline_binaryop_pack8
727
: top_blob.elempack == 4 ? pipeline_binaryop_pack4
730
cmd.record_pipeline(pipeline, bindings, constants, top_blob);
735
const bool a_pack_is_lower = A.elempack < B.elempack;
736
const bool a_pack_is_equal = A.elempack == B.elempack;
737
const bool a_size_is_lower = A.w * A.h * A.d * A.c * A.elempack < B.w * B.h * B.d * B.c * B.elempack;
738
if (a_rank_is_lower || (a_rank_is_equal && a_pack_is_lower) || (a_pack_is_equal && a_size_is_lower))
741
if (A.dims == 1 && ((B.dims == 2 && A.w * A.elempack != B.h * B.elempack) || ((B.dims == 3 || B.dims == 4) && A.w * A.elempack != B.c * B.elempack)))
743
vkdev->convert_packing(A, A2, 1, cmd, opt);
744
A2.dims = top_blob.dims;
751
std::vector<VkImageMat> bindings(3);
754
bindings[2] = top_blob;
756
std::vector<vk_constant_type> constants(18);
757
constants[0].i = B.dims;
758
constants[1].i = B.w;
759
constants[2].i = B.h;
760
constants[3].i = B.d;
761
constants[4].i = B.c;
762
constants[5].i = 0; //B.cstep;
763
constants[6].i = A2.dims;
764
constants[7].i = A2.w;
765
constants[8].i = A2.h;
766
constants[9].i = A2.d;
767
constants[10].i = A2.c;
768
constants[11].i = 0; //A2.cstep;
769
constants[12].i = top_blob.dims;
770
constants[13].i = top_blob.w;
771
constants[14].i = top_blob.h;
772
constants[15].i = top_blob.d;
773
constants[16].i = top_blob.c;
774
constants[17].i = 0; //top_blob.cstep;
776
const int ri = get_reverse_op_type(op_type) == op_type ? 0 : 1;
778
const Pipeline* pipeline = 0;
779
if (A2.elempack == 1 && top_blob.elempack == 1)
781
pipeline = pipeline_binaryop_broadcast[ri];
783
if (A2.elempack == 4 && top_blob.elempack == 4)
785
pipeline = pipeline_binaryop_broadcast_pack4[ri];
787
if (A2.elempack == 1 && top_blob.elempack == 4)
789
pipeline = pipeline_binaryop_broadcast_pack1to4[ri];
791
if (A2.elempack == 8 && top_blob.elempack == 8)
793
pipeline = pipeline_binaryop_broadcast_pack8[ri];
795
if (A2.elempack == 1 && top_blob.elempack == 8)
797
pipeline = pipeline_binaryop_broadcast_pack1to8[ri];
800
cmd.record_pipeline(pipeline, bindings, constants, top_blob);
805
if (B.dims == 1 && ((A.dims == 2 && B.w * B.elempack != A.h * A.elempack) || ((A.dims == 3 || A.dims == 4) && B.w * B.elempack != A.c * A.elempack)))
807
vkdev->convert_packing(B, B2, 1, cmd, opt);
808
B2.dims = top_blob.dims;
815
std::vector<VkImageMat> bindings(3);
818
bindings[2] = top_blob;
820
std::vector<vk_constant_type> constants(18);
821
constants[0].i = A.dims;
822
constants[1].i = A.w;
823
constants[2].i = A.h;
824
constants[3].i = A.d;
825
constants[4].i = A.c;
826
constants[5].i = 0; //A.cstep;
827
constants[6].i = B2.dims;
828
constants[7].i = B2.w;
829
constants[8].i = B2.h;
830
constants[9].i = B2.d;
831
constants[10].i = B2.c;
832
constants[11].i = 0; //B2.cstep;
833
constants[12].i = top_blob.dims;
834
constants[13].i = top_blob.w;
835
constants[14].i = top_blob.h;
836
constants[15].i = top_blob.d;
837
constants[16].i = top_blob.c;
838
constants[17].i = 0; //top_blob.cstep;
840
const Pipeline* pipeline = 0;
841
if (B2.elempack == 1 && top_blob.elempack == 1)
843
pipeline = pipeline_binaryop_broadcast[0];
845
if (B2.elempack == 4 && top_blob.elempack == 4)
847
pipeline = pipeline_binaryop_broadcast_pack4[0];
849
if (B2.elempack == 1 && top_blob.elempack == 4)
851
pipeline = pipeline_binaryop_broadcast_pack1to4[0];
853
if (B2.elempack == 8 && top_blob.elempack == 8)
855
pipeline = pipeline_binaryop_broadcast_pack8[0];
857
if (B2.elempack == 1 && top_blob.elempack == 8)
859
pipeline = pipeline_binaryop_broadcast_pack1to8[0];
862
cmd.record_pipeline(pipeline, bindings, constants, top_blob);
868
int BinaryOp_vulkan::forward_inplace(VkImageMat& bottom_top_blob, VkCompute& cmd, const Option& /*opt*/) const
870
int elempack = bottom_top_blob.elempack;
872
std::vector<VkImageMat> bindings(3);
873
bindings[0] = bottom_top_blob;
874
bindings[1] = bottom_top_blob; // TODO use dummy buffer
875
bindings[2] = bottom_top_blob;
877
std::vector<vk_constant_type> constants(15);
878
constants[10].i = bottom_top_blob.dims;
879
constants[11].i = bottom_top_blob.w;
880
constants[12].i = bottom_top_blob.h * bottom_top_blob.d;
881
constants[13].i = bottom_top_blob.c;
882
constants[14].i = 0; //bottom_top_blob.cstep;
884
const Pipeline* pipeline = elempack == 8 ? pipeline_binaryop_pack8
885
: elempack == 4 ? pipeline_binaryop_pack4
888
cmd.record_pipeline(pipeline, bindings, constants, bottom_top_blob);