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 15 kB

4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 "hybrid/model/hybrid_model_builder.h"
  23. #include "hybrid/model/hybrid_model.h"
  24. #include "model/ge_model.h"
  25. #include "model/ge_root_model.h"
  26. #include "hybrid/node_executor/aicore/aicore_op_task.h"
  27. #include "framework/common/taskdown_common.h"
  28. #include "framework/common/debug/log.h"
  29. #include "graph/ge_context.h"
  30. #include "hybrid/executor/hybrid_execution_context.h"
  31. #include "hybrid/executor/hybrid_model_executor.h"
  32. #include "hybrid/node_executor/aicore/aicore_task_builder.h"
  33. #include "graph/load/model_manager/tbe_handle_store.h"
  34. #include "graph/manager/graph_mem_allocator.h"
  35. #include "hybrid/common/npu_memory_allocator.h"
  36. #include "graph/types.h"
  37. #include "graph/utils/tensor_utils.h"
  38. #undef private
  39. #undef protected
  40. using namespace std;
  41. using namespace testing;
  42. using namespace ge;
  43. using namespace hybrid;
  44. class UtestGeHybrid : public testing::Test {
  45. protected:
  46. void SetUp() {}
  47. void TearDown() {}
  48. };
  49. static ge::OpDescPtr CreateOpDesc(string name = "", string type = "") {
  50. auto op_desc = std::make_shared<ge::OpDesc>(name, type);
  51. op_desc->SetStreamId(0);
  52. op_desc->SetId(0);
  53. op_desc->SetWorkspace({});
  54. ;
  55. op_desc->SetWorkspaceBytes({});
  56. op_desc->SetInputOffset({});
  57. op_desc->SetOutputOffset({});
  58. ge::AttrUtils::SetStr(op_desc, ge::TVM_ATTR_NAME_MAGIC, "RT_DEV_BINARY_MAGIC_ELF_AIVEC");
  59. bool support_dynamic = true;
  60. ge::AttrUtils::GetBool(op_desc, "support_dynamicshape", support_dynamic);
  61. return op_desc;
  62. }
  63. TEST_F(UtestGeHybrid, aicore_op_task_init_success) {
  64. // build aicore task
  65. auto aicore_task = std::unique_ptr<hybrid::AiCoreOpTask>(new(std::nothrow)hybrid::AiCoreOpTask());
  66. domi::TaskDef task_def;
  67. task_def.set_type(RT_MODEL_TASK_ALL_KERNEL);
  68. domi::KernelDefWithHandle *kernel_with_handle = task_def.mutable_kernel_with_handle();
  69. kernel_with_handle->set_original_kernel_key("");
  70. kernel_with_handle->set_node_info("");
  71. kernel_with_handle->set_block_dim(32);
  72. kernel_with_handle->set_args_size(64);
  73. string args(64, '1');
  74. kernel_with_handle->set_args(args.data(), 64);
  75. domi::KernelContext *context = kernel_with_handle->mutable_context();
  76. context->set_op_index(1);
  77. context->set_kernel_type(2); // ccKernelType::TE
  78. uint16_t args_offset[9] = {0};
  79. context->set_args_offset(args_offset, 9 * sizeof(uint16_t));
  80. OpDescPtr op_desc = CreateOpDesc("Add", "Add");
  81. std::vector<char> kernelBin;
  82. TBEKernelPtr tbe_kernel = std::make_shared<ge::OpKernelBin>("name/Add", std::move(kernelBin));
  83. op_desc->SetExtAttr(ge::OP_EXTATTR_NAME_TBE_KERNEL, tbe_kernel);
  84. std::string kernel_name("kernel/Add");
  85. AttrUtils::SetStr(op_desc, op_desc->GetName() + "_kernelname", kernel_name);
  86. ASSERT_EQ(aicore_task->InitWithTaskDef(*op_desc.get(), task_def), SUCCESS);
  87. rtStream_t stream = nullptr;
  88. rtStreamCreate(&stream, 0);
  89. ASSERT_EQ(aicore_task->LaunchKernel(stream), SUCCESS);
  90. char *handle = "";
  91. aicore_task->handle_ = handle;
  92. aicore_task->tiling_key_ = 1;
  93. ASSERT_EQ(aicore_task->LaunchKernel(stream), SUCCESS);
  94. }
  95. TEST_F(UtestGeHybrid, task_update_tiling_info) {
  96. auto aicore_task = std::unique_ptr<hybrid::AiCoreOpTask>(new(std::nothrow)hybrid::AiCoreOpTask());
  97. aicore_task->is_single_op_ = true;
  98. auto graph = make_shared<ComputeGraph>("graph");
  99. OpDescPtr op_desc = CreateOpDesc("Add", "Add");
  100. ge::AttrUtils::SetStr(op_desc, "compile_info_key", "key");
  101. ge::AttrUtils::SetStr(op_desc, "compile_info_json", "json");
  102. auto node = graph->AddNode(op_desc);
  103. optiling::OpRunInfo tiling_info;
  104. ASSERT_EQ(aicore_task->CalcTilingInfo(node, tiling_info), SUCCESS);
  105. }
  106. TEST_F(UtestGeHybrid, index_taskdefs_failed) {
  107. // build aicore task
  108. domi::ModelTaskDef model_task_def;
  109. std::shared_ptr<domi::ModelTaskDef> model_task_def_ptr = make_shared<domi::ModelTaskDef>(model_task_def);
  110. domi::TaskDef *task_def = model_task_def_ptr->add_task();
  111. GeModelPtr ge_model = make_shared<GeModel>();
  112. ge_model->SetModelTaskDef(model_task_def_ptr);
  113. auto aicore_task = std::unique_ptr<hybrid::AiCoreOpTask>(new(std::nothrow)hybrid::AiCoreOpTask());
  114. task_def->set_type(RT_MODEL_TASK_ALL_KERNEL);
  115. domi::KernelDefWithHandle *kernel_with_handle = task_def->mutable_kernel_with_handle();
  116. kernel_with_handle->set_original_kernel_key("");
  117. kernel_with_handle->set_node_info("");
  118. kernel_with_handle->set_block_dim(32);
  119. kernel_with_handle->set_args_size(64);
  120. string args(64, '1');
  121. kernel_with_handle->set_args(args.data(), 64);
  122. domi::KernelContext *context = kernel_with_handle->mutable_context();
  123. context->set_op_index(1);
  124. context->set_kernel_type(2); // ccKernelType::TE
  125. uint16_t args_offset[9] = {0};
  126. context->set_args_offset(args_offset, 9 * sizeof(uint16_t));
  127. OpDescPtr op_desc = CreateOpDesc("Add", "Add");
  128. std::vector<char> kernelBin;
  129. TBEKernelPtr tbe_kernel = std::make_shared<ge::OpKernelBin>("name/Add", std::move(kernelBin));
  130. op_desc->SetExtAttr(ge::OP_EXTATTR_NAME_TBE_KERNEL, tbe_kernel);
  131. std::string kernel_name("kernel/Add");
  132. AttrUtils::SetStr(op_desc, op_desc->GetName() + "_kernelname", kernel_name);
  133. ComputeGraphPtr graph = std::make_shared<ComputeGraph>("test");
  134. GeRootModelPtr ge_root_model = make_shared<GeRootModel>(graph);
  135. ge_root_model->SetModelName("test_name");
  136. HybridModel hybrid_model(ge_root_model);
  137. HybridModelBuilder hybrid_model_builder(hybrid_model);
  138. ASSERT_EQ(hybrid_model_builder.Build(), INTERNAL_ERROR);
  139. ASSERT_EQ(hybrid_model_builder.IndexTaskDefs(graph, ge_model), INTERNAL_ERROR);
  140. }
  141. TEST_F(UtestGeHybrid, parse_force_infershape_nodes) {
  142. const char *const kForceInfershape = "_force_infershape_when_running";
  143. auto graph = make_shared<ComputeGraph>("graph");
  144. OpDescPtr op_desc = CreateOpDesc("Conv2D", "Conv2D");
  145. ge::AttrUtils::SetBool(op_desc, kForceInfershape, true);
  146. auto node = graph->AddNode(op_desc);
  147. std::unique_ptr<NodeItem> new_node;
  148. NodeItem::Create(node, new_node);
  149. GeRootModelPtr ge_root_model = make_shared<GeRootModel>(graph);
  150. HybridModel hybrid_model(ge_root_model);
  151. HybridModelBuilder hybrid_model_builder(hybrid_model);
  152. ASSERT_EQ(hybrid_model_builder.ParseForceInfershapeNodes(node, *new_node), SUCCESS);
  153. }
  154. TEST_F(UtestGeHybrid, index_taskdefs_success) {
  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(0);
  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. NodePtr node = graph->AddNode(op_desc);
  183. GeRootModelPtr ge_root_model = make_shared<GeRootModel>(graph);
  184. HybridModel hybrid_model(ge_root_model);
  185. HybridModelBuilder hybrid_model_builder(hybrid_model);
  186. ASSERT_EQ(hybrid_model_builder.IndexTaskDefs(graph, ge_model), SUCCESS);
  187. }
  188. TEST_F(UtestGeHybrid, init_weight_success) {
  189. NpuMemoryAllocator::allocators_.emplace(make_pair(0, nullptr));
  190. // make graph with sub_graph
  191. ComputeGraphPtr graph = std::make_shared<ComputeGraph>("root_graph");
  192. OpDescPtr op_desc = CreateOpDesc("if", IF);
  193. NodePtr node = graph->AddNode(op_desc);
  194. // make sub graph
  195. ComputeGraphPtr sub_graph = std::make_shared<ComputeGraph>("if_sub_graph");
  196. OpDescPtr const_op_desc = CreateOpDesc("const", CONSTANT);
  197. vector<int64_t> dims_vec_0 = {2, 1, 4, 1, 2};
  198. vector<int32_t> data_vec_0 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
  199. GeTensorDesc tensor_desc_0(GeShape(dims_vec_0), FORMAT_NCHW, DT_INT32);
  200. (void)TensorUtils::SetRealDimCnt(tensor_desc_0, dims_vec_0.size());
  201. ConstGeTensorPtr constTensor_0 =
  202. std::make_shared<GeTensor>(tensor_desc_0, (uint8_t *)&data_vec_0[0], data_vec_0.size() * sizeof(int32_t));
  203. AttrUtils::SetTensor(const_op_desc, ge::ATTR_NAME_WEIGHTS, constTensor_0);
  204. const_op_desc->AddOutputDesc(tensor_desc_0);
  205. NodePtr const_node = sub_graph->AddNode(const_op_desc);
  206. graph->AddSubgraph("sub", sub_graph);
  207. GeRootModelPtr ge_root_model = make_shared<GeRootModel>(graph);
  208. GeModelPtr ge_sub_model = make_shared<GeModel>();
  209. //Buffer weight_buffer = Buffer(128,0);
  210. //ge_sub_model->SetWeight(weight_buffer);
  211. ge_root_model->SetSubgraphInstanceNameToModel("sub",ge_sub_model);
  212. HybridModel hybrid_model(ge_root_model);
  213. HybridModelBuilder hybrid_model_builder(hybrid_model);
  214. auto ret = hybrid_model_builder.InitWeights();
  215. ASSERT_EQ(ret,SUCCESS);
  216. Buffer weight_buffer = Buffer(128,0);
  217. ge_sub_model->SetWeight(weight_buffer);
  218. ret = hybrid_model_builder.InitWeights();
  219. ASSERT_EQ(ret,PARAM_INVALID);
  220. }
  221. TEST_F(UtestGeHybrid, hybrid_model_executor) {
  222. ComputeGraphPtr compute_graph = MakeShared<ComputeGraph>("abc");
  223. GeRootModelPtr root_model = MakeShared<ge::GeRootModel>(compute_graph);
  224. HybridModel model(root_model);
  225. HybridModel *model_ptr = &model;
  226. uint32_t device_id = 0;
  227. rtStream_t stream;
  228. HybridModelExecutor executor(model_ptr, device_id, stream);
  229. executor.Init();
  230. }
  231. TEST_F(UtestGeHybrid, unfold_subgraphs_success) {
  232. ComputeGraphPtr merged_graph = nullptr;
  233. ComputeGraphPtr sub_sub_graph1 = std::make_shared<ComputeGraph>("while_cond");
  234. OpDescPtr sub_sub_graph_while_cond_data_op_desc = CreateOpDesc("cond_data", DATA);
  235. NodePtr sub_sub_graph_while_cond_data_node = sub_sub_graph1->AddNode(sub_sub_graph_while_cond_data_op_desc);
  236. ComputeGraphPtr sub_sub_graph2 = std::make_shared<ComputeGraph>("while_body");
  237. /*OpDescPtr sub_sub_graph_while_body_const_op_desc = CreateOpDesc("body_const", CONSTANT);
  238. NodePtr sub_sub_graph_while_body_const_node = sub_sub_graph2->AddNode(sub_sub_graph_while_body_const_op_desc);*/
  239. OpDescPtr sub_sub_graph_while_body_data_op_desc = CreateOpDesc("body_data", DATA);
  240. NodePtr sub_sub_graph_while_body_data_node = sub_sub_graph2->AddNode(sub_sub_graph_while_body_data_op_desc);
  241. sub_sub_graph2->SetGraphUnknownFlag(true);
  242. /*OpDescPtr sub_sub_graph_while_body_add_op_desc = CreateOpDesc("body_add", ADD);
  243. NodePtr sub_sub_graph_while_body_add_node = sub_sub_graph2->AddNode(sub_sub_graph_while_body_add_node);
  244. sub_sub_graph_while_body_add_node->AddLinkFrom(sub_sub_graph_while_body_data_node);
  245. sub_sub_graph_while_body_add_node->AddLinkFrom(sub_sub_graph_while_body_const_node);*/
  246. ComputeGraphPtr sub_graph = std::make_shared<ComputeGraph>("sub_graph");
  247. OpDescPtr sub_graph_while_op_desc = CreateOpDesc("while", WHILE);
  248. NodePtr sub_graph_while_node = sub_graph->AddNode(sub_graph_while_op_desc);
  249. sub_graph->SetGraphUnknownFlag(true);
  250. sub_graph_while_node->GetOpDesc()->AddSubgraphName("while_cond");
  251. sub_graph_while_node->GetOpDesc()->AddSubgraphName("while_body");
  252. sub_graph_while_node->GetOpDesc()->SetSubgraphInstanceName(0, "while_cond");
  253. sub_graph_while_node->GetOpDesc()->SetSubgraphInstanceName(1, "while_body");
  254. ComputeGraphPtr root_graph = std::make_shared<ComputeGraph>("root_graph");
  255. auto partitioned_call_op_desc = MakeShared<OpDesc>("partitioned_call", PARTITIONEDCALL);
  256. auto partitioned_call_node = root_graph->AddNode(partitioned_call_op_desc);
  257. partitioned_call_node->GetOpDesc()->AddSubgraphName("sub_graph");
  258. partitioned_call_node->GetOpDesc()->SetSubgraphInstanceName(0, "sub_graph");
  259. root_graph->AddSubGraph(sub_sub_graph1);
  260. root_graph->AddSubGraph(sub_sub_graph2);
  261. sub_sub_graph1->SetParentGraph(root_graph);
  262. sub_sub_graph2->SetParentGraph(root_graph);
  263. sub_sub_graph1->SetParentNode(sub_graph_while_node);
  264. sub_sub_graph2->SetParentNode(sub_graph_while_node);
  265. root_graph->AddSubGraph(sub_graph);
  266. sub_graph->SetParentNode(partitioned_call_node);
  267. sub_graph->SetParentGraph(root_graph);
  268. GeRootModelPtr root_model = MakeShared<ge::GeRootModel>(root_graph);
  269. HybridModel hybrid_model(root_model);
  270. HybridModelBuilder hybrid_model_builder(hybrid_model);
  271. // subgraph num before unfold: 1
  272. EXPECT_EQ(root_graph->GetAllSubgraphs().size(), 3);
  273. // num of nodes in root_graph before unfold: 1, name: partitioned_call
  274. EXPECT_EQ(root_graph->GetDirectNodesSize(), 1);
  275. EXPECT_EQ(root_graph->GetDirectNode().at(0)->GetName(), "partitioned_call");
  276. // two sub_sub_graphs: while cond & while body, their parent graph is "subgraph" before unfold
  277. EXPECT_EQ(sub_sub_graph1->GetParentGraph()->GetName(), "root_graph");
  278. EXPECT_EQ(sub_sub_graph1->GetParentGraph()->GetName(), "root_graph");
  279. // node "cond_data" & "body_data" has owner compute graph "subgraph" before unfold
  280. EXPECT_EQ(sub_graph_while_node->GetOwnerComputeGraph()->GetName(), "sub_graph");
  281. // unfold success
  282. EXPECT_EQ(hybrid_model_builder.UnfoldSubgraphs(root_graph, merged_graph), SUCCESS);
  283. // subgraph num after unfold: 0
  284. EXPECT_EQ(merged_graph->GetAllSubgraphs().size(), 2);
  285. // num of nodes in MergedGraph after unfold: 1, name: while
  286. EXPECT_EQ(merged_graph->GetDirectNodesSize(), 1);
  287. EXPECT_EQ(merged_graph->GetDirectNode().at(0)->GetName(), "while");
  288. // two sub_sub_graphs: while cond & while body, their parent graph is "MergedGraph" after unfold
  289. EXPECT_EQ(sub_sub_graph1->GetParentGraph()->GetName(), "MergedGraph" );
  290. EXPECT_EQ(sub_sub_graph1->GetParentGraph()->GetName(), "MergedGraph");
  291. // node "cond_data" & "body_data" has owner compute graph "MergedGraph" before unfold
  292. EXPECT_EQ(sub_graph_while_node->GetOwnerComputeGraph()->GetName(), "MergedGraph");
  293. }

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