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

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