| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | #include "layer.h" |
| |
|
| | #include "cpu.h" |
| |
|
| | #include <math.h> |
| | #include <string.h> |
| |
|
| | #ifdef _MSC_VER |
| | #pragma warning(push) |
| | #pragma warning(disable : 4250) |
| | #endif |
| | #ifdef __clang__ |
| | #pragma clang diagnostic push |
| | #pragma clang diagnostic ignored "-Woverloaded-virtual" |
| | #endif |
| | #include "layer_declaration.h" |
| | #ifdef __clang__ |
| | #pragma clang diagnostic pop |
| | #endif |
| | #ifdef _MSC_VER |
| | #pragma warning(pop) |
| | #endif |
| |
|
| | namespace ncnn { |
| |
|
| | Layer::Layer() |
| | { |
| | one_blob_only = false; |
| | support_inplace = false; |
| | support_vulkan = false; |
| | support_packing = false; |
| |
|
| | support_bf16_storage = false; |
| | support_fp16_storage = false; |
| | support_int8_storage = false; |
| | support_image_storage = false; |
| | support_tensor_storage = false; |
| |
|
| | support_reserved_00 = false; |
| |
|
| | typeindex = -1; |
| |
|
| | #if NCNN_VULKAN |
| | vkdev = 0; |
| | #endif |
| |
|
| | userdata = 0; |
| | } |
| |
|
| | Layer::~Layer() |
| | { |
| | } |
| |
|
| | int Layer::load_param(const ParamDict& ) |
| | { |
| | return 0; |
| | } |
| |
|
| | int Layer::load_model(const ModelBin& ) |
| | { |
| | return 0; |
| | } |
| |
|
| | int Layer::create_pipeline(const Option& ) |
| | { |
| | return 0; |
| | } |
| |
|
| | int Layer::destroy_pipeline(const Option& ) |
| | { |
| | return 0; |
| | } |
| |
|
| | int Layer::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| | { |
| | if (!support_inplace) |
| | return -1; |
| |
|
| | top_blobs = bottom_blobs; |
| | for (int i = 0; i < (int)top_blobs.size(); i++) |
| | { |
| | top_blobs[i] = bottom_blobs[i].clone(opt.blob_allocator); |
| | if (top_blobs[i].empty()) |
| | return -100; |
| | } |
| |
|
| | return forward_inplace(top_blobs, opt); |
| | } |
| |
|
| | int Layer::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| | { |
| | if (!support_inplace) |
| | return -1; |
| |
|
| | top_blob = bottom_blob.clone(opt.blob_allocator); |
| | if (top_blob.empty()) |
| | return -100; |
| |
|
| | return forward_inplace(top_blob, opt); |
| | } |
| |
|
| | int Layer::forward_inplace(std::vector<Mat>& , const Option& ) const |
| | { |
| | return -1; |
| | } |
| |
|
| | int Layer::forward_inplace(Mat& , const Option& ) const |
| | { |
| | return -1; |
| | } |
| |
|
| | #if NCNN_VULKAN |
| | int Layer::upload_model(VkTransfer& , const Option& ) |
| | { |
| | return 0; |
| | } |
| |
|
| | int Layer::forward(const std::vector<VkMat>& bottom_blobs, std::vector<VkMat>& top_blobs, VkCompute& cmd, const Option& opt) const |
| | { |
| | if (!support_inplace) |
| | return -1; |
| |
|
| | top_blobs.resize(bottom_blobs.size()); |
| | for (int i = 0; i < (int)top_blobs.size(); i++) |
| | { |
| | cmd.record_clone(bottom_blobs[i], top_blobs[i], opt); |
| | } |
| |
|
| | return forward_inplace(top_blobs, cmd, opt); |
| | } |
| |
|
| | int Layer::forward(const VkMat& bottom_blob, VkMat& top_blob, VkCompute& cmd, const Option& opt) const |
| | { |
| | if (!support_inplace) |
| | return -1; |
| |
|
| | cmd.record_clone(bottom_blob, top_blob, opt); |
| |
|
| | return forward_inplace(top_blob, cmd, opt); |
| | } |
| |
|
| | int Layer::forward(const std::vector<VkImageMat>& bottom_blobs, std::vector<VkImageMat>& top_blobs, VkCompute& cmd, const Option& opt) const |
| | { |
| | if (!support_inplace) |
| | return -1; |
| |
|
| | top_blobs.resize(bottom_blobs.size()); |
| | for (int i = 0; i < (int)top_blobs.size(); i++) |
| | { |
| | cmd.record_clone(bottom_blobs[i], top_blobs[i], opt); |
| | } |
| |
|
| | return forward_inplace(top_blobs, cmd, opt); |
| | } |
| |
|
| | int Layer::forward(const VkImageMat& bottom_blob, VkImageMat& top_blob, VkCompute& cmd, const Option& opt) const |
| | { |
| | if (!support_inplace) |
| | return -1; |
| |
|
| | cmd.record_clone(bottom_blob, top_blob, opt); |
| |
|
| | return forward_inplace(top_blob, cmd, opt); |
| | } |
| |
|
| | int Layer::forward_inplace(std::vector<VkMat>& , VkCompute& , const Option& ) const |
| | { |
| | return -1; |
| | } |
| |
|
| | int Layer::forward_inplace(VkMat& , VkCompute& , const Option& ) const |
| | { |
| | return -1; |
| | } |
| |
|
| | int Layer::forward_inplace(std::vector<VkImageMat>& , VkCompute& , const Option& ) const |
| | { |
| | return -1; |
| | } |
| |
|
| | int Layer::forward_inplace(VkImageMat& , VkCompute& , const Option& ) const |
| | { |
| | return -1; |
| | } |
| | #endif |
| |
|
| | #include "layer_registry.h" |
| |
|
| | static const int layer_registry_entry_count = sizeof(layer_registry) / sizeof(layer_registry_entry); |
| |
|
| | #if NCNN_STRING |
| | int layer_to_index(const char* type) |
| | { |
| | for (int i = 0; i < layer_registry_entry_count; i++) |
| | { |
| | if (strcmp(type, layer_registry[i].name) == 0) |
| | return i; |
| | } |
| |
|
| | return -1; |
| | } |
| |
|
| | Layer* create_layer(const char* type) |
| | { |
| | int index = layer_to_index(type); |
| | if (index == -1) |
| | return 0; |
| |
|
| | return create_layer(index); |
| | } |
| | #endif |
| |
|
| | Layer* create_layer(int index) |
| | { |
| | if (index < 0 || index >= layer_registry_entry_count) |
| | return 0; |
| |
|
| | |
| | |
| | layer_creator_func layer_creator = 0; |
| | #if NCNN_RUNTIME_CPU && NCNN_AVX512 |
| | if (ncnn::cpu_support_x86_avx512()) |
| | { |
| | layer_creator = layer_registry_avx512[index].creator; |
| | } |
| | else |
| | #endif |
| | #if NCNN_RUNTIME_CPU && NCNN_FMA |
| | if (ncnn::cpu_support_x86_fma()) |
| | { |
| | layer_creator = layer_registry_fma[index].creator; |
| | } |
| | else |
| | #endif |
| | #if NCNN_RUNTIME_CPU && NCNN_AVX |
| | if (ncnn::cpu_support_x86_avx()) |
| | { |
| | layer_creator = layer_registry_avx[index].creator; |
| | } |
| | else |
| | #endif |
| | #if NCNN_RUNTIME_CPU && NCNN_LASX |
| | if (ncnn::cpu_support_loongarch_lasx()) |
| | { |
| | layer_creator = layer_registry_lasx[index].creator; |
| | } |
| | else |
| | #endif |
| | #if NCNN_RUNTIME_CPU && NCNN_LSX |
| | if (ncnn::cpu_support_loongarch_lsx()) |
| | { |
| | layer_creator = layer_registry_lsx[index].creator; |
| | } |
| | else |
| | #endif |
| | #if NCNN_RUNTIME_CPU && NCNN_MSA |
| | if (ncnn::cpu_support_mips_msa()) |
| | { |
| | layer_creator = layer_registry_msa[index].creator; |
| | } |
| | else |
| | #endif |
| | #if NCNN_RUNTIME_CPU && NCNN_RVV |
| | if (ncnn::cpu_support_riscv_v()) |
| | { |
| | layer_creator = layer_registry_rvv[index].creator; |
| | } |
| | else |
| | #endif |
| | { |
| | layer_creator = layer_registry[index].creator; |
| | } |
| | |
| | |
| | if (!layer_creator) |
| | return 0; |
| |
|
| | Layer* layer = layer_creator(0); |
| | layer->typeindex = index; |
| | return layer; |
| | } |
| |
|
| | } |
| |
|