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

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