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

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