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_depth_fusion_pass.cc 15 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
4 years ago
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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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_depth_fusion_pass.h"
  17. #include <algorithm>
  18. #include "common/ge_inner_error_codes.h"
  19. #include "common/types.h"
  20. #include "graph/compute_graph.h"
  21. #include "graph/ge_tensor.h"
  22. #include "graph/op_desc.h"
  23. #include "graph/utils/graph_utils.h"
  24. #include "graph/common/transop_util.h"
  25. #include "graph/utils/node_utils.h"
  26. namespace ge {
  27. graphStatus TransOpDepthFusionPass::Run(ComputeGraphPtr graph) {
  28. GELOGI("[TransOpDepthFusionPass]: optimize in depth begin...");
  29. if (graph == nullptr) {
  30. return GRAPH_SUCCESS;
  31. }
  32. for (const auto &node : graph->GetDirectNode()) {
  33. GE_CHECK_NOTNULL(node);
  34. if (TransOpUtil::IsTransOp(node)) {
  35. continue;
  36. }
  37. GELOGD("Current normal node is: %s, type: %s, begin in-depth recursive", node->GetName().c_str(),
  38. node->GetType().c_str());
  39. for (const auto &out_anchor : node->GetAllOutDataAnchors()) {
  40. GE_CHECK_NOTNULL(out_anchor);
  41. for (const auto &peer_in_anchor : out_anchor->GetPeerInDataAnchors()) {
  42. if (RecursiveInDepth(peer_in_anchor, graph) != GRAPH_SUCCESS) {
  43. GELOGE(INTERNAL_ERROR, "Recursive failed, root node is: %s, type: %s", node->GetName().c_str(),
  44. node->GetType().c_str());
  45. }
  46. }
  47. }
  48. }
  49. GELOGI("[TransOpDepthFusionPass]: Optimize in depth success...");
  50. return GRAPH_SUCCESS;
  51. }
  52. /// @@ Method:
  53. /// Depth-first recursive strategy was utilized to traverse all the trans ops.
  54. /// Both trans ops will be offset when the back one's output desc is consistent
  55. /// with it's former neighbor's input.
  56. /// @@ Limitation:
  57. /// The current method only judge the neighbors. Trans ops separated by some
  58. /// other ops which can't be offset are not taken into account in current
  59. /// @@ Recursive depth
  60. /// To ensure that the stack does not overflow, the maximum depth in recursive is
  61. /// set to be maxRecursiveDepth = 20. More trans ops are seen abnormally.
  62. graphStatus TransOpDepthFusionPass::RecursiveInDepth(const InDataAnchorPtr &dst_in_anchor,
  63. const ge::ComputeGraphPtr &graph) {
  64. static unsigned int temp_depth = 0;
  65. static const unsigned int max_recursive_depth = 20;
  66. temp_depth++;
  67. if (temp_depth >= max_recursive_depth) {
  68. GELOGI(
  69. "Caution: recursive depth is become %u."
  70. "It's abnormally to have so many trans ops between two normal ops"
  71. "Please check your graph in detail!"
  72. "The search terminate here and continue to another branch.",
  73. temp_depth);
  74. temp_depth--;
  75. return GRAPH_SUCCESS;
  76. }
  77. if (dst_in_anchor == nullptr || dst_in_anchor->GetOwnerNode() == nullptr ||
  78. dst_in_anchor->GetOwnerNode()->GetOpDesc() == nullptr) {
  79. REPORT_INNER_ERROR("E19999", "Param dst_in_anchor related node info has nullptr, check invalid");
  80. GELOGE(FAILED, "parameter is null.");
  81. return GRAPH_FAILED;
  82. }
  83. auto node = dst_in_anchor->GetOwnerNode();
  84. if (!TransOpUtil::IsTransOp(node) || dst_in_anchor->GetIdx() != TransOpUtil::GetTransOpDataIndex(node)) {
  85. GELOGD("Now the end of this branch, node: %s, type: %s, recursive depth: %u", node->GetName().c_str(),
  86. node->GetType().c_str(), temp_depth);
  87. temp_depth--;
  88. return GRAPH_SUCCESS;
  89. } else if (CheckNodeCanBeDeleted(node)) {
  90. GELOGD("node: %s, type: %s does not change memory, just delete", node->GetName().c_str(), node->GetType().c_str());
  91. auto out_anchor = node->GetOutDataAnchor(0);
  92. GE_CHECK_NOTNULL(out_anchor);
  93. auto in_anchors = out_anchor->GetPeerInDataAnchors();
  94. GE_CHK_STATUS_RET(RemoveNode(node, graph), "remove edge failed");
  95. GELOGI("remove node: %s, type: %s.", node->GetName().c_str(), node->GetType().c_str());
  96. for (auto &in_anchor : in_anchors) {
  97. GE_CHECK_NOTNULL(in_anchor);
  98. GE_CHK_STATUS_RET(UpdateSrcAttr(in_anchor->GetPeerOutAnchor(), out_anchor, in_anchor), "UpdateSrcAttr failed");
  99. GE_CHK_STATUS_RET(RecursiveInDepth(in_anchor, graph), "RecursiveInDepth failed");
  100. }
  101. } else if (trans_op_.empty() || !DescAreSymmetry(trans_op_.top(), node)) {
  102. GELOGD("node: %s, type: %s can't be offset, push to trans_op_", node->GetName().c_str(), node->GetType().c_str());
  103. trans_op_.push(node);
  104. auto out_anchor = node->GetOutDataAnchor(0);
  105. GE_CHECK_NOTNULL(out_anchor);
  106. for (const auto &in_anchor : out_anchor->GetPeerInDataAnchors()) {
  107. GE_CHK_STATUS_RET(RecursiveInDepth(in_anchor, graph), "RecursiveInDepth failed");
  108. }
  109. if (node->GetOutDataNodesSize() == 0) {
  110. GE_CHK_STATUS_RET(RemoveNode(node, graph), "remove node failed");
  111. GELOGI("backtracking, trans op: %s, type: %s will be removed", node->GetName().c_str(), node->GetType().c_str());
  112. }
  113. GELOGD("backtracking, trans_op_ fall back. pop node: %s, type: %s.", trans_op_.top()->GetName().c_str(),
  114. trans_op_.top()->GetType().c_str());
  115. trans_op_.pop();
  116. } else if (DescAreSymmetry(trans_op_.top(), node)) {
  117. GELOGD("current node: %s, type: %s can be offset with node: %s, type %s", node->GetName().c_str(),
  118. node->GetType().c_str(), trans_op_.top()->GetName().c_str(), trans_op_.top()->GetType().c_str());
  119. GELOGD("offset_op_ push node: %s, type: %s.", trans_op_.top()->GetName().c_str(),
  120. trans_op_.top()->GetType().c_str());
  121. offset_op_.push(trans_op_.top());
  122. auto in_data_anchor = node->GetInDataAnchor(0);
  123. GE_CHECK_NOTNULL(in_data_anchor);
  124. auto old_out_anchor = in_data_anchor->GetPeerOutAnchor();
  125. GE_CHECK_NOTNULL(old_out_anchor);
  126. auto new_out_anchor = trans_op_.top()->GetInDataAnchor(0)->GetPeerOutAnchor();
  127. GE_CHECK_NOTNULL(new_out_anchor);
  128. GE_IF_BOOL_EXEC(RelinkEdges(new_out_anchor, old_out_anchor, in_data_anchor) != GRAPH_SUCCESS,
  129. GELOGE(FAILED, "RelinkEdges fail.");
  130. return FAILED)
  131. auto out_anchor = node->GetOutDataAnchor(0);
  132. GE_CHECK_NOTNULL(out_anchor);
  133. auto in_anchors = out_anchor->GetPeerInDataAnchors();
  134. GELOGD("begin offset,trans_op_ pop node: %s, type: %s.", trans_op_.top()->GetName().c_str(),
  135. trans_op_.top()->GetType().c_str());
  136. GELOGI("the offset node : %s, type: %s will be removed.", node->GetName().c_str(), node->GetType().c_str());
  137. GE_CHK_STATUS_RET(RemoveNode(node, graph), "remove node failed");
  138. trans_op_.pop();
  139. for (const auto &in_anchor : in_anchors) {
  140. GE_CHECK_NOTNULL(in_anchor);
  141. GE_CHK_STATUS_RET(UpdateSrcAttr(in_anchor->GetPeerOutAnchor(), out_anchor, in_anchor), "UpdateSrcAttr failed");
  142. GE_CHK_STATUS_RET(RecursiveInDepth(in_anchor, graph), "RecursiveInDepth failed");
  143. }
  144. GELOGD("backtracking, trans_op_ push node: %s, type: %s.", offset_op_.top()->GetName().c_str(),
  145. offset_op_.top()->GetType().c_str());
  146. trans_op_.push(offset_op_.top());
  147. offset_op_.pop();
  148. }
  149. temp_depth--;
  150. return GRAPH_SUCCESS;
  151. }
  152. bool TransOpDepthFusionPass::CheckNodeCanBeDeleted(const NodePtr &node) {
  153. bool is_shape_unknown = false;
  154. if (NodeUtils::GetNodeUnknownShapeStatus(*node, is_shape_unknown) == GRAPH_SUCCESS) {
  155. if (is_shape_unknown) {
  156. GELOGI("op:%s is unknown shape, can not be deleted.",
  157. node->GetName().c_str());
  158. return false;
  159. }
  160. }
  161. return node->GetType() == RESHAPE || node->GetType() == REFORMAT || node->GetType() == SQUEEZE ||
  162. node->GetType() == EXPANDDIMS;
  163. }
  164. bool TransOpDepthFusionPass::DescAreSymmetry(const NodePtr &src_node, const NodePtr &dst_node) {
  165. if (src_node == nullptr || dst_node == nullptr || src_node->GetOpDesc() == nullptr ||
  166. dst_node->GetOpDesc() == nullptr) {
  167. return false;
  168. }
  169. const auto &src_input_desc = src_node->GetOpDesc()->MutableInputDesc(0);
  170. const auto &dst_output_desc = dst_node->GetOpDesc()->MutableOutputDesc(0);
  171. GE_CHECK_NOTNULL_EXEC(src_input_desc, return false);
  172. GE_CHECK_NOTNULL_EXEC(dst_output_desc, return false);
  173. const auto &src_input_dtype = src_input_desc->GetDataType();
  174. const auto &src_input_format = src_input_desc->GetFormat();
  175. const auto &src_input_shape = src_input_desc->GetShape().GetDims();
  176. const auto &dst_output_dtype = dst_output_desc->GetDataType();
  177. const auto &dst_output_format = dst_output_desc->GetFormat();
  178. const auto &dst_output_shape = dst_output_desc->GetShape().GetDims();
  179. if (src_node->GetType() == CAST && dst_node->GetType() == CAST) {
  180. return src_input_dtype == dst_output_dtype && src_input_format == dst_output_format;
  181. } else {
  182. return src_input_dtype == dst_output_dtype && src_input_shape == dst_output_shape &&
  183. src_input_format == dst_output_format;
  184. }
  185. }
  186. // If the relationship was changed, the input and src name will be update
  187. graphStatus TransOpDepthFusionPass::UpdateSrcAttr(const OutDataAnchorPtr &new_out_anchor,
  188. const OutDataAnchorPtr &ori_out_anchor,
  189. const InDataAnchorPtr &dst_in_anchor) {
  190. if (dst_in_anchor == nullptr || dst_in_anchor->GetOwnerNode() == nullptr ||
  191. dst_in_anchor->GetOwnerNode()->GetOpDesc() == nullptr) {
  192. GELOGW("dst_in_anchor or it's owner node and op_desc is nullptr");
  193. return GRAPH_SUCCESS;
  194. }
  195. GE_CHECK_NOTNULL(new_out_anchor);
  196. GE_CHECK_NOTNULL(new_out_anchor->GetOwnerNode());
  197. GE_CHECK_NOTNULL(ori_out_anchor);
  198. GE_CHECK_NOTNULL(ori_out_anchor->GetOwnerNode());
  199. auto new_name = new_out_anchor->GetOwnerNode()->GetName();
  200. auto ori_name = ori_out_anchor->GetOwnerNode()->GetName();
  201. auto dst_desc = dst_in_anchor->GetOwnerNode()->GetOpDesc();
  202. auto ori_src_name = dst_desc->GetSrcName();
  203. auto ori_input_name = dst_desc->GetInputName();
  204. std::vector<string> new_src_name;
  205. std::vector<string> new_input_name;
  206. if (ori_src_name.empty()) {
  207. new_src_name.push_back(new_name);
  208. } else {
  209. for (auto &src_name : ori_src_name) {
  210. if (src_name == ori_name) {
  211. new_src_name.push_back(new_name);
  212. } else {
  213. new_src_name.push_back(src_name);
  214. }
  215. }
  216. }
  217. if (ori_input_name.empty()) {
  218. new_input_name.push_back(new_name);
  219. } else {
  220. for (auto &input_name : ori_input_name) {
  221. if (input_name == ori_name) {
  222. new_input_name.push_back(new_name);
  223. } else {
  224. new_input_name.push_back(input_name);
  225. }
  226. }
  227. }
  228. dst_desc->SetSrcName(new_src_name);
  229. dst_desc->SetInputName(new_input_name);
  230. return GRAPH_SUCCESS;
  231. }
  232. /// Relink the offset trans op with it's former neighbor's father node.
  233. /// Note: control edge will be added to link the two offset ops, if the former op
  234. /// has in control nodes
  235. graphStatus TransOpDepthFusionPass::RelinkEdges(const OutDataAnchorPtr &new_out_anchor,
  236. const OutDataAnchorPtr &old_out_anchor,
  237. const InDataAnchorPtr &in_data_anchor) {
  238. if (new_out_anchor == nullptr || old_out_anchor == nullptr || in_data_anchor == nullptr) {
  239. REPORT_INNER_ERROR("E19999", "Param anchor info has nullptr, check invalid");
  240. GELOGE(INTERNAL_ERROR, "new_out_anchor or old_out_anchor or in_data_anchor is nullptr");
  241. return GRAPH_FAILED;
  242. }
  243. if (new_out_anchor->GetOwnerNode() == nullptr || old_out_anchor->GetOwnerNode() == nullptr ||
  244. in_data_anchor->GetOwnerNode() == nullptr) {
  245. REPORT_INNER_ERROR("E19999", "Param anchor info owner node has nullptr, check invalid");
  246. GELOGE(INTERNAL_ERROR, "anchor's owner node is nullptr");
  247. return GRAPH_FAILED;
  248. }
  249. GE_CHK_STATUS_RET(GraphUtils::RemoveEdge(old_out_anchor, in_data_anchor), "remove edge failed");
  250. GE_CHK_STATUS_RET(GraphUtils::AddEdge(new_out_anchor, in_data_anchor), "add edge failed");
  251. GELOGD(
  252. "relink edges before remove node, remove data edge between node: %s, "
  253. "type: %s and node: %s, type: %s.",
  254. old_out_anchor->GetOwnerNode()->GetName().c_str(), old_out_anchor->GetOwnerNode()->GetType().c_str(),
  255. in_data_anchor->GetOwnerNode()->GetName().c_str(), in_data_anchor->GetOwnerNode()->GetType().c_str());
  256. GELOGD(
  257. "relink edges before remove node, add data edge between node: %s, "
  258. "type: %s and node: %s, type: %s.",
  259. new_out_anchor->GetOwnerNode()->GetName().c_str(), new_out_anchor->GetOwnerNode()->GetType().c_str(),
  260. in_data_anchor->GetOwnerNode()->GetName().c_str(), in_data_anchor->GetOwnerNode()->GetType().c_str());
  261. bool is_linked = false;
  262. auto dst_node = in_data_anchor->GetOwnerNode();
  263. auto src_node = old_out_anchor->GetOwnerNode();
  264. auto in_ctrl_nodes = dst_node->GetInControlNodes();
  265. if (!in_ctrl_nodes.empty()) {
  266. auto iter = std::find(in_ctrl_nodes.begin(), in_ctrl_nodes.end(), src_node);
  267. is_linked = iter != in_ctrl_nodes.end();
  268. }
  269. if (!src_node->GetInControlNodes().empty() && !is_linked) {
  270. auto out_ctrl_anchor = src_node->GetOutControlAnchor();
  271. auto in_ctrl_anchor = dst_node->GetInControlAnchor();
  272. GE_CHK_STATUS_RET(GraphUtils::AddEdge(out_ctrl_anchor, in_ctrl_anchor), "add edge failed");
  273. GELOGD(
  274. "relink edges before remove node, add control edge between node: %s,"
  275. " type: %s and node: %s, type: %s.",
  276. src_node->GetName().c_str(), src_node->GetType().c_str(), dst_node->GetName().c_str(),
  277. dst_node->GetType().c_str());
  278. }
  279. return GRAPH_SUCCESS;
  280. }
  281. // Remove trans op by using interface: IsolateNode & RemoveNodeWithoutRelink
  282. graphStatus TransOpDepthFusionPass::RemoveNode(const NodePtr &node, const ge::ComputeGraphPtr &graph) {
  283. if (node == nullptr || graph == nullptr) {
  284. return GRAPH_FAILED;
  285. }
  286. if (GraphUtils::IsolateNode(node, {0}) != GRAPH_SUCCESS) {
  287. REPORT_CALL_ERROR("E19999", "Isolate node:%s(%s) failed", node->GetName().c_str(), node->GetType().c_str());
  288. GELOGE(INTERNAL_ERROR, "Isolate removed node: %s, type: %s failed", node->GetName().c_str(),
  289. node->GetType().c_str());
  290. return GRAPH_FAILED;
  291. }
  292. if (GraphUtils::RemoveNodeWithoutRelink(graph, node) != GRAPH_SUCCESS) {
  293. REPORT_CALL_ERROR("E19999", "Remove node:%s(%s) without relink in graph:%s failed",
  294. node->GetName().c_str(), node->GetType().c_str(), graph->GetName().c_str());
  295. GELOGE(INTERNAL_ERROR, "Remove node: %s, type: %s without relink failed", node->GetName().c_str(),
  296. node->GetType().c_str());
  297. return GRAPH_FAILED;
  298. }
  299. return GRAPH_SUCCESS;
  300. }
  301. } // namespace ge

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