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.

graph_builder.cc 27 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /**
  2. * Copyright 2019-2020 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 "graph/build/graph_builder.h"
  17. #include "common/ge/ge_util.h"
  18. #include "common/helper/model_helper.h"
  19. #include "graph/build/logical_stream_allocator.h"
  20. #include "graph/build/run_context.h"
  21. #include "graph/build/stream_graph_optimizer.h"
  22. #include "graph/common/ge_call_wrapper.h"
  23. #include "graph/ge_context.h"
  24. #include "graph/manager/graph_var_manager.h"
  25. #include "graph/passes/mark_same_addr_pass.h"
  26. #include "graph/utils/node_utils.h"
  27. #include "graph/utils/type_utils.h"
  28. #include "init/gelib.h"
  29. using domi::BuildMode;
  30. namespace {
  31. const int32_t kInvalidPerfLevel = -1;
  32. enum NodeType { kSubgraphData, kSubgraphNode, kOthers };
  33. } // namespace
  34. namespace ge {
  35. NodeType TransferNodeType(const NodePtr &node) {
  36. const std::string type = node->GetType();
  37. if (type == ge::DATA) {
  38. if (node->GetOwnerComputeGraph()->GetParentNode() == nullptr) {
  39. GELOGD("access src data node:%s", node->GetName().c_str());
  40. return kOthers;
  41. }
  42. GELOGD("access subgraph input node:%s", node->GetName().c_str());
  43. return kSubgraphData;
  44. } else if (type == PARTITIONEDCALL) {
  45. GELOGD("access subgraph node:%s", node->GetName().c_str());
  46. return kSubgraphNode;
  47. }
  48. GELOGD("access other node:%s", node->GetName().c_str());
  49. return kOthers;
  50. }
  51. Status HandleSubgraphNode(NodePtr &src_node, OutDataAnchorPtr &src_out_anchor) {
  52. auto subgraph = NodeUtils::GetSubgraph(*src_node, 0);
  53. GE_CHECK_NOTNULL(subgraph);
  54. const NodePtr &net_output_node = subgraph->FindFirstNodeMatchType(NETOUTPUT);
  55. GE_CHECK_NOTNULL(net_output_node);
  56. const InDataAnchorPtr &in_data_anchor = net_output_node->GetInDataAnchor(src_out_anchor->GetIdx());
  57. GE_CHECK_NOTNULL(in_data_anchor);
  58. const OutDataAnchorPtr &peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  59. GE_CHECK_NOTNULL(peer_out_anchor);
  60. src_node = peer_out_anchor->GetOwnerNode();
  61. src_out_anchor = peer_out_anchor;
  62. return SUCCESS;
  63. }
  64. Status HandleSubgraphDataNode(NodePtr &src_node, OutDataAnchorPtr &src_out_anchor) {
  65. uint32_t index = 0;
  66. if (!AttrUtils::GetInt(src_node->GetOpDesc(), ATTR_NAME_PARENT_NODE_INDEX, index)) {
  67. GELOGE(FAILED, "Get attr ATTR_NAME_PARENT_NODE_INDEX failed, node:%s.", src_node->GetName().c_str());
  68. return FAILED;
  69. }
  70. const NodePtr &parent_node = src_node->GetOwnerComputeGraph()->GetParentNode();
  71. GE_CHECK_NOTNULL(parent_node);
  72. const InDataAnchorPtr &in_data_anchor = parent_node->GetInDataAnchor(index);
  73. GE_CHECK_NOTNULL(in_data_anchor);
  74. const OutDataAnchorPtr &peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  75. GE_CHECK_NOTNULL(peer_out_anchor);
  76. src_node = peer_out_anchor->GetOwnerNode();
  77. src_out_anchor = peer_out_anchor;
  78. return SUCCESS;
  79. }
  80. GraphBuilder::GraphBuilder() : build_mode_(BuildMode::GEN_TASK_WITH_FUSION), hcom_parallel_(false) {}
  81. void GraphBuilder::SetOptions(const ge::GraphManagerOptions &options) {
  82. stream_max_parallel_num_ = options.stream_max_parallel_num;
  83. hcom_parallel_ = options.hcom_parallel;
  84. if (options.perf_level == kInvalidPerfLevel) {
  85. build_mode_ = static_cast<int>(BuildMode::GEN_TASK_WITH_FUSION);
  86. } else {
  87. build_mode_ = options.perf_level;
  88. }
  89. }
  90. Status GraphBuilder::CalcOpParam(const ge::ComputeGraphPtr &graph) {
  91. GELOGI("Begin to calculate op running param.");
  92. GE_CHECK_NOTNULL(graph);
  93. auto instance_ptr = ge::GELib::GetInstance();
  94. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  95. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "GraphBuilder: GE is not initialized");
  96. return GE_CLI_GE_NOT_INITIALIZED;
  97. }
  98. for (const auto &node_ptr : graph->GetNodes(graph->GetGraphUnknownFlag())) {
  99. GE_CHECK_NOTNULL(node_ptr->GetOpDesc());
  100. std::string kernel_lib_name = node_ptr->GetOpDesc()->GetOpKernelLibName();
  101. if (kernel_lib_name.empty()) {
  102. // reset op kernel lib
  103. (void)instance_ptr->DNNEngineManagerObj().GetDNNEngineName(node_ptr);
  104. kernel_lib_name = node_ptr->GetOpDesc()->GetOpKernelLibName();
  105. if (kernel_lib_name.empty()) {
  106. GELOGE(INTERNAL_ERROR, "Get node:%s(%s) kernel lib failed.", node_ptr->GetName().c_str(),
  107. node_ptr->GetType().c_str());
  108. return INTERNAL_ERROR;
  109. }
  110. }
  111. OpsKernelInfoStorePtr kernel_info = instance_ptr->OpsKernelManagerObj().GetOpsKernelInfoStore(kernel_lib_name);
  112. if (kernel_info != nullptr) {
  113. auto ret = SetInputSize(node_ptr);
  114. if (ret != SUCCESS) {
  115. GELOGE(ret, "Set node inputDesc size failed, node name is %s", node_ptr->GetName().c_str());
  116. return ret;
  117. }
  118. ret = kernel_info->CalcOpRunningParam(*node_ptr);
  119. if (ret != SUCCESS) {
  120. GELOGE(ret, "Calculate op running param failed, node name is %s", node_ptr->GetName().c_str());
  121. return ret;
  122. }
  123. GE_CHK_STATUS_RET(AddOutputMemTypeForNode(node_ptr));
  124. } else {
  125. GELOGE(GE_GRAPH_PARAM_NULLPTR, "Get op %s ops kernel info store failed", node_ptr->GetName().c_str());
  126. return INTERNAL_ERROR;
  127. }
  128. }
  129. auto parent_node = graph->GetParentNode();
  130. if (parent_node == nullptr) {
  131. GELOGI("Graph[%s] do not have parent node, no need update parent node output size.", graph->GetName().c_str());
  132. return SUCCESS;
  133. }
  134. GE_CHK_STATUS_RET(UpdateParentNodeOutputSize(graph, parent_node));
  135. GELOGI("Success to calculate op running param.");
  136. return SUCCESS;
  137. }
  138. Status GraphBuilder::UpdateParentNodeOutputSize(const ge::ComputeGraphPtr &graph, ge::NodePtr &parent_node_ptr) {
  139. GELOGI("Begin to update parent node[%s] of graph[%s] output size.", parent_node_ptr->GetName().c_str(),
  140. graph->GetName().c_str());
  141. auto parent_op_desc = parent_node_ptr->GetOpDesc();
  142. GE_CHECK_NOTNULL(parent_op_desc);
  143. bool is_unknown_shape = graph->GetGraphUnknownFlag();
  144. if (is_unknown_shape) {
  145. GELOGI("Current graph[%s] is unknown, no need to update parent node[%s] output size.", graph->GetName().c_str(),
  146. parent_node_ptr->GetName().c_str());
  147. return SUCCESS;
  148. }
  149. for (const auto &node_ptr : graph->GetDirectNode()) {
  150. if (node_ptr->GetType() != NETOUTPUT) {
  151. continue;
  152. }
  153. auto op_desc = node_ptr->GetOpDesc();
  154. GE_CHECK_NOTNULL(op_desc);
  155. for (const auto &in_data_anchor : node_ptr->GetAllInDataAnchors()) {
  156. auto index = in_data_anchor->GetIdx();
  157. ge::GeTensorDesc desc_temp = op_desc->GetInputDesc(index);
  158. uint32_t parent_index = 0;
  159. if (!AttrUtils::GetInt(desc_temp, ATTR_NAME_PARENT_NODE_INDEX, parent_index)) {
  160. GELOGI("NetOutput input tensor %d, attr %s not found.", index, ATTR_NAME_PARENT_NODE_INDEX.c_str());
  161. continue;
  162. }
  163. int64_t size = 0;
  164. GE_IF_BOOL_EXEC(ge::TensorUtils::GetSize(desc_temp, size) != SUCCESS, GELOGI("Get size failed!"));
  165. ge::GeTensorDesc parent_desc_temp = parent_op_desc->GetOutputDesc(parent_index);
  166. ge::TensorUtils::SetSize(parent_desc_temp, size);
  167. GE_CHK_STATUS_RET(parent_op_desc->UpdateOutputDesc(parent_index, parent_desc_temp));
  168. GELOGI("Update parent node[%s] output index[%u] to size[%ld].", parent_node_ptr->GetName().c_str(), parent_index,
  169. size);
  170. }
  171. }
  172. return SUCCESS;
  173. }
  174. Status GraphBuilder::Build(ComputeGraphPtr &comp_graph, std::vector<SubGraphInfoPtr> &subgraph_ptr_list,
  175. GeRootModelPtr &ge_root_model_ptr, uint64_t session_id) {
  176. GELOGI("Start to build model.");
  177. if (comp_graph == nullptr) {
  178. GELOGE(GE_GRAPH_PARAM_NULLPTR, "Graph build comp_graph is null.");
  179. return GE_GRAPH_PARAM_NULLPTR;
  180. }
  181. ge_root_model_ptr = MakeShared<ge::GeRootModel>(comp_graph);
  182. if (ge_root_model_ptr == nullptr) {
  183. return MEMALLOC_FAILED;
  184. }
  185. GeModelPtr ge_model_ptr = nullptr;
  186. bool is_dynamic_shape = false;
  187. // To be compatible with the old process, do not verify the return value temporarily.
  188. (void)AttrUtils::GetBool(comp_graph, ATTR_NAME_DYNAMIC_SHAPE_PARTITIONED, is_dynamic_shape);
  189. if (is_dynamic_shape) {
  190. GE_CHK_STATUS_RET(
  191. BuildForDynamicShapeGraph(comp_graph, subgraph_ptr_list, ge_root_model_ptr, ge_model_ptr, session_id),
  192. "Build for dynamic shape graph failed.");
  193. return SUCCESS;
  194. }
  195. GE_CHK_STATUS_RET(BuildForKnownShapeGraph(comp_graph, subgraph_ptr_list, ge_model_ptr, session_id),
  196. "Build for known shape graph failed.");
  197. ge_root_model_ptr->SetSubgraphInstanceNameToModel(comp_graph->GetName(), ge_model_ptr);
  198. return SUCCESS;
  199. }
  200. Status GraphBuilder::BuildForKnownShapeGraph(ComputeGraphPtr &comp_graph, std::vector<SubGraphInfoPtr> &subgraph_list,
  201. GeModelPtr &ge_model_ptr, uint64_t session_id) {
  202. if (ge::GetContext().GetHostExecFlag()) {
  203. GE_CHK_STATUS_RET(BuildForHostCpuGraph(comp_graph, ge_model_ptr, session_id), "Build for host-cpu graph failed.");
  204. return SUCCESS;
  205. }
  206. GELOGI("Begin to build known shape graph[%s].", comp_graph->GetName().c_str());
  207. Status ret = SecondPartition(comp_graph, subgraph_list);
  208. GE_CHK_STATUS_RET(ret, "Graph[%s] second partition Failed.", comp_graph->GetName().c_str());
  209. auto subgraph_map = graph_partitioner_.GetSubGraphMap();
  210. GE_TIMESTAMP_START(BuildSubgraph);
  211. ge::ModelBuilder builder(session_id, comp_graph, subgraph_map, stream_max_parallel_num_, hcom_parallel_, build_mode_);
  212. GE_DUMP(comp_graph, "BeforePreBuildModel");
  213. GE_TIMESTAMP_START(PreBuildModel);
  214. GE_CHK_STATUS_RET(builder.PreBuildModel(), "Graph[%s] builder PreBuildModel() return fail.",
  215. comp_graph->GetName().c_str());
  216. GE_TIMESTAMP_END(PreBuildModel, "GraphBuilder::PreBuildModel");
  217. GE_DUMP(comp_graph, "AfterPreBuildModel");
  218. GE_TIMESTAMP_START(CalcOpParam);
  219. GE_CHK_STATUS_RET(CalcOpParam(comp_graph), "Graph[%s] builder CalcOpParam() return fail.",
  220. comp_graph->GetName().c_str());
  221. GE_TIMESTAMP_END(CalcOpParam, "GraphBuilder::CalcOpParam");
  222. GE_DUMP(comp_graph, "AfterCalcOpParam");
  223. ModelPtr model_ptr = MakeShared<ge::Model>();
  224. if (model_ptr == nullptr) {
  225. return MEMALLOC_FAILED;
  226. }
  227. GE_TIMESTAMP_START(BuildModelForGetTask);
  228. GE_CHK_STATUS_RET(builder.BuildModelForGetTask(*model_ptr), "Graph[%s] builder BuildModelForGetTask() return fail.",
  229. comp_graph->GetName().c_str());
  230. GE_TIMESTAMP_END(BuildModelForGetTask, "GraphBuilder::BuildModelForGetTask");
  231. GE_DUMP(comp_graph, "AfterBuildModel");
  232. GE_TIMESTAMP_START(GetTaskInfo);
  233. ret = GetTaskInfo(builder, model_ptr, comp_graph, subgraph_map, session_id);
  234. GE_TIMESTAMP_END(GetTaskInfo, "GraphBuilder::GetTaskInfo");
  235. GE_DUMP(comp_graph, "AfterGetTask");
  236. if (ret != SUCCESS) {
  237. GELOGE(ret, "Graph[%s] builder GetTaskInfo() return fail.", comp_graph->GetName().c_str());
  238. return ret;
  239. }
  240. ge_model_ptr = MakeShared<ge::GeModel>();
  241. if (ge_model_ptr == nullptr) {
  242. return MEMALLOC_FAILED;
  243. }
  244. GE_CHK_STATUS_RET(builder.SaveDataToModel(*model_ptr, *ge_model_ptr),
  245. "Graph[%s] builder SaveDataToModel() return fail.", comp_graph->GetName().c_str());
  246. GELOGI("Success to build graph[%s] model.", comp_graph->GetName().c_str());
  247. GE_TIMESTAMP_END(BuildSubgraph, "GraphBuilder::Build");
  248. return SUCCESS;
  249. }
  250. Status GraphBuilder::BuildForUnknownShapeGraph(ComputeGraphPtr &comp_graph, GeModelPtr &ge_model_ptr,
  251. uint64_t session_id) {
  252. GELOGI("Begin to build unknown shape graph[%s].", comp_graph->GetName().c_str());
  253. GE_TIMESTAMP_START(CalcOpParam);
  254. GE_CHK_STATUS_RET(CalcOpParam(comp_graph), "Graph[%s] builder CalcOpParam() return fail.",
  255. comp_graph->GetName().c_str());
  256. GE_TIMESTAMP_END(CalcOpParam, "GraphBuilder::CalcOpParam");
  257. GE_DUMP(comp_graph, "AfterCalcOpParam");
  258. Graph2SubGraphInfoList subgraph_map;
  259. ge::ModelBuilder builder(session_id, comp_graph, subgraph_map, stream_max_parallel_num_, hcom_parallel_, build_mode_);
  260. ModelPtr model_ptr = MakeShared<ge::Model>();
  261. if (model_ptr == nullptr) {
  262. return MEMALLOC_FAILED;
  263. }
  264. GE_TIMESTAMP_START(BuildModelForGetDynShapeTask);
  265. GE_CHK_STATUS_RET(builder.BuildModelForGetDynShapeTask(*model_ptr),
  266. "Graph[%s] builder BuildModelForGetDynShapeTask() return fail.", comp_graph->GetName().c_str());
  267. GE_TIMESTAMP_END(BuildModelForGetDynShapeTask, "GraphBuilder::BuildModelForGetDynShapeTask");
  268. GE_TIMESTAMP_START(GetTaskInfo);
  269. Status ret = GetTaskInfo(builder, model_ptr, comp_graph, subgraph_map, session_id);
  270. GE_TIMESTAMP_END(GetTaskInfo, "GraphBuilder::GetTaskInfo");
  271. GraphUtils::DumpGEGraph(comp_graph, "AfterGetTask");
  272. GraphUtils::DumpGEGraphToOnnx(*comp_graph, "AfterGetTask");
  273. if (ret != SUCCESS) {
  274. GELOGE(ret, "Graph[%s] builder GetTaskInfo() return fail.", comp_graph->GetName().c_str());
  275. return ret;
  276. }
  277. ge_model_ptr = MakeShared<ge::GeModel>();
  278. if (ge_model_ptr == nullptr) {
  279. return MEMALLOC_FAILED;
  280. }
  281. GE_CHK_STATUS_RET(builder.SaveDataToModel(*model_ptr, *ge_model_ptr),
  282. "Graph[%s] builder SaveDataToModel() return fail.", comp_graph->GetName().c_str());
  283. GELOGI("Success to build graph[%s] model.", comp_graph->GetName().c_str());
  284. return SUCCESS;
  285. }
  286. Status GraphBuilder::BuildForHostCpuGraph(ComputeGraphPtr &comp_graph, GeModelPtr &ge_model_ptr, uint64_t session_id) {
  287. return BuildForUnknownShapeGraph(comp_graph, ge_model_ptr, session_id);
  288. }
  289. Status GraphBuilder::BuildForDynamicShapeGraph(ComputeGraphPtr &comp_graph,
  290. std::vector<SubGraphInfoPtr> &subgraph_ptr_list,
  291. GeRootModelPtr &ge_root_model_ptr, GeModelPtr &ge_model_ptr,
  292. uint64_t session_id) {
  293. GELOGI("Start to build BuildForDynamicShape for dynamic shape.");
  294. // Update Root Graph Data size
  295. for (auto &node : comp_graph->GetDirectNode()) {
  296. auto op_desc = node->GetOpDesc();
  297. GE_CHECK_NOTNULL(op_desc);
  298. op_desc->SetStreamId(kInvalidStream);
  299. if (node->GetType() == DATA) {
  300. GE_CHK_STATUS_RET(CalcDynShapeRootGraphDataSize(op_desc), "Calc dynamic shape root graph data[%s] size failed.",
  301. op_desc->GetName().c_str());
  302. }
  303. }
  304. //
  305. for (auto &sub_graph : comp_graph->GetAllSubgraphs()) {
  306. // exclude functional subgraph in known subgraph
  307. if (sub_graph->GetParentGraph() != comp_graph && !sub_graph->GetParentGraph()->GetGraphUnknownFlag()) {
  308. continue;
  309. }
  310. if (sub_graph->GetGraphUnknownFlag()) {
  311. // unknown shape build flow
  312. GE_CHK_STATUS_RET(BuildForUnknownShapeGraph(sub_graph, ge_model_ptr, session_id),
  313. "Build for unknown shape graph failed.");
  314. } else {
  315. // reset functional subgraph parent graph as known subgraph
  316. for (const auto &node : sub_graph->GetDirectNode()) {
  317. for (const auto &sub_graph_name : node->GetOpDesc()->GetSubgraphInstanceNames()) {
  318. auto sub_sub_graph = comp_graph->GetSubgraph(sub_graph_name);
  319. GE_CHK_STATUS_RET(sub_graph->AddSubgraph(sub_sub_graph), "Failed add subgraph to known graph.");
  320. }
  321. }
  322. // known shape build flow
  323. GE_CHK_STATUS_RET(BuildForKnownShapeGraph(sub_graph, subgraph_ptr_list, ge_model_ptr, session_id),
  324. "Build for known shape graph failed.");
  325. }
  326. ge_root_model_ptr->SetSubgraphInstanceNameToModel(sub_graph->GetName(), ge_model_ptr);
  327. }
  328. return SUCCESS;
  329. }
  330. Status GraphBuilder::GetTaskInfo(const ge::ModelBuilder &builder, const ModelPtr &model_ptr,
  331. ComputeGraphPtr &comp_graph, Graph2SubGraphInfoList &subgraph_map,
  332. uint64_t session_id) {
  333. GE_CHECK_NOTNULL(model_ptr);
  334. GE_CHECK_NOTNULL(comp_graph);
  335. int64_t memory_size = 0;
  336. if (!AttrUtils::GetInt(model_ptr, ATTR_MODEL_MEMORY_SIZE, memory_size)) {
  337. GELOGE(INTERNAL_ERROR, "Get memory size fail.");
  338. return INTERNAL_ERROR;
  339. }
  340. int64_t p2p_memory_size = 0;
  341. if (!AttrUtils::GetInt(model_ptr, ATTR_MODEL_P2P_MEMORY_SIZE, p2p_memory_size)) {
  342. GELOGE(INTERNAL_ERROR, "Get p2p memory size fail.");
  343. return INTERNAL_ERROR;
  344. }
  345. int64_t weight_size = 0;
  346. if (!AttrUtils::GetInt(model_ptr, ATTR_MODEL_WEIGHT_SIZE, weight_size)) {
  347. GELOGE(INTERNAL_ERROR, "Get weight memory size fail.");
  348. return INTERNAL_ERROR;
  349. }
  350. auto var_manager = VarManager::Instance(session_id);
  351. auto *get_mem_base = reinterpret_cast<uint8_t *>(reinterpret_cast<uintptr_t>(var_manager->GetVarMemMaxSize()));
  352. uint8_t *get_weight_mem_base = get_mem_base;
  353. if (weight_size > 0) {
  354. get_weight_mem_base = get_mem_base + memory_size + p2p_memory_size;
  355. }
  356. std::map<int64_t, uint8_t *> mem_type_to_data_mem_base;
  357. mem_type_to_data_mem_base[RT_MEMORY_HBM] = get_mem_base;
  358. if (p2p_memory_size == 0) {
  359. mem_type_to_data_mem_base[RT_MEMORY_P2P_DDR] = nullptr;
  360. } else {
  361. mem_type_to_data_mem_base[RT_MEMORY_P2P_DDR] = get_mem_base + memory_size;
  362. }
  363. std::map<int64_t, uint64_t> mem_type_to_data_mem_size;
  364. mem_type_to_data_mem_size[RT_MEMORY_HBM] = memory_size;
  365. mem_type_to_data_mem_size[RT_MEMORY_P2P_DDR] = p2p_memory_size;
  366. RunContextUtil run_context;
  367. Status ret = run_context.InitMemInfo(get_mem_base, memory_size, mem_type_to_data_mem_base, mem_type_to_data_mem_size,
  368. get_weight_mem_base, weight_size);
  369. if (ret != SUCCESS) {
  370. GELOGE(ret, "task_generator init mem info fail.");
  371. return ret;
  372. }
  373. auto weight_buffer = builder.GetWeightBuffer();
  374. ret = run_context.CreateRunContext(*model_ptr, comp_graph, weight_buffer, session_id);
  375. if (ret != SUCCESS) {
  376. GELOGE(ret, "runContext create run context fail.");
  377. return ret;
  378. }
  379. StreamGraphOptimizer stream_optimizer;
  380. ret = stream_optimizer.OptimizeStreamedSubGraph(comp_graph, subgraph_map, run_context.GetRunContext());
  381. if (ret != SUCCESS) {
  382. GELOGE(ret, "Optimize streamed subGraph fail.");
  383. return ret;
  384. }
  385. GE_DUMP(comp_graph, "AfterOptimizeStreamedSubGraph");
  386. auto *get_var_mem_base = reinterpret_cast<uint8_t *>(reinterpret_cast<uintptr_t>(var_manager->GetVarMemLogicBase()));
  387. uint64_t var_size = (var_manager->GetVarMemSize(RT_MEMORY_HBM) > 0) ? var_manager->GetVarMemMaxSize() : 0;
  388. TaskGenerator task_generator(get_var_mem_base, var_size);
  389. ret = task_generator.GetTaskInfo(*model_ptr, comp_graph, session_id, run_context.GetRunContext());
  390. return ret;
  391. }
  392. Status GraphBuilder::SetInputSize(const ge::NodePtr &node_ptr) {
  393. // set input_desc.size = src_node.output_desc.size
  394. if (node_ptr->GetType() == DATA) {
  395. bool is_unknown_shape = false;
  396. GE_CHK_STATUS_RET(ge::NodeUtils::GetNodeUnknownShapeStatus(*node_ptr, is_unknown_shape),
  397. "Get data node[%s] shape status failed!", node_ptr->GetName().c_str());
  398. if (is_unknown_shape) {
  399. GELOGD("data node: %s is unknown shape, do not set input size!", node_ptr->GetName().c_str());
  400. return SUCCESS;
  401. }
  402. if (UpdateDataInputSize(node_ptr) != SUCCESS) {
  403. GELOGE(FAILED, "Update data input size failed.");
  404. return FAILED;
  405. }
  406. }
  407. for (const auto &in_data_anchor : node_ptr->GetAllInDataAnchors()) {
  408. const auto &peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  409. GE_IF_BOOL_EXEC(peer_out_anchor == nullptr, continue);
  410. const auto &src_node = peer_out_anchor->GetOwnerNode();
  411. const auto &src_op = src_node->GetOpDesc();
  412. GE_IF_BOOL_EXEC(src_op == nullptr, continue);
  413. auto node_op_desc = node_ptr->GetOpDesc();
  414. GE_IF_BOOL_EXEC(node_op_desc == nullptr, continue);
  415. // set dst_node.input_desc = src_node.output_desc
  416. ge::GeTensorDesc desc_temp(src_op->GetOutputDesc(peer_out_anchor->GetIdx()));
  417. int64_t size = 0;
  418. GE_IF_BOOL_EXEC(ge::TensorUtils::GetSize(desc_temp, size) != SUCCESS, GELOGI("Get size failed!"));
  419. GELOGD("src node %s output desc, dim_size: %zu, mem_size: %ld, format: %s, type: %s.", src_node->GetName().c_str(),
  420. desc_temp.GetShape().GetDimNum(), size, TypeUtils::FormatToSerialString(desc_temp.GetFormat()).c_str(),
  421. TypeUtils::DataTypeToSerialString(desc_temp.GetDataType()).c_str());
  422. for (size_t i = 0; i < desc_temp.GetShape().GetDimNum(); ++i) {
  423. GELOGD("dims[%zu]: %ld", i, desc_temp.GetShape().GetDim(i));
  424. }
  425. auto input_desc = node_op_desc->GetInputDescPtr(in_data_anchor->GetIdx());
  426. GE_CHECK_NOTNULL(input_desc);
  427. ge::TensorUtils::SetSize(const_cast<GeTensorDesc &>(*input_desc), size);
  428. GE_CHK_STATUS_RET(node_op_desc->UpdateInputDesc(in_data_anchor->GetIdx(), *input_desc));
  429. GELOGD("%s input desc, dim_size: %zu, mem_size: %ld, format: %s, type: %s.", node_ptr->GetName().c_str(),
  430. input_desc->GetShape().GetDimNum(), size, TypeUtils::FormatToSerialString(input_desc->GetFormat()).c_str(),
  431. TypeUtils::DataTypeToSerialString(input_desc->GetDataType()).c_str());
  432. }
  433. return SUCCESS;
  434. }
  435. Status GraphBuilder::UpdateDataInputSize(const ge::NodePtr &node_ptr) {
  436. const auto &op_desc = node_ptr->GetOpDesc();
  437. if (op_desc == nullptr) {
  438. GELOGE(FAILED, "Op desc is nullptr.");
  439. return FAILED;
  440. }
  441. // data op only has one output anchor
  442. ge::GeTensorDesc output_desc = op_desc->GetOutputDesc(0);
  443. int64_t output_size = 0;
  444. if (ge::TensorUtils::GetSize(output_desc, output_size) != SUCCESS) {
  445. GELOGW("Get size failed!");
  446. }
  447. if (output_size > 0) {
  448. GELOGI("No need to update data input size.");
  449. return SUCCESS;
  450. } else {
  451. int64_t real_dim_size = 0;
  452. ge::graphStatus graph_status = TensorUtils::GetTensorSizeInBytes(output_desc, real_dim_size);
  453. if (graph_status != GRAPH_SUCCESS) {
  454. GELOGE(FAILED, "Get tensor size in bytes failed.");
  455. return FAILED;
  456. }
  457. // data op only has one input anchor
  458. ge::GeTensorDesc input_desc = op_desc->GetInputDesc(0);
  459. ge::TensorUtils::SetSize(input_desc, real_dim_size);
  460. if (op_desc->UpdateInputDesc(0, input_desc) != GRAPH_SUCCESS) {
  461. GELOGE(FAILED, "Update input desc size failed.");
  462. return FAILED;
  463. }
  464. }
  465. return SUCCESS;
  466. }
  467. Status GraphBuilder::CalcDynShapeRootGraphDataSize(const ge::OpDescPtr &op_desc) {
  468. GELOGI("Begin to calc dynamic shape graph data[%s] size.", op_desc->GetName().c_str());
  469. // data op only has one output anchor
  470. ge::GeTensorDesc output_desc = op_desc->GetOutputDesc(0);
  471. if (output_desc.MutableShape().IsUnknownShape()) {
  472. GELOGI("No need to update dynamic shape graph data output size for unknown shape data.");
  473. return SUCCESS;
  474. }
  475. int64_t output_size = 0;
  476. if (ge::TensorUtils::GetSize(output_desc, output_size) != SUCCESS) {
  477. GELOGW("Get size failed!");
  478. }
  479. if (output_size > 0) {
  480. GELOGI("No need to update dynamic shape graph data output size[%ld].", output_size);
  481. return SUCCESS;
  482. } else {
  483. int64_t real_dim_size = 0;
  484. ge::graphStatus graph_status = TensorUtils::GetTensorSizeInBytes(output_desc, real_dim_size);
  485. if (graph_status != GRAPH_SUCCESS) {
  486. GELOGE(FAILED, "Get tensor size in bytes failed.");
  487. return FAILED;
  488. }
  489. ge::TensorUtils::SetSize(output_desc, real_dim_size);
  490. GELOGI("Update dynamic shape graph data output size to [%ld].", real_dim_size);
  491. if (op_desc->UpdateOutputDesc(0, output_desc) != GRAPH_SUCCESS) {
  492. GELOGE(FAILED, "Update dynamic shape graph data output desc size failed.");
  493. return FAILED;
  494. }
  495. }
  496. return SUCCESS;
  497. }
  498. Status GraphBuilder::SecondPartition(ge::ComputeGraphPtr &comp_graph, vector<ge::SubGraphInfoPtr> &subgraph_ptr_list) {
  499. GELOGI("[SecondPartition] second partition.");
  500. GE_TIMESTAMP_START(GraphPartition2);
  501. auto ret = graph_partitioner_.Partition(comp_graph, GraphPartitioner::kSecondPartitioning);
  502. if (ret != SUCCESS) {
  503. GELOGE(ret, "Graph partition Failed");
  504. return ret;
  505. }
  506. GE_CHK_STATUS_RET(ret, "Graph partition Failed.");
  507. auto graph_2_subgraphlist = graph_partitioner_.GetSubGraphMap();
  508. if (graph_2_subgraphlist.find(comp_graph) != graph_2_subgraphlist.end()) {
  509. subgraph_ptr_list = graph_2_subgraphlist[comp_graph];
  510. } else {
  511. GELOGE(FAILED, "Find subgraph failed.");
  512. return FAILED;
  513. }
  514. GE_TIMESTAMP_END(GraphPartition2, "GraphPartitioner::Partition2");
  515. return ret;
  516. }
  517. Status GraphBuilder::AddOutputMemTypeForNode(const NodePtr &node) {
  518. auto op_desc = node->GetOpDesc();
  519. GE_CHECK_NOTNULL(op_desc);
  520. uint32_t mem_type;
  521. if (!AttrUtils::GetInt(op_desc, ATTR_INPUT_MEMORY_TYPE, mem_type)) {
  522. return SUCCESS;
  523. }
  524. GELOGD("[%s] has attr input_memory_type %ld", op_desc->GetName().c_str(), mem_type);
  525. for (const auto &in_data_anchor : node->GetAllInDataAnchors()) {
  526. const auto &peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  527. GE_IF_BOOL_EXEC(peer_out_anchor == nullptr, continue);
  528. bool valid_flag = false;
  529. auto src_node = peer_out_anchor->GetOwnerNode();
  530. auto src_out_anchor = peer_out_anchor;
  531. while (true) {
  532. const auto &src_desc = src_node->GetOpDesc();
  533. GE_IF_BOOL_EXEC(src_desc == nullptr, continue);
  534. GELOGD("[%s:%u] set attr output_memory_type %ld", src_desc->GetName().c_str(), src_out_anchor->GetIdx(),
  535. mem_type);
  536. if (!AttrUtils::SetInt(src_desc->MutableOutputDesc(src_out_anchor->GetIdx()), ATTR_OUTPUT_MEMORY_TYPE,
  537. mem_type)) {
  538. GELOGE(INTERNAL_ERROR, "Set out_memory_type attr for [%s:%d] failed.", src_desc->GetName().c_str(),
  539. src_out_anchor->GetIdx());
  540. return INTERNAL_ERROR;
  541. }
  542. switch (TransferNodeType(src_node)) {
  543. case kSubgraphNode:
  544. GE_CHK_STATUS_RET(HandleSubgraphNode(src_node, src_out_anchor), "Handle subgraph node %s failed",
  545. src_node->GetName().c_str());
  546. break;
  547. case kSubgraphData:
  548. GE_CHK_STATUS_RET(HandleSubgraphDataNode(src_node, src_out_anchor), "Handle Data node %s in subgraph failed",
  549. src_node->GetName().c_str());
  550. break;
  551. case kOthers:
  552. default:
  553. valid_flag = true;
  554. break;
  555. }
  556. if (valid_flag) {
  557. break;
  558. }
  559. }
  560. }
  561. return SUCCESS;
  562. }
  563. } // namespace ge

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