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_symmetry_elimination_pass.cc 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /**
  2. * Copyright 2019-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 "transop_symmetry_elimination_pass.h"
  17. #include "common/formats/utils/formats_trans_utils.h"
  18. #include "framework/common/debug/ge_log.h"
  19. #include "framework/common/util.h"
  20. #include "graph/common/transop_util.h"
  21. #include "graph/debug/ge_attr_define.h"
  22. #include "graph/utils/graph_utils.h"
  23. #include "graph/utils/node_utils.h"
  24. #include "graph/utils/type_utils.h"
  25. #include "types.h"
  26. namespace {
  27. const std::set<std::string> white_list_op{ge::TRANSPOSED, ge::RESHAPE, ge::REFORMAT, ge::CAST, ge::TRANSDATA};
  28. } // namespace
  29. namespace ge {
  30. Status TransOpSymmetryEliminationPass::Run(NodePtr &node) {
  31. GE_CHECK_NOTNULL(node);
  32. GE_CHECK_NOTNULL(node->GetOpDesc());
  33. if (white_list_op.find(node->GetType()) == white_list_op.end()) {
  34. return SUCCESS;
  35. }
  36. GELOGD("Symmetry Elimination Pass in.");
  37. for (const auto &out_anchor : node->GetAllOutDataAnchors()) {
  38. GE_CHECK_NOTNULL(out_anchor);
  39. for (const auto &peer_in_anchor : out_anchor->GetPeerInDataAnchors()) {
  40. GE_CHECK_NOTNULL(peer_in_anchor);
  41. GE_CHECK_NOTNULL(peer_in_anchor->GetOwnerNode());
  42. GE_CHECK_NOTNULL(peer_in_anchor->GetOwnerNode()->GetOpDesc());
  43. if (!CheckCanBeEliminated(node, peer_in_anchor)) {
  44. continue;
  45. }
  46. auto dst_node = peer_in_anchor->GetOwnerNode();
  47. Status ret = EliminateTransOp(node, out_anchor, dst_node, peer_in_anchor);
  48. if (ret != SUCCESS) {
  49. // if eliminate failed ,it should't break precess, so give a warning here
  50. GELOGW("Eliminate %s and %s failed, ignore current pass.", node->GetName().c_str(),
  51. dst_node->GetName().c_str());
  52. return ret;
  53. }
  54. }
  55. }
  56. GELOGD("Symmetry Elimination Pass end.");
  57. return SUCCESS;
  58. }
  59. bool TransOpSymmetryEliminationPass::CheckCanBeEliminated(const ge::NodePtr &src_node,
  60. const InDataAnchorPtr &dst_in_anchor) {
  61. auto dst_node = dst_in_anchor->GetOwnerNode();
  62. if (src_node->GetType() != dst_node->GetType()) {
  63. GELOGD("Pre node %s type %s is not equal with node %s type %s. Ignore pass.", src_node->GetName().c_str(),
  64. src_node->GetType().c_str(), dst_node->GetName().c_str(), dst_node->GetType().c_str());
  65. return false;
  66. }
  67. if (dst_in_anchor->GetIdx() != TransOpUtil::GetTransOpDataIndex(src_node)) {
  68. GELOGD("Next node %s type %s input %d is not for transform. Ignore pass.", dst_node->GetName().c_str(),
  69. dst_node->GetType().c_str(), dst_in_anchor->GetIdx());
  70. return false;
  71. }
  72. if (src_node->GetType() == ge::RESHAPE) {
  73. GE_CHECK_NOTNULL(src_node->GetOpDesc());
  74. auto unknown_dims_num = GetUnknownDimsNum(src_node->GetOpDesc()->GetInputDesc(0));
  75. if (unknown_dims_num != 0 && (unknown_dims_num == UNKNOWN_DIM_NUM || unknown_dims_num > 1)) {
  76. GELOGD(
  77. "Pre node %s is reshape op which input is dynamic shape and has more than one unknown dimension. "
  78. "Ignore pass.",
  79. src_node->GetName().c_str());
  80. return false;
  81. }
  82. } else if (src_node->GetType() == ge::TRANSPOSED) {
  83. if (!JudgeTransposeDBack2Raw(src_node, dst_node)) {
  84. GELOGD("Two Transpose op src node %s dst node %s will change the raw data. Ignore pass.",
  85. src_node->GetName().c_str(), dst_node->GetName().c_str());
  86. return false;
  87. }
  88. } else if (src_node->GetType() == ge::TRANSDATA) {
  89. auto unknown_dims_num = GetUnknownDimsNum(src_node->GetOpDesc()->GetInputDesc(0));
  90. if (unknown_dims_num == UNKNOWN_DIM_NUM) {
  91. GELOGD("Pre node %s is transdata op which input is dynamic shape and all dimension are unknown(-2). Ignore pass.",
  92. src_node->GetName().c_str());
  93. return false;
  94. }
  95. }
  96. return TransOpUtil::CheckPrecisionLoss(src_node) && DescAreSymmetry(src_node, dst_node);
  97. }
  98. bool TransOpSymmetryEliminationPass::DescAreSymmetry(const NodePtr &src_node, const NodePtr &dst_node) {
  99. const auto &src_input_desc = src_node->GetOpDesc()->MutableInputDesc(0);
  100. const auto &dst_output_desc = dst_node->GetOpDesc()->MutableOutputDesc(0);
  101. GE_CHECK_NOTNULL(src_input_desc);
  102. GE_CHECK_NOTNULL(dst_output_desc);
  103. const auto &src_input_dtype = src_input_desc->GetDataType();
  104. const auto &src_input_format = src_input_desc->GetFormat();
  105. const auto &src_input_shape = src_input_desc->GetShape().GetDims();
  106. const auto &dst_output_dtype = dst_output_desc->GetDataType();
  107. const auto &dst_output_format = dst_output_desc->GetFormat();
  108. const auto &dst_output_shape = dst_output_desc->GetShape().GetDims();
  109. bool is_symmetry = true;
  110. if (src_node->GetType() == CAST && dst_node->GetType() == CAST) {
  111. bool is_format_symmetry =
  112. (src_input_format == dst_output_format) || (dst_output_format == FORMAT_ND) || (src_input_format == FORMAT_ND);
  113. is_symmetry = (src_input_dtype == dst_output_dtype) && is_format_symmetry;
  114. } else {
  115. is_symmetry = (src_input_dtype == dst_output_dtype) && (src_input_shape == dst_output_shape) &&
  116. (src_input_format == dst_output_format);
  117. }
  118. if (!is_symmetry) {
  119. GELOGD(
  120. "Not satisfied symmetry. ignore pass.\n"
  121. "Src node %s input type: %s format: %s shape: %s, "
  122. "dst node %s output type: %s format: %s shape: %s. ",
  123. src_node->GetName().c_str(), TypeUtils::DataTypeToSerialString(src_input_dtype).c_str(),
  124. TypeUtils::FormatToSerialString(src_input_format).c_str(), formats::ShapeToString(src_input_shape).c_str(),
  125. dst_node->GetName().c_str(), TypeUtils::DataTypeToSerialString(dst_output_dtype).c_str(),
  126. TypeUtils::FormatToSerialString(dst_output_format).c_str(), formats::ShapeToString(dst_output_shape).c_str());
  127. }
  128. return is_symmetry;
  129. }
  130. int TransOpSymmetryEliminationPass::GetUnknownDimsNum(const GeTensorDesc &node_desc) {
  131. //
  132. // unknown_dims_num != 0 , is dynamic shape
  133. // unknown_dims_num = UNKNOWN_DIM_NUM , all dims are unknown
  134. // unknown_dims_num = n , n > 0 , has n dims unknown
  135. //
  136. int unknown_dims_num = 0;
  137. auto ge_shape = node_desc.GetShape();
  138. for (const auto dim : ge_shape.GetDims()) {
  139. if (dim == UNKNOWN_DIM_NUM) {
  140. return UNKNOWN_DIM_NUM;
  141. }
  142. if (dim == UNKNOWN_DIM) {
  143. ++unknown_dims_num;
  144. }
  145. }
  146. return unknown_dims_num;
  147. }
  148. bool TransOpSymmetryEliminationPass::JudgeTransposeDBack2Raw(const NodePtr &src_node, const NodePtr &dst_node) {
  149. //
  150. // A transpose to C : A---->(perm_1)---->B---->(perm_2)---->C
  151. // we want to judge A is equal with C or not
  152. // suppose A = C then:
  153. // 1. B[i] = A[perm_1[i]]
  154. // 2. C[i] = B[perm_2[i]]
  155. // 3. combine 1 and 2 then: C[i] = A[perm_1[perm_2[i]]]
  156. // which we get through 3: i = perm_1[perm_2[i]]
  157. //
  158. vector<int64_t> src_node_perm;
  159. (void)AttrUtils::GetListInt(src_node->GetOpDesc(), ge::PERMUTE_ATTR_PERM, src_node_perm);
  160. vector<int64_t> dst_node_perm;
  161. (void)AttrUtils::GetListInt(dst_node->GetOpDesc(), ge::PERMUTE_ATTR_PERM, dst_node_perm);
  162. if (src_node_perm.size() != dst_node_perm.size()) {
  163. return false;
  164. }
  165. for (size_t src_index = 0; src_index < src_node_perm.size(); ++src_index) {
  166. if (dst_node_perm[src_index] >= static_cast<int64_t>(src_node_perm.size())) {
  167. return false;
  168. }
  169. if (static_cast<int64_t>(src_index) != src_node_perm[dst_node_perm[src_index]]) {
  170. return false;
  171. }
  172. }
  173. return true;
  174. }
  175. Status TransOpSymmetryEliminationPass::EliminateTransOp(NodePtr &src_node, const OutDataAnchorPtr &src_out_anchor,
  176. NodePtr &dst_node, const InDataAnchorPtr &dst_in_anchor) {
  177. // Two transform nodes can be offset like A->T1->T2->B
  178. // 1.Unlink T1->T2
  179. auto ret = src_out_anchor->Unlink(dst_in_anchor);
  180. if (ret != GRAPH_SUCCESS) {
  181. GELOGE(FAILED, "Unlink data anchor from %s to %s.", src_node->GetName().c_str(), dst_node->GetName().c_str());
  182. return ret;
  183. }
  184. // 2.Link A->T2
  185. auto data_idx = TransOpUtil::GetTransOpDataIndex(src_node);
  186. auto in_anchor = src_node->GetInDataAnchor(data_idx);
  187. GE_CHECK_NOTNULL(in_anchor);
  188. GE_CHECK_NOTNULL(in_anchor->GetPeerOutAnchor());
  189. auto pre_normal_node = in_anchor->GetPeerOutAnchor()->GetOwnerNode();
  190. ret = GraphUtils::AddEdge(in_anchor->GetPeerOutAnchor(), dst_in_anchor);
  191. if (ret != GRAPH_SUCCESS) {
  192. GELOGE(FAILED, "Add data edge from %s to %s failed.", pre_normal_node->GetName().c_str(),
  193. dst_node->GetName().c_str());
  194. return ret;
  195. }
  196. // 3.Copy in-control/data-in-control from T1->T2
  197. ret = GraphUtils::CopyInCtrlEdges(src_node, dst_node);
  198. if (ret != GRAPH_SUCCESS) {
  199. GELOGE(FAILED, "Copy control edge from %s to %s failed.", src_node->GetName().c_str(), dst_node->GetName().c_str());
  200. return ret;
  201. }
  202. // 4.Add control edge from T1 other input to T2, like reshape second input
  203. for (const auto &in_node : src_node->GetInDataNodes()) {
  204. if (in_node->GetName() == pre_normal_node->GetName()) {
  205. continue;
  206. }
  207. ret = GraphUtils::AddEdge(in_node->GetOutControlAnchor(), dst_node->GetInControlAnchor());
  208. if (ret != GRAPH_SUCCESS) {
  209. GELOGE(FAILED, "Add control edge from %s to %s failed.", in_node->GetName().c_str(), dst_node->GetName().c_str());
  210. return ret;
  211. }
  212. }
  213. // 5.IsolateAndDelete T2, A will link to B automatically, and all control edge will also relink.
  214. ret = IsolateAndDeleteNode(dst_node, {0});
  215. if (ret != GRAPH_SUCCESS) {
  216. GELOGE(INTERNAL_ERROR, "Isolate removed node: %s, type: %s failed", dst_node->GetName().c_str(),
  217. dst_node->GetType().c_str());
  218. return ret;
  219. }
  220. GELOGI("Trans op symmetry eliminate successfully. Node %s has been removed.", dst_node->GetName().c_str());
  221. // 6.If T1 has no data out, isolate and deleted it.
  222. ret = RemoveTransOpWithoutOutput(pre_normal_node, src_node);
  223. if (ret != GRAPH_SUCCESS) {
  224. GELOGE(ret, "Isolate removed node: %s, type: %s failed", src_node->GetName().c_str(), src_node->GetType().c_str());
  225. return ret;
  226. }
  227. return SUCCESS;
  228. }
  229. Status TransOpSymmetryEliminationPass::RemoveTransOpWithoutOutput(NodePtr &pre_node, NodePtr &trans_node) {
  230. if (trans_node->GetOutDataNodesSize() == 0) {
  231. // 6.1 Copy out control to pre normal node
  232. Status ret = GraphUtils::CopyOutCtrlEdges(trans_node, pre_node);
  233. if (ret != GRAPH_SUCCESS) {
  234. GELOGE(FAILED, "Copy control edge from %s to %s failed.", trans_node->GetName().c_str(),
  235. pre_node->GetName().c_str());
  236. return ret;
  237. }
  238. // 6.2 Isolate and delete T1
  239. ret = IsolateAndDeleteNode(trans_node, {});
  240. if (ret != GRAPH_SUCCESS) {
  241. GELOGE(INTERNAL_ERROR, "Isolate removed node: %s, type: %s failed", trans_node->GetName().c_str(),
  242. trans_node->GetType().c_str());
  243. return ret;
  244. }
  245. GELOGI("Trans op symmetry eliminate successfully. Node %s has been removed.", trans_node->GetName().c_str());
  246. }
  247. return SUCCESS;
  248. }
  249. } // namespace ge

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