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.

transop_nearby_allreduce_fusion_pass.cc 8.5 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
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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/transop_nearby_allreduce_fusion_pass.h"
  17. #include "framework/common/debug/ge_log.h"
  18. #include "common/debug/log.h"
  19. #include "common/types.h"
  20. #include "graph/utils/graph_utils.h"
  21. #include "graph/common/transop_util.h"
  22. namespace ge {
  23. Status TransOpNearbyAllreduceFusionPass::Run(NodePtr &node) {
  24. if (node == nullptr) {
  25. GELOGW("null node is existed in graph");
  26. return SUCCESS;
  27. }
  28. if (node->GetType() == HCOMALLREDUCE || node->GetType() == HVDCALLBACKALLREDUCE) {
  29. GELOGI("found allreduce op %s", node->GetName().c_str());
  30. Status ret = RemoveNearbyPairedTransOps(node);
  31. if (ret != SUCCESS) {
  32. GELOGE(FAILED, "[Remove][PairedTransOp] for allreduce op:%s", node->GetName().c_str());
  33. return FAILED;
  34. }
  35. GELOGI("successfully remove paired transop for allreduce op (%s)", node->GetName().c_str());
  36. }
  37. return SUCCESS;
  38. }
  39. bool TransOpNearbyAllreduceFusionPass::IsSymmetricTransOps(const NodePtr &node1, const NodePtr &node2) {
  40. if (node1 == nullptr || node2 == nullptr || node1->GetOpDesc() == nullptr || node2->GetOpDesc() == nullptr) {
  41. return false;
  42. }
  43. if (node1->GetType() != TRANSDATA || node2->GetType() != TRANSDATA) {
  44. return false;
  45. }
  46. // two symmetric trans ops should have same type
  47. if (node1->GetType() != node2->GetType()) {
  48. return false;
  49. }
  50. const auto &node1_input_desc = node1->GetOpDesc()->MutableInputDesc(0);
  51. const auto &node1_output_desc = node1->GetOpDesc()->MutableOutputDesc(0);
  52. GE_CHECK_NOTNULL_EXEC(node1_input_desc, return false);
  53. GE_CHECK_NOTNULL_EXEC(node1_output_desc, return false);
  54. const auto &node2_input_desc = node2->GetOpDesc()->MutableInputDesc(0);
  55. const auto &node2_output_desc = node2->GetOpDesc()->MutableOutputDesc(0);
  56. GE_CHECK_NOTNULL_EXEC(node2_input_desc, return false);
  57. GE_CHECK_NOTNULL_EXEC(node2_output_desc, return false);
  58. // two symmetric trans ops should have symmetric input/output datatype
  59. GELOGD("format: nod1_input=%d, nod1_output=%d, nod2_input=%d, nod2_output=%d",
  60. node1_input_desc->GetFormat(), node1_output_desc->GetFormat(), node2_input_desc->GetFormat(),
  61. node2_output_desc->GetFormat());
  62. if (node1_input_desc->GetFormat() != node2_output_desc->GetFormat() ||
  63. node1_output_desc->GetFormat() != node2_input_desc->GetFormat()) {
  64. return false;
  65. }
  66. // two symmetric trans ops should have symmetric input/output format
  67. GELOGD("datatype: nod1_input=%d, nod1_output=%d, nod2_input=%d, nod2_output=%d",
  68. node1_input_desc->GetDataType(), node1_output_desc->GetDataType(), node2_input_desc->GetDataType(),
  69. node2_output_desc->GetDataType());
  70. if (node1_input_desc->GetDataType() != node2_output_desc->GetDataType() ||
  71. node1_output_desc->GetDataType() != node2_input_desc->GetDataType()) {
  72. return false;
  73. }
  74. // two symmetric trans ops should have symmetric input/output shape
  75. if (node1_input_desc->GetShape().GetDims() != node2_output_desc->GetShape().GetDims() ||
  76. node1_output_desc->GetShape().GetDims() != node2_input_desc->GetShape().GetDims()) {
  77. return false;
  78. }
  79. return true;
  80. }
  81. Status TransOpNearbyAllreduceFusionPass::RemoveNearbyPairedTransOps(const NodePtr &node) {
  82. if (node == nullptr) {
  83. return FAILED;
  84. }
  85. GELOGI("find allReduce node %s", node->GetName().c_str());
  86. auto in_data_anchors = node->GetAllInDataAnchors();
  87. auto out_data_anchors = node->GetAllOutDataAnchors();
  88. if (in_data_anchors.size() != out_data_anchors.size()) {
  89. REPORT_INNER_ERROR("E19999", "In data anchors size:%zu not equal to out data anchors size:%zu in node:%s(%s), "
  90. "check invalid", in_data_anchors.size(), out_data_anchors.size(),
  91. node->GetName().c_str(), node->GetType().c_str());
  92. GELOGE(FAILED, "[Check][Param] in and out data anchor size are not equal, node=%s, in_size=%zu, out_size=%zu",
  93. node->GetName().c_str(), in_data_anchors.size(), out_data_anchors.size());
  94. return FAILED;
  95. }
  96. size_t data_anchor_size = in_data_anchors.size();
  97. GELOGI("node = %s, data_anchor_size = %zu", node->GetName().c_str(), data_anchor_size);
  98. size_t removed_node_count = 0;
  99. for (size_t i = 0; i < data_anchor_size; i++) {
  100. if (in_data_anchors.at(i) == nullptr || out_data_anchors.at(i) == nullptr) {
  101. GELOGW("node=%s has a null anchor at idx=%zu", node->GetName().c_str(), i);
  102. continue;
  103. }
  104. if (in_data_anchors.at(i)->GetPeerAnchors().size() != 1) {
  105. GELOGW("nodes=%s has abnormal in peer anchors at %zu", node->GetName().c_str(), i);
  106. continue;
  107. }
  108. if (out_data_anchors.at(i)->GetPeerAnchors().size() != 1) {
  109. GELOGW("nodes=%s has abnormal out peer anchors at %zu", node->GetName().c_str(), i);
  110. continue;
  111. }
  112. auto in_first_peer_anchor = in_data_anchors.at(i)->GetFirstPeerAnchor();
  113. if (in_first_peer_anchor == nullptr) {
  114. GELOGW("node=%s, input anchor idx=%zu, first peer anchor is null", node->GetName().c_str(), i);
  115. continue;
  116. }
  117. auto out_first_peer_anchor = out_data_anchors.at(i)->GetFirstPeerAnchor();
  118. if (out_first_peer_anchor == nullptr) {
  119. GELOGW("node=%s, output anchor idx=%zu, first peer anchor is null", node->GetName().c_str(), i);
  120. continue;
  121. }
  122. auto in_node = in_first_peer_anchor->GetOwnerNode();
  123. auto out_node = out_first_peer_anchor->GetOwnerNode();
  124. GELOGI("in_node=%s, out_node=%s", in_node->GetName().c_str(), out_node->GetName().c_str());
  125. if (!IsSymmetricTransOps(in_node, out_node)) {
  126. GELOGD("ignore asymmetric transop %s and %s for node %s",
  127. in_node->GetName().c_str(), out_node->GetName().c_str(), node->GetName().c_str());
  128. continue;
  129. }
  130. // delete in_node
  131. if (IsolateAndDeleteNode(in_node, {0}) != SUCCESS) {
  132. REPORT_CALL_ERROR("E19999", "Isolate and delete node:%s(%s) failed",
  133. in_node->GetName().c_str(), in_node->GetType().c_str());
  134. GELOGE(FAILED, "[Remove][Node] %s failed", in_node->GetName().c_str());
  135. return FAILED;
  136. }
  137. removed_node_count++;
  138. // delete out_node
  139. if (IsolateAndDeleteNode(out_node, {0}) != SUCCESS) {
  140. REPORT_CALL_ERROR("E19999", "Isolate and delete node:%s(%s) failed",
  141. out_node->GetName().c_str(), out_node->GetType().c_str());
  142. GELOGE(FAILED, "[Remove][Node] %s failed", out_node->GetName().c_str());
  143. return FAILED;
  144. }
  145. removed_node_count++;
  146. // update allreduce input/output desc
  147. GE_CHECK_NOTNULL(node->GetOpDesc());
  148. GE_CHECK_NOTNULL(in_node->GetOpDesc());
  149. GE_CHECK_NOTNULL(out_node->GetOpDesc());
  150. auto input_desc = in_node->GetOpDesc()->GetInputDesc(0);
  151. auto output_desc = out_node->GetOpDesc()->GetOutputDesc(0);
  152. if (node->GetOpDesc()->UpdateInputDesc(static_cast<uint32_t>(i), input_desc) != GRAPH_SUCCESS) {
  153. REPORT_CALL_ERROR("E19999", "Update input:%zu desc in op:%s(%s) failed",
  154. i, node->GetName().c_str(), node->GetType().c_str());
  155. GELOGE(FAILED, "[Update][InputDesc] in op:%s(%s) failed, input index:%zu",
  156. node->GetName().c_str(), node->GetType().c_str(), i);
  157. }
  158. if (node->GetOpDesc()->UpdateOutputDesc(static_cast<uint32_t>(i), output_desc) != GRAPH_SUCCESS) {
  159. REPORT_CALL_ERROR("E19999", "Update output:%zu desc in op:%s(%s) failed",
  160. i, node->GetName().c_str(), node->GetType().c_str());
  161. GELOGE(FAILED, "[Update][OutputDesc] in op:%s(%s) failed, input index:%zu",
  162. node->GetName().c_str(), node->GetType().c_str(), i);
  163. }
  164. GELOGI("successfully remove paired transop (%s and %s) for node %s",
  165. in_node->GetName().c_str(), out_node->GetName().c_str(), node->GetName().c_str());
  166. }
  167. GELOGI("successfully remove %zu pair of transops in total for node %s", removed_node_count, node->GetName().c_str());
  168. return SUCCESS;
  169. }
  170. } // namespace ge

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