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.

ge_hybrid_unittest.cc 36 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
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
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
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. /**
  2. * Copyright 2019-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. #include <gmock/gmock.h>
  18. #include <vector>
  19. #include "runtime/rt.h"
  20. #define protected public
  21. #define private public
  22. #include "graph/utils/node_utils.h"
  23. #include "hybrid/model/hybrid_model_builder.h"
  24. #include "hybrid/model/hybrid_model.h"
  25. #include "hybrid/node_executor/node_executor.h"
  26. #include "model/ge_model.h"
  27. #include "model/ge_root_model.h"
  28. #include "hybrid/node_executor/aicore/aicore_op_task.h"
  29. #include "framework/common/taskdown_common.h"
  30. #include "framework/common/debug/log.h"
  31. #include "graph/ge_context.h"
  32. #include "hybrid/executor/hybrid_execution_context.h"
  33. #include "hybrid/executor/hybrid_model_executor.h"
  34. #include "hybrid/node_executor/aicore/aicore_task_builder.h"
  35. #include "graph/load/model_manager/tbe_handle_store.h"
  36. #include "graph/manager/graph_mem_allocator.h"
  37. #include "hybrid/common/npu_memory_allocator.h"
  38. #include "graph/types.h"
  39. #include "graph/utils/tensor_utils.h"
  40. #include "graph/testcase/ge_graph/graph_builder_utils.h"
  41. #include "graph/op_desc_impl.h"
  42. using namespace std;
  43. namespace ge {
  44. using namespace hybrid;
  45. class UtestGeHybrid : public testing::Test {
  46. protected:
  47. void SetUp() {}
  48. void TearDown() {
  49. NpuMemoryAllocator::allocators_.clear();
  50. }
  51. };
  52. static ge::OpDescPtr CreateOpDesc(string name = "", string type = "", int in_num = 0, int out_num = 0) {
  53. auto op_desc = std::make_shared<ge::OpDesc>(name, type);
  54. op_desc->SetStreamId(0);
  55. static int32_t index = 0;
  56. op_desc->SetId(index++);
  57. GeTensorDesc tensor(GeShape(), FORMAT_ND, DT_INT64);
  58. TensorUtils::SetSize(tensor, 64);
  59. vector<int64_t> input_offset;
  60. for (int i = 0; i < in_num; ++i) {
  61. op_desc->AddInputDesc(tensor);
  62. input_offset.emplace_back(index * 64 + i * 64);
  63. }
  64. op_desc->SetInputOffset(input_offset);
  65. vector<int64_t> output_offset;
  66. for (int i = 0; i < out_num; ++i) {
  67. op_desc->AddOutputDesc(tensor);
  68. output_offset.emplace_back(index * 64 + in_num * 64 + i * 64);
  69. }
  70. op_desc->SetOutputOffset(output_offset);
  71. op_desc->SetWorkspace({});
  72. op_desc->SetWorkspaceBytes({});
  73. ge::AttrUtils::SetStr(op_desc, ge::TVM_ATTR_NAME_MAGIC, "RT_DEV_BINARY_MAGIC_ELF_AIVEC");
  74. bool support_dynamic = true;
  75. ge::AttrUtils::GetBool(op_desc, "support_dynamicshape", support_dynamic);
  76. return op_desc;
  77. }
  78. TEST_F(UtestGeHybrid, aicore_op_task_init_success) {
  79. // build aicore task
  80. auto aicore_task = std::unique_ptr<hybrid::AiCoreOpTask>(new(std::nothrow)hybrid::AiCoreOpTask());
  81. domi::TaskDef task_def;
  82. task_def.set_type(RT_MODEL_TASK_ALL_KERNEL);
  83. domi::KernelDefWithHandle *kernel_with_handle = task_def.mutable_kernel_with_handle();
  84. kernel_with_handle->set_original_kernel_key("");
  85. kernel_with_handle->set_node_info("");
  86. kernel_with_handle->set_block_dim(32);
  87. kernel_with_handle->set_args_size(64);
  88. string args(64, '1');
  89. kernel_with_handle->set_args(args.data(), 64);
  90. domi::KernelContext *context = kernel_with_handle->mutable_context();
  91. context->set_op_index(1);
  92. context->set_kernel_type(2); // ccKernelType::TE
  93. uint16_t args_offset[9] = {0};
  94. context->set_args_offset(args_offset, 9 * sizeof(uint16_t));
  95. OpDescPtr op_desc = CreateOpDesc("Add", "Add");
  96. std::vector<char> kernelBin;
  97. TBEKernelPtr tbe_kernel = std::make_shared<ge::OpKernelBin>("name/Add", std::move(kernelBin));
  98. op_desc->SetExtAttr(ge::OP_EXTATTR_NAME_TBE_KERNEL, tbe_kernel);
  99. std::string kernel_name("kernel/Add");
  100. AttrUtils::SetStr(op_desc, op_desc->GetName() + "_kernelname", kernel_name);
  101. ASSERT_EQ(aicore_task->InitWithTaskDef(*op_desc.get(), task_def), SUCCESS);
  102. rtStream_t stream = nullptr;
  103. rtStreamCreate(&stream, 0);
  104. ASSERT_EQ(aicore_task->LaunchKernel(stream), SUCCESS);
  105. char *handle = "";
  106. aicore_task->handle_ = handle;
  107. aicore_task->tiling_key_ = 1;
  108. ASSERT_EQ(aicore_task->LaunchKernel(stream), SUCCESS);
  109. }
  110. TEST_F(UtestGeHybrid, aicore_op_task_init_success2) {
  111. // build aicore task
  112. auto aicore_task = std::unique_ptr<hybrid::AiCoreOpTask>(new(std::nothrow)hybrid::AiCoreOpTask());
  113. aicore_task->is_single_op_ = true;
  114. domi::TaskDef task_def;
  115. task_def.set_type(RT_MODEL_TASK_KERNEL);
  116. domi::KernelDef *kernel = task_def.mutable_kernel();
  117. kernel->set_block_dim(32);
  118. kernel->set_args_size(64);
  119. string args(64, '1');
  120. kernel->set_args(args.data(), 64);
  121. domi::KernelContext *context = kernel->mutable_context();
  122. context->set_op_index(1);
  123. context->set_kernel_type(2); // ccKernelType::TE
  124. uint16_t args_offset[9] = {0};
  125. context->set_args_offset(args_offset, 9 * sizeof(uint16_t));
  126. OpDescPtr op_desc = CreateOpDesc("Add", "Add");
  127. std::vector<char> kernelBin;
  128. TBEKernelPtr tbe_kernel = std::make_shared<ge::OpKernelBin>("name/Add", std::move(kernelBin));
  129. op_desc->SetExtAttr(ge::OP_EXTATTR_NAME_TBE_KERNEL, tbe_kernel);
  130. std::string kernel_name("kernel/Add");
  131. AttrUtils::SetStr(op_desc, op_desc->GetName() + "_kernelname", kernel_name);
  132. ASSERT_EQ(aicore_task->InitWithTaskDef(*op_desc.get(), task_def), SUCCESS);
  133. rtStream_t stream = nullptr;
  134. rtStreamCreate(&stream, 0);
  135. ASSERT_EQ(aicore_task->LaunchKernel(stream), SUCCESS);
  136. char *handle = "";
  137. aicore_task->handle_ = handle;
  138. aicore_task->tiling_key_ = 1;
  139. ASSERT_EQ(aicore_task->LaunchKernel(stream), SUCCESS);
  140. }
  141. TEST_F(UtestGeHybrid, task_update_tiling_info) {
  142. auto aicore_task = std::unique_ptr<hybrid::AiCoreOpTask>(new(std::nothrow)hybrid::AiCoreOpTask());
  143. auto graph = make_shared<ComputeGraph>("graph");
  144. OpDescPtr op_desc = CreateOpDesc("Add", "Add");
  145. ge::AttrUtils::SetStr(op_desc, "compile_info_key", "key");
  146. ge::AttrUtils::SetStr(op_desc, "compile_info_json", "json");
  147. ge::AttrUtils::SetBool(op_desc, "support_dynamicshape", true);
  148. ge::AttrUtils::SetInt(op_desc, "op_para_size", 1);
  149. auto node = graph->AddNode(op_desc);
  150. std::unique_ptr<NodeItem> node_item;
  151. NodeItem::Create(node, node_item);
  152. node_item->input_start = 0;
  153. node_item->output_start = 0;
  154. GraphExecutionContext execution_context;
  155. SubgraphContext subgraph_context(nullptr, &execution_context);
  156. auto node_state = subgraph_context.GetOrCreateNodeState(node_item.get());
  157. ASSERT_EQ(aicore_task->InitTilingInfo(*op_desc), SUCCESS);
  158. ASSERT_EQ(aicore_task->UpdateTilingInfo(*node_state->GetTaskContext()), SUCCESS);
  159. }
  160. TEST_F(UtestGeHybrid, index_taskdefs_failed) {
  161. // build aicore task
  162. domi::ModelTaskDef model_task_def;
  163. std::shared_ptr<domi::ModelTaskDef> model_task_def_ptr = make_shared<domi::ModelTaskDef>(model_task_def);
  164. domi::TaskDef *task_def = model_task_def_ptr->add_task();
  165. GeModelPtr ge_model = make_shared<GeModel>();
  166. ge_model->SetModelTaskDef(model_task_def_ptr);
  167. auto aicore_task = std::unique_ptr<hybrid::AiCoreOpTask>(new(std::nothrow)hybrid::AiCoreOpTask());
  168. task_def->set_type(RT_MODEL_TASK_ALL_KERNEL);
  169. domi::KernelDefWithHandle *kernel_with_handle = task_def->mutable_kernel_with_handle();
  170. kernel_with_handle->set_original_kernel_key("");
  171. kernel_with_handle->set_node_info("");
  172. kernel_with_handle->set_block_dim(32);
  173. kernel_with_handle->set_args_size(64);
  174. string args(64, '1');
  175. kernel_with_handle->set_args(args.data(), 64);
  176. domi::KernelContext *context = kernel_with_handle->mutable_context();
  177. context->set_op_index(1);
  178. context->set_kernel_type(2); // ccKernelType::TE
  179. uint16_t args_offset[9] = {0};
  180. context->set_args_offset(args_offset, 9 * sizeof(uint16_t));
  181. OpDescPtr op_desc = CreateOpDesc("Add", "Add");
  182. std::vector<char> kernelBin;
  183. TBEKernelPtr tbe_kernel = std::make_shared<ge::OpKernelBin>("name/Add", std::move(kernelBin));
  184. op_desc->SetExtAttr(ge::OP_EXTATTR_NAME_TBE_KERNEL, tbe_kernel);
  185. std::string kernel_name("kernel/Add");
  186. AttrUtils::SetStr(op_desc, op_desc->GetName() + "_kernelname", kernel_name);
  187. ComputeGraphPtr graph = std::make_shared<ComputeGraph>("test");
  188. GeRootModelPtr ge_root_model = make_shared<GeRootModel>(graph);
  189. ge_root_model->SetModelName("test_name");
  190. HybridModel hybrid_model(ge_root_model);
  191. HybridModelBuilder hybrid_model_builder(hybrid_model);
  192. ASSERT_EQ(hybrid_model_builder.Build(), INTERNAL_ERROR);
  193. ASSERT_EQ(hybrid_model_builder.IndexTaskDefs(graph, ge_model), INTERNAL_ERROR);
  194. }
  195. TEST_F(UtestGeHybrid, parse_force_infershape_nodes) {
  196. const char *const kForceInfershape = "_force_infershape_when_running";
  197. auto graph = make_shared<ComputeGraph>("graph");
  198. OpDescPtr op_desc = CreateOpDesc("Conv2D", "Conv2D");
  199. ge::AttrUtils::SetBool(op_desc, kForceInfershape, true);
  200. auto node = graph->AddNode(op_desc);
  201. std::unique_ptr<NodeItem> new_node;
  202. NodeItem::Create(node, new_node);
  203. GeRootModelPtr ge_root_model = make_shared<GeRootModel>(graph);
  204. HybridModel hybrid_model(ge_root_model);
  205. HybridModelBuilder hybrid_model_builder(hybrid_model);
  206. ASSERT_EQ(hybrid_model_builder.ParseForceInfershapeNodes(node, *new_node), SUCCESS);
  207. }
  208. static ComputeGraphPtr BuildDataDirectConnectGraph() {
  209. const char *kRefIndex = "_parent_node_index";
  210. ge::ut::GraphBuilder builder("subgraph");
  211. auto data = builder.AddNode("Data", "Data", 1, 1);
  212. auto netoutput = builder.AddNode("NetOutput", "NetOutput", 1, 1);
  213. (void)AttrUtils::SetInt(netoutput->GetOpDesc()->MutableInputDesc(0), kRefIndex, 0);
  214. builder.AddDataEdge(data, 0, netoutput, 0);
  215. return builder.GetGraph();
  216. }
  217. TEST_F(UtestGeHybrid, data_direct_connect) {
  218. std::unique_ptr<NodeItem> node_item;
  219. auto root_graph = make_shared<ComputeGraph>("root_graph");
  220. OpDescPtr op_desc = CreateOpDesc("PartitionedCall", "PartitionedCall");
  221. auto node = root_graph->AddNode(op_desc);
  222. node->SetOwnerComputeGraph(root_graph);
  223. auto sub_graph = BuildDataDirectConnectGraph();
  224. sub_graph->SetParentGraph(root_graph);
  225. sub_graph->SetParentNode(node);
  226. node->GetOpDesc()->AddSubgraphName("subgraph");
  227. node->GetOpDesc()->SetSubgraphInstanceName(0, "subgraph");
  228. root_graph->AddSubgraph("subgraph", sub_graph);
  229. std::unique_ptr<NodeItem> new_node;
  230. NodeItem::Create(node, new_node);
  231. GeRootModelPtr ge_root_model = make_shared<GeRootModel>(root_graph);
  232. HybridModel hybrid_model(ge_root_model);
  233. HybridModelBuilder hybrid_model_builder(hybrid_model);
  234. auto ret = hybrid_model_builder.IdentifyVariableOutputs(*new_node.get(), sub_graph);
  235. ASSERT_EQ(ret, SUCCESS);
  236. }
  237. TEST_F(UtestGeHybrid, index_taskdefs_success) {
  238. // build aicore task
  239. domi::ModelTaskDef model_task_def;
  240. std::shared_ptr<domi::ModelTaskDef> model_task_def_ptr = make_shared<domi::ModelTaskDef>(model_task_def);
  241. domi::TaskDef *task_def = model_task_def_ptr->add_task();
  242. GeModelPtr ge_model = make_shared<GeModel>();
  243. ge_model->SetModelTaskDef(model_task_def_ptr);
  244. auto aicore_task = std::unique_ptr<hybrid::AiCoreOpTask>(new(std::nothrow)hybrid::AiCoreOpTask());
  245. task_def->set_type(RT_MODEL_TASK_ALL_KERNEL);
  246. domi::KernelDefWithHandle *kernel_with_handle = task_def->mutable_kernel_with_handle();
  247. kernel_with_handle->set_original_kernel_key("");
  248. kernel_with_handle->set_node_info("");
  249. kernel_with_handle->set_block_dim(32);
  250. kernel_with_handle->set_args_size(64);
  251. string args(64, '1');
  252. kernel_with_handle->set_args(args.data(), 64);
  253. domi::KernelContext *context = kernel_with_handle->mutable_context();
  254. context->set_op_index(0);
  255. context->set_kernel_type(2); // ccKernelType::TE
  256. uint16_t args_offset[9] = {0};
  257. context->set_args_offset(args_offset, 9 * sizeof(uint16_t));
  258. OpDescPtr op_desc = CreateOpDesc("Add", "Add");
  259. std::vector<char> kernelBin;
  260. TBEKernelPtr tbe_kernel = std::make_shared<ge::OpKernelBin>("name/Add", std::move(kernelBin));
  261. op_desc->SetExtAttr(ge::OP_EXTATTR_NAME_TBE_KERNEL, tbe_kernel);
  262. std::string kernel_name("kernel/Add");
  263. AttrUtils::SetStr(op_desc, op_desc->GetName() + "_kernelname", kernel_name);
  264. ComputeGraphPtr graph = std::make_shared<ComputeGraph>("test");
  265. NodePtr node = graph->AddNode(op_desc);
  266. GeRootModelPtr ge_root_model = make_shared<GeRootModel>(graph);
  267. HybridModel hybrid_model(ge_root_model);
  268. HybridModelBuilder hybrid_model_builder(hybrid_model);
  269. ASSERT_EQ(hybrid_model_builder.IndexTaskDefs(graph, ge_model), SUCCESS);
  270. }
  271. TEST_F(UtestGeHybrid, init_weight_success) {
  272. NpuMemoryAllocator::allocators_.emplace(make_pair(0, nullptr));
  273. // make graph with sub_graph
  274. ComputeGraphPtr graph = std::make_shared<ComputeGraph>("root_graph");
  275. OpDescPtr op_desc = CreateOpDesc("if", IF);
  276. NodePtr node = graph->AddNode(op_desc);
  277. // make sub graph
  278. ComputeGraphPtr sub_graph = std::make_shared<ComputeGraph>("if_sub_graph");
  279. OpDescPtr const_op_desc = CreateOpDesc("const", CONSTANT);
  280. vector<int64_t> dims_vec_0 = {2, 1, 4, 1, 2};
  281. vector<int32_t> data_vec_0 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
  282. GeTensorDesc tensor_desc_0(GeShape(dims_vec_0), FORMAT_NCHW, DT_INT32);
  283. (void)TensorUtils::SetRealDimCnt(tensor_desc_0, dims_vec_0.size());
  284. ConstGeTensorPtr constTensor_0 =
  285. std::make_shared<GeTensor>(tensor_desc_0, (uint8_t *)&data_vec_0[0], data_vec_0.size() * sizeof(int32_t));
  286. AttrUtils::SetTensor(const_op_desc, ge::ATTR_NAME_WEIGHTS, constTensor_0);
  287. const_op_desc->AddOutputDesc(tensor_desc_0);
  288. NodePtr const_node = sub_graph->AddNode(const_op_desc);
  289. graph->AddSubgraph("sub", sub_graph);
  290. GeRootModelPtr ge_root_model = make_shared<GeRootModel>(graph);
  291. GeModelPtr ge_sub_model = make_shared<GeModel>();
  292. //Buffer weight_buffer = Buffer(128,0);
  293. //ge_sub_model->SetWeight(weight_buffer);
  294. ge_root_model->SetSubgraphInstanceNameToModel("sub",ge_sub_model);
  295. HybridModel hybrid_model(ge_root_model);
  296. HybridModelBuilder hybrid_model_builder(hybrid_model);
  297. auto ret = hybrid_model_builder.InitWeights();
  298. ASSERT_EQ(ret,SUCCESS);
  299. Buffer weight_buffer = Buffer(128,0);
  300. ge_sub_model->SetWeight(weight_buffer);
  301. ret = hybrid_model_builder.InitWeights();
  302. ASSERT_EQ(ret,PARAM_INVALID);
  303. }
  304. TEST_F(UtestGeHybrid, hybrid_model_executor) {
  305. ComputeGraphPtr compute_graph = MakeShared<ComputeGraph>("abc");
  306. GeRootModelPtr root_model = MakeShared<ge::GeRootModel>(compute_graph);
  307. HybridModel model(root_model);
  308. model.root_graph_item_.reset(new GraphItem);
  309. HybridModel *model_ptr = &model;
  310. uint32_t device_id = 0;
  311. rtStream_t stream = nullptr;
  312. HybridModelExecutor executor(model_ptr, device_id, stream);
  313. executor.Init();
  314. }
  315. TEST_F(UtestGeHybrid, test_parse_parallel_group) {
  316. NodeExecutorManager::GetInstance().engine_mapping_.emplace("ops_kernel_info_hccl",
  317. NodeExecutorManager::ExecutorType::HCCL);
  318. ComputeGraphPtr compute_graph = MakeShared<ComputeGraph>("test");
  319. OpDescPtr op_desc = CreateOpDesc("AllReduce", "AllReduce");
  320. op_desc->SetId(0);
  321. ge::AttrUtils::SetStr(op_desc, ATTR_NAME_PARALLEL_GROUP, "group_1");
  322. auto node = compute_graph->AddNode(op_desc);
  323. std::unique_ptr<NodeItem> node_item;
  324. NodeItem::Create(node, node_item);
  325. node_item->node_id = 0;
  326. op_desc->SetOpKernelLibName("ops_kernel_info_hccl");
  327. GeRootModelPtr root_model = MakeShared<ge::GeRootModel>(compute_graph);
  328. HybridModel model(root_model);
  329. model.root_graph_ = compute_graph;
  330. HybridModelBuilder builder(model);
  331. ASSERT_EQ(builder.CollectParallelGroups(node_item.get()), SUCCESS);
  332. ASSERT_EQ(builder.node_to_parallel_groups_.size(), 1);
  333. ASSERT_EQ(builder.parallel_group_to_nodes_.size(), 1);
  334. OpDescPtr op_desc_1 = CreateOpDesc("subgraph", "PartitionedCall");
  335. op_desc_1->AddSubgraphName("subgraph");
  336. auto node_1 = compute_graph->AddNode(op_desc_1);
  337. ComputeGraphPtr subgraph = MakeShared<ComputeGraph>("subgraph");
  338. ASSERT_EQ(NodeUtils::SetSubgraph(*node_1, 0, subgraph), GRAPH_SUCCESS);
  339. std::unique_ptr<NodeItem> node_item_1;
  340. NodeItem::Create(node_1, node_item_1);
  341. node_item_1->node_id = 1;
  342. ASSERT_EQ(builder.CollectParallelGroups(node_item_1.get()), SUCCESS);
  343. ASSERT_EQ(builder.node_to_parallel_groups_.size(), 1);
  344. ASSERT_EQ(builder.parallel_group_to_nodes_.size(), 1);
  345. OpDescPtr op_desc_2 = CreateOpDesc("sub_node_1", "AllReduce");
  346. ge::AttrUtils::SetStr(op_desc_2, ATTR_NAME_PARALLEL_GROUP, "group_1");
  347. auto node_2 = subgraph->AddNode(op_desc_2);
  348. ASSERT_TRUE(node_2 != nullptr);
  349. OpDescPtr op_desc_3 = CreateOpDesc("sub_node_2", "AllReduce2");
  350. ge::AttrUtils::SetStr(op_desc_3, ATTR_NAME_PARALLEL_GROUP, "group_2");
  351. auto node_3 = subgraph->AddNode(op_desc_3);
  352. ASSERT_TRUE(node_3 != nullptr);
  353. ASSERT_EQ(builder.CollectParallelGroups(node_item_1.get()), SUCCESS);
  354. ASSERT_EQ(builder.node_to_parallel_groups_.size(), 2);
  355. ASSERT_EQ(builder.parallel_group_to_nodes_.size(), 2);
  356. ASSERT_EQ(builder.parallel_group_to_nodes_["group_1"].size(), 2);
  357. ASSERT_EQ(builder.parallel_group_to_nodes_["group_2"].size(), 1);
  358. builder.parallel_group_to_nodes_.clear();
  359. builder.node_ref_inputs_.clear();
  360. model.node_items_[node] = std::move(node_item);
  361. model.node_items_[node_1] = std::move(node_item_1);
  362. ASSERT_FALSE(model.node_items_[node]->has_observer);
  363. ASSERT_TRUE(model.node_items_[node_1]->dependents_for_execution.empty());
  364. ASSERT_EQ(builder.ParseDependentByParallelGroup(), SUCCESS);
  365. ASSERT_TRUE(model.node_items_[node]->has_observer);
  366. ASSERT_EQ(model.node_items_[node_1]->dependents_for_execution.size(), 1);
  367. ASSERT_EQ(model.node_items_[node_1]->dependents_for_execution[0], node);
  368. // repeat parse
  369. ASSERT_EQ(builder.ParseDependentByParallelGroup(), SUCCESS);
  370. ASSERT_TRUE(model.node_items_[node]->has_observer);
  371. ASSERT_EQ(model.node_items_[node_1]->dependents_for_execution.size(), 1);
  372. ASSERT_EQ(model.node_items_[node_1]->dependents_for_execution[0], node);
  373. }
  374. TEST_F(UtestGeHybrid, unfold_subgraphs_success) {
  375. ComputeGraphPtr root_graph = std::make_shared<ComputeGraph>("root_graph");
  376. auto partitioned_call_op_desc = CreateOpDesc("partitioned_call", PARTITIONEDCALL, 3, 1);
  377. auto partitioned_call_node = root_graph->AddNode(partitioned_call_op_desc);
  378. partitioned_call_op_desc->AddSubgraphName("f");
  379. partitioned_call_op_desc->SetSubgraphInstanceName(0, "sub_graph");
  380. ComputeGraphPtr sub_sub_graph1 = std::make_shared<ComputeGraph>("while_cond");
  381. {
  382. OpDescPtr sub_sub_graph_while_cond_data_op_desc = CreateOpDesc("cond_data", DATA);
  383. NodePtr sub_sub_graph_while_cond_data_node = sub_sub_graph1->AddNode(sub_sub_graph_while_cond_data_op_desc);
  384. sub_sub_graph1->SetParentGraph(root_graph);
  385. root_graph->AddSubGraph(sub_sub_graph1);
  386. }
  387. ComputeGraphPtr sub_sub_graph2 = std::make_shared<ComputeGraph>("while_body");
  388. {
  389. OpDescPtr sub_sub_graph_while_body_data_op_desc = CreateOpDesc("body_data", DATA);
  390. NodePtr sub_sub_graph_while_body_data_node = sub_sub_graph2->AddNode(sub_sub_graph_while_body_data_op_desc);
  391. sub_sub_graph2->SetGraphUnknownFlag(true);
  392. sub_sub_graph2->SetParentGraph(root_graph);
  393. root_graph->AddSubGraph(sub_sub_graph2);
  394. }
  395. // Will unfold to merged_graph.
  396. ComputeGraphPtr sub_graph = std::make_shared<ComputeGraph>("sub_graph");
  397. {
  398. OpDescPtr sub_graph_data1_op_desc = CreateOpDesc("data1", DATA, 1, 1);
  399. OpDescPtr sub_graph_data2_op_desc = CreateOpDesc("data2", DATA, 1, 1);
  400. OpDescPtr sub_graph_data3_op_desc = CreateOpDesc("data3", DATA, 1, 1);
  401. NodePtr sub_graph_data1_node = sub_graph->AddNode(sub_graph_data1_op_desc);
  402. NodePtr sub_graph_data2_node = sub_graph->AddNode(sub_graph_data2_op_desc);
  403. NodePtr sub_graph_data3_node = sub_graph->AddNode(sub_graph_data3_op_desc);
  404. AttrUtils::SetInt(sub_graph_data1_op_desc, ATTR_NAME_PARENT_NODE_INDEX, 0);
  405. AttrUtils::SetInt(sub_graph_data2_op_desc, ATTR_NAME_PARENT_NODE_INDEX, 1);
  406. AttrUtils::SetInt(sub_graph_data3_op_desc, ATTR_NAME_PARENT_NODE_INDEX, 2);
  407. OpDescPtr sub_graph_while_op_desc = CreateOpDesc("while", WHILE, 2, 2);
  408. NodePtr sub_graph_while_node = sub_graph->AddNode(sub_graph_while_op_desc);
  409. sub_sub_graph1->SetParentNode(sub_graph_while_node);
  410. sub_sub_graph2->SetParentNode(sub_graph_while_node);
  411. sub_graph_while_op_desc->AddSubgraphName("while_cond");
  412. sub_graph_while_op_desc->SetSubgraphInstanceName(0, "while_cond");
  413. sub_graph_while_op_desc->AddSubgraphName("while_body");
  414. sub_graph_while_op_desc->SetSubgraphInstanceName(1, "while_body");
  415. OpDescPtr sub_graph_matmul_op_desc = CreateOpDesc("matmul", MATMUL, 2, 1);
  416. NodePtr sub_graph_matmul_node = sub_graph->AddNode(sub_graph_matmul_op_desc);
  417. OpDescPtr sub_graph_output_op_desc = CreateOpDesc("output", NETOUTPUT, 1, 1);
  418. NodePtr sub_graph_output_node = sub_graph->AddNode(sub_graph_output_op_desc);
  419. GraphUtils::AddEdge(sub_graph_data1_node->GetOutDataAnchor(0), sub_graph_while_node->GetInDataAnchor(0));
  420. GraphUtils::AddEdge(sub_graph_data2_node->GetOutDataAnchor(0), sub_graph_while_node->GetInDataAnchor(1));
  421. GraphUtils::AddEdge(sub_graph_data3_node->GetOutDataAnchor(0), sub_graph_matmul_node->GetInDataAnchor(0));
  422. GraphUtils::AddEdge(sub_graph_while_node->GetOutDataAnchor(0), sub_graph_matmul_node->GetInDataAnchor(1));
  423. GraphUtils::AddEdge(sub_graph_matmul_node->GetOutDataAnchor(0), sub_graph_output_node->GetInDataAnchor(0));
  424. sub_graph->SetGraphUnknownFlag(true);
  425. sub_graph->SetParentNode(partitioned_call_node);
  426. sub_graph->SetParentGraph(root_graph);
  427. root_graph->AddSubGraph(sub_graph);
  428. }
  429. OpDescPtr graph_data1_op_desc = CreateOpDesc("data1", DATA, 1, 1);
  430. OpDescPtr graph_data2_op_desc = CreateOpDesc("data2", DATA, 1, 1);
  431. OpDescPtr graph_data3_op_desc = CreateOpDesc("data3", DATA, 1, 1);
  432. NodePtr graph_data1_node = root_graph->AddNode(graph_data1_op_desc);
  433. NodePtr graph_data2_node = root_graph->AddNode(graph_data2_op_desc);
  434. NodePtr graph_data3_node = root_graph->AddNode(graph_data3_op_desc);
  435. AttrUtils::SetInt(graph_data1_op_desc, ATTR_NAME_INDEX, 0);
  436. AttrUtils::SetInt(graph_data2_op_desc, ATTR_NAME_INDEX, 1);
  437. AttrUtils::SetInt(graph_data3_op_desc, ATTR_NAME_INDEX, 2);
  438. GraphUtils::AddEdge(graph_data1_node->GetOutDataAnchor(0), partitioned_call_node->GetInDataAnchor(0));
  439. GraphUtils::AddEdge(graph_data2_node->GetOutDataAnchor(0), partitioned_call_node->GetInDataAnchor(1));
  440. GraphUtils::AddEdge(graph_data3_node->GetOutDataAnchor(0), partitioned_call_node->GetInDataAnchor(2));
  441. ComputeGraphPtr merged_graph = nullptr;
  442. GeRootModelPtr root_model = MakeShared<ge::GeRootModel>(root_graph);
  443. HybridModel hybrid_model(root_model);
  444. HybridModelBuilder hybrid_model_builder(hybrid_model);
  445. EXPECT_EQ(hybrid_model_builder.UnfoldSubgraphs(root_graph, merged_graph), SUCCESS);
  446. }
  447. TEST_F(UtestGeHybrid, TestTaskContext) {
  448. auto graph = make_shared<ComputeGraph>("graph");
  449. OpDescPtr op_desc = CreateOpDesc("Add", "Add");
  450. GeShape shape({2, 16});
  451. GeTensorDesc tensor_desc(shape);
  452. op_desc->AddInputDesc(tensor_desc);
  453. op_desc->AddInputDesc(tensor_desc);
  454. op_desc->AddOutputDesc(tensor_desc);
  455. auto node = graph->AddNode(op_desc);
  456. std::unique_ptr<NodeItem> node_item;
  457. NodeItem::Create(node, node_item);
  458. node_item->input_start = 0;
  459. node_item->output_start = 0;
  460. GraphExecutionContext execution_context;
  461. GraphItem graph_item;
  462. SubgraphContext subgraph_context(&graph_item, &execution_context);
  463. ASSERT_EQ(subgraph_context.Init(), SUCCESS);
  464. subgraph_context.all_inputs_.resize(2);
  465. subgraph_context.all_outputs_.resize(1);
  466. auto node_state = subgraph_context.GetOrCreateNodeState(node_item.get());
  467. auto task_context = node_state->GetTaskContext();
  468. ASSERT_TRUE(task_context != nullptr);
  469. auto desc = task_context->MutableInputDesc(2);
  470. ASSERT_TRUE(desc == nullptr);
  471. desc = task_context->MutableOutputDesc(0);
  472. ASSERT_TRUE(desc != nullptr);
  473. ASSERT_EQ(desc->GetShape().GetDims(), shape.GetDims());
  474. GeTensorDesc output_desc;
  475. ASSERT_EQ(task_context->GetOutputDesc(0, output_desc), SUCCESS);
  476. ASSERT_EQ(output_desc.GetShape().GetDims(), shape.GetDims());
  477. desc = task_context->MutableInputDesc(0);
  478. ASSERT_TRUE(desc != nullptr);
  479. ASSERT_EQ(desc->GetShape().GetDims(), shape.GetDims());
  480. GeShape new_shape({8, 2});
  481. tensor_desc.SetShape(new_shape);
  482. task_context->UpdateInputDesc(1, tensor_desc);
  483. GeTensorDesc new_desc;
  484. ASSERT_EQ(task_context->GetInputDesc(1, new_desc), SUCCESS);
  485. ASSERT_EQ(new_desc.GetShape().GetDims(), new_shape.GetDims());
  486. }
  487. TEST_F(UtestGeHybrid, hybrid_model_executor_update_args) {
  488. auto aicore_task = std::unique_ptr<hybrid::AiCoreOpTask>(new(std::nothrow)hybrid::AiCoreOpTask());
  489. auto graph = make_shared<ComputeGraph>("graph");
  490. OpDescPtr op_desc = CreateOpDesc("Add", "Add");
  491. GeShape shape({2, 16});
  492. GeTensorDesc tensor_desc(shape);
  493. op_desc->AddInputDesc(tensor_desc);
  494. op_desc->AddInputDesc(tensor_desc);
  495. op_desc->AddOutputDesc(tensor_desc);
  496. auto node = graph->AddNode(op_desc);
  497. std::unique_ptr<NodeItem> node_item;
  498. NodeItem::Create(node, node_item);
  499. node_item->input_start = 0;
  500. node_item->output_start = 0;
  501. GraphExecutionContext execution_context;
  502. GraphItem graph_item;
  503. SubgraphContext subgraph_context(&graph_item, &execution_context);
  504. ASSERT_EQ(subgraph_context.Init(), SUCCESS);
  505. subgraph_context.all_inputs_.resize(2);
  506. subgraph_context.all_outputs_.resize(1);
  507. auto node_state = subgraph_context.GetOrCreateNodeState(node_item.get());
  508. auto task_context = node_state->GetTaskContext();
  509. int32_t buffer[1];
  510. aicore_task->tiling_buffer_ = TensorBuffer::Create(buffer, sizeof(buffer));
  511. EXPECT_NE(aicore_task->tiling_buffer_, nullptr);
  512. aicore_task->max_arg_count_ = 0;
  513. EXPECT_EQ(aicore_task->UpdateArgs(*task_context), ACL_ERROR_GE_MEMORY_OPERATE_FAILED);
  514. aicore_task->args_ = std::unique_ptr<uint8_t[]>(new uint8_t[sizeof(uintptr_t) * 2]);
  515. EXPECT_EQ(aicore_task->UpdateArgs(*task_context), SUCCESS);
  516. }
  517. TEST_F(UtestGeHybrid, hybrid_model_executor_check_shape) {
  518. HybridModelExecutor::ExecuteArgs args;
  519. GeTensorDescPtr ge_tensor = make_shared<GeTensorDesc>(GeTensorDesc());
  520. vector<int64_t> dim = {2 , 3};
  521. ge_tensor->SetShape(GeShape(dim));
  522. args.input_desc.push_back(ge_tensor);
  523. // create node
  524. ge::ComputeGraphPtr graph = std::make_shared<ComputeGraph>("God");
  525. OpDescPtr op_desc = std::make_shared<OpDesc>("data", DATA);
  526. GeTensorDesc tensor_desc(GeShape({2, 3}));
  527. std::vector<std::pair<int64_t, int64_t>> shape_range({std::pair<int64_t, int64_t>(1, 3),
  528. std::pair<int64_t, int64_t>(2, 4)});
  529. tensor_desc.SetShapeRange(shape_range);
  530. op_desc->AddInputDesc(tensor_desc);
  531. op_desc->AddOutputDesc(tensor_desc);
  532. NodePtr node = graph->AddNode(op_desc);
  533. std::unique_ptr<NodeItem> new_node;
  534. NodeItem::Create(node, new_node);
  535. new_node->is_dynamic = true;
  536. GraphItem graph_item;
  537. graph_item.input_nodes_.emplace_back(new_node.get());
  538. Status ret = HybridModelExecutor::CheckInputShapeByShapeRange(&graph_item, args);
  539. ASSERT_EQ(ret, ge::SUCCESS);
  540. HybridModelExecutor::ExecuteArgs args1;
  541. ret = HybridModelExecutor::CheckInputShapeByShapeRange(&graph_item, args1);
  542. ASSERT_EQ(ret, ge::INTERNAL_ERROR);
  543. HybridModelExecutor::ExecuteArgs args2;
  544. GeTensorDescPtr ge_tensor2 = make_shared<GeTensorDesc>(GeTensorDesc());
  545. vector<int64_t> dim2 = {-1 , 3};
  546. ge_tensor2->SetShape(GeShape(dim2));
  547. args2.input_desc.push_back(ge_tensor2);
  548. ret = HybridModelExecutor::CheckInputShapeByShapeRange(&graph_item, args1);
  549. ASSERT_EQ(ret, ge::INTERNAL_ERROR);
  550. HybridModelExecutor::ExecuteArgs args3;
  551. ret = HybridModelExecutor::CheckInputShapeByShapeRange(&graph_item, args3);
  552. ASSERT_EQ(ret, ge::INTERNAL_ERROR);
  553. }
  554. TEST_F(UtestGeHybrid, TestOptimizeDependenciesForConstInputs) {
  555. ComputeGraphPtr compute_graph = MakeShared<ComputeGraph>("test");
  556. GeRootModelPtr root_model = MakeShared<ge::GeRootModel>(compute_graph);
  557. HybridModel model(root_model);
  558. model.root_graph_ = compute_graph;
  559. HybridModelBuilder builder(model);
  560. GeShape shape({2, 16});
  561. GeTensorDesc tensor_desc(shape);
  562. std::unique_ptr<NodeItem> const_node_item;
  563. {
  564. OpDescPtr const_op_desc = CreateOpDesc("Constant", "Const");
  565. const_op_desc->AddOutputDesc(tensor_desc);
  566. auto const_node = compute_graph->AddNode(const_op_desc);
  567. NodeItem::Create(const_node, const_node_item);
  568. }
  569. std::unique_ptr<NodeItem> non_const_node_item;
  570. {
  571. OpDescPtr op_desc = CreateOpDesc("Add", "Add");
  572. op_desc->AddOutputDesc(tensor_desc);
  573. auto const_node = compute_graph->AddNode(op_desc);
  574. NodeItem::Create(const_node, non_const_node_item);
  575. }
  576. std::unique_ptr<NodeItem> known_node_item;
  577. {
  578. OpDescPtr known_op_desc = CreateOpDesc("known", "PartitionedCall");
  579. known_op_desc->AddOutputDesc(tensor_desc);
  580. known_op_desc->AddOutputDesc(tensor_desc);
  581. auto known_node = compute_graph->AddNode(known_op_desc);
  582. NodeItem::Create(known_node, known_node_item);
  583. }
  584. std::unique_ptr<NodeItem> dst_node_item;
  585. {
  586. OpDescPtr known_op_desc = CreateOpDesc("SomeOp", "SomeOpType ");
  587. known_op_desc->AddOutputDesc(tensor_desc);
  588. known_op_desc->AddOutputDesc(tensor_desc);
  589. auto known_node = compute_graph->AddNode(known_op_desc);
  590. NodeItem::Create(known_node, dst_node_item);
  591. }
  592. float buffer[2 * 16];
  593. unique_ptr<TensorValue> tensor_value(new TensorValue(buffer, sizeof(buffer)));
  594. model.constant_tensors_[const_node_item->node] = std::move(tensor_value);
  595. // Case 1. connect to Const
  596. auto output_id = 1;
  597. builder.host_input_value_dependencies_[dst_node_item.get()].emplace_back(output_id, const_node_item.get());
  598. builder.host_input_value_dependencies_[dst_node_item.get()].emplace_back(0, non_const_node_item.get());
  599. dst_node_item->dependents_for_shape_inference.emplace_back(const_node_item->node);
  600. dst_node_item->dependents_for_shape_inference.emplace_back(non_const_node_item->node);
  601. ASSERT_EQ(builder.OptimizeDependenciesForConstantInputs(), SUCCESS);
  602. ASSERT_EQ(dst_node_item->dependents_for_shape_inference.size(), 1);
  603. ASSERT_EQ(dst_node_item->dependents_for_shape_inference[0], non_const_node_item->node);
  604. // Case 2. connect to known-subgraph, netoutput connect to Const
  605. builder.host_input_value_dependencies_.clear();
  606. dst_node_item->dependents_for_shape_inference.clear();
  607. builder.known_subgraph_constant_output_refs_[known_node_item.get()].emplace(output_id, const_node_item->node);
  608. builder.host_input_value_dependencies_[dst_node_item.get()].emplace_back(output_id, known_node_item.get());
  609. builder.host_input_value_dependencies_[dst_node_item.get()].emplace_back(0, non_const_node_item.get());
  610. dst_node_item->dependents_for_shape_inference.emplace_back(known_node_item->node);
  611. dst_node_item->dependents_for_shape_inference.emplace_back(non_const_node_item->node);
  612. ASSERT_EQ(builder.OptimizeDependenciesForConstantInputs(), SUCCESS);
  613. ASSERT_EQ(dst_node_item->dependents_for_shape_inference.size(), 1);
  614. ASSERT_EQ(dst_node_item->dependents_for_shape_inference[0], non_const_node_item->node);
  615. }
  616. TEST_F(UtestGeHybrid, test_key_for_kernel_bin) {
  617. auto aicore_task = std::unique_ptr<hybrid::AiCoreOpTask>(new(std::nothrow)hybrid::AiCoreOpTask());
  618. OpDesc op_desc("Sum", "Sum");
  619. EXPECT_EQ(aicore_task->GetKeyForTbeKernel(), OP_EXTATTR_NAME_TBE_KERNEL);
  620. EXPECT_EQ(aicore_task->GetKeyForTvmMagic(), TVM_ATTR_NAME_MAGIC);
  621. EXPECT_EQ(aicore_task->GetKeyForTvmMetaData(), TVM_ATTR_NAME_METADATA);
  622. EXPECT_EQ(aicore_task->GetKeyForKernelName(op_desc), "Sum_kernelname");
  623. auto atomic_task = std::unique_ptr<hybrid::AtomicAddrCleanOpTask>(new(std::nothrow)hybrid::AtomicAddrCleanOpTask());
  624. EXPECT_EQ(atomic_task->GetKeyForTbeKernel(), EXT_ATTR_ATOMIC_TBE_KERNEL);
  625. EXPECT_EQ(atomic_task->GetKeyForTvmMagic(), ATOMIC_ATTR_TVM_MAGIC);
  626. EXPECT_EQ(atomic_task->GetKeyForTvmMetaData(), ATOMIC_ATTR_TVM_METADATA);
  627. EXPECT_EQ(atomic_task->GetKeyForKernelName(op_desc), "Sum_atomic_kernelname");
  628. }
  629. TEST_F(UtestGeHybrid, TestParseDependentInputNodesForHccl) {
  630. NodeExecutorManager::GetInstance().engine_mapping_.emplace("ops_kernel_info_hccl",
  631. NodeExecutorManager::ExecutorType::HCCL);
  632. ComputeGraphPtr compute_graph = MakeShared<ComputeGraph>("test");
  633. OpDescPtr op_desc = CreateOpDesc("Add", "Add");
  634. auto node = compute_graph->AddNode(op_desc);
  635. std::unique_ptr<NodeItem> node_item;
  636. NodeItem::Create(node, node_item);
  637. node_item->node_id = 0;
  638. OpDescPtr op_desc_1 = CreateOpDesc("AllReduce", "AllReduce");
  639. op_desc_1->SetOpKernelLibName("ops_kernel_info_hccl");
  640. auto node_1 = compute_graph->AddNode(op_desc_1);
  641. std::unique_ptr<NodeItem> node_item_1;
  642. NodeItem::Create(node_1, node_item_1);
  643. node_item_1->node_id = 1;
  644. node->GetOutControlAnchor()->LinkTo(node_1->GetInControlAnchor());
  645. OpDescPtr op_desc_2 = CreateOpDesc("net_output", NETOUTPUT);
  646. auto node_2 = compute_graph->AddNode(op_desc_2);
  647. std::unique_ptr<NodeItem> node_item_2;
  648. NodeItem::Create(node_2, node_item_2);
  649. node_item_2->node_id = 2;
  650. node_1->GetOutControlAnchor()->LinkTo(node_2->GetInControlAnchor());
  651. GeRootModelPtr root_model = MakeShared<ge::GeRootModel>(compute_graph);
  652. HybridModel model(root_model);
  653. model.root_graph_ = compute_graph;
  654. model.node_items_.emplace(node, std::move(node_item));
  655. model.node_items_.emplace(node_1, std::move(node_item_1));
  656. model.node_items_.emplace(node_2, std::move(node_item_2));
  657. HybridModelBuilder builder(model);
  658. std::vector<std::string> deps;
  659. ASSERT_EQ(builder.ParseDependentInputNodes(*model.node_items_[node_1], deps), SUCCESS);
  660. ASSERT_EQ(builder.ParseDependentInputNodes(*model.node_items_[node_2], deps), SUCCESS);
  661. ASSERT_FALSE(model.GetNodeItem(node)->has_observer);
  662. ASSERT_TRUE(model.GetNodeItem(node_1)->has_observer);
  663. ASSERT_EQ(model.node_items_[node_1]->dependents_for_execution.size(), 0);
  664. ASSERT_EQ(model.node_items_[node_2]->dependents_for_execution.size(), 1);
  665. }
  666. TEST_F(UtestGeHybrid, TestParseDependencies) {
  667. // make graph
  668. ut::GraphBuilder graph_builder = ut::GraphBuilder("graph");
  669. auto data = graph_builder.AddNode("Data", "Data", 0, 1);
  670. auto netoutput = graph_builder.AddNode("Netoutput", "NetOutput", 1, 0);
  671. graph_builder.AddDataEdge(data, 0, netoutput, 0);
  672. auto graph = graph_builder.GetGraph();
  673. GeRootModelPtr root_model = MakeShared<ge::GeRootModel>(graph);
  674. HybridModel model(root_model);
  675. HybridModelBuilder builder(model);
  676. std::unique_ptr<NodeItem> node_item;
  677. NodeItem::Create(netoutput, node_item);
  678. std::unique_ptr<NodeItem> node_item2;
  679. NodeItem::Create(data, node_item2);
  680. model.node_items_.emplace(data, std::move(node_item2));
  681. std::vector<std::string> deps;
  682. deps.push_back("Data");
  683. auto op_desc = netoutput->GetOpDesc();
  684. op_desc->impl_->input_name_idx_["Data"] = 0;
  685. auto data_desc = data->GetOpDesc();
  686. auto tensor = std::make_shared<GeTensor>();
  687. auto tensor_desc = data_desc->MutableInputDesc(0);
  688. AttrUtils::SetTensor(tensor_desc, "_value", tensor);
  689. std::set<NodePtr> dependent_for_shape_inference;
  690. ASSERT_EQ(builder.ParseDependencies(*node_item, deps, dependent_for_shape_inference), SUCCESS);
  691. }
  692. TEST_F(UtestGeHybrid, TestTaskExecuteAsync) {
  693. auto graph = make_shared<ComputeGraph>("graph");
  694. OpDescPtr op_desc = CreateOpDesc("Add", "Add");
  695. GeShape shape({2, 16});
  696. GeTensorDesc tensor_desc(shape);
  697. op_desc->AddInputDesc(tensor_desc);
  698. op_desc->AddInputDesc(tensor_desc);
  699. op_desc->AddOutputDesc(tensor_desc);
  700. auto node = graph->AddNode(op_desc);
  701. std::unique_ptr<NodeItem> node_item;
  702. NodeItem::Create(node, node_item);
  703. node_item->input_start = 0;
  704. node_item->output_start = 0;
  705. GraphExecutionContext execution_context;
  706. GraphItem graph_item;
  707. SubgraphContext subgraph_context(&graph_item, &execution_context);
  708. ASSERT_EQ(subgraph_context.Init(), SUCCESS);
  709. subgraph_context.all_inputs_.resize(2);
  710. subgraph_context.all_outputs_.resize(1);
  711. auto node_state = subgraph_context.GetOrCreateNodeState(node_item.get());
  712. auto task_context = *node_state->GetTaskContext();
  713. ASSERT_NE(BuildTaskUtils::GetTaskInfo(task_context), "");
  714. std::unique_ptr<AiCoreOpTask> task1(new AiCoreOpTask());
  715. std::vector<std::unique_ptr<AiCoreOpTask>> tasks;
  716. AiCoreNodeTask node_task(std::move(tasks));
  717. ASSERT_EQ(node_task.ExecuteAsync(task_context, nullptr), SUCCESS);
  718. }
  719. } // namespace ge

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