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

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