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

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