c_api.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /* Tencent is pleased to support the open source community by making ncnn available.
  2. *
  3. * Copyright (C) 2020 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_C_API_H
  16. #define NCNN_C_API_H
  17. #include "platform.h"
  18. #if NCNN_C_API
  19. #include <stddef.h>
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. NCNN_EXPORT const char* ncnn_version();
  24. /* allocator api */
  25. typedef struct __ncnn_allocator_t* ncnn_allocator_t;
  26. struct NCNN_EXPORT __ncnn_allocator_t
  27. {
  28. void* pthis;
  29. void* (*fast_malloc)(ncnn_allocator_t allocator, size_t size);
  30. void (*fast_free)(ncnn_allocator_t allocator, void* ptr);
  31. };
  32. NCNN_EXPORT ncnn_allocator_t ncnn_allocator_create_pool_allocator();
  33. NCNN_EXPORT ncnn_allocator_t ncnn_allocator_create_unlocked_pool_allocator();
  34. NCNN_EXPORT void ncnn_allocator_destroy(ncnn_allocator_t allocator);
  35. /* option api */
  36. typedef struct __ncnn_option_t* ncnn_option_t;
  37. NCNN_EXPORT ncnn_option_t ncnn_option_create();
  38. NCNN_EXPORT void ncnn_option_destroy(ncnn_option_t opt);
  39. NCNN_EXPORT int ncnn_option_get_num_threads(const ncnn_option_t opt);
  40. NCNN_EXPORT void ncnn_option_set_num_threads(ncnn_option_t opt, int num_threads);
  41. NCNN_EXPORT int ncnn_option_get_use_local_pool_allocator(const ncnn_option_t opt);
  42. NCNN_EXPORT void ncnn_option_set_use_local_pool_allocator(ncnn_option_t opt, int use_local_pool_allocator);
  43. NCNN_EXPORT void ncnn_option_set_blob_allocator(ncnn_option_t opt, ncnn_allocator_t allocator);
  44. NCNN_EXPORT void ncnn_option_set_workspace_allocator(ncnn_option_t opt, ncnn_allocator_t allocator);
  45. NCNN_EXPORT int ncnn_option_get_use_vulkan_compute(const ncnn_option_t opt);
  46. NCNN_EXPORT void ncnn_option_set_use_vulkan_compute(ncnn_option_t opt, int use_vulkan_compute);
  47. /* mat api */
  48. typedef struct __ncnn_mat_t* ncnn_mat_t;
  49. NCNN_EXPORT ncnn_mat_t ncnn_mat_create();
  50. NCNN_EXPORT ncnn_mat_t ncnn_mat_create_1d(int w, ncnn_allocator_t allocator);
  51. NCNN_EXPORT ncnn_mat_t ncnn_mat_create_2d(int w, int h, ncnn_allocator_t allocator);
  52. NCNN_EXPORT ncnn_mat_t ncnn_mat_create_3d(int w, int h, int c, ncnn_allocator_t allocator);
  53. NCNN_EXPORT ncnn_mat_t ncnn_mat_create_4d(int w, int h, int d, int c, ncnn_allocator_t allocator);
  54. NCNN_EXPORT ncnn_mat_t ncnn_mat_create_external_1d(int w, void* data, ncnn_allocator_t allocator);
  55. NCNN_EXPORT ncnn_mat_t ncnn_mat_create_external_2d(int w, int h, void* data, ncnn_allocator_t allocator);
  56. NCNN_EXPORT ncnn_mat_t ncnn_mat_create_external_3d(int w, int h, int c, void* data, ncnn_allocator_t allocator);
  57. NCNN_EXPORT ncnn_mat_t ncnn_mat_create_external_4d(int w, int h, int d, int c, void* data, ncnn_allocator_t allocator);
  58. NCNN_EXPORT ncnn_mat_t ncnn_mat_create_1d_elem(int w, size_t elemsize, int elempack, ncnn_allocator_t allocator);
  59. NCNN_EXPORT ncnn_mat_t ncnn_mat_create_2d_elem(int w, int h, size_t elemsize, int elempack, ncnn_allocator_t allocator);
  60. NCNN_EXPORT ncnn_mat_t ncnn_mat_create_3d_elem(int w, int h, int c, size_t elemsize, int elempack, ncnn_allocator_t allocator);
  61. NCNN_EXPORT ncnn_mat_t ncnn_mat_create_4d_elem(int w, int h, int d, int c, size_t elemsize, int elempack, ncnn_allocator_t allocator);
  62. NCNN_EXPORT ncnn_mat_t ncnn_mat_create_external_1d_elem(int w, void* data, size_t elemsize, int elempack, ncnn_allocator_t allocator);
  63. NCNN_EXPORT ncnn_mat_t ncnn_mat_create_external_2d_elem(int w, int h, void* data, size_t elemsize, int elempack, ncnn_allocator_t allocator);
  64. NCNN_EXPORT ncnn_mat_t ncnn_mat_create_external_3d_elem(int w, int h, int c, void* data, size_t elemsize, int elempack, ncnn_allocator_t allocator);
  65. NCNN_EXPORT ncnn_mat_t ncnn_mat_create_external_4d_elem(int w, int h, int d, int c, void* data, size_t elemsize, int elempack, ncnn_allocator_t allocator);
  66. NCNN_EXPORT void ncnn_mat_destroy(ncnn_mat_t mat);
  67. NCNN_EXPORT void ncnn_mat_fill_float(ncnn_mat_t mat, float v);
  68. NCNN_EXPORT ncnn_mat_t ncnn_mat_clone(const ncnn_mat_t mat, ncnn_allocator_t allocator);
  69. NCNN_EXPORT ncnn_mat_t ncnn_mat_reshape_1d(const ncnn_mat_t mat, int w, ncnn_allocator_t allocator);
  70. NCNN_EXPORT ncnn_mat_t ncnn_mat_reshape_2d(const ncnn_mat_t mat, int w, int h, ncnn_allocator_t allocator);
  71. NCNN_EXPORT ncnn_mat_t ncnn_mat_reshape_3d(const ncnn_mat_t mat, int w, int h, int c, ncnn_allocator_t allocator);
  72. NCNN_EXPORT ncnn_mat_t ncnn_mat_reshape_4d(const ncnn_mat_t mat, int w, int h, int d, int c, ncnn_allocator_t allocator);
  73. NCNN_EXPORT int ncnn_mat_get_dims(const ncnn_mat_t mat);
  74. NCNN_EXPORT int ncnn_mat_get_w(const ncnn_mat_t mat);
  75. NCNN_EXPORT int ncnn_mat_get_h(const ncnn_mat_t mat);
  76. NCNN_EXPORT int ncnn_mat_get_d(const ncnn_mat_t mat);
  77. NCNN_EXPORT int ncnn_mat_get_c(const ncnn_mat_t mat);
  78. NCNN_EXPORT size_t ncnn_mat_get_elemsize(const ncnn_mat_t mat);
  79. NCNN_EXPORT int ncnn_mat_get_elempack(const ncnn_mat_t mat);
  80. NCNN_EXPORT size_t ncnn_mat_get_cstep(const ncnn_mat_t mat);
  81. NCNN_EXPORT void* ncnn_mat_get_data(const ncnn_mat_t mat);
  82. NCNN_EXPORT void* ncnn_mat_get_channel_data(const ncnn_mat_t mat, int c);
  83. #if NCNN_PIXEL
  84. /* mat pixel api */
  85. #define NCNN_MAT_PIXEL_RGB 1
  86. #define NCNN_MAT_PIXEL_BGR 2
  87. #define NCNN_MAT_PIXEL_GRAY 3
  88. #define NCNN_MAT_PIXEL_RGBA 4
  89. #define NCNN_MAT_PIXEL_BGRA 5
  90. #define NCNN_MAT_PIXEL_X2Y(X, Y) (X | (Y << 16))
  91. NCNN_EXPORT ncnn_mat_t ncnn_mat_from_pixels(const unsigned char* pixels, int type, int w, int h, int stride, ncnn_allocator_t allocator);
  92. NCNN_EXPORT ncnn_mat_t ncnn_mat_from_pixels_resize(const unsigned char* pixels, int type, int w, int h, int stride, int target_width, int target_height, ncnn_allocator_t allocator);
  93. NCNN_EXPORT ncnn_mat_t ncnn_mat_from_pixels_roi(const unsigned char* pixels, int type, int w, int h, int stride, int roix, int roiy, int roiw, int roih, ncnn_allocator_t allocator);
  94. NCNN_EXPORT ncnn_mat_t ncnn_mat_from_pixels_roi_resize(const unsigned char* pixels, int type, int w, int h, int stride, int roix, int roiy, int roiw, int roih, int target_width, int target_height, ncnn_allocator_t allocator);
  95. NCNN_EXPORT void ncnn_mat_to_pixels(const ncnn_mat_t mat, unsigned char* pixels, int type, int stride);
  96. NCNN_EXPORT void ncnn_mat_to_pixels_resize(const ncnn_mat_t mat, unsigned char* pixels, int type, int target_width, int target_height, int target_stride);
  97. #endif /* NCNN_PIXEL */
  98. NCNN_EXPORT void ncnn_mat_substract_mean_normalize(ncnn_mat_t mat, const float* mean_vals, const float* norm_vals);
  99. NCNN_EXPORT void ncnn_convert_packing(const ncnn_mat_t src, ncnn_mat_t* dst, int elempack, const ncnn_option_t opt);
  100. NCNN_EXPORT void ncnn_flatten(const ncnn_mat_t src, ncnn_mat_t* dst, const ncnn_option_t opt);
  101. /* blob api */
  102. typedef struct __ncnn_blob_t* ncnn_blob_t;
  103. #if NCNN_STRING
  104. NCNN_EXPORT const char* ncnn_blob_get_name(const ncnn_blob_t blob);
  105. #endif /* NCNN_STRING */
  106. NCNN_EXPORT int ncnn_blob_get_producer(const ncnn_blob_t blob);
  107. NCNN_EXPORT int ncnn_blob_get_consumer(const ncnn_blob_t blob);
  108. NCNN_EXPORT void ncnn_blob_get_shape(const ncnn_blob_t blob, int* dims, int* w, int* h, int* c);
  109. /* paramdict api */
  110. typedef struct __ncnn_paramdict_t* ncnn_paramdict_t;
  111. NCNN_EXPORT ncnn_paramdict_t ncnn_paramdict_create();
  112. NCNN_EXPORT void ncnn_paramdict_destroy(ncnn_paramdict_t pd);
  113. NCNN_EXPORT int ncnn_paramdict_get_type(const ncnn_paramdict_t pd, int id);
  114. NCNN_EXPORT int ncnn_paramdict_get_int(const ncnn_paramdict_t pd, int id, int def);
  115. NCNN_EXPORT float ncnn_paramdict_get_float(const ncnn_paramdict_t pd, int id, float def);
  116. NCNN_EXPORT ncnn_mat_t ncnn_paramdict_get_array(const ncnn_paramdict_t pd, int id, const ncnn_mat_t def);
  117. NCNN_EXPORT void ncnn_paramdict_set_int(ncnn_paramdict_t pd, int id, int i);
  118. NCNN_EXPORT void ncnn_paramdict_set_float(ncnn_paramdict_t pd, int id, float f);
  119. NCNN_EXPORT void ncnn_paramdict_set_array(ncnn_paramdict_t pd, int id, const ncnn_mat_t v);
  120. /* datareader api */
  121. typedef struct __ncnn_datareader_t* ncnn_datareader_t;
  122. struct NCNN_EXPORT __ncnn_datareader_t
  123. {
  124. void* pthis;
  125. #if NCNN_STRING
  126. int (*scan)(ncnn_datareader_t dr, const char* format, void* p);
  127. #endif /* NCNN_STRING */
  128. size_t (*read)(ncnn_datareader_t dr, void* buf, size_t size);
  129. };
  130. NCNN_EXPORT ncnn_datareader_t ncnn_datareader_create();
  131. #if NCNN_STDIO
  132. NCNN_EXPORT ncnn_datareader_t ncnn_datareader_create_from_stdio(FILE* fp);
  133. #endif /* NCNN_STDIO */
  134. NCNN_EXPORT ncnn_datareader_t ncnn_datareader_create_from_memory(const unsigned char** mem);
  135. NCNN_EXPORT void ncnn_datareader_destroy(ncnn_datareader_t dr);
  136. /* modelbin api */
  137. typedef struct __ncnn_modelbin_t* ncnn_modelbin_t;
  138. struct NCNN_EXPORT __ncnn_modelbin_t
  139. {
  140. void* pthis;
  141. ncnn_mat_t (*load_1d)(const ncnn_modelbin_t mb, int w, int type);
  142. ncnn_mat_t (*load_2d)(const ncnn_modelbin_t mb, int w, int h, int type);
  143. ncnn_mat_t (*load_3d)(const ncnn_modelbin_t mb, int w, int h, int c, int type);
  144. };
  145. NCNN_EXPORT ncnn_modelbin_t ncnn_modelbin_create_from_datareader(const ncnn_datareader_t dr);
  146. NCNN_EXPORT ncnn_modelbin_t ncnn_modelbin_create_from_mat_array(const ncnn_mat_t* weights, int n);
  147. NCNN_EXPORT void ncnn_modelbin_destroy(ncnn_modelbin_t mb);
  148. /* layer api */
  149. typedef struct __ncnn_layer_t* ncnn_layer_t;
  150. struct NCNN_EXPORT __ncnn_layer_t
  151. {
  152. void* pthis;
  153. int (*load_param)(ncnn_layer_t layer, const ncnn_paramdict_t pd);
  154. int (*load_model)(ncnn_layer_t layer, const ncnn_modelbin_t mb);
  155. int (*create_pipeline)(ncnn_layer_t layer, const ncnn_option_t opt);
  156. int (*destroy_pipeline)(ncnn_layer_t layer, const ncnn_option_t opt);
  157. int (*forward_1)(const ncnn_layer_t layer, const ncnn_mat_t bottom_blob, ncnn_mat_t* top_blob, const ncnn_option_t opt);
  158. int (*forward_n)(const ncnn_layer_t layer, const ncnn_mat_t* bottom_blobs, int n, ncnn_mat_t* top_blobs, int n2, const ncnn_option_t opt);
  159. int (*forward_inplace_1)(const ncnn_layer_t layer, ncnn_mat_t bottom_top_blob, const ncnn_option_t opt);
  160. int (*forward_inplace_n)(const ncnn_layer_t layer, ncnn_mat_t* bottom_top_blobs, int n, const ncnn_option_t opt);
  161. };
  162. NCNN_EXPORT ncnn_layer_t ncnn_layer_create();
  163. NCNN_EXPORT ncnn_layer_t ncnn_layer_create_by_typeindex(int typeindex);
  164. #if NCNN_STRING
  165. NCNN_EXPORT ncnn_layer_t ncnn_layer_create_by_type(const char* type);
  166. #endif /* NCNN_STRING */
  167. NCNN_EXPORT void ncnn_layer_destroy(ncnn_layer_t layer);
  168. #if NCNN_STRING
  169. NCNN_EXPORT const char* ncnn_layer_get_name(const ncnn_layer_t layer);
  170. #endif /* NCNN_STRING */
  171. NCNN_EXPORT int ncnn_layer_get_typeindex(const ncnn_layer_t layer);
  172. #if NCNN_STRING
  173. NCNN_EXPORT const char* ncnn_layer_get_type(const ncnn_layer_t layer);
  174. #endif /* NCNN_STRING */
  175. NCNN_EXPORT int ncnn_layer_get_one_blob_only(const ncnn_layer_t layer);
  176. NCNN_EXPORT int ncnn_layer_get_support_inplace(const ncnn_layer_t layer);
  177. NCNN_EXPORT int ncnn_layer_get_support_vulkan(const ncnn_layer_t layer);
  178. NCNN_EXPORT int ncnn_layer_get_support_packing(const ncnn_layer_t layer);
  179. NCNN_EXPORT int ncnn_layer_get_support_bf16_storage(const ncnn_layer_t layer);
  180. NCNN_EXPORT int ncnn_layer_get_support_fp16_storage(const ncnn_layer_t layer);
  181. NCNN_EXPORT int ncnn_layer_get_support_image_storage(const ncnn_layer_t layer);
  182. NCNN_EXPORT void ncnn_layer_set_one_blob_only(ncnn_layer_t layer, int enable);
  183. NCNN_EXPORT void ncnn_layer_set_support_inplace(ncnn_layer_t layer, int enable);
  184. NCNN_EXPORT void ncnn_layer_set_support_vulkan(ncnn_layer_t layer, int enable);
  185. NCNN_EXPORT void ncnn_layer_set_support_packing(ncnn_layer_t layer, int enable);
  186. NCNN_EXPORT void ncnn_layer_set_support_bf16_storage(ncnn_layer_t layer, int enable);
  187. NCNN_EXPORT void ncnn_layer_set_support_fp16_storage(ncnn_layer_t layer, int enable);
  188. NCNN_EXPORT void ncnn_layer_set_support_image_storage(ncnn_layer_t layer, int enable);
  189. NCNN_EXPORT int ncnn_layer_get_bottom_count(const ncnn_layer_t layer);
  190. NCNN_EXPORT int ncnn_layer_get_bottom(const ncnn_layer_t layer, int i);
  191. NCNN_EXPORT int ncnn_layer_get_top_count(const ncnn_layer_t layer);
  192. NCNN_EXPORT int ncnn_layer_get_top(const ncnn_layer_t layer, int i);
  193. NCNN_EXPORT void ncnn_blob_get_bottom_shape(const ncnn_layer_t layer, int i, int* dims, int* w, int* h, int* c);
  194. NCNN_EXPORT void ncnn_blob_get_top_shape(const ncnn_layer_t layer, int i, int* dims, int* w, int* h, int* c);
  195. /* layer factory function */
  196. typedef ncnn_layer_t (*ncnn_layer_creator_t)(void* userdata);
  197. typedef void (*ncnn_layer_destroyer_t)(ncnn_layer_t layer, void* userdata);
  198. typedef struct __ncnn_net_custom_layer_factory_t* ncnn_net_custom_layer_factory_t;
  199. struct __ncnn_net_custom_layer_factory_t
  200. {
  201. ncnn_layer_creator_t creator;
  202. ncnn_layer_destroyer_t destroyer;
  203. void* userdata;
  204. ncnn_net_custom_layer_factory_t next;
  205. };
  206. /* net api */
  207. typedef struct __ncnn_net_t* ncnn_net_t;
  208. struct __ncnn_net_t
  209. {
  210. void* pthis;
  211. ncnn_net_custom_layer_factory_t custom_layer_factory;
  212. };
  213. NCNN_EXPORT ncnn_net_t ncnn_net_create();
  214. NCNN_EXPORT void ncnn_net_destroy(ncnn_net_t net);
  215. NCNN_EXPORT ncnn_option_t ncnn_net_get_option(ncnn_net_t net);
  216. NCNN_EXPORT void ncnn_net_set_option(ncnn_net_t net, ncnn_option_t opt);
  217. #if NCNN_STRING
  218. NCNN_EXPORT void ncnn_net_register_custom_layer_by_type(ncnn_net_t net, const char* type, ncnn_layer_creator_t creator, ncnn_layer_destroyer_t destroyer, void* userdata);
  219. #endif /* NCNN_STRING */
  220. NCNN_EXPORT void ncnn_net_register_custom_layer_by_typeindex(ncnn_net_t net, int typeindex, ncnn_layer_creator_t creator, ncnn_layer_destroyer_t destroyer, void* userdata);
  221. #if NCNN_STDIO
  222. #if NCNN_STRING
  223. NCNN_EXPORT int ncnn_net_load_param(ncnn_net_t net, const char* path);
  224. #endif /* NCNN_STRING */
  225. NCNN_EXPORT int ncnn_net_load_param_bin(ncnn_net_t net, const char* path);
  226. NCNN_EXPORT int ncnn_net_load_model(ncnn_net_t net, const char* path);
  227. #endif /* NCNN_STDIO */
  228. #if NCNN_STDIO
  229. #if NCNN_STRING
  230. NCNN_EXPORT int ncnn_net_load_param_memory(ncnn_net_t net, const char* mem);
  231. #endif /* NCNN_STRING */
  232. #endif /* NCNN_STDIO */
  233. NCNN_EXPORT int ncnn_net_load_param_bin_memory(ncnn_net_t net, const unsigned char* mem);
  234. NCNN_EXPORT int ncnn_net_load_model_memory(ncnn_net_t net, const unsigned char* mem);
  235. #if NCNN_STRING
  236. NCNN_EXPORT int ncnn_net_load_param_datareader(ncnn_net_t net, const ncnn_datareader_t dr);
  237. #endif /* NCNN_STRING */
  238. NCNN_EXPORT int ncnn_net_load_param_bin_datareader(ncnn_net_t net, const ncnn_datareader_t dr);
  239. NCNN_EXPORT int ncnn_net_load_model_datareader(ncnn_net_t net, const ncnn_datareader_t dr);
  240. NCNN_EXPORT void ncnn_net_clear(ncnn_net_t net);
  241. NCNN_EXPORT int ncnn_net_get_input_count(const ncnn_net_t net);
  242. NCNN_EXPORT int ncnn_net_get_output_count(const ncnn_net_t net);
  243. #if NCNN_STRING
  244. NCNN_EXPORT const char* ncnn_net_get_input_name(const ncnn_net_t net, int i);
  245. NCNN_EXPORT const char* ncnn_net_get_output_name(const ncnn_net_t net, int i);
  246. #endif /* NCNN_STRING */
  247. NCNN_EXPORT int ncnn_net_get_input_index(const ncnn_net_t net, int i);
  248. NCNN_EXPORT int ncnn_net_get_output_index(const ncnn_net_t net, int i);
  249. /* extractor api */
  250. typedef struct __ncnn_extractor_t* ncnn_extractor_t;
  251. NCNN_EXPORT ncnn_extractor_t ncnn_extractor_create(ncnn_net_t net);
  252. NCNN_EXPORT void ncnn_extractor_destroy(ncnn_extractor_t ex);
  253. NCNN_EXPORT void ncnn_extractor_set_option(ncnn_extractor_t ex, const ncnn_option_t opt);
  254. #if NCNN_STRING
  255. NCNN_EXPORT int ncnn_extractor_input(ncnn_extractor_t ex, const char* name, const ncnn_mat_t mat);
  256. NCNN_EXPORT int ncnn_extractor_extract(ncnn_extractor_t ex, const char* name, ncnn_mat_t* mat);
  257. #endif /* NCNN_STRING */
  258. NCNN_EXPORT int ncnn_extractor_input_index(ncnn_extractor_t ex, int index, const ncnn_mat_t mat);
  259. NCNN_EXPORT int ncnn_extractor_extract_index(ncnn_extractor_t ex, int index, ncnn_mat_t* mat);
  260. #ifdef __cplusplus
  261. } /* extern "C" */
  262. #endif
  263. #endif /* NCNN_C_API */
  264. #endif /* NCNN_C_API_H */