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_tensor_c.cpp 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #include "lite_build_config.h"
  2. #if LITE_BUILD_WITH_MGE
  3. #include "../src/misc.h"
  4. #include "lite-c/global_c.h"
  5. #include "lite-c/tensor_c.h"
  6. #include <gtest/gtest.h>
  7. #include <memory>
  8. #include <thread>
  9. TEST(TestCapiTensor, Basic) {
  10. LiteTensor c_tensor0, c_tensor1;
  11. LiteTensorDesc description = default_desc;
  12. LITE_make_tensor(description, &c_tensor0);
  13. int is_pinned_host = false;
  14. LITE_is_pinned_host(c_tensor0, &is_pinned_host);
  15. ASSERT_FALSE(is_pinned_host);
  16. LiteDeviceType device_type;
  17. LITE_get_tensor_device_type(c_tensor0, &device_type);
  18. ASSERT_EQ(device_type, LiteDeviceType::LITE_CPU);
  19. size_t length = 0;
  20. LITE_get_tensor_total_size_in_byte(c_tensor0, &length);
  21. ASSERT_EQ(length, 0);
  22. LiteLayout layout{{1, 3, 224, 224}, 4, LiteDataType::LITE_FLOAT};
  23. description.device_type = LiteDeviceType::LITE_CPU;
  24. description.layout = layout;
  25. description.is_pinned_host = true;
  26. LITE_make_tensor(description, &c_tensor1);
  27. LITE_is_pinned_host(c_tensor1, &is_pinned_host);
  28. ASSERT_TRUE(is_pinned_host);
  29. LITE_get_tensor_total_size_in_byte(c_tensor1, &length);
  30. ASSERT_EQ(length, 1 * 3 * 224 * 224 * 4);
  31. LiteLayout get_layout;
  32. LITE_get_tensor_layout(c_tensor1, &get_layout);
  33. ASSERT_EQ(get_layout.ndim, layout.ndim);
  34. ASSERT_EQ(get_layout.data_type, layout.data_type);
  35. ASSERT_EQ(get_layout.shapes[0], layout.shapes[0]);
  36. ASSERT_EQ(get_layout.shapes[1], layout.shapes[1]);
  37. ASSERT_EQ(get_layout.shapes[2], layout.shapes[2]);
  38. ASSERT_EQ(get_layout.shapes[3], layout.shapes[3]);
  39. //! test error
  40. ASSERT_EQ(LITE_is_pinned_host(c_tensor0, nullptr), -1);
  41. ASSERT_NE(strlen(LITE_get_last_error()), 0);
  42. ASSERT_EQ(LITE_get_last_error_code(), ErrorCode::LITE_INTERNAL_ERROR);
  43. printf("The last error is: %s\n", LITE_get_last_error());
  44. LITE_clear_last_error();
  45. ASSERT_EQ(strlen(LITE_get_last_error()), 0);
  46. ASSERT_EQ(LITE_get_last_error_code(), ErrorCode::OK);
  47. LITE_destroy_tensor(c_tensor0);
  48. LITE_destroy_tensor(c_tensor1);
  49. }
  50. TEST(TestCapiTensor, SetLayoutReAlloc) {
  51. LiteTensor c_tensor0;
  52. LiteTensorDesc description = default_desc;
  53. description.layout = LiteLayout{{1, 3, 224, 224}, 4, LiteDataType::LITE_FLOAT};
  54. LITE_make_tensor(description, &c_tensor0);
  55. void *old_ptr, *new_ptr;
  56. LITE_get_tensor_memory(c_tensor0, &old_ptr);
  57. LiteLayout new_layout = LiteLayout{{1, 3, 100, 100}, 4, LiteDataType::LITE_INT8};
  58. LITE_set_tensor_layout(c_tensor0, new_layout);
  59. LITE_get_tensor_memory(c_tensor0, &new_ptr);
  60. size_t length = 0;
  61. LITE_get_tensor_total_size_in_byte(c_tensor0, &length);
  62. ASSERT_EQ(length, 1 * 3 * 100 * 100);
  63. ASSERT_EQ(old_ptr, new_ptr);
  64. }
  65. TEST(TestCapiTensor, Reset) {
  66. LiteTensor c_tensor0, c_tensor1;
  67. LiteTensorDesc description = default_desc;
  68. description.layout = LiteLayout{{3, 20}, 2, LiteDataType::LITE_FLOAT};
  69. LITE_make_tensor(description, &c_tensor0);
  70. LITE_make_tensor(description, &c_tensor1);
  71. void *old_ptr0, *old_ptr1;
  72. LITE_get_tensor_memory(c_tensor0, &old_ptr0);
  73. LITE_get_tensor_memory(c_tensor1, &old_ptr1);
  74. //! make sure memory is allocted
  75. ASSERT_NO_THROW(memcpy(old_ptr0, old_ptr1, 3 * 20 * 4));
  76. std::shared_ptr<float> new_ptr0(
  77. new float[3 * 20], [](float* ptr) { delete[] ptr; });
  78. std::shared_ptr<float> new_ptr1(
  79. new float[3 * 20], [](float* ptr) { delete[] ptr; });
  80. LITE_reset_tensor_memory(c_tensor0, new_ptr0.get(), 3 * 20 * 4);
  81. LITE_reset_tensor_memory(c_tensor1, new_ptr1.get(), 3 * 20 * 4);
  82. void *tmp_ptr0, *tmp_ptr1;
  83. LITE_get_tensor_memory(c_tensor0, &tmp_ptr0);
  84. LITE_get_tensor_memory(c_tensor1, &tmp_ptr1);
  85. ASSERT_EQ(tmp_ptr0, new_ptr0.get());
  86. ASSERT_EQ(tmp_ptr1, new_ptr1.get());
  87. ASSERT_NO_THROW(memcpy(new_ptr0.get(), new_ptr1.get(), 3 * 20 * 4));
  88. LiteLayout layout1{{6, 20}, 2, LiteDataType::LITE_FLOAT};
  89. std::shared_ptr<float> ptr2(new float[6 * 20], [](float* ptr) { delete[] ptr; });
  90. std::shared_ptr<float> ptr3(new float[6 * 20], [](float* ptr) { delete[] ptr; });
  91. LITE_reset_tensor(c_tensor0, layout1, new_ptr0.get());
  92. LITE_reset_tensor(c_tensor1, layout1, new_ptr1.get());
  93. //! memory is not freed by Tensor reset
  94. ASSERT_NO_THROW(memcpy(new_ptr0.get(), new_ptr1.get(), 3 * 20 * 4));
  95. LiteLayout tmp_layout0, tmp_layout1;
  96. LITE_get_tensor_layout(c_tensor0, &tmp_layout0);
  97. LITE_get_tensor_layout(c_tensor1, &tmp_layout1);
  98. ASSERT_EQ(tmp_layout0.ndim, tmp_layout1.ndim);
  99. ASSERT_EQ(tmp_layout0.data_type, tmp_layout1.data_type);
  100. ASSERT_EQ(tmp_layout0.shapes[0], tmp_layout1.shapes[0]);
  101. ASSERT_EQ(tmp_layout0.shapes[1], tmp_layout1.shapes[1]);
  102. LITE_destroy_tensor(c_tensor0);
  103. LITE_destroy_tensor(c_tensor1);
  104. }
  105. TEST(TestCapiTensor, CrossCNCopy) {
  106. LiteTensor c_tensor0, c_tensor1, c_tensor2;
  107. LiteTensorDesc description = default_desc;
  108. LITE_make_tensor(description, &c_tensor0);
  109. description.layout = LiteLayout{{1, 3, 224, 224}, 4, LiteDataType::LITE_FLOAT};
  110. LITE_make_tensor(description, &c_tensor1);
  111. LITE_make_tensor(description, &c_tensor2);
  112. LITE_tensor_copy(c_tensor1, c_tensor2);
  113. LITE_tensor_copy(c_tensor2, c_tensor1);
  114. void *old_ptr1, *old_ptr2, *new_ptr1, *new_ptr2;
  115. LITE_get_tensor_memory(c_tensor1, &old_ptr1);
  116. LITE_get_tensor_memory(c_tensor2, &old_ptr2);
  117. //! test source tenor is empty
  118. ASSERT_EQ(LITE_tensor_copy(c_tensor1, c_tensor0), -1);
  119. ASSERT_NE(strlen(LITE_get_last_error()), 0);
  120. printf("The last error is: %s\n", LITE_get_last_error());
  121. LITE_tensor_copy(c_tensor0, c_tensor1);
  122. LITE_tensor_copy(c_tensor1, c_tensor2);
  123. LITE_tensor_copy(c_tensor2, c_tensor0);
  124. LITE_get_tensor_memory(c_tensor1, &new_ptr1);
  125. LITE_get_tensor_memory(c_tensor2, &new_ptr2);
  126. ASSERT_EQ(old_ptr1, new_ptr1);
  127. ASSERT_EQ(old_ptr2, new_ptr2);
  128. LITE_destroy_tensor(c_tensor0);
  129. LITE_destroy_tensor(c_tensor1);
  130. LITE_destroy_tensor(c_tensor2);
  131. }
  132. TEST(TestCapiTensor, ShareMemoryWith) {
  133. LiteTensor c_tensor0, c_tensor1;
  134. LiteTensorDesc description = default_desc;
  135. LITE_make_tensor(description, &c_tensor0);
  136. description.layout = LiteLayout{{1, 3, 224, 224}, 4, LiteDataType::LITE_FLOAT};
  137. LITE_make_tensor(description, &c_tensor1);
  138. ASSERT_EQ(LITE_tensor_share_memory_with(c_tensor1, c_tensor0), -1);
  139. LITE_tensor_share_memory_with(c_tensor0, c_tensor1);
  140. void *ptr0, *ptr1;
  141. LITE_get_tensor_memory(c_tensor0, &ptr0);
  142. LITE_get_tensor_memory(c_tensor1, &ptr1);
  143. ASSERT_EQ(ptr0, ptr1);
  144. LITE_destroy_tensor(c_tensor0);
  145. LITE_destroy_tensor(c_tensor1);
  146. }
  147. TEST(TestCapiTensor, Reshape) {
  148. LiteTensor c_tensor0;
  149. LiteTensorDesc description = default_desc;
  150. description.layout = LiteLayout{{8, 8, 100, 100}, 4, LiteDataType::LITE_FLOAT};
  151. LITE_make_tensor(description, &c_tensor0);
  152. void* old_ptr;
  153. LITE_get_tensor_memory(c_tensor0, &old_ptr);
  154. auto check = [&](std::vector<size_t> expect, const LiteTensor& tensor) {
  155. LiteLayout get_layout;
  156. LITE_get_tensor_layout(tensor, &get_layout);
  157. ASSERT_EQ(get_layout.ndim, expect.size());
  158. for (size_t i = 0; i < expect.size(); i++) {
  159. ASSERT_EQ(get_layout.shapes[i], expect[i]);
  160. }
  161. void* new_ptr;
  162. LITE_get_tensor_memory(tensor, &new_ptr);
  163. ASSERT_EQ(old_ptr, new_ptr);
  164. };
  165. {
  166. int shape[2] = {-1, 50};
  167. LITE_tensor_reshape(c_tensor0, shape, 2);
  168. check({8 * 8 * 100 * 2, 50}, c_tensor0);
  169. }
  170. {
  171. int shape[3] = {64, 100, 100};
  172. LITE_tensor_reshape(c_tensor0, shape, 3);
  173. check({8 * 8, 100, 100}, c_tensor0);
  174. }
  175. {
  176. int shape[3] = {16, 100, -1};
  177. LITE_tensor_reshape(c_tensor0, shape, 3);
  178. check({16, 100, 400}, c_tensor0);
  179. }
  180. LITE_destroy_tensor(c_tensor0);
  181. }
  182. TEST(TestCapiTensor, Slice) {
  183. LiteTensor c_tensor0;
  184. LiteTensorDesc description = default_desc;
  185. description.layout = LiteLayout{{20, 20}, 2, LiteDataType::LITE_FLOAT};
  186. LITE_make_tensor(description, &c_tensor0);
  187. void* old_ptr;
  188. LITE_get_tensor_memory(c_tensor0, &old_ptr);
  189. for (size_t i = 0; i < 20 * 20; i++) {
  190. *(static_cast<float*>(old_ptr) + i) = i;
  191. }
  192. auto check = [&](size_t start, size_t end, size_t step, bool have_step) {
  193. LiteTensor tensor, slice_tensor;
  194. LITE_make_tensor(default_desc, &tensor);
  195. size_t start_ptr[2] = {start, start};
  196. size_t end_ptr[2] = {end, end};
  197. size_t step_ptr[2] = {step, step};
  198. if (have_step) {
  199. LITE_tensor_slice(
  200. c_tensor0, start_ptr, end_ptr, step_ptr, 2, &slice_tensor);
  201. } else {
  202. LITE_tensor_slice(c_tensor0, start_ptr, end_ptr, nullptr, 2, &slice_tensor);
  203. }
  204. int is_continue = true;
  205. LITE_is_memory_continue(slice_tensor, &is_continue);
  206. ASSERT_FALSE(is_continue);
  207. LITE_tensor_copy(tensor, slice_tensor);
  208. void* new_ptr;
  209. LITE_get_tensor_memory(tensor, &new_ptr);
  210. float* ptr = static_cast<float*>(new_ptr);
  211. for (size_t i = start; i < end; i += step) {
  212. for (size_t j = start; j < end; j += step) {
  213. ASSERT_EQ(float(i * 20 + j), *ptr);
  214. ++ptr;
  215. }
  216. }
  217. LITE_destroy_tensor(tensor);
  218. };
  219. check(1, 8, 1, true);
  220. check(1, 8, 1, false);
  221. check(2, 10, 2, true);
  222. check(10, 18, 4, true);
  223. check(10, 18, 1, false);
  224. LITE_destroy_tensor(c_tensor0);
  225. }
  226. TEST(TestCapiTensor, Memset) {
  227. LiteTensor c_tensor0;
  228. LiteTensorDesc description = default_desc;
  229. description.layout = LiteLayout{{20, 20}, 2, LiteDataType::LITE_FLOAT};
  230. LITE_make_tensor(description, &c_tensor0);
  231. void* ptr;
  232. uint8_t* uint8_ptr;
  233. LITE_get_tensor_memory(c_tensor0, &ptr);
  234. LITE_tensor_fill_zero(c_tensor0);
  235. uint8_ptr = static_cast<uint8_t*>(ptr);
  236. for (size_t i = 0; i < 20 * 20; i++) {
  237. ASSERT_EQ(0, *uint8_ptr);
  238. uint8_ptr++;
  239. }
  240. LITE_destroy_tensor(c_tensor0);
  241. }
  242. TEST(TestCapiTensor, GetMemoryByIndex) {
  243. LiteTensor c_tensor0;
  244. LiteTensorDesc description = default_desc;
  245. description.layout = LiteLayout{{20, 20}, 2, LiteDataType::LITE_FLOAT};
  246. LITE_make_tensor(description, &c_tensor0);
  247. void *ptr0, *ptr1, *ptr2, *ptr3;
  248. LITE_get_tensor_memory(c_tensor0, &ptr0);
  249. size_t index0[] = {3, 4};
  250. LITE_get_tensor_memory_with_index(c_tensor0, &index0[0], 2, &ptr1);
  251. size_t index1[] = {5, 7};
  252. LITE_get_tensor_memory_with_index(c_tensor0, &index1[0], 2, &ptr2);
  253. size_t index2[] = {5};
  254. LITE_get_tensor_memory_with_index(c_tensor0, &index2[0], 1, &ptr3);
  255. ASSERT_EQ(ptr1, static_cast<float*>(ptr0) + 3 * 20 + 4);
  256. ASSERT_EQ(ptr2, static_cast<float*>(ptr0) + 5 * 20 + 7);
  257. ASSERT_EQ(ptr3, static_cast<float*>(ptr0) + 5 * 20);
  258. LITE_destroy_tensor(c_tensor0);
  259. }
  260. TEST(TestCapiTensor, ThreadLocalError) {
  261. LiteTensor c_tensor0;
  262. LiteTensorDesc description = default_desc;
  263. description.layout = LiteLayout{{20, 20}, 2, LiteDataType::LITE_FLOAT};
  264. void *ptr0, *ptr1;
  265. std::thread thread1([&]() {
  266. LITE_make_tensor(description, &c_tensor0);
  267. LITE_get_tensor_memory(c_tensor0, &ptr0);
  268. });
  269. thread1.join();
  270. std::thread thread2([&]() {
  271. LITE_get_tensor_memory(c_tensor0, &ptr1);
  272. LITE_destroy_tensor(c_tensor0);
  273. });
  274. thread2.join();
  275. }
  276. #endif
  277. // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}