From 6ba55bf22293ca91481d2c39e297e18b5a503e3c Mon Sep 17 00:00:00 2001 From: nihuini Date: Wed, 3 Jan 2024 14:30:40 +0800 Subject: [PATCH] create layer decoupled --- cmake/ncnn_add_layer.cmake | 56 +--- cmake/ncnn_add_shader.cmake | 4 +- cmake/ncnn_generate_shader_comp_header.cmake | 4 +- src/gpu.cpp | 2 +- src/layer.cpp | 293 ++++++++++++++++++- src/layer.h | 8 + src/layer/convolution.cpp | 13 +- src/layer/convolution.h | 2 - src/layer/convolution1d.cpp | 8 - src/layer/convolution1d.h | 2 - src/layer/convolutiondepthwise.cpp | 12 +- src/layer/convolutiondepthwise.h | 2 - src/layer/convolutiondepthwise1d.cpp | 5 - src/layer/convolutiondepthwise1d.h | 2 - src/layer/innerproduct.cpp | 12 +- src/layer/innerproduct.h | 2 - src/layer/noop.cpp | 14 - src/layer/noop.h | 5 - src/layer/split.cpp | 26 -- src/layer/split.h | 7 - src/layer/vulkan/noop_vulkan.cpp | 35 +++ src/layer/vulkan/noop_vulkan.h | 34 +++ src/layer/vulkan/split_vulkan.cpp | 47 +++ src/layer/vulkan/split_vulkan.h | 34 +++ src/layer_registry.h.in | 8 + 25 files changed, 472 insertions(+), 165 deletions(-) create mode 100644 src/layer/vulkan/noop_vulkan.cpp create mode 100644 src/layer/vulkan/noop_vulkan.h create mode 100644 src/layer/vulkan/split_vulkan.cpp create mode 100644 src/layer/vulkan/split_vulkan.h diff --git a/cmake/ncnn_add_layer.cmake b/cmake/ncnn_add_layer.cmake index e6c74fec5eb..079bdd9f506 100644 --- a/cmake/ncnn_add_layer.cmake +++ b/cmake/ncnn_add_layer.cmake @@ -31,35 +31,14 @@ macro(ncnn_add_arch_opt_layer class NCNN_TARGET_ARCH_OPT NCNN_TARGET_ARCH_OPT_CF list(APPEND ncnn_SRCS ${NCNN_${NCNN_TARGET_ARCH_OPT}_HEADER} ${NCNN_${NCNN_TARGET_ARCH_OPT}_SOURCE}) # generate layer_declaration and layer_registry file - set(layer_declaration "${layer_declaration}#include \"layer/${name}.h\"\n") - set(layer_declaration_class "class ${class}_final_${NCNN_TARGET_ARCH_OPT} : virtual public ${class}") - set(create_pipeline_content " { int ret = ${class}::create_pipeline(opt); if (ret) return ret; }\n") - set(destroy_pipeline_content " { int ret = ${class}::destroy_pipeline(opt); if (ret) return ret; }\n") - - if(WITH_LAYER_${name}_vulkan) - set(layer_declaration "${layer_declaration}#include \"layer/vulkan/${name}_vulkan.h\"\n") - set(layer_declaration_class "${layer_declaration_class}, virtual public ${class}_vulkan") - set(create_pipeline_content "${create_pipeline_content} if (vkdev) { int ret = ${class}_vulkan::create_pipeline(opt); if (ret) return ret; }\n") - set(destroy_pipeline_content " if (vkdev) { int ret = ${class}_vulkan::destroy_pipeline(opt); if (ret) return ret; }\n${destroy_pipeline_content}") - endif() - set(layer_declaration "${layer_declaration}#include \"layer/${NCNN_TARGET_ARCH}/${name}_${NCNN_TARGET_ARCH}_${NCNN_TARGET_ARCH_OPT}.h\"\n") - set(layer_declaration_class "${layer_declaration_class}, virtual public ${class}_${NCNN_TARGET_ARCH}_${NCNN_TARGET_ARCH_OPT}") - set(create_pipeline_content "${create_pipeline_content} { int ret = ${class}_${NCNN_TARGET_ARCH}_${NCNN_TARGET_ARCH_OPT}::create_pipeline(opt); if (ret) return ret; }\n") - set(destroy_pipeline_content " { int ret = ${class}_${NCNN_TARGET_ARCH}_${NCNN_TARGET_ARCH_OPT}::destroy_pipeline(opt); if (ret) return ret; }\n${destroy_pipeline_content}") - - set(layer_declaration "${layer_declaration}namespace ncnn {\n${layer_declaration_class}\n{\n") - set(layer_declaration "${layer_declaration}public:\n") - set(layer_declaration "${layer_declaration} virtual int create_pipeline(const Option& opt) {\n${create_pipeline_content} return 0;\n }\n") - set(layer_declaration "${layer_declaration} virtual int destroy_pipeline(const Option& opt) {\n${destroy_pipeline_content} return 0;\n }\n") - set(layer_declaration "${layer_declaration}};\n") - set(layer_declaration "${layer_declaration}DEFINE_LAYER_CREATOR(${class}_final_${NCNN_TARGET_ARCH_OPT})\n} // namespace ncnn\n\n") - - set(layer_registry_${NCNN_TARGET_ARCH_OPT} "${layer_registry_${NCNN_TARGET_ARCH_OPT}}#if NCNN_STRING\n{\"${class}\", ${class}_final_${NCNN_TARGET_ARCH_OPT}_layer_creator},\n#else\n{${class}_final_${NCNN_TARGET_ARCH_OPT}_layer_creator},\n#endif\n") + set(layer_declaration "${layer_declaration}namespace ncnn { DEFINE_LAYER_CREATOR(${class}_${NCNN_TARGET_ARCH}_${NCNN_TARGET_ARCH_OPT}) }\n") + + set(layer_registry_${NCNN_TARGET_ARCH_OPT} "${layer_registry_${NCNN_TARGET_ARCH_OPT}}#if NCNN_STRING\n{\"${class}\", ${class}_${NCNN_TARGET_ARCH}_${NCNN_TARGET_ARCH_OPT}_layer_creator},\n#else\n{${class}_${NCNN_TARGET_ARCH}_${NCNN_TARGET_ARCH_OPT}_layer_creator},\n#endif\n") else() # no isa optimized version if(WITH_LAYER_${name}) - set(layer_registry_${NCNN_TARGET_ARCH_OPT} "${layer_registry_${NCNN_TARGET_ARCH_OPT}}#if NCNN_STRING\n{\"${class}\", ${class}_final_layer_creator},\n#else\n{${class}_final_layer_creator},\n#endif\n") + set(layer_registry_${NCNN_TARGET_ARCH_OPT} "${layer_registry_${NCNN_TARGET_ARCH_OPT}}#if NCNN_STRING\n{\"${class}\", ${class}_layer_creator},\n#else\n{${class}_layer_creator},\n#endif\n") else() set(layer_registry_${NCNN_TARGET_ARCH_OPT} "${layer_registry_${NCNN_TARGET_ARCH_OPT}}#if NCNN_STRING\n{\"${class}\", 0},\n#else\n{0},\n#endif\n") endif() @@ -110,18 +89,14 @@ macro(ncnn_add_layer class) # generate layer_declaration and layer_registry file if(WITH_LAYER_${name}) set(layer_declaration "${layer_declaration}#include \"layer/${name}.h\"\n") - set(layer_declaration_class "class ${class}_final : virtual public ${class}") - set(create_pipeline_content " { int ret = ${class}::create_pipeline(opt); if (ret) return ret; }\n") - set(destroy_pipeline_content " { int ret = ${class}::destroy_pipeline(opt); if (ret) return ret; }\n") + set(layer_declaration "${layer_declaration}namespace ncnn { DEFINE_LAYER_CREATOR(${class}) }\n") source_group ("sources\\\\layers" FILES "${CMAKE_CURRENT_SOURCE_DIR}/layer/${name}.cpp") endif() if(WITH_LAYER_${name}_vulkan) set(layer_declaration "${layer_declaration}#include \"layer/vulkan/${name}_vulkan.h\"\n") - set(layer_declaration_class "${layer_declaration_class}, virtual public ${class}_vulkan") - set(create_pipeline_content "${create_pipeline_content} if (vkdev) { int ret = ${class}_vulkan::create_pipeline(opt); if (ret) return ret; }\n") - set(destroy_pipeline_content " if (vkdev) { int ret = ${class}_vulkan::destroy_pipeline(opt); if (ret) return ret; }\n${destroy_pipeline_content}") + set(layer_declaration "${layer_declaration}namespace ncnn { DEFINE_LAYER_CREATOR(${class}_vulkan) }\n") file(GLOB_RECURSE NCNN_SHADER_SRCS "layer/vulkan/shader/${name}.comp") file(GLOB_RECURSE NCNN_SHADER_SUBSRCS "layer/vulkan/shader/${name}_*.comp") @@ -135,26 +110,21 @@ macro(ncnn_add_layer class) if(WITH_LAYER_${name}_${NCNN_TARGET_ARCH}) set(layer_declaration "${layer_declaration}#include \"layer/${NCNN_TARGET_ARCH}/${name}_${NCNN_TARGET_ARCH}.h\"\n") - set(layer_declaration_class "${layer_declaration_class}, virtual public ${class}_${NCNN_TARGET_ARCH}") - set(create_pipeline_content "${create_pipeline_content} { int ret = ${class}_${NCNN_TARGET_ARCH}::create_pipeline(opt); if (ret) return ret; }\n") - set(destroy_pipeline_content " { int ret = ${class}_${NCNN_TARGET_ARCH}::destroy_pipeline(opt); if (ret) return ret; }\n${destroy_pipeline_content}") + set(layer_declaration "${layer_declaration}namespace ncnn { DEFINE_LAYER_CREATOR(${class}_${NCNN_TARGET_ARCH}) }\n") source_group ("sources\\\\layers\\\\${NCNN_TARGET_ARCH}" FILES "${CMAKE_CURRENT_SOURCE_DIR}/layer/${NCNN_TARGET_ARCH}/${name}_${NCNN_TARGET_ARCH}.cpp") endif() if(WITH_LAYER_${name}) - set(layer_declaration "${layer_declaration}namespace ncnn {\n${layer_declaration_class}\n{\n") - set(layer_declaration "${layer_declaration}public:\n") - set(layer_declaration "${layer_declaration} virtual int create_pipeline(const Option& opt) {\n${create_pipeline_content} return 0;\n }\n") - set(layer_declaration "${layer_declaration} virtual int destroy_pipeline(const Option& opt) {\n${destroy_pipeline_content} return 0;\n }\n") - set(layer_declaration "${layer_declaration}};\n") - set(layer_declaration "${layer_declaration}DEFINE_LAYER_CREATOR(${class}_final)\n} // namespace ncnn\n\n") + set(layer_registry "${layer_registry}#if NCNN_STRING\n{\"${class}\", ${class}_layer_creator},\n#else\n{${class}_layer_creator},\n#endif\n") + else() + set(layer_registry "${layer_registry}#if NCNN_STRING\n{\"${class}\", 0},\n#else\n{0},\n#endif\n") endif() - if(WITH_LAYER_${name}) - set(layer_registry "${layer_registry}#if NCNN_STRING\n{\"${class}\", ${class}_final_layer_creator},\n#else\n{${class}_final_layer_creator},\n#endif\n") + if(WITH_LAYER_${name}_vulkan) + set(layer_registry_vulkan "${layer_registry_vulkan}#if NCNN_STRING\n{\"${class}\", ${class}_vulkan_layer_creator},\n#else\n{${class}_vulkan_layer_creator},\n#endif\n") else() - set(layer_registry "${layer_registry}#if NCNN_STRING\n{\"${class}\", 0},\n#else\n{0},\n#endif\n") + set(layer_registry_vulkan "${layer_registry_vulkan}#if NCNN_STRING\n{\"${class}\", 0},\n#else\n{0},\n#endif\n") endif() if(NCNN_TARGET_ARCH STREQUAL "x86") diff --git a/cmake/ncnn_add_shader.cmake b/cmake/ncnn_add_shader.cmake index 8006241bc05..76680f4ca81 100644 --- a/cmake/ncnn_add_shader.cmake +++ b/cmake/ncnn_add_shader.cmake @@ -1,7 +1,7 @@ macro(ncnn_add_shader NCNN_SHADER_SRC) get_filename_component(NCNN_SHADER_SRC_NAME_WE ${NCNN_SHADER_SRC} NAME_WE) - set(NCNN_SHADER_COMP_HEADER ${CMAKE_CURRENT_BINARY_DIR}/${NCNN_SHADER_SRC_NAME_WE}.comp.hex.h) + set(NCNN_SHADER_COMP_HEADER ${CMAKE_CURRENT_BINARY_DIR}/layer/vulkan/shader/${NCNN_SHADER_SRC_NAME_WE}.comp.hex.h) add_custom_command( OUTPUT ${NCNN_SHADER_COMP_HEADER} @@ -13,7 +13,7 @@ macro(ncnn_add_shader NCNN_SHADER_SRC) set_source_files_properties(${NCNN_SHADER_COMP_HEADER} PROPERTIES GENERATED TRUE) get_filename_component(NCNN_SHADER_COMP_HEADER_NAME ${NCNN_SHADER_COMP_HEADER} NAME) - string(APPEND layer_shader_spv_data "#include \"${NCNN_SHADER_COMP_HEADER_NAME}\"\n") + string(APPEND layer_shader_spv_data "#include \"layer/vulkan/shader/${NCNN_SHADER_COMP_HEADER_NAME}\"\n") get_filename_component(NCNN_SHADER_SRC_NAME_WE ${NCNN_SHADER_SRC} NAME_WE) string(APPEND layer_shader_registry "{${NCNN_SHADER_SRC_NAME_WE}_comp_data,sizeof(${NCNN_SHADER_SRC_NAME_WE}_comp_data)},\n") diff --git a/cmake/ncnn_generate_shader_comp_header.cmake b/cmake/ncnn_generate_shader_comp_header.cmake index a41b6328d8d..79f7c1eff3b 100644 --- a/cmake/ncnn_generate_shader_comp_header.cmake +++ b/cmake/ncnn_generate_shader_comp_header.cmake @@ -18,8 +18,8 @@ string(REGEX REPLACE "\n\n" "\n" comp_data "${comp_data}") get_filename_component(SHADER_SRC_NAME_WE ${SHADER_SRC} NAME_WE) # text to hex -file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/${SHADER_SRC_NAME_WE}.text2hex.txt "${comp_data}") -file(READ ${CMAKE_CURRENT_BINARY_DIR}/${SHADER_SRC_NAME_WE}.text2hex.txt comp_data_hex HEX) +file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/layer/vulkan/shader/${SHADER_SRC_NAME_WE}.text2hex.txt "${comp_data}") +file(READ ${CMAKE_CURRENT_BINARY_DIR}/layer/vulkan/shader/${SHADER_SRC_NAME_WE}.text2hex.txt comp_data_hex HEX) string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1," comp_data_hex ${comp_data_hex}) string(FIND "${comp_data_hex}" "," tail_comma REVERSE) string(SUBSTRING "${comp_data_hex}" 0 ${tail_comma} comp_data_hex) diff --git a/src/gpu.cpp b/src/gpu.cpp index 447f66bae44..adba869e1e9 100644 --- a/src/gpu.cpp +++ b/src/gpu.cpp @@ -26,7 +26,7 @@ #include "glslang/glslang/Public/ShaderLang.h" #endif -#include "vulkan_activation.comp.hex.h" +#include "layer/vulkan/shader/vulkan_activation.comp.hex.h" #include "command.h" #include "layer.h" diff --git a/src/layer.cpp b/src/layer.cpp index 562576a5493..cf895b4169d 100644 --- a/src/layer.cpp +++ b/src/layer.cpp @@ -18,21 +18,7 @@ #include -#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 { @@ -221,9 +207,272 @@ Layer* create_layer(const char* type) return create_layer(index); } + +Layer* create_layer_cpu(const char* type) +{ + int index = layer_to_index(type); + if (index == -1) + return 0; + + return create_layer_cpu(index); +} + +#if NCNN_VULKAN +Layer* create_layer_vulkan(const char* type) +{ + int index = layer_to_index(type); + if (index == -1) + return 0; + + return create_layer_vulkan(index); +} +#endif // NCNN_VULKAN #endif // NCNN_STRING +// internal wrapper +class Layer_final : public Layer +{ +public: + Layer* layer_cpu; +#if NCNN_VULKAN + Layer* layer_vulkan; +#endif + + // utility functions for transfer layer properties + void set_layer_properties() + { + layer_cpu->userdata = userdata; + + layer_cpu->bottoms = bottoms; + layer_cpu->tops = tops; + layer_cpu->bottom_shapes = bottom_shapes; + layer_cpu->top_shapes = top_shapes; + layer_cpu->featmask = featmask; + +#if NCNN_VULKAN + if (layer_vulkan) + { + layer_vulkan->vkdev = vkdev; + + layer_vulkan->userdata = userdata; + + layer_vulkan->bottoms = bottoms; + layer_vulkan->tops = tops; + layer_vulkan->bottom_shapes = bottom_shapes; + layer_vulkan->top_shapes = top_shapes; + layer_vulkan->featmask = featmask; + } +#endif + } + + void get_layer_properties() + { + one_blob_only = layer_cpu->one_blob_only; + support_inplace = layer_cpu->support_inplace; + support_packing = layer_cpu->support_packing; + support_bf16_storage = layer_cpu->support_bf16_storage; + support_fp16_storage = layer_cpu->support_fp16_storage; + support_int8_storage = layer_cpu->support_int8_storage; + + support_vulkan = 0; + support_image_storage = 0; + support_tensor_storage = 0; + +#if NCNN_VULKAN + if (layer_vulkan && vkdev) + { + support_vulkan = layer_vulkan->support_vulkan; + support_image_storage = layer_vulkan->support_image_storage; + support_tensor_storage = layer_vulkan->support_tensor_storage; + } +#endif + } + +public: + Layer_final() + { + layer_cpu = 0; +#if NCNN_VULKAN + layer_vulkan = 0; +#endif + } + + ~Layer_final() + { + delete layer_cpu; +#if NCNN_VULKAN + delete layer_vulkan; +#endif + } + + virtual int load_param(const ParamDict& pd) + { + set_layer_properties(); + { + int ret = layer_cpu->load_param(pd); + if (ret) + return ret; + } +#if NCNN_VULKAN + if (layer_vulkan && vkdev) + { + int ret = layer_vulkan->load_param(pd); + if (ret) + return ret; + } +#endif // NCNN_VULKAN + get_layer_properties(); + return 0; + } + + virtual int load_model(const ModelBin& mb) + { + { + int ret = layer_cpu->load_model(mb); + if (ret) + return ret; + } +#if NCNN_VULKAN + if (layer_vulkan && vkdev) + { + int ret = layer_vulkan->load_model(mb); + if (ret) + return ret; + } +#endif // NCNN_VULKAN + get_layer_properties(); + return 0; + } + + virtual int create_pipeline(const Option& opt) + { + set_layer_properties(); + { + int ret = layer_cpu->create_pipeline(opt); + if (ret) + return ret; + } +#if NCNN_VULKAN + if (layer_vulkan && vkdev) + { + int ret = layer_vulkan->create_pipeline(opt); + if (ret) + return ret; + } +#endif // NCNN_VULKAN + get_layer_properties(); + return 0; + } + + virtual int destroy_pipeline(const Option& opt) + { + { + int ret = layer_cpu->destroy_pipeline(opt); + if (ret) + return ret; + } +#if NCNN_VULKAN + if (layer_vulkan && vkdev) + { + int ret = layer_vulkan->destroy_pipeline(opt); + if (ret) + return ret; + } +#endif // NCNN_VULKAN + return 0; + } + +public: + virtual int forward(const std::vector& bottom_blobs, std::vector& top_blobs, const Option& opt) const + { + return layer_cpu->forward(bottom_blobs, top_blobs, opt); + } + + virtual int forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const + { + return layer_cpu->forward(bottom_blob, top_blob, opt); + } + + virtual int forward_inplace(std::vector& bottom_top_blobs, const Option& opt) const + { + return layer_cpu->forward_inplace(bottom_top_blobs, opt); + } + + virtual int forward_inplace(Mat& bottom_top_blob, const Option& opt) const + { + return layer_cpu->forward_inplace(bottom_top_blob, opt); + } + +#if NCNN_VULKAN +public: + virtual int upload_model(VkTransfer& cmd, const Option& opt) + { + return layer_vulkan->upload_model(cmd, opt); + } + + virtual int forward(const std::vector& bottom_blobs, std::vector& top_blobs, VkCompute& cmd, const Option& opt) const + { + return layer_vulkan->forward(bottom_blobs, top_blobs, cmd, opt); + } + + virtual int forward(const VkMat& bottom_blob, VkMat& top_blob, VkCompute& cmd, const Option& opt) const + { + return layer_vulkan->forward(bottom_blob, top_blob, cmd, opt); + } + + virtual int forward(const std::vector& bottom_blobs, std::vector& top_blobs, VkCompute& cmd, const Option& opt) const + { + return layer_vulkan->forward(bottom_blobs, top_blobs, cmd, opt); + } + + virtual int forward(const VkImageMat& bottom_blob, VkImageMat& top_blob, VkCompute& cmd, const Option& opt) const + { + return layer_vulkan->forward(bottom_blob, top_blob, cmd, opt); + } + + virtual int forward_inplace(std::vector& bottom_top_blobs, VkCompute& cmd, const Option& opt) const + { + return layer_vulkan->forward_inplace(bottom_top_blobs, cmd, opt); + } + + virtual int forward_inplace(VkMat& bottom_top_blob, VkCompute& cmd, const Option& opt) const + { + return layer_vulkan->forward_inplace(bottom_top_blob, cmd, opt); + } + + virtual int forward_inplace(std::vector& bottom_top_blobs, VkCompute& cmd, const Option& opt) const + { + return layer_vulkan->forward_inplace(bottom_top_blobs, cmd, opt); + } + + virtual int forward_inplace(VkImageMat& bottom_top_blob, VkCompute& cmd, const Option& opt) const + { + return layer_vulkan->forward_inplace(bottom_top_blob, cmd, opt); + } +#endif // NCNN_VULKAN +}; + Layer* create_layer(int index) +{ + Layer* layer_cpu = create_layer_cpu(index); + if (!layer_cpu) + return 0; + + Layer_final* layer_final = new Layer_final; + layer_final->layer_cpu = layer_cpu; + +#if NCNN_VULKAN + layer_final->layer_vulkan = create_layer_vulkan(index); +#endif + + layer_final->typeindex = index; + layer_final->set_layer_properties(); + layer_final->get_layer_properties(); + + return layer_final; +} + +Layer* create_layer_cpu(int index) { if (index < 0 || index >= layer_registry_entry_count) return 0; @@ -293,4 +542,20 @@ Layer* create_layer(int index) return layer; } +#if NCNN_VULKAN +Layer* create_layer_vulkan(int index) +{ + if (index < 0 || index >= layer_registry_entry_count) + return 0; + + layer_creator_func layer_creator = layer_registry_vulkan[index].creator; + if (!layer_creator) + return 0; + + Layer* layer = layer_creator(0); + layer->typeindex = index; + return layer; +} +#endif // NCNN_VULKAN + } // namespace ncnn diff --git a/src/layer.h b/src/layer.h index 573f58cf94a..e04f606145b 100644 --- a/src/layer.h +++ b/src/layer.h @@ -199,9 +199,17 @@ struct overwrite_builtin_layer_registry_entry NCNN_EXPORT int layer_to_index(const char* type); // create layer from type name NCNN_EXPORT Layer* create_layer(const char* type); +NCNN_EXPORT Layer* create_layer_cpu(const char* type); +#if NCNN_VULKAN +NCNN_EXPORT Layer* create_layer_vulkan(const char* type); +#endif // NCNN_VULKAN #endif // NCNN_STRING // create layer from layer type NCNN_EXPORT Layer* create_layer(int index); +NCNN_EXPORT Layer* create_layer_cpu(int index); +#if NCNN_VULKAN +NCNN_EXPORT Layer* create_layer_vulkan(int index); +#endif // NCNN_VULKAN #define DEFINE_LAYER_CREATOR(name) \ ::ncnn::Layer* name##_layer_creator(void* /*userdata*/) \ diff --git a/src/layer/convolution.cpp b/src/layer/convolution.cpp index 4acf91869ae..bf83a69a100 100644 --- a/src/layer/convolution.cpp +++ b/src/layer/convolution.cpp @@ -95,17 +95,9 @@ int Convolution::load_model(const ModelBin& mb) } #endif // NCNN_INT8 - return 0; -} - -int Convolution::create_pipeline(const Option& opt) -{ - if (dynamic_weight) - return 0; - #if NCNN_INT8 // runtime quantize the weight data - if (opt.use_int8_inference && weight_data.elemsize == (size_t)4u && int8_scale_term) + if (weight_data.elemsize == (size_t)4u && int8_scale_term) { const int maxk = kernel_w * kernel_h; const int num_input = weight_data_size / num_output / maxk; @@ -114,7 +106,8 @@ int Convolution::create_pipeline(const Option& opt) Mat weight_data_int8; - Option opt_q = opt; + Option opt_q; + opt_q.num_threads = 1; opt_q.blob_allocator = weight_data.allocator; opt_q.use_packing_layout = false; quantize_to_int8(weight_data_r2, weight_data_int8, weight_data_int8_scales, opt_q); diff --git a/src/layer/convolution.h b/src/layer/convolution.h index 476a7aaf67b..7af0735fd30 100644 --- a/src/layer/convolution.h +++ b/src/layer/convolution.h @@ -28,8 +28,6 @@ class Convolution : public Layer virtual int load_model(const ModelBin& mb); - virtual int create_pipeline(const Option& opt); - virtual int forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const; virtual int forward(const std::vector& bottom_blobs, std::vector& top_blobs, const Option& opt) const; diff --git a/src/layer/convolution1d.cpp b/src/layer/convolution1d.cpp index 184b2bdb60d..7d6be1e111e 100644 --- a/src/layer/convolution1d.cpp +++ b/src/layer/convolution1d.cpp @@ -67,14 +67,6 @@ int Convolution1D::load_model(const ModelBin& mb) return 0; } -int Convolution1D::create_pipeline(const Option&) -{ - if (dynamic_weight) - return 0; - - return 0; -} - static int convolution1d(const Mat& bottom_blob, Mat& top_blob, const Mat& weight_data, const Mat& bias_data, int kernel_w, int stride_w, int dilation_w, int activation_type, const Mat& activation_params, const Option& opt) { const int h = bottom_blob.h; diff --git a/src/layer/convolution1d.h b/src/layer/convolution1d.h index e30807e5c9b..d87099e25f2 100644 --- a/src/layer/convolution1d.h +++ b/src/layer/convolution1d.h @@ -28,8 +28,6 @@ class Convolution1D : public Layer virtual int load_model(const ModelBin& mb); - virtual int create_pipeline(const Option& opt); - virtual int forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const; virtual int forward(const std::vector& bottom_blobs, std::vector& top_blobs, const Option& opt) const; diff --git a/src/layer/convolutiondepthwise.cpp b/src/layer/convolutiondepthwise.cpp index e820a192cb3..fb8e1e5c0b2 100644 --- a/src/layer/convolutiondepthwise.cpp +++ b/src/layer/convolutiondepthwise.cpp @@ -124,14 +124,9 @@ int ConvolutionDepthWise::load_model(const ModelBin& mb) } #endif // NCNN_INT8 - return 0; -} - -int ConvolutionDepthWise::create_pipeline(const Option& opt) -{ #if NCNN_INT8 // runtime quantize the weight data - if (opt.use_int8_inference && weight_data.elemsize == (size_t)4u && int8_scale_term) + if (weight_data.elemsize == (size_t)4u && int8_scale_term) { Mat int8_weight_data(weight_data_size, (size_t)1u); if (int8_weight_data.empty()) @@ -141,7 +136,8 @@ int ConvolutionDepthWise::create_pipeline(const Option& opt) for (int g = 0; g < group; g++) { - Option opt_q = opt; + Option opt_q; + opt_q.num_threads = 1; opt_q.blob_allocator = int8_weight_data.allocator; opt_q.use_packing_layout = false; @@ -153,8 +149,6 @@ int ConvolutionDepthWise::create_pipeline(const Option& opt) weight_data = int8_weight_data; } -#else - (void)(opt); #endif // NCNN_INT8 return 0; diff --git a/src/layer/convolutiondepthwise.h b/src/layer/convolutiondepthwise.h index e893aa07fc9..8a955dbd23b 100644 --- a/src/layer/convolutiondepthwise.h +++ b/src/layer/convolutiondepthwise.h @@ -28,8 +28,6 @@ class ConvolutionDepthWise : public Layer virtual int load_model(const ModelBin& mb); - virtual int create_pipeline(const Option& opt); - virtual int forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const; virtual int forward(const std::vector& bottom_blobs, std::vector& top_blobs, const Option& opt) const; diff --git a/src/layer/convolutiondepthwise1d.cpp b/src/layer/convolutiondepthwise1d.cpp index 79c83168051..61fddf881f7 100644 --- a/src/layer/convolutiondepthwise1d.cpp +++ b/src/layer/convolutiondepthwise1d.cpp @@ -73,11 +73,6 @@ int ConvolutionDepthWise1D::load_model(const ModelBin& mb) return 0; } -int ConvolutionDepthWise1D::create_pipeline(const Option&) -{ - return 0; -} - static int convolutiondepthwise1d(const Mat& bottom_blob, Mat& top_blob, const Mat& weight_data, const Mat& bias_data, int kernel_w, int stride_w, int dilation_w, int group, int activation_type, const Mat& activation_params, const Option& opt) { const int h = bottom_blob.h; diff --git a/src/layer/convolutiondepthwise1d.h b/src/layer/convolutiondepthwise1d.h index e2c195dc489..6026f04981d 100644 --- a/src/layer/convolutiondepthwise1d.h +++ b/src/layer/convolutiondepthwise1d.h @@ -28,8 +28,6 @@ class ConvolutionDepthWise1D : public Layer virtual int load_model(const ModelBin& mb); - virtual int create_pipeline(const Option& opt); - virtual int forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const; virtual int forward(const std::vector& bottom_blobs, std::vector& top_blobs, const Option& opt) const; diff --git a/src/layer/innerproduct.cpp b/src/layer/innerproduct.cpp index 4cc22981c34..9cb422d21b6 100644 --- a/src/layer/innerproduct.cpp +++ b/src/layer/innerproduct.cpp @@ -69,21 +69,17 @@ int InnerProduct::load_model(const ModelBin& mb) } #endif // NCNN_INT8 - return 0; -} - -int InnerProduct::create_pipeline(const Option& opt) -{ #if NCNN_INT8 // runtime quantize the weight data - if (opt.use_int8_inference && weight_data.elemsize == (size_t)4u && int8_scale_term) + if (weight_data.elemsize == (size_t)4u && int8_scale_term) { const int num_input = weight_data_size / num_output; Mat weight_data_r2 = weight_data.reshape(num_input, num_output); Mat weight_data_int8; - Option opt_q = opt; + Option opt_q; + opt_q.num_threads = 1; opt_q.use_packing_layout = false; quantize_to_int8(weight_data_r2, weight_data_int8, weight_data_int8_scales, opt_q); if (weight_data_int8.empty()) @@ -91,8 +87,6 @@ int InnerProduct::create_pipeline(const Option& opt) weight_data = weight_data_int8.reshape(weight_data_size); } -#else - (void)(opt); #endif // NCNN_INT8 return 0; diff --git a/src/layer/innerproduct.h b/src/layer/innerproduct.h index 1f9b3fdc0a5..becf7b1d01a 100644 --- a/src/layer/innerproduct.h +++ b/src/layer/innerproduct.h @@ -28,8 +28,6 @@ class InnerProduct : public Layer virtual int load_model(const ModelBin& mb); - virtual int create_pipeline(const Option& opt); - virtual int forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const; protected: diff --git a/src/layer/noop.cpp b/src/layer/noop.cpp index 68572b0ba28..a8b42f70e83 100644 --- a/src/layer/noop.cpp +++ b/src/layer/noop.cpp @@ -20,11 +20,9 @@ namespace ncnn { Noop::Noop() { support_inplace = true; - support_vulkan = true; support_packing = true; support_fp16_storage = cpu_support_arm_asimdhp() || cpu_support_riscv_zfh(); support_bf16_storage = true; - support_image_storage = true; } int Noop::forward_inplace(std::vector& /*bottom_top_blobs*/, const Option& /*opt*/) const @@ -32,16 +30,4 @@ int Noop::forward_inplace(std::vector& /*bottom_top_blobs*/, const Option& return 0; } -#if NCNN_VULKAN -int Noop::forward_inplace(std::vector& /*bottom_top_blobs*/, VkCompute& /*cmd*/, const Option& /*opt*/) const -{ - return 0; -} - -int Noop::forward_inplace(std::vector& /*bottom_top_blobs*/, VkCompute& /*cmd*/, const Option& /*opt*/) const -{ - return 0; -} -#endif // NCNN_VULKAN - } // namespace ncnn diff --git a/src/layer/noop.h b/src/layer/noop.h index 1fb7af35c08..75bbdd1a308 100644 --- a/src/layer/noop.h +++ b/src/layer/noop.h @@ -25,11 +25,6 @@ class Noop : public Layer Noop(); virtual int forward_inplace(std::vector& bottom_top_blobs, const Option& opt) const; - -#if NCNN_VULKAN - virtual int forward_inplace(std::vector& bottom_top_blobs, VkCompute& cmd, const Option& opt) const; - virtual int forward_inplace(std::vector& bottom_top_blobs, VkCompute& cmd, const Option& opt) const; -#endif // NCNN_VULKAN }; } // namespace ncnn diff --git a/src/layer/split.cpp b/src/layer/split.cpp index f79fce0f15c..996624dfe7a 100644 --- a/src/layer/split.cpp +++ b/src/layer/split.cpp @@ -21,11 +21,9 @@ Split::Split() { one_blob_only = false; support_inplace = false; - support_vulkan = true; support_packing = true; support_fp16_storage = cpu_support_arm_asimdhp() || cpu_support_riscv_zfh(); support_bf16_storage = true; - support_image_storage = true; } int Split::forward(const std::vector& bottom_blobs, std::vector& top_blobs, const Option& /*opt*/) const @@ -39,28 +37,4 @@ int Split::forward(const std::vector& bottom_blobs, std::vector& top_b return 0; } -#if NCNN_VULKAN -int Split::forward(const std::vector& bottom_blobs, std::vector& top_blobs, VkCompute& /*cmd*/, const Option& /*opt*/) const -{ - const VkMat& bottom_blob = bottom_blobs[0]; - for (size_t i = 0; i < top_blobs.size(); i++) - { - top_blobs[i] = bottom_blob; - } - - return 0; -} - -int Split::forward(const std::vector& bottom_blobs, std::vector& top_blobs, VkCompute& /*cmd*/, const Option& /*opt*/) const -{ - const VkImageMat& bottom_blob = bottom_blobs[0]; - for (size_t i = 0; i < top_blobs.size(); i++) - { - top_blobs[i] = bottom_blob; - } - - return 0; -} -#endif // NCNN_VULKAN - } // namespace ncnn diff --git a/src/layer/split.h b/src/layer/split.h index 7437866cfc5..53686f82be3 100644 --- a/src/layer/split.h +++ b/src/layer/split.h @@ -25,13 +25,6 @@ class Split : public Layer Split(); virtual int forward(const std::vector& bottom_blobs, std::vector& top_blobs, const Option& opt) const; - -#if NCNN_VULKAN - virtual int forward(const std::vector& bottom_blobs, std::vector& top_blobs, VkCompute& cmd, const Option& opt) const; - virtual int forward(const std::vector& bottom_blobs, std::vector& top_blobs, VkCompute& cmd, const Option& opt) const; -#endif // NCNN_VULKAN - -public: }; } // namespace ncnn diff --git a/src/layer/vulkan/noop_vulkan.cpp b/src/layer/vulkan/noop_vulkan.cpp new file mode 100644 index 00000000000..3a59d2613a3 --- /dev/null +++ b/src/layer/vulkan/noop_vulkan.cpp @@ -0,0 +1,35 @@ +// Tencent is pleased to support the open source community by making ncnn available. +// +// Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved. +// +// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// https://opensource.org/licenses/BSD-3-Clause +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#include "noop_vulkan.h" + +namespace ncnn { + +Noop_vulkan::Noop_vulkan() +{ + support_vulkan = true; + support_image_storage = true; +} + +int Noop_vulkan::forward_inplace(std::vector& /*bottom_top_blobs*/, VkCompute& /*cmd*/, const Option& /*opt*/) const +{ + return 0; +} + +int Noop_vulkan::forward_inplace(std::vector& /*bottom_top_blobs*/, VkCompute& /*cmd*/, const Option& /*opt*/) const +{ + return 0; +} + +} // namespace ncnn diff --git a/src/layer/vulkan/noop_vulkan.h b/src/layer/vulkan/noop_vulkan.h new file mode 100644 index 00000000000..a26cf626ab6 --- /dev/null +++ b/src/layer/vulkan/noop_vulkan.h @@ -0,0 +1,34 @@ +// Tencent is pleased to support the open source community by making ncnn available. +// +// Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved. +// +// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// https://opensource.org/licenses/BSD-3-Clause +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef LAYER_NOOP_VULKAN_H +#define LAYER_NOOP_VULKAN_H + +#include "noop.h" + +namespace ncnn { + +class Noop_vulkan : virtual public Noop +{ +public: + Noop_vulkan(); + + using Noop::forward; + virtual int forward_inplace(std::vector& bottom_top_blobs, VkCompute& cmd, const Option& opt) const; + virtual int forward_inplace(std::vector& bottom_top_blobs, VkCompute& cmd, const Option& opt) const; +}; + +} // namespace ncnn + +#endif // LAYER_NOOP_VULKAN_H diff --git a/src/layer/vulkan/split_vulkan.cpp b/src/layer/vulkan/split_vulkan.cpp new file mode 100644 index 00000000000..791069cc7d7 --- /dev/null +++ b/src/layer/vulkan/split_vulkan.cpp @@ -0,0 +1,47 @@ +// Tencent is pleased to support the open source community by making ncnn available. +// +// Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved. +// +// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// https://opensource.org/licenses/BSD-3-Clause +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#include "split_vulkan.h" + +namespace ncnn { + +Split_vulkan::Split_vulkan() +{ + support_vulkan = true; + support_image_storage = true; +} + +int Split_vulkan::forward(const std::vector& bottom_blobs, std::vector& top_blobs, VkCompute& /*cmd*/, const Option& /*opt*/) const +{ + const VkMat& bottom_blob = bottom_blobs[0]; + for (size_t i = 0; i < top_blobs.size(); i++) + { + top_blobs[i] = bottom_blob; + } + + return 0; +} + +int Split_vulkan::forward(const std::vector& bottom_blobs, std::vector& top_blobs, VkCompute& /*cmd*/, const Option& /*opt*/) const +{ + const VkImageMat& bottom_blob = bottom_blobs[0]; + for (size_t i = 0; i < top_blobs.size(); i++) + { + top_blobs[i] = bottom_blob; + } + + return 0; +} + +} // namespace ncnn diff --git a/src/layer/vulkan/split_vulkan.h b/src/layer/vulkan/split_vulkan.h new file mode 100644 index 00000000000..b5ace0cb2ce --- /dev/null +++ b/src/layer/vulkan/split_vulkan.h @@ -0,0 +1,34 @@ +// Tencent is pleased to support the open source community by making ncnn available. +// +// Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved. +// +// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// https://opensource.org/licenses/BSD-3-Clause +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef LAYER_SPLIT_VULKAN_H +#define LAYER_SPLIT_VULKAN_H + +#include "split.h" + +namespace ncnn { + +class Split_vulkan : virtual public Split +{ +public: + Split_vulkan(); + + using Split::forward; + virtual int forward(const std::vector& bottom_blobs, std::vector& top_blobs, VkCompute& cmd, const Option& opt) const; + virtual int forward(const std::vector& bottom_blobs, std::vector& top_blobs, VkCompute& cmd, const Option& opt) const; +}; + +} // namespace ncnn + +#endif // LAYER_SPLIT_VULKAN_H diff --git a/src/layer_registry.h.in b/src/layer_registry.h.in index 52393b498e6..4b6398d0ced 100644 --- a/src/layer_registry.h.in +++ b/src/layer_registry.h.in @@ -11,11 +11,13 @@ static const layer_registry_entry layer_registry_avx512[] = { @layer_registry_avx512@ }; #endif // NCNN_RUNTIME_CPU && NCNN_AVX512 + #if NCNN_RUNTIME_CPU && NCNN_FMA static const layer_registry_entry layer_registry_fma[] = { @layer_registry_fma@ }; #endif // NCNN_RUNTIME_CPU && NCNN_FMA + #if NCNN_RUNTIME_CPU && NCNN_AVX static const layer_registry_entry layer_registry_avx[] = { @layer_registry_avx@ @@ -45,3 +47,9 @@ static const layer_registry_entry layer_registry_rvv[] = { @layer_registry_rvv@ }; #endif // NCNN_RUNTIME_CPU && NCNN_RVV + +#if NCNN_VULKAN +static const layer_registry_entry layer_registry_vulkan[] = { +@layer_registry_vulkan@ +}; +#endif // NCNN_VULKAN