21
#include "layer_declaration.h"
27
one_blob_only = false;
28
support_inplace = false;
29
support_vulkan = false;
30
support_packing = false;
32
support_bf16_storage = false;
33
support_fp16_storage = false;
34
support_int8_storage = false;
35
support_image_storage = false;
36
support_tensor_storage = false;
38
support_reserved_00 = false;
53
int Layer::load_param(const ParamDict& )
58
int Layer::load_model(const ModelBin& )
63
int Layer::create_pipeline(const Option& )
68
int Layer::destroy_pipeline(const Option& )
73
int Layer::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
78
top_blobs = bottom_blobs;
79
for (int i = 0; i < (int)top_blobs.size(); i++)
81
top_blobs[i] = bottom_blobs[i].clone(opt.blob_allocator);
82
if (top_blobs[i].empty())
86
return forward_inplace(top_blobs, opt);
89
int Layer::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
94
top_blob = bottom_blob.clone(opt.blob_allocator);
98
return forward_inplace(top_blob, opt);
101
int Layer::forward_inplace(std::vector<Mat>& , const Option& ) const
106
int Layer::forward_inplace(Mat& , const Option& ) const
112
int Layer::upload_model(VkTransfer& , const Option& )
117
int Layer::forward(const std::vector<VkMat>& bottom_blobs, std::vector<VkMat>& top_blobs, VkCompute& cmd, const Option& opt) const
119
if (!support_inplace)
122
top_blobs.resize(bottom_blobs.size());
123
for (int i = 0; i < (int)top_blobs.size(); i++)
125
cmd.record_clone(bottom_blobs[i], top_blobs[i], opt);
128
return forward_inplace(top_blobs, cmd, opt);
131
int Layer::forward(const VkMat& bottom_blob, VkMat& top_blob, VkCompute& cmd, const Option& opt) const
133
if (!support_inplace)
136
cmd.record_clone(bottom_blob, top_blob, opt);
138
return forward_inplace(top_blob, cmd, opt);
141
int Layer::forward(const std::vector<VkImageMat>& bottom_blobs, std::vector<VkImageMat>& top_blobs, VkCompute& cmd, const Option& opt) const
143
if (!support_inplace)
146
top_blobs.resize(bottom_blobs.size());
147
for (int i = 0; i < (int)top_blobs.size(); i++)
149
cmd.record_clone(bottom_blobs[i], top_blobs[i], opt);
152
return forward_inplace(top_blobs, cmd, opt);
155
int Layer::forward(const VkImageMat& bottom_blob, VkImageMat& top_blob, VkCompute& cmd, const Option& opt) const
157
if (!support_inplace)
160
cmd.record_clone(bottom_blob, top_blob, opt);
162
return forward_inplace(top_blob, cmd, opt);
165
int Layer::forward_inplace(std::vector<VkMat>& , VkCompute& , const Option& ) const
170
int Layer::forward_inplace(VkMat& , VkCompute& , const Option& ) const
175
int Layer::forward_inplace(std::vector<VkImageMat>& , VkCompute& , const Option& ) const
180
int Layer::forward_inplace(VkImageMat& , VkCompute& , const Option& ) const
186
#include "layer_registry.h"
188
static const int layer_registry_entry_count = sizeof(layer_registry) / sizeof(layer_registry_entry);
191
int layer_to_index(const char* type)
193
for (int i = 0; i < layer_registry_entry_count; i++)
195
if (strcmp(type, layer_registry[i].name) == 0)
202
Layer* create_layer(const char* type)
204
int index = layer_to_index(type);
208
return create_layer(index);
211
Layer* create_layer_naive(const char* type)
213
int index = layer_to_index(type);
217
return create_layer_naive(index);
220
Layer* create_layer_cpu(const char* type)
222
int index = layer_to_index(type);
226
return create_layer_cpu(index);
230
Layer* create_layer_vulkan(const char* type)
232
int index = layer_to_index(type);
236
return create_layer_vulkan(index);
242
class Layer_final : public Layer
251
void set_layer_properties()
253
layer_cpu->userdata = userdata;
255
layer_cpu->bottoms = bottoms;
256
layer_cpu->tops = tops;
257
layer_cpu->bottom_shapes = bottom_shapes;
258
layer_cpu->top_shapes = top_shapes;
259
layer_cpu->featmask = featmask;
264
layer_vulkan->vkdev = vkdev;
266
layer_vulkan->userdata = userdata;
268
layer_vulkan->bottoms = bottoms;
269
layer_vulkan->tops = tops;
270
layer_vulkan->bottom_shapes = bottom_shapes;
271
layer_vulkan->top_shapes = top_shapes;
272
layer_vulkan->featmask = featmask;
277
void get_layer_properties()
279
one_blob_only = layer_cpu->one_blob_only;
280
support_inplace = layer_cpu->support_inplace;
281
support_packing = layer_cpu->support_packing;
282
support_bf16_storage = layer_cpu->support_bf16_storage;
283
support_fp16_storage = layer_cpu->support_fp16_storage;
284
support_int8_storage = layer_cpu->support_int8_storage;
287
support_image_storage = 0;
288
support_tensor_storage = 0;
293
support_vulkan = layer_vulkan->support_vulkan;
294
support_image_storage = layer_vulkan->support_image_storage;
295
support_tensor_storage = layer_vulkan->support_tensor_storage;
317
virtual int load_param(const ParamDict& pd)
319
set_layer_properties();
325
int ret = layer_vulkan->load_param(pd);
326
get_layer_properties();
328
if (layer_vulkan->support_vulkan)
338
int ret = layer_cpu->load_param(pd);
339
get_layer_properties();
343
virtual int load_model(const ModelBin& mb)
348
int ret = layer_vulkan->load_model(mb);
349
get_layer_properties();
354
int ret = layer_cpu->load_model(mb);
355
get_layer_properties();
359
virtual int create_pipeline(const Option& opt)
361
set_layer_properties();
367
int ret = layer_vulkan->create_pipeline(opt);
368
get_layer_properties();
378
int ret = layer_cpu->create_pipeline(opt);
379
get_layer_properties();
383
virtual int destroy_pipeline(const Option& opt)
388
return layer_vulkan->destroy_pipeline(opt);
392
return layer_cpu->destroy_pipeline(opt);
396
virtual int forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
398
return layer_cpu->forward(bottom_blobs, top_blobs, opt);
401
virtual int forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
403
return layer_cpu->forward(bottom_blob, top_blob, opt);
406
virtual int forward_inplace(std::vector<Mat>& bottom_top_blobs, const Option& opt) const
408
return layer_cpu->forward_inplace(bottom_top_blobs, opt);
411
virtual int forward_inplace(Mat& bottom_top_blob, const Option& opt) const
413
return layer_cpu->forward_inplace(bottom_top_blob, opt);
418
virtual int upload_model(VkTransfer& cmd, const Option& opt)
420
return layer_vulkan ? layer_vulkan->upload_model(cmd, opt) : -1;
423
virtual int forward(const std::vector<VkMat>& bottom_blobs, std::vector<VkMat>& top_blobs, VkCompute& cmd, const Option& opt) const
425
return layer_vulkan ? layer_vulkan->forward(bottom_blobs, top_blobs, cmd, opt) : -1;
428
virtual int forward(const VkMat& bottom_blob, VkMat& top_blob, VkCompute& cmd, const Option& opt) const
430
return layer_vulkan ? layer_vulkan->forward(bottom_blob, top_blob, cmd, opt) : -1;
433
virtual int forward(const std::vector<VkImageMat>& bottom_blobs, std::vector<VkImageMat>& top_blobs, VkCompute& cmd, const Option& opt) const
435
return layer_vulkan ? layer_vulkan->forward(bottom_blobs, top_blobs, cmd, opt) : -1;
438
virtual int forward(const VkImageMat& bottom_blob, VkImageMat& top_blob, VkCompute& cmd, const Option& opt) const
440
return layer_vulkan ? layer_vulkan->forward(bottom_blob, top_blob, cmd, opt) : -1;
443
virtual int forward_inplace(std::vector<VkMat>& bottom_top_blobs, VkCompute& cmd, const Option& opt) const
445
return layer_vulkan ? layer_vulkan->forward_inplace(bottom_top_blobs, cmd, opt) : -1;
448
virtual int forward_inplace(VkMat& bottom_top_blob, VkCompute& cmd, const Option& opt) const
450
return layer_vulkan ? layer_vulkan->forward_inplace(bottom_top_blob, cmd, opt) : -1;
453
virtual int forward_inplace(std::vector<VkImageMat>& bottom_top_blobs, VkCompute& cmd, const Option& opt) const
455
return layer_vulkan ? layer_vulkan->forward_inplace(bottom_top_blobs, cmd, opt) : -1;
458
virtual int forward_inplace(VkImageMat& bottom_top_blob, VkCompute& cmd, const Option& opt) const
460
return layer_vulkan ? layer_vulkan->forward_inplace(bottom_top_blob, cmd, opt) : -1;
465
Layer* create_layer(int index)
467
Layer* layer_cpu = create_layer_cpu(index);
471
Layer_final* layer_final = new Layer_final;
472
layer_final->layer_cpu = layer_cpu;
475
layer_final->layer_vulkan = create_layer_vulkan(index);
478
layer_final->typeindex = index;
479
layer_final->set_layer_properties();
480
layer_final->get_layer_properties();
485
Layer* create_layer_naive(int index)
487
if (index < 0 || index >= layer_registry_entry_count)
490
layer_creator_func layer_creator = layer_registry[index].creator;
494
Layer* layer = layer_creator(0);
495
layer->typeindex = index;
499
Layer* create_layer_cpu(int index)
501
if (index < 0 || index >= layer_registry_entry_count)
506
layer_creator_func layer_creator = 0;
507
#if NCNN_RUNTIME_CPU && NCNN_AVX512
508
if (ncnn::cpu_support_x86_avx512())
510
layer_creator = layer_registry_avx512[index].creator;
514
#if NCNN_RUNTIME_CPU && NCNN_FMA
515
if (ncnn::cpu_support_x86_fma())
517
layer_creator = layer_registry_fma[index].creator;
521
#if NCNN_RUNTIME_CPU && NCNN_AVX
522
if (ncnn::cpu_support_x86_avx())
524
layer_creator = layer_registry_avx[index].creator;
528
#if NCNN_RUNTIME_CPU && NCNN_LASX
529
if (ncnn::cpu_support_loongarch_lasx())
531
layer_creator = layer_registry_lasx[index].creator;
535
#if NCNN_RUNTIME_CPU && NCNN_LSX
536
if (ncnn::cpu_support_loongarch_lsx())
538
layer_creator = layer_registry_lsx[index].creator;
542
#if NCNN_RUNTIME_CPU && NCNN_MSA
543
if (ncnn::cpu_support_mips_msa())
545
layer_creator = layer_registry_msa[index].creator;
549
#if NCNN_RUNTIME_CPU && NCNN_RVV
550
if (ncnn::cpu_support_riscv_v())
552
layer_creator = layer_registry_rvv[index].creator;
557
layer_creator = layer_registry_arch[index].creator;
562
layer_creator = layer_registry[index].creator;
569
Layer* layer = layer_creator(0);
570
layer->typeindex = index;
575
Layer* create_layer_vulkan(int index)
577
if (index < 0 || index >= layer_registry_entry_count)
580
layer_creator_func layer_creator = layer_registry_vulkan[index].creator;
584
Layer* layer = layer_creator(0);
585
layer->typeindex = index;