Skip to content

Commit 7a8f68a

Browse files
committed
move vulkan code to subdir, new layer interface create_pipeline and destroy_pipeline for post-loading works
1 parent d46c598 commit 7a8f68a

File tree

266 files changed

+9027
-7430
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

266 files changed

+9027
-7430
lines changed

benchmark/benchncnn.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ class BenchNet : public Net
5757
// load file
5858
int ret = 0;
5959

60+
Option opt;
61+
opt.vulkan_compute = use_vulkan_compute;
62+
6063
ModelBinFromEmpty mb;
6164
for (size_t i=0; i<layers.size(); i++)
6265
{
@@ -69,6 +72,14 @@ class BenchNet : public Net
6972
ret = -1;
7073
break;
7174
}
75+
76+
int cret = layer->create_pipeline(opt);
77+
if (cret != 0)
78+
{
79+
fprintf(stderr, "layer create_pipeline %d failed\n", (int)i);
80+
ret = -1;
81+
break;
82+
}
7283
}
7384

7485
#if NCNN_VULKAN

src/CMakeLists.txt

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,36 +55,43 @@ macro(ncnn_add_layer class)
5555
else()
5656
set(arch x86)
5757
endif()
58-
set(LAYER_SRC ${CMAKE_CURRENT_SOURCE_DIR}/layer/${arch}/${name}_${arch}.cpp)
59-
if(EXISTS ${LAYER_SRC})
58+
59+
set(LAYER_ARCH_SRC ${CMAKE_CURRENT_SOURCE_DIR}/layer/${arch}/${name}_${arch}.cpp)
60+
if(EXISTS ${LAYER_ARCH_SRC})
6061
set(WITH_LAYER_${name}_${arch} 1)
61-
list(APPEND ncnn_SRCS ${LAYER_SRC})
62-
if(NCNN_CMAKE_VERBOSE)
63-
message(STATUS "Adding layer: ${LAYER_SRC}")
64-
endif()
62+
list(APPEND ncnn_SRCS ${LAYER_ARCH_SRC})
63+
endif()
64+
65+
set(LAYER_VULKAN_SRC ${CMAKE_CURRENT_SOURCE_DIR}/layer/vulkan/${name}_vulkan.cpp)
66+
if(NCNN_VULKAN AND EXISTS ${LAYER_VULKAN_SRC})
67+
set(WITH_LAYER_${name}_vulkan 1)
68+
list(APPEND ncnn_SRCS ${LAYER_VULKAN_SRC})
6569
endif()
6670
endif()
6771

6872
# generate layer_declaration and layer_registry file
6973
if(WITH_LAYER_${name})
70-
if(WITH_LAYER_${name}_${arch})
71-
string(APPEND layer_declaration
72-
"extern Layer* ${class}_${arch}_layer_creator();\n")
73-
string(APPEND layer_registry
74-
"#if NCNN_STRING\n{\"${class}\",${class}_${arch}_layer_creator},\n#else\n{${class}_${arch}_layer_creator},\n#endif\n")
75-
else()
76-
string(APPEND layer_declaration
77-
"extern Layer* ${class}_layer_creator();\n")
78-
string(APPEND layer_registry
79-
"#if NCNN_STRING\n{\"${class}\",${class}_layer_creator},\n#else\n{${class}_layer_creator},\n#endif\n")
80-
endif()
81-
else()
82-
string(APPEND layer_registry "#if NCNN_STRING\n{\"${class}\",0},\n#else\n{0},\n#endif\n")
74+
set(layer_declaration "${layer_declaration}#include \"layer/${name}.h\"\n")
75+
set(layer_declaration_class "class ${class}_final : virtual public ${class}")
76+
set(create_pipeline_content " { int ret = ${class}::create_pipeline(opt); if (ret) return ret; }\n")
77+
set(destroy_pipeline_content " { int ret = ${class}::destroy_pipeline(opt); if (ret) return ret; }\n")
78+
endif()
79+
80+
if(WITH_LAYER_${name}_${arch})
81+
set(layer_declaration "${layer_declaration}#include \"layer/${arch}/${name}_${arch}.h\"\n")
82+
set(layer_declaration_class "${layer_declaration_class}, virtual public ${class}_${arch}")
83+
set(create_pipeline_content "${create_pipeline_content} { int ret = ${class}_${arch}::create_pipeline(opt); if (ret) return ret; }\n")
84+
set(destroy_pipeline_content " { int ret = ${class}_${arch}::destroy_pipeline(opt); if (ret) return ret; }\n${destroy_pipeline_content}")
8385
endif()
8486

