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

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

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