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

5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 "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. GELOGE(FAILED, "Map size out of range.");
  32. return FAILED;
  33. }
  34. if (summary_output_indexes_.find(compute_graph->GetGraphID()) != summary_output_indexes_.end()) {
  35. return SUCCESS;
  36. }
  37. if (compute_graph == nullptr) {
  38. GELOGE(GE_GRAPH_PARAM_NULLPTR, "compute_graph is nullptr.");
  39. return GE_GRAPH_PARAM_NULLPTR;
  40. }
  41. vector<NodePtr> del_nodes;
  42. vector<NodePtr> front_nodes;
  43. vector<uint8_t> out_index;
  44. std::map<string, size_t> summary_output_indexes = {};
  45. size_t output_index = compute_graph->GetGraphOutNodesInfo().size();
  46. for (auto &node_ptr : compute_graph->GetAllNodes()) {
  47. GE_CHECK_NOTNULL(node_ptr);
  48. OpDescPtr op = node_ptr->GetOpDesc();
  49. GE_IF_BOOL_EXEC(op == nullptr, GELOGW("op is nullptr!"); continue);
  50. if (op->GetType() == kSummary) {
  51. compute_graph->SetSummaryFlag(true);
  52. auto in = node_ptr->GetInDataAnchor(0);
  53. if (in == nullptr) {
  54. GELOGE(GE_GRAPH_PARAM_NULLPTR, "in is nullptr.");
  55. return GE_GRAPH_PARAM_NULLPTR;
  56. }
  57. auto peerin = in->GetPeerOutAnchor();
  58. GE_IF_BOOL_EXEC(peerin == nullptr, GELOGE(GE_GRAPH_PARAM_NULLPTR, "peerin is nullptr.");
  59. return GE_GRAPH_PARAM_NULLPTR);
  60. auto ret = GraphUtils::RemoveEdge(peerin, in);
  61. if (ret != SUCCESS) {
  62. return ret;
  63. }
  64. auto front_node = peerin->GetOwnerNode();
  65. front_nodes.emplace_back(front_node);
  66. auto idx = peerin->GetIdx();
  67. out_index.emplace_back(idx);
  68. GELOGI("[GraphOptimize] Summary name: %s, output index: %zu", op->GetName().c_str(), output_index);
  69. summary_output_indexes.emplace(op->GetName(), output_index);
  70. output_index += 1;
  71. del_nodes.emplace_back(node_ptr);
  72. }
  73. }
  74. GE_IF_BOOL_EXEC(!summary_output_indexes.empty(), summary_output_indexes_.insert({compute_graph->GetGraphID(),
  75. summary_output_indexes}));
  76. // add output nodes for summary
  77. std::vector<std::pair<NodePtr, int32_t>> out_nodes_info;
  78. for (size_t i = 0; i < front_nodes.size(); i++) {
  79. out_nodes_info.emplace_back(pair<NodePtr, int32_t>(front_nodes[i], out_index[i]));
  80. }
  81. compute_graph->AppendGraphOutNodesInfo(out_nodes_info);
  82. // delete summary node
  83. for (auto &node_ptr : del_nodes) {
  84. auto ret = GraphUtils::RemoveNodeWithoutRelink(compute_graph, node_ptr);
  85. if (ret != SUCCESS) {
  86. GELOGE(ret, "GraphUtils::RemoveNodeWithoutRelink failed.");
  87. return ret;
  88. }
  89. // update Target list
  90. vector<NodePtr> graph_target = compute_graph->GetGraphTargetNodesInfo();
  91. auto iter = find(graph_target.begin(), graph_target.end(), node_ptr);
  92. if (iter != graph_target.end()) {
  93. GELOGI("Current node %s is as Target, remove it from target vector.", node_ptr->GetName().c_str());
  94. (void)graph_target.erase(iter);
  95. compute_graph->SetGraphTargetNodesInfo(graph_target);
  96. }
  97. }
  98. return SUCCESS;
  99. }
  100. } // namespace ge

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