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.

transpose_transdata_pass.cc 9.7 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
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 "graph/passes/transpose_transdata_pass.h"
  17. #include <memory>
  18. #include <string>
  19. #include <vector>
  20. #include "common/formats/utils/formats_trans_utils.h"
  21. #include "framework/common/debug/ge_log.h"
  22. #include "graph/utils/type_utils.h"
  23. #include "graph/debug/ge_attr_define.h"
  24. #include "graph/utils/node_utils.h"
  25. #include "init/gelib.h"
  26. #include "opskernel_manager/ops_kernel_manager.h"
  27. namespace {
  28. const char *const kAttrNameSrcFormat = "src_format";
  29. } // namespace
  30. namespace ge {
  31. Status TransposeTransDataPass::Run(NodePtr &node) {
  32. if (node == nullptr) {
  33. GELOGE(PARAM_INVALID, "param [node] must not be null.");
  34. return PARAM_INVALID;
  35. }
  36. auto op_desc = node->GetOpDesc();
  37. if (op_desc == nullptr) {
  38. GELOGE(PARAM_INVALID, "OpDesc of param [node] must not be null.");
  39. return PARAM_INVALID;
  40. }
  41. if (op_desc->GetType() != TRANSPOSED) {
  42. return SUCCESS;
  43. }
  44. if (CheckOneInAndOneOutDataAnchor(node) != SUCCESS) {
  45. return FAILED;
  46. }
  47. bool is_unknown = false;
  48. auto ret = NodeUtils::GetNodeUnknownShapeStatus(*node, is_unknown);
  49. if (ret != GRAPH_SUCCESS) {
  50. GELOGW("Get node unknown status failed, node name:%s, type:%s.", node->GetName().c_str(), node->GetType().c_str());
  51. return INTERNAL_ERROR;
  52. }
  53. if (is_unknown) {
  54. GELOGI("Current node %s, type %s is unknown shape which should be skip.", node->GetName().c_str(),
  55. node->GetType().c_str());
  56. return SUCCESS;
  57. }
  58. GELOGD("[%s] TransposeTransDataPass in.", node->GetName().c_str());
  59. auto out_nodes = node->GetOutDataNodes();
  60. bool is_add_flag = false;
  61. for (auto &out_node : out_nodes) {
  62. GE_CHECK_NOTNULL(out_node);
  63. OpDescPtr out_op_desc = out_node->GetOpDesc();
  64. if (out_op_desc == nullptr) {
  65. GELOGE(FAILED, "OpDesc of out data node of [%s] must not be null.", node->GetName().c_str());
  66. return FAILED;
  67. }
  68. if (out_op_desc->GetType() != TRANSDATA) {
  69. continue;
  70. }
  71. if (CheckOneInAndOneOutDataAnchor(out_node)) {
  72. return FAILED;
  73. }
  74. if (!FusionIfNeed(op_desc, out_op_desc)) {
  75. continue;
  76. }
  77. CopyInputEdges(node, out_node);
  78. is_add_flag = true;
  79. }
  80. if (is_add_flag) {
  81. AddRePassNode(node->GetInDataNodes().at(0));
  82. }
  83. if (node->GetOutDataNodesSize() == 0) {
  84. // all output nodes of transpose has fused, delete transpose
  85. return RemoveTranspose(node);
  86. }
  87. return SUCCESS;
  88. }
  89. Status TransposeTransDataPass::CheckOneInAndOneOutDataAnchor(NodePtr &node) const {
  90. GE_CHECK_NOTNULL(node);
  91. // Trans op has one input one output data anchor
  92. uint32_t in_data_anchor_nums = node->GetAllInDataAnchorsSize();
  93. uint32_t out_data_anchor_nums = node->GetAllOutDataAnchorsSize();
  94. // Trans op has one input data node, maybe has N output data nodes
  95. uint32_t in_data_node_nums = node->GetInDataNodes().size();
  96. if (in_data_anchor_nums != 1 || out_data_anchor_nums != 1 || in_data_node_nums != 1) {
  97. GELOGE(FAILED, "[%s] %s has %u in %u out data anchor, has %u in data node.", node->GetType().c_str(),
  98. node->GetName().c_str(), in_data_anchor_nums, out_data_anchor_nums, in_data_node_nums);
  99. return FAILED;
  100. }
  101. return SUCCESS;
  102. }
  103. Status TransposeTransDataPass::RemoveTranspose(NodePtr &node) {
  104. GE_CHECK_NOTNULL(node);
  105. ComputeGraphPtr graph = node->GetOwnerComputeGraph();
  106. if (graph == nullptr) {
  107. GELOGE(FAILED, "[%s] The owner graph must not be null.", node->GetName().c_str());
  108. return FAILED;
  109. }
  110. // If delete Transpos/TransposeD, change its peer in ctrl anchor to its input node
  111. // If not delete, need do nothing
  112. auto origin_node_in = node->GetInDataNodes().at(0);
  113. GE_CHECK_NOTNULL(node->GetOutControlAnchor());
  114. for (auto &peer_anchor : node->GetOutControlAnchor()->GetPeerInControlAnchors()) {
  115. GE_CHECK_NOTNULL(origin_node_in);
  116. GE_CHECK_NOTNULL(origin_node_in->GetOutControlAnchor());
  117. GE_CHK_STATUS_RET(origin_node_in->GetOutControlAnchor()->LinkTo(peer_anchor), "link failed");
  118. }
  119. for (const auto &anchor : node->GetAllInAnchors()) {
  120. GE_CHECK_NOTNULL(anchor);
  121. anchor->UnlinkAll();
  122. }
  123. for (const auto &anchor : node->GetAllOutAnchors()) {
  124. GE_CHECK_NOTNULL(anchor);
  125. anchor->UnlinkAll();
  126. }
  127. AddNodeDeleted(node);
  128. if (GraphUtils::RemoveNodeWithoutRelink(graph, node) != GRAPH_SUCCESS) {
  129. GELOGE(FAILED, "[%s] RemoveNodeWithoutRelink failed.", node->GetName().c_str());
  130. return FAILED;
  131. }
  132. return SUCCESS;
  133. }
  134. bool TransposeTransDataPass::FusionIfNeed(OpDescPtr &op_desc, OpDescPtr &transdata_op_desc) {
  135. GE_CHECK_NOTNULL(op_desc);
  136. GE_CHECK_NOTNULL(transdata_op_desc);
  137. auto out_input_desc = transdata_op_desc->MutableInputDesc(0);
  138. GE_CHECK_NOTNULL(out_input_desc);
  139. auto out_input_format = out_input_desc->GetFormat();
  140. auto out_input_shape = out_input_desc->GetShape();
  141. auto input_desc = op_desc->MutableInputDesc(0);
  142. auto out_desc = op_desc->MutableOutputDesc(0);
  143. GE_CHECK_NOTNULL(input_desc);
  144. GE_CHECK_NOTNULL(out_desc);
  145. auto src_format = input_desc->GetFormat();
  146. auto dst_format = out_desc->GetFormat();
  147. auto &dst_shape = out_desc->MutableShape();
  148. if (dst_format != out_input_format || !formats::IsShapeEqual(dst_shape, out_input_shape) || src_format == FORMAT_ND) {
  149. GELOGD("Output of transpose isn't the same as input of transdata, or transpose input format must not be ND.");
  150. GELOGD("Transpose input format %s, output format %s shape %s. transdata in %s %s.",
  151. TypeUtils::FormatToSerialString(src_format).c_str(), TypeUtils::FormatToSerialString(dst_format).c_str(),
  152. formats::ShapeToString(dst_shape.GetDims()).c_str(),
  153. TypeUtils::FormatToSerialString(out_input_format).c_str(),
  154. formats::ShapeToString(out_input_shape.GetDims()).c_str());
  155. return false;
  156. }
  157. auto &src_shape = input_desc->MutableShape();
  158. GELOGI("Begin to fuse transpose transdata, transpose in format %s shape %s, transdata in %s %s",
  159. TypeUtils::FormatToSerialString(src_format).c_str(), formats::ShapeToString(src_shape.GetDims()).c_str(),
  160. TypeUtils::FormatToSerialString(out_input_format).c_str(),
  161. formats::ShapeToString(out_input_shape.GetDims()).c_str());
  162. // Transpose can change format and shape
  163. out_input_desc->SetFormat(src_format);
  164. out_input_desc->SetShape(src_shape);
  165. if (!TransDataCheckAccuracySupported(transdata_op_desc)) {
  166. out_input_desc->SetFormat(out_input_format);
  167. out_input_desc->SetShape(out_input_shape);
  168. return false;
  169. }
  170. // add attr to fused TransData, then will be rebuild
  171. string new_node_name = op_desc->GetName() + transdata_op_desc->GetName();
  172. transdata_op_desc->SetName(new_node_name);
  173. GE_IF_BOOL_EXEC(!AttrUtils::SetBool(transdata_op_desc, ATTR_NEED_COMPILE, true), GELOGW("set ext attr failed");
  174. return false);
  175. string format_val = TypeUtils::FormatToSerialString(src_format);
  176. GE_IF_BOOL_EXEC(!AttrUtils::SetStr(transdata_op_desc, kAttrNameSrcFormat, format_val),
  177. GELOGW("set kAttrNameSrcFormat failed");
  178. return false);
  179. GELOGI("TransposeTransDataPass, fuse to be node %s.", transdata_op_desc->GetName().c_str());
  180. return true;
  181. }
  182. void TransposeTransDataPass::CopyInputEdges(NodePtr &origin_node, NodePtr &new_node) {
  183. if (origin_node == nullptr || new_node == nullptr) {
  184. return;
  185. }
  186. InDataAnchorPtr new_in_data_anchor = new_node->GetInDataAnchor(0);
  187. if (new_in_data_anchor == nullptr || origin_node->GetInDataAnchor(0) == nullptr) {
  188. return;
  189. }
  190. OutDataAnchorPtr out_anchor = origin_node->GetInDataAnchor(0)->GetPeerOutAnchor();
  191. new_in_data_anchor->UnlinkAll();
  192. GE_IF_BOOL_EXEC(new_in_data_anchor->LinkFrom(out_anchor) != GRAPH_SUCCESS, GELOGW("Link failed"); return );
  193. // control anchor only link to control anchor
  194. GE_IF_BOOL_EXEC(
  195. GraphUtils::CopyInCtrlEdges(origin_node, new_node) != GRAPH_SUCCESS, GELOGW("Copy in ctrl edges failed"); return );
  196. }
  197. bool TransposeTransDataPass::TransDataCheckAccuracySupported(const OpDescPtr &op_desc) {
  198. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  199. if ((instance_ptr == nullptr) || (!instance_ptr->InitFlag())) {
  200. GELOGW("GELib not initialized");
  201. return false;
  202. }
  203. OpsKernelManager &ops_kernel_manager = instance_ptr->OpsKernelManagerObj();
  204. vector<OpInfo> op_infos = ops_kernel_manager.GetOpsKernelInfo(op_desc->GetType());
  205. if (op_infos.empty()) {
  206. GELOGW("Can not get op info by op type %s", op_desc->GetType().c_str());
  207. return false;
  208. }
  209. std::string unsupported_reason;
  210. for (auto &it : op_infos) {
  211. auto kernel_map = ops_kernel_manager.GetAllOpsKernelInfoStores();
  212. auto &kernel_name = it.opKernelLib;
  213. auto kernel_info_store = kernel_map.find(kernel_name);
  214. if (kernel_info_store != kernel_map.end()) {
  215. if (kernel_info_store->second->CheckAccuracySupported(op_desc, unsupported_reason, true)) {
  216. return true;
  217. }
  218. }
  219. }
  220. GELOGI("TransposeTransDataPass CheckAccuracySupported[%s] all not support, reason:%s.", op_desc->GetName().c_str(),
  221. unsupported_reason.c_str());
  222. return false;
  223. }
  224. } // namespace ge

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