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.

assign_remove_pass.cc 7.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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/assign_remove_pass.h"
  17. #include "framework/common/debug/log.h"
  18. #include "graph/utils/graph_utils.h"
  19. #include "graph/debug/ge_attr_define.h"
  20. namespace ge {
  21. namespace {
  22. constexpr uint32_t kValidInputNodeOutputNum = 1;
  23. constexpr int32_t kAssignRefInputIndex = 0;
  24. constexpr int32_t kAssignValueInputIndex = 1;
  25. const std::set<std::string> kNoTaskNodeTypes = { ge::DATA, ge::ANN_DATA, ge::AIPPDATA,
  26. ge::CONSTANT, ge::CONSTANTOP,
  27. ge::VARIABLE, ge::VARIABLEV2 };
  28. }
  29. Status AssignRemovePass::Run(NodePtr &node) {
  30. GELOGD("AssignRemovePass running");
  31. if (TransformAttr(node) != SUCCESS) {
  32. GELOGE(FAILED, "Transform assign_var_name attr failed, node=%s", node->GetName().c_str());
  33. return FAILED;
  34. }
  35. if (node->GetType() == ASSIGN) {
  36. if (OptimizedAssignNode(node) != SUCCESS) {
  37. GELOGE(FAILED, "Optimize for assign_node %s failed", node->GetName().c_str());
  38. return FAILED;
  39. }
  40. }
  41. GELOGD("AssignRemovePass success");
  42. return SUCCESS;
  43. }
  44. ///
  45. /// @brief Optimize for assign_node
  46. /// @param [in] assign_node
  47. /// @return Status
  48. ///
  49. Status AssignRemovePass::OptimizedAssignNode(NodePtr &assign_node) {
  50. const auto &ref_in_anchor = assign_node->GetInDataAnchor(kAssignRefInputIndex);
  51. const auto &value_in_anchor = assign_node->GetInDataAnchor(kAssignValueInputIndex);
  52. if ((ref_in_anchor == nullptr) || (value_in_anchor == nullptr)) {
  53. GELOGE(FAILED, "In data anchor is null, node:%s", assign_node->GetName().c_str());
  54. return FAILED;
  55. }
  56. const auto &ref_peer_anchor = ref_in_anchor->GetPeerOutAnchor();
  57. const auto &value_peer_anchor = value_in_anchor->GetPeerOutAnchor();
  58. if ((ref_peer_anchor == nullptr) || (value_peer_anchor == nullptr)) {
  59. GELOGE(FAILED, "Peer data anchor is null, node:%s", assign_node->GetName().c_str());
  60. return FAILED;
  61. }
  62. if (IsCondMatch(assign_node, ref_peer_anchor, value_peer_anchor)) {
  63. ///
  64. /// variable not-const not-const
  65. /// \ / |
  66. /// \ / |
  67. /// Assign ----> variable
  68. /// | |
  69. /// | |
  70. /// node node
  71. ///
  72. GELOGD("Optimization for assign_node %s start", assign_node->GetName().c_str());
  73. if (IsolateAndDeleteNode(assign_node, {kAssignRefInputIndex}) != SUCCESS) {
  74. GELOGE(FAILED, "Isolate and delete assign_node %s failed.", assign_node->GetName().c_str());
  75. return FAILED;
  76. }
  77. const auto &ref_input = ref_peer_anchor->GetOwnerNode()->GetOpDesc();
  78. const auto &value_input = value_peer_anchor->GetOwnerNode()->GetOpDesc();
  79. if ((ref_input == nullptr) || (value_input == nullptr)) {
  80. GELOGE(FAILED, "value input is null");
  81. return FAILED;
  82. }
  83. // variable has and only has one input
  84. if (ref_input->UpdateInputDesc(0, value_input->GetOutputDesc(value_peer_anchor->GetIdx())) != GRAPH_SUCCESS) {
  85. GELOGE(FAILED, "Update input_desc for variable %s failed.", ref_input->GetName().c_str());
  86. return FAILED;
  87. }
  88. if (GraphUtils::AddEdge(value_peer_anchor, ref_peer_anchor->GetOwnerNode()->GetInDataAnchor(0)) != GRAPH_SUCCESS) {
  89. GELOGE(FAILED, "Add data edge %s->%s failed", value_input->GetName().c_str(), ref_input->GetName().c_str());
  90. return FAILED;
  91. }
  92. GELOGD("add attr ASSIGN_VAR_NAME on node %s, var_name=%s",
  93. value_input->GetName().c_str(), ref_input->GetName().c_str());
  94. if (!AttrUtils::SetStr(value_input->MutableOutputDesc(value_peer_anchor->GetIdx()), ASSIGN_VAR_NAME,
  95. ref_input->GetName())) {
  96. GELOGE(FAILED, "Set attr ASSIGN_VAR_NAME failed.");
  97. return FAILED;
  98. }
  99. auto value_node = value_peer_anchor->GetOwnerNode();
  100. AddRePassNode(value_node);
  101. }
  102. return SUCCESS;
  103. }
  104. ///
  105. /// @brief Transform assign_var_name attr
  106. /// @param [in] node
  107. /// @return Status
  108. ///
  109. Status AssignRemovePass::TransformAttr(NodePtr &node) {
  110. GE_CHECK_NOTNULL(node->GetOpDesc());
  111. for (const auto &output_desc : node->GetOpDesc()->GetAllOutputsDesc()) {
  112. int32_t inplace_input_idx = -1;
  113. std::string assign_var_name;
  114. if (AttrUtils::GetInt(output_desc, INPLACE_SUPPORT_INPUT_INDEX, inplace_input_idx) &&
  115. AttrUtils::GetStr(output_desc, ASSIGN_VAR_NAME, assign_var_name)) {
  116. GELOGD("Transform attr ASSIGN_VAR_NAME on node %s, assign_var_name=%s, inplace_input_idx=%d, ",
  117. node->GetName().c_str(), assign_var_name.c_str(), inplace_input_idx);
  118. const auto &in_data_anchor = node->GetInDataAnchor(inplace_input_idx);
  119. GE_CHECK_NOTNULL(in_data_anchor);
  120. const auto &peer_data_anchor = in_data_anchor->GetPeerOutAnchor();
  121. GE_CHECK_NOTNULL(peer_data_anchor);
  122. auto in_node = peer_data_anchor->GetOwnerNode();
  123. GE_CHECK_NOTNULL(in_node->GetOpDesc());
  124. GELOGD("add attr ASSIGN_VAR_NAME on node %s, var_name=%s", in_node->GetName().c_str(), assign_var_name.c_str());
  125. if (!AttrUtils::SetStr(in_node->GetOpDesc()->MutableOutputDesc(peer_data_anchor->GetIdx()),
  126. ASSIGN_VAR_NAME, assign_var_name)) {
  127. GELOGE(FAILED, "Set attr ASSIGN_VAR_NAME failed.");
  128. return FAILED;
  129. }
  130. AddRePassNode(in_node);
  131. }
  132. }
  133. return SUCCESS;
  134. }
  135. ///
  136. /// @brief Check if need optimize for assign_node
  137. /// @param [in] assign_node
  138. /// @param [in] peer_data_anchor for ref_input of assign_node
  139. /// @param [in] peer_data_anchor for value_input of assign_node
  140. /// @return Status
  141. ///
  142. bool AssignRemovePass::IsCondMatch(const NodePtr &node, const OutDataAnchorPtr &ref_peer_anchor,
  143. const OutDataAnchorPtr &value_peer_anchor) {
  144. GELOGD("Check if assign_node %s match optimization condition, ref_input: %s, value_input: %s",
  145. node->GetName().c_str(), ref_peer_anchor->GetOwnerNode()->GetName().c_str(),
  146. value_peer_anchor->GetOwnerNode()->GetName().c_str());
  147. if (kNoTaskNodeTypes.count(value_peer_anchor->GetOwnerNode()->GetType()) > 0) {
  148. GELOGD("value input is not calculate node");
  149. return false;
  150. }
  151. const std::string &ref_type = ref_peer_anchor->GetOwnerNode()->GetType();
  152. if ((ref_type != VARIABLE) && (ref_type != VARIABLEV2)) {
  153. GELOGD("ref input is not var");
  154. return false;
  155. }
  156. if (!ref_peer_anchor->GetOwnerNode()->GetInDataNodes().empty()) {
  157. GELOGD("ref input has data input");
  158. return false;
  159. }
  160. if ((ref_peer_anchor->GetPeerInDataNodesSize() != kValidInputNodeOutputNum) ||
  161. (value_peer_anchor->GetPeerInDataNodesSize() != kValidInputNodeOutputNum)) {
  162. GELOGD("ref / value input has other output(s)");
  163. return false;
  164. }
  165. GELOGD("Optimization condition matches, assign_node: %s", node->GetName().c_str());
  166. return true;
  167. }
  168. } // namespace ge

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