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 10 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. REPORT_INNER_ERROR("E19999", "Index %d or %d input anchor of node:%s(%s) is nullptr, check invalid "
  54. "when AssignRemovePass %s", kAssignRefInputIndex, kAssignValueInputIndex,
  55. assign_node->GetName().c_str(), assign_node->GetType().c_str(), __FUNCTION__);
  56. GELOGE(FAILED, "In data anchor is null, node:%s", assign_node->GetName().c_str());
  57. return FAILED;
  58. }
  59. const auto &ref_peer_anchor = ref_in_anchor->GetPeerOutAnchor();
  60. const auto &value_peer_anchor = value_in_anchor->GetPeerOutAnchor();
  61. if ((ref_peer_anchor == nullptr) || (value_peer_anchor == nullptr)) {
  62. REPORT_INNER_ERROR("E19999", "Index %d or %d input anchor of node:%s(%s), peer anchor is nullptr, check invalid "
  63. "when AssignRemovePass %s", kAssignRefInputIndex, kAssignValueInputIndex,
  64. assign_node->GetName().c_str(), assign_node->GetType().c_str(), __FUNCTION__);
  65. GELOGE(FAILED, "Peer data anchor is null, node:%s", assign_node->GetName().c_str());
  66. return FAILED;
  67. }
  68. if (IsCondMatch(assign_node, ref_peer_anchor, value_peer_anchor)) {
  69. ///
  70. /// variable not-const not-const
  71. /// \ / |
  72. /// \ / |
  73. /// Assign ----> variable
  74. /// | |
  75. /// | |
  76. /// node node
  77. ///
  78. GELOGD("Optimization for assign_node %s start", assign_node->GetName().c_str());
  79. if (IsolateAndDeleteNode(assign_node, {kAssignRefInputIndex}) != SUCCESS) {
  80. REPORT_CALL_ERROR("E19999", "Isolate and delete node:%s(%s) failed when AssignRemovePass %s",
  81. assign_node->GetName().c_str(), assign_node->GetType().c_str(), __FUNCTION__);
  82. GELOGE(FAILED, "Isolate and delete assign_node %s failed.", assign_node->GetName().c_str());
  83. return FAILED;
  84. }
  85. const auto &ref_input = ref_peer_anchor->GetOwnerNode()->GetOpDesc();
  86. const auto &value_input = value_peer_anchor->GetOwnerNode()->GetOpDesc();
  87. if ((ref_input == nullptr) || (value_input == nullptr)) {
  88. REPORT_INNER_ERROR("E19999", "Input index %d or %d of node:%s(%s), peer op is nullptr, check invalid "
  89. "when AssignRemovePass %s", kAssignRefInputIndex, kAssignValueInputIndex,
  90. assign_node->GetName().c_str(), assign_node->GetType().c_str(), __FUNCTION__);
  91. GELOGE(FAILED, "value input is null");
  92. return FAILED;
  93. }
  94. // variable has and only has one input
  95. if (ref_input->UpdateInputDesc(0, value_input->GetOutputDesc(value_peer_anchor->GetIdx())) != GRAPH_SUCCESS) {
  96. REPORT_CALL_ERROR("E19999", "Input index %d of node:%s(%s), update it's peer op input:0 desc failed "
  97. "when AssignRemovePass %s", kAssignRefInputIndex,
  98. assign_node->GetName().c_str(), assign_node->GetType().c_str(), __FUNCTION__);
  99. GELOGE(FAILED, "Update input_desc for variable %s failed.", ref_input->GetName().c_str());
  100. return FAILED;
  101. }
  102. if (GraphUtils::AddEdge(value_peer_anchor, ref_peer_anchor->GetOwnerNode()->GetInDataAnchor(0)) != GRAPH_SUCCESS) {
  103. REPORT_CALL_ERROR("E19999", "Add edge between op:%s(%s)(out_index:%d) and op:%s(%s)(in_index:0) failed "
  104. "when AssignRemovePass %s", value_peer_anchor->GetOwnerNode()->GetName().c_str(),
  105. value_peer_anchor->GetOwnerNode()->GetType().c_str(), value_peer_anchor->GetIdx(),
  106. ref_peer_anchor->GetOwnerNode()->GetName().c_str(),
  107. ref_peer_anchor->GetOwnerNode()->GetType().c_str(), __FUNCTION__);
  108. GELOGE(FAILED, "Add data edge %s->%s failed", value_input->GetName().c_str(), ref_input->GetName().c_str());
  109. return FAILED;
  110. }
  111. GELOGD("add attr ASSIGN_VAR_NAME on node %s, var_name=%s",
  112. value_input->GetName().c_str(), ref_input->GetName().c_str());
  113. if (!AttrUtils::SetStr(value_input->MutableOutputDesc(value_peer_anchor->GetIdx()), ASSIGN_VAR_NAME,
  114. ref_input->GetName())) {
  115. REPORT_CALL_ERROR("E19999", "Set Attr:%s to output:%d desc of node:%s(%s) failed when %s",
  116. ASSIGN_VAR_NAME.c_str(), value_peer_anchor->GetIdx(),
  117. value_input->GetName().c_str(), value_input->GetType().c_str(), __FUNCTION__);
  118. GELOGE(FAILED, "Set attr ASSIGN_VAR_NAME failed.");
  119. return FAILED;
  120. }
  121. auto value_node = value_peer_anchor->GetOwnerNode();
  122. AddRePassNode(value_node);
  123. }
  124. return SUCCESS;
  125. }
  126. ///
  127. /// @brief Transform assign_var_name attr
  128. /// @param [in] node
  129. /// @return Status
  130. ///
  131. Status AssignRemovePass::TransformAttr(NodePtr &node) {
  132. GE_CHECK_NOTNULL(node->GetOpDesc());
  133. for (const auto &output_desc : node->GetOpDesc()->GetAllOutputsDesc()) {
  134. int32_t inplace_input_idx = -1;
  135. std::string assign_var_name;
  136. if (AttrUtils::GetInt(output_desc, INPLACE_SUPPORT_INPUT_INDEX, inplace_input_idx) &&
  137. AttrUtils::GetStr(output_desc, ASSIGN_VAR_NAME, assign_var_name)) {
  138. GELOGD("Transform attr ASSIGN_VAR_NAME on node %s, assign_var_name=%s, inplace_input_idx=%d, ",
  139. node->GetName().c_str(), assign_var_name.c_str(), inplace_input_idx);
  140. const auto &in_data_anchor = node->GetInDataAnchor(inplace_input_idx);
  141. GE_CHECK_NOTNULL(in_data_anchor);
  142. const auto &peer_data_anchor = in_data_anchor->GetPeerOutAnchor();
  143. GE_CHECK_NOTNULL(peer_data_anchor);
  144. auto in_node = peer_data_anchor->GetOwnerNode();
  145. GE_CHECK_NOTNULL(in_node->GetOpDesc());
  146. GELOGD("add attr ASSIGN_VAR_NAME on node %s, var_name=%s", in_node->GetName().c_str(), assign_var_name.c_str());
  147. if (!AttrUtils::SetStr(in_node->GetOpDesc()->MutableOutputDesc(peer_data_anchor->GetIdx()),
  148. ASSIGN_VAR_NAME, assign_var_name)) {
  149. REPORT_CALL_ERROR("E19999", "Set Attr:%s to output:%d desc of node:%s(%s) failed when %s",
  150. ASSIGN_VAR_NAME.c_str(), peer_data_anchor->GetIdx(),
  151. in_node->GetName().c_str(), in_node->GetType().c_str(), __FUNCTION__);
  152. GELOGE(FAILED, "Set attr ASSIGN_VAR_NAME failed.");
  153. return FAILED;
  154. }
  155. AddRePassNode(in_node);
  156. }
  157. }
  158. return SUCCESS;
  159. }
  160. ///
  161. /// @brief Check if need optimize for assign_node
  162. /// @param [in] assign_node
  163. /// @param [in] peer_data_anchor for ref_input of assign_node
  164. /// @param [in] peer_data_anchor for value_input of assign_node
  165. /// @return Status
  166. ///
  167. bool AssignRemovePass::IsCondMatch(const NodePtr &node, const OutDataAnchorPtr &ref_peer_anchor,
  168. const OutDataAnchorPtr &value_peer_anchor) {
  169. GELOGD("Check if assign_node %s match optimization condition, ref_input: %s, value_input: %s",
  170. node->GetName().c_str(), ref_peer_anchor->GetOwnerNode()->GetName().c_str(),
  171. value_peer_anchor->GetOwnerNode()->GetName().c_str());
  172. if (kNoTaskNodeTypes.count(value_peer_anchor->GetOwnerNode()->GetType()) > 0) {
  173. GELOGD("value input is not calculate node");
  174. return false;
  175. }
  176. const std::string &ref_type = ref_peer_anchor->GetOwnerNode()->GetType();
  177. if ((ref_type != VARIABLE) && (ref_type != VARIABLEV2)) {
  178. GELOGD("ref input is not var");
  179. return false;
  180. }
  181. if (!ref_peer_anchor->GetOwnerNode()->GetInDataNodes().empty()) {
  182. GELOGD("ref input has data input");
  183. return false;
  184. }
  185. if ((ref_peer_anchor->GetPeerInDataNodesSize() != kValidInputNodeOutputNum) ||
  186. (value_peer_anchor->GetPeerInDataNodesSize() != kValidInputNodeOutputNum)) {
  187. GELOGD("ref / value input has other output(s)");
  188. return false;
  189. }
  190. GELOGD("Optimization condition matches, assign_node: %s", node->GetName().c_str());
  191. return true;
  192. }
  193. } // namespace ge

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