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.4 kB

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

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