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

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