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_pass.cc 5.2 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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_pass.h"
  17. #include "framework/common/debug/ge_log.h"
  18. #include "framework/common/debug/log.h"
  19. #include "graph/utils/graph_utils.h"
  20. #include "graph/debug/ge_attr_define.h"
  21. namespace {
  22. const uint32_t kValidInputNodeOutputNum = 1;
  23. const int32_t kAssignRefInputIndex = 0;
  24. const int32_t kAssignValueInputIndex = 1;
  25. }
  26. namespace ge {
  27. Status AssignPass::Run(NodePtr &node) {
  28. GELOGD("AssignPass running");
  29. if (node->GetType() != ASSIGN) {
  30. GELOGD("No need run AssignPass on [%s, %s].", node->GetName().c_str(), node->GetType().c_str());
  31. return SUCCESS;
  32. }
  33. const auto &ref_in_anchor = node->GetInDataAnchor(kAssignRefInputIndex);
  34. const auto &value_in_anchor = node->GetInDataAnchor(kAssignValueInputIndex);
  35. if ((ref_in_anchor == nullptr) || (value_in_anchor == nullptr)) {
  36. GELOGE(FAILED, "In data anchor is null, node:%s", node->GetName().c_str());
  37. return FAILED;
  38. }
  39. const auto &ref_peer_anchor = ref_in_anchor->GetPeerOutAnchor();
  40. const auto &value_peer_anchor = value_in_anchor->GetPeerOutAnchor();
  41. if ((ref_peer_anchor == nullptr) || (value_peer_anchor == nullptr)) {
  42. GELOGE(FAILED, "Peer data anchor is null, node:%s", node->GetName().c_str());
  43. return FAILED;
  44. }
  45. if (IsCondMatch(node, ref_peer_anchor, value_peer_anchor)) {
  46. ///
  47. /// variable not-const not-const
  48. /// \ / |
  49. /// \ / |
  50. /// Assign ----> variable
  51. /// | |
  52. /// | |
  53. /// node node
  54. ///
  55. GELOGI("Optimization for assign_node %s start", node->GetName().c_str());
  56. if (IsolateAndDeleteNode(node, {kAssignRefInputIndex}) != SUCCESS) {
  57. GELOGE(FAILED, "Isolate and delete assign_node %s failed.", node->GetName().c_str());
  58. return FAILED;
  59. }
  60. AddNodeDeleted(node);
  61. const auto &ref_input = ref_peer_anchor->GetOwnerNode()->GetOpDesc();
  62. const auto &value_input = value_peer_anchor->GetOwnerNode()->GetOpDesc();
  63. if ((ref_input == nullptr) || (value_input == nullptr)) {
  64. GELOGE(FAILED, "value input is null");
  65. return FAILED;
  66. }
  67. if (!AttrUtils::SetStr(value_input->MutableOutputDesc(value_peer_anchor->GetIdx()), ASSIGN_VAR_NAME,
  68. ref_input->GetName())) {
  69. GELOGE(FAILED, "Set attr ASSIGN_VAR_NAME failed.");
  70. return FAILED;
  71. }
  72. // variable has and only has one input
  73. if (ref_input->UpdateInputDesc(0, value_input->GetOutputDesc(value_peer_anchor->GetIdx())) != GRAPH_SUCCESS) {
  74. GELOGE(FAILED, "Update input_desc for variable %s failed.", ref_input->GetName().c_str());
  75. return FAILED;
  76. }
  77. if (GraphUtils::AddEdge(value_peer_anchor, ref_peer_anchor->GetOwnerNode()->GetInDataAnchor(0)) != GRAPH_SUCCESS) {
  78. GELOGE(FAILED, "Add data edge %s->%s failed", value_input->GetName().c_str(), ref_input->GetName().c_str());
  79. return FAILED;
  80. }
  81. }
  82. GELOGD("AssignPass success");
  83. return SUCCESS;
  84. }
  85. ///
  86. /// @brief Check if need optimize for assign_node
  87. /// @param [in] assign_node
  88. /// @param [in] peer_data_anchor for ref_input of assign_node
  89. /// @param [in] peer_data_anchor for value_input of assign_node
  90. /// @return Status
  91. ///
  92. bool AssignPass::IsCondMatch(const NodePtr &node, const OutDataAnchorPtr &ref_peer_anchor,
  93. const OutDataAnchorPtr &value_peer_anchor) {
  94. GELOGD("Check if assign_node %s match optimization condition, ref_input: %s, value_input: %s",
  95. node->GetName().c_str(), ref_peer_anchor->GetOwnerNode()->GetName().c_str(),
  96. value_peer_anchor->GetOwnerNode()->GetName().c_str());
  97. const std::string &value_type = value_peer_anchor->GetOwnerNode()->GetType();
  98. if ((value_type == CONSTANTOP) || (value_type == CONSTANT)) {
  99. GELOGD("value input is const");
  100. return false;
  101. }
  102. const std::string &ref_type = ref_peer_anchor->GetOwnerNode()->GetType();
  103. if ((ref_type != VARIABLE) && (ref_type != VARIABLEV2)) {
  104. GELOGD("ref input is not var");
  105. return false;
  106. }
  107. if (!ref_peer_anchor->GetOwnerNode()->GetInDataNodes().empty()) {
  108. GELOGD("ref input has data input");
  109. return false;
  110. }
  111. if ((ref_peer_anchor->GetPeerInDataNodesSize() != kValidInputNodeOutputNum) ||
  112. (value_peer_anchor->GetPeerInDataNodesSize() != kValidInputNodeOutputNum)) {
  113. GELOGD("ref / value input has other output(s)");
  114. return false;
  115. }
  116. GELOGD("Optimization condition matches, assign_node: %s", node->GetName().c_str());
  117. return true;
  118. }
  119. } // namespace ge

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