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_breadth_fusion_pass.cc 7.0 kB

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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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_breadth_fusion_pass.h"
  17. #include <set>
  18. #include <string>
  19. #include "common/types.h"
  20. #include "graph/common/transop_util.h"
  21. #include "graph/utils/node_utils.h"
  22. namespace ge {
  23. Status TransOpBreadthFusionPass::Run(ge::ComputeGraphPtr graph) {
  24. if (graph == nullptr) {
  25. return SUCCESS;
  26. }
  27. // breadth fusion pass requires new topologic
  28. Status ret_topo = graph->TopologicalSorting();
  29. if (ret_topo != SUCCESS) {
  30. GELOGE(ret_topo, "TopologicalSorting the merged graph failed.");
  31. return ret_topo;
  32. }
  33. for (auto const &node : graph->GetDirectNode()) {
  34. GE_CHECK_NOTNULL(node);
  35. auto ids_to_trans_nodes = GetOutputTransOpNodes(node);
  36. for (auto const &id_to_trans_nodes : ids_to_trans_nodes) {
  37. if (id_to_trans_nodes.second.size() > 1) {
  38. GELOGI(
  39. "Begin to breath fusion output trans-op-nodes for %s, "
  40. "trans id %s, trans-op count %zu",
  41. node->GetName().c_str(), id_to_trans_nodes.first.c_str(), id_to_trans_nodes.second.size());
  42. graphStatus status = Fusion(id_to_trans_nodes.second, graph);
  43. if (status != GRAPH_SUCCESS) {
  44. return FAILED;
  45. }
  46. }
  47. }
  48. }
  49. return SUCCESS;
  50. }
  51. std::string TransOpBreadthFusionPass::GetNodeId(const int anchor_index, const NodePtr &node) {
  52. std::stringstream id;
  53. bool trans_data_type = false;
  54. bool trans_format = false;
  55. bool trans_shape = false;
  56. GE_IF_BOOL_EXEC(node == nullptr || node->GetOpDesc() == nullptr, GELOGE(FAILED, "node is null"); return "");
  57. if (node->GetType() == CAST) {
  58. trans_data_type = true;
  59. } else if (node->GetType() == TRANSPOSE || node->GetType() == TRANSPOSED) {
  60. trans_format = true;
  61. trans_shape = true;
  62. } else if (node->GetType() == TRANSDATA) {
  63. trans_data_type = true;
  64. trans_format = true;
  65. trans_shape = true;
  66. } else if (node->GetType() == RESHAPE) {
  67. trans_shape = true;
  68. }
  69. id << node->GetType() << '-' << anchor_index;
  70. // temp solution, we should not care about which stream the trans op on
  71. std::string stream_label;
  72. if (AttrUtils::GetStr(node->GetOpDesc(), ATTR_NAME_STREAM_LABEL, stream_label)) {
  73. GELOGD("Get stream label %s for node %s, add it to fusion id", stream_label.c_str(), node->GetName().c_str());
  74. id << '-' << stream_label;
  75. }
  76. for (const auto &in_ctrl_node : node->GetInControlNodes()) {
  77. // c
  78. // switch-->Identity ---> node
  79. // the control edge from a identity node can not be removed
  80. if (in_ctrl_node->GetType() == IDENTITY) {
  81. id << "-control-in-" << in_ctrl_node->GetName();
  82. }
  83. }
  84. // [Cascade pointer]
  85. const auto &input_desc = node->GetOpDesc()->MutableInputDesc(0);
  86. const auto &output_desc = node->GetOpDesc()->MutableOutputDesc(0);
  87. GE_CHECK_NOTNULL_EXEC(input_desc, return "");
  88. GE_CHECK_NOTNULL_EXEC(output_desc, return "");
  89. if (trans_data_type) {
  90. id << '-';
  91. id << static_cast<int>(input_desc->GetDataType());
  92. id << '-';
  93. id << static_cast<int>(output_desc->GetDataType());
  94. }
  95. if (trans_format) {
  96. id << '-';
  97. id << static_cast<int>(input_desc->GetFormat());
  98. id << '-';
  99. id << static_cast<int>(output_desc->GetFormat());
  100. }
  101. if (trans_shape) {
  102. id << '-';
  103. id << JoinDims(",", input_desc->GetShape().GetDims());
  104. id << '-';
  105. id << JoinDims(",", output_desc->GetShape().GetDims());
  106. }
  107. return id.str();
  108. }
  109. /**
  110. * Get all transform operators in the output of node.
  111. * @param node
  112. * @return std::map
  113. * key - transform operator identifer
  114. * value - transform operator set
  115. */
  116. std::map<std::string, std::vector<NodePtr>> TransOpBreadthFusionPass::GetOutputTransOpNodes(const NodePtr &node) {
  117. auto result = std::map<std::string, std::vector<NodePtr>>();
  118. if (node == nullptr) {
  119. return result;
  120. }
  121. for (const auto &out_anchor : node->GetAllOutDataAnchors()) {
  122. if (out_anchor == nullptr) {
  123. continue;
  124. }
  125. for (const auto &peer_in_anchor : out_anchor->GetPeerInDataAnchors()) {
  126. if (peer_in_anchor == nullptr) {
  127. continue;
  128. }
  129. auto peer_node = peer_in_anchor->GetOwnerNode();
  130. if (peer_node == nullptr) {
  131. continue;
  132. }
  133. if (TransOpUtil::IsTransOp(peer_node) &&
  134. peer_in_anchor->GetIdx() == TransOpUtil::GetTransOpDataIndex(peer_node)) {
  135. auto output_node_id = GetNodeId(out_anchor->GetIdx(), peer_node);
  136. result[output_node_id].push_back(peer_node);
  137. }
  138. }
  139. }
  140. return result;
  141. }
  142. /**
  143. * Reserving Transform operators which with smaller topo index,
  144. * other transform operators's output edges merge to the reserved transform operator.
  145. * Removed transform operators have no output edges.
  146. * @param trans_nodes
  147. * @param graph
  148. */
  149. graphStatus TransOpBreadthFusionPass::Fusion(const std::vector<NodePtr> &trans_nodes, ComputeGraphPtr &graph) {
  150. if (trans_nodes.empty()) {
  151. return GRAPH_FAILED;
  152. }
  153. size_t min_index = 0;
  154. GE_CHECK_NOTNULL(trans_nodes[0]);
  155. auto op_desc = trans_nodes[0]->GetOpDesc();
  156. GE_CHECK_NOTNULL(op_desc);
  157. int64_t min_id = op_desc->GetId();
  158. size_t vec_size = trans_nodes.size();
  159. for (size_t i = 1; i < vec_size; i++) {
  160. GE_CHECK_NOTNULL(trans_nodes[i]);
  161. op_desc = trans_nodes[i]->GetOpDesc();
  162. GE_CHECK_NOTNULL(op_desc);
  163. if (op_desc->GetId() < min_id) {
  164. min_index = i;
  165. min_id = op_desc->GetId();
  166. }
  167. }
  168. NodePtr node_remain = trans_nodes[min_index];
  169. for (size_t i = 0; i < trans_nodes.size(); ++i) {
  170. if (min_index == i) {
  171. continue;
  172. }
  173. graphStatus status = NodeUtils::MoveOutputEdges(trans_nodes[i], node_remain);
  174. if (status != GRAPH_SUCCESS) {
  175. return status;
  176. }
  177. // remove useless trans_node
  178. status = GraphUtils::IsolateNode(trans_nodes[i], {});
  179. if (status != GRAPH_SUCCESS) {
  180. return status;
  181. }
  182. status = GraphUtils::RemoveNodeWithoutRelink(graph, trans_nodes[i]);
  183. if (status != GRAPH_SUCCESS) {
  184. return status;
  185. }
  186. GELOGD("[Breadth fusion] Remove node %s from graph", trans_nodes[i]->GetName().c_str());
  187. }
  188. return GRAPH_SUCCESS;
  189. }
  190. std::string TransOpBreadthFusionPass::JoinDims(const std::string &sp, const std::vector<int64_t> &dims) {
  191. std::stringstream ss;
  192. bool first = true;
  193. for (int64_t dim : dims) {
  194. if (first) {
  195. first = false;
  196. } else {
  197. ss << sp;
  198. }
  199. ss << dim;
  200. }
  201. return ss.str();
  202. }
  203. } // namespace ge

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