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.

summary_optimize.cc 5.1 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
4 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 <string>
  17. #include <utility>
  18. #include <vector>
  19. #include "graph/optimize/graph_optimize.h"
  20. #include "graph/utils/graph_utils.h"
  21. #include "graph/utils/tensor_utils.h"
  22. #include "framework/omg/omg_inner_types.h"
  23. namespace {
  24. const char *const kSummary = "Summary";
  25. const int kMaxMapSize = 10000;
  26. } // namespace
  27. namespace ge {
  28. Status GraphOptimize::HandleSummaryOp(ComputeGraphPtr &compute_graph) {
  29. GELOGI("[HandleSummaryOp] HandleSummaryOp start!");
  30. if (summary_output_indexes_.size() >= kMaxMapSize) {
  31. REPORT_INNER_ERROR("E19999", "Map size:%zu out of range:%d, check invalid.",
  32. summary_output_indexes_.size(), kMaxMapSize);
  33. GELOGE(FAILED, "[Check][Param] Map size:%zu out of range:%d.", summary_output_indexes_.size(), kMaxMapSize);
  34. return FAILED;
  35. }
  36. if (compute_graph == nullptr) {
  37. REPORT_INNER_ERROR("E19999", "Param compute_graph is nullptr, check invalid.");
  38. GELOGE(GE_GRAPH_PARAM_NULLPTR, "[Check][Param] compute_graph is nullptr.");
  39. return GE_GRAPH_PARAM_NULLPTR;
  40. }
  41. if (summary_output_indexes_.find(compute_graph->GetGraphID()) != summary_output_indexes_.end()) {
  42. return SUCCESS;
  43. }
  44. vector<NodePtr> del_nodes;
  45. vector<NodePtr> front_nodes;
  46. vector<uint8_t> out_index;
  47. std::map<string, size_t> summary_output_indexes = {};
  48. size_t output_index = compute_graph->GetGraphOutNodesInfo().size();
  49. for (auto &node_ptr : compute_graph->GetAllNodes()) {
  50. GE_CHECK_NOTNULL(node_ptr);
  51. OpDescPtr op = node_ptr->GetOpDesc();
  52. GE_IF_BOOL_EXEC(op == nullptr, GELOGW("op is nullptr!"); continue);
  53. if (op->GetType() == kSummary) {
  54. compute_graph->SetSummaryFlag(true);
  55. auto in = node_ptr->GetInDataAnchor(0);
  56. if (in == nullptr) {
  57. REPORT_INNER_ERROR("E19999", "In data anchor(index:0) of node:%s is nullptr", node_ptr->GetName().c_str());
  58. GELOGE(GE_GRAPH_PARAM_NULLPTR, "[Get][InDataAnchor] of node:%s is nullptr, index:0",
  59. node_ptr->GetName().c_str());
  60. return GE_GRAPH_PARAM_NULLPTR;
  61. }
  62. auto peerin = in->GetPeerOutAnchor();
  63. GE_IF_BOOL_EXEC(peerin == nullptr,
  64. REPORT_INNER_ERROR("E19999", "peer out anchor is nullptr, node:%s, in anchor index:0",
  65. node_ptr->GetName().c_str());
  66. GELOGE(GE_GRAPH_PARAM_NULLPTR, "[Get][PeerOutAnchor] of node:%s is nullptr, in anchor index:0",
  67. node_ptr->GetName().c_str());
  68. return GE_GRAPH_PARAM_NULLPTR);
  69. auto ret = GraphUtils::RemoveEdge(peerin, in);
  70. if (ret != SUCCESS) {
  71. return ret;
  72. }
  73. auto front_node = peerin->GetOwnerNode();
  74. front_nodes.emplace_back(front_node);
  75. auto idx = peerin->GetIdx();
  76. out_index.emplace_back(idx);
  77. GELOGI("[GraphOptimize] Summary name: %s, output index: %zu", op->GetName().c_str(), output_index);
  78. summary_output_indexes.emplace(op->GetName(), output_index);
  79. output_index += 1;
  80. del_nodes.emplace_back(node_ptr);
  81. }
  82. }
  83. GE_IF_BOOL_EXEC(!summary_output_indexes.empty(), summary_output_indexes_.insert({compute_graph->GetGraphID(),
  84. summary_output_indexes}));
  85. // add output nodes for summary
  86. std::vector<std::pair<NodePtr, int32_t>> out_nodes_info;
  87. for (size_t i = 0; i < front_nodes.size(); i++) {
  88. out_nodes_info.emplace_back(pair<NodePtr, int32_t>(front_nodes[i], out_index[i]));
  89. }
  90. compute_graph->AppendGraphOutNodesInfo(out_nodes_info);
  91. // delete summary node
  92. for (auto &node_ptr : del_nodes) {
  93. auto ret = GraphUtils::RemoveNodeWithoutRelink(compute_graph, node_ptr);
  94. if (ret != SUCCESS) {
  95. REPORT_CALL_ERROR("E19999", "Call RemoveNodeWithoutRelink failed, node:%s, graph:%s",
  96. node_ptr->GetName().c_str(), compute_graph->GetName().c_str());
  97. GELOGE(ret, "[Call][RemoveNodeWithoutRelink] failed, node:%s, graph:%s",
  98. node_ptr->GetName().c_str(), compute_graph->GetName().c_str());
  99. return ret;
  100. }
  101. // update Target list
  102. vector<NodePtr> graph_target = compute_graph->GetGraphTargetNodesInfo();
  103. auto iter = find(graph_target.begin(), graph_target.end(), node_ptr);
  104. if (iter != graph_target.end()) {
  105. GELOGI("Current node %s is as Target, remove it from target vector.", node_ptr->GetName().c_str());
  106. (void)graph_target.erase(iter);
  107. compute_graph->SetGraphTargetNodesInfo(graph_target);
  108. }
  109. }
  110. return SUCCESS;
  111. }
  112. } // namespace ge

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