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

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

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