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

5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 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
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 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
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 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
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. /**
  2. * Copyright 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 "graph/build/memory/graph_mem_assigner.h"
  18. #include "common/ge/ge_util.h"
  19. #include "framework/common/helper/model_helper.h"
  20. #include "graph/build/logical_stream_allocator.h"
  21. #include "graph/build/run_context.h"
  22. #include "graph/build/stream_graph_optimizer.h"
  23. #include "graph/common/ge_call_wrapper.h"
  24. #include "graph/ge_context.h"
  25. #include "graph/manager/graph_var_manager.h"
  26. #include "graph/passes/mark_same_addr_pass.h"
  27. #include "graph/utils/node_utils.h"
  28. #include "graph/utils/type_utils.h"
  29. #include "init/gelib.h"
  30. #include "model/ge_model.h"
  31. #include "graph/ge_context.h"
  32. #include "opskernel_manager/ops_kernel_builder_manager.h"
  33. #include "graph/utils/op_desc_utils.h"
  34. using domi::BuildMode;
  35. namespace {
  36. const int32_t kInvalidPerfLevel = -1;
  37. const int64_t kProfilingArStep = 2;
  38. const int64_t kProfilingArStartLogid = 3;
  39. enum NodeType { kSubgraphData, kSubgraphNode, kOthers };
  40. } // namespace
  41. namespace ge {
  42. NodeType TransferNodeType(const NodePtr &node) {
  43. const std::string type = node->GetType();
  44. if (type == ge::DATA) {
  45. if (node->GetOwnerComputeGraph()->GetParentNode() == nullptr) {
  46. GELOGD("access src data node:%s", node->GetName().c_str());
  47. return kOthers;
  48. }
  49. GELOGD("access subgraph input node:%s", node->GetName().c_str());
  50. return kSubgraphData;
  51. } else if (type == PARTITIONEDCALL) {
  52. GELOGD("access subgraph node:%s", node->GetName().c_str());
  53. return kSubgraphNode;
  54. }
  55. GELOGD("access other node:%s", node->GetName().c_str());
  56. return kOthers;
  57. }
  58. Status HandleSubgraphNode(NodePtr &src_node, OutDataAnchorPtr &src_out_anchor) {
  59. auto subgraph = NodeUtils::GetSubgraph(*src_node, 0);
  60. GE_CHECK_NOTNULL(subgraph);
  61. const NodePtr &net_output_node = subgraph->FindFirstNodeMatchType(NETOUTPUT);
  62. GE_CHECK_NOTNULL(net_output_node);
  63. const InDataAnchorPtr &in_data_anchor = net_output_node->GetInDataAnchor(src_out_anchor->GetIdx());
  64. GE_CHECK_NOTNULL(in_data_anchor);
  65. const OutDataAnchorPtr &peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  66. GE_CHECK_NOTNULL(peer_out_anchor);
  67. src_node = peer_out_anchor->GetOwnerNode();
  68. src_out_anchor = peer_out_anchor;
  69. return SUCCESS;
  70. }
  71. Status HandleSubgraphDataNode(NodePtr &src_node, OutDataAnchorPtr &src_out_anchor) {
  72. uint32_t index = 0;
  73. if (!AttrUtils::GetInt(src_node->GetOpDesc(), ATTR_NAME_PARENT_NODE_INDEX, index)) {
  74. REPORT_INNER_ERROR("E19999", "get attr:%s failed from node:%s",
  75. ATTR_NAME_PARENT_NODE_INDEX.c_str(), src_node->GetName().c_str());
  76. GELOGE(FAILED, "[Get][Attr] %s failed, node:%s.", ATTR_NAME_PARENT_NODE_INDEX.c_str(),
  77. src_node->GetName().c_str());
  78. return FAILED;
  79. }
  80. const NodePtr &parent_node = src_node->GetOwnerComputeGraph()->GetParentNode();
  81. GE_CHECK_NOTNULL(parent_node);
  82. const InDataAnchorPtr &in_data_anchor = parent_node->GetInDataAnchor(index);
  83. GE_CHECK_NOTNULL(in_data_anchor);
  84. const OutDataAnchorPtr &peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  85. GE_CHECK_NOTNULL(peer_out_anchor);
  86. src_node = peer_out_anchor->GetOwnerNode();
  87. src_out_anchor = peer_out_anchor;
  88. return SUCCESS;
  89. }
  90. GraphBuilder::GraphBuilder() : build_mode_(BuildMode::GEN_TASK_WITH_FUSION), hcom_parallel_(false) {}
  91. void GraphBuilder::SetOptions(const ge::GraphManagerOptions &options) {
  92. stream_max_parallel_num_ = options.stream_max_parallel_num;
  93. hcom_parallel_ = options.hcom_parallel;
  94. if (options.perf_level == kInvalidPerfLevel) {
  95. build_mode_ = static_cast<int>(BuildMode::GEN_TASK_WITH_FUSION);
  96. } else {
  97. build_mode_ = options.perf_level;
  98. }
  99. }
  100. Status GraphBuilder::CalcOpParam(const ge::ComputeGraphPtr &graph) {
  101. GE_CHECK_NOTNULL(graph);
  102. auto instance_ptr = ge::GELib::GetInstance();
  103. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  104. REPORT_INNER_ERROR("E19999", "check gelib instance null, graph:%s",
  105. graph->GetName().c_str());
  106. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Check][GELib] GraphBuilder: GE is not initialized, graph:%s",
  107. graph->GetName().c_str());
  108. return GE_CLI_GE_NOT_INITIALIZED;
  109. }
  110. for (const auto &node_ptr : graph->GetNodes(graph->GetGraphUnknownFlag())) {
  111. GE_CHECK_NOTNULL(node_ptr->GetOpDesc());
  112. std::string kernel_lib_name = node_ptr->GetOpDesc()->GetOpKernelLibName();
  113. if (kernel_lib_name.empty()) {
  114. // reset op kernel lib
  115. (void)instance_ptr->DNNEngineManagerObj().GetDNNEngineName(node_ptr);
  116. kernel_lib_name = node_ptr->GetOpDesc()->GetOpKernelLibName();
  117. if (kernel_lib_name.empty()) {
  118. REPORT_INNER_ERROR("E19999", "op kernel lib is empty in node:%s(%s)",
  119. node_ptr->GetName().c_str(), node_ptr->GetType().c_str());
  120. GELOGE(INTERNAL_ERROR, "[Get][KernelLibName] of node:%s(%s) failed.", node_ptr->GetName().c_str(),
  121. node_ptr->GetType().c_str());
  122. return INTERNAL_ERROR;
  123. }
  124. }
  125. auto ret = SetInputSize(node_ptr);
  126. if (ret != SUCCESS) {
  127. REPORT_CALL_ERROR("E19999", "Set node:%s(%s) inputDesc size failed",
  128. node_ptr->GetName().c_str(), node_ptr->GetType().c_str());
  129. GELOGE(ret, "[Set][InputSize] to node:%s failed.", node_ptr->GetName().c_str());
  130. return ret;
  131. }
  132. ret = OpsKernelBuilderManager::Instance().CalcOpRunningParam(*node_ptr);
  133. if (ret != SUCCESS) {
  134. REPORT_CALL_ERROR("E19999", "Call Calculate op:%s(%s) running param failed",
  135. node_ptr->GetName().c_str(), node_ptr->GetType().c_str());
  136. GELOGE(ret, "[Call][Calculate] op running param failed, node name is %s", node_ptr->GetName().c_str());
  137. return ret;
  138. }
  139. GE_CHK_STATUS_RET(AddOutputMemTypeForNode(node_ptr));
  140. }
  141. auto parent_node = graph->GetParentNode();
  142. if (parent_node == nullptr) {
  143. return SUCCESS;
  144. }
  145. GE_CHK_STATUS_RET(UpdateParentNodeOutputSize(graph, parent_node));
  146. GELOGI("Success to calculate op running param.");
  147. return SUCCESS;
  148. }
  149. Status GraphBuilder::UpdateParentNodeOutputSize(const ge::ComputeGraphPtr &graph, ge::NodePtr &parent_node_ptr) {
  150. GELOGI("Begin to update parent node[%s] of graph[%s] output size.", parent_node_ptr->GetName().c_str(),
  151. graph->GetName().c_str());
  152. auto parent_op_desc = parent_node_ptr->GetOpDesc();
  153. GE_CHECK_NOTNULL(parent_op_desc);
  154. bool is_unknown_shape = graph->GetGraphUnknownFlag();
  155. if (is_unknown_shape) {
  156. GELOGI("Current graph[%s] is unknown, no need to update parent node[%s] output size.", graph->GetName().c_str(),
  157. parent_node_ptr->GetName().c_str());
  158. return SUCCESS;
  159. }
  160. for (const auto &node_ptr : graph->GetDirectNode()) {
  161. if (node_ptr->GetType() != NETOUTPUT) {
  162. continue;
  163. }
  164. auto op_desc = node_ptr->GetOpDesc();
  165. GE_CHECK_NOTNULL(op_desc);
  166. for (const auto &in_data_anchor : node_ptr->GetAllInDataAnchors()) {
  167. auto index = in_data_anchor->GetIdx();
  168. ge::GeTensorDesc desc_temp = op_desc->GetInputDesc(index);
  169. uint32_t parent_index = 0;
  170. if (!AttrUtils::GetInt(desc_temp, ATTR_NAME_PARENT_NODE_INDEX, parent_index)) {
  171. GELOGI("NetOutput input tensor %d, attr %s not found.", index, ATTR_NAME_PARENT_NODE_INDEX.c_str());
  172. continue;
  173. }
  174. int64_t size = 0;
  175. GE_IF_BOOL_EXEC(ge::TensorUtils::GetSize(desc_temp, size) != SUCCESS, GELOGI("Get size failed!"));
  176. ge::GeTensorDesc parent_desc_temp = parent_op_desc->GetOutputDesc(parent_index);
  177. ge::TensorUtils::SetSize(parent_desc_temp, size);
  178. GE_CHK_STATUS_RET(parent_op_desc->UpdateOutputDesc(parent_index, parent_desc_temp));
  179. GELOGI("Update parent node[%s] output index[%u] to size[%ld].", parent_node_ptr->GetName().c_str(), parent_index,
  180. size);
  181. }
  182. }
  183. return SUCCESS;
  184. }
  185. Status GraphBuilder::Build(ComputeGraphPtr &comp_graph, GeRootModelPtr &ge_root_model_ptr, uint64_t session_id) {
  186. if (comp_graph == nullptr) {
  187. REPORT_INNER_ERROR("E19999", "check compute_graph nullptr, session_id:%lu", session_id);
  188. GELOGE(GE_GRAPH_PARAM_NULLPTR, "[Check][Param] comp_graph is null, session_id:%lu", session_id);
  189. return GE_GRAPH_PARAM_NULLPTR;
  190. }
  191. ge_root_model_ptr = MakeShared<ge::GeRootModel>(comp_graph);
  192. if (ge_root_model_ptr == nullptr) {
  193. return MEMALLOC_FAILED;
  194. }
  195. GeModelPtr ge_model_ptr = nullptr;
  196. bool is_dynamic_shape = false;
  197. // To be compatible with the old process, do not verify the return value temporarily.
  198. (void)AttrUtils::GetBool(comp_graph, ATTR_NAME_DYNAMIC_SHAPE_PARTITIONED, is_dynamic_shape);
  199. if (is_dynamic_shape || comp_graph->GetGraphUnknownFlag()) {
  200. GE_CHK_STATUS_RET(
  201. BuildForDynamicShapeGraph(comp_graph, ge_root_model_ptr, ge_model_ptr, session_id),
  202. "[Build][DynamicShapeGraph] failed, graph:%s, session id:%lu.", comp_graph->GetName().c_str(), session_id);
  203. return SUCCESS;
  204. }
  205. GE_CHK_STATUS_RET(BuildForKnownShapeGraph(comp_graph, ge_model_ptr, session_id),
  206. "[Build][KnownShapeGraph] failed, graph:%s, session id:%lu.",
  207. comp_graph->GetName().c_str(), session_id);
  208. ge_root_model_ptr->SetSubgraphInstanceNameToModel(comp_graph->GetName(), ge_model_ptr);
  209. return SUCCESS;
  210. }
  211. Status GraphBuilder::BuildForKnownShapeGraph(ComputeGraphPtr &comp_graph,
  212. GeModelPtr &ge_model_ptr, uint64_t session_id) {
  213. if (ge::GetContext().GetHostExecFlag()) {
  214. GE_CHK_STATUS_RET(BuildForHostCpuGraph(comp_graph, ge_model_ptr, session_id),
  215. "[Build][HostCpuGraph] failed, graph:%s, session id:%lu.",
  216. comp_graph->GetName().c_str(), session_id);
  217. return SUCCESS;
  218. }
  219. ErrorManager::GetInstance().SetStage(error_message::kModelCompile, error_message::kPreBuild);
  220. GELOGI("Begin to build known shape graph[%s].", comp_graph->GetName().c_str());
  221. Status ret = SecondPartition(comp_graph);
  222. GE_CHK_STATUS_RET(ret, "[Call][SecondPartition] for Graph[%s] failed.", comp_graph->GetName().c_str());
  223. auto subgraph_map = graph_partitioner_.GetSubGraphMap();
  224. GE_TIMESTAMP_START(BuildSubgraph);
  225. ge::ModelBuilder builder(session_id, comp_graph, subgraph_map, stream_max_parallel_num_, hcom_parallel_, build_mode_);
  226. GE_DUMP(comp_graph, "BeforePreBuildModel");
  227. GE_TIMESTAMP_START(PreBuildModel);
  228. GE_CHK_STATUS_RET(builder.PreBuildModel(), "[PreBuild][Model] failed, Graph[%s].",
  229. comp_graph->GetName().c_str());
  230. GE_TIMESTAMP_END(PreBuildModel, "GraphBuilder::PreBuildModel");
  231. GE_DUMP(comp_graph, "AfterPreBuildModel");
  232. GE_TIMESTAMP_START(CalcOpParam);
  233. GE_CHK_STATUS_RET(CalcOpParam(comp_graph), "[Calc][OpParam] fail, Graph[%s].", comp_graph->GetName().c_str());
  234. GE_TIMESTAMP_END(CalcOpParam, "GraphBuilder::CalcOpParam");
  235. GE_DUMP(comp_graph, "AfterCalcOpParam");
  236. ModelPtr model_ptr = MakeShared<ge::Model>();
  237. if (model_ptr == nullptr) {
  238. return MEMALLOC_FAILED;
  239. }
  240. GE_TIMESTAMP_START(BuildModelForGetTask);
  241. GE_CHK_STATUS_RET(builder.BuildModelForGetTask(*model_ptr), "[Build][Model] ForGetTask fail, Graph[%s].",
  242. comp_graph->GetName().c_str());
  243. GE_TIMESTAMP_END(BuildModelForGetTask, "GraphBuilder::BuildModelForGetTask");
  244. GE_DUMP(comp_graph, "AfterBuildModel");
  245. ErrorManager::GetInstance().SetStage(error_message::kModelCompile, error_message::kTaskGenerate);
  246. GE_TIMESTAMP_START(GetTaskInfo);
  247. ret = GetTaskInfo(builder, model_ptr, comp_graph, subgraph_map, session_id);
  248. GE_TIMESTAMP_END(GetTaskInfo, "GraphBuilder::GetTaskInfo");
  249. GE_DUMP(comp_graph, "AfterGetTask");
  250. if (ret != SUCCESS) {
  251. GELOGE(ret, "[Get][TaskInfo] fail, Graph[%s].", comp_graph->GetName().c_str());
  252. return ret;
  253. }
  254. ErrorManager::GetInstance().SetStage(error_message::kModelCompile, error_message::kOther);
  255. ge_model_ptr = MakeShared<ge::GeModel>();
  256. if (ge_model_ptr == nullptr) {
  257. return MEMALLOC_FAILED;
  258. }
  259. GE_CHK_STATUS_RET(builder.SaveDataToModel(*model_ptr, *ge_model_ptr),
  260. "[Save][Data] ToModel fail, Graph[%s].", comp_graph->GetName().c_str());
  261. GELOGD("Success to build graph[%s] model.", comp_graph->GetName().c_str());
  262. GE_TIMESTAMP_END(BuildSubgraph, "GraphBuilder::Build");
  263. return SUCCESS;
  264. }
  265. Status GraphBuilder::SetConstantInputOffset(ComputeGraphPtr &comp_graph) {
  266. for (auto &node : comp_graph->GetDirectNode()) {
  267. GE_CHECK_NOTNULL(node);
  268. auto op_desc = node->GetOpDesc();
  269. GE_CHECK_NOTNULL(op_desc);
  270. auto num_inputs = op_desc->GetInputsSize();
  271. std::vector<int64_t> input_offsets(num_inputs, 0);
  272. int valid_input_index = -1;
  273. for (uint32_t i = 0; i < node->GetAllInDataAnchorsSize(); ++i) {
  274. auto in_anchor = node->GetInDataAnchor(i);
  275. auto peer_out_anchor = in_anchor->GetPeerOutAnchor();
  276. if (peer_out_anchor == nullptr) {
  277. continue;
  278. }
  279. ++valid_input_index;
  280. auto peer_node = peer_out_anchor->GetOwnerNode();
  281. if (peer_node == nullptr) {
  282. continue;
  283. }
  284. if (peer_node->GetType() != CONSTANT) {
  285. continue;
  286. }
  287. std::vector<GeTensorPtr> weights = OpDescUtils::MutableWeights(peer_node);
  288. if (weights.empty()) {
  289. REPORT_INNER_ERROR("E19999", "check weights size of node %s(%s) is empty",
  290. node->GetName().c_str(), node->GetType().c_str());
  291. GELOGE(FAILED, "[Check][Param] weights size of node %s is empty", node->GetName().c_str());
  292. return FAILED;
  293. }
  294. GeTensorPtr weight = weights[0];
  295. GE_CHECK_NOTNULL(weight);
  296. int64_t input_offset = 0;
  297. (void) TensorUtils::GetDataOffset(weight->MutableTensorDesc(), input_offset);
  298. // valid_input_index must smaller than num_inputs
  299. input_offsets[valid_input_index] = input_offset;
  300. GELOGD("[%s] input[%u] is const, offset = %ld", node->GetName().c_str(), valid_input_index, input_offset);
  301. }
  302. op_desc->SetInputOffset(input_offsets);
  303. std::vector<int64_t> output_offsets(op_desc->GetOutputsSize(), 0);
  304. op_desc->SetOutputOffset(output_offsets);
  305. }
  306. return SUCCESS;
  307. }
  308. Status GraphBuilder::BuildForUnknownShapeGraph(ComputeGraphPtr &comp_graph, GeModelPtr &ge_model_ptr,
  309. uint64_t session_id) {
  310. ErrorManager::GetInstance().SetStage(error_message::kModelCompile, error_message::kPreBuild);
  311. GELOGI("Begin to build unknown shape graph[%s].", comp_graph->GetName().c_str());
  312. Graph2SubGraphInfoList subgraph_map;
  313. ge::ModelBuilder builder(session_id, comp_graph, subgraph_map, stream_max_parallel_num_, hcom_parallel_, build_mode_);
  314. GE_DUMP(comp_graph, "BeforePreBuildModel");
  315. GE_TIMESTAMP_START(PreBuildModel);
  316. GE_CHK_STATUS_RET(builder.PreBuildModel(), "[PreBuild][Model] fail, Graph[%s].", comp_graph->GetName().c_str());
  317. GE_TIMESTAMP_END(PreBuildModel, "GraphBuilder::PreBuildModel");
  318. GE_DUMP(comp_graph, "AfterPreBuildModel");
  319. GE_TIMESTAMP_START(CalcOpParam);
  320. GE_CHK_STATUS_RET(CalcOpParam(comp_graph), "[Calc][OpParam] fail, Graph[%s].", comp_graph->GetName().c_str());
  321. GE_TIMESTAMP_END(CalcOpParam, "GraphBuilder::CalcOpParam");
  322. GE_DUMP(comp_graph, "AfterCalcOpParam");
  323. GE_TIMESTAMP_START(SetConstantInputOffset);
  324. GE_CHK_STATUS_RET(SetConstantInputOffset(comp_graph),
  325. "[Set][Offset] Graph[%s] failed to set constant input offset.", comp_graph->GetName().c_str());
  326. GE_TIMESTAMP_END(SetConstantInputOffset, "GraphBuilder::SetConstantInputOffset");
  327. GE_TIMESTAMP_START(MergeWeights);
  328. GE_CHK_STATUS_RET(builder.MergeWeights(), "[Merge][Weights] failed for Graph[%s].", comp_graph->GetName().c_str());
  329. GE_TIMESTAMP_END(MergeWeights, "GraphBuilder::MergeWeights");
  330. ModelPtr model_ptr = MakeShared<ge::Model>();
  331. if (model_ptr == nullptr) {
  332. return MEMALLOC_FAILED;
  333. }
  334. GE_TIMESTAMP_START(BuildModelForGetDynShapeTask);
  335. GE_CHK_STATUS_RET(builder.BuildModelForGetDynShapeTask(*model_ptr),
  336. "[Build][Model] ForGetDynShapeTask fail, Graph[%s].", comp_graph->GetName().c_str());
  337. GE_TIMESTAMP_END(BuildModelForGetDynShapeTask, "GraphBuilder::BuildModelForGetDynShapeTask");
  338. ErrorManager::GetInstance().SetStage(error_message::kModelCompile, error_message::kTaskGenerate);
  339. GE_TIMESTAMP_START(GetTaskInfo);
  340. Status ret = GetTaskInfo(builder, model_ptr, comp_graph, subgraph_map, session_id);
  341. GE_TIMESTAMP_END(GetTaskInfo, "GraphBuilder::GetTaskInfo");
  342. ErrorManager::GetInstance().SetStage(error_message::kModelCompile, error_message::kOther);
  343. GraphUtils::DumpGEGraph(comp_graph, "AfterGetTask");
  344. GraphUtils::DumpGEGraphToOnnx(*comp_graph, "AfterGetTask");
  345. if (ret != SUCCESS) {
  346. GELOGE(ret, "[Get][TaskInfo] fail, Graph[%s].", comp_graph->GetName().c_str());
  347. return ret;
  348. }
  349. ge_model_ptr = MakeShared<ge::GeModel>();
  350. if (ge_model_ptr == nullptr) {
  351. return MEMALLOC_FAILED;
  352. }
  353. GE_CHK_STATUS_RET(builder.SaveDataToModel(*model_ptr, *ge_model_ptr),
  354. "[Save][Data] ToModel fail, Graph[%s].", comp_graph->GetName().c_str());
  355. GELOGD("Success to build graph[%s] model.", comp_graph->GetName().c_str());
  356. return SUCCESS;
  357. }
  358. Status GraphBuilder::BuildForHostCpuGraph(ComputeGraphPtr &comp_graph, GeModelPtr &ge_model_ptr, uint64_t session_id) {
  359. return BuildForUnknownShapeGraph(comp_graph, ge_model_ptr, session_id);
  360. }
  361. Status GraphBuilder::MarkFpBpProfilingTaskAttr(ComputeGraphPtr &com_graph) {
  362. bool original_unknown_shape_flag = com_graph->GetGraphUnknownFlag();
  363. com_graph->SetGraphUnknownFlag(false);
  364. GELOGD("Start to mark profiling task attr for fp and bp.");
  365. TaskGenerator task_generator;
  366. ProfilingPoint profiling_point;
  367. std::vector<uint32_t> all_reduce_node_index;
  368. Status ret = task_generator.FindProfilingNodeIndex(com_graph, profiling_point, all_reduce_node_index);
  369. com_graph->SetGraphUnknownFlag(original_unknown_shape_flag);
  370. if (ret != SUCCESS) {
  371. GELOGW("Find profiling node index failed.");
  372. }
  373. if (profiling_point.fp_index == 0 || profiling_point.bp_index == 0 || profiling_point.end_index.empty()) {
  374. GELOGD("No need to mark fp bp profiling task attr.");
  375. return SUCCESS;
  376. }
  377. // mark profiling task attr for node
  378. uint32_t node_index = 0;
  379. for (const auto &node : com_graph->GetAllNodes()) {
  380. OpDescPtr op_desc = node->GetOpDesc();
  381. GE_CHECK_NOTNULL(node->GetOpDesc());
  382. node_index++;
  383. if (profiling_point.fp_index == node_index) {
  384. GELOGI("The first fp node of dynamic graph is %s, idx %u", op_desc->GetName().c_str(), node_index);
  385. (void)ge::AttrUtils::SetBool(op_desc, ATTR_NAME_INSERT_FP_PROFILILNG_TASK, true);
  386. }
  387. if (profiling_point.bp_index == node_index) {
  388. GELOGI("The bp node of dynamic graph is %s, idx %u", op_desc->GetName().c_str(), node_index);
  389. (void)ge::AttrUtils::SetBool(op_desc, ATTR_NAME_INSERT_BP_PROFILILNG_TASK, true);
  390. }
  391. for (size_t i = 0; i < all_reduce_node_index.size(); i++) {
  392. if (all_reduce_node_index[i] == node_index) {
  393. GELOGI("The all reduce node of dynamic graph is %s, idx %u", op_desc->GetName().c_str(), node_index);
  394. (void)ge::AttrUtils::SetBool(op_desc, ATTR_NAME_INSERT_BP_PROFILILNG_TASK, true);
  395. GE_IF_BOOL_EXEC(TypeUtils::CheckUint64MulOverflow(i, kProfilingArStep),
  396. REPORT_INNER_ERROR("E19999", "Multiply result is out of range when calc profiling ar log id "
  397. "for node:%s(%s)", op_desc->GetName().c_str(), op_desc->GetType().c_str());
  398. GELOGE(FAILED, "[Check][Param] Multiply result is out of range, node:%s(%s)",
  399. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  400. return FAILED);
  401. int64_t log_id = i * kProfilingArStep + kProfilingArStartLogid;
  402. (void)ge::AttrUtils::SetInt(op_desc, ATTR_NAME_INSERT_PROFILILNG_TASK_LOG_ID, log_id);
  403. continue;
  404. }
  405. }
  406. if (profiling_point.end_index.find(node_index) != profiling_point.end_index.end()) {
  407. GELOGI("The end node of dynamic graph is %s, idx %u", op_desc->GetName().c_str(), node_index);
  408. (void)ge::AttrUtils::SetBool(op_desc, ATTR_NAME_INSERT_END_PROFILILNG_TASK, true);
  409. }
  410. }
  411. return SUCCESS;
  412. }
  413. Status GraphBuilder::BuildForDynamicShapeGraph(ComputeGraphPtr &comp_graph,
  414. GeRootModelPtr &ge_root_model_ptr, GeModelPtr &ge_model_ptr,
  415. uint64_t session_id) {
  416. GELOGI("Start to build BuildForDynamicShape for dynamic shape.");
  417. // Update Root Graph Data size
  418. for (auto &node : comp_graph->GetDirectNode()) {
  419. auto op_desc = node->GetOpDesc();
  420. GE_CHECK_NOTNULL(op_desc);
  421. op_desc->SetStreamId(kInvalidStream);
  422. if (node->GetType() == DATA) {
  423. GE_CHK_STATUS_RET(CalcDynShapeRootGraphDataSize(op_desc), "[Calc][DynShapeRootGraphDataSize] failed, op:%s.",
  424. op_desc->GetName().c_str());
  425. }
  426. }
  427. // Set fp bp profiling task attr for graph
  428. if (MarkFpBpProfilingTaskAttr(comp_graph) != SUCCESS) {
  429. GELOGE(FAILED, "[Mark][TaskAttr]Set fp bp profiling task attr for graph:%s failed.", comp_graph->GetName().c_str());
  430. return FAILED;
  431. }
  432. auto all_graphs = comp_graph->GetAllSubgraphs();
  433. if (all_graphs.empty()) {
  434. all_graphs.push_back(comp_graph);
  435. }
  436. for (auto &sub_graph : all_graphs) {
  437. // exclude functional subgraph in known subgraph
  438. if (sub_graph->GetParentGraph() != nullptr && sub_graph->GetParentGraph() != comp_graph &&
  439. !sub_graph->GetParentGraph()->GetGraphUnknownFlag()) {
  440. continue;
  441. }
  442. if (sub_graph->GetGraphUnknownFlag()) {
  443. // unknown shape build flow
  444. GE_CHK_STATUS_RET(BuildForUnknownShapeGraph(sub_graph, ge_model_ptr, session_id),
  445. "[Build][Graph] as unknown shape failed, session id:%lu.", session_id);
  446. } else {
  447. // reset functional subgraph parent graph as known subgraph
  448. for (const auto &node : sub_graph->GetDirectNode()) {
  449. for (const auto &sub_graph_name : node->GetOpDesc()->GetSubgraphInstanceNames()) {
  450. auto sub_sub_graph = comp_graph->GetSubgraph(sub_graph_name);
  451. GE_CHK_STATUS_RET(sub_graph->AddSubgraph(sub_sub_graph),
  452. "[Add][SubGraph] %s to known graph:%s failed.", sub_sub_graph->GetName().c_str(),
  453. sub_graph->GetName().c_str());
  454. }
  455. }
  456. // known shape build flow
  457. GE_CHK_STATUS_RET(BuildForKnownShapeGraph(sub_graph, ge_model_ptr, session_id),
  458. "[Build][Graph] for known shape failed, session id:%lu.", session_id);
  459. }
  460. ge_root_model_ptr->SetSubgraphInstanceNameToModel(sub_graph->GetName(), ge_model_ptr);
  461. }
  462. return SUCCESS;
  463. }
  464. Status GraphBuilder::GetTaskInfo(const ge::ModelBuilder &builder, const ModelPtr &model_ptr,
  465. ComputeGraphPtr &comp_graph, Graph2SubGraphInfoList &subgraph_map,
  466. uint64_t session_id) {
  467. GE_CHECK_NOTNULL(model_ptr);
  468. GE_CHECK_NOTNULL(comp_graph);
  469. int64_t memory_size = 0;
  470. if (!AttrUtils::GetInt(model_ptr, ATTR_MODEL_MEMORY_SIZE, memory_size)) {
  471. REPORT_INNER_ERROR("E19999", "Get Attr:%s fail in model", ATTR_MODEL_MEMORY_SIZE.c_str());
  472. GELOGE(INTERNAL_ERROR, "[Get][Attr] memory size fail, graph:%s, session id:%lu.", comp_graph->GetName().c_str(),
  473. session_id);
  474. return INTERNAL_ERROR;
  475. }
  476. int64_t p2p_memory_size = 0;
  477. if (!AttrUtils::GetInt(model_ptr, ATTR_MODEL_P2P_MEMORY_SIZE, p2p_memory_size)) {
  478. REPORT_INNER_ERROR("E19999", "Get Attr:%s fail in model", ATTR_MODEL_P2P_MEMORY_SIZE.c_str());
  479. GELOGE(INTERNAL_ERROR, "[Get][Attr] %s fail in model", ATTR_MODEL_P2P_MEMORY_SIZE.c_str());
  480. return INTERNAL_ERROR;
  481. }
  482. int64_t weight_size = 0;
  483. if (!AttrUtils::GetInt(model_ptr, ATTR_MODEL_WEIGHT_SIZE, weight_size)) {
  484. REPORT_INNER_ERROR("E19999", "Get Attr:%s fail in model", ATTR_MODEL_WEIGHT_SIZE.c_str());
  485. GELOGE(INTERNAL_ERROR, "[Get][Attr] %s fail in model", ATTR_MODEL_WEIGHT_SIZE.c_str());
  486. return INTERNAL_ERROR;
  487. }
  488. auto var_manager = VarManager::Instance(session_id);
  489. // since var_mem_logic_base_ = graph_mem_max_size_ + kGraphMemoryBuffer in graph_var_manager.cc,
  490. // get_mem_base should not bigger than kGraphMemoryBuffer
  491. auto *get_mem_base = reinterpret_cast<uint8_t *>(reinterpret_cast<uintptr_t>(kGraphMemoryBuffer>>1));
  492. uint8_t *get_weight_mem_base = get_mem_base;
  493. if (weight_size > 0) {
  494. get_weight_mem_base = get_mem_base + memory_size + p2p_memory_size;
  495. }
  496. std::map<int64_t, uint8_t *> mem_type_to_data_mem_base;
  497. mem_type_to_data_mem_base[RT_MEMORY_HBM] = get_mem_base;
  498. if (p2p_memory_size == 0) {
  499. mem_type_to_data_mem_base[RT_MEMORY_P2P_DDR] = nullptr;
  500. } else {
  501. mem_type_to_data_mem_base[RT_MEMORY_P2P_DDR] = get_mem_base + memory_size;
  502. }
  503. std::map<int64_t, uint64_t> mem_type_to_data_mem_size;
  504. mem_type_to_data_mem_size[RT_MEMORY_HBM] = memory_size;
  505. mem_type_to_data_mem_size[RT_MEMORY_P2P_DDR] = p2p_memory_size;
  506. RunContextUtil run_context;
  507. Status ret = run_context.InitMemInfo(get_mem_base, memory_size, mem_type_to_data_mem_base, mem_type_to_data_mem_size,
  508. get_weight_mem_base, weight_size);
  509. if (ret != SUCCESS) {
  510. GELOGE(ret, "[Init][MemInfo] fail, ret:%d.", ret);
  511. return ret;
  512. }
  513. auto weight_buffer = builder.GetWeightBuffer();
  514. ret = run_context.CreateRunContext(*model_ptr, comp_graph, weight_buffer, session_id);
  515. if (ret != SUCCESS) {
  516. GELOGE(ret, "[Create][RunContext] fail, ret:%d, graph:%s.", ret, comp_graph->GetName().c_str());
  517. return ret;
  518. }
  519. StreamGraphOptimizer stream_optimizer;
  520. ret = stream_optimizer.OptimizeStreamedSubGraph(comp_graph, subgraph_map, run_context.GetRunContext());
  521. if (ret != SUCCESS) {
  522. GELOGE(ret, "[Optimize][StreamedSubGraph] fail, graph:%s.", comp_graph->GetName().c_str());
  523. return ret;
  524. }
  525. GE_DUMP(comp_graph, "AfterOptimizeStreamedSubGraph");
  526. auto *get_var_mem_base = reinterpret_cast<uint8_t *>(reinterpret_cast<uintptr_t>(var_manager->GetVarMemLogicBase()));
  527. uint64_t var_size = (var_manager->GetVarMemSize(RT_MEMORY_HBM) > 0) ? var_manager->GetVarMemMaxSize() : 0;
  528. TaskGenerator task_generator(get_var_mem_base, var_size);
  529. ret = task_generator.GetTaskInfo(*model_ptr, comp_graph, session_id, run_context.GetRunContext());
  530. return ret;
  531. }
  532. Status GraphBuilder::SetInputSize(const ge::NodePtr &node_ptr) {
  533. // Set the size of input_desc to 'src_node.output_desc.size'
  534. if (node_ptr->GetType() == DATA) {
  535. bool is_unknown_shape = false;
  536. GE_CHK_STATUS_RET(ge::NodeUtils::GetNodeUnknownShapeStatus(*node_ptr, is_unknown_shape),
  537. "[Get][Status] of data node[%s] shape failed!", node_ptr->GetName().c_str());
  538. if (is_unknown_shape) {
  539. GELOGD("data node: %s is unknown shape, do not set input size!", node_ptr->GetName().c_str());
  540. return SUCCESS;
  541. }
  542. if (UpdateDataInputSize(node_ptr) != SUCCESS) {
  543. GELOGE(FAILED, "[Update][Data] input size failed, node:%s.", node_ptr->GetName().c_str());
  544. return FAILED;
  545. }
  546. }
  547. for (const auto &in_data_anchor : node_ptr->GetAllInDataAnchors()) {
  548. const auto &peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  549. GE_IF_BOOL_EXEC(peer_out_anchor == nullptr, continue);
  550. const auto &src_node = peer_out_anchor->GetOwnerNode();
  551. const auto &src_op = src_node->GetOpDesc();
  552. GE_IF_BOOL_EXEC(src_op == nullptr, continue);
  553. auto node_op_desc = node_ptr->GetOpDesc();
  554. GE_IF_BOOL_EXEC(node_op_desc == nullptr, continue);
  555. // Set the input_desc of dst_node to 'src_node.output_desc'
  556. auto output_desc = src_op->GetOutputDescPtr(peer_out_anchor->GetIdx());
  557. int64_t size = 0;
  558. GE_IF_BOOL_EXEC(ge::TensorUtils::GetSize(*output_desc, size) != SUCCESS, GELOGI("Get size failed!"));
  559. GELOGD("src node %s output desc, dim_size: %zu, mem_size: %ld, format: %s, type: %s.", src_node->GetName().c_str(),
  560. output_desc->GetShape().GetDimNum(), size, TypeUtils::FormatToSerialString(output_desc->GetFormat()).c_str(),
  561. TypeUtils::DataTypeToSerialString(output_desc->GetDataType()).c_str());
  562. for (size_t i = 0; i < output_desc->GetShape().GetDimNum(); ++i) {
  563. GELOGD("dims[%zu]: %ld", i, output_desc->GetShape().GetDim(i));
  564. }
  565. auto input_desc = node_op_desc->MutableInputDesc(in_data_anchor->GetIdx());
  566. GE_CHECK_NOTNULL(input_desc);
  567. (void) ge::TensorUtils::SetSize(*input_desc, size);
  568. GELOGD("%s input desc, dim_size: %zu, mem_size: %ld, format: %s, type: %s.", node_ptr->GetName().c_str(),
  569. input_desc->GetShape().GetDimNum(), size, TypeUtils::FormatToSerialString(input_desc->GetFormat()).c_str(),
  570. TypeUtils::DataTypeToSerialString(input_desc->GetDataType()).c_str());
  571. // inherit some attr
  572. int64_t tensor_size_attr;
  573. if (AttrUtils::GetInt(output_desc, ATTR_NAME_SPECIAL_OUTPUT_SIZE, tensor_size_attr) && (tensor_size_attr > 0)) {
  574. GE_IF_BOOL_EXEC(!AttrUtils::SetInt(*input_desc, ATTR_NAME_SPECIAL_OUTPUT_SIZE, tensor_size_attr),
  575. GELOGW("Set size attr failed!"); continue);
  576. GELOGD("node[%s] [%d]th output has sepcial size[%ld], and update to node[%s] [%d]th input",
  577. src_op->GetName().c_str(), peer_out_anchor->GetIdx(), tensor_size_attr,
  578. node_op_desc->GetName().c_str(), in_data_anchor->GetIdx());
  579. }
  580. }
  581. return SUCCESS;
  582. }
  583. Status GraphBuilder::UpdateDataInputSize(const ge::NodePtr &node_ptr) {
  584. const auto &op_desc = node_ptr->GetOpDesc();
  585. if (op_desc == nullptr) {
  586. REPORT_INNER_ERROR("E19999", "check op_desc is nullptr");
  587. GELOGE(FAILED, "[Check][Param] Op desc is nullptr.");
  588. return FAILED;
  589. }
  590. // data op only has one output anchor
  591. ge::GeTensorDesc output_desc = op_desc->GetOutputDesc(0);
  592. int64_t output_size = 0;
  593. if (ge::TensorUtils::GetSize(output_desc, output_size) != SUCCESS) {
  594. GELOGW("Get size failed!");
  595. }
  596. if (output_size > 0) {
  597. GELOGI("No need to update data input size.");
  598. return SUCCESS;
  599. } else {
  600. int64_t real_dim_size = 0;
  601. ge::graphStatus graph_status = TensorUtils::GetTensorSizeInBytes(output_desc, real_dim_size);
  602. if (graph_status != GRAPH_SUCCESS) {
  603. REPORT_CALL_ERROR("E19999", "Get tensor size in bytes failed for op:%s(%s) index:0",
  604. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  605. GELOGE(FAILED, "[Get][TensorSize] in bytes failed, op:%s.", op_desc->GetName().c_str());
  606. return FAILED;
  607. }
  608. // data op only has one input anchor
  609. ge::GeTensorDesc input_desc = op_desc->GetInputDesc(0);
  610. ge::TensorUtils::SetSize(input_desc, real_dim_size);
  611. if (op_desc->UpdateInputDesc(0, input_desc) != GRAPH_SUCCESS) {
  612. REPORT_CALL_ERROR("E19999", "Update input desc size failed for op:%s(%s) index:0",
  613. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  614. GELOGE(FAILED, "[Update][InputDesc] failed, op:%s.", op_desc->GetName().c_str());
  615. return FAILED;
  616. }
  617. }
  618. return SUCCESS;
  619. }
  620. Status GraphBuilder::CalcDynShapeRootGraphDataSize(const ge::OpDescPtr &op_desc) {
  621. GELOGI("Begin to calc dynamic shape graph data[%s] size.", op_desc->GetName().c_str());
  622. // data op only has one output anchor
  623. ge::GeTensorDesc output_desc = op_desc->GetOutputDesc(0);
  624. if (output_desc.MutableShape().IsUnknownShape()) {
  625. GELOGI("No need to update dynamic shape graph data output size for unknown shape data.");
  626. return SUCCESS;
  627. }
  628. int64_t output_size = 0;
  629. if (ge::TensorUtils::GetSize(output_desc, output_size) != SUCCESS) {
  630. GELOGW("Get size failed!");
  631. }
  632. if (output_size > 0) {
  633. GELOGI("No need to update dynamic shape graph data output size[%ld].", output_size);
  634. return SUCCESS;
  635. } else {
  636. int64_t real_dim_size = 0;
  637. ge::graphStatus graph_status = TensorUtils::GetTensorSizeInBytes(output_desc, real_dim_size);
  638. if (graph_status != GRAPH_SUCCESS) {
  639. REPORT_CALL_ERROR("E19999", "Get tensor size in bytes failed for op:%s(%s) index:0 ",
  640. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  641. GELOGE(FAILED, "[Get][TensorSize] in bytes failed, op:%s.", op_desc->GetName().c_str());
  642. return FAILED;
  643. }
  644. ge::TensorUtils::SetSize(output_desc, real_dim_size);
  645. GELOGI("Update dynamic shape graph data output size to [%ld].", real_dim_size);
  646. if (op_desc->UpdateOutputDesc(0, output_desc) != GRAPH_SUCCESS) {
  647. REPORT_CALL_ERROR("E19999", "Update output desc size failed for op:%s(%s) index:0 ",
  648. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  649. GELOGE(FAILED, "[Update][OutputDesc] for dynamic shape graph data failed, op:%s.", op_desc->GetName().c_str());
  650. return FAILED;
  651. }
  652. }
  653. return SUCCESS;
  654. }
  655. Status GraphBuilder::SecondPartition(ge::ComputeGraphPtr &comp_graph) {
  656. GE_TIMESTAMP_START(GraphPartition2);
  657. auto ret = graph_partitioner_.Partition(comp_graph, GraphPartitioner::kSecondPartitioning);
  658. if (ret != SUCCESS) {
  659. GELOGE(ret, "[Call][Partition] for Graph Failed");
  660. return ret;
  661. }
  662. const auto &graph_2_subgraphlist = graph_partitioner_.GetSubGraphMap();
  663. if (graph_2_subgraphlist.find(comp_graph) == graph_2_subgraphlist.end()) {
  664. REPORT_INNER_ERROR("E19999", "find subgraphlis in graph:%s failed", comp_graph->GetName().c_str());
  665. GELOGE(FAILED, "[Check][Param] Find subgraph graph:%s failed.", comp_graph->GetName().c_str());
  666. return FAILED;
  667. }
  668. GE_TIMESTAMP_END(GraphPartition2, "GraphPartitioner::Partition2");
  669. return ret;
  670. }
  671. Status GraphBuilder::AddOutputMemTypeForNode(const NodePtr &node) {
  672. auto op_desc = node->GetOpDesc();
  673. GE_CHECK_NOTNULL(op_desc);
  674. uint32_t mem_type;
  675. if (!AttrUtils::GetInt(op_desc, ATTR_INPUT_MEMORY_TYPE, mem_type)) {
  676. return SUCCESS;
  677. }
  678. GELOGD("[%s] has attr input_memory_type %u", op_desc->GetName().c_str(), mem_type);
  679. for (const auto &in_data_anchor : node->GetAllInDataAnchors()) {
  680. const auto &peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  681. GE_IF_BOOL_EXEC(peer_out_anchor == nullptr, continue);
  682. bool valid_flag = false;
  683. auto src_node = peer_out_anchor->GetOwnerNode();
  684. auto src_out_anchor = peer_out_anchor;
  685. while (true) {
  686. const auto &src_desc = src_node->GetOpDesc();
  687. GE_IF_BOOL_EXEC(src_desc == nullptr, continue);
  688. GELOGD("[%s:%u] set attr output_memory_type %d", src_desc->GetName().c_str(), src_out_anchor->GetIdx(),
  689. mem_type);
  690. if (!AttrUtils::SetInt(src_desc->MutableOutputDesc(src_out_anchor->GetIdx()), ATTR_OUTPUT_MEMORY_TYPE,
  691. mem_type)) {
  692. REPORT_INNER_ERROR("E19999", "Set Attr:%s for node:%s(%s) out_index:%u failed",
  693. ATTR_OUTPUT_MEMORY_TYPE.c_str(), src_desc->GetName().c_str(), src_desc->GetType().c_str(),
  694. src_out_anchor->GetIdx());
  695. GELOGE(INTERNAL_ERROR, "[Set][Attr] out_memory_type for [%s:%d] failed.", src_desc->GetName().c_str(),
  696. src_out_anchor->GetIdx());
  697. return INTERNAL_ERROR;
  698. }
  699. switch (TransferNodeType(src_node)) {
  700. case kSubgraphNode:
  701. GE_CHK_STATUS_RET(HandleSubgraphNode(src_node, src_out_anchor),
  702. "[Handle][Node] %s in subgraph failed", src_node->GetName().c_str());
  703. break;
  704. case kSubgraphData:
  705. GE_CHK_STATUS_RET(HandleSubgraphDataNode(src_node, src_out_anchor),
  706. "[Handle][DataNode] %s in subgraph failed", src_node->GetName().c_str());
  707. break;
  708. case kOthers:
  709. default:
  710. valid_flag = true;
  711. break;
  712. }
  713. if (valid_flag) {
  714. break;
  715. }
  716. }
  717. }
  718. return SUCCESS;
  719. }
  720. } // namespace ge

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