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.

hccl_memcpy_pass.cc 6.9 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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/hccl_memcpy_pass.h"
  17. #include <string>
  18. #include "common/debug/log.h"
  19. #include "framework/common/debug/ge_log.h"
  20. #include "common/ge_inner_error_codes.h"
  21. #include "common/ge/ge_util.h"
  22. #include "framework/common/types.h"
  23. #include "graph/utils/graph_utils.h"
  24. namespace {
  25. const int32_t kAnchorSize = 1;
  26. const int kAnchorNum = 0;
  27. const char *const kInputMutable = "_input_mutable";
  28. } // namespace
  29. namespace ge {
  30. Status HcclMemcpyPass::Run(ge::ComputeGraphPtr graph) {
  31. GE_IF_BOOL_EXEC(graph == nullptr, GELOGE(PARAM_INVALID, "param [graph] must not be null."); return PARAM_INVALID);
  32. for (const auto &node : graph->GetDirectNode()) {
  33. auto op_desc = node->GetOpDesc();
  34. GE_IF_BOOL_EXEC(op_desc == nullptr, continue);
  35. bool node_input_mutable = false;
  36. if (!AttrUtils::HasAttr(op_desc, kInputMutable)) {
  37. continue;
  38. }
  39. GE_IF_BOOL_EXEC(!AttrUtils::GetBool(op_desc, kInputMutable, node_input_mutable),
  40. GELOGE(INTERNAL_ERROR, "node:%s get attr:_input_mutable failed.", node->GetName().c_str()); return FAILED);
  41. if (!node_input_mutable) {
  42. continue;
  43. }
  44. GELOGI("hcom op is:%s.", op_desc->GetName().c_str());
  45. for (auto &hccl_in_anchor : node->GetAllInDataAnchors()) {
  46. if (hccl_in_anchor == nullptr) {
  47. continue;
  48. }
  49. auto src_out_anchor = hccl_in_anchor->GetPeerOutAnchor();
  50. GE_CHECK_NOTNULL(src_out_anchor);
  51. int32_t src_out_anchor_size = src_out_anchor->GetPeerInDataAnchors().size();
  52. if (src_out_anchor_size == kAnchorSize) {
  53. // Memcpyasync needs to be inserted between constant (/data) and hcomallreduce to avoid constant being cleared.
  54. NodePtr src_node = src_out_anchor->GetOwnerNode();
  55. std::string src_type = src_node->GetType();
  56. bool check_src_type = (src_type == CONSTANTOP) || (src_type == DATA) || (src_type == CONSTANT);
  57. if (check_src_type) {
  58. Status ret = ModifyEdgeConnection(graph, src_out_anchor, hccl_in_anchor);
  59. if (ret != SUCCESS) {
  60. GELOGE(INTERNAL_ERROR, "Failed to modify the connection.");
  61. return ret;
  62. }
  63. }
  64. continue;
  65. }
  66. Status ret = ModifyEdgeConnection(graph, src_out_anchor, hccl_in_anchor);
  67. if (ret != SUCCESS) {
  68. GELOGE(INTERNAL_ERROR, "Failed to modify the connection.");
  69. return ret;
  70. }
  71. }
  72. }
  73. return SUCCESS;
  74. }
  75. ///
  76. /// @brief Add MemcpyAsync Node
  77. /// @param [in] ge::ComputeGraphPtr graph
  78. /// @param [in] ge::OutDataAnchorPtr in_node
  79. /// @return ge::NodePtr
  80. ///
  81. NodePtr HcclMemcpyPass::CreateIdentityNode(const ComputeGraphPtr &graph, const OutDataAnchorPtr &out_data_anchor) {
  82. GE_IF_BOOL_EXEC(graph == nullptr, return nullptr);
  83. NodePtr pre_node = out_data_anchor->GetOwnerNode();
  84. OpDescPtr pre_op_desc = pre_node->GetOpDesc();
  85. if (pre_op_desc == nullptr) {
  86. GELOGE(INTERNAL_ERROR, "OpDesc of pre node is invalid.");
  87. return nullptr;
  88. }
  89. std::string node_name = pre_node->GetName() + "_" + IDENTITY;
  90. node_name = CheckDuplicateName(node_name);
  91. OpDescPtr op_desc = MakeShared<OpDesc>(node_name.c_str(), IDENTITY);
  92. if (op_desc == nullptr) {
  93. GELOGE(INTERNAL_ERROR, "Create identity op: MakeShared op_desc fail.");
  94. return nullptr;
  95. }
  96. GELOGI("Create identity op:%s.", op_desc->GetName().c_str());
  97. graphStatus ret = op_desc->AddInputDesc("x", pre_op_desc->GetOutputDesc(out_data_anchor->GetIdx()));
  98. if (ret != GRAPH_SUCCESS) {
  99. GELOGE(INTERNAL_ERROR, "Create identity op: add input desc fail.");
  100. return nullptr;
  101. }
  102. ret = op_desc->AddOutputDesc("y", pre_op_desc->GetOutputDesc(out_data_anchor->GetIdx()));
  103. if (ret != GRAPH_SUCCESS) {
  104. GELOGE(INTERNAL_ERROR, "Create identity op: add output desc fail.");
  105. return nullptr;
  106. }
  107. // because history reason ,this pass can not do work after constant fold so mark it
  108. (void)AttrUtils::SetBool(op_desc, ATTR_NO_NEED_CONSTANT_FOLDING, false);
  109. NodePtr memcpy_node = graph->AddNode(op_desc);
  110. if (memcpy_node == nullptr) {
  111. GELOGE(INTERNAL_ERROR, "Insert identity node fail.");
  112. return nullptr;
  113. }
  114. return memcpy_node;
  115. }
  116. ///
  117. /// @brief Check duplicate node_name
  118. /// @param [in] std::string& node_name
  119. /// @return std::string
  120. ///
  121. std::string HcclMemcpyPass::CheckDuplicateName(const std::string &node_name) {
  122. std::string tmp_name = node_name;
  123. auto iter = node_num_map_.find(tmp_name);
  124. if (iter != node_num_map_.end()) {
  125. tmp_name = tmp_name + "_" + std::to_string(iter->second);
  126. (iter->second)++;
  127. } else {
  128. node_num_map_[tmp_name] = 1;
  129. }
  130. return tmp_name;
  131. }
  132. ///
  133. /// @brief Modify edge connection
  134. /// @param [in] ComputeGraphPtr graph
  135. /// @param [in] OutDataAnchorPtr src_out_anchor
  136. /// @param [in] InDataAnchorPtr hccl_in_anchor
  137. /// @return status
  138. ///
  139. Status HcclMemcpyPass::ModifyEdgeConnection(const ComputeGraphPtr &graph, const OutDataAnchorPtr &src_out_anchor,
  140. const InDataAnchorPtr &hccl_in_anchor) {
  141. GELOGI("The op %s need insert memcpy async op.", src_out_anchor->GetOwnerNode()->GetName().c_str());
  142. NodePtr memcpy_node = CreateIdentityNode(graph, src_out_anchor);
  143. GE_CHECK_NOTNULL(memcpy_node);
  144. Status ret1 = src_out_anchor->Unlink(hccl_in_anchor);
  145. if (ret1 != SUCCESS) {
  146. GELOGE(INTERNAL_ERROR, "The op %s Unlink anchor %s fail.", src_out_anchor->GetOwnerNode()->GetName().c_str(),
  147. hccl_in_anchor->GetOwnerNode()->GetName().c_str());
  148. return FAILED;
  149. }
  150. auto out_data_anchor_0 = memcpy_node->GetOutDataAnchor(kAnchorNum);
  151. GE_CHECK_NOTNULL(out_data_anchor_0);
  152. ret1 = out_data_anchor_0->LinkTo(hccl_in_anchor);
  153. if (ret1 != SUCCESS) {
  154. GELOGE(INTERNAL_ERROR, "The op %s link anchor %s fail.", memcpy_node->GetName().c_str(),
  155. hccl_in_anchor->GetOwnerNode()->GetName().c_str());
  156. return FAILED;
  157. }
  158. Status ret = src_out_anchor->LinkTo(memcpy_node->GetInDataAnchor(kAnchorNum));
  159. if (ret != SUCCESS) {
  160. GELOGE(INTERNAL_ERROR, "The op %s link anchor %s fail.", src_out_anchor->GetOwnerNode()->GetName().c_str(),
  161. memcpy_node->GetName().c_str());
  162. return FAILED;
  163. }
  164. return SUCCESS;
  165. }
  166. ///
  167. /// @brief Clear Status, used for subgraph pass
  168. /// @return SUCCESS
  169. ///
  170. Status HcclMemcpyPass::ClearStatus() {
  171. node_num_map_.clear();
  172. return SUCCESS;
  173. }
  174. } // namespace ge

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