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

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