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

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