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.

identity_pass.cc 4.6 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 "graph/passes/identity_pass.h"
  17. #include <string>
  18. #include <vector>
  19. #include "framework/common/debug/ge_log.h"
  20. #include "graph/common/omg_util.h"
  21. #include "graph/utils/node_utils.h"
  22. #include "graph/utils/attr_utils.h"
  23. #include "graph/debug/ge_attr_define.h"
  24. namespace ge {
  25. namespace {
  26. ///
  27. /// 1. A `Identity` node may after a `Switch` node and has control-dependency-out nodes.
  28. /// Or a `Identity` node may before a `Merge` node and has control-dependency-in nodes.
  29. /// The identity nodes are used to represent control dependencies in condition branch, and can not be deleted.
  30. /// 2. Check identity is near subgraph.
  31. /// Eg. As output of Data node in subgraph
  32. /// or as input of Netoutput of subgraph
  33. /// or as input of one node with subgraph
  34. /// or as output of one node with subgraph
  35. /// 3. identity with attr no_need_constant_folding should not be deleted too
  36. Status CheckIdentityUsable(const NodePtr &node, bool &usable) {
  37. std::string node_type;
  38. if (node->GetOpDesc()->HasAttr(ge::ATTR_NO_NEED_CONSTANT_FOLDING)) {
  39. usable = true;
  40. return SUCCESS;
  41. }
  42. for (auto &in_node : node->GetInDataNodes()) {
  43. auto in_node_opdesc = in_node->GetOpDesc();
  44. GE_CHECK_NOTNULL(in_node_opdesc);
  45. // near entrance of subgraph || near subgraph
  46. if ((in_node->GetType() == DATA && NodeUtils::IsSubgraphInput(in_node))
  47. || !in_node_opdesc->GetSubgraphInstanceNames().empty()) {
  48. usable = true;
  49. return SUCCESS;
  50. }
  51. GE_CHK_STATUS_RET(GetOriginalType(in_node, node_type),
  52. "Failed to get node type from node %s", node->GetName().c_str());
  53. bool need_skip = (node_type != SWITCH) && (node_type != REFSWITCH) && (node_type != SWITCHN);
  54. if (need_skip) {
  55. GELOGD("skip identity %s connected to switch", node->GetName().c_str());
  56. break;
  57. }
  58. GE_CHECK_NOTNULL(node->GetOutControlAnchor());
  59. if (!node->GetOutControlAnchor()->GetPeerInControlAnchors().empty()) {
  60. usable = true;
  61. return SUCCESS;
  62. }
  63. }
  64. for (auto &out_node : node->GetOutDataNodes()) {
  65. auto out_node_opdesc = out_node->GetOpDesc();
  66. GE_CHECK_NOTNULL(out_node_opdesc);
  67. // near output of subgraph || near subgraph
  68. if (NodeUtils::IsSubgraphOutput(out_node)
  69. || !out_node_opdesc->GetSubgraphInstanceNames().empty()) {
  70. usable = true;
  71. return SUCCESS;
  72. }
  73. GE_CHK_STATUS_RET(GetOriginalType(out_node, node_type),
  74. "Failed to get node type from node %s", node->GetName().c_str());
  75. if ((node_type != MERGE) && (node_type != REFMERGE)) {
  76. GELOGD("skip identity %s connected to merge", node->GetName().c_str());
  77. break;
  78. }
  79. GE_CHECK_NOTNULL(node->GetInControlAnchor());
  80. if (!node->GetInControlAnchor()->GetPeerOutControlAnchors().empty()) {
  81. usable = true;
  82. return SUCCESS;
  83. }
  84. }
  85. usable = false;
  86. return SUCCESS;
  87. }
  88. } // namespace
  89. Status IdentityPass::Run(NodePtr &node) {
  90. GE_CHECK_NOTNULL(node);
  91. auto op_desc = node->GetOpDesc();
  92. GE_CHECK_NOTNULL(op_desc);
  93. string type;
  94. Status status_ret = GetOriginalType(node, type);
  95. if (status_ret != SUCCESS) {
  96. GELOGE(status_ret, "Identity pass get original type fail.");
  97. return status_ret;
  98. }
  99. if ((type != IDENTITY) && (type != IDENTITYN) && (type != READVARIABLEOP)) {
  100. return SUCCESS;
  101. }
  102. if (!force_) {
  103. bool usable = false;
  104. auto ret = CheckIdentityUsable(node, usable);
  105. if (ret != SUCCESS) {
  106. return ret;
  107. }
  108. if (usable) {
  109. return SUCCESS;
  110. }
  111. }
  112. size_t n = node->GetOpDesc()->GetOutputsSize();
  113. if (node->GetOpDesc()->GetInputsSize() != n) {
  114. GELOGE(PARAM_INVALID, "Identity input / output size must be equal. in size:%lu, out size:%lu",
  115. node->GetOpDesc()->GetInputsSize(), n);
  116. return PARAM_INVALID;
  117. }
  118. std::vector<int> io_map;
  119. for (size_t i = 0; i < n; i++) {
  120. io_map.push_back(i);
  121. }
  122. return IsolateAndDeleteNode(node, io_map);
  123. }
  124. } // namespace ge

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