85-
if(WITH_LAYER_${name} AND NCNN_VULKAN)
86-
file(GLOB_RECURSE SHADER_SRCS "layer/shader/${name}.comp")
87-
file(GLOB_RECURSE SHADER_SUBSRCS "layer/shader/${name}_*.comp")
87+
if(WITH_LAYER_${name}_vulkan)
88+
set(layer_declaration "${layer_declaration}#include \"layer/vulkan/${name}_vulkan.h\"\n")
89+
set(layer_declaration_class "${layer_declaration_class}, virtual public ${class}_vulkan")
90+
set(create_pipeline_content "${create_pipeline_content} if (opt.vulkan_compute) { int ret = ${class}_vulkan::create_pipeline(opt); if (ret) return ret; }\n")
91+
set(destroy_pipeline_content " if (opt.vulkan_compute) { int ret = ${class}_vulkan::destroy_pipeline(opt); if (ret) return ret; }\n${destroy_pipeline_content}")
92+
93+
file(GLOB_RECURSE SHADER_SRCS "layer/vulkan/shader/${name}.comp")
94+
file(GLOB_RECURSE SHADER_SUBSRCS "layer/vulkan/shader/${name}_*.comp")
8895
list(APPEND SHADER_SRCS ${SHADER_SUBSRCS})
8996
foreach(SHADER_SRC ${SHADER_SRCS})
9097
get_filename_component(SHADER_SRC_NAME_WE ${SHADER_SRC} NAME_WE)
@@ -145,6 +152,21 @@ macro(ncnn_add_layer class)
145152
endforeach()
146153
endif()
147154

155+
if(WITH_LAYER_${name})
156+
set(layer_declaration "${layer_declaration}namespace ncnn {\n${layer_declaration_class}\n{\n")
157+
set(layer_declaration "${layer_declaration}public:\n")
158+
set(layer_declaration "${layer_declaration} virtual int create_pipeline(const Option& opt) {\n${create_pipeline_content} return 0;\n }\n")
159+
set(layer_declaration "${layer_declaration} virtual int destroy_pipeline(const Option& opt) {\n${destroy_pipeline_content} return 0;\n }\n")
160+
set(layer_declaration "${layer_declaration}};\n")
161+
set(layer_declaration "${layer_declaration}DEFINE_LAYER_CREATOR(${class}_final)\n} // namespace ncnn\n\n")
162+
endif()
163+
164+
if(WITH_LAYER_${name})
165+
set(layer_registry "${layer_registry}#if NCNN_STRING\n{\"${class}\",${class}_final_layer_creator},\n#else\n{${class}_final_layer_creator},\n#endif\n")
166+
else()
167+
set(layer_registry "${layer_registry}#if NCNN_STRING\n{\"${class}\",0},\n#else\n{0},\n#endif\n")
168+
endif()
169+
148170
# generate layer_type_enum file
149171
string(APPEND layer_type_enum "${class} = ${__LAYER_TYPE_ENUM_INDEX},\n")
150172
math(EXPR __LAYER_TYPE_ENUM_INDEX "${__LAYER_TYPE_ENUM_INDEX}+1")

src/layer.cpp

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
#include <algorithm>
2121
#include "cpu.h"
2222

