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

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

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