You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_c_api.cpp 7.9 kB

5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // Copyright 2020 Tencent
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. #include <string.h>
  4. #include "c_api.h"
  5. static int test_c_api_0()
  6. {
  7. ncnn_mat_t a = ncnn_mat_create_1d(2, NULL);
  8. ncnn_mat_t b = ncnn_mat_create_1d(2, NULL);
  9. ncnn_mat_t c = 0;
  10. ncnn_option_t opt = ncnn_option_create();
  11. // set a and b
  12. {
  13. ncnn_mat_fill_float(a, 2.f);
  14. ncnn_mat_fill_float(b, 3.f);
  15. }
  16. // c = a + b
  17. {
  18. ncnn_layer_t op = ncnn_layer_create_by_type("BinaryOp");
  19. // load param
  20. {
  21. ncnn_paramdict_t pd = ncnn_paramdict_create();
  22. ncnn_paramdict_set_int(pd, 0, 0); // op_type = ADD
  23. op->load_param(op, pd);
  24. ncnn_paramdict_destroy(pd);
  25. }
  26. // load model
  27. {
  28. ncnn_modelbin_t mb = ncnn_modelbin_create_from_mat_array(0, 0);
  29. op->load_model(op, mb);
  30. ncnn_modelbin_destroy(mb);
  31. }
  32. op->create_pipeline(op, opt);
  33. const ncnn_mat_t bottom_blobs[2] = {a, b};
  34. ncnn_mat_t top_blobs[1] = {0};
  35. op->forward_n(op, bottom_blobs, 2, top_blobs, 1, opt);
  36. c = top_blobs[0];
  37. op->destroy_pipeline(op, opt);
  38. ncnn_layer_destroy(op);
  39. }
  40. // check c == a + b
  41. bool success = false;
  42. if (c)
  43. {
  44. int dims = ncnn_mat_get_dims(c);
  45. int w = ncnn_mat_get_w(c);
  46. const float* c_data = (const float*)ncnn_mat_get_data(c);
  47. success = dims == 1 && w == 2 && c_data[0] == 5.f && c_data[1] == 5.f;
  48. }
  49. ncnn_option_destroy(opt);
  50. ncnn_mat_destroy(a);
  51. ncnn_mat_destroy(b);
  52. ncnn_mat_destroy(c);
  53. if (!success)
  54. {
  55. fprintf(stderr, "test_c_api_0 failed\n");
  56. }
  57. return success ? 0 : -1;
  58. }
  59. static int test_c_api_1()
  60. {
  61. ncnn_mat_t a = ncnn_mat_create_1d(24, NULL);
  62. // set a
  63. {
  64. const float data[] = {
  65. 0, 1, 2, 3, 4, 5, 6, 7,
  66. 10, 11, 12, 13, 14, 15, 16, 17,
  67. 20, 21, 22, 23, 24, 25, 26, 27
  68. };
  69. float* a_data = (float*)ncnn_mat_get_data(a);
  70. memcpy(a_data, data, 24 * sizeof(float));
  71. }
  72. ncnn_mat_t b = ncnn_mat_reshape_3d(a, 4, 2, 3, NULL);
  73. ncnn_mat_t c = 0;
  74. ncnn_option_t opt = ncnn_option_create();
  75. // c = reorg(b, 2)
  76. {
  77. ncnn_layer_t op = ncnn_layer_create_by_type("Reorg");
  78. // load param
  79. {
  80. ncnn_paramdict_t pd = ncnn_paramdict_create();
  81. ncnn_paramdict_set_int(pd, 0, 2); // stride
  82. op->load_param(op, pd);
  83. ncnn_paramdict_destroy(pd);
  84. }
  85. // load model
  86. {
  87. ncnn_modelbin_t mb = ncnn_modelbin_create_from_mat_array(0, 0);
  88. op->load_model(op, mb);
  89. ncnn_modelbin_destroy(mb);
  90. }
  91. op->create_pipeline(op, opt);
  92. op->forward_1(op, b, &c, opt);
  93. op->destroy_pipeline(op, opt);
  94. ncnn_layer_destroy(op);
  95. }
  96. // check c
  97. bool success = false;
  98. if (c)
  99. {
  100. int dims = ncnn_mat_get_dims(c);
  101. int w = ncnn_mat_get_w(c);
  102. int h = ncnn_mat_get_h(c);
  103. int ch = ncnn_mat_get_c(c);
  104. success = dims == 3 && w == 2 && h == 1 && ch == 12;
  105. const float expected[] = {
  106. 0, 2,
  107. 1, 3,
  108. 4, 6,
  109. 5, 7,
  110. 10, 12,
  111. 11, 13,
  112. 14, 16,
  113. 15, 17,
  114. 20, 22,
  115. 21, 23,
  116. 24, 26,
  117. 25, 27
  118. };
  119. ncnn_mat_t c2 = 0;
  120. ncnn_flatten(c, &c2, opt);
  121. const float* c2_data = (const float*)ncnn_mat_get_data(c2);
  122. if (memcmp(c2_data, expected, 24) != 0)
  123. {
  124. success = false;
  125. }
  126. ncnn_mat_destroy(c2);
  127. }
  128. ncnn_option_destroy(opt);
  129. ncnn_mat_destroy(a);
  130. ncnn_mat_destroy(b);
  131. ncnn_mat_destroy(c);
  132. if (!success)
  133. {
  134. fprintf(stderr, "test_c_api_1 failed\n");
  135. }
  136. return success ? 0 : -1;
  137. }
  138. static int mylayer_forward_inplace_1(const ncnn_layer_t layer, ncnn_mat_t bottom_top_blob, const ncnn_option_t opt)
  139. {
  140. int w = ncnn_mat_get_w(bottom_top_blob);
  141. int h = ncnn_mat_get_h(bottom_top_blob);
  142. int channels = ncnn_mat_get_c(bottom_top_blob);
  143. int size = w * h;
  144. #pragma omp parallel for num_threads(ncnn_option_get_num_threads(opt))
  145. for (int q = 0; q < channels; q++)
  146. {
  147. float* ptr = (float*)ncnn_mat_get_channel_data(bottom_top_blob, q);
  148. for (int i = 0; i < size; i++)
  149. {
  150. *ptr = *ptr + 100.f;
  151. ptr++;
  152. }
  153. }
  154. return 0;
  155. }
  156. static ncnn_layer_t mylayer_creator(void* /*userdata*/)
  157. {
  158. ncnn_layer_t layer = ncnn_layer_create();
  159. ncnn_layer_set_one_blob_only(layer, 1);
  160. ncnn_layer_set_support_inplace(layer, 1);
  161. layer->forward_inplace_1 = mylayer_forward_inplace_1;
  162. return layer;
  163. }
  164. static void mylayer_destroyer(ncnn_layer_t layer, void* /*userdata*/)
  165. {
  166. ncnn_layer_destroy(layer);
  167. }
  168. static size_t emptydr_read(ncnn_datareader_t /*dr*/, void* buf, size_t size)
  169. {
  170. memset(buf, 0, size);
  171. return size;
  172. }
  173. static int test_c_api_2()
  174. {
  175. // datareader from empty
  176. ncnn_datareader_t emptydr = ncnn_datareader_create();
  177. {
  178. emptydr->read = emptydr_read;
  179. }
  180. ncnn_allocator_t blob_allocator = ncnn_allocator_create_pool_allocator();
  181. ncnn_allocator_t workspace_allocator = ncnn_allocator_create_unlocked_pool_allocator();
  182. ncnn_option_t opt = ncnn_option_create();
  183. {
  184. ncnn_option_set_num_threads(opt, 1);
  185. ncnn_option_set_blob_allocator(opt, blob_allocator);
  186. ncnn_option_set_workspace_allocator(opt, workspace_allocator);
  187. }
  188. ncnn_net_t net = ncnn_net_create();
  189. {
  190. ncnn_net_set_option(net, opt);
  191. ncnn_net_register_custom_layer_by_type(net, "MyLayer", mylayer_creator, mylayer_destroyer, 0);
  192. const char param_txt[] = "7767517\n2 2\nInput input 0 1 data\nMyLayer mylayer 1 1 data output\n";
  193. ncnn_net_load_param_memory(net, param_txt);
  194. ncnn_net_load_model_datareader(net, emptydr);
  195. }
  196. ncnn_mat_t a = ncnn_mat_create_1d(24, blob_allocator);
  197. // set a
  198. {
  199. const float data[] = {
  200. 0, 1, 2, 3, 4, 5, 6, 7,
  201. 10, 11, 12, 13, 14, 15, 16, 17,
  202. 20, 21, 22, 23, 24, 25, 26, 27
  203. };
  204. float* a_data = (float*)ncnn_mat_get_data(a);
  205. memcpy(a_data, data, 24 * sizeof(float));
  206. }
  207. ncnn_mat_t b = ncnn_mat_reshape_3d(a, 4, 2, 3, blob_allocator);
  208. ncnn_mat_t c = 0;
  209. {
  210. ncnn_extractor_t ex = ncnn_extractor_create(net);
  211. ncnn_extractor_input(ex, "data", b);
  212. ncnn_extractor_extract(ex, "output", &c);
  213. ncnn_extractor_destroy(ex);
  214. }
  215. ncnn_net_destroy(net);
  216. // check c
  217. bool success = false;
  218. if (c)
  219. {
  220. int dims = ncnn_mat_get_dims(c);
  221. int w = ncnn_mat_get_w(c);
  222. int h = ncnn_mat_get_h(c);
  223. int ch = ncnn_mat_get_c(c);
  224. success = dims == 3 && w == 4 && h == 2 && ch == 3;
  225. const float expected[] = {
  226. 100, 101, 102, 103, 104, 105, 106, 107,
  227. 110, 111, 112, 113, 114, 115, 116, 117,
  228. 120, 121, 122, 123, 124, 125, 126, 127
  229. };
  230. ncnn_mat_t c2 = 0;
  231. ncnn_flatten(c, &c2, opt);
  232. const float* c2_data = (const float*)ncnn_mat_get_data(c2);
  233. if (memcmp(c2_data, expected, 24) != 0)
  234. {
  235. success = false;
  236. }
  237. ncnn_mat_destroy(c2);
  238. }
  239. ncnn_mat_destroy(a);
  240. ncnn_mat_destroy(b);
  241. ncnn_mat_destroy(c);
  242. ncnn_option_destroy(opt);
  243. ncnn_allocator_destroy(blob_allocator);
  244. ncnn_allocator_destroy(workspace_allocator);
  245. ncnn_datareader_destroy(emptydr);
  246. if (!success)
  247. {
  248. fprintf(stderr, "test_c_api_2 failed\n");
  249. }
  250. return success ? 0 : -1;
  251. }
  252. int main()
  253. {
  254. return test_c_api_0() || test_c_api_1() || test_c_api_2();
  255. }