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

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

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