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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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), 0, 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, 0, 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. // Error for: LoadModelPartitionTable: Invalid partition_table->num:0
  195. EXPECT_EQ(mm.LoadModelOffline(model_id, data, nullptr, nullptr), ACL_ERROR_GE_PARAM_INVALID);
  196. delete[](uint8_t *) data.model_data;
  197. }
  198. TEST_F(UtestModelManagerModelManager, case_load_model_data_success) {
  199. ModelData data;
  200. LoadStandardModelData(data);
  201. uint32_t model_id = 1;
  202. ModelManager mm;
  203. EXPECT_EQ(mm.LoadModelOffline(model_id, data, nullptr, nullptr), SUCCESS);
  204. delete[](uint8_t *) data.model_data;
  205. }
  206. /*
  207. shared_ptr<ModelListener> LabelCallBack(new DModelListener());
  208. // test HandleCommand
  209. TEST_F(UtestModelManagerModelManager, command_success1) {
  210. ModelManager manager;
  211. Command cmd;
  212. cmd.cmd_type = "INFERENCE";
  213. EXPECT_EQ(PARAM_INVALID, manager.HandleCommand(cmd));
  214. cmd.cmd_type = "NOT SUPPORT";
  215. EXPECT_EQ(PARAM_INVALID, manager.HandleCommand(cmd));
  216. }
  217. TEST_F(UtestModelManagerModelManager, command_success2) {
  218. ModelManager manager;
  219. Command cmd;
  220. cmd.cmd_type = "dump";
  221. cmd.cmd_params.push_back("status");
  222. cmd.cmd_params.push_back("on");
  223. cmd.cmd_params.push_back("model_name");
  224. cmd.cmd_params.push_back("test_model");
  225. cmd.cmd_params.push_back("path");
  226. cmd.cmd_params.push_back("/test");
  227. cmd.cmd_params.push_back("layer");
  228. cmd.cmd_params.push_back("layer1");
  229. EXPECT_EQ(SUCCESS, manager.HandleCommand(cmd));
  230. }
  231. // test profile
  232. TEST_F(UtestModelManagerModelManager, command_profile_success) {
  233. ModelManager manager;
  234. Command cmd;
  235. cmd.cmd_type = "profile";
  236. cmd.cmd_params.push_back("ome");
  237. cmd.cmd_params.push_back("on");
  238. EXPECT_EQ(SUCCESS, manager.HandleCommand(cmd));
  239. bool ome_profile_on = PropertiesManager::Instance().GetPropertyValue(OME_PROFILE) == "1";
  240. EXPECT_EQ(true, ome_profile_on);
  241. cmd.cmd_params.clear();
  242. cmd.cmd_params.push_back("ome");
  243. cmd.cmd_params.push_back("off");
  244. EXPECT_EQ(SUCCESS, manager.HandleCommand(cmd));
  245. ome_profile_on = PropertiesManager::Instance().GetPropertyValue(OME_PROFILE) == "1";
  246. EXPECT_FALSE(ome_profile_on);
  247. cmd.cmd_params.clear();
  248. cmd.cmd_params.push_back("cce");
  249. cmd.cmd_params.push_back("on");
  250. EXPECT_EQ(SUCCESS, manager.HandleCommand(cmd));
  251. bool cce_profile_on = PropertiesManager::Instance().GetPropertyValue(CCE_PROFILE) == "1";
  252. EXPECT_EQ(true, cce_profile_on);
  253. cmd.cmd_params.clear();
  254. cmd.cmd_params.push_back("cce");
  255. cmd.cmd_params.push_back("off");
  256. EXPECT_EQ(SUCCESS, manager.HandleCommand(cmd));
  257. cce_profile_on = PropertiesManager::Instance().GetPropertyValue(CCE_PROFILE) == "1";
  258. EXPECT_FALSE(cce_profile_on);
  259. cmd.cmd_params.clear();
  260. cmd.cmd_params.push_back("runtime");
  261. cmd.cmd_params.push_back("on");
  262. EXPECT_EQ(SUCCESS, manager.HandleCommand(cmd));
  263. bool rts_profile_on = PropertiesManager::Instance().GetPropertyValue(RTS_PROFILE) == "1";
  264. EXPECT_EQ(true, rts_profile_on);
  265. cmd.cmd_params.clear();
  266. cmd.cmd_params.push_back("runtime");
  267. cmd.cmd_params.push_back("off");
  268. EXPECT_EQ(SUCCESS, manager.HandleCommand(cmd));
  269. rts_profile_on = PropertiesManager::Instance().GetPropertyValue(RTS_PROFILE) == "1";
  270. EXPECT_FALSE(rts_profile_on);
  271. cmd.cmd_params.clear();
  272. cmd.cmd_params.push_back("profiler_jobctx");
  273. cmd.cmd_params.push_back("jobctx");
  274. EXPECT_EQ(SUCCESS, manager.HandleCommand(cmd));
  275. EXPECT_EQ("jobctx", PropertiesManager::Instance().GetPropertyValue(PROFILER_JOBCTX));
  276. cmd.cmd_params.clear();
  277. cmd.cmd_params.push_back("profiler_target_path");
  278. cmd.cmd_params.push_back("/test/target");
  279. EXPECT_EQ(SUCCESS, manager.HandleCommand(cmd));
  280. EXPECT_EQ("/test/target", PropertiesManager::Instance().GetPropertyValue(PROFILER_TARGET_PATH));
  281. cmd.cmd_params.clear();
  282. cmd.cmd_params.push_back("RTS_PATH");
  283. cmd.cmd_params.push_back("/test/rts_path");
  284. EXPECT_EQ(SUCCESS, manager.HandleCommand(cmd));
  285. EXPECT_EQ("/test/rts_path", PropertiesManager::Instance().GetPropertyValue(RTS_PROFILE_PATH));
  286. }
  287. // test acl profiling
  288. TEST_F(UtestModelManagerModelManager, command_profiling) {
  289. ModelManager manager;
  290. Command cmd;
  291. cmd.cmd_type = "profiling";
  292. cmd.cmd_params.push_back("config");
  293. cmd.cmd_params.push_back("on");
  294. EXPECT_EQ(SUCCESS, manager.HandleCommand(cmd));
  295. }
  296. TEST_F(UtestModelManagerModelManager, command_profile_failed) {
  297. ModelManager manager;
  298. Command cmd;
  299. cmd.cmd_type = "profile";
  300. cmd.cmd_params.push_back("ome");
  301. EXPECT_EQ(PARAM_INVALID, manager.HandleCommand(cmd));
  302. }
  303. // test Start
  304. TEST_F(UtestModelManagerModelManager, start_fail) {
  305. ModelManager manager;
  306. manager.model_map_[2] = nullptr;
  307. EXPECT_EQ(PARAM_INVALID, manager.Start(2));
  308. }
  309. // test GetMaxUsedMemory
  310. TEST_F(UtestModelManagerModelManager, get_max_used_memory_fail) {
  311. ModelManager manager;
  312. uint64_t max_size = 0;
  313. manager.model_map_[2] = nullptr;
  314. EXPECT_EQ(PARAM_INVALID, manager.GetMaxUsedMemory(2, max_size));
  315. }
  316. // test GetInputOutputDescInfo
  317. TEST_F(UtestModelManagerModelManager, get_input_output_desc_info_fail) {
  318. ModelManager manager;
  319. manager.model_map_[2] = nullptr;
  320. vector<InputOutputDescInfo> input_shape;
  321. vector<InputOutputDescInfo> output_shape;
  322. EXPECT_EQ(PARAM_INVALID, manager.GetInputOutputDescInfo(2, input_shape, output_shape));
  323. }
  324. *//*
  325. // test GetInputOutputDescInfo fail
  326. TEST_F(UtestModelManagerModelManager, get_input_output_desc_info_zero_copy_fail) {
  327. ModelManager manager;
  328. manager.model_map_[2] = nullptr;
  329. vector<InputOutputDescInfo> input_shape;
  330. vector<InputOutputDescInfo> output_shape;
  331. EXPECT_EQ(PARAM_INVALID, manager.GetInputOutputDescInfoForZeroCopy(2, input_shape, output_shape));
  332. }
  333. *//*
  334. // test Stop
  335. TEST_F(UtestModelManagerModelManager, stop_fail) {
  336. ModelManager manager;
  337. manager.model_map_[2] = nullptr;
  338. EXPECT_EQ(PARAM_INVALID, manager.Stop(2));
  339. }
  340. // build input_data
  341. TEST_F(UtestModelManagerModelManager, check_data_len_success) {
  342. shared_ptr<ModelListener> g_label_call_back(new DModelListener());
  343. DavinciModel model(0, g_label_call_back);
  344. ModelManager model_manager;
  345. InputData input_data;
  346. DataBuffer data_buffer;
  347. data_buffer.data = new char[51200];
  348. data_buffer.length = 51200;
  349. input_data.index = 0;
  350. input_data.model_id = 1;
  351. input_data.blobs.push_back(data_buffer);
  352. delete[](char *) data_buffer.data;
  353. }
  354. // test LoadModeldef
  355. TEST_F(UtestModelManagerModelManager, destroy_aicpu_session) {
  356. ModelManager manager;
  357. manager.DestroyAicpuSession(0);
  358. manager.sess_ids_.insert(0);
  359. manager.DestroyAicpuSession(0);
  360. }*/
  361. // test DataInputTensor
  362. TEST_F(UtestModelManagerModelManager, test_data_input_tensor) {
  363. shared_ptr<ModelListener> g_label_call_back(nullptr);
  364. auto model = std::make_shared<DavinciModel>(0, g_label_call_back);
  365. ModelManager mm;
  366. uint32_t model_id = 1;
  367. mm.model_map_[1] = model;
  368. mm.hybrid_model_map_[1] = std::make_shared<hybrid::HybridDavinciModel>();
  369. ge::Tensor input_tensor;
  370. vector<ge::Tensor> inputs;
  371. inputs.emplace_back(input_tensor);
  372. auto ret = mm.DataInputTensor(model_id,inputs);
  373. EXPECT_EQ(PARAM_INVALID, ret); // HybridDavinciModel::impl_ is null.
  374. }
  375. TEST_F(UtestModelManagerModelManager, test_launch_kernel_cust_aicpu) {
  376. ModelManager mm;
  377. // cust_aicpu_so_ is empty.
  378. EXPECT_EQ(mm.LaunchKernelCustAicpuSo("empty_cust_aicpu"), SUCCESS);
  379. // deleteCustOp after Launch will deleted.
  380. uintptr_t resource_id = 1; // for rtCtxGetCurrent stub
  381. std::vector<char> kernel_bin(256);
  382. auto &cust_resource_001 = mm.cust_aicpu_so_[resource_id];
  383. auto tbe_kernel = std::shared_ptr<OpKernelBin>(new OpKernelBin("deleteCustOp", std::move(kernel_bin)));
  384. auto &cust_opkernel_001 = cust_resource_001["deleteCustOp"] = tbe_kernel;
  385. EXPECT_FALSE(mm.cust_aicpu_so_.empty());
  386. EXPECT_EQ(mm.LaunchKernelCustAicpuSo("deleteCustOp"), SUCCESS);
  387. EXPECT_TRUE(mm.cust_aicpu_so_.empty());
  388. }
  389. } // namespace ge

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