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
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
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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/load/model_manager/davinci_model_parser.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((uint8_t *)data.model_data + sizeof(ModelFileHeader), 10, 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 GenEncryptModelData(ModelData &data) {
  62. const int model_len = 10;
  63. data.key = ENC_KEY;
  64. data.model_data = new uint8_t[data.model_len];
  65. uint8_t data_ori[model_len];
  66. memset(data_ori, 10, model_len);
  67. ModelFileHeader *header = (ModelFileHeader *)data.model_data;
  68. header->magic = MODEL_FILE_MAGIC_NUM;
  69. header->version = MODEL_VERSION;
  70. header->is_encrypt = ModelEncryptType::ENCRYPTED;
  71. header->length = 10; // encrypt_len;
  72. }
  73. void LoadStandardModelData(ModelData &data) {
  74. data.model_len = 512;
  75. data.model_data = new uint8_t[data.model_len];
  76. uint8_t *model_data = reinterpret_cast<uint8_t *>(data.model_data);
  77. uint32_t mem_offset = sizeof(ModelFileHeader);
  78. ModelPartitionTable *partition_table = reinterpret_cast<ModelPartitionTable *>(model_data + mem_offset);
  79. partition_table->num = PARTITION_SIZE;
  80. mem_offset += sizeof(ModelPartitionTable) + sizeof(ModelPartitionMemInfo) * 5;
  81. {
  82. Model model;
  83. ComputeGraphPtr graph = make_shared<ComputeGraph>("default");
  84. model.SetGraph(GraphUtils::CreateGraphFromComputeGraph(graph));
  85. model.SetVersion(123);
  86. Buffer buffer;
  87. model.Save(buffer);
  88. EXPECT_TRUE(mem_offset + buffer.GetSize() < 512);
  89. memcpy(model_data + mem_offset, buffer.GetData(), buffer.GetSize());
  90. ModelPartitionMemInfo &partition_info = partition_table->partition[0];
  91. partition_info.type = ModelPartitionType::MODEL_DEF;
  92. partition_info.mem_size = buffer.GetSize();
  93. mem_offset += buffer.GetSize();
  94. }
  95. {
  96. ModelPartitionMemInfo &partition_info = partition_table->partition[1];
  97. partition_info.type = ModelPartitionType::WEIGHTS_DATA;
  98. partition_info.mem_offset = mem_offset;
  99. partition_info.mem_size = 0;
  100. }
  101. {
  102. ModelPartitionMemInfo &partition_info = partition_table->partition[2];
  103. partition_info.type = ModelPartitionType::TASK_INFO;
  104. partition_info.mem_offset = mem_offset;
  105. partition_info.mem_size = 0;
  106. }
  107. {
  108. ModelPartitionMemInfo &partition_info = partition_table->partition[3];
  109. partition_info.type = ModelPartitionType::TBE_KERNELS;
  110. partition_info.mem_offset = mem_offset;
  111. partition_info.mem_size = 0;
  112. }
  113. {
  114. ModelPartitionMemInfo &partition_info = partition_table->partition[4];
  115. partition_info.type = ModelPartitionType::CUST_AICPU_KERNELS;
  116. partition_info.mem_offset = mem_offset;
  117. partition_info.mem_size = 0;
  118. }
  119. EXPECT_TRUE(mem_offset < 512);
  120. ModelFileHeader *header = new (data.model_data) ModelFileHeader;
  121. header->length = mem_offset - sizeof(ModelFileHeader);
  122. data.model_len = mem_offset;
  123. }
  124. };
  125. class DModelListener : public ModelListener {
  126. public:
  127. DModelListener(){};
  128. uint32_t OnComputeDone(uint32_t model_id, uint32_t data_index, uint32_t resultCode) { return 0; }
  129. };
  130. TEST_F(UtestModelManagerModelManager, case_load_incorrect_param) {
  131. ModelManager mm;
  132. uint32_t model_id = 0;
  133. ModelData data;
  134. // Load allow listener is null
  135. EXPECT_EQ(mm.LoadModelOffline(model_id, data, nullptr, nullptr), ACL_ERROR_GE_EXEC_MODEL_DATA_SIZE_INVALID);
  136. }
  137. TEST_F(UtestModelManagerModelManager, case_load_model_len_too_short) {
  138. ModelManager mm;
  139. ModelData data;
  140. data.model_len = 10;
  141. data.model_data = (void *)&data;
  142. uint32_t model_id = 1;
  143. EXPECT_EQ(mm.LoadModelOffline(model_id, data, nullptr, nullptr), ACL_ERROR_GE_PARAM_INVALID);
  144. data.model_data = nullptr;
  145. }
  146. TEST_F(UtestModelManagerModelManager, case_load_model_len_not_match) {
  147. ModelManager mm;
  148. ModelData data;
  149. GenUnencryptModelData(data);
  150. data.model_len = sizeof(ModelFileHeader) + 1;
  151. uint32_t model_id = 1;
  152. EXPECT_EQ(mm.LoadModelOffline(model_id, data, nullptr, nullptr), ACL_ERROR_GE_PARAM_INVALID);
  153. delete[](uint8_t *) data.model_data;
  154. }
  155. TEST_F(UtestModelManagerModelManager, case_load_model_encypt_not_match) {
  156. ModelManager mm;
  157. ModelData data;
  158. GenUnencryptModelData(data);
  159. data.key = ENC_KEY;
  160. uint32_t model_id = 1;
  161. EXPECT_EQ(mm.LoadModelOffline(model_id, data, nullptr, nullptr), ACL_ERROR_GE_PARAM_INVALID);
  162. delete[](uint8_t *) data.model_data;
  163. }
  164. TEST_F(UtestModelManagerModelManager, case_load_model_encypt_type_unsupported) {
  165. ModelManager mm;
  166. ModelData data;
  167. GenUnencryptModelData(data);
  168. ModelFileHeader *header = (ModelFileHeader *)data.model_data;
  169. header->is_encrypt = 255;
  170. uint32_t model_id = 1;
  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. auto input_tensor = InputTensorInfo();
  346. vector<InputTensorInfo> inputs;
  347. inputs.emplace_back(input_tensor);
  348. auto ret = mm.DataInputTensor(model_id,inputs);
  349. EXPECT_EQ(UNSUPPORTED, ret);
  350. }
  351. } // namespace ge

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