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 12 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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_load_graph_sync) {
  56. ModelExecutor model_executor;
  57. EXPECT_EQ(model_executor.Initialize({}, 0), SUCCESS);
  58. auto compute_graph = MakeShared<ComputeGraph>("test_graph");
  59. GeRootModelPtr ge_root_model = MakeShared<GeRootModel>(compute_graph);
  60. GeModelPtr ge_model = MakeShared<GeModel>();
  61. ge_model->SetGraph(GraphUtils::CreateGraphFromComputeGraph(compute_graph));
  62. ge_root_model->SetSubgraphInstanceNameToModel(compute_graph->GetName(), ge_model);
  63. GraphId graph_id = 1;
  64. GraphNodePtr graph_node = MakeShared<ge::GraphNode>(graph_id);
  65. graph_node->SetGeRootModel(ge_root_model);
  66. graph_node->SetLoadFlag(true);
  67. graph_node->SetAsync(false);
  68. EXPECT_EQ(model_executor.LoadGraph(ge_root_model, graph_node), SUCCESS);
  69. EXPECT_EQ(model_executor.UnloadGraph(ge_root_model, graph_id), SUCCESS);
  70. EXPECT_EQ(model_executor.Finalize(), SUCCESS);
  71. }
  72. TEST_F(UtestModelExecutorTest, test_load_graph_async) {
  73. ModelExecutor model_executor;
  74. EXPECT_EQ(model_executor.Initialize({}, 0), SUCCESS);
  75. Graph graph("test_graph");
  76. auto compute_graph = MakeShared<ComputeGraph>("test_graph");
  77. GeRootModelPtr ge_root_model = MakeShared<GeRootModel>(compute_graph);
  78. GeModelPtr ge_model = MakeShared<GeModel>();
  79. ge_model->SetGraph(GraphUtils::CreateGraphFromComputeGraph(compute_graph));
  80. ge_root_model->SetSubgraphInstanceNameToModel(compute_graph->GetName(), ge_model);
  81. GraphId graph_id = 1;
  82. GraphNodePtr graph_node = MakeShared<ge::GraphNode>(graph_id);
  83. graph_node->SetGeRootModel(ge_root_model);
  84. graph_node->SetLoadFlag(true);
  85. graph_node->SetAsync(true);
  86. EXPECT_EQ(model_executor.LoadGraph(ge_root_model, graph_node), SUCCESS);
  87. EXPECT_EQ(model_executor.UnloadGraph(ge_root_model, graph_id), SUCCESS);
  88. EXPECT_EQ(model_executor.Finalize(), SUCCESS);
  89. }
  90. TEST_F(UtestModelExecutorTest, test_load_graph_failed) {
  91. ModelExecutor model_executor;
  92. EXPECT_EQ(model_executor.Initialize({}, 0), SUCCESS);
  93. Graph graph("test_graph");
  94. auto compute_graph = MakeShared<ComputeGraph>("test_graph");
  95. GeRootModelPtr ge_root_model = MakeShared<GeRootModel>(compute_graph);
  96. GraphId graph_id = 1;
  97. GraphNodePtr graph_node = MakeShared<ge::GraphNode>(graph_id);
  98. graph_node->SetGeRootModel(ge_root_model);
  99. graph_node->SetLoadFlag(true);
  100. graph_node->SetAsync(true);
  101. // GeModel is null, DavinciModel::Assign will return FAILED
  102. setenv(kEnvGeuseStaticMemory, "1", true);
  103. EXPECT_EQ(model_executor.LoadGraph(ge_root_model, graph_node), FAILED);
  104. EXPECT_EQ(model_executor.UnloadGraph(ge_root_model, graph_id), SUCCESS);
  105. EXPECT_EQ(model_executor.Finalize(), SUCCESS);
  106. unsetenv(kEnvGeuseStaticMemory);
  107. }
  108. TEST_F(UtestModelExecutorTest, test_check_and_release_memory) {
  109. {
  110. auto listener = MakeShared<RunAsyncListener>();
  111. shared_ptr<DavinciModel> davinci_model1 = MakeShared<DavinciModel>(1, listener);
  112. davinci_model1->SetId(1);
  113. ModelManager::GetInstance()->InsertModel(1, davinci_model1);
  114. shared_ptr<DavinciModel> davinci_model2 = MakeShared<DavinciModel>(2, listener);
  115. davinci_model1->SetId(2);
  116. ModelManager::GetInstance()->InsertModel(2, davinci_model2);
  117. }
  118. ModelExecutor model_executor;
  119. EXPECT_EQ(model_executor.Initialize({}, 0), SUCCESS);
  120. GeModelPtr ge_model = make_shared<GeModel>();
  121. int64_t memory_size = 25 * 1024UL * 1024UL * 1024UL;
  122. int64_t weight_size = 25 * 1024UL * 1024UL * 1024UL;
  123. uint64_t session_id = 0;
  124. EXPECT_TRUE(AttrUtils::SetInt(ge_model, ATTR_MODEL_MEMORY_SIZE, memory_size));
  125. EXPECT_TRUE(AttrUtils::SetInt(ge_model, ATTR_MODEL_WEIGHT_SIZE, weight_size));
  126. EXPECT_TRUE(AttrUtils::SetInt(ge_model, MODEL_ATTR_SESSION_ID, session_id));
  127. GraphId graph_id = 1;
  128. GraphNodePtr graph_node = MakeShared<GraphNode>(graph_id);
  129. model_executor.AddGraphNode(graph_id, graph_node);
  130. ComputeGraphPtr compute_graph = MakeShared<ComputeGraph>("test_graph");
  131. GeRootModelPtr ge_root_model = MakeShared<GeRootModel>(compute_graph);
  132. ge_root_model->SetModelId(1);
  133. ge_root_model->SetModelId(2);
  134. graph_node->SetGeRootModel(ge_root_model);
  135. graph_node->SetLoadFlag(true);
  136. EXPECT_EQ(model_executor.CheckAndReleaseMemory(ge_model, graph_node), SUCCESS);
  137. EXPECT_EQ(model_executor.Finalize(), SUCCESS);
  138. }
  139. TEST_F(UtestModelExecutorTest, parse_inputs_dims_data) {
  140. ModelExecutor model_executor;
  141. EXPECT_EQ(model_executor.Initialize({}, 0), SUCCESS);
  142. OmeContext context;
  143. SetLocalOmeContext(context);
  144. ComputeGraphPtr compute_graph = MakeShared<ComputeGraph>("test_graph");
  145. const auto data1 = CreateNode(*compute_graph, DATA, "data1", 1, 1);
  146. const auto next1 = CreateNode(*compute_graph, GETNEXT, "data1", 1, 1);
  147. Tensor tensor;
  148. std::vector<Tensor> input_tensors;
  149. input_tensors.emplace_back(tensor);
  150. EXPECT_EQ(model_executor.ParseInputsDims(input_tensors), SUCCESS); // dynamic_node_type is empty, just return
  151. context.dynamic_node_type = DATA;
  152. EXPECT_EQ(model_executor.ParseInputsDims(input_tensors), SUCCESS); // ParseInputsDimsForData
  153. context.getnext_nosink_nodes.emplace_back(next1);
  154. EXPECT_EQ(model_executor.ParseInputsDims(input_tensors), SUCCESS); // ParseInputsDimsForGetNexNosinkAndData
  155. EXPECT_EQ(model_executor.Finalize(), SUCCESS);
  156. }
  157. TEST_F(UtestModelExecutorTest, parse_inputs_dims_getnext) {
  158. ModelExecutor model_executor;
  159. EXPECT_EQ(model_executor.Initialize({}, 0), SUCCESS);
  160. OmeContext context;
  161. SetLocalOmeContext(context);
  162. ComputeGraphPtr compute_graph = MakeShared<ComputeGraph>("test_graph");
  163. const auto data1 = CreateNode(*compute_graph, DATA, "data1", 1, 1);
  164. const auto next1 = CreateNode(*compute_graph, GETNEXT, "data1", 1, 1);
  165. Tensor tensor;
  166. std::vector<Tensor> input_tensors;
  167. input_tensors.emplace_back(tensor);
  168. context.dynamic_node_type = GETNEXT;
  169. EXPECT_EQ(model_executor.ParseInputsDims(input_tensors), SUCCESS); // just getnext_sink
  170. context.getnext_nosink_nodes.emplace_back(next1);
  171. EXPECT_EQ(model_executor.ParseInputsDims(input_tensors), SUCCESS); // ParseInputsDimsForData
  172. context.data_nodes.emplace_back(data1);
  173. EXPECT_EQ(model_executor.ParseInputsDims(input_tensors), PARAM_INVALID); // ParseInputsDimsForGetNexNosinkAndData
  174. AttrUtils::SetInt(next1->GetOpDesc(), ATTR_NAME_INDEX, 0);
  175. EXPECT_EQ(model_executor.ParseInputsDims(input_tensors), SUCCESS); // ParseInputsDimsForGetNexNosinkAndData
  176. EXPECT_EQ(model_executor.Finalize(), SUCCESS);
  177. }
  178. TEST_F(UtestModelExecutorTest, test_run_thread) {
  179. ModelExecutor model_executor;
  180. EXPECT_EQ(model_executor.Initialize({}, 0), SUCCESS);
  181. GraphId graph_id = 1;
  182. uint64_t session_id = 0;
  183. error_message::Context error_context;
  184. GEThreadLocalContext context;
  185. const auto callback = [](Status status, std::vector<ge::Tensor> &outputs) { };
  186. auto compute_graph = MakeShared<ComputeGraph>("test_graph");
  187. GeRootModelPtr ge_root_model = MakeShared<GeRootModel>(compute_graph);
  188. GeModelPtr ge_model = MakeShared<GeModel>();
  189. ge_model->SetGraph(GraphUtils::CreateGraphFromComputeGraph(compute_graph));
  190. ge_root_model->SetSubgraphInstanceNameToModel(compute_graph->GetName(), ge_model);
  191. GraphNodePtr graph_node = MakeShared<ge::GraphNode>(graph_id);
  192. graph_node->SetGeRootModel(ge_root_model);
  193. graph_node->SetLoadFlag(false);
  194. graph_node->SetAsync(true);
  195. graph_node->IncreaseLoadCount();
  196. graph_node->Lock();
  197. Tensor tensor;
  198. std::vector<Tensor> input_tensors;
  199. input_tensors.emplace_back(tensor);
  200. RunArgs run_args{graph_node, graph_id, session_id, error_context, input_tensors, ge_root_model, context, callback};
  201. EXPECT_EQ(model_executor.PushGraph(run_args), SUCCESS);
  202. while (model_executor.run_args_q_.Size() > 0) {
  203. usleep(10); // 0.01ms, Wait for RunThread.
  204. }
  205. EXPECT_EQ(model_executor.Finalize(), SUCCESS);
  206. }
  207. static void test_run_graph(ModelExecutor &model_executor) {
  208. auto compute_graph = MakeShared<ComputeGraph>("test_graph");
  209. GeRootModelPtr ge_root_model = MakeShared<GeRootModel>(compute_graph);
  210. GeModelPtr ge_model = MakeShared<GeModel>();
  211. ge_model->SetGraph(GraphUtils::CreateGraphFromComputeGraph(compute_graph));
  212. ge_root_model->SetSubgraphInstanceNameToModel(compute_graph->GetName(), ge_model);
  213. GraphId graph_id = 1;
  214. GraphNodePtr graph_node = MakeShared<ge::GraphNode>(graph_id);
  215. graph_node->SetGeRootModel(ge_root_model);
  216. graph_node->SetLoadFlag(false);
  217. graph_node->SetAsync(false); // RunGraph is Synchronization.
  218. EXPECT_EQ(model_executor.LoadGraph(ge_root_model, graph_node), SUCCESS);
  219. std::vector<GeTensor> inputs;
  220. std::vector<GeTensor> outputs;
  221. EXPECT_EQ(model_executor.RunGraph(graph_node, graph_id, inputs, outputs), SUCCESS);
  222. }
  223. TEST_F(UtestModelExecutorTest, test_run_graph_train) {
  224. GetThreadLocalContext().SetGlobalOption({{OPTION_GRAPH_RUN_MODE, "1"}});
  225. ModelExecutor model_executor;
  226. EXPECT_EQ(model_executor.Initialize({}, 0), SUCCESS);
  227. test_run_graph(model_executor);
  228. EXPECT_EQ(model_executor.Finalize(), SUCCESS);
  229. }
  230. TEST_F(UtestModelExecutorTest, test_run_graph_infer) {
  231. GetThreadLocalContext().SetGlobalOption({});
  232. GetThreadLocalContext().SetSessionOption({});
  233. GetThreadLocalContext().SetGraphOption({});
  234. ModelExecutor model_executor;
  235. EXPECT_EQ(model_executor.Initialize({}, 0), SUCCESS);
  236. test_run_graph(model_executor);
  237. EXPECT_EQ(model_executor.Finalize(), SUCCESS);
  238. }
  239. TEST_F(UtestModelExecutorTest, test_run_graph_with_stream) {
  240. ModelExecutor model_executor;
  241. EXPECT_EQ(model_executor.Initialize({}, 0), SUCCESS);
  242. GraphId graph_id = 1;
  243. auto compute_graph = MakeShared<ComputeGraph>("test_graph");
  244. GeRootModelPtr ge_root_model = MakeShared<GeRootModel>(compute_graph);
  245. GeModelPtr ge_model = MakeShared<GeModel>();
  246. ge_model->SetGraph(GraphUtils::CreateGraphFromComputeGraph(compute_graph));
  247. ge_root_model->SetSubgraphInstanceNameToModel(compute_graph->GetName(), ge_model);
  248. GraphNodePtr graph_node = MakeShared<ge::GraphNode>(graph_id);
  249. graph_node->SetGeRootModel(ge_root_model);
  250. graph_node->SetLoadFlag(false);
  251. graph_node->SetAsync(true);
  252. GeTensor tensor;
  253. std::vector<GeTensor> inputs{tensor};
  254. std::vector<GeTensor> outputs;
  255. rtStream_t stream = nullptr;
  256. rtStreamCreate(&stream, 0);
  257. EXPECT_EQ(model_executor.RunGraphWithStream(graph_node, graph_id, stream, inputs, outputs), 145003);
  258. EXPECT_EQ(model_executor.Finalize(), SUCCESS);
  259. rtStreamDestroy(stream);
  260. }
  261. } // namespace ge

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