23+
#pragma clang diagnostic push
24+
#pragma clang diagnostic ignored "-Woverloaded-virtual"
25+
#include "layer_declaration.h"
26+
#pragma clang diagnostic pop
27+
2328
namespace ncnn {
2429

2530
Option::Option()
@@ -29,12 +34,17 @@ Option::Option()
2934
blob_allocator = 0;
3035
workspace_allocator = 0;
3136

32-
#if NCNN_VULKAN
3337
vulkan_compute = false;
38+
39+
#if NCNN_VULKAN
3440
blob_vkallocator = 0;
3541
workspace_vkallocator = 0;
3642
staging_vkallocator = 0;
3743
#endif // NCNN_VULKAN
44+
45+
use_winograd_convolution = 1;
46+
use_sgemm_convolution = 1;
47+
use_int8_inference = 1;
3848
}
3949

4050
static Option g_default_option;
@@ -82,6 +92,16 @@ int Layer::load_model(const ModelBin& /*mb*/)
8292
return 0;
8393
}
8494

95+
int Layer::create_pipeline(const Option& /*opt*/)
96+
{
97+
return 0;
98+
}
99+
100+
int Layer::destroy_pipeline(const Option& /*opt*/)
101+
{
102+
return 0;
103+
}
104+
85105
int Layer::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
86106
{
87107
if (!support_inplace)
@@ -126,16 +146,6 @@ int Layer::upload_model(VkTransfer& /*cmd*/)
126146
return 0;
127147
}
128148

129-
int Layer::create_pipeline()
130-
{
131-
return 0;
132-
}
133-
134-
int Layer::destroy_pipeline()
135-
{
136-
return 0;
137-
}
138-
139149
int Layer::forward(const std::vector<VkMat>& bottom_blobs, std::vector<VkMat>& top_blobs, VkCompute& cmd, const Option& opt) const
140150
{
141151
if (!support_inplace)
@@ -179,8 +189,6 @@ int Layer::forward_inplace(VkMat& /*bottom_top_blob*/, VkCompute& /*cmd*/, const
179189
}
180190
#endif // NCNN_VULKAN
181191

182-
#include "layer_declaration.h"
183-
184192
static const layer_registry_entry layer_registry[] =
185193
{
186194
#include "layer_registry.h"

src/layer.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ class Option
5959
// workspace memory allocator
6060
Allocator* workspace_allocator;
6161

62-
#if NCNN_VULKAN
6362
// enable vulkan compute
6463
bool vulkan_compute;
6564

65+
#if NCNN_VULKAN
6666
// blob memory allocator
6767
VkAllocator* blob_vkallocator;
6868

@@ -72,6 +72,11 @@ class Option
7272
// staging memory allocator
7373
VkAllocator* staging_vkallocator;
7474
#endif // NCNN_VULKAN
75+
76+
public:
77+
int use_winograd_convolution;
78+
int use_sgemm_convolution;
79+
int use_int8_inference;
7580
};
7681

7782
// the global default option
@@ -94,6 +99,12 @@ class Layer
9499
// return 0 if success
95100
virtual int load_model(const ModelBin& mb);
96101

102+
//
103+
virtual int create_pipeline(const Option& opt = get_default_option());
104+
105+
//
106+
virtual int destroy_pipeline(const Option& opt = get_default_option());
107+
97108
public:
98109
// one input and one output blob
99110
bool one_blob_only;
@@ -120,9 +131,6 @@ class Layer
120131
// upload weight blob from host to device
121132
virtual int upload_model(VkTransfer& cmd);
122133

123-
virtual int create_pipeline();
124-
virtual int destroy_pipeline();
125-
126134
public:
127135
// implement inference
128136
// return 0 if success

src/layer/absval.cpp

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@ AbsVal::AbsVal()
2222
{
2323
one_blob_only = true;
2424
support_inplace = true;
25-
support_vulkan = true;
26-
27-
#if NCNN_VULKAN
28-
pipeline_absval = 0;
29-
pipeline_absval_pack4 = 0;
30-
#endif // NCNN_VULKAN
3125
}
3226

3327
int AbsVal::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
@@ -52,62 +46,4 @@ int AbsVal::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
5246
return 0;
5347
}
5448

