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.

pass_utils.cc 17 kB

5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 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
4 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
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 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
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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/pass_utils.h"
  17. #include <climits>
  18. #include <memory>
  19. #include <queue>
  20. #include <string>
  21. #include <vector>
  22. #include "framework/common/debug/ge_log.h"
  23. #include "framework/common/ge_inner_error_codes.h"
  24. #include "common/ge/ge_util.h"
  25. #include "framework/common/op/ge_op_utils.h"
  26. #include "framework/common/types.h"
  27. #include "common/omg_util.h"
  28. #include "graph/debug/ge_attr_define.h"
  29. #include "graph/ge_tensor.h"
  30. #include "graph/manager/graph_var_manager.h"
  31. #include "graph/utils/graph_utils.h"
  32. #include "graph/utils/op_desc_utils.h"
  33. #include "graph/utils/tensor_utils.h"
  34. #include "graph/utils/type_utils.h"
  35. #include "graph/utils/node_utils.h"
  36. #include "common/formats/utils/formats_trans_utils.h"
  37. namespace ge {
  38. Status PassUtils::ConstructTensorDescWithData(const GeTensorDesc &out_desc, std::vector<int64_t> &data,
  39. std::vector<GeTensorPtr> &v_output, const bool scalar_output) {
  40. Status ret = SUCCESS;
  41. const uint32_t dim_size = static_cast<uint32_t>(data.size());
  42. DataType data_type = out_desc.GetDataType();
  43. if (data_type == DT_INT32) {
  44. unique_ptr<int32_t[]> buf(new (std::nothrow) int32_t[dim_size]());
  45. if (buf == nullptr) {
  46. REPORT_CALL_ERROR("E19999", "New buffer failed, size:%u", dim_size);
  47. GELOGE(MEMALLOC_FAILED, "[New][Buffer] failed, size:%u", dim_size);
  48. return MEMALLOC_FAILED;
  49. }
  50. for (uint32_t i = 0; i < dim_size; i++) {
  51. if (data[i] >= INT_MAX) {
  52. REPORT_CALL_ERROR("E19999", "Param data:%s will overflow after multi", formats::JoinToString(data).c_str());
  53. GELOGE(PARAM_INVALID, "[Check][Param] int32 overflow, data[%u]:%ld", i, data[i]);
  54. return PARAM_INVALID;
  55. }
  56. buf[i] = static_cast<int32_t>(data[i]);
  57. }
  58. ret = ConstructTensorDescWithData(out_desc, buf.get(), dim_size, v_output, scalar_output);
  59. } else if (data_type == DT_INT64) {
  60. unique_ptr<int64_t[]> buf(new (std::nothrow) int64_t[dim_size]());
  61. if (buf == nullptr) {
  62. REPORT_CALL_ERROR("E19999", "New buffer failed, size:%u", dim_size);
  63. GELOGE(MEMALLOC_FAILED, "[New][Buffer] failed, size:%u", dim_size);
  64. return MEMALLOC_FAILED;
  65. }
  66. for (uint32_t i = 0; i < dim_size; i++) {
  67. buf[i] = data[i];
  68. }
  69. ret = ConstructTensorDescWithData(out_desc, buf.get(), dim_size, v_output, scalar_output);
  70. } else {
  71. REPORT_CALL_ERROR("E19999", "Only support DT_INT32 and DT_INT64. Input data_type:%s not support",
  72. formats::JoinToString(data).c_str());
  73. GELOGE(PARAM_INVALID, "[Check][Param] Only support DT_INT32 and DT_INT64. data_type:%s not support",
  74. TypeUtils::DataTypeToSerialString(data_type).c_str());
  75. return PARAM_INVALID;
  76. }
  77. if (ret != SUCCESS) {
  78. GELOGE(ret, "[Get][ShapeTensor] failed, ret:%u.", ret);
  79. return ret;
  80. }
  81. return SUCCESS;
  82. }
  83. template <typename T>
  84. Status PassUtils::ConstructTensorDescWithData(const GeTensorDesc &out_desc, T *buf, uint32_t len,
  85. std::vector<GeTensorPtr> &v_output, const bool scalar_output) {
  86. // construct TensorDesc
  87. GeShape out_shape = (scalar_output ? GeShape() : GeShape({len}));
  88. GeTensorDesc output_tensor_desc(out_desc);
  89. output_tensor_desc.SetShape(out_shape);
  90. GeTensorPtr output_tensor_ptr = MakeShared<GeTensor>(
  91. output_tensor_desc, reinterpret_cast<uint8_t *>(buf), sizeof(T) * len);
  92. if (output_tensor_ptr == nullptr) {
  93. REPORT_CALL_ERROR("E19999", "New GeTensor failed");
  94. GELOGE(MEMALLOC_FAILED, "[New][GeTensor] failed");
  95. return MEMALLOC_FAILED;
  96. }
  97. v_output.push_back(output_tensor_ptr);
  98. return SUCCESS;
  99. }
  100. bool PassUtils::IsConstant(const ConstNodePtr &node) {
  101. if (node == nullptr) {
  102. REPORT_INNER_ERROR("E19999", "Param node is nullptr, check invalid");
  103. GELOGE(PARAM_INVALID, "[Check][Param] node is nullptr");
  104. return false;
  105. }
  106. auto src_node_type = node->GetType();
  107. bool is_constant = (src_node_type == CONSTANT) || (src_node_type == CONSTANTOP);
  108. return is_constant;
  109. }
  110. Status PassUtils::SetOutNodeWeight(const OutDataAnchorPtr &out_data_anchor, const NodePtr &src_node) {
  111. GE_IF_BOOL_EXEC(src_node == nullptr,
  112. REPORT_INNER_ERROR("E19999", "Param src_node is nullptr, check invalid");
  113. GELOGE(PARAM_INVALID, "[Check][Param] src_node is nullptr"); return PARAM_INVALID);
  114. if (!IsConstant(src_node)) {
  115. return SUCCESS;
  116. }
  117. auto weights = OpDescUtils::MutableWeights(src_node);
  118. if (weights.empty()) {
  119. REPORT_INNER_ERROR("E19999", "Weight of node:%s(%s) is empty, check invalid",
  120. src_node->GetName().c_str(), src_node->GetType().c_str());
  121. GELOGE(PARAM_INVALID, "[Check][Param] Weight of node:%s(%s) is empty",
  122. src_node->GetName().c_str(), src_node->GetType().c_str());
  123. return PARAM_INVALID;
  124. }
  125. auto weight = weights.at(0);
  126. auto src_in_ctrl = src_node->GetInControlAnchor();
  127. if ((src_in_ctrl == nullptr) || (out_data_anchor == nullptr)) {
  128. REPORT_INNER_ERROR("E19999", "Param out_data_anchor or in control anchor in Param src_node:%s(%s) is nullptr, "
  129. "check invalid", src_node->GetName().c_str(), src_node->GetType().c_str());
  130. GELOGE(FAILED, "[Check][Param] Param out_data_anchor or in control anchor in Param src_node:%s(%s) is nullptr",
  131. src_node->GetName().c_str(), src_node->GetType().c_str());
  132. return FAILED;
  133. }
  134. auto src_out_control_anchors = src_in_ctrl->GetPeerAnchors();
  135. for (const auto &dst_in_data : out_data_anchor->GetPeerInDataAnchors()) {
  136. auto dst_node = dst_in_data->GetOwnerNode();
  137. auto dst_op_desc = dst_node->GetOpDesc();
  138. if (dst_op_desc == nullptr) {
  139. continue;
  140. }
  141. std::vector<bool> is_input_const = dst_op_desc->GetIsInputConst();
  142. auto input_index = static_cast<size_t>(dst_in_data->GetIdx());
  143. if (input_index < is_input_const.size()) {
  144. is_input_const[input_index] = true;
  145. dst_op_desc->SetIsInputConst(is_input_const);
  146. }
  147. GE_CHK_GRAPH_STATUS_RET(GraphUtils::RemoveEdge(out_data_anchor, dst_in_data),
  148. "[Remove][Edge] between %s and %s failed",
  149. out_data_anchor->GetOwnerNode()->GetName().c_str(),
  150. dst_in_data->GetOwnerNode()->GetName().c_str());
  151. graphStatus ret = OpDescUtils::AddConstOpToAnchor(dst_in_data, weight);
  152. if (ret != SUCCESS) {
  153. return ret;
  154. }
  155. GE_CHECK_NOTNULL(dst_in_data->GetPeerOutAnchor());
  156. auto dynamic_const_node = dst_in_data->GetPeerOutAnchor()->GetOwnerNode();
  157. GE_CHECK_NOTNULL(dynamic_const_node->GetOpDesc());
  158. dynamic_const_node->GetOpDesc()->SetType(src_node->GetType());
  159. // restore control inputs to dynamically added constant ops, if any
  160. for (const auto &src_out_control_anchor : src_out_control_anchors) {
  161. GE_CHK_GRAPH_STATUS_RET(GraphUtils::AddEdge(src_out_control_anchor, dynamic_const_node->GetInControlAnchor()),
  162. "[Add][ControlEdge] between %s and %s failed",
  163. src_out_control_anchor->GetOwnerNode()->GetName().c_str(),
  164. dynamic_const_node->GetName().c_str());
  165. }
  166. }
  167. /// Before:
  168. /// Op1 - - - > Constant ------> Switch - - - > Op2
  169. /// After:
  170. /// Op1 - - - > Op2
  171. for (const auto &dst_in_ctrl : out_data_anchor->GetPeerInControlAnchors()) {
  172. for (const auto &src_out_control_anchor : src_out_control_anchors) {
  173. GE_CHK_GRAPH_STATUS_RET(GraphUtils::AddEdge(src_out_control_anchor, dst_in_ctrl),
  174. "[Add][ControlEdge] between %s and %s failed",
  175. src_out_control_anchor->GetOwnerNode()->GetName().c_str(),
  176. dst_in_ctrl->GetOwnerNode()->GetName().c_str());
  177. }
  178. }
  179. return SUCCESS;
  180. }
  181. Status PassUtils::RemoveBranch(const NodePtr &node, std::vector<NodePtr> &delete_nodes,
  182. std::vector<NodePtr> &end_nodes) {
  183. if (node == nullptr) {
  184. REPORT_INNER_ERROR("E19999", "Param node is nullptr, check invalid");
  185. GELOGE(FAILED, "[Check][Param] parameter node is nullptr.");
  186. return FAILED;
  187. }
  188. GELOGI("Remove branch starting from node %s", node->GetName().c_str());
  189. std::queue<NodePtr> search_queue;
  190. search_queue.push(node);
  191. while (!search_queue.empty()) {
  192. const NodePtr src_node = search_queue.front();
  193. if (src_node == nullptr) {
  194. continue;
  195. }
  196. delete_nodes.push_back(src_node);
  197. search_queue.pop();
  198. for (const auto &src_out_anchor : src_node->GetAllOutAnchors()) {
  199. for (const auto &dst_in_anchor : src_out_anchor->GetPeerAnchors()) {
  200. if (dst_in_anchor == nullptr) {
  201. continue;
  202. }
  203. auto dst_node = dst_in_anchor->GetOwnerNode();
  204. std::string node_type;
  205. GE_CHK_STATUS_RET(GetOriginalType(dst_node, node_type),
  206. "[Get][OriginalType] of node:%s failed", dst_node->GetName().c_str());
  207. if (node_type == NETOUTPUT) {
  208. if (dst_in_anchor->IsTypeOf<InDataAnchor>()) {
  209. REPORT_INNER_ERROR("E19999", "Node:%s(%s) nactive branch connected to NetOutput with data anchor, "
  210. "check invalid", node->GetName().c_str(), node->GetType().c_str());
  211. GELOGE(INTERNAL_ERROR, "[Check][Param] [%s] Inactive branch connected to NetOutput with data anchor.",
  212. node->GetName().c_str());
  213. return INTERNAL_ERROR;
  214. } else {
  215. // safe to unlink control edges
  216. GE_CHK_GRAPH_STATUS_RET(GraphUtils::RemoveEdge(src_out_anchor, dst_in_anchor),
  217. "[Remove][Edge] between %s and %s failed",
  218. src_out_anchor->GetOwnerNode()->GetName().c_str(),
  219. dst_in_anchor->GetOwnerNode()->GetName().c_str());
  220. end_nodes.push_back(dst_node);
  221. }
  222. } else if (node_type == MERGE) {
  223. /// Unlink connection between the inactive branch and Merge/NetOutput.
  224. /// The removal of inactive nodes will be handled in PrunePass
  225. GE_CHK_GRAPH_STATUS_RET(GraphUtils::RemoveEdge(src_out_anchor, dst_in_anchor),
  226. "[Remove][Edge] between %s and %s failed",
  227. src_out_anchor->GetOwnerNode()->GetName().c_str(),
  228. dst_in_anchor->GetOwnerNode()->GetName().c_str());
  229. end_nodes.push_back(dst_node);
  230. GELOGD("Reach the end merge node %s, the branch removing stop", dst_node->GetName().c_str());
  231. } else {
  232. search_queue.push(dst_node);
  233. }
  234. }
  235. }
  236. }
  237. return SUCCESS;
  238. }
  239. NodePtr PassUtils::GetInDataNode(const ConstNodePtr &node, int index) {
  240. if (node == nullptr) {
  241. return nullptr;
  242. }
  243. auto in_data_anchor = node->GetInDataAnchor(index);
  244. if (in_data_anchor == nullptr) {
  245. return nullptr;
  246. }
  247. auto peer_out_data_anchor = in_data_anchor->GetPeerOutAnchor();
  248. if (peer_out_data_anchor == nullptr) {
  249. return nullptr;
  250. }
  251. auto src_node = peer_out_data_anchor->GetOwnerNode();
  252. return src_node;
  253. }
  254. NodePtr PassUtils::GetInNodeCrossSubgraphByIndex(const ConstNodePtr &node, int index) {
  255. auto src_node = GetInDataNode(node, index);
  256. return NodeUtils::GetInNodeCrossSubgraph(src_node);
  257. }
  258. bool PassUtils::IsNeedTrainIteFlowCtrl(const ComputeGraphPtr &compute_graph) {
  259. if (compute_graph == nullptr) {
  260. return false;
  261. }
  262. if (compute_graph->GetParentGraph() != nullptr) {
  263. GELOGI("Subgraph %s no need flow ctrl.", compute_graph->GetName().c_str());
  264. return false;
  265. }
  266. if (GraphUtils::IsUnknownShapeGraph(compute_graph)) {
  267. GELOGI("Unknown shape graph %s no need flow ctrl.", compute_graph->GetName().c_str());
  268. return false;
  269. }
  270. if (!ge::VarManager::Instance(compute_graph->GetSessionID())->IsVarExist(NODE_NAME_FLOWCTRL_LOOP_PER_ITER)) {
  271. return false;
  272. }
  273. return compute_graph->GetNeedIteration();
  274. }
  275. int PassUtils::GetUniqueInDataAnchorIndex(const NodePtr &node_ptr) {
  276. const int invalid_index = -1;
  277. if (node_ptr == nullptr) {
  278. REPORT_INNER_ERROR("E19999", "Param node_ptr is nullptr, check invalid");
  279. GELOGE(INTERNAL_ERROR, "[Check][Param] node is nullptr");
  280. return invalid_index;
  281. }
  282. for (const auto &in_anchor : node_ptr->GetAllInDataAnchors()) {
  283. if ((in_anchor != nullptr) && (in_anchor->GetPeerOutAnchor() != nullptr) &&
  284. (in_anchor->GetPeerOutAnchor()->GetOwnerNode() != nullptr)) {
  285. return (in_anchor->GetIdx());
  286. }
  287. }
  288. REPORT_INNER_ERROR("E19999", "Failed to find in data anchor of node:%s(%s) with a valid peer out node",
  289. node_ptr->GetName().c_str(), node_ptr->GetType().c_str());
  290. GELOGE(INTERNAL_ERROR, "[Check][Param] Failed to find in data anchor of node:%s(%s) with a valid peer out node",
  291. node_ptr->GetName().c_str(), node_ptr->GetType().c_str());
  292. return invalid_index;
  293. }
  294. Status PassUtils::UnlinkNodeWithControlCopy(NodePtr &node, int index) {
  295. if (node == nullptr) {
  296. REPORT_INNER_ERROR("E19999", "Param node is nullptr, check invalid");
  297. GELOGE(PARAM_INVALID, "[Check][Param] node is nullptr.");
  298. return PARAM_INVALID;
  299. }
  300. auto in_data_anchor = node->GetInDataAnchor(index);
  301. if (in_data_anchor == nullptr) {
  302. GELOGW("[%s] in_data_anchor is null with index [%d].", node->GetName().c_str(), index);
  303. return SUCCESS;
  304. }
  305. auto out_data_anchor = in_data_anchor->GetPeerOutAnchor();
  306. if (out_data_anchor == nullptr) {
  307. REPORT_INNER_ERROR("E19999", "Index:%d in data anchor of node:%s(%s), its peer anchor is nullptr, check invalid",
  308. index, node->GetName().c_str(), node->GetType().c_str());
  309. GELOGE(FAILED, "[Get][PeerOutAnchor] failed, Index:%d in data anchor of node:%s(%s), its peer anchor is nullptr.",
  310. index, node->GetName().c_str(), node->GetType().c_str());
  311. return FAILED;
  312. }
  313. // Remove link between father_node and node
  314. in_data_anchor->UnlinkAll();
  315. auto father_node = out_data_anchor->GetOwnerNode();
  316. // link father_node's in control nodes to node
  317. if (GraphUtils::CopyInCtrlEdges(father_node, node) != GRAPH_SUCCESS) {
  318. REPORT_CALL_ERROR("E19999", "Copy in control edge from node:%s(%s) to node:%s(%s) failed",
  319. father_node->GetName().c_str(), father_node->GetType().c_str(),
  320. node->GetName().c_str(), node->GetType().c_str());
  321. GELOGE(FAILED, "[Copy][InCtrlEdges] from node:%s(%s) to node:%s(%s) failed",
  322. father_node->GetName().c_str(), father_node->GetType().c_str(),
  323. node->GetName().c_str(), node->GetType().c_str());
  324. return FAILED;
  325. }
  326. return SUCCESS;
  327. }
  328. Status PassUtils::RemoveInactiveBranchToMerge(const OutDataAnchorPtr &inactive_output_anchor,
  329. std::vector<NodePtr> &delete_nodes, std::vector<NodePtr> &end_nodes) {
  330. if (inactive_output_anchor == nullptr) {
  331. REPORT_INNER_ERROR("E19999", "Param inactive_output_anchor is nullptr, check invalid");
  332. GELOGE(FAILED, "[Check][Param] parameter inactive_output_anchor is nullptr.");
  333. return FAILED;
  334. }
  335. for (const auto &dst_anchor : inactive_output_anchor->GetPeerAnchors()) {
  336. if (dst_anchor == nullptr) {
  337. continue;
  338. }
  339. auto dst_node = dst_anchor->GetOwnerNode();
  340. if (dst_node != nullptr) {
  341. std::string dst_node_type;
  342. GE_CHK_STATUS_RET(GetOriginalType(dst_node, dst_node_type),
  343. "[Get][OriginalType] of node:%s failed", dst_node->GetName().c_str());
  344. if (dst_node_type == MERGE) {
  345. GELOGD("[%s] Switch connected directly to Merge", inactive_output_anchor->GetOwnerNode()->GetName().c_str());
  346. GE_CHK_GRAPH_STATUS_RET(GraphUtils::RemoveEdge(inactive_output_anchor, dst_anchor),
  347. "[Remove][Edge] between %s and %s failed",
  348. inactive_output_anchor->GetOwnerNode()->GetName().c_str(),
  349. dst_node->GetName().c_str());
  350. continue;
  351. }
  352. Status ret = PassUtils::RemoveBranch(dst_node, delete_nodes, end_nodes);
  353. if (ret != SUCCESS) {
  354. return ret;
  355. }
  356. }
  357. }
  358. return SUCCESS;
  359. }
  360. } // namespace ge

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