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.

remove_same_const_pass.cc 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 "remove_same_const_pass.h"
  17. #include <sstream>
  18. #include <string>
  19. #include <set>
  20. #include "common/base64.h"
  21. #include "ge_local_engine/engine/host_cpu_engine.h"
  22. #include "graph/utils/node_utils.h"
  23. namespace ge {
  24. namespace {
  25. std::string GetCseKey(const NodePtr &node) {
  26. std::stringstream ss;
  27. ss << node->GetType() << "control-inputs-";
  28. std::set<std::string> control_in_node_names;
  29. for (auto &src_node : node->GetInControlNodes()) {
  30. control_in_node_names.insert(src_node->GetName());
  31. }
  32. for (auto &name : control_in_node_names) {
  33. ss << name << "-";
  34. }
  35. ss << "attrs-" << AttrUtils::GetAllAttrsStr(node->GetOpDesc());
  36. return ss.str();
  37. }
  38. bool IsConstType(const NodePtr &node) { return (node->GetType() == CONSTANT || node->GetType() == CONSTANTOP); }
  39. } // namespace
  40. Status RemoveSameConstPass::Run(ComputeGraphPtr graph) {
  41. GELOGD("Begin to run RemoveSameConstPass on the graph");
  42. GE_CHECK_NOTNULL(graph);
  43. std::map<std::string, NodePtr> keys_to_node;
  44. for (const auto &node : graph->GetDirectNode()) {
  45. GE_CHECK_NOTNULL(node);
  46. if (!IsConstType(node)) {
  47. continue;
  48. }
  49. bool is_unknown = false;
  50. auto ret = NodeUtils::GetNodeUnknownShapeStatus(*node, is_unknown);
  51. if (ret != GRAPH_SUCCESS) {
  52. GELOGW("Get node unknown status failed, node name:%s, type:%s.",
  53. node->GetName().c_str(), node->GetType().c_str());
  54. continue;
  55. }
  56. if (is_unknown) {
  57. GELOGI("Current node %s, type %s is unknown shape which should be skip.",
  58. node->GetName().c_str(), node->GetType().c_str());
  59. continue;
  60. }
  61. auto key = GetCseKey(node);
  62. GELOGD("The const node %s cse key %s", node->GetName().c_str(), ge::base64::EncodeToBase64(key).c_str());
  63. auto iter = keys_to_node.find(key);
  64. if (iter == keys_to_node.end()) {
  65. keys_to_node[key] = node;
  66. continue;
  67. }
  68. if (node->GetAllOutDataAnchorsSize() != iter->second->GetAllOutDataAnchorsSize()) {
  69. GELOGW("The const node %s and %s have the same CSE key, but different output anchor count, skip to fusion them",
  70. iter->second->GetName().c_str(), node->GetName().c_str());
  71. continue;
  72. }
  73. std::vector<int> output_map(node->GetAllOutDataAnchorsSize());
  74. for (size_t i = 0; i < node->GetAllOutDataAnchorsSize(); ++i) {
  75. output_map[i] = i;
  76. }
  77. ret = GraphUtils::ReplaceNodeAnchors(iter->second, node, {}, output_map);
  78. if (ret != GRAPH_SUCCESS) {
  79. GELOGE(INTERNAL_ERROR, "Failed to replace node %s by node %s", node->GetName().c_str(),
  80. iter->second->GetName().c_str(), ret);
  81. return INTERNAL_ERROR;
  82. }
  83. NodeUtils::UnlinkAll(*node);
  84. ret = GraphUtils::RemoveNodeWithoutRelink(graph, node);
  85. if (ret != GRAPH_SUCCESS) {
  86. GELOGE(INTERNAL_ERROR, "Failed to remove node %s from graph", node->GetName().c_str());
  87. return INTERNAL_ERROR;
  88. }
  89. GELOGI("Remove const node %s by RemoveSameConstPass, replace it with node %s", node->GetName().c_str(),
  90. iter->second->GetName().c_str());
  91. }
  92. return SUCCESS;
  93. }
  94. } // namespace ge

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