55-
#if NCNN_VULKAN
56-
int AbsVal::create_pipeline()
57-
{
58-
std::vector<vk_specialization_type> specializations;
59-
60-
// pack1
61-
{
62-
pipeline_absval = new Pipeline(vkdev);
63-
pipeline_absval->set_optimal_local_size_xyz();
64-
pipeline_absval->create("absval", specializations, 1, 5);
65-
}
66-
67-
// pack4
68-
{
69-
pipeline_absval_pack4 = new Pipeline(vkdev);
70-
pipeline_absval_pack4->set_optimal_local_size_xyz();
71-
pipeline_absval_pack4->create("absval_pack4", specializations, 1, 5);
72-
}
73-
74-
return 0;
75-
}
76-
77-
int AbsVal::destroy_pipeline()
78-
{
79-
delete pipeline_absval;
80-
pipeline_absval = 0;
81-
82-
delete pipeline_absval_pack4;
83-
pipeline_absval_pack4 = 0;
84-
85-
return 0;
86-
}
87-
88-
int AbsVal::forward_inplace(VkMat& bottom_top_blob, VkCompute& cmd, const Option& opt) const
89-
{
90-
int packing = bottom_top_blob.packing;
91-
92-
// fprintf(stderr, "AbsVal::forward_inplace %p\n", bottom_top_blob.buffer());
93-
94-
std::vector<VkMat> bindings(1);
95-
bindings[0] = bottom_top_blob;
96-
97-
std::vector<vk_constant_type> constants(5);
98-
constants[0].i = bottom_top_blob.dims;
99-
constants[1].i = bottom_top_blob.w;
100-
constants[2].i = bottom_top_blob.h;
101-
constants[3].i = bottom_top_blob.c;
102-
constants[4].i = bottom_top_blob.cstep;
103-
104-
const Pipeline* pipeline = packing == 4 ? pipeline_absval_pack4 : pipeline_absval;
105-
106-
// record
107-
cmd.record_pipeline(pipeline, bindings, constants, bottom_top_blob);
108-
109-
return 0;
110-
}
111-
#endif // NCNN_VULKAN
112-
11349
} // namespace ncnn

src/layer/absval.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,6 @@ class AbsVal : public Layer
2525
AbsVal();
2626

2727
virtual int forward_inplace(Mat& bottom_top_blob, const Option& opt) const;
28-
29-
#if NCNN_VULKAN
30-
virtual int create_pipeline();
31-
virtual int destroy_pipeline();
32-
33-
virtual int forward_inplace(VkMat& bottom_top_blob, VkCompute& cmd, const Option& opt) const;
34-
#endif // NCNN_VULKAN
35-
36-
public:
37-
38-
#if NCNN_VULKAN
39-
Pipeline* pipeline_absval;
40-
Pipeline* pipeline_absval_pack4;
41-
#endif // NCNN_VULKAN
4228
};
4329

4430
} // namespace ncnn

src/layer/arm/absval_arm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
namespace ncnn {
2121

22-
class AbsVal_arm : public AbsVal
22+
class AbsVal_arm : virtual public AbsVal
2323
{
2424
public:
2525
virtual int forward_inplace(Mat& bottom_top_blob, const Option& opt) const;

src/layer/arm/batchnorm_arm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
namespace ncnn {
2121

22-
class BatchNorm_arm : public BatchNorm
22+
class BatchNorm_arm : virtual public BatchNorm
2323
{
2424
public:
2525
virtual int forward_inplace(Mat& bottom_top_blob, const Option& opt) const;

src/layer/arm/bias_arm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
namespace ncnn {
2121

22-
class Bias_arm : public Bias
22+
class Bias_arm : virtual public Bias
2323
{
2424
public:
2525
virtual int forward_inplace(Mat& bottom_top_blob, const Option& opt) const;

src/layer/arm/clip_arm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
namespace ncnn {
2121

22-
class Clip_arm : public Clip
22+
class Clip_arm : virtual public Clip
2323
{
2424
public:
2525
virtual int forward_inplace(Mat& bottom_top_blob, const Option& opt) const;

0 commit comments

Comments
 (0)