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.

common_subexpression_elimination_pass.cc 4.5 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 "common_subexpression_elimination_pass.h"
  17. #include <sstream>
  18. #include <string>
  19. #include <set>
  20. #include "common/base64.h"
  21. #include "graph/utils/node_utils.h"
  22. #include "ge_local_engine/engine/host_cpu_engine.h"
  23. #include "graph/passes/folding_pass.h"
  24. namespace ge {
  25. namespace {
  26. std::set<std::string> un_compute_attrs = {
  27. {ATTR_NAME_DATA_DUMP_ORIGIN_OP_NAMES},
  28. };
  29. std::string GetCseKey(const NodePtr &node) {
  30. std::stringstream ss;
  31. ss << node->GetType() << "-data-inputs-";
  32. for (auto &in_anchor : node->GetAllInDataAnchors()) {
  33. auto src_anchor = in_anchor->GetPeerOutAnchor();
  34. if (src_anchor == nullptr) {
  35. ss << in_anchor->GetIdx() << "-null-";
  36. } else {
  37. ss << in_anchor->GetIdx() << "-"
  38. << src_anchor->GetOwnerNode()->GetName() << "-"
  39. << src_anchor->GetIdx() << "-";
  40. }
  41. }
  42. ss << "control-inputs-";
  43. std::set<std::string> control_in_node_names;
  44. for (auto &src_node : node->GetInControlNodes()) {
  45. control_in_node_names.insert(src_node->GetName());
  46. }
  47. for (auto &name : control_in_node_names) {
  48. ss << name << "-";
  49. }
  50. ss << "attrs-" << AttrUtils::GetAttrsStrAfterRid(node->GetOpDesc(), un_compute_attrs);
  51. return ss.str();
  52. }
  53. /// As the operator category has not been defined, we do not know what types of node can be processed by CSE.
  54. /// To avoid delete wrong nodes(e.g. stateful nodes),
  55. /// only nodes have folding kernel will be considered for the CSE process
  56. bool IsNodeSupportCse(const NodePtr &node) {
  57. if (HostCpuEngine::CheckSupported(NodeUtils::GetNodeType(*node))) {
  58. return true;
  59. }
  60. return folding_pass::GetKernelByType(node) != nullptr;
  61. }
  62. } // namespace
  63. Status CommonSubexpressionEliminationPass::Run(ComputeGraphPtr graph) {
  64. GELOGD("Begin to run the CSE process on the graph");
  65. GE_CHECK_NOTNULL(graph);
  66. std::map<std::string, NodePtr> keys_to_node;
  67. for (const auto &node : graph->GetDirectNode()) {
  68. if (!IsNodeSupportCse(node)) {
  69. continue;
  70. }
  71. bool is_unknown = false;
  72. auto ret = NodeUtils::GetNodeUnknownShapeStatus(*node, is_unknown);
  73. if (ret != GRAPH_SUCCESS) {
  74. GELOGW("Get node unknown status failed, node name:%s, type:%s.",
  75. node->GetName().c_str(), node->GetType().c_str());
  76. continue;
  77. }
  78. if (is_unknown) {
  79. GELOGI("Current node %s, type %s is unknown shape which should be skip.",
  80. node->GetName().c_str(), node->GetType().c_str());
  81. continue;
  82. }
  83. auto key = GetCseKey(node);
  84. GELOGD("The node %s cse key %s", node->GetName().c_str(), ge::base64::EncodeToBase64(key).c_str());
  85. auto iter = keys_to_node.find(key);
  86. if (iter == keys_to_node.end()) {
  87. keys_to_node[key] = node;
  88. continue;
  89. }
  90. if (node->GetAllOutDataAnchorsSize() != iter->second->GetAllOutDataAnchorsSize()) {
  91. GELOGW("The node %s and %s have the same CSE key, but different output anchor count, skip to fusion them",
  92. iter->second->GetName().c_str(), node->GetName().c_str());
  93. continue;
  94. }
  95. std::vector<int> output_map(node->GetAllOutDataAnchorsSize());
  96. for (size_t i = 0; i < node->GetAllOutDataAnchorsSize(); ++i) {
  97. output_map[i] = i;
  98. }
  99. ret = GraphUtils::ReplaceNodeAnchors(iter->second, node, {}, output_map);
  100. if (ret != GRAPH_SUCCESS) {
  101. GELOGE(INTERNAL_ERROR, "Failed to replace node %s by node %s error node %u",
  102. node->GetName().c_str(), iter->second->GetName().c_str(), ret);
  103. return INTERNAL_ERROR;
  104. }
  105. NodeUtils::UnlinkAll(*node);
  106. ret = GraphUtils::RemoveNodeWithoutRelink(graph, node);
  107. if (ret != GRAPH_SUCCESS) {
  108. GELOGE(INTERNAL_ERROR, "Failed to remove node %s from graph", node->GetName().c_str());
  109. return INTERNAL_ERROR;
  110. }
  111. GELOGI("Remove node %s by the CSE process, replace it with node %s",
  112. node->GetName().c_str(), iter->second->GetName().c_str());
  113. }
  114. return SUCCESS;
  115. }
  116. } // namespace ge

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