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 46 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  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. std::shared_ptr<Network> network = std::make_shared<Network>();
  414. network->load_model(model_path);
  415. input_names = network->get_all_input_name();
  416. output_names = network->get_all_output_name();
  417. // prepare test data
  418. for (int i = 0; i < 3; i++) {
  419. std::vector<std::shared_ptr<Tensor>> net_inputs;
  420. std::vector<std::shared_ptr<Tensor>> net_outputs;
  421. for (size_t j = 0; j < input_names.size(); j++) {
  422. auto in_tesnor = network->get_io_tensor(input_names[j]);
  423. auto in_layout = in_tesnor->get_layout();
  424. auto tmp_in = std::make_shared<Tensor>(LiteDeviceType::LITE_CPU, in_layout);
  425. auto size = in_tesnor->get_tensor_total_size_in_byte() /
  426. in_layout.get_elem_size();
  427. if (in_layout.data_type == LiteDataType::LITE_INT16) {
  428. auto ptr = static_cast<short*>(tmp_in->get_memory_ptr());
  429. for (size_t id = 0; id < size; id++) {
  430. ptr[id] = i + 1;
  431. }
  432. } else if (in_layout.data_type == LiteDataType::LITE_UINT8) {
  433. auto ptr = static_cast<uint8_t*>(tmp_in->get_memory_ptr());
  434. for (size_t id = 0; id < size; id++) {
  435. ptr[id] = i + 1;
  436. }
  437. }
  438. net_inputs.push_back(tmp_in);
  439. in_tesnor->copy_from(*tmp_in);
  440. }
  441. inputs.push_back(net_inputs);
  442. network->forward();
  443. network->wait();
  444. for (size_t j = 0; j < output_names.size(); j++) {
  445. auto out_tesnor = network->get_io_tensor(output_names[j]);
  446. auto out_layout = out_tesnor->get_layout();
  447. auto tmp_out =
  448. std::make_shared<Tensor>(LiteDeviceType::LITE_CPU, out_layout);
  449. tmp_out->copy_from(*out_tesnor);
  450. net_outputs.push_back(tmp_out);
  451. }
  452. outputs.push_back(net_outputs);
  453. }
  454. Config config;
  455. config.options.force_output_use_user_specified_memory = true;
  456. config.options.comp_node_seq_record_level = record;
  457. config.options.const_shape = true;
  458. std::shared_ptr<Network> network_record = std::make_shared<Network>(config);
  459. network_record->load_model(model_path);
  460. for (int i = 0; i < 3; i++) {
  461. for (size_t j = 0; j < inputs[i].size(); j++) {
  462. auto input_tensor = network_record->get_io_tensor(input_names[j]);
  463. input_tensor->reset(
  464. inputs[i][j]->get_memory_ptr(), inputs[i][j]->get_layout());
  465. }
  466. std::vector<std::shared_ptr<Tensor>> net_outputs;
  467. for (size_t j = 0; j < outputs[i].size(); j++) {
  468. auto output_tensor = network_record->get_io_tensor(output_names[j]);
  469. auto tmp_out = std::make_shared<Tensor>(
  470. LiteDeviceType::LITE_CPU, output_tensor->get_layout());
  471. output_tensor->reset(
  472. tmp_out->get_memory_ptr(), output_tensor->get_layout());
  473. net_outputs.push_back(tmp_out);
  474. }
  475. network_record->forward();
  476. network_record->wait();
  477. for (size_t j = 0; j < outputs[i].size(); j++) {
  478. auto output_tensor = network_record->get_io_tensor(output_names[j]);
  479. compare_lite_tensor<float>(output_tensor, outputs[i][j]);
  480. }
  481. }
  482. printf("profile the model %s run\n", model_path.c_str());
  483. std::vector<std::shared_ptr<Tensor>> net_outputs;
  484. for (size_t j = 0; j < outputs[0].size(); j++) {
  485. auto output_tensor = network_record->get_io_tensor(output_names[j]);
  486. auto tmp_out = std::make_shared<Tensor>(
  487. LiteDeviceType::LITE_CPU, output_tensor->get_layout());
  488. output_tensor->reset(tmp_out->get_memory_ptr(), output_tensor->get_layout());
  489. net_outputs.push_back(tmp_out);
  490. }
  491. lite::Timer timer("profile");
  492. for (int i = 0; i < 10; i++) {
  493. network_record->forward();
  494. network_record->wait();
  495. }
  496. auto sum_time = timer.get_used_time();
  497. printf("model %s used time average %f ms\n", model_path.c_str(), sum_time / 10);
  498. }
  499. } // namespace
  500. TEST(TestNetWork, OutputNoCopy) {
  501. test_output_no_copy(0);
  502. }
  503. TEST(TestNetWork, OutputNoCopyRecord) {
  504. test_output_no_copy(1);
  505. }
  506. TEST(TestNetWork, IONoCopy) {
  507. test_input_no_copy(0);
  508. }
  509. TEST(TestNetWork, IONoCopyRecord) {
  510. test_input_no_copy(1);
  511. }
  512. TEST(TestNetWork, IONoCopyRecordAx) {
  513. std::vector<std::string> file_names;
  514. #ifndef WIN32
  515. DIR* dirptr = NULL;
  516. struct dirent* dirp;
  517. std::string model_dir = "./ax_models";
  518. dirptr = opendir(model_dir.c_str());
  519. while (dirptr != NULL && (dirp = readdir(dirptr)) != NULL) {
  520. std::string file_name(dirp->d_name);
  521. if (file_name.find(".axe", 0) != std::string::npos) {
  522. file_names.push_back(model_dir + "/" + file_name);
  523. }
  524. }
  525. closedir(dirptr);
  526. #endif
  527. for (auto file_name : file_names) {
  528. printf("test model: %s\n", file_name.c_str());
  529. test_io_no_copy_ax(file_name);
  530. }
  531. }
  532. TEST(TestNetWork, OutputDynamicAlloc) {
  533. Config config;
  534. config.options.force_output_dynamic_alloc = true;
  535. auto tensor = get_input_data("./input_data.npy");
  536. std::string model_path = "./shufflenet.mge";
  537. std::string input_name = "data";
  538. auto result_mgb = mgb_lar(model_path, config, input_name, tensor);
  539. std::shared_ptr<Network> network = std::make_shared<Network>(config);
  540. network->load_model(model_path);
  541. std::shared_ptr<Tensor> input_tensor = network->get_io_tensor(input_name);
  542. auto src_ptr = tensor->get_memory_ptr();
  543. auto src_layout = tensor->get_layout();
  544. input_tensor->reset(src_ptr, src_layout);
  545. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  546. size_t times = 5;
  547. for (size_t i = 0; i < times; i++) {
  548. network->forward();
  549. network->wait();
  550. compare_lite_tensor<float>(output_tensor, result_mgb);
  551. }
  552. }
  553. TEST(TestNetWork, AsyncExec) {
  554. Config config;
  555. config.options.var_sanity_check_first_run = false;
  556. auto tensor = get_input_data("./input_data.npy");
  557. std::string model_path = "./shufflenet.mge";
  558. std::string input_name = "data";
  559. auto result_mgb = mgb_lar(model_path, config, input_name, tensor);
  560. std::shared_ptr<Network> network = std::make_shared<Network>(config);
  561. network->load_model(model_path);
  562. std::shared_ptr<Tensor> input_tensor = network->get_io_tensor(input_name);
  563. auto src_ptr = tensor->get_memory_ptr();
  564. auto src_layout = tensor->get_layout();
  565. input_tensor->reset(src_ptr, src_layout);
  566. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  567. auto result_tensor = std::make_shared<Tensor>(
  568. LiteDeviceType::LITE_CPU, Layout{{1, 1000}, 2, LiteDataType::LITE_FLOAT});
  569. void* out_data = result_tensor->get_memory_ptr();
  570. output_tensor->reset(out_data, result_tensor->get_layout());
  571. //! set async mode and callback
  572. volatile bool finished = false;
  573. network->set_async_callback([&finished]() { finished = true; });
  574. network->forward();
  575. size_t count = 0;
  576. while (finished == false) {
  577. count++;
  578. }
  579. ASSERT_GT(count, 0);
  580. compare_lite_tensor<float>(output_tensor, result_mgb);
  581. }
  582. TEST(TestNetWork, CPUDeviceInput) {
  583. auto tensor = get_input_data("./input_data.npy");
  584. Layout layout{{1, 3, 224, 224}, 4, LiteDataType::LITE_FLOAT};
  585. std::string model_path = "./shufflenet.mge";
  586. std::string input_name = "data";
  587. auto result_mgb = mgb_lar(model_path, {}, input_name, tensor);
  588. NetworkIO IO;
  589. bool is_host = false;
  590. IO.inputs.push_back({input_name, is_host});
  591. std::shared_ptr<Network> network = std::make_shared<Network>(IO);
  592. network->load_model(model_path);
  593. std::shared_ptr<Tensor> input_tensor = network->get_io_tensor(input_name);
  594. auto src_ptr = tensor->get_memory_ptr();
  595. input_tensor->reset(src_ptr, layout);
  596. network->forward();
  597. network->wait();
  598. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  599. compare_lite_tensor<float>(output_tensor, result_mgb);
  600. }
  601. TEST(TestNetWork, ShareTensorWith) {
  602. auto tensor = get_input_data("./input_data.npy");
  603. std::string model_path = "./shufflenet.mge";
  604. std::string input_name = "data";
  605. auto result_mgb = mgb_lar(model_path, {}, input_name, tensor);
  606. std::shared_ptr<Network> network = std::make_shared<Network>();
  607. network->load_model(model_path);
  608. std::shared_ptr<Tensor> input_tensor = network->get_io_tensor(input_name);
  609. input_tensor->share_memory_with(*tensor);
  610. network->forward();
  611. network->wait();
  612. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  613. compare_lite_tensor<float>(output_tensor, result_mgb);
  614. }
  615. TEST(TestNetWork, InputCallBack) {
  616. auto tensor = get_input_data("./input_data.npy");
  617. std::string model_path = "./shufflenet.mge";
  618. std::string input_name = "data";
  619. auto result_mgb = mgb_lar(model_path, {}, input_name, tensor);
  620. NetworkIO ios;
  621. bool is_host = false;
  622. ios.inputs.push_back({input_name, is_host});
  623. std::shared_ptr<Network> network = std::make_shared<Network>(ios);
  624. network->load_model(model_path);
  625. volatile bool finised_check_input = false;
  626. auto input_callback =
  627. [&tensor, &finised_check_input,
  628. input_name](const std::unordered_map<
  629. std::string, std::pair<IO, std::shared_ptr<Tensor>>>&
  630. input_map) {
  631. ASSERT_EQ(input_map.size(), 1);
  632. auto tensor_input = input_map.at(input_name).second;
  633. compare_lite_tensor<float>(tensor_input, tensor);
  634. finised_check_input = true;
  635. };
  636. network->set_start_callback(input_callback);
  637. std::shared_ptr<Tensor> input_tensor = network->get_io_tensor(input_name);
  638. input_tensor->share_memory_with(*tensor);
  639. network->forward();
  640. network->wait();
  641. ASSERT_TRUE(finised_check_input);
  642. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  643. compare_lite_tensor<float>(output_tensor, result_mgb);
  644. }
  645. TEST(TestNetWork, OutputCallBack) {
  646. auto tensor = get_input_data("./input_data.npy");
  647. std::string model_path = "./shufflenet.mge";
  648. std::string input_name = "data";
  649. auto result_mgb = mgb_lar(model_path, {}, input_name, tensor);
  650. std::shared_ptr<Network> network = std::make_shared<Network>();
  651. network->load_model(model_path);
  652. auto output_name = network->get_output_name(0);
  653. volatile bool finised_check_output = false;
  654. auto output_callback =
  655. [&result_mgb, &finised_check_output,
  656. output_name](const std::unordered_map<
  657. std::string, std::pair<IO, std::shared_ptr<Tensor>>>&
  658. output_map) {
  659. ASSERT_EQ(output_map.size(), 1);
  660. auto tensor_output = output_map.at(output_name).second;
  661. compare_lite_tensor<float>(tensor_output, result_mgb);
  662. finised_check_output = true;
  663. };
  664. network->set_finish_callback(output_callback);
  665. std::shared_ptr<Tensor> input_tensor = network->get_io_tensor(input_name);
  666. input_tensor->share_memory_with(*tensor);
  667. network->forward();
  668. network->wait();
  669. ASSERT_TRUE(finised_check_output);
  670. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  671. compare_lite_tensor<float>(output_tensor, result_mgb);
  672. }
  673. TEST(TestNetWork, OutputShapeOnly) {
  674. auto tensor = get_input_data("./input_data.npy");
  675. std::string model_path = "./shufflenet.mge";
  676. std::string input_name = "data";
  677. std::string output_name = "TRUE_DIV(EXP[12065],reduce0[12067])[12077]";
  678. NetworkIO IO;
  679. bool is_host = true;
  680. IO.outputs.push_back({output_name, is_host, LiteIOType::LITE_IO_SHAPE});
  681. Config config;
  682. std::shared_ptr<Network> network = std::make_shared<Network>(config, IO);
  683. network->load_model(model_path);
  684. std::shared_ptr<Tensor> input_tensor = network->get_io_tensor(input_name);
  685. std::shared_ptr<Tensor> output_tensor = network->get_io_tensor(output_name);
  686. auto src_ptr = tensor->get_memory_ptr();
  687. auto src_layout = tensor->get_layout();
  688. input_tensor->reset(src_ptr, src_layout);
  689. network->forward();
  690. network->wait();
  691. ASSERT_EQ(output_tensor->get_tensor_total_size_in_byte() / sizeof(float), 1000);
  692. }
  693. TEST(TestNetWork, ProfileIOdump) {
  694. auto tensor = get_input_data("./input_data.npy");
  695. std::string model_path = "./shufflenet.mge";
  696. std::string input_name = "data";
  697. NetworkIO IO;
  698. Config config;
  699. std::shared_ptr<Network> network = std::make_shared<Network>(config, IO);
  700. network->enable_profile_performance("./profile.json");
  701. network->load_model(model_path);
  702. std::shared_ptr<Tensor> input_tensor = network->get_io_tensor(input_name);
  703. auto src_ptr = tensor->get_memory_ptr();
  704. auto src_layout = tensor->get_layout();
  705. input_tensor->reset(src_ptr, src_layout);
  706. network->forward();
  707. network->wait();
  708. ASSERT_TRUE(fopen("./profile.json", "r"));
  709. Runtime::enable_io_txt_dump(network, "./io_txt_dump.txt");
  710. network->forward();
  711. network->wait();
  712. ASSERT_TRUE(fopen("./io_txt_dump.txt", "r"));
  713. }
  714. TEST(TestNetWork, LoadPackedModel) {
  715. auto tensor = get_input_data("./input_data.npy");
  716. std::string model_path = "./test_packed_model.lite";
  717. std::string input_name = "data";
  718. NetworkIO IO;
  719. Config config;
  720. std::shared_ptr<Network> network = std::make_shared<Network>(config, IO);
  721. network->load_model(model_path);
  722. std::shared_ptr<Tensor> input_tensor = network->get_io_tensor(input_name);
  723. auto src_ptr = tensor->get_memory_ptr();
  724. auto src_layout = tensor->get_layout();
  725. input_tensor->reset(src_ptr, src_layout);
  726. network->forward();
  727. network->wait();
  728. }
  729. TEST(TestNetWork, GetDeviceType) {
  730. auto tensor = get_input_data("./input_data.npy");
  731. std::string model_path = "./shufflenet.mge";
  732. Config config;
  733. std::shared_ptr<Network> network = std::make_shared<Network>(config);
  734. network->load_model(model_path);
  735. ASSERT_TRUE(network->get_device_type() == LiteDeviceType::LITE_CPU);
  736. }
  737. TEST(TestNetWork, GetModelExtraInfo) {
  738. std::string model_path = "./track_640_320_pack_model_rc4_with_info.lite";
  739. Config config;
  740. std::shared_ptr<Network> network = std::make_shared<Network>(config);
  741. network->load_model(model_path);
  742. auto& extra_info = network->get_model_extra_info();
  743. ASSERT_TRUE(extra_info.size() > 0);
  744. printf("extra_info %s \n", extra_info.c_str());
  745. }
  746. #ifndef __IN_TEE_ENV__
  747. #if MGB_ENABLE_JSON
  748. TEST(TestNetWork, GetMemoryInfo) {
  749. Config config;
  750. auto lite_tensor = get_input_data("./input_data.npy");
  751. std::string model_path = "./shufflenet.mge";
  752. auto result_mgb = mgb_lar(model_path, config, "data", lite_tensor);
  753. std::shared_ptr<Network> network = std::make_shared<Network>(config);
  754. Runtime::set_cpu_threads_number(network, 2);
  755. network->load_model(model_path);
  756. network->get_static_memory_alloc_info();
  757. std::shared_ptr<Tensor> input_tensor = network->get_input_tensor(0);
  758. auto src_ptr = lite_tensor->get_memory_ptr();
  759. auto src_layout = lite_tensor->get_layout();
  760. input_tensor->reset(src_ptr, src_layout);
  761. network->forward();
  762. network->wait();
  763. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  764. compare_lite_tensor<float>(output_tensor, result_mgb);
  765. }
  766. #endif
  767. #endif
  768. #if LITE_WITH_CUDA
  769. TEST(TestNetWork, BasicDevice) {
  770. auto lite_tensor = get_input_data("./input_data.npy");
  771. Config config;
  772. config.device_type = LiteDeviceType::LITE_CUDA;
  773. std::string model_path = "./shufflenet.mge";
  774. auto result_lite = mgelite_lar(model_path, config, "data", lite_tensor);
  775. auto result_mgb = mgb_lar(model_path, config, "data", lite_tensor);
  776. compare_lite_tensor<float>(result_lite, result_mgb);
  777. }
  778. TEST(TestNetWork, DeviceInput) {
  779. auto tensor = get_input_data("./input_data.npy");
  780. Layout layout{{1, 3, 224, 224}, 4, LiteDataType::LITE_FLOAT};
  781. std::string model_path = "./shufflenet.mge";
  782. std::string input_name = "data";
  783. auto result_mgb = mgb_lar(model_path, {}, input_name, tensor);
  784. NetworkIO IO;
  785. bool is_host = false;
  786. IO.inputs.push_back({input_name, is_host});
  787. Config config;
  788. config.device_type = LiteDeviceType::LITE_CUDA;
  789. std::shared_ptr<Network> network = std::make_shared<Network>(config, IO);
  790. network->load_model(model_path);
  791. std::shared_ptr<Tensor> input_tensor = network->get_io_tensor(input_name);
  792. auto tensor_cuda = Tensor(LiteDeviceType::LITE_CUDA, layout);
  793. tensor_cuda.copy_from(*tensor);
  794. auto src_ptr = tensor_cuda.get_memory_ptr();
  795. input_tensor->reset(src_ptr, layout);
  796. network->forward();
  797. network->wait();
  798. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  799. compare_lite_tensor<float>(output_tensor, result_mgb);
  800. }
  801. TEST(TestNetWork, ChangeInputShapeDevice) {
  802. Config config;
  803. auto tensor = get_input_data("./input_data.npy");
  804. std::string model_path = "./shufflenet.mge";
  805. std::string input_name = "data";
  806. auto result_mgb = mgb_lar(model_path, config, input_name, tensor);
  807. config.device_type = LiteDeviceType::LITE_CUDA;
  808. std::shared_ptr<Network> network = std::make_shared<Network>(config);
  809. network->load_model(model_path);
  810. std::shared_ptr<Tensor> input_tensor = network->get_io_tensor(input_name);
  811. auto src_layout = Layout{{2, 3, 200, 200}, 4, LiteDataType::LITE_FLOAT};
  812. input_tensor->set_layout(src_layout);
  813. std::shared_ptr<Tensor> input_tensor2 = network->get_io_tensor(input_name);
  814. //! Check memory is equal
  815. ASSERT_EQ(input_tensor->get_memory_ptr(), input_tensor2->get_memory_ptr());
  816. network->forward();
  817. network->wait();
  818. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  819. auto output_layout = output_tensor->get_layout();
  820. ASSERT_EQ(output_layout.shapes[0], 2);
  821. ASSERT_EQ(output_layout.shapes[1], 1000);
  822. }
  823. TEST(TestNetWork, DeviceOutput) {
  824. auto tensor = get_input_data("./input_data.npy");
  825. std::string model_path = "./shufflenet.mge";
  826. std::string input_name = "data";
  827. std::string output_name = "TRUE_DIV(EXP[12065],reduce0[12067])[12077]";
  828. auto result_mgb = mgb_lar(model_path, {}, input_name, tensor);
  829. NetworkIO IO;
  830. bool is_host = false;
  831. IO.outputs.push_back({output_name, is_host});
  832. Config config;
  833. config.device_type = LiteDeviceType::LITE_CUDA;
  834. std::shared_ptr<Network> network = std::make_shared<Network>(config, IO);
  835. network->load_model(model_path);
  836. std::shared_ptr<Tensor> input_tensor = network->get_io_tensor(input_name);
  837. std::shared_ptr<Tensor> output_tensor_cuda = network->get_io_tensor(output_name);
  838. auto src_ptr = tensor->get_memory_ptr();
  839. auto src_layout = tensor->get_layout();
  840. input_tensor->reset(src_ptr, src_layout);
  841. network->forward();
  842. network->wait();
  843. auto output_tensor = std::make_shared<Tensor>();
  844. output_tensor->copy_from(*output_tensor_cuda);
  845. compare_lite_tensor<float>(output_tensor, result_mgb);
  846. }
  847. TEST(TestNetWork, WrongIONameDevice) {
  848. auto tensor = get_input_data("./input_data.npy");
  849. Layout layout{{1, 3, 224, 224}, 4, LiteDataType::LITE_FLOAT};
  850. std::string model_path = "./shufflenet.mge";
  851. std::string input_name = "data";
  852. std::string input_name_wrong = "data0";
  853. std::string output_name = "TRUE_DIV(EXP[12065],reduce0[12067])[12077]";
  854. std::string output_name_wrong = "w_TRUE_DIV(EXP[12065],reduce0[12067])[12077]";
  855. auto result_mgb = mgb_lar(model_path, {}, input_name, tensor);
  856. NetworkIO IO;
  857. bool is_host = false;
  858. IO.inputs.push_back({input_name, is_host});
  859. IO.outputs.push_back({output_name, is_host});
  860. IO.outputs.push_back({output_name_wrong, is_host});
  861. Config config;
  862. config.device_type = LiteDeviceType::LITE_CUDA;
  863. std::shared_ptr<Network> network = std::make_shared<Network>(config, IO);
  864. network->load_model(model_path);
  865. auto tensor_cuda = Tensor(LiteDeviceType::LITE_CUDA, layout);
  866. tensor_cuda.copy_from(*tensor);
  867. std::shared_ptr<Tensor> input_tensor = network->get_io_tensor(input_name);
  868. auto src_ptr = tensor_cuda.get_memory_ptr();
  869. auto src_layout = tensor_cuda.get_layout();
  870. input_tensor->reset(src_ptr, src_layout);
  871. std::shared_ptr<Tensor> output_tensor_cuda = network->get_io_tensor(output_name);
  872. network->forward();
  873. network->wait();
  874. auto output_tensor = std::make_shared<Tensor>();
  875. output_tensor->copy_from(*output_tensor_cuda);
  876. compare_lite_tensor<float>(output_tensor, result_mgb);
  877. }
  878. TEST(TestNetWork, ConfigIONameDevice) {
  879. std::string model_path = "./model.mgb";
  880. NetworkIO IO;
  881. bool is_host = false;
  882. IO.outputs.push_back({"clsfy", 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->compute_only_configured_output();
  887. network->load_model(model_path);
  888. ASSERT_EQ(network->get_all_output_name().size(), 1);
  889. ASSERT_EQ(network->get_all_output_name()[0], "clsfy");
  890. std::shared_ptr<Network> network2 = std::make_shared<Network>(config, IO);
  891. network2->load_model(model_path);
  892. ASSERT_EQ(network2->get_all_output_name().size(), 2);
  893. }
  894. TEST(TestNetWork, SetDeviceIdDeviceTest) {
  895. #if LITE_WITH_CUDA
  896. if (get_device_count(LITE_CUDA) <= 1)
  897. return;
  898. #endif
  899. std::string model_path = "./model.mgb";
  900. NetworkIO IO;
  901. bool is_host = false;
  902. IO.inputs.push_back({"data", is_host});
  903. IO.outputs.push_back({"clsfy", is_host});
  904. Config config;
  905. config.device_type = LiteDeviceType::LITE_CUDA;
  906. std::shared_ptr<Network> network = std::make_shared<Network>(config, IO);
  907. network->set_device_id(1);
  908. network->load_model(model_path);
  909. auto inputs_names = network->get_all_input_name();
  910. for (auto name : inputs_names) {
  911. auto tensor = network->get_io_tensor(name);
  912. ASSERT_EQ(tensor->get_device_id(), 1);
  913. if (name == "idx") {
  914. int* index_ptr = static_cast<int*>(tensor->get_memory_ptr());
  915. for (int i = 0; i < 23; i++) {
  916. index_ptr[i] = i % 3;
  917. }
  918. }
  919. if (name == "landmark") {
  920. float* landmakrk_ptr = static_cast<float*>(tensor->get_memory_ptr());
  921. for (int i = 0; i < 23 * 18 * 2; i++) {
  922. landmakrk_ptr[i] = 0.1f;
  923. }
  924. }
  925. }
  926. auto outputs_names = network->get_all_output_name();
  927. for (auto name : outputs_names) {
  928. auto tensor = network->get_io_tensor(name);
  929. ASSERT_EQ(tensor->get_device_id(), 1);
  930. }
  931. network->forward();
  932. network->wait();
  933. }
  934. TEST(TestNetWork, SetStreamIdDeviceTest) {
  935. std::string model_path = "./model.mgb";
  936. NetworkIO IO;
  937. bool is_host = false;
  938. IO.inputs.push_back({"data", is_host});
  939. IO.outputs.push_back({"clsfy", is_host});
  940. Config config;
  941. config.device_type = LiteDeviceType::LITE_CUDA;
  942. std::shared_ptr<Network> network = std::make_shared<Network>(config, IO);
  943. network->set_stream_id(1);
  944. network->load_model(model_path);
  945. auto inputs_names = network->get_all_input_name();
  946. for (auto name : inputs_names) {
  947. auto tensor = network->get_io_tensor(name);
  948. if (name == "idx") {
  949. int* index_ptr = static_cast<int*>(tensor->get_memory_ptr());
  950. for (int i = 0; i < 23; i++) {
  951. index_ptr[i] = i % 3;
  952. }
  953. }
  954. if (name == "landmark") {
  955. float* landmakrk_ptr = static_cast<float*>(tensor->get_memory_ptr());
  956. for (int i = 0; i < 23 * 18 * 2; i++) {
  957. landmakrk_ptr[i] = 0.1f;
  958. }
  959. }
  960. }
  961. network->forward();
  962. network->wait();
  963. }
  964. #if CUDART_VERSION >= 10000
  965. TEST(TestNetWork, DeviceAsyncExec) {
  966. auto tensor = get_input_data("./input_data.npy");
  967. Config config;
  968. config.device_type = LiteDeviceType::LITE_CUDA;
  969. config.options.var_sanity_check_first_run = false;
  970. std::string model_path = "./shufflenet.mge";
  971. std::string input_name = "data";
  972. auto result_mgb = mgb_lar(model_path, config, input_name, tensor);
  973. std::shared_ptr<Network> network = std::make_shared<Network>(config);
  974. network->load_model(model_path);
  975. std::shared_ptr<Tensor> input_tensor = network->get_io_tensor(input_name);
  976. auto src_ptr = tensor->get_memory_ptr();
  977. auto src_layout = tensor->get_layout();
  978. input_tensor->reset(src_ptr, src_layout);
  979. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  980. auto result_tensor = std::make_shared<Tensor>(
  981. LiteDeviceType::LITE_CPU, Layout{{1, 1000}, 2, LiteDataType::LITE_FLOAT});
  982. void* out_data = result_tensor->get_memory_ptr();
  983. output_tensor->reset(out_data, result_tensor->get_layout());
  984. //! set async mode and callback
  985. volatile bool finished = false;
  986. network->set_async_callback([&finished]() { finished = true; });
  987. network->forward();
  988. size_t count = 0;
  989. while (finished == false) {
  990. count++;
  991. }
  992. ASSERT_GT(count, 0);
  993. compare_lite_tensor<float>(output_tensor, result_mgb);
  994. }
  995. #endif
  996. #endif
  997. #if MGB_ATLAS
  998. TEST(TestNetWork, AtlasLoadNoDevice) {
  999. lite::Config config;
  1000. config.device_type = LiteDeviceType::LITE_DEVICE_DEFAULT;
  1001. auto network = std::make_shared<lite::Network>(config);
  1002. network->load_model("./model_atlas.mgb");
  1003. network->forward();
  1004. network->wait();
  1005. }
  1006. TEST(TestNetWork, AtlasLoadDeviceInput) {
  1007. lite::NetworkIO networkio;
  1008. lite::IO input_data_io = {};
  1009. input_data_io.name = "data";
  1010. input_data_io.is_host = false;
  1011. networkio.inputs.emplace_back(input_data_io);
  1012. lite::IO input_input0_io = {};
  1013. input_input0_io.name = "input0";
  1014. input_input0_io.is_host = false;
  1015. networkio.inputs.emplace_back(input_input0_io);
  1016. lite::Config config;
  1017. config.device_type = LiteDeviceType::LITE_DEVICE_DEFAULT;
  1018. auto network = std::make_shared<lite::Network>(config, networkio);
  1019. network->load_model("./model_atlas.mgb");
  1020. network->forward();
  1021. network->wait();
  1022. }
  1023. TEST(TestNetWork, AtlasLoadAtlas) {
  1024. lite::Config config;
  1025. config.device_type = LiteDeviceType::LITE_ATLAS;
  1026. auto network = std::make_shared<lite::Network>(config);
  1027. network->load_model("./model_atlas.mgb");
  1028. network->forward();
  1029. network->wait();
  1030. }
  1031. TEST(TestNetWork, AtlasLoadAtlasDeviceInput) {
  1032. lite::NetworkIO networkio;
  1033. lite::IO input_data_io = {};
  1034. input_data_io.name = "data";
  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 = "input0";
  1039. input_input0_io.is_host = false;
  1040. networkio.inputs.emplace_back(input_input0_io);
  1041. lite::Config config;
  1042. config.device_type = LiteDeviceType::LITE_ATLAS;
  1043. auto network = std::make_shared<lite::Network>(config, networkio);
  1044. network->load_model("./model_atlas.mgb");
  1045. network->forward();
  1046. network->wait();
  1047. }
  1048. TEST(TestNetWork, AtlasDeviceID) {
  1049. lite::Config config;
  1050. config.device_type = LiteDeviceType::LITE_ATLAS;
  1051. auto network = std::make_shared<lite::Network>(config);
  1052. network->set_device_id(1);
  1053. network->load_model("./model_atlas.mgb");
  1054. std::shared_ptr<Tensor> input_tensor = network->get_input_tensor(0);
  1055. std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
  1056. network->forward();
  1057. network->wait();
  1058. ASSERT_EQ(output_tensor->get_device_id(), 1);
  1059. }
  1060. #endif
  1061. #endif
  1062. // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台