| 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_NET_H |
| 16 | #define NCNN_NET_H |
| 17 | |
| 18 | #include "blob.h" |
| 19 | #include "layer.h" |
| 20 | #include "mat.h" |
| 21 | #include "option.h" |
| 22 | #include "platform.h" |
| 23 | |
| 24 | #if NCNN_PLATFORM_API |
| 25 | #if __ANDROID_API__ >= 9 |
| 26 | #include <android/asset_manager.h> |
| 27 | #endif // __ANDROID_API__ >= 9 |
| 28 | #endif // NCNN_PLATFORM_API |
| 29 | |
| 30 | namespace ncnn { |
| 31 | |
| 32 | #if NCNN_VULKAN |
| 33 | class VkCompute; |
| 34 | #endif // NCNN_VULKAN |
| 35 | class DataReader; |
| 36 | class Extractor; |
| 37 | class NetPrivate; |
| 38 | class NCNN_EXPORT Net |
| 39 | { |
| 40 | public: |
| 41 | // empty init |
| 42 | Net(); |
| 43 | // clear and destroy |
| 44 | virtual ~Net(); |
| 45 | |
| 46 | public: |
| 47 | // option can be changed before loading |
| 48 | Option opt; |
| 49 | |
| 50 | #if NCNN_VULKAN |
| 51 | // set gpu device by index |
| 52 | void set_vulkan_device(int device_index); |
| 53 | |
| 54 | // set gpu device by device handle, no owner transfer |
| 55 | void set_vulkan_device(const VulkanDevice* vkdev); |
| 56 | |
| 57 | const VulkanDevice* vulkan_device() const; |
| 58 | #endif // NCNN_VULKAN |
| 59 | |
| 60 | #if NCNN_STRING |
| 61 | // register custom layer or overwrite built-in layer by layer type name |
| 62 | // return 0 if success |
| 63 | int register_custom_layer(const char* type, layer_creator_func creator, layer_destroyer_func destroyer = 0, void* userdata = 0); |
| 64 | virtual int custom_layer_to_index(const char* type); |
| 65 | #endif // NCNN_STRING |
| 66 | // register custom layer or overwrite built-in layer by layer type |
| 67 | // return 0 if success |
| 68 | int register_custom_layer(int index, layer_creator_func creator, layer_destroyer_func destroyer = 0, void* userdata = 0); |
| 69 | |
| 70 | #if NCNN_STRING |
| 71 | int load_param(const DataReader& dr); |
| 72 | #endif // NCNN_STRING |
| 73 | |
| 74 | int load_param_bin(const DataReader& dr); |
| 75 | |
| 76 | int load_model(const DataReader& dr); |
| 77 | |
| 78 | #if NCNN_STDIO |
| 79 | #if NCNN_STRING |
| 80 | // load network structure from plain param file |
| 81 | // return 0 if success |
| 82 | int load_param(FILE* fp); |
| 83 | int load_param(const char* protopath); |
| 84 | int load_param_mem(const char* mem); |
| 85 | #endif // NCNN_STRING |
| 86 | // load network structure from binary param file |
| 87 | // return 0 if success |
| 88 | int load_param_bin(FILE* fp); |
| 89 | int load_param_bin(const char* protopath); |
| 90 | |
| 91 | // load network weight data from model file |
| 92 | // return 0 if success |
| 93 | int load_model(FILE* fp); |
| 94 | int load_model(const char* modelpath); |
| 95 | #endif // NCNN_STDIO |
| 96 | |
| 97 | // load network structure from external memory |
| 98 | // memory pointer must be 32-bit aligned |
| 99 | // return bytes consumed |
| 100 | int load_param(const unsigned char* mem); |
| 101 | |
| 102 | // reference network weight data from external memory |
| 103 | // weight data is not copied but referenced |
| 104 | // so external memory should be retained when used |
| 105 | // memory pointer must be 32-bit aligned |
| 106 | // return bytes consumed |
| 107 | int load_model(const unsigned char* mem); |
| 108 | |
| 109 | #if NCNN_PLATFORM_API |
| 110 | #if __ANDROID_API__ >= 9 |
| 111 | #if NCNN_STRING |
| 112 | // convenient load network structure from android asset plain param file |
| 113 | int load_param(AAsset* asset); |
| 114 | int load_param(AAssetManager* mgr, const char* assetpath); |
| 115 | #endif // NCNN_STRING |
| 116 | // convenient load network structure from android asset binary param file |
| 117 | int load_param_bin(AAsset* asset); |
| 118 | int load_param_bin(AAssetManager* mgr, const char* assetpath); |
| 119 | |
| 120 | // convenient load network weight data from android asset model file |
| 121 | int load_model(AAsset* asset); |
| 122 | int load_model(AAssetManager* mgr, const char* assetpath); |
| 123 | #endif // __ANDROID_API__ >= 9 |
| 124 | #endif // NCNN_PLATFORM_API |
| 125 | |
| 126 | // unload network structure and weight data |
| 127 | void clear(); |
| 128 | |
| 129 | // construct an Extractor from network |
| 130 | Extractor create_extractor() const; |
| 131 | |
| 132 | // get input/output indexes/names |
| 133 | const std::vector<int>& input_indexes() const; |
| 134 | const std::vector<int>& output_indexes() const; |
| 135 | #if NCNN_STRING |
| 136 | const std::vector<const char*>& input_names() const; |
| 137 | const std::vector<const char*>& output_names() const; |
| 138 | #endif |
| 139 | |
| 140 | const std::vector<Blob>& blobs() const; |
| 141 | const std::vector<Layer*>& layers() const; |
| 142 | |
| 143 | std::vector<Blob>& mutable_blobs(); |
| 144 | std::vector<Layer*>& mutable_layers(); |
| 145 | |
| 146 | protected: |
| 147 | friend class Extractor; |
| 148 | #if NCNN_STRING |
| 149 | int find_blob_index_by_name(const char* name) const; |
| 150 | int find_layer_index_by_name(const char* name) const; |
| 151 | virtual Layer* create_custom_layer(const char* type); |
| 152 | virtual Layer* create_overwrite_builtin_layer(const char* type); |
| 153 | #endif // NCNN_STRING |
| 154 | virtual Layer* create_custom_layer(int index); |
| 155 | virtual Layer* create_overwrite_builtin_layer(int typeindex); |
| 156 | |
| 157 | private: |
| 158 | Net(const Net&); |
| 159 | Net& operator=(const Net&); |
| 160 | |
| 161 | private: |
| 162 | NetPrivate* const d; |
| 163 | }; |
| 164 | |
| 165 | class ExtractorPrivate; |
| 166 | class NCNN_EXPORT Extractor |
| 167 | { |
| 168 | public: |
| 169 | virtual ~Extractor(); |
| 170 | |
| 171 | // copy |
| 172 | Extractor(const Extractor&); |
| 173 | |
| 174 | // assign |
| 175 | Extractor& operator=(const Extractor&); |
| 176 | |
| 177 | // clear blob mats and alloctors |
| 178 | void clear(); |
| 179 | |
| 180 | // enable light mode |
| 181 | // intermediate blob will be recycled when enabled |
| 182 | // enabled by default |
| 183 | void set_light_mode(bool enable); |
| 184 | |
| 185 | // deprecated, no-op |
| 186 | // instead, set net.opt.num_threads before net.load_param() |
| 187 | void set_num_threads(int num_threads); |
| 188 | |
| 189 | // set blob memory allocator |
| 190 | void set_blob_allocator(Allocator* allocator); |
| 191 | |
| 192 | // set workspace memory allocator |
| 193 | void set_workspace_allocator(Allocator* allocator); |
| 194 | |
| 195 | #if NCNN_VULKAN |
| 196 | // deprecated, no-op |
| 197 | // instead, set net.opt.use_vulkan_compute before net.load_param() |
| 198 | void set_vulkan_compute(bool enable); |
| 199 | |
| 200 | void set_blob_vkallocator(VkAllocator* allocator); |
| 201 | |
| 202 | void set_workspace_vkallocator(VkAllocator* allocator); |
| 203 | |
| 204 | void set_staging_vkallocator(VkAllocator* allocator); |
| 205 | #endif // NCNN_VULKAN |
| 206 | |
| 207 | #if NCNN_STRING |
| 208 | // set input by blob name |
| 209 | // return 0 if success |
| 210 | int input(const char* blob_name, const Mat& in); |
| 211 | |
| 212 | // get result by blob name |
| 213 | // return 0 if success |
| 214 | // type = 0, default |
| 215 | // type = 1, do not convert fp16/bf16 or / and packing |
| 216 | int extract(const char* blob_name, Mat& feat, int type = 0); |
| 217 | #endif // NCNN_STRING |
| 218 | |
| 219 | // set input by blob index |
| 220 | // return 0 if success |
| 221 | int input(int blob_index, const Mat& in); |
| 222 | |
| 223 | // get result by blob index |
| 224 | // return 0 if success |
| 225 | // type = 0, default |
| 226 | // type = 1, do not convert fp16/bf16 or / and packing |
| 227 | int extract(int blob_index, Mat& feat, int type = 0); |
| 228 | |
| 229 | #if NCNN_VULKAN |
| 230 | #if NCNN_STRING |
| 231 | // set input by blob name |
| 232 | // return 0 if success |
| 233 | int input(const char* blob_name, const VkMat& in); |
| 234 | |
| 235 | // get result by blob name |
| 236 | // return 0 if success |
| 237 | int extract(const char* blob_name, VkMat& feat, VkCompute& cmd); |
| 238 | |
| 239 | // set input by blob name |
| 240 | // return 0 if success |
| 241 | int input(const char* blob_name, const VkImageMat& in); |
| 242 | |
| 243 | // get result by blob name |
| 244 | // return 0 if success |
| 245 | int extract(const char* blob_name, VkImageMat& feat, VkCompute& cmd); |
| 246 | #endif // NCNN_STRING |
| 247 | |
| 248 | // set input by blob index |
| 249 | // return 0 if success |
| 250 | int input(int blob_index, const VkMat& in); |
| 251 | |
| 252 | // get result by blob index |
| 253 | // return 0 if success |
| 254 | int extract(int blob_index, VkMat& feat, VkCompute& cmd); |
| 255 | |
| 256 | // set input by blob index |
| 257 | // return 0 if success |
| 258 | int input(int blob_index, const VkImageMat& in); |
| 259 | |
| 260 | // get result by blob index |
| 261 | // return 0 if success |
| 262 | int extract(int blob_index, VkImageMat& feat, VkCompute& cmd); |
| 263 | #endif // NCNN_VULKAN |
| 264 | |
| 265 | protected: |
| 266 | friend Extractor Net::create_extractor() const; |
| 267 | Extractor(const Net* net, size_t blob_count); |
| 268 | |
| 269 | private: |
| 270 | ExtractorPrivate* const d; |
| 271 | }; |
| 272 | |
| 273 | } // namespace ncnn |
| 274 | |
| 275 | #endif // NCNN_NET_H |