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.

cast_translate_pass.cc 12 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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/cast_translate_pass.h"
  17. #include <memory>
  18. #include <string>
  19. #include <vector>
  20. #include "framework/common/debug/ge_log.h"
  21. #include "framework/common/ge_inner_error_codes.h"
  22. #include "graph/common/omg_util.h"
  23. #include "graph/debug/ge_attr_define.h"
  24. #include "graph/passes/pass_utils.h"
  25. #include "graph/utils/node_utils.h"
  26. #include "graph/utils/type_utils.h"
  27. #include "init/gelib.h"
  28. #include "opskernel_manager/ops_kernel_manager.h"
  29. namespace ge {
  30. bool CastTranslatePass::CheckInAndOutDataAnchor(NodePtr &node) const {
  31. if (node == nullptr) {
  32. GELOGE(FAILED, "parameter is null.");
  33. return false;
  34. }
  35. if (node->GetOpDesc() == nullptr) {
  36. GELOGW("Param [node] op desc is null.");
  37. return false;
  38. }
  39. auto in_anchors = node->GetAllInDataAnchors();
  40. auto out_anchors = node->GetAllOutDataAnchors();
  41. // Cast|Translate has one input one output data anchor
  42. if (in_anchors.size() != 1 || out_anchors.size() != 1) {
  43. return false;
  44. }
  45. return true;
  46. }
  47. bool CastTranslatePass::IsCastNode(NodePtr &node) const {
  48. std::string original_type;
  49. GE_IF_BOOL_EXEC(GetOriginalType(node, original_type) != SUCCESS, GELOGW("get original type failed"); return false);
  50. return (original_type == CAST);
  51. }
  52. bool CastTranslatePass::IsTranslateNode(NodePtr &node) const {
  53. std::string original_type;
  54. GE_IF_BOOL_EXEC(GetOriginalType(node, original_type) != SUCCESS, GELOGW("get original type failed"); return false);
  55. return (original_type == TRANSLATE);
  56. }
  57. bool CastTranslatePass::IsSameCastOrTranslate(NodePtr &node, NodePtr &base_node) const {
  58. GE_IF_BOOL_EXEC(node == nullptr, GELOGW("node is null."); return false);
  59. GE_IF_BOOL_EXEC(base_node == nullptr, GELOGW("base_node is null."); return false);
  60. auto op_desc = node->GetOpDesc();
  61. GE_IF_BOOL_EXEC(op_desc == nullptr, return false);
  62. auto base_op_desc = base_node->GetOpDesc();
  63. GE_IF_BOOL_EXEC(base_op_desc == nullptr, return false);
  64. auto in_desc = op_desc->MutableInputDesc(0);
  65. auto out_desc = op_desc->MutableOutputDesc(0);
  66. auto base_in_desc = base_op_desc->MutableInputDesc(0);
  67. auto base_out_desc = base_op_desc->MutableOutputDesc(0);
  68. GE_IF_BOOL_EXEC(in_desc == nullptr, GELOGW("in_desc is null."); return false);
  69. GE_IF_BOOL_EXEC(out_desc == nullptr, GELOGW("out_desc is null."); return false);
  70. GE_IF_BOOL_EXEC(base_in_desc == nullptr, GELOGW("base_in_desc is null."); return false);
  71. GE_IF_BOOL_EXEC(base_out_desc == nullptr, GELOGW("base_out_desc is null."); return false);
  72. if (in_desc->GetDataType() == base_in_desc->GetDataType() &&
  73. out_desc->GetDataType() == base_out_desc->GetDataType() && in_desc->GetFormat() == base_in_desc->GetFormat() &&
  74. out_desc->GetFormat() == base_out_desc->GetFormat()) {
  75. return true;
  76. }
  77. GELOGD("Output node [%s] isn't the same Cast or Translate.", node->GetName().c_str());
  78. return false;
  79. }
  80. bool CastTranslatePass::IsNodeNeedOptimize(NodePtr &node) const {
  81. if (CheckInAndOutDataAnchor(node) && (IsCastNode(node) || IsTranslateNode(node))) {
  82. return true;
  83. }
  84. return false;
  85. }
  86. bool CastTranslatePass::CheckDstNode(NodePtr &out_node, bool &is_src_cast) const {
  87. return (CheckInAndOutDataAnchor(out_node) &&
  88. ((!is_src_cast && IsCastNode(out_node)) || (is_src_cast && IsTranslateNode(out_node))));
  89. }
  90. bool CastTranslatePass::IsNextNodeNeedOptimize(NodePtr &node, bool &is_src_cast) const {
  91. GE_IF_BOOL_EXEC(node == nullptr, GELOGW("cast_node is null."); return false);
  92. const std::string &node_name = node->GetName();
  93. auto out_data_nodes = node->GetOutDataNodes();
  94. if (out_data_nodes.empty()) {
  95. return false;
  96. }
  97. auto &out_node = out_data_nodes.at(0);
  98. bool is_first = true;
  99. // Cast-->all Translate; Translate-->all Cast
  100. for (auto &out_data_node : out_data_nodes) {
  101. if (out_data_node == nullptr) {
  102. continue;
  103. }
  104. if (CheckDstNode(out_data_node, is_src_cast) && (is_first || IsSameCastOrTranslate(out_data_node, out_node))) {
  105. is_first = false;
  106. continue;
  107. }
  108. GELOGD("[%s] Output node is %s, can't optimize.", node_name.c_str(), out_data_node->GetType().c_str());
  109. return false;
  110. }
  111. GELOGD("[%s] %zu dst nodes have the same input and output.", node_name.c_str(), out_data_nodes.size());
  112. return true;
  113. }
  114. bool CastTranslatePass::IsOpSupportedOptimize(NodePtr &cast_node, NodePtr &trans_node, bool &is_src_cast) {
  115. GE_IF_BOOL_EXEC(cast_node == nullptr, GELOGW("cast_node is null."); return false);
  116. GE_IF_BOOL_EXEC(trans_node == nullptr, GELOGW("trans_node is null."); return false);
  117. OpDescPtr trans_op_desc = trans_node->GetOpDesc();
  118. GE_IF_BOOL_EXEC(trans_op_desc == nullptr, GELOGW("trans_op_desc is null."); return false);
  119. // backup datatype
  120. const auto &trans_op_indesc = trans_op_desc->MutableInputDesc(0);
  121. const auto &trans_op_outdesc = trans_op_desc->MutableOutputDesc(0);
  122. GE_CHECK_NOTNULL_EXEC(trans_op_indesc, return false);
  123. GE_CHECK_NOTNULL_EXEC(trans_op_outdesc, return false);
  124. DataType trans_in_datatype = trans_op_indesc->GetDataType();
  125. DataType trans_out_datatype = trans_op_outdesc->GetDataType();
  126. auto cast_op_desc = cast_node->GetOpDesc();
  127. GE_IF_BOOL_EXEC(cast_op_desc == nullptr, GELOGW("cast_op_desc is null."); return false);
  128. const auto &cast_op_indesc = cast_op_desc->MutableInputDesc(0);
  129. const auto &cast_op_outdesc = cast_op_desc->MutableOutputDesc(0);
  130. GE_CHECK_NOTNULL_EXEC(cast_op_indesc, return false);
  131. GE_CHECK_NOTNULL_EXEC(cast_op_outdesc, return false);
  132. DataType cast_in_datatype = cast_op_indesc->GetDataType();
  133. DataType cast_out_datatype = cast_op_outdesc->GetDataType();
  134. GELOGI("CastTranslatePass, cast in %s out %s, translate in %s out %s.",
  135. TypeUtils::DataTypeToSerialString(cast_in_datatype).c_str(),
  136. TypeUtils::DataTypeToSerialString(cast_out_datatype).c_str(),
  137. TypeUtils::DataTypeToSerialString(trans_in_datatype).c_str(),
  138. TypeUtils::DataTypeToSerialString(trans_out_datatype).c_str());
  139. if (is_src_cast) {
  140. // A-->Cast-->Translate
  141. // change Translate input datatype to be the input of Cast
  142. // then delete Cast
  143. // [MutableInputDesc guarantees non empty throughout the process]
  144. trans_op_indesc->SetDataType(cast_in_datatype);
  145. } else {
  146. // Translate-->Cast-->A
  147. // change Translate output datatype to be the output of Cast
  148. // then delete Cast
  149. // [MutableInputDesc guarantees non empty throughout the process]
  150. trans_op_outdesc->SetDataType(cast_out_datatype);
  151. }
  152. if (!TranslateCheckAccuracySupported(trans_op_desc)) {
  153. if (is_src_cast) {
  154. trans_op_desc->MutableInputDesc(0)->SetDataType(trans_in_datatype);
  155. } else {
  156. trans_op_desc->MutableOutputDesc(0)->SetDataType(trans_out_datatype);
  157. }
  158. GELOGW("CheckAccuracySupported fail, don't delete Cast[%s].", cast_node->GetName().c_str());
  159. return false;
  160. }
  161. if (is_src_cast) {
  162. GE_IF_BOOL_EXEC(!AttrUtils::SetInt(trans_op_desc, ATTR_NAME_INPUT_DATATYPE, static_cast<int64_t>(cast_in_datatype)),
  163. GELOGW("set ATTR_NAME_INPUT_DATATYPE failed");
  164. return false);
  165. } else {
  166. GE_IF_BOOL_EXEC(
  167. !AttrUtils::SetInt(trans_op_desc, ATTR_NAME_OUTPUT_DATATYPE, static_cast<int64_t>(cast_out_datatype)),
  168. GELOGW("set ATTR_NAME_INPUT_DATATYPE failed");
  169. return false);
  170. }
  171. GELOGI("CastTranslatePass, translate in %d out %d.", trans_op_indesc->GetDataType(), trans_op_outdesc->GetDataType());
  172. return true;
  173. }
  174. bool CastTranslatePass::CheckOpSupportOptimize(NodePtr &node, bool &is_src_cast) {
  175. GE_IF_BOOL_EXEC(node == nullptr, GELOGE(FAILED, "node is null."); return false);
  176. auto out_node = node->GetOutDataNodes().at(0);
  177. // N dst nodes have the same datatype and format, check the first node
  178. if (is_src_cast) {
  179. return IsOpSupportedOptimize(node, out_node, is_src_cast);
  180. } else {
  181. return IsOpSupportedOptimize(out_node, node, is_src_cast);
  182. }
  183. }
  184. Status CastTranslatePass::Run(NodePtr &node) {
  185. GE_CHECK_NOTNULL(node);
  186. bool is_src_cast = IsCastNode(node);
  187. if (!IsNodeNeedOptimize(node) || !IsNextNodeNeedOptimize(node, is_src_cast)) {
  188. return SUCCESS;
  189. }
  190. GELOGI("CastTranslatePass, optimize %s.", node->GetName().c_str());
  191. if (CheckOpSupportOptimize(node, is_src_cast)) {
  192. if (is_src_cast) {
  193. if (FuseDstNTranslates(node) != SUCCESS) {
  194. return FAILED;
  195. }
  196. return IsolateAndDeleteNode(node, {0});
  197. } else {
  198. auto out_data_nodes = node->GetOutDataNodes();
  199. for (auto &out_data_node : out_data_nodes) {
  200. if (out_data_node == nullptr) {
  201. continue;
  202. }
  203. if (IsolateAndDeleteNode(out_data_node, {0}) != SUCCESS) {
  204. return FAILED;
  205. }
  206. }
  207. }
  208. }
  209. return SUCCESS;
  210. }
  211. Status CastTranslatePass::FuseDstNTranslates(NodePtr &node) {
  212. GE_CHECK_NOTNULL(node);
  213. auto out_data_nodes = node->GetOutDataNodes();
  214. size_t nums = out_data_nodes.size();
  215. if (nums == 1) {
  216. return SUCCESS;
  217. }
  218. auto &base_node = out_data_nodes.at(0);
  219. GE_CHECK_NOTNULL(base_node);
  220. for (size_t i = 1; i < nums; i++) {
  221. auto &out_data_node = out_data_nodes.at(i);
  222. GE_CHECK_NOTNULL(out_data_node);
  223. AddRePassNodesWithInOut(out_data_node);
  224. // Has checked nodes only has one in data anchor one out data anchor
  225. GE_CHK_STATUS_RET(NodeUtils::MoveOutputEdges(out_data_node, base_node), "move out put edge failed");
  226. // Relink in control anchor, delete in data anchor
  227. auto in_ctr_anchor = out_data_node->GetInControlAnchor();
  228. GE_CHECK_NOTNULL(in_ctr_anchor);
  229. for (const auto &peer_anchor : in_ctr_anchor->GetPeerOutControlAnchors()) {
  230. GE_CHECK_NOTNULL(base_node->GetInControlAnchor());
  231. GE_CHK_STATUS_RET(base_node->GetInControlAnchor()->LinkFrom(peer_anchor), "link from peer anchor failed");
  232. }
  233. in_ctr_anchor->UnlinkAll();
  234. out_data_node->GetAllInDataAnchors().at(0)->UnlinkAll();
  235. ComputeGraphPtr graph = out_data_node->GetOwnerComputeGraph();
  236. GE_CHECK_NOTNULL(graph);
  237. if (GraphUtils::RemoveNodeWithoutRelink(graph, out_data_node) != SUCCESS) {
  238. GELOGE(FAILED, "[%s] RemoveNodeWithoutRelink failed.", out_data_node->GetName().c_str());
  239. return FAILED;
  240. }
  241. AddNodeDeleted(out_data_node);
  242. }
  243. return SUCCESS;
  244. }
  245. bool CastTranslatePass::TranslateCheckAccuracySupported(const OpDescPtr &op_desc) {
  246. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  247. if ((instance_ptr == nullptr) || (!instance_ptr->InitFlag())) {
  248. GELOGW("GE is not initialized or is finalized.");
  249. return false;
  250. }
  251. OpsKernelManager &ops_kernel_manager = instance_ptr->OpsKernelManagerObj();
  252. GE_IF_BOOL_EXEC(op_desc == nullptr, GELOGE(FAILED, "Opdesc is nullptr"); return false);
  253. vector<OpInfo> op_infos = ops_kernel_manager.GetOpsKernelInfo(op_desc->GetType());
  254. if (op_infos.empty()) {
  255. GELOGI("Can not get op info by op type %s", op_desc->GetType().c_str());
  256. return false;
  257. }
  258. std::string unsupported_reason;
  259. for (auto &it : op_infos) {
  260. auto kernel_map = ops_kernel_manager.GetAllOpsKernelInfoStores();
  261. auto &kernel_name = it.opKernelLib;
  262. auto kernel_info_store = kernel_map.find(kernel_name);
  263. if (kernel_info_store != kernel_map.end()) {
  264. if (kernel_info_store->second != nullptr &&
  265. kernel_info_store->second->CheckAccuracySupported(op_desc, unsupported_reason)) {
  266. return true;
  267. }
  268. }
  269. }
  270. GELOGI("CastTranslatePass CheckAccuracySupported[%s] fail.", op_desc->GetName().c_str());
  271. return false;
  272. }
  273. } // namespace ge

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