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.

model_manager_unittest.cc 16 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /**
  2. * Copyright 2019-2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <gtest/gtest.h>
  17. #define private public
  18. #define protected public
  19. #include "graph/load/model_manager/model_manager.h"
  20. #include "common/helper/om_file_helper.h"
  21. #include "graph/utils/graph_utils.h"
  22. #include "graph/debug/ge_attr_define.h"
  23. #include "common/op/ge_op_utils.h"
  24. #include "graph/load/graph_loader.h"
  25. #include "graph/load/model_manager/davinci_model.h"
  26. #include "graph/ops_stub.h"
  27. #include "common/profiling/profiling_manager.h"
  28. using namespace std;
  29. using namespace testing;
  30. namespace ge {
  31. const static std::string ENC_KEY = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
  32. class UtestModelManagerModelManager : public testing::Test {
  33. protected:
  34. static Status LoadStub(const uint8_t *data, size_t len, Model &model) {
  35. InitModelDefault(model);
  36. return SUCCESS;
  37. }
  38. static void InitModelDefault(Model &model) {
  39. AttrUtils::SetInt(&model, ATTR_MODEL_MEMORY_SIZE, 0);
  40. AttrUtils::SetInt(&model, ATTR_MODEL_WEIGHT_SIZE, 0);
  41. AttrUtils::SetInt(&model, ATTR_MODEL_STREAM_NUM, 0);
  42. AttrUtils::SetInt(&model, ATTR_MODEL_EVENT_NUM, 0);
  43. AttrUtils::SetStr(&model, ATTR_MODEL_TARGET_TYPE, "MINI"); // domi::MINI
  44. auto computeGraph = std::make_shared<ComputeGraph>("graph");
  45. auto graph = GraphUtils::CreateGraphFromComputeGraph(computeGraph);
  46. model.SetGraph(graph);
  47. }
  48. void SetUp() {}
  49. void TearDown() {}
  50. void GenUnencryptModelData(ModelData &data) {
  51. const int model_len = 10;
  52. data.model_len = sizeof(ModelFileHeader) + model_len;
  53. data.model_data = new uint8_t[data.model_len];
  54. memset(data.model_data, 0, data.model_len);
  55. ModelFileHeader *header = (ModelFileHeader *)data.model_data;
  56. header->magic = MODEL_FILE_MAGIC_NUM;
  57. header->version = MODEL_VERSION;
  58. header->is_encrypt = ModelEncryptType::UNENCRYPTED;
  59. header->length = model_len;
  60. header->is_checksum = ModelCheckType::CHECK;
  61. }
  62. void LoadStandardModelData(ModelData &data) {
  63. data.model_len = 512;
  64. data.model_data = new uint8_t[data.model_len];
  65. uint8_t *model_data = reinterpret_cast<uint8_t *>(data.model_data);
  66. uint32_t mem_offset = sizeof(ModelFileHeader);
  67. ModelPartitionTable *partition_table = reinterpret_cast<ModelPartitionTable *>(model_data + mem_offset);
  68. partition_table->num = PARTITION_SIZE;
  69. mem_offset += sizeof(ModelPartitionTable) + sizeof(ModelPartitionMemInfo) * 5;
  70. {
  71. Model model;
  72. ComputeGraphPtr graph = make_shared<ComputeGraph>("default");
  73. model.SetGraph(GraphUtils::CreateGraphFromComputeGraph(graph));
  74. model.SetVersion(123);
  75. Buffer buffer;
  76. model.Save(buffer);
  77. EXPECT_TRUE(mem_offset + buffer.GetSize() < 512);
  78. memcpy(model_data + mem_offset, buffer.GetData(), buffer.GetSize());
  79. ModelPartitionMemInfo &partition_info = partition_table->partition[0];
  80. partition_info.type = ModelPartitionType::MODEL_DEF;
  81. partition_info.mem_size = buffer.GetSize();
  82. mem_offset += buffer.GetSize();
  83. }
  84. {
  85. ModelPartitionMemInfo &partition_info = partition_table->partition[1];
  86. partition_info.type = ModelPartitionType::WEIGHTS_DATA;
  87. partition_info.mem_offset = mem_offset;
  88. partition_info.mem_size = 0;
  89. }
  90. {
  91. ModelPartitionMemInfo &partition_info = partition_table->partition[2];
  92. partition_info.type = ModelPartitionType::TASK_INFO;
  93. partition_info.mem_offset = mem_offset;
  94. partition_info.mem_size = 0;
  95. }
  96. {
  97. ModelPartitionMemInfo &partition_info = partition_table->partition[3];
  98. partition_info.type = ModelPartitionType::TBE_KERNELS;
  99. partition_info.mem_offset = mem_offset;
  100. partition_info.mem_size = 0;
  101. }
  102. {
  103. ModelPartitionMemInfo &partition_info = partition_table->partition[4];
  104. partition_info.type = ModelPartitionType::CUST_AICPU_KERNELS;
  105. partition_info.mem_offset = mem_offset;
  106. partition_info.mem_size = 0;
  107. }
  108. EXPECT_TRUE(mem_offset < 512);
  109. ModelFileHeader *header = new (data.model_data) ModelFileHeader;
  110. header->length = mem_offset - sizeof(ModelFileHeader);
  111. data.model_len = mem_offset;
  112. }
  113. };
  114. class DModelListener : public ModelListener {
  115. public:
  116. DModelListener(){};
  117. uint32_t OnComputeDone(uint32_t model_id, uint32_t data_index,
  118. uint32_t resultCode, std::vector<ge::Tensor> &outputs) { return 0; }
  119. };
  120. TEST_F(UtestModelManagerModelManager, case_is_need_hybrid_load) {
  121. ModelManager mm;
  122. uint32_t model_id = 0;
  123. ComputeGraphPtr root_graph = std::make_shared<ComputeGraph>("graph");
  124. ge::GeRootModel model;
  125. EXPECT_EQ(mm.IsNeedHybridLoad(model), false);
  126. model.SetRootGraph(root_graph);
  127. EXPECT_EQ(mm.IsNeedHybridLoad(model), false);
  128. }
  129. TEST_F(UtestModelManagerModelManager, case_load_incorrect_param) {
  130. ModelManager mm;
  131. uint32_t model_id = 0;
  132. ModelData data;
  133. // Load allow listener is null
  134. EXPECT_EQ(mm.LoadModelOffline(model_id, data, nullptr, nullptr), ACL_ERROR_GE_EXEC_MODEL_DATA_SIZE_INVALID);
  135. }
  136. TEST_F(UtestModelManagerModelManager, case_load_model_len_too_short) {
  137. ModelManager mm;
  138. ModelData data;
  139. data.model_len = 10;
  140. data.model_data = (void *)&data;
  141. uint32_t model_id = 1;
  142. EXPECT_EQ(mm.LoadModelOffline(model_id, data, nullptr, nullptr), ACL_ERROR_GE_PARAM_INVALID);
  143. data.model_data = nullptr;
  144. }
  145. TEST_F(UtestModelManagerModelManager, case_load_model_len_not_match) {
  146. ModelManager mm;
  147. ModelData data;
  148. GenUnencryptModelData(data);
  149. data.model_len = sizeof(ModelFileHeader) + 1;
  150. uint32_t model_id = 1;
  151. EXPECT_EQ(mm.LoadModelOffline(model_id, data, nullptr, nullptr), ACL_ERROR_GE_PARAM_INVALID);
  152. delete[](uint8_t *) data.model_data;
  153. }
  154. TEST_F(UtestModelManagerModelManager, case_load_model_encypt_not_match) {
  155. ModelManager mm;
  156. ModelData data;
  157. GenUnencryptModelData(data);
  158. data.key = ENC_KEY;
  159. uint32_t model_id = 1;
  160. EXPECT_EQ(mm.LoadModelOffline(model_id, data, nullptr, nullptr), ACL_ERROR_GE_PARAM_INVALID);
  161. delete[](uint8_t *) data.model_data;
  162. }
  163. TEST_F(UtestModelManagerModelManager, case_load_model_encypt_type_unsupported) {
  164. ModelManager mm;
  165. ModelData data;
  166. GenUnencryptModelData(data);
  167. ModelFileHeader *header = (ModelFileHeader *)data.model_data;
  168. header->is_encrypt = 255;
  169. uint32_t model_id = 1;
  170. // Error for: LoadModelPartitionTable: Invalid partition_table->num:0
  171. EXPECT_EQ(mm.LoadModelOffline(model_id, data, nullptr, nullptr), ACL_ERROR_GE_PARAM_INVALID);
  172. delete[](uint8_t *) data.model_data;
  173. }
  174. TEST_F(UtestModelManagerModelManager, case_load_model_data_success) {
  175. ModelData data;
  176. LoadStandardModelData(data);
  177. uint32_t model_id = 1;
  178. ModelManager mm;
  179. EXPECT_EQ(mm.LoadModelOffline(model_id, data, nullptr, nullptr), SUCCESS);
  180. delete[](uint8_t *) data.model_data;
  181. }
  182. /*
  183. shared_ptr<ModelListener> LabelCallBack(new DModelListener());
  184. // test HandleCommand
  185. TEST_F(UtestModelManagerModelManager, command_success1) {
  186. ModelManager manager;
  187. Command cmd;
  188. cmd.cmd_type = "INFERENCE";
  189. EXPECT_EQ(PARAM_INVALID, manager.HandleCommand(cmd));
  190. cmd.cmd_type = "NOT SUPPORT";
  191. EXPECT_EQ(PARAM_INVALID, manager.HandleCommand(cmd));
  192. }
  193. TEST_F(UtestModelManagerModelManager, command_success2) {
  194. ModelManager manager;
  195. Command cmd;
  196. cmd.cmd_type = "dump";
  197. cmd.cmd_params.push_back("status");
  198. cmd.cmd_params.push_back("on");
  199. cmd.cmd_params.push_back("model_name");
  200. cmd.cmd_params.push_back("test_model");
  201. cmd.cmd_params.push_back("path");
  202. cmd.cmd_params.push_back("/test");
  203. cmd.cmd_params.push_back("layer");
  204. cmd.cmd_params.push_back("layer1");
  205. EXPECT_EQ(SUCCESS, manager.HandleCommand(cmd));
  206. }
  207. // test profile
  208. TEST_F(UtestModelManagerModelManager, command_profile_success) {
  209. ModelManager manager;
  210. Command cmd;
  211. cmd.cmd_type = "profile";
  212. cmd.cmd_params.push_back("ome");
  213. cmd.cmd_params.push_back("on");
  214. EXPECT_EQ(SUCCESS, manager.HandleCommand(cmd));
  215. bool ome_profile_on = PropertiesManager::Instance().GetPropertyValue(OME_PROFILE) == "1";
  216. EXPECT_EQ(true, ome_profile_on);
  217. cmd.cmd_params.clear();
  218. cmd.cmd_params.push_back("ome");
  219. cmd.cmd_params.push_back("off");
  220. EXPECT_EQ(SUCCESS, manager.HandleCommand(cmd));
  221. ome_profile_on = PropertiesManager::Instance().GetPropertyValue(OME_PROFILE) == "1";
  222. EXPECT_FALSE(ome_profile_on);
  223. cmd.cmd_params.clear();
  224. cmd.cmd_params.push_back("cce");
  225. cmd.cmd_params.push_back("on");
  226. EXPECT_EQ(SUCCESS, manager.HandleCommand(cmd));
  227. bool cce_profile_on = PropertiesManager::Instance().GetPropertyValue(CCE_PROFILE) == "1";
  228. EXPECT_EQ(true, cce_profile_on);
  229. cmd.cmd_params.clear();
  230. cmd.cmd_params.push_back("cce");
  231. cmd.cmd_params.push_back("off");
  232. EXPECT_EQ(SUCCESS, manager.HandleCommand(cmd));
  233. cce_profile_on = PropertiesManager::Instance().GetPropertyValue(CCE_PROFILE) == "1";
  234. EXPECT_FALSE(cce_profile_on);
  235. cmd.cmd_params.clear();
  236. cmd.cmd_params.push_back("runtime");
  237. cmd.cmd_params.push_back("on");
  238. EXPECT_EQ(SUCCESS, manager.HandleCommand(cmd));
  239. bool rts_profile_on = PropertiesManager::Instance().GetPropertyValue(RTS_PROFILE) == "1";
  240. EXPECT_EQ(true, rts_profile_on);
  241. cmd.cmd_params.clear();
  242. cmd.cmd_params.push_back("runtime");
  243. cmd.cmd_params.push_back("off");
  244. EXPECT_EQ(SUCCESS, manager.HandleCommand(cmd));
  245. rts_profile_on = PropertiesManager::Instance().GetPropertyValue(RTS_PROFILE) == "1";
  246. EXPECT_FALSE(rts_profile_on);
  247. cmd.cmd_params.clear();
  248. cmd.cmd_params.push_back("profiler_jobctx");
  249. cmd.cmd_params.push_back("jobctx");
  250. EXPECT_EQ(SUCCESS, manager.HandleCommand(cmd));
  251. EXPECT_EQ("jobctx", PropertiesManager::Instance().GetPropertyValue(PROFILER_JOBCTX));
  252. cmd.cmd_params.clear();
  253. cmd.cmd_params.push_back("profiler_target_path");
  254. cmd.cmd_params.push_back("/test/target");
  255. EXPECT_EQ(SUCCESS, manager.HandleCommand(cmd));
  256. EXPECT_EQ("/test/target", PropertiesManager::Instance().GetPropertyValue(PROFILER_TARGET_PATH));
  257. cmd.cmd_params.clear();
  258. cmd.cmd_params.push_back("RTS_PATH");
  259. cmd.cmd_params.push_back("/test/rts_path");
  260. EXPECT_EQ(SUCCESS, manager.HandleCommand(cmd));
  261. EXPECT_EQ("/test/rts_path", PropertiesManager::Instance().GetPropertyValue(RTS_PROFILE_PATH));
  262. }
  263. // test acl profiling
  264. TEST_F(UtestModelManagerModelManager, command_profiling) {
  265. ModelManager manager;
  266. Command cmd;
  267. cmd.cmd_type = "profiling";
  268. cmd.cmd_params.push_back("config");
  269. cmd.cmd_params.push_back("on");
  270. EXPECT_EQ(SUCCESS, manager.HandleCommand(cmd));
  271. }
  272. TEST_F(UtestModelManagerModelManager, command_profile_failed) {
  273. ModelManager manager;
  274. Command cmd;
  275. cmd.cmd_type = "profile";
  276. cmd.cmd_params.push_back("ome");
  277. EXPECT_EQ(PARAM_INVALID, manager.HandleCommand(cmd));
  278. }
  279. // test Start
  280. TEST_F(UtestModelManagerModelManager, start_fail) {
  281. ModelManager manager;
  282. manager.model_map_[2] = nullptr;
  283. EXPECT_EQ(PARAM_INVALID, manager.Start(2));
  284. }
  285. // test GetMaxUsedMemory
  286. TEST_F(UtestModelManagerModelManager, get_max_used_memory_fail) {
  287. ModelManager manager;
  288. uint64_t max_size = 0;
  289. manager.model_map_[2] = nullptr;
  290. EXPECT_EQ(PARAM_INVALID, manager.GetMaxUsedMemory(2, max_size));
  291. }
  292. // test GetInputOutputDescInfo
  293. TEST_F(UtestModelManagerModelManager, get_input_output_desc_info_fail) {
  294. ModelManager manager;
  295. manager.model_map_[2] = nullptr;
  296. vector<InputOutputDescInfo> input_shape;
  297. vector<InputOutputDescInfo> output_shape;
  298. EXPECT_EQ(PARAM_INVALID, manager.GetInputOutputDescInfo(2, input_shape, output_shape));
  299. }
  300. *//*
  301. // test GetInputOutputDescInfo fail
  302. TEST_F(UtestModelManagerModelManager, get_input_output_desc_info_zero_copy_fail) {
  303. ModelManager manager;
  304. manager.model_map_[2] = nullptr;
  305. vector<InputOutputDescInfo> input_shape;
  306. vector<InputOutputDescInfo> output_shape;
  307. EXPECT_EQ(PARAM_INVALID, manager.GetInputOutputDescInfoForZeroCopy(2, input_shape, output_shape));
  308. }
  309. *//*
  310. // test Stop
  311. TEST_F(UtestModelManagerModelManager, stop_fail) {
  312. ModelManager manager;
  313. manager.model_map_[2] = nullptr;
  314. EXPECT_EQ(PARAM_INVALID, manager.Stop(2));
  315. }
  316. // build input_data
  317. TEST_F(UtestModelManagerModelManager, check_data_len_success) {
  318. shared_ptr<ModelListener> g_label_call_back(new DModelListener());
  319. DavinciModel model(0, g_label_call_back);
  320. ModelManager model_manager;
  321. InputData input_data;
  322. DataBuffer data_buffer;
  323. data_buffer.data = new char[51200];
  324. data_buffer.length = 51200;
  325. input_data.index = 0;
  326. input_data.model_id = 1;
  327. input_data.blobs.push_back(data_buffer);
  328. delete[](char *) data_buffer.data;
  329. }
  330. // test LoadModeldef
  331. TEST_F(UtestModelManagerModelManager, destroy_aicpu_session) {
  332. ModelManager manager;
  333. manager.DestroyAicpuSession(0);
  334. manager.sess_ids_.insert(0);
  335. manager.DestroyAicpuSession(0);
  336. }*/
  337. // test DataInputTensor
  338. TEST_F(UtestModelManagerModelManager, test_data_input_tensor) {
  339. shared_ptr<ModelListener> g_label_call_back(nullptr);
  340. auto model = std::make_shared<DavinciModel>(0, g_label_call_back);
  341. ModelManager mm;
  342. uint32_t model_id = 1;
  343. mm.model_map_[1] = model;
  344. mm.hybrid_model_map_[1] = std::make_shared<hybrid::HybridDavinciModel>();
  345. ge::Tensor input_tensor;
  346. vector<ge::Tensor> inputs;
  347. inputs.emplace_back(input_tensor);
  348. auto ret = mm.DataInputTensor(model_id,inputs);
  349. EXPECT_EQ(PARAM_INVALID, ret); // HybridDavinciModel::impl_ is null.
  350. }
  351. TEST_F(UtestModelManagerModelManager, test_launch_kernel_cust_aicpu) {
  352. ModelManager mm;
  353. // cust_aicpu_so_ is empty.
  354. EXPECT_EQ(mm.LaunchKernelCustAicpuSo("empty_cust_aicpu"), SUCCESS);
  355. // deleteCustOp after Launch will deleted.
  356. uintptr_t resource_id = 1; // for rtCtxGetCurrent stub
  357. std::vector<char> kernel_bin(256);
  358. auto &cust_resource_001 = mm.cust_aicpu_so_[resource_id];
  359. auto tbe_kernel = std::shared_ptr<OpKernelBin>(new OpKernelBin("deleteCustOp", std::move(kernel_bin)));
  360. auto &cust_opkernel_001 = cust_resource_001["deleteCustOp"] = tbe_kernel;
  361. EXPECT_FALSE(mm.cust_aicpu_so_.empty());
  362. EXPECT_EQ(mm.LaunchKernelCustAicpuSo("deleteCustOp"), SUCCESS);
  363. EXPECT_TRUE(mm.cust_aicpu_so_.empty());
  364. }
  365. shared_ptr<ModelListener> listerner(new DModelListener());
  366. TEST_F(UtestModelManagerModelManager, test_load_model_online) {
  367. ModelManager mm;
  368. uint32_t model_id = 1;
  369. ComputeGraphPtr graph = std::make_shared<ComputeGraph>("test");
  370. GeRootModelPtr ge_root_model = make_shared<GeRootModel>(graph);
  371. auto &profiling_manager = ge::ProfilingManager::Instance();
  372. profiling_manager.SetSubscribeInfo(0, model_id, true);
  373. Status ret = mm.LoadModelOnline(model_id, ge_root_model, listerner);
  374. profiling_manager.CleanSubscribeInfo();
  375. }
  376. TEST_F(UtestModelManagerModelManager, command_profiling) {
  377. ModelManager manager;
  378. uint32_t model_id = 1;
  379. Command cmd;
  380. auto model = std::make_shared<DavinciModel>(1, listerner);
  381. model->SetId(model_id);
  382. cmd.cmd_params.push_back("modelId");
  383. cmd.cmd_params.push_back(to_string(model_id));
  384. auto &profiling_manager = ge::ProfilingManager::Instance();
  385. profiling_manager.SetSubscribeInfo(0, model_id, true);
  386. Status ret = manager.HandleProfModelUnsubscribeCommand(cmd);
  387. profiling_manager.CleanSubscribeInfo();
  388. }
  389. } // namespace ge

图引擎模块(GE)是MindSpore的一个子模块,其代码由C++实现,位于前端模块ME和底层硬件之间,起到承接作用。图引擎模块以ME下发的图作为输入,然后进行一系列的深度图优化操作,最后输出一张可以在底层硬件上高效运行的图。GE针对昇腾AI处理器的硬件结构特点,做了特定的优化工作,以此来充分发挥出昇腾AI处理器的强大算力。在进行模型训练/推理时,GE会被自动调用而用户并不感知。GE主要由GE API和GE Core两部分组成,详细的架构图如下所示