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.1 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 || node->GetType() == EXPANDDIMS) {
  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 || node->GetType() == EXPANDDIMS || node->GetType() == SQUEEZE) {
  67. trans_shape = true;
  68. } else if (node->GetType() == REFORMAT) {
  69. trans_format = true;
  70. }
  71. id << node->GetType() << '-' << anchor_index;
  72. // temp solution, we should not care about which stream the trans op on
  73. std::string stream_label;
  74. if (AttrUtils::GetStr(node->GetOpDesc(), ATTR_NAME_STREAM_LABEL, stream_label)) {
  75. GELOGD("Get stream label %s for node %s, add it to fusion id", stream_label.c_str(), node->GetName().c_str());
  76. id << '-' << stream_label;
  77. }
  78. for (const auto &in_ctrl_node : node->GetInControlNodes()) {
  79. // c
  80. // switch-->Identity ---> node
  81. // the control edge from a identity node can not be removed
  82. if (in_ctrl_node->GetType() == IDENTITY) {
  83. id << "-control-in-" << in_ctrl_node->GetName();
  84. }
  85. }
  86. // [Cascade pointer]
  87. const auto &input_desc = node->GetOpDesc()->MutableInputDesc(0);
  88. const auto &output_desc = node->GetOpDesc()->MutableOutputDesc(0);
  89. GE_CHECK_NOTNULL_EXEC(input_desc, return "");
  90. GE_CHECK_NOTNULL_EXEC(output_desc, return "");
  91. if (trans_data_type) {
  92. id << '-';
  93. id << static_cast<int>(input_desc->GetDataType());
  94. id << '-';
  95. id << static_cast<int>(output_desc->GetDataType());
  96. }
  97. if (trans_format) {
  98. id << '-';
  99. id << static_cast<int>(input_desc->GetFormat());
  100. id << '-';
  101. id << static_cast<int>(output_desc->GetFormat());
  102. }
  103. if (trans_shape) {
  104. id << '-';
  105. id << JoinDims(",", input_desc->GetShape().GetDims());
  106. id << '-';
  107. id << JoinDims(",", output_desc->GetShape().GetDims());
  108. }
  109. return id.str();
  110. }
  111. /**
  112. * Get all transform operators in the output of node.
  113. * @param node
  114. * @return std::map
  115. * key - transform operator identifer
  116. * value - transform operator set
  117. */
  118. std::map<std::string, std::vector<NodePtr>> TransOpBreadthFusionPass::GetOutputTransOpNodes(const NodePtr &node) {
  119. auto result = std::map<std::string, std::vector<NodePtr>>();
  120. if (node == nullptr) {
  121. return result;
  122. }
  123. for (const auto &out_anchor : node->GetAllOutDataAnchors()) {
  124. if (out_anchor == nullptr) {
  125. continue;
  126. }
  127. for (const auto &peer_in_anchor : out_anchor->GetPeerInDataAnchors()) {
  128. if (peer_in_anchor == nullptr) {
  129. continue;
  130. }
  131. auto peer_node = peer_in_anchor->GetOwnerNode();
  132. if (peer_node == nullptr) {
  133. continue;
  134. }
  135. if (TransOpUtil::IsTransOp(peer_node) &&
  136. peer_in_anchor->GetIdx() == TransOpUtil::GetTransOpDataIndex(peer_node)) {
  137. auto output_node_id = GetNodeId(out_anchor->GetIdx(), peer_node);
  138. result[output_node_id].push_back(peer_node);
  139. }
  140. }
  141. }
  142. return result;
  143. }
  144. /**
  145. * Reserving Transform operators which with smaller topo index,
  146. * other transform operators's output edges merge to the reserved transform operator.
  147. * Removed transform operators have no output edges.
  148. * @param trans_nodes
  149. * @param graph
  150. */
  151. graphStatus TransOpBreadthFusionPass::Fusion(const std::vector<NodePtr> &trans_nodes, ComputeGraphPtr &graph) {
  152. if (trans_nodes.empty()) {
  153. return GRAPH_FAILED;
  154. }
  155. size_t min_index = 0;
  156. GE_CHECK_NOTNULL(trans_nodes[0]);
  157. auto op_desc = trans_nodes[0]->GetOpDesc();
  158. GE_CHECK_NOTNULL(op_desc);
  159. int64_t min_id = op_desc->GetId();
  160. size_t vec_size = trans_nodes.size();
  161. for (size_t i = 1; i < vec_size; i++) {
  162. GE_CHECK_NOTNULL(trans_nodes[i]);
  163. op_desc = trans_nodes[i]->GetOpDesc();
  164. GE_CHECK_NOTNULL(op_desc);
  165. if (op_desc->GetId() < min_id) {
  166. min_index = i;
  167. min_id = op_desc->GetId();
  168. }
  169. }
  170. NodePtr node_remain = trans_nodes[min_index];
  171. for (size_t i = 0; i < trans_nodes.size(); ++i) {
  172. if (min_index == i) {
  173. continue;
  174. }
  175. graphStatus status = NodeUtils::MoveOutputEdges(trans_nodes[i], node_remain);
  176. if (status != GRAPH_SUCCESS) {
  177. return status;
  178. }
  179. // remove useless trans_node
  180. status = GraphUtils::IsolateNode(trans_nodes[i], {});
  181. if (status != GRAPH_SUCCESS) {
  182. return status;
  183. }
  184. status = GraphUtils::RemoveNodeWithoutRelink(graph, trans_nodes[i]);
  185. if (status != GRAPH_SUCCESS) {
  186. return status;
  187. }
  188. GELOGD("[Breadth fusion] Remove node %s from graph", trans_nodes[i]->GetName().c_str());
  189. }
  190. return GRAPH_SUCCESS;
  191. }
  192. std::string TransOpBreadthFusionPass::JoinDims(const std::string &sp, const std::vector<int64_t> &dims) {
  193. std::stringstream ss;
  194. bool first = true;
  195. for (int64_t dim : dims) {
  196. if (first) {
  197. first = false;
  198. } else {
  199. ss << sp;
  200. }
  201. ss << dim;
  202. }
  203. return ss.str();
  204. }
  205. } // namespace ge

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