Elffuss BraidHub

kikocis/ncnn public

master · 62.2 KiBRawPermalink
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 "concat_vulkan.h"
16
17#include "layer_shader_type.h"
18
19namespace ncnn {
20
21Concat_vulkan::Concat_vulkan()
22{
23 support_vulkan = true;
24 support_image_storage = true;
25
26 pipeline_concat[0] = 0;
27 pipeline_concat[1] = 0;
28 pipeline_concat_pack4[0] = 0;
29 pipeline_concat_pack4[1] = 0;
30 pipeline_concat_pack4to1[0] = 0;
31 pipeline_concat_pack4to1[1] = 0;
32 pipeline_concat_pack8[0] = 0;
33 pipeline_concat_pack8[1] = 0;
34 pipeline_concat_pack8to4[0] = 0;
35 pipeline_concat_pack8to4[1] = 0;
36 pipeline_concat_pack8to1[0] = 0;
37 pipeline_concat_pack8to1[1] = 0;
38}
39
40int Concat_vulkan::create_pipeline(const Option& _opt)
41{
42 Option opt = _opt;
43
44 const Mat& shape = bottom_shapes.empty() ? Mat() : bottom_shapes[0];
45 const Mat& out_shape = top_shapes.empty() ? Mat() : top_shapes[0];
46 int positive_axis = axis < 0 ? shape.dims + axis : axis;
47
48 int out_elempack = 1;
49 if (out_shape.dims == 1) out_elempack = opt.use_shader_pack8 && out_shape.w % 8 == 0 ? 8 : out_shape.w % 4 == 0 ? 4 : 1;
50 if (out_shape.dims == 2) out_elempack = opt.use_shader_pack8 && out_shape.h % 8 == 0 ? 8 : out_shape.h % 4 == 0 ? 4 : 1;
51 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;
52
53 int elempack = 1;
54 if (positive_axis == 0)
55 {
56 if (shape.dims == 1) elempack = opt.use_shader_pack8 && shape.w % 8 == 0 ? 8 : shape.w % 4 == 0 ? 4 : 1;
57 if (shape.dims == 2) elempack = opt.use_shader_pack8 && shape.h % 8 == 0 ? 8 : shape.h % 4 == 0 ? 4 : 1;
58 if (shape.dims == 3 || shape.dims == 4) elempack = opt.use_shader_pack8 && shape.c % 8 == 0 ? 8 : shape.c % 4 == 0 ? 4 : 1;
59
60 for (size_t b = 1; b < bottom_shapes.size(); b++)
61 {
62 const Mat& shape1 = bottom_shapes[b];
63
64 int elempack1 = 1;
65 if (shape1.dims == 1) elempack1 = opt.use_shader_pack8 && shape1.w % 8 == 0 ? 8 : shape1.w % 4 == 0 ? 4 : 1;
66 if (shape1.dims == 2) elempack1 = opt.use_shader_pack8 && shape1.h % 8 == 0 ? 8 : shape1.h % 4 == 0 ? 4 : 1;
67 if (shape1.dims == 3 || shape1.dims == 4) elempack1 = opt.use_shader_pack8 && shape1.c % 8 == 0 ? 8 : shape1.c % 4 == 0 ? 4 : 1;
68
69 elempack = std::min(elempack, elempack1);
70 }
71 }
72 else
73 {
74 elempack = out_elempack;
75 }
76
77 size_t elemsize;
78 if (opt.use_fp16_storage)
79 {
80 elemsize = elempack * 2u;
81 }
82 else if (opt.use_fp16_packed)
83 {
84 elemsize = elempack == 1 ? 4u : elempack * 2u;
85 }
86 else
87 {
88 elemsize = elempack * 4u;
89 }
90
91 Mat out_shape_unpacked;
92 if (out_shape.dims == 1) out_shape_unpacked = Mat(out_shape.w / elempack, (void*)0, elemsize, elempack);
93 if (out_shape.dims == 2) out_shape_unpacked = Mat(out_shape.w, out_shape.h / elempack, (void*)0, elemsize, elempack);
94 if (out_shape.dims == 3) out_shape_unpacked = Mat(out_shape.w, out_shape.h, out_shape.c / elempack, (void*)0, elemsize, elempack);
95 if (out_shape.dims == 4) out_shape_unpacked = Mat(out_shape.w, out_shape.h, out_shape.d, out_shape.c / elempack, (void*)0, elemsize, elempack);
96
97 if (!vkdev->shape_support_image_storage(out_shape_unpacked))
98 {
99 support_image_storage = false;
100 opt.use_image_storage = false;
101 }
102
103 std::vector<vk_specialization_type> specializations(1 + 12);
104 specializations[0].i = axis;
105 specializations[1 + 0].i = 0; // TODO handle shape_packed for concat2
106 specializations[1 + 1].i = 0;
107 specializations[1 + 2].i = 0;
108 specializations[1 + 3].i = 0;
109 specializations[1 + 4].i = 0;
110 specializations[1 + 5].i = 0;
111 specializations[1 + 6].i = out_shape_unpacked.dims;
112 specializations[1 + 7].i = out_shape_unpacked.w;
113 specializations[1 + 8].i = out_shape_unpacked.h;
114 specializations[1 + 9].i = out_shape_unpacked.d;
115 specializations[1 + 10].i = out_shape_unpacked.c;
116 specializations[1 + 11].i = out_shape_unpacked.cstep;
117
118 Mat local_size_xyz; // TODO more precise group size guessed from out_shape_unpacked
119 if (out_shape_unpacked.dims == 1)
120 {
121 local_size_xyz.w = 64;
122 local_size_xyz.h = 1;
123 local_size_xyz.c = 1;
124 }
125 if (out_shape_unpacked.dims == 2)
126 {
127 local_size_xyz.w = 8;
128 local_size_xyz.h = 8;
129 local_size_xyz.c = 1;
130 }
131 if (out_shape_unpacked.dims == 3)
132 {
133 local_size_xyz.w = 4;
134 local_size_xyz.h = 4;
135 local_size_xyz.c = 4;
136 }
137 if (out_shape_unpacked.dims == 4)
138 {
139 local_size_xyz.w = 4;
140 local_size_xyz.h = 4;
141 local_size_xyz.c = 4;
142 }
143
144 // pack1
145 if (shape.dims == 0 || elempack == 1)
146 {
147 pipeline_concat[0] = new Pipeline(vkdev);
148 pipeline_concat[0]->set_optimal_local_size_xyz(local_size_xyz);
149 pipeline_concat[0]->create(LayerShaderType::concat, opt, specializations);
150 pipeline_concat[1] = new Pipeline(vkdev);
151 pipeline_concat[1]->set_optimal_local_size_xyz(local_size_xyz);
152 pipeline_concat[1]->create(LayerShaderType::concat, opt, specializations);
153 }
154
155 // pack4
156 if (shape.dims == 0 || elempack == 4)
157 {
158 pipeline_concat_pack4[0] = new Pipeline(vkdev);
159 pipeline_concat_pack4[0]->set_optimal_local_size_xyz(local_size_xyz);
160 pipeline_concat_pack4[0]->create(LayerShaderType::concat_pack4, opt, specializations);
161 pipeline_concat_pack4[1] = new Pipeline(vkdev);
162 pipeline_concat_pack4[1]->set_optimal_local_size_xyz(local_size_xyz);
163 pipeline_concat_pack4[1]->create(LayerShaderType::concat_pack4, opt, specializations);
164 }
165
166 // pack4to1
167 if ((positive_axis <= 0 && shape.dims == 0) || elempack == 1)
168 {
169 pipeline_concat_pack4to1[0] = new Pipeline(vkdev);
170 pipeline_concat_pack4to1[0]->set_optimal_local_size_xyz(local_size_xyz);
171 pipeline_concat_pack4to1[0]->create(LayerShaderType::concat_pack4to1, opt, specializations);
172 pipeline_concat_pack4to1[1] = new Pipeline(vkdev);
173 pipeline_concat_pack4to1[1]->set_optimal_local_size_xyz(local_size_xyz);
174 pipeline_concat_pack4to1[1]->create(LayerShaderType::concat_pack4to1, opt, specializations);
175 }
176
177 // pack8
178 if (opt.use_shader_pack8 && (shape.dims == 0 || elempack == 8))
179 {
180 pipeline_concat_pack8[0] = new Pipeline(vkdev);
181 pipeline_concat_pack8[0]->set_optimal_local_size_xyz(local_size_xyz);
182 pipeline_concat_pack8[0]->create(LayerShaderType::concat_pack8, opt, specializations);
183 pipeline_concat_pack8[1] = new Pipeline(vkdev);
184 pipeline_concat_pack8[1]->set_optimal_local_size_xyz(local_size_xyz);
185 pipeline_concat_pack8[1]->create(LayerShaderType::concat_pack8, opt, specializations);
186 }
187
188 // pack8to4
189 if (opt.use_shader_pack8 && ((positive_axis <= 0 && shape.dims == 0) || elempack == 4))
190 {
191 pipeline_concat_pack8to4[0] = new Pipeline(vkdev);
192 pipeline_concat_pack8to4[0]->set_optimal_local_size_xyz(local_size_xyz);
193 pipeline_concat_pack8to4[0]->create(LayerShaderType::concat_pack8to4, opt, specializations);
194 pipeline_concat_pack8to4[1] = new Pipeline(vkdev);
195 pipeline_concat_pack8to4[1]->set_optimal_local_size_xyz(local_size_xyz);
196 pipeline_concat_pack8to4[1]->create(LayerShaderType::concat_pack8to4, opt, specializations);
197 }
198
199 // pack8to1
200 if (opt.use_shader_pack8 && ((positive_axis <= 0 && shape.dims == 0) || elempack == 1))
201 {
202 pipeline_concat_pack8to1[0] = new Pipeline(vkdev);
203 pipeline_concat_pack8to1[0]->set_optimal_local_size_xyz(local_size_xyz);
204 pipeline_concat_pack8to1[0]->create(LayerShaderType::concat_pack8to1, opt, specializations);
205 pipeline_concat_pack8to1[1] = new Pipeline(vkdev);
206 pipeline_concat_pack8to1[1]->set_optimal_local_size_xyz(local_size_xyz);
207 pipeline_concat_pack8to1[1]->create(LayerShaderType::concat_pack8to1, opt, specializations);
208 }
209
210 return 0;
211}
212
213int Concat_vulkan::destroy_pipeline(const Option& /*opt*/)
214{
215 delete pipeline_concat[0];
216 delete pipeline_concat[1];
217 pipeline_concat[0] = 0;
218 pipeline_concat[1] = 0;
219
220 delete pipeline_concat_pack4[0];
221 delete pipeline_concat_pack4[1];
222 pipeline_concat_pack4[0] = 0;
223 pipeline_concat_pack4[1] = 0;
224
225 delete pipeline_concat_pack4to1[0];
226 delete pipeline_concat_pack4to1[1];
227 pipeline_concat_pack4to1[0] = 0;
228 pipeline_concat_pack4to1[1] = 0;
229
230 delete pipeline_concat_pack8[0];
231 delete pipeline_concat_pack8[1];
232 pipeline_concat_pack8[0] = 0;
233 pipeline_concat_pack8[1] = 0;
234
235 delete pipeline_concat_pack8to4[0];
236 delete pipeline_concat_pack8to4[1];
237 pipeline_concat_pack8to4[0] = 0;
238 pipeline_concat_pack8to4[1] = 0;
239
240 delete pipeline_concat_pack8to1[0];
241 delete pipeline_concat_pack8to1[1];
242 pipeline_concat_pack8to1[0] = 0;
243 pipeline_concat_pack8to1[1] = 0;
244
245 return 0;
246}
247
248int Concat_vulkan::forward(const std::vector<VkMat>& bottom_blobs, std::vector<VkMat>& top_blobs, VkCompute& cmd, const Option& opt) const
249{
250 int dims = bottom_blobs[0].dims;
251 int positive_axis = axis < 0 ? dims + axis : axis;
252
253 if (dims == 1) // positive_axis == 0
254 {
255 // concat vector
256 // total length
257 size_t elemsize = bottom_blobs[0].elemsize;
258 int elempack = bottom_blobs[0].elempack;
259 int top_w = 0;
260 for (size_t b = 0; b < bottom_blobs.size(); b++)
261 {
262 const VkMat& bottom_blob = bottom_blobs[b];
263 elemsize = std::min(elemsize, bottom_blob.elemsize);
264 elempack = std::min(elempack, bottom_blob.elempack);
265 top_w += bottom_blob.w * bottom_blob.elempack;
266 }
267
268 int out_elempack = opt.use_shader_pack8 && top_w % 8 == 0 ? 8 : top_w % 4 == 0 ? 4 : 1;
269 size_t out_elemsize = elemsize / elempack * out_elempack;
270
271 if (opt.use_fp16_packed && !opt.use_fp16_storage)
272 {
273 if (out_elempack == 8) out_elemsize = 8 * 2u;
274 if (out_elempack == 4) out_elemsize = 4 * 2u;
275 if (out_elempack == 1) out_elemsize = 4u;
276 }
277
278 VkMat& top_blob = top_blobs[0];
279 top_blob.create(top_w / out_elempack, out_elemsize, out_elempack, opt.blob_vkallocator);
280 if (top_blob.empty())
281 return -100;
282
283 VkMat top_blob_unpacked = top_blob;
284 if (elempack < out_elempack)
285 {
286 top_blob_unpacked.create(top_w / elempack, elemsize, elempack, opt.workspace_vkallocator);
287 if (top_blob_unpacked.empty())
288 return -100;
289 }
290
291 int woffset = 0;
292 for (size_t b = 0; b < bottom_blobs.size(); b++)
293 {
294 const VkMat& bottom_blob = bottom_blobs[b];
295
296 std::vector<VkMat> bindings(2);
297 bindings[0] = bottom_blob;
298 bindings[1] = top_blob_unpacked;
299
300 std::vector<vk_constant_type> constants(13);
301 constants[0].i = bottom_blob.dims;
302 constants[1].i = bottom_blob.w;
303 constants[2].i = bottom_blob.h;
304 constants[3].i = bottom_blob.d;
305 constants[4].i = bottom_blob.c;
306 constants[5].i = bottom_blob.cstep;
307 constants[6].i = top_blob_unpacked.dims;
308 constants[7].i = top_blob_unpacked.w;
309 constants[8].i = top_blob_unpacked.h;
310 constants[9].i = top_blob_unpacked.d;
311 constants[10].i = top_blob_unpacked.c;
312 constants[11].i = top_blob_unpacked.cstep;
313 constants[12].i = woffset;
314
315 const Pipeline* pipeline = 0;
316 if (bottom_blob.elempack == 1 && elempack == 1)
317 {
318 pipeline = pipeline_concat[b % 2];
319 }
320 else if (bottom_blob.elempack == 4 && elempack == 4)
321 {
322 pipeline = pipeline_concat_pack4[b % 2];
323 }
324 else if (bottom_blob.elempack == 4 && elempack == 1)
325 {
326 pipeline = pipeline_concat_pack4to1[b % 2];
327 }
328 else if (bottom_blob.elempack == 8 && elempack == 8)
329 {
330 pipeline = pipeline_concat_pack8[b % 2];
331 }
332 else if (bottom_blob.elempack == 8 && elempack == 4)
333 {
334 pipeline = pipeline_concat_pack8to4[b % 2];
335 }
336 else if (bottom_blob.elempack == 8 && elempack == 1)
337 {
338 pipeline = pipeline_concat_pack8to1[b % 2];
339 }
340
341 cmd.record_pipeline(pipeline, bindings, constants, bottom_blob);
342
343 woffset += bottom_blob.w * bottom_blob.elempack / elempack;
344 }
345
346 // packing
347 if (elempack < out_elempack)
348 {
349 vkdev->convert_packing(top_blob_unpacked, top_blob, out_elempack, cmd, opt);
350 }
351
352 return 0;
353 }
354
355 if (dims == 2 && positive_axis == 0)
356 {
357 // concat image
358 int w = bottom_blobs[0].w;
359
360 // total height
361 size_t elemsize = bottom_blobs[0].elemsize;
362 int elempack = bottom_blobs[0].elempack;
363 int top_h = 0;
364 for (size_t b = 0; b < bottom_blobs.size(); b++)
365 {
366 const VkMat& bottom_blob = bottom_blobs[b];
367 elemsize = std::min(elemsize, bottom_blob.elemsize);
368 elempack = std::min(elempack, bottom_blob.elempack);
369 top_h += bottom_blob.h * bottom_blob.elempack;
370 }
371
372 int out_elempack = opt.use_shader_pack8 && top_h % 8 == 0 ? 8 : top_h % 4 == 0 ? 4 : 1;
373 size_t out_elemsize = elemsize / elempack * out_elempack;
374
375 if (opt.use_fp16_packed && !opt.use_fp16_storage)
376 {
377 if (out_elempack == 8) out_elemsize = 8 * 2u;
378 if (out_elempack == 4) out_elemsize = 4 * 2u;
379 if (out_elempack == 1) out_elemsize = 4u;
380 }
381
382 VkMat& top_blob = top_blobs[0];
383 top_blob.create(w, top_h / out_elempack, out_elemsize, out_elempack, opt.blob_vkallocator);
384 if (top_blob.empty())
385 return -100;
386
387 VkMat top_blob_unpacked = top_blob;
388 if (elempack < out_elempack)
389 {
390 top_blob_unpacked.create(w, top_h / elempack, elemsize, elempack, opt.workspace_vkallocator);
391 if (top_blob_unpacked.empty())
392 return -100;
393 }
394
395 int hoffset = 0;
396 for (size_t b = 0; b < bottom_blobs.size(); b++)
397 {
398 const VkMat& bottom_blob = bottom_blobs[b];
399
400 std::vector<VkMat> bindings(2);
401 bindings[0] = bottom_blob;
402 bindings[1] = top_blob_unpacked;
403
404 std::vector<vk_constant_type> constants(13);
405 constants[0].i = bottom_blob.dims;
406 constants[1].i = bottom_blob.w;
407 constants[2].i = bottom_blob.h;
408 constants[3].i = bottom_blob.d;
409 constants[4].i = bottom_blob.c;
410 constants[5].i = bottom_blob.cstep;
411 constants[6].i = top_blob_unpacked.dims;
412 constants[7].i = top_blob_unpacked.w;
413 constants[8].i = top_blob_unpacked.h;
414 constants[9].i = top_blob_unpacked.d;
415 constants[10].i = top_blob_unpacked.c;
416 constants[11].i = top_blob_unpacked.cstep;
417 constants[12].i = hoffset;
418
419 const Pipeline* pipeline = 0;
420 if (bottom_blob.elempack == 1 && elempack == 1)
421 {
422 pipeline = pipeline_concat[b % 2];
423 }
424 else if (bottom_blob.elempack == 4 && elempack == 4)
425 {
426 pipeline = pipeline_concat_pack4[b % 2];
427 }
428 else if (bottom_blob.elempack == 4 && elempack == 1)
429 {
430 pipeline = pipeline_concat_pack4to1[b % 2];
431 }
432 else if (bottom_blob.elempack == 8 && elempack == 8)
433 {
434 pipeline = pipeline_concat_pack8[b % 2];
435 }
436 else if (bottom_blob.elempack == 8 && elempack == 4)
437 {
438 pipeline = pipeline_concat_pack8to4[b % 2];
439 }
440 else if (bottom_blob.elempack == 8 && elempack == 1)
441 {
442 pipeline = pipeline_concat_pack8to1[b % 2];
443 }
444
445 cmd.record_pipeline(pipeline, bindings, constants, bottom_blob);
446
447 hoffset += bottom_blob.h * bottom_blob.elempack / elempack;
448 }
449
450 // packing
451 if (elempack < out_elempack)
452 {
453 vkdev->convert_packing(top_blob_unpacked, top_blob, out_elempack, cmd, opt);
454 }
455
456 return 0;
457 }
458
459 if (dims == 2 && positive_axis == 1)
460 {
461 // interleave image row
462 int h = bottom_blobs[0].h;
463 size_t elemsize = bottom_blobs[0].elemsize;
464 int elempack = bottom_blobs[0].elempack;
465
466 // total width
467 int top_w = 0;
468 for (size_t b = 0; b < bottom_blobs.size(); b++)
469 {
470 const VkMat& bottom_blob = bottom_blobs[b];
471 top_w += bottom_blob.w;
472 }
473
474 VkMat& top_blob = top_blobs[0];
475 top_blob.create(top_w, h, elemsize, elempack, opt.blob_vkallocator);
476 if (top_blob.empty())
477 return -100;
478
479 int woffset = 0;
480 for (size_t b = 0; b < bottom_blobs.size(); b++)
481 {
482 const VkMat& bottom_blob = bottom_blobs[b];
483
484 std::vector<VkMat> bindings(2);
485 bindings[0] = bottom_blob;
486 bindings[1] = top_blob;
487
488 std::vector<vk_constant_type> constants(13);
489 constants[0].i = bottom_blob.dims;
490 constants[1].i = bottom_blob.w;
491 constants[2].i = bottom_blob.h;
492 constants[3].i = bottom_blob.d;
493 constants[4].i = bottom_blob.c;
494 constants[5].i = bottom_blob.cstep;
495 constants[6].i = top_blob.dims;
496 constants[7].i = top_blob.w;
497 constants[8].i = top_blob.h;
498 constants[9].i = top_blob.d;
499 constants[10].i = top_blob.c;
500 constants[11].i = top_blob.cstep;
501 constants[12].i = woffset;
502
503 const Pipeline* pipeline = elempack == 8 ? pipeline_concat_pack8[b % 2]
504 : elempack == 4 ? pipeline_concat_pack4[b % 2]
505 : pipeline_concat[b % 2];
506
507 cmd.record_pipeline(pipeline, bindings, constants, bottom_blob);
508
509 woffset += bottom_blob.w;
510 }
511
512 return 0;
513 }
514
515 if (dims == 3 && positive_axis == 0)
516 {
517 // concat dim
518 int w = bottom_blobs[0].w;
519 int h = bottom_blobs[0].h;
520
521 // total channels
522 size_t elemsize = bottom_blobs[0].elemsize;
523 int elempack = bottom_blobs[0].elempack;
524 int top_channels = 0;
525 for (size_t b = 0; b < bottom_blobs.size(); b++)
526 {
527 const VkMat& bottom_blob = bottom_blobs[b];
528 elemsize = std::min(elemsize, bottom_blob.elemsize);
529 elempack = std::min(elempack, bottom_blob.elempack);
530 top_channels += bottom_blob.c * bottom_blob.elempack;
531 }
532
533 int out_elempack = opt.use_shader_pack8 && top_channels % 8 == 0 ? 8 : top_channels % 4 == 0 ? 4 : 1;
534 size_t out_elemsize = elemsize / elempack * out_elempack;
535
536 if (opt.use_fp16_packed && !opt.use_fp16_storage)
537 {
538 if (out_elempack == 8) out_elemsize = 8 * 2u;
539 if (out_elempack == 4) out_elemsize = 4 * 2u;
540 if (out_elempack == 1) out_elemsize = 4u;
541 }
542
543 VkMat& top_blob = top_blobs[0];
544 top_blob.create(w, h, top_channels / out_elempack, out_elemsize, out_elempack, opt.blob_vkallocator);
545 if (top_blob.empty())
546 return -100;
547
548 VkMat top_blob_unpacked = top_blob;
549 if (elempack < out_elempack)
550 {
551 top_blob_unpacked.create(w, h, top_channels / elempack, elemsize, elempack, opt.workspace_vkallocator);
552 if (top_blob_unpacked.empty())
553 return -100;
554 }
555
556 int coffset = 0;
557 for (size_t b = 0; b < bottom_blobs.size(); b++)
558 {
559 const VkMat& bottom_blob = bottom_blobs[b];
560
561 std::vector<VkMat> bindings(2);
562 bindings[0] = bottom_blob;
563 bindings[1] = top_blob_unpacked;
564
565 std::vector<vk_constant_type> constants(13);
566 constants[0].i = bottom_blob.dims;
567 constants[1].i = bottom_blob.w;
568 constants[2].i = bottom_blob.h;
569 constants[3].i = bottom_blob.d;
570 constants[4].i = bottom_blob.c;
571 constants[5].i = bottom_blob.cstep;
572 constants[6].i = top_blob_unpacked.dims;
573 constants[7].i = top_blob_unpacked.w;
574 constants[8].i = top_blob_unpacked.h;
575 constants[9].i = top_blob_unpacked.d;
576 constants[10].i = top_blob_unpacked.c;
577 constants[11].i = top_blob_unpacked.cstep;
578 constants[12].i = coffset;
579
580 const Pipeline* pipeline = 0;
581 if (bottom_blob.elempack == 1 && elempack == 1)
582 {
583 pipeline = pipeline_concat[b % 2];
584 }
585 else if (bottom_blob.elempack == 4 && elempack == 4)
586 {
587 pipeline = pipeline_concat_pack4[b % 2];
588 }
589 else if (bottom_blob.elempack == 4 && elempack == 1)
590 {
591 pipeline = pipeline_concat_pack4to1[b % 2];
592 }
593 else if (bottom_blob.elempack == 8 && elempack == 8)
594 {
595 pipeline = pipeline_concat_pack8[b % 2];
596 }
597 else if (bottom_blob.elempack == 8 && elempack == 4)
598 {
599 pipeline = pipeline_concat_pack8to4[b % 2];
600 }
601 else if (bottom_blob.elempack == 8 && elempack == 1)
602 {
603 pipeline = pipeline_concat_pack8to1[b % 2];
604 }
605
606 cmd.record_pipeline(pipeline, bindings, constants, bottom_blob);
607
608 coffset += bottom_blob.c * bottom_blob.elempack / elempack;
609 }
610
611 // packing
612 if (elempack < out_elempack)
613 {
614 vkdev->convert_packing(top_blob_unpacked, top_blob, out_elempack, cmd, opt);
615 }
616
617 return 0;
618 }
619
620 if (dims == 3 && positive_axis == 1)
621 {
622 // interleave dim height
623 int w = bottom_blobs[0].w;
624 int channels = bottom_blobs[0].c;
625 size_t elemsize = bottom_blobs[0].elemsize;
626 int elempack = bottom_blobs[0].elempack;
627
628 // total height
629 int top_h = 0;
630 for (size_t b = 0; b < bottom_blobs.size(); b++)
631 {
632 const VkMat& bottom_blob = bottom_blobs[b];
633 top_h += bottom_blob.h;
634 }
635
636 VkMat& top_blob = top_blobs[0];
637 top_blob.create(w, top_h, channels, elemsize, elempack, opt.blob_vkallocator);
638 if (top_blob.empty())
639 return -100;
640
641 int hoffset = 0;
642 for (size_t b = 0; b < bottom_blobs.size(); b++)
643 {
644 const VkMat& bottom_blob = bottom_blobs[b];
645
646 std::vector<VkMat> bindings(2);
647 bindings[0] = bottom_blob;
648 bindings[1] = top_blob;
649
650 std::vector<vk_constant_type> constants(13);
651 constants[0].i = bottom_blob.dims;
652 constants[1].i = bottom_blob.w;
653 constants[2].i = bottom_blob.h;
654 constants[3].i = bottom_blob.d;
655 constants[4].i = bottom_blob.c;
656 constants[5].i = bottom_blob.cstep;
657 constants[6].i = top_blob.dims;
658 constants[7].i = top_blob.w;
659 constants[8].i = top_blob.h;
660 constants[9].i = top_blob.d;
661 constants[10].i = top_blob.c;
662 constants[11].i = top_blob.cstep;
663 constants[12].i = hoffset;
664
665 const Pipeline* pipeline = elempack == 8 ? pipeline_concat_pack8[b % 2]
666 : elempack == 4 ? pipeline_concat_pack4[b % 2]
667 : pipeline_concat[b % 2];
668
669 cmd.record_pipeline(pipeline, bindings, constants, bottom_blob);
670
671 hoffset += bottom_blob.h;
672 }
673
674 return 0;
675 }
676
677 if (dims == 3 && positive_axis == 2)
678 {
679 // interleave dim width
680 int h = bottom_blobs[0].h;
681 int channels = bottom_blobs[0].c;
682 size_t elemsize = bottom_blobs[0].elemsize;
683 int elempack = bottom_blobs[0].elempack;
684
685 // total height
686 int top_w = 0;
687 for (size_t b = 0; b < bottom_blobs.size(); b++)
688 {
689 const VkMat& bottom_blob = bottom_blobs[b];
690 top_w += bottom_blob.w;
691 }
692
693 VkMat& top_blob = top_blobs[0];
694 top_blob.create(top_w, h, channels, elemsize, elempack, opt.blob_vkallocator);
695 if (top_blob.empty())
696 return -100;
697
698 int woffset = 0;
699 for (size_t b = 0; b < bottom_blobs.size(); b++)
700 {
701 const VkMat& bottom_blob = bottom_blobs[b];
702
703 std::vector<VkMat> bindings(2);
704 bindings[0] = bottom_blob;
705 bindings[1] = top_blob;
706
707 std::vector<vk_constant_type> constants(13);
708 constants[0].i = bottom_blob.dims;
709 constants[1].i = bottom_blob.w;
710 constants[2].i = bottom_blob.h;
711 constants[3].i = bottom_blob.d;
712 constants[4].i = bottom_blob.c;
713 constants[5].i = bottom_blob.cstep;
714 constants[6].i = top_blob.dims;
715 constants[7].i = top_blob.w;
716 constants[8].i = top_blob.h;
717 constants[9].i = top_blob.d;
718 constants[10].i = top_blob.c;
719 constants[11].i = top_blob.cstep;
720 constants[12].i = woffset;
721
722 const Pipeline* pipeline = elempack == 8 ? pipeline_concat_pack8[b % 2]
723 : elempack == 4 ? pipeline_concat_pack4[b % 2]
724 : pipeline_concat[b % 2];
725
726 cmd.record_pipeline(pipeline, bindings, constants, bottom_blob);
727
728 woffset += bottom_blob.w;
729 }
730
731 return 0;
732 }
733
734 if (dims == 4 && positive_axis == 0)
735 {
736 int w = bottom_blobs[0].w;
737 int h = bottom_blobs[0].h;
738 int d = bottom_blobs[0].d;
739
740 // total channels
741 size_t elemsize = bottom_blobs[0].elemsize;
742 int elempack = bottom_blobs[0].elempack;
743 int top_channels = 0;
744 for (size_t b = 0; b < bottom_blobs.size(); b++)
745 {
746 const VkMat& bottom_blob = bottom_blobs[b];
747 elemsize = std::min(elemsize, bottom_blob.elemsize);
748 elempack = std::min(elempack, bottom_blob.elempack);
749 top_channels += bottom_blob.c * bottom_blob.elempack;
750 }
751
752 int out_elempack = opt.use_shader_pack8 && top_channels % 8 == 0 ? 8 : top_channels % 4 == 0 ? 4 : 1;
753 size_t out_elemsize = elemsize / elempack * out_elempack;
754
755 if (opt.use_fp16_packed && !opt.use_fp16_storage)
756 {
757 if (out_elempack == 8) out_elemsize = 8 * 2u;
758 if (out_elempack == 4) out_elemsize = 4 * 2u;
759 if (out_elempack == 1) out_elemsize = 4u;
760 }
761
762 VkMat& top_blob = top_blobs[0];
763 top_blob.create(w, h, d, top_channels / out_elempack, out_elemsize, out_elempack, opt.blob_vkallocator);
764 if (top_blob.empty())
765 return -100;
766
767 VkMat top_blob_unpacked = top_blob;
768 if (elempack < out_elempack)
769 {
770 top_blob_unpacked.create(w, h, d, top_channels / elempack, elemsize, elempack, opt.workspace_vkallocator);
771 if (top_blob_unpacked.empty())
772 return -100;
773 }
774
775 int coffset = 0;
776 for (size_t b = 0; b < bottom_blobs.size(); b++)
777 {
778 const VkMat& bottom_blob = bottom_blobs[b];
779
780 std::vector<VkMat> bindings(2);
781 bindings[0] = bottom_blob;
782 bindings[1] = top_blob_unpacked;
783
784 std::vector<vk_constant_type> constants(13);
785 constants[0].i = bottom_blob.dims;
786 constants[1].i = bottom_blob.w;
787 constants[2].i = bottom_blob.h;
788 constants[3].i = bottom_blob.d;
789 constants[4].i = bottom_blob.c;
790 constants[5].i = bottom_blob.cstep;
791 constants[6].i = top_blob_unpacked.dims;
792 constants[7].i = top_blob_unpacked.w;
793 constants[8].i = top_blob_unpacked.h;
794 constants[9].i = top_blob_unpacked.d;
795 constants[10].i = top_blob_unpacked.c;
796 constants[11].i = top_blob_unpacked.cstep;
797 constants[12].i = coffset;
798
799 const Pipeline* pipeline = 0;
800 if (bottom_blob.elempack == 1 && elempack == 1)
801 {
802 pipeline = pipeline_concat[b % 2];
803 }
804 else if (bottom_blob.elempack == 4 && elempack == 4)
805 {
806 pipeline = pipeline_concat_pack4[b % 2];
807 }
808 else if (bottom_blob.elempack == 4 && elempack == 1)
809 {
810 pipeline = pipeline_concat_pack4to1[b % 2];
811 }
812 else if (bottom_blob.elempack == 8 && elempack == 8)
813 {
814 pipeline = pipeline_concat_pack8[b % 2];
815 }
816 else if (bottom_blob.elempack == 8 && elempack == 4)
817 {
818 pipeline = pipeline_concat_pack8to4[b % 2];
819 }
820 else if (bottom_blob.elempack == 8 && elempack == 1)
821 {
822 pipeline = pipeline_concat_pack8to1[b % 2];
823 }
824
825 cmd.record_pipeline(pipeline, bindings, constants, bottom_blob);
826
827 coffset += bottom_blob.c * bottom_blob.elempack / elempack;
828 }
829
830 // packing
831 if (elempack < out_elempack)
832 {
833 vkdev->convert_packing(top_blob_unpacked, top_blob, out_elempack, cmd, opt);
834 }
835
836 return 0;
837 }
838
839 if (dims == 4 && positive_axis == 1)
840 {
841 int w = bottom_blobs[0].w;
842 int h = bottom_blobs[0].h;
843 int channels = bottom_blobs[0].c;
844 size_t elemsize = bottom_blobs[0].elemsize;
845 int elempack = bottom_blobs[0].elempack;
846
847 // total depth
848 int top_d = 0;
849 for (size_t b = 0; b < bottom_blobs.size(); b++)
850 {
851 const VkMat& bottom_blob = bottom_blobs[b];
852 top_d += bottom_blob.d;
853 }
854
855 VkMat& top_blob = top_blobs[0];
856 top_blob.create(w, h, top_d, channels, elemsize, elempack, opt.blob_vkallocator);
857 if (top_blob.empty())
858 return -100;
859
860 int doffset = 0;
861 for (size_t b = 0; b < bottom_blobs.size(); b++)
862 {
863 const VkMat& bottom_blob = bottom_blobs[b];
864
865 std::vector<VkMat> bindings(2);
866 bindings[0] = bottom_blob;
867 bindings[1] = top_blob;
868
869 std::vector<vk_constant_type> constants(13);
870 constants[0].i = bottom_blob.dims;
871 constants[1].i = bottom_blob.w;
872 constants[2].i = bottom_blob.h;
873 constants[3].i = bottom_blob.d;
874 constants[4].i = bottom_blob.c;
875 constants[5].i = bottom_blob.cstep;
876 constants[6].i = top_blob.dims;
877 constants[7].i = top_blob.w;
878 constants[8].i = top_blob.h;
879 constants[9].i = top_blob.d;
880 constants[10].i = top_blob.c;
881 constants[11].i = top_blob.cstep;
882 constants[12].i = doffset;
883
884 const Pipeline* pipeline = elempack == 8 ? pipeline_concat_pack8[b % 2]
885 : elempack == 4 ? pipeline_concat_pack4[b % 2]
886 : pipeline_concat[b % 2];
887
888 cmd.record_pipeline(pipeline, bindings, constants, bottom_blob);
889
890 doffset += bottom_blob.d;
891 }
892
893 return 0;
894 }
895
896 if (dims == 4 && positive_axis == 2)
897 {
898 int w = bottom_blobs[0].w;
899 int d = bottom_blobs[0].d;
900 int channels = bottom_blobs[0].c;
901 size_t elemsize = bottom_blobs[0].elemsize;
902 int elempack = bottom_blobs[0].elempack;
903
904 // total height
905 int top_h = 0;
906 for (size_t b = 0; b < bottom_blobs.size(); b++)
907 {
908 const VkMat& bottom_blob = bottom_blobs[b];
909 top_h += bottom_blob.h;
910 }
911
912 VkMat& top_blob = top_blobs[0];
913 top_blob.create(w, top_h, d, channels, elemsize, elempack, opt.blob_vkallocator);
914 if (top_blob.empty())
915 return -100;
916
917 int hoffset = 0;
918 for (size_t b = 0; b < bottom_blobs.size(); b++)
919 {
920 const VkMat& bottom_blob = bottom_blobs[b];
921
922 std::vector<VkMat> bindings(2);
923 bindings[0] = bottom_blob;
924 bindings[1] = top_blob;
925
926 std::vector<vk_constant_type> constants(13);
927 constants[0].i = bottom_blob.dims;
928 constants[1].i = bottom_blob.w;
929 constants[2].i = bottom_blob.h;
930 constants[3].i = bottom_blob.d;
931 constants[4].i = bottom_blob.c;
932 constants[5].i = bottom_blob.cstep;
933 constants[6].i = top_blob.dims;
934 constants[7].i = top_blob.w;
935 constants[8].i = top_blob.h;
936 constants[9].i = top_blob.d;
937 constants[10].i = top_blob.c;
938 constants[11].i = top_blob.cstep;
939 constants[12].i = hoffset;
940
941 const Pipeline* pipeline = elempack == 8 ? pipeline_concat_pack8[b % 2]
942 : elempack == 4 ? pipeline_concat_pack4[b % 2]
943 : pipeline_concat[b % 2];
944
945 cmd.record_pipeline(pipeline, bindings, constants, bottom_blob);
946
947 hoffset += bottom_blob.h;
948 }
949
950 return 0;
951 }
952
953 if (dims == 4 && positive_axis == 3)
954 {
955 int h = bottom_blobs[0].h;
956 int d = bottom_blobs[0].d;
957 int channels = bottom_blobs[0].c;
958 size_t elemsize = bottom_blobs[0].elemsize;
959 int elempack = bottom_blobs[0].elempack;
960
961 // total height
962 int top_w = 0;
963 for (size_t b = 0; b < bottom_blobs.size(); b++)
964 {
965 const VkMat& bottom_blob = bottom_blobs[b];
966 top_w += bottom_blob.w;
967 }
968
969 VkMat& top_blob = top_blobs[0];
970 top_blob.create(top_w, h, d, channels, elemsize, elempack, opt.blob_vkallocator);
971 if (top_blob.empty())
972 return -100;
973
974 int woffset = 0;
975 for (size_t b = 0; b < bottom_blobs.size(); b++)
976 {
977 const VkMat& bottom_blob = bottom_blobs[b];
978
979 std::vector<VkMat> bindings(2);
980 bindings[0] = bottom_blob;
981 bindings[1] = top_blob;
982
983 std::vector<vk_constant_type> constants(13);
984 constants[0].i = bottom_blob.dims;
985 constants[1].i = bottom_blob.w;
986 constants[2].i = bottom_blob.h;
987 constants[3].i = bottom_blob.d;
988 constants[4].i = bottom_blob.c;
989 constants[5].i = bottom_blob.cstep;
990 constants[6].i = top_blob.dims;
991 constants[7].i = top_blob.w;
992 constants[8].i = top_blob.h;
993 constants[9].i = top_blob.d;
994 constants[10].i = top_blob.c;
995 constants[11].i = top_blob.cstep;
996 constants[12].i = woffset;
997
998 const Pipeline* pipeline = elempack == 8 ? pipeline_concat_pack8[b % 2]
999 : elempack == 4 ? pipeline_concat_pack4[b % 2]
1000 : pipeline_concat[b % 2];
1001
1002 cmd.record_pipeline(pipeline, bindings, constants, bottom_blob);
1003
1004 woffset += bottom_blob.w;
1005 }
1006
1007 return 0;
1008 }
1009
1010 return 0;
1011}
1012
1013int Concat_vulkan::forward(const std::vector<VkImageMat>& bottom_blobs, std::vector<VkImageMat>& top_blobs, VkCompute& cmd, const Option& opt) const
1014{
1015 int dims = bottom_blobs[0].dims;
1016 int positive_axis = axis < 0 ? dims + axis : axis;
1017
1018 if (dims == 1) // positive_axis == 0
1019 {
1020 // concat vector
1021 // total length
1022 size_t elemsize = bottom_blobs[0].elemsize;
1023 int elempack = bottom_blobs[0].elempack;
1024 int top_w = 0;
1025 for (size_t b = 0; b < bottom_blobs.size(); b++)
1026 {
1027 const VkImageMat& bottom_blob = bottom_blobs[b];
1028 elemsize = std::min(elemsize, bottom_blob.elemsize);
1029 elempack = std::min(elempack, bottom_blob.elempack);
1030 top_w += bottom_blob.w * bottom_blob.elempack;
1031 }
1032
1033 int out_elempack = opt.use_shader_pack8 && top_w % 8 == 0 ? 8 : top_w % 4 == 0 ? 4 : 1;
1034 size_t out_elemsize = elemsize / elempack * out_elempack;
1035
1036 if (opt.use_fp16_packed && !opt.use_fp16_storage)
1037 {
1038 if (out_elempack == 8) out_elemsize = 8 * 2u;
1039 if (out_elempack == 4) out_elemsize = 4 * 2u;
1040 if (out_elempack == 1) out_elemsize = 4u;
1041 }
1042
1043 VkImageMat& top_blob = top_blobs[0];
1044 top_blob.create(top_w / out_elempack, out_elemsize, out_elempack, opt.blob_vkallocator);
1045 if (top_blob.empty())
1046 return -100;
1047
1048 VkImageMat top_blob_unpacked = top_blob;
1049 if (elempack < out_elempack)
1050 {
1051 top_blob_unpacked.create(top_w / elempack, elemsize, elempack, opt.workspace_vkallocator);
1052 if (top_blob_unpacked.empty())
1053 return -100;
1054 }
1055
1056 int woffset = 0;
1057 for (size_t b = 0; b < bottom_blobs.size(); b++)
1058 {
1059 const VkImageMat& bottom_blob = bottom_blobs[b];
1060
1061 std::vector<VkImageMat> bindings(2);
1062 bindings[0] = bottom_blob;
1063 bindings[1] = top_blob_unpacked;
1064
1065 std::vector<vk_constant_type> constants(13);
1066 constants[0].i = bottom_blob.dims;
1067 constants[1].i = bottom_blob.w;
1068 constants[2].i = bottom_blob.h;
1069 constants[3].i = bottom_blob.d;
1070 constants[4].i = bottom_blob.c;
1071 constants[5].i = 0; //bottom_blob.cstep;
1072 constants[6].i = top_blob_unpacked.dims;
1073 constants[7].i = top_blob_unpacked.w;
1074 constants[8].i = top_blob_unpacked.h;
1075 constants[9].i = top_blob_unpacked.d;
1076 constants[10].i = top_blob_unpacked.c;
1077 constants[11].i = 0; //top_blob_unpacked.cstep;
1078 constants[12].i = woffset;
1079
1080 const Pipeline* pipeline = 0;
1081 if (bottom_blob.elempack == 1 && elempack == 1)
1082 {
1083 pipeline = pipeline_concat[b % 2];
1084 }
1085 else if (bottom_blob.elempack == 4 && elempack == 4)
1086 {
1087 pipeline = pipeline_concat_pack4[b % 2];
1088 }
1089 else if (bottom_blob.elempack == 4 && elempack == 1)
1090 {
1091 pipeline = pipeline_concat_pack4to1[b % 2];
1092 }
1093 else if (bottom_blob.elempack == 8 && elempack == 8)
1094 {
1095 pipeline = pipeline_concat_pack8[b % 2];
1096 }
1097 else if (bottom_blob.elempack == 8 && elempack == 4)
1098 {
1099 pipeline = pipeline_concat_pack8to4[b % 2];
1100 }
1101 else if (bottom_blob.elempack == 8 && elempack == 1)
1102 {
1103 pipeline = pipeline_concat_pack8to1[b % 2];
1104 }
1105
1106 cmd.record_pipeline(pipeline, bindings, constants, bottom_blob);
1107
1108 woffset += bottom_blob.w * bottom_blob.elempack / elempack;
1109 }
1110
1111 // packing
1112 if (elempack < out_elempack)
1113 {
1114 vkdev->convert_packing(top_blob_unpacked, top_blob, out_elempack, cmd, opt);
1115 }
1116
1117 return 0;
1118 }
1119
1120 if (dims == 2 && positive_axis == 0)
1121 {
1122 // concat image
1123 int w = bottom_blobs[0].w;
1124
1125 // total height
1126 size_t elemsize = bottom_blobs[0].elemsize;
1127 int elempack = bottom_blobs[0].elempack;
1128 int top_h = 0;
1129 for (size_t b = 0; b < bottom_blobs.size(); b++)
1130 {
1131 const VkImageMat& bottom_blob = bottom_blobs[b];
1132 elemsize = std::min(elemsize, bottom_blob.elemsize);
1133 elempack = std::min(elempack, bottom_blob.elempack);
1134 top_h += bottom_blob.h * bottom_blob.elempack;
1135 }
1136
1137 int out_elempack = opt.use_shader_pack8 && top_h % 8 == 0 ? 8 : top_h % 4 == 0 ? 4 : 1;
1138 size_t out_elemsize = elemsize / elempack * out_elempack;
1139
1140 if (opt.use_fp16_packed && !opt.use_fp16_storage)
1141 {
1142 if (out_elempack == 8) out_elemsize = 8 * 2u;
1143 if (out_elempack == 4) out_elemsize = 4 * 2u;
1144 if (out_elempack == 1) out_elemsize = 4u;
1145 }
1146
1147 VkImageMat& top_blob = top_blobs[0];
1148 top_blob.create(w, top_h / out_elempack, out_elemsize, out_elempack, opt.blob_vkallocator);
1149 if (top_blob.empty())
1150 return -100;
1151
1152 VkImageMat top_blob_unpacked = top_blob;
1153 if (elempack < out_elempack)
1154 {
1155 top_blob_unpacked.create(w, top_h / elempack, elemsize, elempack, opt.workspace_vkallocator);
1156 if (top_blob_unpacked.empty())
1157 return -100;
1158 }
1159
1160 int hoffset = 0;
1161 for (size_t b = 0; b < bottom_blobs.size(); b++)
1162 {
1163 const VkImageMat& bottom_blob = bottom_blobs[b];
1164
1165 std::vector<VkImageMat> bindings(2);
1166 bindings[0] = bottom_blob;
1167 bindings[1] = top_blob_unpacked;
1168
1169 std::vector<vk_constant_type> constants(13);
1170 constants[0].i = bottom_blob.dims;
1171 constants[1].i = bottom_blob.w;
1172 constants[2].i = bottom_blob.h;
1173 constants[3].i = bottom_blob.d;
1174 constants[4].i = bottom_blob.c;
1175 constants[5].i = 0; //bottom_blob.cstep;
1176 constants[6].i = top_blob_unpacked.dims;
1177 constants[7].i = top_blob_unpacked.w;
1178 constants[8].i = top_blob_unpacked.h;
1179 constants[9].i = top_blob_unpacked.d;
1180 constants[10].i = top_blob_unpacked.c;
1181 constants[11].i = 0; //top_blob_unpacked.cstep;
1182 constants[12].i = hoffset;
1183
1184 const Pipeline* pipeline = 0;
1185 if (bottom_blob.elempack == 1 && elempack == 1)
1186 {
1187 pipeline = pipeline_concat[b % 2];
1188 }
1189 else if (bottom_blob.elempack == 4 && elempack == 4)
1190 {
1191 pipeline = pipeline_concat_pack4[b % 2];
1192 }
1193 else if (bottom_blob.elempack == 4 && elempack == 1)
1194 {
1195 pipeline = pipeline_concat_pack4to1[b % 2];
1196 }
1197 else if (bottom_blob.elempack == 8 && elempack == 8)
1198 {
1199 pipeline = pipeline_concat_pack8[b % 2];
1200 }
1201 else if (bottom_blob.elempack == 8 && elempack == 4)
1202 {
1203 pipeline = pipeline_concat_pack8to4[b % 2];
1204 }
1205 else if (bottom_blob.elempack == 8 && elempack == 1)
1206 {
1207 pipeline = pipeline_concat_pack8to1[b % 2];
1208 }
1209
1210 cmd.record_pipeline(pipeline, bindings, constants, bottom_blob);
1211
1212 hoffset += bottom_blob.h * bottom_blob.elempack / elempack;
1213 }
1214
1215 // packing
1216 if (elempack < out_elempack)
1217 {
1218 vkdev->convert_packing(top_blob_unpacked, top_blob, out_elempack, cmd, opt);
1219 }
1220
1221 return 0;
1222 }
1223
1224 if (dims == 2 && positive_axis == 1)
1225 {
1226 // interleave image row
1227 int h = bottom_blobs[0].h;
1228 size_t elemsize = bottom_blobs[0].elemsize;
1229 int elempack = bottom_blobs[0].elempack;
1230
1231 // total width
1232 int top_w = 0;
1233 for (size_t b = 0; b < bottom_blobs.size(); b++)
1234 {
1235 const VkImageMat& bottom_blob = bottom_blobs[b];
1236 top_w += bottom_blob.w;
1237 }
1238
1239 VkImageMat& top_blob = top_blobs[0];
1240 top_blob.create(top_w, h, elemsize, elempack, opt.blob_vkallocator);
1241 if (top_blob.empty())
1242 return -100;
1243
1244 int woffset = 0;
1245 for (size_t b = 0; b < bottom_blobs.size(); b++)
1246 {
1247 const VkImageMat& bottom_blob = bottom_blobs[b];
1248
1249 std::vector<VkImageMat> bindings(2);
1250 bindings[0] = bottom_blob;
1251 bindings[1] = top_blob;
1252
1253 std::vector<vk_constant_type> constants(13);
1254 constants[0].i = bottom_blob.dims;
1255 constants[1].i = bottom_blob.w;
1256 constants[2].i = bottom_blob.h;
1257 constants[3].i = bottom_blob.d;
1258 constants[4].i = bottom_blob.c;
1259 constants[5].i = 0; //bottom_blob.cstep;
1260 constants[6].i = top_blob.dims;
1261 constants[7].i = top_blob.w;
1262 constants[8].i = top_blob.h;
1263 constants[9].i = top_blob.d;
1264 constants[10].i = top_blob.c;
1265 constants[11].i = 0; //top_blob.cstep;
1266 constants[12].i = woffset;
1267
1268 const Pipeline* pipeline = elempack == 8 ? pipeline_concat_pack8[b % 2]
1269 : elempack == 4 ? pipeline_concat_pack4[b % 2]
1270 : pipeline_concat[b % 2];
1271
1272 cmd.record_pipeline(pipeline, bindings, constants, bottom_blob);
1273
1274 woffset += bottom_blob.w;
1275 }
1276
1277 return 0;
1278 }
1279
1280 if (dims == 3 && positive_axis == 0)
1281 {
1282 // concat dim
1283 int w = bottom_blobs[0].w;
1284 int h = bottom_blobs[0].h;
1285
1286 // total channels
1287 size_t elemsize = bottom_blobs[0].elemsize;
1288 int elempack = bottom_blobs[0].elempack;
1289 int top_channels = 0;
1290 for (size_t b = 0; b < bottom_blobs.size(); b++)
1291 {
1292 const VkImageMat& bottom_blob = bottom_blobs[b];
1293 elemsize = std::min(elemsize, bottom_blob.elemsize);
1294 elempack = std::min(elempack, bottom_blob.elempack);
1295 top_channels += bottom_blob.c * bottom_blob.elempack;
1296 }
1297
1298 int out_elempack = opt.use_shader_pack8 && top_channels % 8 == 0 ? 8 : top_channels % 4 == 0 ? 4 : 1;
1299 size_t out_elemsize = elemsize / elempack * out_elempack;
1300
1301 if (opt.use_fp16_packed && !opt.use_fp16_storage)
1302 {
1303 if (out_elempack == 8) out_elemsize = 8 * 2u;
1304 if (out_elempack == 4) out_elemsize = 4 * 2u;
1305 if (out_elempack == 1) out_elemsize = 4u;
1306 }
1307
1308 VkImageMat& top_blob = top_blobs[0];
1309 top_blob.create(w, h, top_channels / out_elempack, out_elemsize, out_elempack, opt.blob_vkallocator);
1310 if (top_blob.empty())
1311 return -100;
1312
1313 VkImageMat top_blob_unpacked = top_blob;
1314 if (elempack < out_elempack)
1315 {
1316 top_blob_unpacked.create(w, h, top_channels / elempack, elemsize, elempack, opt.workspace_vkallocator);
1317 if (top_blob_unpacked.empty())
1318 return -100;
1319 }
1320
1321 int coffset = 0;
1322 for (size_t b = 0; b < bottom_blobs.size(); b++)
1323 {
1324 const VkImageMat& bottom_blob = bottom_blobs[b];
1325
1326 std::vector<VkImageMat> bindings(2);
1327 bindings[0] = bottom_blob;
1328 bindings[1] = top_blob_unpacked;
1329
1330 std::vector<vk_constant_type> constants(13);
1331 constants[0].i = bottom_blob.dims;
1332 constants[1].i = bottom_blob.w;
1333 constants[2].i = bottom_blob.h;
1334 constants[3].i = bottom_blob.d;
1335 constants[4].i = bottom_blob.c;
1336 constants[5].i = 0; //bottom_blob.cstep;
1337 constants[6].i = top_blob_unpacked.dims;
1338 constants[7].i = top_blob_unpacked.w;
1339 constants[8].i = top_blob_unpacked.h;
1340 constants[9].i = top_blob_unpacked.d;
1341 constants[10].i = top_blob_unpacked.c;
1342 constants[11].i = 0; //top_blob_unpacked.cstep;
1343 constants[12].i = coffset;
1344
1345 const Pipeline* pipeline = 0;
1346 if (bottom_blob.elempack == 1 && elempack == 1)
1347 {
1348 pipeline = pipeline_concat[b % 2];
1349 }
1350 else if (bottom_blob.elempack == 4 && elempack == 4)
1351 {
1352 pipeline = pipeline_concat_pack4[b % 2];
1353 }
1354 else if (bottom_blob.elempack == 4 && elempack == 1)
1355 {
1356 pipeline = pipeline_concat_pack4to1[b % 2];
1357 }
1358 else if (bottom_blob.elempack == 8 && elempack == 8)
1359 {
1360 pipeline = pipeline_concat_pack8[b % 2];
1361 }
1362 else if (bottom_blob.elempack == 8 && elempack == 4)
1363 {
1364 pipeline = pipeline_concat_pack8to4[b % 2];
1365 }
1366 else if (bottom_blob.elempack == 8 && elempack == 1)
1367 {
1368 pipeline = pipeline_concat_pack8to1[b % 2];
1369 }
1370
1371 cmd.record_pipeline(pipeline, bindings, constants, bottom_blob);
1372
1373 coffset += bottom_blob.c * bottom_blob.elempack / elempack;
1374 }
1375
1376 // packing
1377 if (elempack < out_elempack)
1378 {
1379 vkdev->convert_packing(top_blob_unpacked, top_blob, out_elempack, cmd, opt);
1380 }
1381
1382 return 0;
1383 }
1384
1385 if (dims == 3 && positive_axis == 1)
1386 {
1387 // interleave dim height
1388 int w = bottom_blobs[0].w;
1389 int channels = bottom_blobs[0].c;
1390 size_t elemsize = bottom_blobs[0].elemsize;
1391 int elempack = bottom_blobs[0].elempack;
1392
1393 // total height
1394 int top_h = 0;
1395 for (size_t b = 0; b < bottom_blobs.size(); b++)
1396 {
1397 const VkImageMat& bottom_blob = bottom_blobs[b];
1398 top_h += bottom_blob.h;
1399 }
1400
1401 VkImageMat& top_blob = top_blobs[0];
1402 top_blob.create(w, top_h, channels, elemsize, elempack, opt.blob_vkallocator);
1403 if (top_blob.empty())
1404 return -100;
1405
1406 int hoffset = 0;
1407 for (size_t b = 0; b < bottom_blobs.size(); b++)
1408 {
1409 const VkImageMat& bottom_blob = bottom_blobs[b];
1410
1411 std::vector<VkImageMat> bindings(2);
1412 bindings[0] = bottom_blob;
1413 bindings[1] = top_blob;
1414
1415 std::vector<vk_constant_type> constants(13);
1416 constants[0].i = bottom_blob.dims;
1417 constants[1].i = bottom_blob.w;
1418 constants[2].i = bottom_blob.h;
1419 constants[3].i = bottom_blob.d;
1420 constants[4].i = bottom_blob.c;
1421 constants[5].i = 0; //bottom_blob.cstep;
1422 constants[6].i = top_blob.dims;
1423 constants[7].i = top_blob.w;
1424 constants[8].i = top_blob.h;
1425 constants[9].i = top_blob.d;
1426 constants[10].i = top_blob.c;
1427 constants[11].i = 0; //top_blob.cstep;
1428 constants[12].i = hoffset;
1429
1430 const Pipeline* pipeline = elempack == 8 ? pipeline_concat_pack8[b % 2]
1431 : elempack == 4 ? pipeline_concat_pack4[b % 2]
1432 : pipeline_concat[b % 2];
1433
1434 cmd.record_pipeline(pipeline, bindings, constants, bottom_blob);
1435
1436 hoffset += bottom_blob.h;
1437 }
1438
1439 return 0;
1440 }
1441
1442 if (dims == 3 && positive_axis == 2)
1443 {
1444 // interleave dim width
1445 int h = bottom_blobs[0].h;
1446 int channels = bottom_blobs[0].c;
1447 size_t elemsize = bottom_blobs[0].elemsize;
1448 int elempack = bottom_blobs[0].elempack;
1449
1450 // total height
1451 int top_w = 0;
1452 for (size_t b = 0; b < bottom_blobs.size(); b++)
1453 {
1454 const VkImageMat& bottom_blob = bottom_blobs[b];
1455 top_w += bottom_blob.w;
1456 }
1457
1458 VkImageMat& top_blob = top_blobs[0];
1459 top_blob.create(top_w, h, channels, elemsize, elempack, opt.blob_vkallocator);
1460 if (top_blob.empty())
1461 return -100;
1462
1463 int woffset = 0;
1464 for (size_t b = 0; b < bottom_blobs.size(); b++)
1465 {
1466 const VkImageMat& bottom_blob = bottom_blobs[b];
1467
1468 std::vector<VkImageMat> bindings(2);
1469 bindings[0] = bottom_blob;
1470 bindings[1] = top_blob;
1471
1472 std::vector<vk_constant_type> constants(13);
1473 constants[0].i = bottom_blob.dims;
1474 constants[1].i = bottom_blob.w;
1475 constants[2].i = bottom_blob.h;
1476 constants[3].i = bottom_blob.d;
1477 constants[4].i = bottom_blob.c;
1478 constants[5].i = 0; //bottom_blob.cstep;
1479 constants[6].i = top_blob.dims;
1480 constants[7].i = top_blob.w;
1481 constants[8].i = top_blob.h;
1482 constants[9].i = top_blob.d;
1483 constants[10].i = top_blob.c;
1484 constants[11].i = 0; //top_blob.cstep;
1485 constants[12].i = woffset;
1486
1487 const Pipeline* pipeline = elempack == 8 ? pipeline_concat_pack8[b % 2]
1488 : elempack == 4 ? pipeline_concat_pack4[b % 2]
1489 : pipeline_concat[b % 2];
1490
1491 cmd.record_pipeline(pipeline, bindings, constants, bottom_blob);
1492
1493 woffset += bottom_blob.w;
1494 }
1495
1496 return 0;
1497 }
1498
1499 if (dims == 4 && positive_axis == 0)
1500 {
1501 int w = bottom_blobs[0].w;
1502 int h = bottom_blobs[0].h;
1503 int d = bottom_blobs[0].d;
1504
1505 // total channels
1506 size_t elemsize = bottom_blobs[0].elemsize;
1507 int elempack = bottom_blobs[0].elempack;
1508 int top_channels = 0;
1509 for (size_t b = 0; b < bottom_blobs.size(); b++)
1510 {
1511 const VkImageMat& bottom_blob = bottom_blobs[b];
1512 elemsize = std::min(elemsize, bottom_blob.elemsize);
1513 elempack = std::min(elempack, bottom_blob.elempack);
1514 top_channels += bottom_blob.c * bottom_blob.elempack;
1515 }
1516
1517 int out_elempack = opt.use_shader_pack8 && top_channels % 8 == 0 ? 8 : top_channels % 4 == 0 ? 4 : 1;
1518 size_t out_elemsize = elemsize / elempack * out_elempack;
1519
1520 if (opt.use_fp16_packed && !opt.use_fp16_storage)
1521 {
1522 if (out_elempack == 8) out_elemsize = 8 * 2u;
1523 if (out_elempack == 4) out_elemsize = 4 * 2u;
1524 if (out_elempack == 1) out_elemsize = 4u;
1525 }
1526
1527 VkImageMat& top_blob = top_blobs[0];
1528 top_blob.create(w, h, d, top_channels / out_elempack, out_elemsize, out_elempack, opt.blob_vkallocator);
1529 if (top_blob.empty())
1530 return -100;
1531
1532 VkImageMat top_blob_unpacked = top_blob;
1533 if (elempack < out_elempack)
1534 {
1535 top_blob_unpacked.create(w, h, d, top_channels / elempack, elemsize, elempack, opt.workspace_vkallocator);
1536 if (top_blob_unpacked.empty())
1537 return -100;
1538 }
1539
1540 int coffset = 0;
1541 for (size_t b = 0; b < bottom_blobs.size(); b++)
1542 {
1543 const VkImageMat& bottom_blob = bottom_blobs[b];
1544
1545 std::vector<VkImageMat> bindings(2);
1546 bindings[0] = bottom_blob;
1547 bindings[1] = top_blob_unpacked;
1548
1549 std::vector<vk_constant_type> constants(13);
1550 constants[0].i = bottom_blob.dims;
1551 constants[1].i = bottom_blob.w;
1552 constants[2].i = bottom_blob.h;
1553 constants[3].i = bottom_blob.d;
1554 constants[4].i = bottom_blob.c;
1555 constants[5].i = 0; //bottom_blob.cstep;
1556 constants[6].i = top_blob_unpacked.dims;
1557 constants[7].i = top_blob_unpacked.w;
1558 constants[8].i = top_blob_unpacked.h;
1559 constants[9].i = top_blob_unpacked.d;
1560 constants[10].i = top_blob_unpacked.c;
1561 constants[11].i = 0; //top_blob_unpacked.cstep;
1562 constants[12].i = coffset;
1563
1564 const Pipeline* pipeline = 0;
1565 if (bottom_blob.elempack == 1 && elempack == 1)
1566 {
1567 pipeline = pipeline_concat[b % 2];
1568 }
1569 else if (bottom_blob.elempack == 4 && elempack == 4)
1570 {
1571 pipeline = pipeline_concat_pack4[b % 2];
1572 }
1573 else if (bottom_blob.elempack == 4 && elempack == 1)
1574 {
1575 pipeline = pipeline_concat_pack4to1[b % 2];
1576 }
1577 else if (bottom_blob.elempack == 8 && elempack == 8)
1578 {
1579 pipeline = pipeline_concat_pack8[b % 2];
1580 }
1581 else if (bottom_blob.elempack == 8 && elempack == 4)
1582 {
1583 pipeline = pipeline_concat_pack8to4[b % 2];
1584 }
1585 else if (bottom_blob.elempack == 8 && elempack == 1)
1586 {
1587 pipeline = pipeline_concat_pack8to1[b % 2];
1588 }
1589
1590 cmd.record_pipeline(pipeline, bindings, constants, bottom_blob);
1591
1592 coffset += bottom_blob.c * bottom_blob.elempack / elempack;
1593 }
1594
1595 // packing
1596 if (elempack < out_elempack)
1597 {
1598 vkdev->convert_packing(top_blob_unpacked, top_blob, out_elempack, cmd, opt);
1599 }
1600
1601 return 0;
1602 }
1603
1604 if (dims == 4 && positive_axis == 1)
1605 {
1606 int w = bottom_blobs[0].w;
1607 int h = bottom_blobs[0].h;
1608 int channels = bottom_blobs[0].c;
1609 size_t elemsize = bottom_blobs[0].elemsize;
1610 int elempack = bottom_blobs[0].elempack;
1611
1612 // total depth
1613 int top_d = 0;
1614 for (size_t b = 0; b < bottom_blobs.size(); b++)
1615 {
1616 const VkImageMat& bottom_blob = bottom_blobs[b];
1617 top_d += bottom_blob.d;
1618 }
1619
1620 VkImageMat& top_blob = top_blobs[0];
1621 top_blob.create(w, h, top_d, channels, elemsize, elempack, opt.blob_vkallocator);
1622 if (top_blob.empty())
1623 return -100;
1624
1625 int doffset = 0;
1626 for (size_t b = 0; b < bottom_blobs.size(); b++)
1627 {
1628 const VkImageMat& bottom_blob = bottom_blobs[b];
1629
1630 std::vector<VkImageMat> bindings(2);
1631 bindings[0] = bottom_blob;
1632 bindings[1] = top_blob;
1633
1634 std::vector<vk_constant_type> constants(13);
1635 constants[0].i = bottom_blob.dims;
1636 constants[1].i = bottom_blob.w;
1637 constants[2].i = bottom_blob.h;
1638 constants[3].i = bottom_blob.d;
1639 constants[4].i = bottom_blob.c;
1640 constants[5].i = 0; //bottom_blob.cstep;
1641 constants[6].i = top_blob.dims;
1642 constants[7].i = top_blob.w;
1643 constants[8].i = top_blob.h;
1644 constants[9].i = top_blob.d;
1645 constants[10].i = top_blob.c;
1646 constants[11].i = 0; //top_blob.cstep;
1647 constants[12].i = doffset;
1648
1649 const Pipeline* pipeline = elempack == 8 ? pipeline_concat_pack8[b % 2]
1650 : elempack == 4 ? pipeline_concat_pack4[b % 2]
1651 : pipeline_concat[b % 2];
1652
1653 cmd.record_pipeline(pipeline, bindings, constants, bottom_blob);
1654
1655 doffset += bottom_blob.d;
1656 }
1657
1658 return 0;
1659 }
1660
1661 if (dims == 4 && positive_axis == 2)
1662 {
1663 int w = bottom_blobs[0].w;
1664 int d = bottom_blobs[0].d;
1665 int channels = bottom_blobs[0].c;
1666 size_t elemsize = bottom_blobs[0].elemsize;
1667 int elempack = bottom_blobs[0].elempack;
1668
1669 // total height
1670 int top_h = 0;
1671 for (size_t b = 0; b < bottom_blobs.size(); b++)
1672 {
1673 const VkImageMat& bottom_blob = bottom_blobs[b];
1674 top_h += bottom_blob.h;
1675 }
1676
1677 VkImageMat& top_blob = top_blobs[0];
1678 top_blob.create(w, top_h, d, channels, elemsize, elempack, opt.blob_vkallocator);
1679 if (top_blob.empty())
1680 return -100;
1681
1682 int hoffset = 0;
1683 for (size_t b = 0; b < bottom_blobs.size(); b++)
1684 {
1685 const VkImageMat& bottom_blob = bottom_blobs[b];
1686
1687 std::vector<VkImageMat> bindings(2);
1688 bindings[0] = bottom_blob;
1689 bindings[1] = top_blob;
1690
1691 std::vector<vk_constant_type> constants(13);
1692 constants[0].i = bottom_blob.dims;
1693 constants[1].i = bottom_blob.w;
1694 constants[2].i = bottom_blob.h;
1695 constants[3].i = bottom_blob.d;
1696 constants[4].i = bottom_blob.c;
1697 constants[5].i = 0; //bottom_blob.cstep;
1698 constants[6].i = top_blob.dims;
1699 constants[7].i = top_blob.w;
1700 constants[8].i = top_blob.h;
1701 constants[9].i = top_blob.d;
1702 constants[10].i = top_blob.c;
1703 constants[11].i = 0; //top_blob.cstep;
1704 constants[12].i = hoffset;
1705
1706 const Pipeline* pipeline = elempack == 8 ? pipeline_concat_pack8[b % 2]
1707 : elempack == 4 ? pipeline_concat_pack4[b % 2]
1708 : pipeline_concat[b % 2];
1709
1710 cmd.record_pipeline(pipeline, bindings, constants, bottom_blob);
1711
1712 hoffset += bottom_blob.h;
1713 }
1714
1715 return 0;
1716 }
1717
1718 if (dims == 4 && positive_axis == 3)
1719 {
1720 int h = bottom_blobs[0].h;
1721 int d = bottom_blobs[0].d;
1722 int channels = bottom_blobs[0].c;
1723 size_t elemsize = bottom_blobs[0].elemsize;
1724 int elempack = bottom_blobs[0].elempack;
1725
1726 // total height
1727 int top_w = 0;
1728 for (size_t b = 0; b < bottom_blobs.size(); b++)
1729 {
1730 const VkImageMat& bottom_blob = bottom_blobs[b];
1731 top_w += bottom_blob.w;
1732 }
1733
1734 VkImageMat& top_blob = top_blobs[0];
1735 top_blob.create(top_w, h, d, channels, elemsize, elempack, opt.blob_vkallocator);
1736 if (top_blob.empty())
1737 return -100;
1738
1739 int woffset = 0;
1740 for (size_t b = 0; b < bottom_blobs.size(); b++)
1741 {
1742 const VkImageMat& bottom_blob = bottom_blobs[b];
1743
1744 std::vector<VkImageMat> bindings(2);
1745 bindings[0] = bottom_blob;
1746 bindings[1] = top_blob;
1747
1748 std::vector<vk_constant_type> constants(13);
1749 constants[0].i = bottom_blob.dims;
1750 constants[1].i = bottom_blob.w;
1751 constants[2].i = bottom_blob.h;
1752 constants[3].i = bottom_blob.d;
1753 constants[4].i = bottom_blob.c;
1754 constants[5].i = 0; //bottom_blob.cstep;
1755 constants[6].i = top_blob.dims;
1756 constants[7].i = top_blob.w;
1757 constants[8].i = top_blob.h;
1758 constants[9].i = top_blob.d;
1759 constants[10].i = top_blob.c;
1760 constants[11].i = 0; //top_blob.cstep;
1761 constants[12].i = woffset;
1762
1763 const Pipeline* pipeline = elempack == 8 ? pipeline_concat_pack8[b % 2]
1764 : elempack == 4 ? pipeline_concat_pack4[b % 2]
1765 : pipeline_concat[b % 2];
1766
1767 cmd.record_pipeline(pipeline, bindings, constants, bottom_blob);
1768
1769 woffset += bottom_blob.w;
1770 }
1771
1772 return 0;
1773 }
1774
1775 return 0;
1776}
1777
1778} // namespace ncnn