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

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
5 years ago
5 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
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
4 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
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
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 "common/ge_inner_error_codes.h"
  24. #include "common/ge/ge_util.h"
  25. #include "common/op/ge_op_utils.h"
  26. #include "common/types.h"
  27. #include "graph/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 "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 failed");
  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, "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 failed");
  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, "Only support DT_INT32 and DT_INT64. data_type:%s",
  74. TypeUtils::DataTypeToSerialString(data_type).c_str());
  75. return PARAM_INVALID;
  76. }
  77. if (ret != SUCCESS) {
  78. GELOGE(ret, "GetShapeTensor failed.");
  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, "Make shared 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, "node is null");
  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, "src_node is null"); 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. return PARAM_INVALID;
  122. }
  123. auto weight = weights.at(0);
  124. auto src_in_ctrl = src_node->GetInControlAnchor();
  125. if ((src_in_ctrl == nullptr) || (out_data_anchor == nullptr)) {
  126. REPORT_INNER_ERROR("E19999", "Param out_data_anchor or in control anchor in Param src_node:%s(%s) is nullptr, "
  127. "check invalid", src_node->GetName().c_str(), src_node->GetType().c_str());
  128. GELOGE(FAILED, "parameter is null.");
  129. return FAILED;
  130. }
  131. auto src_out_control_anchors = src_in_ctrl->GetPeerAnchors();
  132. for (const auto &dst_in_data : out_data_anchor->GetPeerInDataAnchors()) {
  133. auto dst_node = dst_in_data->GetOwnerNode();
  134. auto dst_op_desc = dst_node->GetOpDesc();
  135. if (dst_op_desc == nullptr) {
  136. continue;
  137. }
  138. std::vector<bool> is_input_const = dst_op_desc->GetIsInputConst();
  139. auto input_index = static_cast<size_t>(dst_in_data->GetIdx());
  140. if (input_index < is_input_const.size()) {
  141. is_input_const[input_index] = true;
  142. dst_op_desc->SetIsInputConst(is_input_const);
  143. }
  144. GE_CHK_GRAPH_STATUS_RET(GraphUtils::RemoveEdge(out_data_anchor, dst_in_data), "remove edge failed");
  145. graphStatus ret = OpDescUtils::AddConstOpToAnchor(dst_in_data, weight);
  146. if (ret != SUCCESS) {
  147. return ret;
  148. }
  149. GE_CHECK_NOTNULL(dst_in_data->GetPeerOutAnchor());
  150. auto dynamic_const_node = dst_in_data->GetPeerOutAnchor()->GetOwnerNode();
  151. GE_CHECK_NOTNULL(dynamic_const_node->GetOpDesc());
  152. dynamic_const_node->GetOpDesc()->SetType(src_node->GetType());
  153. // restore control inputs to dynamically added constant ops, if any
  154. for (const auto &src_out_control_anchor : src_out_control_anchors) {
  155. GE_CHK_GRAPH_STATUS_RET(GraphUtils::AddEdge(src_out_control_anchor, dynamic_const_node->GetInControlAnchor()),
  156. "add edge failed");
  157. }
  158. }
  159. /// Before:
  160. /// Op1 - - - > Constant ------> Switch - - - > Op2
  161. /// After:
  162. /// Op1 - - - > Op2
  163. for (const auto &dst_in_ctrl : out_data_anchor->GetPeerInControlAnchors()) {
  164. for (const auto &src_out_control_anchor : src_out_control_anchors) {
  165. GE_CHK_GRAPH_STATUS_RET(GraphUtils::AddEdge(src_out_control_anchor, dst_in_ctrl), "add edge failed");
  166. }
  167. }
  168. return SUCCESS;
  169. }
  170. Status PassUtils::RemoveBranch(const NodePtr &node, std::vector<NodePtr> &delete_nodes,
  171. std::vector<NodePtr> &end_nodes) {
  172. if (node == nullptr) {
  173. REPORT_INNER_ERROR("E19999", "Param node is nullptr, check invalid");
  174. GELOGE(FAILED, "parameter is null.");
  175. return FAILED;
  176. }
  177. GELOGI("Remove branch starting from node %s", node->GetName().c_str());
  178. std::queue<NodePtr> search_queue;
  179. search_queue.push(node);
  180. while (!search_queue.empty()) {
  181. const NodePtr src_node = search_queue.front();
  182. if (src_node == nullptr) {
  183. continue;
  184. }
  185. delete_nodes.push_back(src_node);
  186. search_queue.pop();
  187. for (const auto &src_out_anchor : src_node->GetAllOutAnchors()) {
  188. for (const auto &dst_in_anchor : src_out_anchor->GetPeerAnchors()) {
  189. if (dst_in_anchor == nullptr) {
  190. continue;
  191. }
  192. auto dst_node = dst_in_anchor->GetOwnerNode();
  193. std::string node_type;
  194. GE_CHK_STATUS_RET(GetOriginalType(dst_node, node_type), "get original type failed");
  195. if (node_type == NETOUTPUT) {
  196. if (dst_in_anchor->IsTypeOf<InDataAnchor>()) {
  197. REPORT_INNER_ERROR("E19999", "Node:%s(%s) nactive branch connected to NetOutput with data anchor, "
  198. "check invalid", node->GetName().c_str(), node->GetType().c_str());
  199. GELOGE(INTERNAL_ERROR,
  200. "[%s] Inactive branch connected to "
  201. "NetOutput with data anchor.",
  202. node->GetName().c_str());
  203. return INTERNAL_ERROR;
  204. } else {
  205. // safe to unlink control edges
  206. GE_CHK_GRAPH_STATUS_RET(GraphUtils::RemoveEdge(src_out_anchor, dst_in_anchor), "remove edge failed");
  207. end_nodes.push_back(dst_node);
  208. }
  209. } else if (node_type == MERGE) {
  210. /// Unlink connection between the inactive branch and Merge/NetOutput.
  211. /// The removal of inactive nodes will be handled in PrunePass
  212. GE_CHK_GRAPH_STATUS_RET(GraphUtils::RemoveEdge(src_out_anchor, dst_in_anchor), "remove edge failed");
  213. end_nodes.push_back(dst_node);
  214. GELOGD("Reach the end merge node %s, the branch removing stop", dst_node->GetName().c_str());
  215. } else {
  216. search_queue.push(dst_node);
  217. }
  218. }
  219. }
  220. }
  221. return SUCCESS;
  222. }
  223. NodePtr PassUtils::GetInDataNode(const ConstNodePtr &node, int index) {
  224. if (node == nullptr) {
  225. return nullptr;
  226. }
  227. auto in_data_anchor = node->GetInDataAnchor(index);
  228. if (in_data_anchor == nullptr) {
  229. return nullptr;
  230. }
  231. auto peer_out_data_anchor = in_data_anchor->GetPeerOutAnchor();
  232. if (peer_out_data_anchor == nullptr) {
  233. return nullptr;
  234. }
  235. auto src_node = peer_out_data_anchor->GetOwnerNode();
  236. return src_node;
  237. }
  238. NodePtr PassUtils::GetInNodeCrossSubgraphByIndex(const ConstNodePtr &node, int index) {
  239. auto src_node = GetInDataNode(node, index);
  240. return NodeUtils::GetInNodeCrossSubgraph(src_node);
  241. }
  242. bool PassUtils::IsNeedTrainIteFlowCtrl(const ComputeGraphPtr &compute_graph) {
  243. if (compute_graph == nullptr) {
  244. return false;
  245. }
  246. if (compute_graph->GetParentGraph() != nullptr) {
  247. GELOGI("Subgraph %s no need flow ctrl.", compute_graph->GetName().c_str());
  248. return false;
  249. }
  250. if (GraphUtils::IsUnknownShapeGraph(compute_graph)) {
  251. GELOGI("Unknown shape graph %s no need flow ctrl.", compute_graph->GetName().c_str());
  252. return false;
  253. }
  254. if (!ge::VarManager::Instance(compute_graph->GetSessionID())->IsVarExist(NODE_NAME_FLOWCTRL_LOOP_PER_ITER)) {
  255. return false;
  256. }
  257. return compute_graph->GetNeedIteration();
  258. }
  259. int PassUtils::GetUniqueInDataAnchorIndex(const NodePtr &node_ptr) {
  260. const int invalid_index = -1;
  261. if (node_ptr == nullptr) {
  262. REPORT_INNER_ERROR("E19999", "Param node_ptr is nullptr, check invalid");
  263. GELOGE(INTERNAL_ERROR, "GetUniqueInDataAnchorIndex: node is null");
  264. return invalid_index;
  265. }
  266. for (const auto &in_anchor : node_ptr->GetAllInDataAnchors()) {
  267. if ((in_anchor != nullptr) && (in_anchor->GetPeerOutAnchor() != nullptr) &&
  268. (in_anchor->GetPeerOutAnchor()->GetOwnerNode() != nullptr)) {
  269. return (in_anchor->GetIdx());
  270. }
  271. }
  272. REPORT_INNER_ERROR("E19999", "Failed to find in data anchor of node:%s(%s) with a valid peer out node",
  273. node_ptr->GetName().c_str(), node_ptr->GetType().c_str());
  274. GELOGE(INTERNAL_ERROR,
  275. "GetUniqueInDataAnchorIndex: [%s] failed to find "
  276. "in data anchor with a valid peer out node",
  277. node_ptr->GetName().c_str());
  278. return invalid_index;
  279. }
  280. Status PassUtils::UnlinkNodeWithControlCopy(NodePtr &node, int index) {
  281. if (node == nullptr) {
  282. REPORT_INNER_ERROR("E19999", "Param node is nullptr, check invalid");
  283. GELOGE(PARAM_INVALID, "node is null.");
  284. return PARAM_INVALID;
  285. }
  286. auto in_data_anchor = node->GetInDataAnchor(index);
  287. if (in_data_anchor == nullptr) {
  288. GELOGW("[%s] in_data_anchor is null with index [%d].", node->GetName().c_str(), index);
  289. return SUCCESS;
  290. }
  291. auto out_data_anchor = in_data_anchor->GetPeerOutAnchor();
  292. if (out_data_anchor == nullptr) {
  293. REPORT_INNER_ERROR("E19999", "Index:%d in data anchor of node:%s(%s), its peer anchor is nullptr, check invalid",
  294. index, node->GetName().c_str(), node->GetType().c_str());
  295. GELOGE(FAILED, "[%s] peer out_data_anchor is null with index [%d].", node->GetName().c_str(), index);
  296. return FAILED;
  297. }
  298. // Remove link between father_node and node
  299. in_data_anchor->UnlinkAll();
  300. auto father_node = out_data_anchor->GetOwnerNode();
  301. // link father_node's in control nodes to node
  302. if (GraphUtils::CopyInCtrlEdges(father_node, node) != GRAPH_SUCCESS) {
  303. REPORT_CALL_ERROR("E19999", "Copy in control edge from node:%s(%s) to node:%s(%s) failed",
  304. father_node->GetName().c_str(), father_node->GetType().c_str(),
  305. node->GetName().c_str(), node->GetType().c_str());
  306. return FAILED;
  307. }
  308. return SUCCESS;
  309. }
  310. Status PassUtils::RemoveInactiveBranchToMerge(const OutDataAnchorPtr &inactive_output_anchor,
  311. std::vector<NodePtr> &delete_nodes, std::vector<NodePtr> &end_nodes) {
  312. if (inactive_output_anchor == nullptr) {
  313. REPORT_INNER_ERROR("E19999", "Param inactive_output_anchor is nullptr, check invalid");
  314. GELOGE(FAILED, "parameter is null.");
  315. return FAILED;
  316. }
  317. for (const auto &dst_anchor : inactive_output_anchor->GetPeerAnchors()) {
  318. if (dst_anchor == nullptr) {
  319. continue;
  320. }
  321. auto dst_node = dst_anchor->GetOwnerNode();
  322. if (dst_node != nullptr) {
  323. std::string dst_node_type;
  324. GE_CHK_STATUS_RET(GetOriginalType(dst_node, dst_node_type), "get original type failed");
  325. if (dst_node_type == MERGE) {
  326. GELOGD("[%s] Switch connected directly to Merge", inactive_output_anchor->GetOwnerNode()->GetName().c_str());
  327. GE_CHK_GRAPH_STATUS_RET(GraphUtils::RemoveEdge(inactive_output_anchor, dst_anchor), "remove edge failed");
  328. continue;
  329. }
  330. Status ret = PassUtils::RemoveBranch(dst_node, delete_nodes, end_nodes);
  331. if (ret != SUCCESS) {
  332. return ret;
  333. }
  334. }
  335. }
  336. return SUCCESS;
  337. }
  338. } // namespace ge

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