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

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

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