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

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