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_executor_unittest.cc 13 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /**
  2. * Copyright 2021 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 protected public
  18. #define private public
  19. #include "graph/execute/model_executor.h"
  20. #include "graph/manager/graph_manager.h"
  21. #include "graph/manager/graph_var_manager.h"
  22. #include "graph/load/model_manager/model_manager.h"
  23. #include "graph/load/model_manager/davinci_model.h"
  24. using namespace std;
  25. namespace ge {
  26. class UtestModelExecutorTest : public testing::Test {
  27. protected:
  28. void SetUp() {}
  29. void TearDown() {}
  30. };
  31. static NodePtr CreateNode(ComputeGraph &graph, const string &name, const string &type, int in_num, int out_num) {
  32. OpDescPtr op_desc = std::make_shared<OpDesc>(name, type);
  33. op_desc->SetStreamId(0);
  34. static int32_t index = 0;
  35. op_desc->SetId(index++);
  36. GeTensorDesc tensor(GeShape(), FORMAT_ND, DT_INT64);
  37. TensorUtils::SetSize(tensor, 64);
  38. vector<int64_t> input_offset;
  39. for (int i = 0; i < in_num; i++) {
  40. op_desc->AddInputDesc(tensor);
  41. input_offset.emplace_back(index * 64 + i * 64);
  42. }
  43. op_desc->SetInputOffset(input_offset);
  44. vector<int64_t> output_offset;
  45. for (int i = 0; i < out_num; i++) {
  46. op_desc->AddOutputDesc(tensor);
  47. output_offset.emplace_back(index * 64 + in_num * 64 + i * 64);
  48. }
  49. op_desc->SetOutputOffset(output_offset);
  50. op_desc->SetWorkspace({});
  51. op_desc->SetWorkspaceBytes({});
  52. op_desc->SetOpKernelLibName("DNN_VM_RTS_OP_STORE");
  53. return graph.AddNode(op_desc);
  54. }
  55. TEST_F(UtestModelExecutorTest, test_get_total_memory_size) {
  56. ModelExecutor model_executor;
  57. size_t total_mem_size = 0;
  58. EXPECT_EQ(model_executor.GetTotalMemorySize(total_mem_size), SUCCESS);
  59. EXPECT_EQ(total_mem_size, 1024UL * 1024UL * 1024UL);
  60. }
  61. TEST_F(UtestModelExecutorTest, test_load_graph_sync) {
  62. ModelExecutor model_executor;
  63. EXPECT_EQ(model_executor.Initialize({}, 0), SUCCESS);
  64. auto compute_graph = MakeShared<ComputeGraph>("test_graph");
  65. GeRootModelPtr ge_root_model = MakeShared<GeRootModel>(compute_graph);
  66. GeModelPtr ge_model = MakeShared<GeModel>();
  67. ge_model->SetGraph(GraphUtils::CreateGraphFromComputeGraph(compute_graph));
  68. ge_root_model->SetSubgraphInstanceNameToModel(compute_graph->GetName(), ge_model);
  69. GraphId graph_id = 1;
  70. GraphNodePtr graph_node = MakeShared<ge::GraphNode>(graph_id);
  71. graph_node->SetGeRootModel(ge_root_model);
  72. graph_node->SetLoadFlag(true);
  73. graph_node->SetAsync(false);
  74. EXPECT_EQ(model_executor.LoadGraph(ge_root_model, graph_node), SUCCESS);
  75. EXPECT_EQ(model_executor.UnloadGraph(ge_root_model, graph_id), SUCCESS);
  76. EXPECT_EQ(model_executor.Finalize(), SUCCESS);
  77. }
  78. TEST_F(UtestModelExecutorTest, test_load_graph_async) {
  79. ModelExecutor model_executor;
  80. EXPECT_EQ(model_executor.Initialize({}, 0), SUCCESS);
  81. Graph graph("test_graph");
  82. auto compute_graph = MakeShared<ComputeGraph>("test_graph");
  83. GeRootModelPtr ge_root_model = MakeShared<GeRootModel>(compute_graph);
  84. GeModelPtr ge_model = MakeShared<GeModel>();
  85. ge_model->SetGraph(GraphUtils::CreateGraphFromComputeGraph(compute_graph));
  86. ge_root_model->SetSubgraphInstanceNameToModel(compute_graph->GetName(), ge_model);
  87. GraphId graph_id = 1;
  88. GraphNodePtr graph_node = MakeShared<ge::GraphNode>(graph_id);
  89. graph_node->SetGeRootModel(ge_root_model);
  90. graph_node->SetLoadFlag(true);
  91. graph_node->SetAsync(true);
  92. EXPECT_EQ(model_executor.LoadGraph(ge_root_model, graph_node), SUCCESS);
  93. EXPECT_EQ(model_executor.UnloadGraph(ge_root_model, graph_id), SUCCESS);
  94. EXPECT_EQ(model_executor.Finalize(), SUCCESS);
  95. }
  96. TEST_F(UtestModelExecutorTest, test_load_graph_failed) {
  97. ModelExecutor model_executor;
  98. EXPECT_EQ(model_executor.Initialize({}, 0), SUCCESS);
  99. Graph graph("test_graph");
  100. auto compute_graph = MakeShared<ComputeGraph>("test_graph");
  101. GeRootModelPtr ge_root_model = MakeShared<GeRootModel>(compute_graph);
  102. GraphId graph_id = 1;
  103. GraphNodePtr graph_node = MakeShared<ge::GraphNode>(graph_id);
  104. graph_node->SetGeRootModel(ge_root_model);
  105. graph_node->SetLoadFlag(true);
  106. graph_node->SetAsync(true);
  107. // GeModel is null, DavinciModel::Assign will return FAILED
  108. setenv(kEnvGeuseStaticMemory, "1", true);
  109. EXPECT_EQ(model_executor.LoadGraph(ge_root_model, graph_node), FAILED);
  110. EXPECT_EQ(model_executor.UnloadGraph(ge_root_model, graph_id), SUCCESS);
  111. EXPECT_EQ(model_executor.Finalize(), SUCCESS);
  112. unsetenv(kEnvGeuseStaticMemory);
  113. }
  114. TEST_F(UtestModelExecutorTest, test_check_and_release_memory) {
  115. {
  116. auto listener = MakeShared<RunAsyncListener>();
  117. shared_ptr<DavinciModel> davinci_model1 = MakeShared<DavinciModel>(1, listener);
  118. davinci_model1->SetId(1);
  119. ModelManager::GetInstance()->InsertModel(1, davinci_model1);
  120. shared_ptr<DavinciModel> davinci_model2 = MakeShared<DavinciModel>(2, listener);
  121. davinci_model1->SetId(2);
  122. ModelManager::GetInstance()->InsertModel(2, davinci_model2);
  123. }
  124. ModelExecutor model_executor;
  125. EXPECT_EQ(model_executor.Initialize({}, 0), SUCCESS);
  126. GeModelPtr ge_model = make_shared<GeModel>();
  127. int64_t memory_size = 25 * 1024UL * 1024UL * 1024UL;
  128. int64_t weight_size = 25 * 1024UL * 1024UL * 1024UL;
  129. uint64_t session_id = 0;
  130. EXPECT_TRUE(AttrUtils::SetInt(ge_model, ATTR_MODEL_MEMORY_SIZE, memory_size));
  131. EXPECT_TRUE(AttrUtils::SetInt(ge_model, ATTR_MODEL_WEIGHT_SIZE, weight_size));
  132. EXPECT_TRUE(AttrUtils::SetInt(ge_model, MODEL_ATTR_SESSION_ID, session_id));
  133. GraphId graph_id = 1;
  134. GraphNodePtr graph_node = MakeShared<GraphNode>(graph_id);
  135. model_executor.AddGraphNode(graph_id, graph_node);
  136. ComputeGraphPtr compute_graph = MakeShared<ComputeGraph>("test_graph");
  137. GeRootModelPtr ge_root_model = MakeShared<GeRootModel>(compute_graph);
  138. ge_root_model->SetModelId(1);
  139. ge_root_model->SetModelId(2);
  140. graph_node->SetGeRootModel(ge_root_model);
  141. graph_node->SetLoadFlag(true);
  142. EXPECT_EQ(model_executor.CheckAndReleaseMemory(ge_model, graph_node), SUCCESS);
  143. EXPECT_EQ(model_executor.Finalize(), SUCCESS);
  144. }
  145. TEST_F(UtestModelExecutorTest, parse_inputs_dims_data) {
  146. ModelExecutor model_executor;
  147. EXPECT_EQ(model_executor.Initialize({}, 0), SUCCESS);
  148. OmeContext context;
  149. SetLocalOmeContext(context);
  150. ComputeGraphPtr compute_graph = MakeShared<ComputeGraph>("test_graph");
  151. const auto data1 = CreateNode(*compute_graph, DATA, "data1", 1, 1);
  152. const auto next1 = CreateNode(*compute_graph, GETNEXT, "data1", 1, 1);
  153. Tensor tensor;
  154. std::vector<Tensor> input_tensors;
  155. input_tensors.emplace_back(tensor);
  156. EXPECT_EQ(model_executor.ParseInputsDims(input_tensors), SUCCESS); // dynamic_node_type is empty, just return
  157. context.dynamic_node_type = DATA;
  158. EXPECT_EQ(model_executor.ParseInputsDims(input_tensors), SUCCESS); // ParseInputsDimsForData
  159. context.getnext_nosink_nodes.emplace_back(next1);
  160. EXPECT_EQ(model_executor.ParseInputsDims(input_tensors), SUCCESS); // ParseInputsDimsForGetNexNosinkAndData
  161. EXPECT_EQ(model_executor.Finalize(), SUCCESS);
  162. }
  163. TEST_F(UtestModelExecutorTest, parse_inputs_dims_getnext) {
  164. ModelExecutor model_executor;
  165. EXPECT_EQ(model_executor.Initialize({}, 0), SUCCESS);
  166. OmeContext context;
  167. SetLocalOmeContext(context);
  168. ComputeGraphPtr compute_graph = MakeShared<ComputeGraph>("test_graph");
  169. const auto data1 = CreateNode(*compute_graph, DATA, "data1", 1, 1);
  170. const auto next1 = CreateNode(*compute_graph, GETNEXT, "data1", 1, 1);
  171. Tensor tensor;
  172. std::vector<Tensor> input_tensors;
  173. input_tensors.emplace_back(tensor);
  174. context.dynamic_node_type = GETNEXT;
  175. EXPECT_EQ(model_executor.ParseInputsDims(input_tensors), SUCCESS); // just getnext_sink
  176. context.getnext_nosink_nodes.emplace_back(next1);
  177. EXPECT_EQ(model_executor.ParseInputsDims(input_tensors), SUCCESS); // ParseInputsDimsForData
  178. context.data_nodes.emplace_back(data1);
  179. EXPECT_EQ(model_executor.ParseInputsDims(input_tensors), PARAM_INVALID); // ParseInputsDimsForGetNexNosinkAndData
  180. AttrUtils::SetInt(next1->GetOpDesc(), ATTR_NAME_INDEX, 0);
  181. EXPECT_EQ(model_executor.ParseInputsDims(input_tensors), SUCCESS); // ParseInputsDimsForGetNexNosinkAndData
  182. EXPECT_EQ(model_executor.Finalize(), SUCCESS);
  183. }
  184. TEST_F(UtestModelExecutorTest, test_run_thread) {
  185. ModelExecutor model_executor;
  186. EXPECT_EQ(model_executor.Initialize({}, 0), SUCCESS);
  187. GraphId graph_id = 1;
  188. uint64_t session_id = 0;
  189. error_message::Context error_context;
  190. GEThreadLocalContext context;
  191. const auto callback = [](Status status, std::vector<ge::Tensor> &outputs) { };
  192. auto compute_graph = MakeShared<ComputeGraph>("test_graph");
  193. GeRootModelPtr ge_root_model = MakeShared<GeRootModel>(compute_graph);
  194. GeModelPtr ge_model = MakeShared<GeModel>();
  195. ge_model->SetGraph(GraphUtils::CreateGraphFromComputeGraph(compute_graph));
  196. ge_root_model->SetSubgraphInstanceNameToModel(compute_graph->GetName(), ge_model);
  197. GraphNodePtr graph_node = MakeShared<ge::GraphNode>(graph_id);
  198. graph_node->SetGeRootModel(ge_root_model);
  199. graph_node->SetLoadFlag(false);
  200. graph_node->SetAsync(true);
  201. graph_node->IncreaseLoadCount();
  202. graph_node->Lock();
  203. Tensor tensor;
  204. std::vector<Tensor> input_tensors;
  205. input_tensors.emplace_back(tensor);
  206. RunArgs run_args{graph_node, graph_id, session_id, error_context, input_tensors, ge_root_model, context, callback};
  207. EXPECT_EQ(model_executor.PushGraph(run_args), SUCCESS);
  208. while (model_executor.run_args_q_.Size() > 0) {
  209. usleep(10); // 0.01ms, Wait for RunThread.
  210. }
  211. EXPECT_EQ(model_executor.Finalize(), SUCCESS);
  212. }
  213. static void test_run_graph(ModelExecutor &model_executor) {
  214. auto compute_graph = MakeShared<ComputeGraph>("test_graph");
  215. GeRootModelPtr ge_root_model = MakeShared<GeRootModel>(compute_graph);
  216. GeModelPtr ge_model = MakeShared<GeModel>();
  217. ge_model->SetGraph(GraphUtils::CreateGraphFromComputeGraph(compute_graph));
  218. ge_root_model->SetSubgraphInstanceNameToModel(compute_graph->GetName(), ge_model);
  219. GraphId graph_id = 1;
  220. GraphNodePtr graph_node = MakeShared<ge::GraphNode>(graph_id);
  221. graph_node->SetGeRootModel(ge_root_model);
  222. graph_node->SetLoadFlag(false);
  223. graph_node->SetAsync(false); // RunGraph is Synchronization.
  224. EXPECT_EQ(model_executor.LoadGraph(ge_root_model, graph_node), SUCCESS);
  225. std::vector<GeTensor> inputs;
  226. std::vector<GeTensor> outputs;
  227. EXPECT_EQ(model_executor.RunGraph(graph_node, graph_id, inputs, outputs), SUCCESS);
  228. }
  229. TEST_F(UtestModelExecutorTest, test_run_graph_train) {
  230. GetThreadLocalContext().SetGlobalOption({{OPTION_GRAPH_RUN_MODE, "1"}});
  231. ModelExecutor model_executor;
  232. EXPECT_EQ(model_executor.Initialize({}, 0), SUCCESS);
  233. test_run_graph(model_executor);
  234. EXPECT_EQ(model_executor.Finalize(), SUCCESS);
  235. }
  236. TEST_F(UtestModelExecutorTest, test_run_graph_infer) {
  237. GetThreadLocalContext().SetGlobalOption({});
  238. GetThreadLocalContext().SetSessionOption({});
  239. GetThreadLocalContext().SetGraphOption({});
  240. ModelExecutor model_executor;
  241. EXPECT_EQ(model_executor.Initialize({}, 0), SUCCESS);
  242. test_run_graph(model_executor);
  243. EXPECT_EQ(model_executor.Finalize(), SUCCESS);
  244. }
  245. TEST_F(UtestModelExecutorTest, test_run_graph_with_stream) {
  246. ModelExecutor model_executor;
  247. EXPECT_EQ(model_executor.Initialize({}, 0), SUCCESS);
  248. GraphId graph_id = 1;
  249. auto compute_graph = MakeShared<ComputeGraph>("test_graph");
  250. GeRootModelPtr ge_root_model = MakeShared<GeRootModel>(compute_graph);
  251. GeModelPtr ge_model = MakeShared<GeModel>();
  252. ge_model->SetGraph(GraphUtils::CreateGraphFromComputeGraph(compute_graph));
  253. ge_root_model->SetSubgraphInstanceNameToModel(compute_graph->GetName(), ge_model);
  254. GraphNodePtr graph_node = MakeShared<ge::GraphNode>(graph_id);
  255. graph_node->SetGeRootModel(ge_root_model);
  256. graph_node->SetLoadFlag(false);
  257. graph_node->SetAsync(true);
  258. GeTensor tensor;
  259. std::vector<GeTensor> inputs{tensor};
  260. std::vector<GeTensor> outputs;
  261. rtStream_t stream = nullptr;
  262. rtStreamCreate(&stream, 0);
  263. EXPECT_EQ(model_executor.RunGraphWithStream(graph_node, graph_id, stream, inputs, outputs), 145003);
  264. EXPECT_EQ(model_executor.Finalize(), SUCCESS);
  265. rtStreamDestroy(stream);
  266. }
  267. } // namespace ge

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