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.4 kB

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

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