| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2017 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 | #ifndef NCNN_LAYER_H |
| 16 | #define NCNN_LAYER_H |
| 17 | |
| 18 | #include "mat.h" |
| 19 | #include "modelbin.h" |
| 20 | #include "option.h" |
| 21 | #include "paramdict.h" |
| 22 | #include "platform.h" |
| 23 | |
| 24 | #if NCNN_VULKAN |
| 25 | #include "command.h" |
| 26 | #include "pipeline.h" |
| 27 | #endif // NCNN_VULKAN |
| 28 | |
| 29 | namespace ncnn { |
| 30 | |
| 31 | class NCNN_EXPORT Layer |
| 32 | { |
| 33 | public: |
| 34 | // empty |
| 35 | Layer(); |
| 36 | // virtual destructor |
| 37 | virtual ~Layer(); |
| 38 | |
| 39 | // load layer specific parameter from parsed dict |
| 40 | // return 0 if success |
| 41 | virtual int load_param(const ParamDict& pd); |
| 42 | |
| 43 | // load layer specific weight data from model binary |
| 44 | // return 0 if success |
| 45 | virtual int load_model(const ModelBin& mb); |
| 46 | |
| 47 | // layer implementation specific setup |
| 48 | // return 0 if success |
| 49 | virtual int create_pipeline(const Option& opt); |
| 50 | |
| 51 | // layer implementation specific clean |
| 52 | // return 0 if success |
| 53 | virtual int destroy_pipeline(const Option& opt); |
| 54 | |
| 55 | public: |
| 56 | // one input and one output blob |
| 57 | bool one_blob_only; |
| 58 | |
| 59 | // support inplace inference |
| 60 | bool support_inplace; |
| 61 | |
| 62 | // support vulkan compute |
| 63 | bool support_vulkan; |
| 64 | |
| 65 | // accept input blob with packed storage |
| 66 | bool support_packing; |
| 67 | |
| 68 | // accept bf16 |
| 69 | bool support_bf16_storage; |
| 70 | |
| 71 | // accept fp16 |
| 72 | bool support_fp16_storage; |
| 73 | |
| 74 | // accept int8 |
| 75 | bool support_int8_storage; |
| 76 | |
| 77 | // shader image storage |
| 78 | bool support_image_storage; |
| 79 | |
| 80 | // shader tensor storage |
| 81 | bool support_tensor_storage; |
| 82 | |
| 83 | bool support_reserved_00; |
| 84 | |
| 85 | bool support_reserved_0; |
| 86 | bool support_reserved_1; |
| 87 | bool support_reserved_2; |
| 88 | bool support_reserved_3; |
| 89 | bool support_reserved_4; |
| 90 | bool support_reserved_5; |
| 91 | bool support_reserved_6; |
| 92 | bool support_reserved_7; |
| 93 | bool support_reserved_8; |
| 94 | bool support_reserved_9; |
| 95 | |
| 96 | // feature disabled set |
| 97 | int featmask; |
| 98 | |
| 99 | public: |
| 100 | // implement inference |
| 101 | // return 0 if success |
| 102 | virtual int forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const; |
| 103 | virtual int forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const; |
| 104 | |
| 105 | // implement inplace inference |
| 106 | // return 0 if success |
| 107 | virtual int forward_inplace(std::vector<Mat>& bottom_top_blobs, const Option& opt) const; |
| 108 | virtual int forward_inplace(Mat& bottom_top_blob, const Option& opt) const; |
| 109 | |
| 110 | #if NCNN_VULKAN |
| 111 | public: |
| 112 | // upload weight blob from host to device |
| 113 | virtual int upload_model(VkTransfer& cmd, const Option& opt); |
| 114 | |
| 115 | public: |
| 116 | // implement inference |
| 117 | // return 0 if success |
| 118 | virtual int forward(const std::vector<VkMat>& bottom_blobs, std::vector<VkMat>& top_blobs, VkCompute& cmd, const Option& opt) const; |
| 119 | virtual int forward(const VkMat& bottom_blob, VkMat& top_blob, VkCompute& cmd, const Option& opt) const; |
| 120 | |
| 121 | // implement inference |
| 122 | // return 0 if success |
| 123 | virtual int forward(const std::vector<VkImageMat>& bottom_blobs, std::vector<VkImageMat>& top_blobs, VkCompute& cmd, const Option& opt) const; |
| 124 | virtual int forward(const VkImageMat& bottom_blob, VkImageMat& top_blob, VkCompute& cmd, const Option& opt) const; |
| 125 | |
| 126 | // implement inplace inference |
| 127 | // return 0 if success |
| 128 | virtual int forward_inplace(std::vector<VkMat>& bottom_top_blobs, VkCompute& cmd, const Option& opt) const; |
| 129 | virtual int forward_inplace(VkMat& bottom_top_blob, VkCompute& cmd, const Option& opt) const; |
| 130 | |
| 131 | // implement inplace inference |
| 132 | // return 0 if success |
| 133 | virtual int forward_inplace(std::vector<VkImageMat>& bottom_top_blobs, VkCompute& cmd, const Option& opt) const; |
| 134 | virtual int forward_inplace(VkImageMat& bottom_top_blob, VkCompute& cmd, const Option& opt) const; |
| 135 | |
| 136 | public: |
| 137 | // assigned immediately after creating this layer |
| 138 | const VulkanDevice* vkdev; |
| 139 | #endif // NCNN_VULKAN |
| 140 | |
| 141 | public: |
| 142 | // custom user data |
| 143 | void* userdata; |
| 144 | // layer type index |
| 145 | int typeindex; |
| 146 | #if NCNN_STRING |
| 147 | // layer type name |
| 148 | std::string type; |
| 149 | // layer name |
| 150 | std::string name; |
| 151 | #endif // NCNN_STRING |
| 152 | // blob index which this layer needs as input |
| 153 | std::vector<int> bottoms; |
| 154 | // blob index which this layer produces as output |
| 155 | std::vector<int> tops; |
| 156 | // shape hint |
| 157 | std::vector<Mat> bottom_shapes; |
| 158 | std::vector<Mat> top_shapes; |
| 159 | }; |
| 160 | |
| 161 | // layer factory function |
| 162 | typedef Layer* (*layer_creator_func)(void*); |
| 163 | typedef void (*layer_destroyer_func)(Layer*, void*); |
| 164 | |
| 165 | struct layer_registry_entry |
| 166 | { |
| 167 | #if NCNN_STRING |
| 168 | // layer type name |
| 169 | const char* name; |
| 170 | #endif // NCNN_STRING |
| 171 | // layer factory entry |
| 172 | layer_creator_func creator; |
| 173 | }; |
| 174 | |
| 175 | struct custom_layer_registry_entry |
| 176 | { |
| 177 | #if NCNN_STRING |
| 178 | // layer type name |
| 179 | const char* name; |
| 180 | #endif // NCNN_STRING |
| 181 | // layer factory entry |
| 182 | layer_creator_func creator; |
| 183 | layer_destroyer_func destroyer; |
| 184 | void* userdata; |
| 185 | }; |
| 186 | |
| 187 | struct overwrite_builtin_layer_registry_entry |
| 188 | { |
| 189 | // layer type index |
| 190 | int typeindex; |
| 191 | // layer factory entry |
| 192 | layer_creator_func creator; |
| 193 | layer_destroyer_func destroyer; |
| 194 | void* userdata; |
| 195 | }; |
| 196 | |
| 197 | #if NCNN_STRING |
| 198 | // get layer type from type name |
| 199 | NCNN_EXPORT int layer_to_index(const char* type); |
| 200 | // create layer from type name |
| 201 | NCNN_EXPORT Layer* create_layer(const char* type); |
| 202 | NCNN_EXPORT Layer* create_layer_naive(const char* type); |
| 203 | NCNN_EXPORT Layer* create_layer_cpu(const char* type); |
| 204 | #if NCNN_VULKAN |
| 205 | NCNN_EXPORT Layer* create_layer_vulkan(const char* type); |
| 206 | #endif // NCNN_VULKAN |
| 207 | #endif // NCNN_STRING |
| 208 | // create layer from layer type |
| 209 | NCNN_EXPORT Layer* create_layer(int index); |
| 210 | NCNN_EXPORT Layer* create_layer_naive(int index); |
| 211 | NCNN_EXPORT Layer* create_layer_cpu(int index); |
| 212 | #if NCNN_VULKAN |
| 213 | NCNN_EXPORT Layer* create_layer_vulkan(int index); |
| 214 | #endif // NCNN_VULKAN |
| 215 | |
| 216 | #define DEFINE_LAYER_CREATOR(name) \ |
| 217 | ::ncnn::Layer* name##_layer_creator(void* /*userdata*/) \ |
| 218 | { \ |
| 219 | return new name; \ |
| 220 | } |
| 221 | |
| 222 | #define DEFINE_LAYER_DESTROYER(name) \ |
| 223 | void name##_layer_destroyer(::ncnn::Layer* layer, void* /*userdata*/) \ |
| 224 | { \ |
| 225 | delete layer; \ |
| 226 | } |
| 227 | |
| 228 | } // namespace ncnn |
| 229 | |
| 230 | #endif // NCNN_LAYER_H |