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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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, "failed to remove paired transop 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. GELOGE(FAILED, "in and out data anchor size are not equal, node=%s, in_size=%zu, out_size=%zu",
  90. node->GetName().c_str(), in_data_anchors.size(), out_data_anchors.size());
  91. return FAILED;
  92. }
  93. size_t data_anchor_size = in_data_anchors.size();
  94. GELOGI("node = %s, data_anchor_size = %zu", node->GetName().c_str(), data_anchor_size);
  95. size_t removed_node_count = 0;
  96. for (size_t i = 0; i < data_anchor_size; i++) {
  97. if (in_data_anchors.at(i) == nullptr || out_data_anchors.at(i) == nullptr) {
  98. GELOGW("node=%s has a null anchor at idx=%zu", node->GetName().c_str(), i);
  99. continue;
  100. }
  101. if (in_data_anchors.at(i)->GetPeerAnchors().size() != 1) {
  102. GELOGW("nodes=%s has abnormal in peer anchors at %zu", node->GetName().c_str(), i);
  103. continue;
  104. }
  105. if (out_data_anchors.at(i)->GetPeerAnchors().size() != 1) {
  106. GELOGW("nodes=%s has abnormal out peer anchors at %zu", node->GetName().c_str(), i);
  107. continue;
  108. }
  109. auto in_first_peer_anchor = in_data_anchors.at(i)->GetFirstPeerAnchor();
  110. if (in_first_peer_anchor == nullptr) {
  111. GELOGW("node=%s, input anchor idx=%zu, first peer anchor is null", node->GetName().c_str(), i);
  112. continue;
  113. }
  114. auto out_first_peer_anchor = out_data_anchors.at(i)->GetFirstPeerAnchor();
  115. if (out_first_peer_anchor == nullptr) {
  116. GELOGW("node=%s, output anchor idx=%zu, first peer anchor is null", node->GetName().c_str(), i);
  117. continue;
  118. }
  119. auto in_node = in_first_peer_anchor->GetOwnerNode();
  120. auto out_node = out_first_peer_anchor->GetOwnerNode();
  121. GELOGI("in_node=%s, out_node=%s", in_node->GetName().c_str(), out_node->GetName().c_str());
  122. if (!IsSymmetricTransOps(in_node, out_node)) {
  123. GELOGD("ignore asymmetric transop %s and %s for node %s",
  124. in_node->GetName().c_str(), out_node->GetName().c_str(), node->GetName().c_str());
  125. continue;
  126. }
  127. // delete in_node
  128. if (IsolateAndDeleteNode(in_node, {0}) != SUCCESS) {
  129. GELOGE(FAILED, "remove node %s failed", in_node->GetName().c_str());
  130. return FAILED;
  131. }
  132. removed_node_count++;
  133. // delete out_node
  134. if (IsolateAndDeleteNode(out_node, {0}) != SUCCESS) {
  135. GELOGE(FAILED, "remove node %s failed", out_node->GetName().c_str());
  136. return FAILED;
  137. }
  138. removed_node_count++;
  139. // update allreduce input/output desc
  140. GE_CHECK_NOTNULL(node->GetOpDesc());
  141. GE_CHECK_NOTNULL(in_node->GetOpDesc());
  142. GE_CHECK_NOTNULL(out_node->GetOpDesc());
  143. auto input_desc = in_node->GetOpDesc()->GetInputDesc(0);
  144. auto output_desc = out_node->GetOpDesc()->GetOutputDesc(0);
  145. if (node->GetOpDesc()->UpdateInputDesc(static_cast<uint32_t>(i), input_desc) != GRAPH_SUCCESS) {
  146. GELOGE(FAILED, "UpdateInputDesc fail.");
  147. }
  148. if (node->GetOpDesc()->UpdateOutputDesc(static_cast<uint32_t>(i), output_desc) != GRAPH_SUCCESS) {
  149. GELOGE(FAILED, "UpdateOutputDesc");
  150. }
  151. GELOGI("successfully remove paired transop (%s and %s) for node %s",
  152. in_node->GetName().c_str(), out_node->GetName().c_str(), node->GetName().c_str());
  153. }
  154. GELOGI("successfully remove %zu pair of transops in total for node %s", removed_node_count, node->GetName().c_str());
  155. return SUCCESS;
  156. }
  157. } // namespace ge

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