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

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

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