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

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

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