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_network.cpp 48 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361
  1. /**
  2. * \file test/test_network.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. */
  11. #include "lite_build_config.h"
  12. #if LITE_BUILD_WITH_MGE
  13. #include "./test_common.h"
  14. #include "megbrain/tensor.h"
  15. #ifndef WIN32
  16. #include <dirent.h>
  17. #include <string.h>
  18. #endif
  19. #include <chrono>
  20. #include <memory>
  21. #include <random>
  22. #include <unordered_map>
  23. using namespace lite;
  24. namespace {
  25. class CheckAllocator : public lite::Allocator {
  26. public:
  27. //! allocate memory of size in the given device with the given align
  28. void* allocate(LiteDeviceType device, int, size_t size, size_t align) override {
  29. LITE_ASSERT(device == LiteDeviceType::LITE_CPU);
  30. m_nr_left++;
  31. m_nr_allocated++;
  32. #ifdef WIN32
  33. return _aligned_malloc(size, align);
  34. #elif defined(__ANDROID__) || defined(ANDROID)
  35. return memalign(align, size);
  36. #else
  37. void* ptr = nullptr;
  38. auto err = posix_memalign(&ptr, align, size);
  39. mgb_assert(!err, "failed to malloc %zubytes with align %zu", size, align);
  40. return ptr;
  41. #endif
  42. };
  43. //! free the memory pointed by ptr in the given device
  44. void free(LiteDeviceType device, int, void* ptr) override {
  45. m_nr_left--;
  46. LITE_ASSERT(device == LiteDeviceType::LITE_CPU);
  47. #ifdef WIN32
  48. _aligned_free(ptr);
  49. #else
  50. ::free(ptr);
  51. #endif
  52. };
  53. std::atomic_size_t m_nr_left{0};
  54. std::atomic_size_t m_nr_allocated{0};
  55. };
  56. } // namespace
  57. TEST(TestNetWork, Basic) {
  58. Config config;
  59. auto lite_tensor = get_input_data("./input_data.npy");
  60. std::string model_path = "./shufflenet.mge";
  61. auto result_lite = mgelite_lar(model_path, config, "data", lite_tensor);
  62. auto result_mgb = mgb_lar(model_path, config, "data", lite_tensor);
  63. compare_lite_tensor<float>(result_lite, result_mgb);
  64. }
  65. TEST(TestNetWork, SetDeviceId) {
  66. Config config;
  67. auto lite_tensor = get_input_data("./input_data.npy");
  68. std::string model_path = "./shufflenet.mge";
  69. std::shared_ptr<Network> network = std::make_shared<Network>(config);
  70. network->set_device_id(4);
  71. network->load_model(model_path);
  72. std::shared_ptr<Tensor> input_tensor = network->get_input_tensor(0);
  73. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  74. network->forward();
  75. network->wait();
  76. ASSERT_EQ(input_tensor->get_device_id(), 4);
  77. ASSERT_EQ(output_tensor->get_device_id(), 4);
  78. }
  79. TEST(TestNetWork, GetAllName) {
  80. Config config;
  81. auto lite_tensor = get_input_data("./input_data.npy");
  82. std::string model_path = "./shufflenet.mge";
  83. std::shared_ptr<Network> network = std::make_shared<Network>(config);
  84. network->load_model(model_path);
  85. auto input_names = network->get_all_input_name();
  86. auto output_names = network->get_all_output_name();
  87. auto output_tensor = network->get_output_tensor(0);
  88. auto out_layout = output_tensor->get_layout();
  89. ASSERT_EQ(out_layout.ndim, 2);
  90. ASSERT_EQ(out_layout.shapes[0], 1);
  91. ASSERT_EQ(out_layout.shapes[1], 1000);
  92. ASSERT_EQ(input_names.size(), 1);
  93. ASSERT_EQ(output_names.size(), 1);
  94. ASSERT_TRUE(input_names[0] == "data");
  95. ASSERT_TRUE(output_names[0] == "TRUE_DIV(EXP[12065],reduce0[12067])[12077]");
  96. }
  97. TEST(TestNetWork, LoadFBSModel) {
  98. Config config;
  99. std::string model_path = "./ax.mge";
  100. std::shared_ptr<Network> network = std::make_shared<Network>(config);
  101. network->load_model(model_path);
  102. auto output_tensor = network->get_output_tensor(0);
  103. auto out_layout = output_tensor->get_layout();
  104. ASSERT_EQ(out_layout.ndim, 4);
  105. ASSERT_EQ(out_layout.shapes[0], 1);
  106. ASSERT_EQ(out_layout.shapes[1], 1);
  107. ASSERT_EQ(out_layout.shapes[2], 40);
  108. ASSERT_EQ(out_layout.shapes[3], 180);
  109. }
  110. TEST(TestNetWork, BasicInplaceAndSingleThreadAffinity) {
  111. Config config;
  112. auto lite_tensor = get_input_data("./input_data.npy");
  113. std::string model_path = "./shufflenet.mge";
  114. auto result_mgb = mgb_lar(model_path, config, "data", lite_tensor);
  115. std::shared_ptr<Network> network = std::make_shared<Network>(config);
  116. Runtime::set_cpu_inplace_mode(network);
  117. network->load_model(model_path);
  118. std::shared_ptr<Tensor> input_tensor = network->get_input_tensor(0);
  119. int affinity_set = false;
  120. Runtime::set_runtime_thread_affinity(network, [&affinity_set](int id) {
  121. ASSERT_EQ(id, 0);
  122. affinity_set = true;
  123. });
  124. auto src_ptr = lite_tensor->get_memory_ptr();
  125. auto src_layout = lite_tensor->get_layout();
  126. input_tensor->reset(src_ptr, src_layout);
  127. //! inplace mode not support async mode
  128. ASSERT_THROW(network->set_async_callback([]() {}), std::exception);
  129. network->forward();
  130. network->wait();
  131. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  132. ASSERT_EQ(affinity_set, true);
  133. compare_lite_tensor<float>(output_tensor, result_mgb);
  134. }
  135. TEST(TestNetWork, NetworkShareWeights) {
  136. Config config;
  137. auto lite_tensor = get_input_data("./input_data.npy");
  138. std::string model_path = "./shufflenet.mge";
  139. auto result_mgb = mgb_lar(model_path, config, "data", lite_tensor);
  140. std::shared_ptr<Network> network = std::make_shared<Network>(config);
  141. network->load_model(model_path);
  142. std::shared_ptr<Tensor> input_tensor = network->get_input_tensor(0);
  143. std::shared_ptr<Network> network2 = std::make_shared<Network>(config);
  144. Runtime::set_cpu_inplace_mode(network2);
  145. Runtime::shared_weight_with_network(network2, network);
  146. std::shared_ptr<Tensor> input_tensor2 = network2->get_input_tensor(0);
  147. auto src_ptr = lite_tensor->get_memory_ptr();
  148. auto src_layout = lite_tensor->get_layout();
  149. input_tensor->reset(src_ptr, src_layout);
  150. input_tensor2->reset(src_ptr, src_layout);
  151. ASSERT_NE(input_tensor, input_tensor2);
  152. network->forward();
  153. network->wait();
  154. network2->forward();
  155. network2->wait();
  156. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  157. std::shared_ptr<Tensor> output_tensor2 = network2->get_output_tensor(0);
  158. ASSERT_NE(output_tensor->get_memory_ptr(), output_tensor2->get_memory_ptr());
  159. compare_lite_tensor<float>(output_tensor, result_mgb);
  160. compare_lite_tensor<float>(output_tensor2, result_mgb);
  161. }
  162. TEST(TestNetWork, SharedRuntimeMem) {
  163. Config config;
  164. auto lite_tensor = get_input_data("./input_data.npy");
  165. std::string model_path = "./shufflenet.mge";
  166. auto result_mgb = mgb_lar(model_path, config, "data", lite_tensor);
  167. std::shared_ptr<Network> network_src = std::make_shared<Network>(config);
  168. std::shared_ptr<Network> network_dst = std::make_shared<Network>(config);
  169. Runtime::share_runtime_memory_with(network_dst, network_src);
  170. network_src->load_model(model_path);
  171. network_dst->load_model(model_path);
  172. }
  173. TEST(TestNetWork, UserAllocator) {
  174. auto allocator = std::make_shared<CheckAllocator>();
  175. {
  176. Config config;
  177. auto lite_tensor = get_input_data("./input_data.npy");
  178. std::string model_path = "./shufflenet.mge";
  179. auto result_mgb = mgb_lar(model_path, config, "data", lite_tensor);
  180. std::shared_ptr<Network> network = std::make_shared<Network>(config);
  181. Runtime::set_memory_allocator(network, allocator);
  182. network->load_model(model_path);
  183. std::shared_ptr<Tensor> input_tensor = network->get_input_tensor(0);
  184. auto src_ptr = lite_tensor->get_memory_ptr();
  185. auto src_layout = lite_tensor->get_layout();
  186. input_tensor->reset(src_ptr, src_layout);
  187. network->forward();
  188. network->wait();
  189. ASSERT_GE(allocator->m_nr_allocated, 1);
  190. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  191. compare_lite_tensor<float>(output_tensor, result_mgb);
  192. }
  193. ASSERT_EQ(allocator->m_nr_left, 0);
  194. }
  195. TEST(TestNetWork, BasicMultiThread) {
  196. Config config;
  197. auto lite_tensor = get_input_data("./input_data.npy");
  198. std::string model_path = "./shufflenet.mge";
  199. auto result_mgb = mgb_lar(model_path, config, "data", lite_tensor);
  200. std::shared_ptr<Network> network = std::make_shared<Network>(config);
  201. Runtime::set_cpu_threads_number(network, 2);
  202. network->load_model(model_path);
  203. std::shared_ptr<Tensor> input_tensor = network->get_input_tensor(0);
  204. auto src_ptr = lite_tensor->get_memory_ptr();
  205. auto src_layout = lite_tensor->get_layout();
  206. input_tensor->reset(src_ptr, src_layout);
  207. network->forward();
  208. network->wait();
  209. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  210. compare_lite_tensor<float>(output_tensor, result_mgb);
  211. }
  212. TEST(TestNetWork, ThreadAffinity) {
  213. size_t nr_threads = 4;
  214. Config config;
  215. auto lite_tensor = get_input_data("./input_data.npy");
  216. std::string model_path = "./shufflenet.mge";
  217. auto result_mgb = mgb_lar(model_path, config, "data", lite_tensor);
  218. std::shared_ptr<Network> network = std::make_shared<Network>(config);
  219. Runtime::set_cpu_threads_number(network, nr_threads);
  220. ASSERT_THROW(
  221. Runtime::set_runtime_thread_affinity(network, [](int) {}), std::exception);
  222. network->load_model(model_path);
  223. std::vector<std::thread::id> thread_ids(nr_threads);
  224. auto affinity = [&](int id) { thread_ids[id] = std::this_thread::get_id(); };
  225. Runtime::set_runtime_thread_affinity(network, affinity);
  226. std::shared_ptr<Tensor> input_tensor = network->get_input_tensor(0);
  227. auto src_ptr = lite_tensor->get_memory_ptr();
  228. auto src_layout = lite_tensor->get_layout();
  229. input_tensor->reset(src_ptr, src_layout);
  230. network->forward();
  231. network->wait();
  232. for (size_t i = 0; i < nr_threads; i++) {
  233. for (size_t j = i + 1; j < nr_threads; j++) {
  234. ASSERT_NE(thread_ids[i], thread_ids[j]);
  235. }
  236. }
  237. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  238. compare_lite_tensor<float>(output_tensor, result_mgb);
  239. }
  240. TEST(TestNetWork, BasicCryptAes) {
  241. Config config;
  242. auto lite_tensor = get_input_data("./input_data.npy");
  243. std::string model_path = "./shufflenet.mge";
  244. std::string model_crypt_path = "./shufflenet_crypt_aes.mge";
  245. auto result_mgb = mgb_lar(model_path, config, "data", lite_tensor);
  246. config.bare_model_cryption_name = "AES_default";
  247. auto result_lite = mgelite_lar(model_crypt_path, config, "data", lite_tensor);
  248. compare_lite_tensor<float>(result_lite, result_mgb);
  249. }
  250. TEST(TestNetWork, BasicCryptRc4) {
  251. Config config;
  252. auto lite_tensor = get_input_data("./input_data.npy");
  253. std::string model_path = "./shufflenet.mge";
  254. std::string model_crypt_path = "./shufflenet_crypt_rc4.mge";
  255. auto result_mgb = mgb_lar(model_path, config, "data", lite_tensor);
  256. config.bare_model_cryption_name = "RC4_default";
  257. auto result_lite = mgelite_lar(model_crypt_path, config, "data", lite_tensor);
  258. compare_lite_tensor<float>(result_lite, result_mgb);
  259. }
  260. TEST(TestNetWork, PackedCryptRc4) {
  261. Config config;
  262. auto lite_tensor = get_input_data("./input_data.npy");
  263. std::string model_path = "./shufflenet.mge";
  264. std::string model_crypt_path = "./test_packed_model_rc4.lite";
  265. auto result_mgb = mgb_lar(model_path, config, "data", lite_tensor);
  266. auto result_lite = mgelite_lar(model_crypt_path, config, "data", lite_tensor);
  267. compare_lite_tensor<float>(result_lite, result_mgb);
  268. }
  269. TEST(TestNetWork, BasicCryptSfRc4) {
  270. Config config;
  271. auto lite_tensor = get_input_data("./input_data.npy");
  272. std::string model_path = "./shufflenet.mge";
  273. std::string model_crypt_path = "./shufflenet_crypt_sfrc4.mge";
  274. auto result_mgb = mgb_lar(model_path, config, "data", lite_tensor);
  275. config.bare_model_cryption_name = "SIMPLE_FAST_RC4_default";
  276. auto result_lite = mgelite_lar(model_crypt_path, config, "data", lite_tensor);
  277. compare_lite_tensor<float>(result_lite, result_mgb);
  278. }
  279. TEST(TestNetWork, ResetInput) {
  280. Config config;
  281. auto tensor = get_input_data("./input_data.npy");
  282. std::string model_path = "./shufflenet.mge";
  283. std::string input_name = "data";
  284. auto result_mgb = mgb_lar(model_path, config, input_name, tensor);
  285. std::shared_ptr<Network> network = std::make_shared<Network>(config);
  286. network->load_model(model_path);
  287. std::shared_ptr<Tensor> input_tensor = network->get_io_tensor(input_name);
  288. auto src_ptr = tensor->get_memory_ptr();
  289. auto src_layout = tensor->get_layout();
  290. input_tensor->reset(src_ptr, src_layout);
  291. network->forward();
  292. network->wait();
  293. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  294. compare_lite_tensor<float>(output_tensor, result_mgb);
  295. }
  296. TEST(TestNetWork, ChangeInputShape) {
  297. Config config;
  298. auto tensor = get_input_data("./input_data.npy");
  299. std::string model_path = "./shufflenet.mge";
  300. std::string input_name = "data";
  301. auto result_mgb = mgb_lar(model_path, config, input_name, tensor);
  302. std::shared_ptr<Network> network = std::make_shared<Network>(config);
  303. network->load_model(model_path);
  304. std::shared_ptr<Tensor> input_tensor = network->get_io_tensor(input_name);
  305. auto src_layout = Layout{{2, 3, 200, 200}, 4, LiteDataType::LITE_FLOAT};
  306. input_tensor->set_layout(src_layout);
  307. std::shared_ptr<Tensor> input_tensor2 = network->get_io_tensor(input_name);
  308. //! Check memory is equal
  309. ASSERT_EQ(input_tensor->get_memory_ptr(), input_tensor2->get_memory_ptr());
  310. network->forward();
  311. network->wait();
  312. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  313. auto output_layout = output_tensor->get_layout();
  314. ASSERT_EQ(output_layout.shapes[0], 2);
  315. ASSERT_EQ(output_layout.shapes[1], 1000);
  316. }
  317. TEST(TestNetWork, ResetOutput) {
  318. Config config;
  319. auto tensor = get_input_data("./input_data.npy");
  320. std::string model_path = "./shufflenet.mge";
  321. std::string input_name = "data";
  322. auto result_mgb = mgb_lar(model_path, config, input_name, tensor);
  323. std::shared_ptr<Network> network = std::make_shared<Network>(config);
  324. network->load_model(model_path);
  325. std::shared_ptr<Tensor> input_tensor = network->get_io_tensor(input_name);
  326. auto src_ptr = tensor->get_memory_ptr();
  327. auto src_layout = tensor->get_layout();
  328. input_tensor->reset(src_ptr, src_layout);
  329. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  330. auto result_tensor = std::make_shared<Tensor>(
  331. LiteDeviceType::LITE_CPU, Layout{{1, 1000}, 2, LiteDataType::LITE_FLOAT});
  332. void* out_data = result_tensor->get_memory_ptr();
  333. output_tensor->reset(out_data, result_tensor->get_layout());
  334. network->forward();
  335. network->wait();
  336. compare_lite_tensor<float>(output_tensor, result_mgb);
  337. }
  338. namespace {
  339. void test_output_no_copy(int record) {
  340. Config config;
  341. config.options.force_output_use_user_specified_memory = true;
  342. config.options.comp_node_seq_record_level = record;
  343. auto tensor = get_input_data("./input_data.npy");
  344. std::string model_path = "./shufflenet.mge";
  345. std::string input_name = "data";
  346. auto result_mgb = mgb_lar(model_path, config, input_name, tensor);
  347. std::shared_ptr<Network> network = std::make_shared<Network>(config);
  348. network->load_model(model_path);
  349. std::shared_ptr<Tensor> input_tensor = network->get_io_tensor(input_name);
  350. auto src_ptr = tensor->get_memory_ptr();
  351. auto src_layout = tensor->get_layout();
  352. input_tensor->reset(src_ptr, src_layout);
  353. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  354. size_t times = 5;
  355. std::vector<std::shared_ptr<Tensor>> result_tensors;
  356. for (size_t i = 0; i < times; i++) {
  357. auto tmp = std::make_shared<Tensor>(
  358. LiteDeviceType::LITE_CPU,
  359. Layout{{1, 1000}, 2, LiteDataType::LITE_FLOAT});
  360. result_tensors.push_back(tmp);
  361. }
  362. for (size_t i = 0; i < times; i++) {
  363. void* out_data = result_tensors[i]->get_memory_ptr();
  364. output_tensor->reset(out_data, result_tensors[i]->get_layout());
  365. network->forward();
  366. network->wait();
  367. ASSERT_EQ(output_tensor->get_memory_ptr(), out_data);
  368. compare_lite_tensor<float>(output_tensor, result_mgb);
  369. }
  370. for (size_t i = 0; i < times; i++) {
  371. compare_lite_tensor<float>(result_tensors[i], result_mgb);
  372. }
  373. }
  374. void test_input_no_copy(int record) {
  375. Config config;
  376. config.options.force_output_use_user_specified_memory = true;
  377. config.options.comp_node_seq_record_level = record;
  378. std::string model_path = "./shufflenet.mge";
  379. std::string input_name = "data";
  380. Layout layout_in{{1, 3, 224, 224}, 4};
  381. std::vector<std::shared_ptr<Tensor>> inputs;
  382. std::vector<std::shared_ptr<Tensor>> outputs;
  383. for (int i = 0; i < 3; i++) {
  384. auto tmp_in = std::make_shared<Tensor>(LiteDeviceType::LITE_CPU, layout_in);
  385. auto ptr = static_cast<float*>(tmp_in->get_memory_ptr());
  386. for (size_t id = 0; id < 2 * 224 * 224; id++) {
  387. ptr[id] = i + 1;
  388. }
  389. inputs.push_back(tmp_in);
  390. outputs.push_back(mgb_lar(model_path, config, input_name, tmp_in));
  391. }
  392. std::shared_ptr<Network> network = std::make_shared<Network>(config);
  393. network->load_model(model_path);
  394. std::shared_ptr<Tensor> input_tensor = network->get_io_tensor(input_name);
  395. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  396. for (int i = 0; i < 3; i++) {
  397. auto ptr = inputs[i]->get_memory_ptr();
  398. input_tensor->reset(ptr, layout_in);
  399. auto tmp_out = std::make_shared<Tensor>(
  400. LiteDeviceType::LITE_CPU,
  401. Layout{{1, 1000}, 2, LiteDataType::LITE_FLOAT});
  402. output_tensor->reset(tmp_out->get_memory_ptr(), output_tensor->get_layout());
  403. network->forward();
  404. network->wait();
  405. compare_lite_tensor<float>(output_tensor, outputs[i]);
  406. }
  407. }
  408. void test_io_no_copy_ax(std::string model_name, int record = 1) {
  409. std::string model_path = model_name;
  410. std::vector<std::string> input_names, output_names;
  411. std::vector<std::vector<std::shared_ptr<Tensor>>> inputs;
  412. std::vector<std::vector<std::shared_ptr<Tensor>>> outputs;
  413. Config config;
  414. config.options.graph_opt_level = 0;
  415. std::shared_ptr<Network> network = std::make_shared<Network>(config);
  416. network->load_model(model_path);
  417. input_names = network->get_all_input_name();
  418. output_names = network->get_all_output_name();
  419. // prepare test data
  420. for (int i = 0; i < 3; i++) {
  421. std::vector<std::shared_ptr<Tensor>> net_inputs;
  422. std::vector<std::shared_ptr<Tensor>> net_outputs;
  423. for (size_t j = 0; j < input_names.size(); j++) {
  424. auto in_tesnor = network->get_io_tensor(input_names[j]);
  425. auto in_layout = in_tesnor->get_layout();
  426. auto tmp_in = std::make_shared<Tensor>(LiteDeviceType::LITE_CPU, in_layout);
  427. auto size = in_tesnor->get_tensor_total_size_in_byte() /
  428. in_layout.get_elem_size();
  429. if (in_layout.data_type == LiteDataType::LITE_INT16) {
  430. auto ptr = static_cast<short*>(tmp_in->get_memory_ptr());
  431. for (size_t id = 0; id < size; id++) {
  432. ptr[id] = i + 1;
  433. }
  434. } else if (in_layout.data_type == LiteDataType::LITE_UINT8) {
  435. auto ptr = static_cast<uint8_t*>(tmp_in->get_memory_ptr());
  436. for (size_t id = 0; id < size; id++) {
  437. ptr[id] = i + 1;
  438. }
  439. }
  440. net_inputs.push_back(tmp_in);
  441. in_tesnor->copy_from(*tmp_in);
  442. }
  443. inputs.push_back(net_inputs);
  444. network->forward();
  445. network->wait();
  446. for (size_t j = 0; j < output_names.size(); j++) {
  447. auto out_tesnor = network->get_io_tensor(output_names[j]);
  448. auto out_layout = out_tesnor->get_layout();
  449. auto tmp_out =
  450. std::make_shared<Tensor>(LiteDeviceType::LITE_CPU, out_layout);
  451. tmp_out->copy_from(*out_tesnor);
  452. net_outputs.push_back(tmp_out);
  453. }
  454. outputs.push_back(net_outputs);
  455. }
  456. config.options.force_output_use_user_specified_memory = true;
  457. config.options.comp_node_seq_record_level = record;
  458. config.options.const_shape = true;
  459. config.options.graph_opt_level = 2;
  460. std::shared_ptr<Network> network_record = std::make_shared<Network>(config);
  461. network_record->load_model(model_path);
  462. for (int i = 0; i < 3; i++) {
  463. for (size_t j = 0; j < inputs[i].size(); j++) {
  464. auto input_tensor = network_record->get_io_tensor(input_names[j]);
  465. input_tensor->reset(
  466. inputs[i][j]->get_memory_ptr(), inputs[i][j]->get_layout());
  467. }
  468. std::vector<std::shared_ptr<Tensor>> net_outputs;
  469. for (size_t j = 0; j < outputs[i].size(); j++) {
  470. auto output_tensor = network_record->get_io_tensor(output_names[j]);
  471. auto tmp_out = std::make_shared<Tensor>(
  472. LiteDeviceType::LITE_CPU, output_tensor->get_layout());
  473. output_tensor->reset(
  474. tmp_out->get_memory_ptr(), output_tensor->get_layout());
  475. net_outputs.push_back(tmp_out);
  476. }
  477. network_record->forward();
  478. network_record->wait();
  479. for (size_t j = 0; j < outputs[i].size(); j++) {
  480. auto output_tensor = network_record->get_io_tensor(output_names[j]);
  481. compare_lite_tensor<float>(output_tensor, outputs[i][j]);
  482. }
  483. }
  484. printf("profile the model %s run\n", model_path.c_str());
  485. std::vector<std::shared_ptr<Tensor>> net_outputs;
  486. for (size_t j = 0; j < outputs[0].size(); j++) {
  487. auto output_tensor = network_record->get_io_tensor(output_names[j]);
  488. auto tmp_out = std::make_shared<Tensor>(
  489. LiteDeviceType::LITE_CPU, output_tensor->get_layout());
  490. output_tensor->reset(tmp_out->get_memory_ptr(), output_tensor->get_layout());
  491. net_outputs.push_back(tmp_out);
  492. }
  493. lite::Timer timer("profile");
  494. for (int i = 0; i < 10; i++) {
  495. network_record->forward();
  496. network_record->wait();
  497. }
  498. auto sum_time = timer.get_used_time();
  499. printf("model %s used time average %f ms\n", model_path.c_str(), sum_time / 10);
  500. }
  501. } // namespace
  502. TEST(TestNetWork, OutputNoCopy) {
  503. test_output_no_copy(0);
  504. }
  505. TEST(TestNetWork, OutputNoCopyRecord) {
  506. test_output_no_copy(1);
  507. }
  508. TEST(TestNetWork, IONoCopy) {
  509. test_input_no_copy(0);
  510. }
  511. TEST(TestNetWork, IONoCopyRecord) {
  512. test_input_no_copy(1);
  513. }
  514. TEST(TestNetWork, IONoCopyRecordAx) {
  515. std::vector<std::string> file_names;
  516. #ifndef WIN32
  517. DIR* dirptr = NULL;
  518. struct dirent* dirp;
  519. std::string model_dir = "./ax_models";
  520. dirptr = opendir(model_dir.c_str());
  521. while (dirptr != NULL && (dirp = readdir(dirptr)) != NULL) {
  522. std::string file_name(dirp->d_name);
  523. if (file_name.find(".axe", 0) != std::string::npos) {
  524. file_names.push_back(model_dir + "/" + file_name);
  525. }
  526. }
  527. closedir(dirptr);
  528. #endif
  529. for (auto file_name : file_names) {
  530. printf("test model: %s\n", file_name.c_str());
  531. test_io_no_copy_ax(file_name);
  532. }
  533. }
  534. TEST(TestNetWork, OutputDynamicAlloc) {
  535. Config config;
  536. config.options.force_output_dynamic_alloc = true;
  537. auto tensor = get_input_data("./input_data.npy");
  538. std::string model_path = "./shufflenet.mge";
  539. std::string input_name = "data";
  540. auto result_mgb = mgb_lar(model_path, config, input_name, tensor);
  541. std::shared_ptr<Network> network = std::make_shared<Network>(config);
  542. network->load_model(model_path);
  543. std::shared_ptr<Tensor> input_tensor = network->get_io_tensor(input_name);
  544. auto src_ptr = tensor->get_memory_ptr();
  545. auto src_layout = tensor->get_layout();
  546. input_tensor->reset(src_ptr, src_layout);
  547. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  548. size_t times = 5;
  549. for (size_t i = 0; i < times; i++) {
  550. network->forward();
  551. network->wait();
  552. compare_lite_tensor<float>(output_tensor, result_mgb);
  553. }
  554. }
  555. TEST(TestNetWork, AsyncExec) {
  556. Config config;
  557. config.options.var_sanity_check_first_run = false;
  558. auto tensor = get_input_data("./input_data.npy");
  559. std::string model_path = "./shufflenet.mge";
  560. std::string input_name = "data";
  561. auto result_mgb = mgb_lar(model_path, config, input_name, tensor);
  562. std::shared_ptr<Network> network = std::make_shared<Network>(config);
  563. network->load_model(model_path);
  564. std::shared_ptr<Tensor> input_tensor = network->get_io_tensor(input_name);
  565. auto src_ptr = tensor->get_memory_ptr();
  566. auto src_layout = tensor->get_layout();
  567. input_tensor->reset(src_ptr, src_layout);
  568. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  569. auto result_tensor = std::make_shared<Tensor>(
  570. LiteDeviceType::LITE_CPU, Layout{{1, 1000}, 2, LiteDataType::LITE_FLOAT});
  571. void* out_data = result_tensor->get_memory_ptr();
  572. output_tensor->reset(out_data, result_tensor->get_layout());
  573. //! set async mode and callback
  574. volatile bool finished = false;
  575. network->set_async_callback([&finished]() { finished = true; });
  576. network->forward();
  577. size_t count = 0;
  578. while (finished == false) {
  579. count++;
  580. }
  581. ASSERT_GT(count, 0);
  582. compare_lite_tensor<float>(output_tensor, result_mgb);
  583. }
  584. TEST(TestNetWork, CPUDeviceInput) {
  585. auto tensor = get_input_data("./input_data.npy");
  586. Layout layout{{1, 3, 224, 224}, 4, LiteDataType::LITE_FLOAT};
  587. std::string model_path = "./shufflenet.mge";
  588. std::string input_name = "data";
  589. auto result_mgb = mgb_lar(model_path, {}, input_name, tensor);
  590. NetworkIO IO;
  591. bool is_host = false;
  592. IO.inputs.push_back({input_name, is_host});
  593. std::shared_ptr<Network> network = std::make_shared<Network>(IO);
  594. network->load_model(model_path);
  595. std::shared_ptr<Tensor> input_tensor = network->get_io_tensor(input_name);
  596. auto src_ptr = tensor->get_memory_ptr();
  597. input_tensor->reset(src_ptr, layout);
  598. network->forward();
  599. network->wait();
  600. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  601. compare_lite_tensor<float>(output_tensor, result_mgb);
  602. }
  603. TEST(TestNetWork, ShareTensorWith) {
  604. auto tensor = get_input_data("./input_data.npy");
  605. std::string model_path = "./shufflenet.mge";
  606. std::string input_name = "data";
  607. auto result_mgb = mgb_lar(model_path, {}, input_name, tensor);
  608. std::shared_ptr<Network> network = std::make_shared<Network>();
  609. network->load_model(model_path);
  610. std::shared_ptr<Tensor> input_tensor = network->get_io_tensor(input_name);
  611. input_tensor->share_memory_with(*tensor);
  612. network->forward();
  613. network->wait();
  614. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  615. compare_lite_tensor<float>(output_tensor, result_mgb);
  616. }
  617. TEST(TestNetWork, InputCallBack) {
  618. auto tensor = get_input_data("./input_data.npy");
  619. std::string model_path = "./shufflenet.mge";
  620. std::string input_name = "data";
  621. auto result_mgb = mgb_lar(model_path, {}, input_name, tensor);
  622. NetworkIO ios;
  623. bool is_host = false;
  624. ios.inputs.push_back({input_name, is_host});
  625. std::shared_ptr<Network> network = std::make_shared<Network>(ios);
  626. network->load_model(model_path);
  627. volatile bool finised_check_input = false;
  628. auto input_callback =
  629. [&tensor, &finised_check_input,
  630. input_name](const std::unordered_map<
  631. std::string, std::pair<IO, std::shared_ptr<Tensor>>>&
  632. input_map) {
  633. ASSERT_EQ(input_map.size(), 1);
  634. auto tensor_input = input_map.at(input_name).second;
  635. compare_lite_tensor<float>(tensor_input, tensor);
  636. finised_check_input = true;
  637. };
  638. network->set_start_callback(input_callback);
  639. std::shared_ptr<Tensor> input_tensor = network->get_io_tensor(input_name);
  640. input_tensor->share_memory_with(*tensor);
  641. network->forward();
  642. network->wait();
  643. ASSERT_TRUE(finised_check_input);
  644. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  645. compare_lite_tensor<float>(output_tensor, result_mgb);
  646. }
  647. TEST(TestNetWork, OutputCallBack) {
  648. auto tensor = get_input_data("./input_data.npy");
  649. std::string model_path = "./shufflenet.mge";
  650. std::string input_name = "data";
  651. auto result_mgb = mgb_lar(model_path, {}, input_name, tensor);
  652. std::shared_ptr<Network> network = std::make_shared<Network>();
  653. network->load_model(model_path);
  654. auto output_name = network->get_output_name(0);
  655. volatile bool finised_check_output = false;
  656. auto output_callback =
  657. [&result_mgb, &finised_check_output,
  658. output_name](const std::unordered_map<
  659. std::string, std::pair<IO, std::shared_ptr<Tensor>>>&
  660. output_map) {
  661. ASSERT_EQ(output_map.size(), 1);
  662. auto tensor_output = output_map.at(output_name).second;
  663. compare_lite_tensor<float>(tensor_output, result_mgb);
  664. finised_check_output = true;
  665. };
  666. network->set_finish_callback(output_callback);
  667. std::shared_ptr<Tensor> input_tensor = network->get_io_tensor(input_name);
  668. input_tensor->share_memory_with(*tensor);
  669. network->forward();
  670. network->wait();
  671. ASSERT_TRUE(finised_check_output);
  672. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  673. compare_lite_tensor<float>(output_tensor, result_mgb);
  674. }
  675. TEST(TestNetWork, OutputShapeOnly) {
  676. auto tensor = get_input_data("./input_data.npy");
  677. std::string model_path = "./shufflenet.mge";
  678. std::string input_name = "data";
  679. std::string output_name = "TRUE_DIV(EXP[12065],reduce0[12067])[12077]";
  680. NetworkIO IO;
  681. bool is_host = true;
  682. IO.outputs.push_back({output_name, is_host, LiteIOType::LITE_IO_SHAPE});
  683. Config config;
  684. std::shared_ptr<Network> network = std::make_shared<Network>(config, IO);
  685. network->load_model(model_path);
  686. std::shared_ptr<Tensor> input_tensor = network->get_io_tensor(input_name);
  687. std::shared_ptr<Tensor> output_tensor = network->get_io_tensor(output_name);
  688. auto src_ptr = tensor->get_memory_ptr();
  689. auto src_layout = tensor->get_layout();
  690. input_tensor->reset(src_ptr, src_layout);
  691. network->forward();
  692. network->wait();
  693. ASSERT_EQ(output_tensor->get_tensor_total_size_in_byte() / sizeof(float), 1000);
  694. }
  695. TEST(TestNetWork, ProfileIOdump) {
  696. auto tensor = get_input_data("./input_data.npy");
  697. std::string model_path = "./shufflenet.mge";
  698. std::string input_name = "data";
  699. NetworkIO IO;
  700. Config config;
  701. std::shared_ptr<Network> network = std::make_shared<Network>(config, IO);
  702. network->enable_profile_performance("./profile.json");
  703. network->load_model(model_path);
  704. std::shared_ptr<Tensor> input_tensor = network->get_io_tensor(input_name);
  705. auto src_ptr = tensor->get_memory_ptr();
  706. auto src_layout = tensor->get_layout();
  707. input_tensor->reset(src_ptr, src_layout);
  708. network->forward();
  709. network->wait();
  710. ASSERT_TRUE(fopen("./profile.json", "r"));
  711. Runtime::enable_io_txt_dump(network, "./io_txt_dump.txt");
  712. network->forward();
  713. network->wait();
  714. ASSERT_TRUE(fopen("./io_txt_dump.txt", "r"));
  715. }
  716. TEST(TestNetWork, LoadPackedModel) {
  717. auto tensor = get_input_data("./input_data.npy");
  718. std::string model_path = "./test_packed_model.lite";
  719. std::string input_name = "data";
  720. NetworkIO IO;
  721. Config config;
  722. std::shared_ptr<Network> network = std::make_shared<Network>(config, IO);
  723. network->load_model(model_path);
  724. std::shared_ptr<Tensor> input_tensor = network->get_io_tensor(input_name);
  725. auto src_ptr = tensor->get_memory_ptr();
  726. auto src_layout = tensor->get_layout();
  727. input_tensor->reset(src_ptr, src_layout);
  728. network->forward();
  729. network->wait();
  730. }
  731. TEST(TestNetWork, GlabalLayoutTransform) {
  732. // set_log_level(LiteLogLevel::DEBUG);
  733. auto tensor = get_input_data("./input_data.npy");
  734. std::string model_path = "./shufflenet.mge";
  735. std::string input_name = "data";
  736. std::string dump_model_name = "./shufflenet_after_trans.mge";
  737. NetworkIO IO;
  738. Config config;
  739. std::shared_ptr<Network> network = std::make_shared<Network>(config, IO);
  740. Runtime::enable_global_layout_transform(network);
  741. network->load_model(model_path);
  742. std::shared_ptr<Tensor> input_tensor = network->get_io_tensor(input_name);
  743. auto src_ptr = tensor->get_memory_ptr();
  744. auto src_layout = tensor->get_layout();
  745. input_tensor->reset(src_ptr, src_layout);
  746. Runtime::dump_layout_transform_model(network, dump_model_name);
  747. network->forward();
  748. network->wait();
  749. ASSERT_TRUE(fopen(dump_model_name.c_str(), "r"));
  750. }
  751. TEST(TestNetWork, GetDeviceType) {
  752. auto tensor = get_input_data("./input_data.npy");
  753. std::string model_path = "./shufflenet.mge";
  754. Config config;
  755. std::shared_ptr<Network> network = std::make_shared<Network>(config);
  756. network->load_model(model_path);
  757. ASSERT_TRUE(network->get_device_type() == LiteDeviceType::LITE_CPU);
  758. }
  759. TEST(TestNetWork, GetModelExtraInfo) {
  760. std::string model_path = "./track_640_320_pack_model_rc4_with_info.lite";
  761. Config config;
  762. std::shared_ptr<Network> network = std::make_shared<Network>(config);
  763. network->load_model(model_path);
  764. auto& extra_info = network->get_model_extra_info();
  765. ASSERT_TRUE(extra_info.size() > 0);
  766. printf("extra_info %s \n", extra_info.c_str());
  767. }
  768. #ifndef __IN_TEE_ENV__
  769. #if MGB_ENABLE_JSON
  770. TEST(TestNetWork, GetMemoryInfo) {
  771. Config config;
  772. auto lite_tensor = get_input_data("./input_data.npy");
  773. std::string model_path = "./shufflenet.mge";
  774. auto result_mgb = mgb_lar(model_path, config, "data", lite_tensor);
  775. std::shared_ptr<Network> network = std::make_shared<Network>(config);
  776. Runtime::set_cpu_threads_number(network, 2);
  777. network->load_model(model_path);
  778. network->get_static_memory_alloc_info();
  779. std::shared_ptr<Tensor> input_tensor = network->get_input_tensor(0);
  780. auto src_ptr = lite_tensor->get_memory_ptr();
  781. auto src_layout = lite_tensor->get_layout();
  782. input_tensor->reset(src_ptr, src_layout);
  783. network->forward();
  784. network->wait();
  785. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  786. compare_lite_tensor<float>(output_tensor, result_mgb);
  787. }
  788. #endif
  789. #endif
  790. #if LITE_WITH_CUDA
  791. TEST(TestNetWork, BasicDevice) {
  792. auto lite_tensor = get_input_data("./input_data.npy");
  793. Config config;
  794. config.device_type = LiteDeviceType::LITE_CUDA;
  795. std::string model_path = "./shufflenet.mge";
  796. auto result_lite = mgelite_lar(model_path, config, "data", lite_tensor);
  797. auto result_mgb = mgb_lar(model_path, config, "data", lite_tensor);
  798. compare_lite_tensor<float>(result_lite, result_mgb);
  799. }
  800. TEST(TestNetWork, DeviceInput) {
  801. auto tensor = get_input_data("./input_data.npy");
  802. Layout layout{{1, 3, 224, 224}, 4, LiteDataType::LITE_FLOAT};
  803. std::string model_path = "./shufflenet.mge";
  804. std::string input_name = "data";
  805. auto result_mgb = mgb_lar(model_path, {}, input_name, tensor);
  806. NetworkIO IO;
  807. bool is_host = false;
  808. IO.inputs.push_back({input_name, is_host});
  809. Config config;
  810. config.device_type = LiteDeviceType::LITE_CUDA;
  811. std::shared_ptr<Network> network = std::make_shared<Network>(config, IO);
  812. network->load_model(model_path);
  813. std::shared_ptr<Tensor> input_tensor = network->get_io_tensor(input_name);
  814. auto tensor_cuda = Tensor(LiteDeviceType::LITE_CUDA, layout);
  815. tensor_cuda.copy_from(*tensor);
  816. auto src_ptr = tensor_cuda.get_memory_ptr();
  817. input_tensor->reset(src_ptr, layout);
  818. network->forward();
  819. network->wait();
  820. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  821. compare_lite_tensor<float>(output_tensor, result_mgb);
  822. }
  823. TEST(TestNetWork, ChangeInputShapeDevice) {
  824. Config config;
  825. auto tensor = get_input_data("./input_data.npy");
  826. std::string model_path = "./shufflenet.mge";
  827. std::string input_name = "data";
  828. auto result_mgb = mgb_lar(model_path, config, input_name, tensor);
  829. config.device_type = LiteDeviceType::LITE_CUDA;
  830. std::shared_ptr<Network> network = std::make_shared<Network>(config);
  831. network->load_model(model_path);
  832. std::shared_ptr<Tensor> input_tensor = network->get_io_tensor(input_name);
  833. auto src_layout = Layout{{2, 3, 200, 200}, 4, LiteDataType::LITE_FLOAT};
  834. input_tensor->set_layout(src_layout);
  835. std::shared_ptr<Tensor> input_tensor2 = network->get_io_tensor(input_name);
  836. //! Check memory is equal
  837. ASSERT_EQ(input_tensor->get_memory_ptr(), input_tensor2->get_memory_ptr());
  838. network->forward();
  839. network->wait();
  840. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  841. auto output_layout = output_tensor->get_layout();
  842. ASSERT_EQ(output_layout.shapes[0], 2);
  843. ASSERT_EQ(output_layout.shapes[1], 1000);
  844. }
  845. TEST(TestNetWork, DeviceOutput) {
  846. auto tensor = get_input_data("./input_data.npy");
  847. std::string model_path = "./shufflenet.mge";
  848. std::string input_name = "data";
  849. std::string output_name = "TRUE_DIV(EXP[12065],reduce0[12067])[12077]";
  850. auto result_mgb = mgb_lar(model_path, {}, input_name, tensor);
  851. NetworkIO IO;
  852. bool is_host = false;
  853. IO.outputs.push_back({output_name, is_host});
  854. Config config;
  855. config.device_type = LiteDeviceType::LITE_CUDA;
  856. std::shared_ptr<Network> network = std::make_shared<Network>(config, IO);
  857. network->load_model(model_path);
  858. std::shared_ptr<Tensor> input_tensor = network->get_io_tensor(input_name);
  859. std::shared_ptr<Tensor> output_tensor_cuda = network->get_io_tensor(output_name);
  860. auto src_ptr = tensor->get_memory_ptr();
  861. auto src_layout = tensor->get_layout();
  862. input_tensor->reset(src_ptr, src_layout);
  863. network->forward();
  864. network->wait();
  865. auto output_tensor = std::make_shared<Tensor>();
  866. output_tensor->copy_from(*output_tensor_cuda);
  867. compare_lite_tensor<float>(output_tensor, result_mgb);
  868. }
  869. TEST(TestNetWork, WrongIONameDevice) {
  870. auto tensor = get_input_data("./input_data.npy");
  871. Layout layout{{1, 3, 224, 224}, 4, LiteDataType::LITE_FLOAT};
  872. std::string model_path = "./shufflenet.mge";
  873. std::string input_name = "data";
  874. std::string input_name_wrong = "data0";
  875. std::string output_name = "TRUE_DIV(EXP[12065],reduce0[12067])[12077]";
  876. std::string output_name_wrong = "w_TRUE_DIV(EXP[12065],reduce0[12067])[12077]";
  877. auto result_mgb = mgb_lar(model_path, {}, input_name, tensor);
  878. NetworkIO IO;
  879. bool is_host = false;
  880. IO.inputs.push_back({input_name, is_host});
  881. IO.outputs.push_back({output_name, is_host});
  882. IO.outputs.push_back({output_name_wrong, is_host});
  883. Config config;
  884. config.device_type = LiteDeviceType::LITE_CUDA;
  885. std::shared_ptr<Network> network = std::make_shared<Network>(config, IO);
  886. network->load_model(model_path);
  887. auto tensor_cuda = Tensor(LiteDeviceType::LITE_CUDA, layout);
  888. tensor_cuda.copy_from(*tensor);
  889. std::shared_ptr<Tensor> input_tensor = network->get_io_tensor(input_name);
  890. auto src_ptr = tensor_cuda.get_memory_ptr();
  891. auto src_layout = tensor_cuda.get_layout();
  892. input_tensor->reset(src_ptr, src_layout);
  893. std::shared_ptr<Tensor> output_tensor_cuda = network->get_io_tensor(output_name);
  894. network->forward();
  895. network->wait();
  896. auto output_tensor = std::make_shared<Tensor>();
  897. output_tensor->copy_from(*output_tensor_cuda);
  898. compare_lite_tensor<float>(output_tensor, result_mgb);
  899. }
  900. TEST(TestNetWork, ConfigIONameDevice) {
  901. std::string model_path = "./model.mgb";
  902. NetworkIO IO;
  903. bool is_host = false;
  904. IO.outputs.push_back({"clsfy", is_host});
  905. Config config;
  906. config.device_type = LiteDeviceType::LITE_CUDA;
  907. std::shared_ptr<Network> network = std::make_shared<Network>(config, IO);
  908. network->compute_only_configured_output();
  909. network->load_model(model_path);
  910. ASSERT_EQ(network->get_all_output_name().size(), 1);
  911. ASSERT_EQ(network->get_all_output_name()[0], "clsfy");
  912. std::shared_ptr<Network> network2 = std::make_shared<Network>(config, IO);
  913. network2->load_model(model_path);
  914. ASSERT_EQ(network2->get_all_output_name().size(), 2);
  915. }
  916. TEST(TestNetWork, SetDeviceIdDeviceTest) {
  917. #if LITE_WITH_CUDA
  918. if (get_device_count(LITE_CUDA) <= 1)
  919. return;
  920. #endif
  921. std::string model_path = "./model.mgb";
  922. NetworkIO IO;
  923. bool is_host = false;
  924. IO.inputs.push_back({"data", is_host});
  925. IO.outputs.push_back({"clsfy", is_host});
  926. Config config;
  927. config.device_type = LiteDeviceType::LITE_CUDA;
  928. std::shared_ptr<Network> network = std::make_shared<Network>(config, IO);
  929. network->set_device_id(1);
  930. network->load_model(model_path);
  931. auto inputs_names = network->get_all_input_name();
  932. for (auto name : inputs_names) {
  933. auto tensor = network->get_io_tensor(name);
  934. ASSERT_EQ(tensor->get_device_id(), 1);
  935. if (name == "idx") {
  936. int* index_ptr = static_cast<int*>(tensor->get_memory_ptr());
  937. for (int i = 0; i < 23; i++) {
  938. index_ptr[i] = i % 3;
  939. }
  940. }
  941. if (name == "landmark") {
  942. float* landmakrk_ptr = static_cast<float*>(tensor->get_memory_ptr());
  943. for (int i = 0; i < 23 * 18 * 2; i++) {
  944. landmakrk_ptr[i] = 0.1f;
  945. }
  946. }
  947. }
  948. auto outputs_names = network->get_all_output_name();
  949. for (auto name : outputs_names) {
  950. auto tensor = network->get_io_tensor(name);
  951. ASSERT_EQ(tensor->get_device_id(), 1);
  952. }
  953. network->forward();
  954. network->wait();
  955. }
  956. TEST(TestNetWork, SetStreamIdDeviceTest) {
  957. std::string model_path = "./model.mgb";
  958. NetworkIO IO;
  959. bool is_host = false;
  960. IO.inputs.push_back({"data", is_host});
  961. IO.outputs.push_back({"clsfy", is_host});
  962. Config config;
  963. config.device_type = LiteDeviceType::LITE_CUDA;
  964. std::shared_ptr<Network> network = std::make_shared<Network>(config, IO);
  965. network->set_stream_id(1);
  966. network->load_model(model_path);
  967. auto inputs_names = network->get_all_input_name();
  968. for (auto name : inputs_names) {
  969. auto tensor = network->get_io_tensor(name);
  970. if (name == "idx") {
  971. int* index_ptr = static_cast<int*>(tensor->get_memory_ptr());
  972. for (int i = 0; i < 23; i++) {
  973. index_ptr[i] = i % 3;
  974. }
  975. }
  976. if (name == "landmark") {
  977. float* landmakrk_ptr = static_cast<float*>(tensor->get_memory_ptr());
  978. for (int i = 0; i < 23 * 18 * 2; i++) {
  979. landmakrk_ptr[i] = 0.1f;
  980. }
  981. }
  982. }
  983. network->forward();
  984. network->wait();
  985. }
  986. #if CUDART_VERSION >= 10000
  987. TEST(TestNetWork, DeviceAsyncExec) {
  988. auto tensor = get_input_data("./input_data.npy");
  989. Config config;
  990. config.device_type = LiteDeviceType::LITE_CUDA;
  991. config.options.var_sanity_check_first_run = false;
  992. std::string model_path = "./shufflenet.mge";
  993. std::string input_name = "data";
  994. auto result_mgb = mgb_lar(model_path, config, input_name, tensor);
  995. std::shared_ptr<Network> network = std::make_shared<Network>(config);
  996. network->load_model(model_path);
  997. std::shared_ptr<Tensor> input_tensor = network->get_io_tensor(input_name);
  998. auto src_ptr = tensor->get_memory_ptr();
  999. auto src_layout = tensor->get_layout();
  1000. input_tensor->reset(src_ptr, src_layout);
  1001. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  1002. auto result_tensor = std::make_shared<Tensor>(
  1003. LiteDeviceType::LITE_CPU, Layout{{1, 1000}, 2, LiteDataType::LITE_FLOAT});
  1004. void* out_data = result_tensor->get_memory_ptr();
  1005. output_tensor->reset(out_data, result_tensor->get_layout());
  1006. //! set async mode and callback
  1007. volatile bool finished = false;
  1008. network->set_async_callback([&finished]() { finished = true; });
  1009. network->forward();
  1010. size_t count = 0;
  1011. while (finished == false) {
  1012. count++;
  1013. }
  1014. ASSERT_GT(count, 0);
  1015. compare_lite_tensor<float>(output_tensor, result_mgb);
  1016. }
  1017. #endif
  1018. #endif
  1019. #if MGB_ATLAS || MGB_CAMBRICON
  1020. namespace {
  1021. void load_no_device(LiteDeviceType device_type, const std::string& model_path) {
  1022. lite::Config config;
  1023. config.device_type = device_type;
  1024. auto network = std::make_shared<lite::Network>(config);
  1025. network->load_model(model_path);
  1026. network->forward();
  1027. network->wait();
  1028. }
  1029. void load_device_input(
  1030. LiteDeviceType device_type, const std::string& model_path,
  1031. const std::vector<std::string>& inputs) {
  1032. lite::NetworkIO networkio;
  1033. lite::IO input_data_io = {};
  1034. input_data_io.name = inputs[0];
  1035. input_data_io.is_host = false;
  1036. networkio.inputs.emplace_back(input_data_io);
  1037. lite::IO input_input0_io = {};
  1038. input_input0_io.name = inputs[1];
  1039. input_input0_io.is_host = false;
  1040. networkio.inputs.emplace_back(input_input0_io);
  1041. lite::Config config;
  1042. config.device_type = device_type;
  1043. auto network = std::make_shared<lite::Network>(config, networkio);
  1044. network->load_model(model_path);
  1045. network->forward();
  1046. network->wait();
  1047. }
  1048. void load_device_id(
  1049. LiteDeviceType device_type, int device_id, const std::string& model_path) {
  1050. lite::Config config;
  1051. config.device_type = device_type;
  1052. auto network = std::make_shared<lite::Network>(config);
  1053. network->set_device_id(device_id);
  1054. network->load_model(model_path);
  1055. std::shared_ptr<Tensor> input_tensor = network->get_input_tensor(0);
  1056. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  1057. network->forward();
  1058. network->wait();
  1059. ASSERT_EQ(output_tensor->get_device_id(), device_id);
  1060. }
  1061. } // namespace
  1062. #endif
  1063. #if MGB_ATLAS
  1064. TEST(TestNetWork, AtlasLoadNoDevice) {
  1065. load_no_device(LiteDeviceType::LITE_DEVICE_DEFAULT, "./model_atlas.mgb");
  1066. }
  1067. TEST(TestNetWork, AtlasLoadDeviceInput) {
  1068. load_device_input(
  1069. LiteDeviceType::LITE_DEVICE_DEFAULT, "./model_atlas.mgb",
  1070. {"data", "input0"});
  1071. }
  1072. TEST(TestNetWork, AtlasLoadAtlas) {
  1073. load_no_device(LiteDeviceType::LITE_ATLAS, "./model_atlas.mgb");
  1074. }
  1075. TEST(TestNetWork, AtlasLoadAtlasDeviceInput) {
  1076. load_device_input(
  1077. LiteDeviceType::LITE_ATLAS, "./model_atlas.mgb", {"data", "input0"});
  1078. }
  1079. TEST(TestNetWork, AtlasDeviceID) {
  1080. load_device_id(LiteDeviceType::LITE_ATLAS, 1, "./model_atlas.mgb");
  1081. }
  1082. #endif
  1083. #if MGB_CAMBRICON
  1084. TEST(TestNetWork, CambriconLoadNoDevice) {
  1085. load_no_device(LiteDeviceType::LITE_DEVICE_DEFAULT, "./model_magicmind.mgb");
  1086. }
  1087. TEST(TestNetWork, CambriconLoadDeviceInput) {
  1088. load_device_input(
  1089. LiteDeviceType::LITE_DEVICE_DEFAULT, "./model_magicmind.mgb",
  1090. {"data", "input0"});
  1091. }
  1092. TEST(TestNetWork, CambriconLoadCambricon) {
  1093. load_no_device(LiteDeviceType::LITE_CAMBRICON, "./model_magicmind.mgb");
  1094. }
  1095. TEST(TestNetWork, CambriconLoadCambriconDeviceInput) {
  1096. load_device_input(
  1097. LiteDeviceType::LITE_CAMBRICON, "./model_magicmind.mgb",
  1098. {"data", "input0"});
  1099. }
  1100. TEST(TestNetWork, CambriconDeviceID) {
  1101. load_device_id(LiteDeviceType::LITE_CAMBRICON, 0, "./model_magicmind.mgb");
  1102. }
  1103. #endif
  1104. #endif
  1105. // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}