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 14 kB

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
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. GELOGE(FAILED, "parameter is null.");
  80. return GRAPH_FAILED;
  81. }
  82. auto node = dst_in_anchor->GetOwnerNode();
  83. if (!TransOpUtil::IsTransOp(node) || dst_in_anchor->GetIdx() != TransOpUtil::GetTransOpDataIndex(node)) {
  84. GELOGD("Now the end of this branch, node: %s, type: %s, recursive depth: %u", node->GetName().c_str(),
  85. node->GetType().c_str(), temp_depth);
  86. temp_depth--;
  87. return GRAPH_SUCCESS;
  88. } else if (CheckNodeCanBeDeleted(node)) {
  89. GELOGD("node: %s, type: %s does not change memory, just delete", node->GetName().c_str(), node->GetType().c_str());
  90. auto out_anchor = node->GetOutDataAnchor(0);
  91. GE_CHECK_NOTNULL(out_anchor);
  92. auto in_anchors = out_anchor->GetPeerInDataAnchors();
  93. GE_CHK_STATUS_RET(RemoveNode(node, graph), "remove edge failed");
  94. GELOGI("remove node: %s, type: %s.", node->GetName().c_str(), node->GetType().c_str());
  95. for (auto &in_anchor : in_anchors) {
  96. GE_CHECK_NOTNULL(in_anchor);
  97. GE_CHK_STATUS_RET(UpdateSrcAttr(in_anchor->GetPeerOutAnchor(), out_anchor, in_anchor), "UpdateSrcAttr failed");
  98. GE_CHK_STATUS_RET(RecursiveInDepth(in_anchor, graph), "RecursiveInDepth failed");
  99. }
  100. } else if (trans_op_.empty() || !DescAreSymmetry(trans_op_.top(), node)) {
  101. GELOGD("node: %s, type: %s can't be offset, push to trans_op_", node->GetName().c_str(), node->GetType().c_str());
  102. trans_op_.push(node);
  103. auto out_anchor = node->GetOutDataAnchor(0);
  104. GE_CHECK_NOTNULL(out_anchor);
  105. for (const auto &in_anchor : out_anchor->GetPeerInDataAnchors()) {
  106. GE_CHK_STATUS_RET(RecursiveInDepth(in_anchor, graph), "RecursiveInDepth failed");
  107. }
  108. if (node->GetOutDataNodesSize() == 0) {
  109. GE_CHK_STATUS_RET(RemoveNode(node, graph), "remove node failed");
  110. GELOGI("backtracking, trans op: %s, type: %s will be removed", node->GetName().c_str(), node->GetType().c_str());
  111. }
  112. GELOGD("backtracking, trans_op_ fall back. pop node: %s, type: %s.", trans_op_.top()->GetName().c_str(),
  113. trans_op_.top()->GetType().c_str());
  114. trans_op_.pop();
  115. } else if (DescAreSymmetry(trans_op_.top(), node)) {
  116. GELOGD("current node: %s, type: %s can be offset with node: %s, type %s", node->GetName().c_str(),
  117. node->GetType().c_str(), trans_op_.top()->GetName().c_str(), trans_op_.top()->GetType().c_str());
  118. GELOGD("offset_op_ push node: %s, type: %s.", trans_op_.top()->GetName().c_str(),
  119. trans_op_.top()->GetType().c_str());
  120. offset_op_.push(trans_op_.top());
  121. auto in_data_anchor = node->GetInDataAnchor(0);
  122. GE_CHECK_NOTNULL(in_data_anchor);
  123. auto old_out_anchor = in_data_anchor->GetPeerOutAnchor();
  124. GE_CHECK_NOTNULL(old_out_anchor);
  125. auto new_out_anchor = trans_op_.top()->GetInDataAnchor(0)->GetPeerOutAnchor();
  126. GE_CHECK_NOTNULL(new_out_anchor);
  127. GE_IF_BOOL_EXEC(RelinkEdges(new_out_anchor, old_out_anchor, in_data_anchor) != GRAPH_SUCCESS,
  128. GELOGE(FAILED, "RelinkEdges fail.");
  129. return FAILED)
  130. auto out_anchor = node->GetOutDataAnchor(0);
  131. GE_CHECK_NOTNULL(out_anchor);
  132. auto in_anchors = out_anchor->GetPeerInDataAnchors();
  133. GELOGD("begin offset,trans_op_ pop node: %s, type: %s.", trans_op_.top()->GetName().c_str(),
  134. trans_op_.top()->GetType().c_str());
  135. GELOGI("the offset node : %s, type: %s will be removed.", node->GetName().c_str(), node->GetType().c_str());
  136. GE_CHK_STATUS_RET(RemoveNode(node, graph), "remove node failed");
  137. trans_op_.pop();
  138. for (const auto &in_anchor : in_anchors) {
  139. GE_CHECK_NOTNULL(in_anchor);
  140. GE_CHK_STATUS_RET(UpdateSrcAttr(in_anchor->GetPeerOutAnchor(), out_anchor, in_anchor), "UpdateSrcAttr failed");
  141. GE_CHK_STATUS_RET(RecursiveInDepth(in_anchor, graph), "RecursiveInDepth failed");
  142. }
  143. GELOGD("backtracking, trans_op_ push node: %s, type: %s.", offset_op_.top()->GetName().c_str(),
  144. offset_op_.top()->GetType().c_str());
  145. trans_op_.push(offset_op_.top());
  146. offset_op_.pop();
  147. }
  148. temp_depth--;
  149. return GRAPH_SUCCESS;
  150. }
  151. bool TransOpDepthFusionPass::CheckNodeCanBeDeleted(const NodePtr &node) {
  152. bool is_shape_unknown = false;
  153. if (NodeUtils::GetNodeUnknownShapeStatus(*node, is_shape_unknown) == GRAPH_SUCCESS) {
  154. if (is_shape_unknown) {
  155. GELOGI("op:%s is unknown shape, can not be deleted.",
  156. node->GetName().c_str());
  157. return false;
  158. }
  159. }
  160. return node->GetType() == RESHAPE || node->GetType() == REFORMAT || node->GetType() == SQUEEZE ||
  161. node->GetType() == EXPANDDIMS;
  162. }
  163. bool TransOpDepthFusionPass::DescAreSymmetry(const NodePtr &src_node, const NodePtr &dst_node) {
  164. if (src_node == nullptr || dst_node == nullptr || src_node->GetOpDesc() == nullptr ||
  165. dst_node->GetOpDesc() == nullptr) {
  166. return false;
  167. }
  168. const auto &src_input_desc = src_node->GetOpDesc()->MutableInputDesc(0);
  169. const auto &dst_output_desc = dst_node->GetOpDesc()->MutableOutputDesc(0);
  170. GE_CHECK_NOTNULL_EXEC(src_input_desc, return false);
  171. GE_CHECK_NOTNULL_EXEC(dst_output_desc, return false);
  172. const auto &src_input_dtype = src_input_desc->GetDataType();
  173. const auto &src_input_format = src_input_desc->GetFormat();
  174. const auto &src_input_shape = src_input_desc->GetShape().GetDims();
  175. const auto &dst_output_dtype = dst_output_desc->GetDataType();
  176. const auto &dst_output_format = dst_output_desc->GetFormat();
  177. const auto &dst_output_shape = dst_output_desc->GetShape().GetDims();
  178. if (src_node->GetType() == CAST && dst_node->GetType() == CAST) {
  179. return src_input_dtype == dst_output_dtype && src_input_format == dst_output_format;
  180. } else {
  181. return src_input_dtype == dst_output_dtype && src_input_shape == dst_output_shape &&
  182. src_input_format == dst_output_format;
  183. }
  184. }
  185. // If the relationship was changed, the input and src name will be update
  186. graphStatus TransOpDepthFusionPass::UpdateSrcAttr(const OutDataAnchorPtr &new_out_anchor,
  187. const OutDataAnchorPtr &ori_out_anchor,
  188. const InDataAnchorPtr &dst_in_anchor) {
  189. if (dst_in_anchor == nullptr || dst_in_anchor->GetOwnerNode() == nullptr ||
  190. dst_in_anchor->GetOwnerNode()->GetOpDesc() == nullptr) {
  191. GELOGW("dst_in_anchor or it's owner node and op_desc is nullptr");
  192. return GRAPH_SUCCESS;
  193. }
  194. GE_CHECK_NOTNULL(new_out_anchor);
  195. GE_CHECK_NOTNULL(new_out_anchor->GetOwnerNode());
  196. GE_CHECK_NOTNULL(ori_out_anchor);
  197. GE_CHECK_NOTNULL(ori_out_anchor->GetOwnerNode());
  198. auto new_name = new_out_anchor->GetOwnerNode()->GetName();
  199. auto ori_name = ori_out_anchor->GetOwnerNode()->GetName();
  200. auto dst_desc = dst_in_anchor->GetOwnerNode()->GetOpDesc();
  201. auto ori_src_name = dst_desc->GetSrcName();
  202. auto ori_input_name = dst_desc->GetInputName();
  203. std::vector<string> new_src_name;
  204. std::vector<string> new_input_name;
  205. if (ori_src_name.empty()) {
  206. new_src_name.push_back(new_name);
  207. } else {
  208. for (auto &src_name : ori_src_name) {
  209. if (src_name == ori_name) {
  210. new_src_name.push_back(new_name);
  211. } else {
  212. new_src_name.push_back(src_name);
  213. }
  214. }
  215. }
  216. if (ori_input_name.empty()) {
  217. new_input_name.push_back(new_name);
  218. } else {
  219. for (auto &input_name : ori_input_name) {
  220. if (input_name == ori_name) {
  221. new_input_name.push_back(new_name);
  222. } else {
  223. new_input_name.push_back(input_name);
  224. }
  225. }
  226. }
  227. dst_desc->SetSrcName(new_src_name);
  228. dst_desc->SetInputName(new_input_name);
  229. return GRAPH_SUCCESS;
  230. }
  231. /// Relink the offset trans op with it's former neighbor's father node.
  232. /// Note: control edge will be added to link the two offset ops, if the former op
  233. /// has in control nodes
  234. graphStatus TransOpDepthFusionPass::RelinkEdges(const OutDataAnchorPtr &new_out_anchor,
  235. const OutDataAnchorPtr &old_out_anchor,
  236. const InDataAnchorPtr &in_data_anchor) {
  237. if (new_out_anchor == nullptr || old_out_anchor == nullptr || in_data_anchor == nullptr) {
  238. GELOGE(INTERNAL_ERROR, "new_out_anchor or old_out_anchor or in_data_anchor is nullptr");
  239. return GRAPH_FAILED;
  240. }
  241. if (new_out_anchor->GetOwnerNode() == nullptr || old_out_anchor->GetOwnerNode() == nullptr ||
  242. in_data_anchor->GetOwnerNode() == nullptr) {
  243. GELOGE(INTERNAL_ERROR, "anchor's owner node is nullptr");
  244. return GRAPH_FAILED;
  245. }
  246. GE_CHK_STATUS_RET(GraphUtils::RemoveEdge(old_out_anchor, in_data_anchor), "remove edge failed");
  247. GE_CHK_STATUS_RET(GraphUtils::AddEdge(new_out_anchor, in_data_anchor), "add edge failed");
  248. GELOGD(
  249. "relink edges before remove node, remove data edge between node: %s, "
  250. "type: %s and node: %s, type: %s.",
  251. old_out_anchor->GetOwnerNode()->GetName().c_str(), old_out_anchor->GetOwnerNode()->GetType().c_str(),
  252. in_data_anchor->GetOwnerNode()->GetName().c_str(), in_data_anchor->GetOwnerNode()->GetType().c_str());
  253. GELOGD(
  254. "relink edges before remove node, add data edge between node: %s, "
  255. "type: %s and node: %s, type: %s.",
  256. new_out_anchor->GetOwnerNode()->GetName().c_str(), new_out_anchor->GetOwnerNode()->GetType().c_str(),
  257. in_data_anchor->GetOwnerNode()->GetName().c_str(), in_data_anchor->GetOwnerNode()->GetType().c_str());
  258. bool is_linked = false;
  259. auto dst_node = in_data_anchor->GetOwnerNode();
  260. auto src_node = old_out_anchor->GetOwnerNode();
  261. auto in_ctrl_nodes = dst_node->GetInControlNodes();
  262. if (!in_ctrl_nodes.empty()) {
  263. auto iter = std::find(in_ctrl_nodes.begin(), in_ctrl_nodes.end(), src_node);
  264. is_linked = iter != in_ctrl_nodes.end();
  265. }
  266. if (!src_node->GetInControlNodes().empty() && !is_linked) {
  267. auto out_ctrl_anchor = src_node->GetOutControlAnchor();
  268. auto in_ctrl_anchor = dst_node->GetInControlAnchor();
  269. GE_CHK_STATUS_RET(GraphUtils::AddEdge(out_ctrl_anchor, in_ctrl_anchor), "add edge failed");
  270. GELOGD(
  271. "relink edges before remove node, add control edge between node: %s,"
  272. " type: %s and node: %s, type: %s.",
  273. src_node->GetName().c_str(), src_node->GetType().c_str(), dst_node->GetName().c_str(),
  274. dst_node->GetType().c_str());
  275. }
  276. return GRAPH_SUCCESS;
  277. }
  278. // Remove trans op by using interface: IsolateNode & RemoveNodeWithoutRelink
  279. graphStatus TransOpDepthFusionPass::RemoveNode(const NodePtr &node, const ge::ComputeGraphPtr &graph) {
  280. if (node == nullptr || graph == nullptr) {
  281. return GRAPH_FAILED;
  282. }
  283. if (GraphUtils::IsolateNode(node, {0}) != GRAPH_SUCCESS) {
  284. GELOGE(INTERNAL_ERROR, "Isolate removed node: %s, type: %s failed", node->GetName().c_str(),
  285. node->GetType().c_str());
  286. return GRAPH_FAILED;
  287. }
  288. if (GraphUtils::RemoveNodeWithoutRelink(graph, node) != GRAPH_SUCCESS) {
  289. GELOGE(INTERNAL_ERROR, "Remove node: %s, type: %s without relink failed", node->GetName().c_str(),
  290. node->GetType().c_str());
  291. return GRAPH_FAILED;
  292. }
  293. return GRAPH_SUCCESS;
  294. }
  295. } // namespace ge

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