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.

folding_pass.cc 16 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
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
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
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
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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /**
  2. * Copyright 2019-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/folding_pass.h"
  17. #include <memory>
  18. #include <string>
  19. #include <utility>
  20. #include <vector>
  21. #include <unordered_set>
  22. #include "framework/common/debug/ge_log.h"
  23. #include "graph/utils/graph_utils.h"
  24. #include "graph/utils/node_utils.h"
  25. #include "inc/kernel.h"
  26. #include "inc/kernel_factory.h"
  27. #include "graph/debug/ge_attr_define.h"
  28. #include "ge_local_engine/engine/host_cpu_engine.h"
  29. namespace ge {
  30. namespace folding_pass {
  31. shared_ptr<Kernel> GetKernelByType(const NodePtr &node) {
  32. if (node == nullptr) {
  33. REPORT_INNER_ERROR("E19999", "Param node is nullptr, check invalid");
  34. GELOGE(FAILED, "parameter is null.");
  35. return nullptr;
  36. }
  37. KernelFactory &factory = KernelFactory::Instance();
  38. string type = node->GetType();
  39. if (type == FRAMEWORKOP) {
  40. if (!ge::AttrUtils::GetStr(node->GetOpDesc(), ATTR_NAME_FRAMEWORK_ORIGINAL_TYPE, type)) {
  41. REPORT_CALL_ERROR("E19999", "Get Attr:%s from op:%s(%s) failed",
  42. ATTR_NAME_FRAMEWORK_ORIGINAL_TYPE.c_str(),
  43. node->GetName().c_str(), node->GetType().c_str());
  44. return nullptr;
  45. }
  46. }
  47. return factory.Create(type);
  48. }
  49. bool IsNoNeedConstantFolding(const NodePtr &node) {
  50. auto node_desc = node->GetOpDesc();
  51. return node_desc == nullptr || node_desc->HasAttr(ATTR_NO_NEED_CONSTANT_FOLDING);
  52. }
  53. } // namespace folding_pass
  54. namespace {
  55. IndexsToAnchors GetIndexAndPeerInDataAnchors(NodePtr &node) {
  56. IndexsToAnchors indexes_to_anchors;
  57. for (auto &out_anchor : node->GetAllOutDataAnchors()) {
  58. if (out_anchor == nullptr) {
  59. continue;
  60. }
  61. for (auto &peer_in_anchor : out_anchor->GetPeerInDataAnchors()) {
  62. if (peer_in_anchor == nullptr) {
  63. continue;
  64. }
  65. const auto &peer_node = peer_in_anchor->GetOwnerNode();
  66. if (peer_node == nullptr) {
  67. continue;
  68. }
  69. indexes_to_anchors[out_anchor->GetIdx()].push_back(peer_in_anchor);
  70. }
  71. }
  72. return indexes_to_anchors;
  73. }
  74. NodePtr AddConstNodeToGraph(GeTensorPtr &tensor, ComputeGraphPtr &graph) {
  75. auto const_desc = OpDescUtils::CreateConstOp(tensor);
  76. if (const_desc == nullptr) {
  77. REPORT_CALL_ERROR("E19999", "Create Const op failed");
  78. GELOGE(OUT_OF_MEMORY, "Failed to get const desc from tensor");
  79. return nullptr;
  80. }
  81. GE_IF_BOOL_EXEC(graph == nullptr, GELOGW("input param graph is null"); return nullptr);
  82. (void) AttrUtils::SetListStr(const_desc, ATTR_NAME_DATA_DUMP_ORIGIN_OP_NAMES, std::move(std::vector<std::string>()));
  83. return graph->AddNodeFront(const_desc);
  84. }
  85. NodePtr AddIdentityNodeToGraph(const std::string &name, const GeTensorDesc &tensor, ComputeGraphPtr &graph) {
  86. if (graph == nullptr) {
  87. REPORT_INNER_ERROR("E19999", "Param graph is nullptr, check invalid");
  88. GELOGE(INTERNAL_ERROR, "Compute graph ptr is null in creating identity node.");
  89. return nullptr;
  90. }
  91. OpDescPtr desc = MakeShared<OpDesc>("", "");
  92. if (desc == nullptr) {
  93. REPORT_CALL_ERROR("E19999", "New OpDesc failed");
  94. GELOGE(MEMALLOC_FAILED, "Failed to create op desc.");
  95. return nullptr;
  96. }
  97. desc->SetName(name);
  98. desc->SetType(IDENTITY);
  99. auto ret = desc->AddInputDesc(tensor);
  100. auto ret2 = desc->AddOutputDesc(tensor);
  101. if ((ret != GRAPH_SUCCESS) || (ret2 != GRAPH_SUCCESS)) {
  102. REPORT_CALL_ERROR("E19999", "Add input or output desc to op:%s(%s) failed",
  103. desc->GetName().c_str(), desc->GetType().c_str());
  104. GELOGE(INTERNAL_ERROR, "Failed to add input/output desc in creating Identity.");
  105. return nullptr;
  106. }
  107. return graph->AddNodeFront(desc);
  108. }
  109. } // namespace
  110. Status FoldingPass::RunOpKernel(NodePtr &node,
  111. const vector<ConstGeTensorPtr> &inputs,
  112. std::vector<GeTensorPtr> &outputs) {
  113. return HostCpuEngine::GetInstance().Run(node, inputs, outputs);
  114. }
  115. Status FoldingPass::Folding(NodePtr &node, vector<GeTensorPtr> &outputs) {
  116. GE_CHECK_NOTNULL(node);
  117. GELOGD("begin folding node:%s", node->GetName().c_str());
  118. // Before processing nodes, collect the relations between the out anchor and the peer out data nodes
  119. // to prepare for const reconnection
  120. auto indexes_to_anchors = GetIndexAndPeerInDataAnchors(node);
  121. auto ret = DealWithInNodes(node);
  122. if (ret != SUCCESS) {
  123. return ret;
  124. }
  125. if (AddConstNode(node, indexes_to_anchors, outputs) != SUCCESS) {
  126. return INTERNAL_ERROR;
  127. }
  128. auto in_data_nodes = node->GetInDataNodes();
  129. std::unordered_set<NodePtr> in_data_nodes_set(in_data_nodes.begin(), in_data_nodes.end());
  130. if (IsolateAndDeleteNode(node, {}) != SUCCESS) {
  131. REPORT_INNER_ERROR("E19999", "Isolate and delete node:%s(%s) faild",
  132. node->GetName().c_str(), node->GetType().c_str());
  133. GELOGE(INTERNAL_ERROR, "Failed to isolate and delete node %s, type %s.",
  134. node->GetName().c_str(), node->GetType().c_str());
  135. return INTERNAL_ERROR;
  136. }
  137. for (auto iter = in_data_nodes_set.begin(); iter != in_data_nodes_set.end(); ++iter) {
  138. auto pre_node = *iter;
  139. if (pre_node->GetOutDataNodesSize() == 0) {
  140. if ((pre_node->GetType() == DATA) || (pre_node->GetType() == ENTER)) {
  141. GELOGI("No need to remove data/enter, node name:%s.", pre_node->GetName().c_str());
  142. continue;
  143. }
  144. if (IsolateAndDeleteNode(pre_node, {}) != SUCCESS) {
  145. REPORT_INNER_ERROR("E19999", "Isolate and delete node:%s(%s) faild",
  146. pre_node->GetName().c_str(), pre_node->GetType().c_str());
  147. GELOGE(INTERNAL_ERROR, "Failed to isolate and delete in data node %s, type %s.",
  148. pre_node->GetName().c_str(), pre_node->GetType().c_str());
  149. return INTERNAL_ERROR;
  150. }
  151. }
  152. }
  153. return SUCCESS;
  154. }
  155. Status FoldingPass::DealWithInNodes(NodePtr &node) {
  156. GE_CHECK_NOTNULL(node);
  157. GE_CHECK_NOTNULL(node->GetOpDesc());
  158. auto graph = node->GetOwnerComputeGraph();
  159. auto in_data_anchors = node->GetAllInDataAnchors();
  160. for (auto &in_data_anchor : in_data_anchors) {
  161. if (in_data_anchor == nullptr) {
  162. continue;
  163. }
  164. auto in_node_anchor = in_data_anchor->GetPeerOutAnchor();
  165. if (in_node_anchor == nullptr) {
  166. continue;
  167. }
  168. auto in_node = in_node_anchor->GetOwnerNode();
  169. if ((in_node->GetType() == SWITCH) || (in_node->GetType() == REFSWITCH) || (in_node->GetType() == SWITCHN)) {
  170. GELOGI("The in_node name is %s, and node type is %s.", in_node->GetName().c_str(), in_node->GetType().c_str());
  171. auto ret = in_node_anchor->Unlink(in_data_anchor);
  172. if (ret != SUCCESS) {
  173. REPORT_CALL_ERROR("E19999",
  174. "Op:%s(%s) out index:%d unlink from op:%s(%s) in index:%d failed",
  175. in_node->GetName().c_str(), in_node->GetType().c_str(), in_node_anchor->GetIdx(),
  176. node->GetName().c_str(), node->GetType().c_str(), in_data_anchor->GetIdx());
  177. GELOGE(INTERNAL_ERROR, "Failed to unlink anchor between const node %s to constant-folding-node %s, type %s.",
  178. in_node->GetName().c_str(), node->GetName().c_str(), node->GetType().c_str());
  179. return INTERNAL_ERROR;
  180. }
  181. GELOGI("Unlink anchor between in_node %s and node %s success.", in_node->GetName().c_str(),
  182. node->GetName().c_str());
  183. auto identity_name = node->GetName() + "_ctrl_identity_" + std::to_string(in_data_anchor->GetIdx());
  184. auto identity =
  185. AddIdentityNodeToGraph(identity_name, node->GetOpDesc()->GetInputDesc(in_data_anchor->GetIdx()), graph);
  186. if (identity == nullptr) {
  187. GELOGE(INTERNAL_ERROR, "Failed to add identity node to graph.");
  188. return INTERNAL_ERROR;
  189. }
  190. ret = GraphUtils::AddEdge(in_node_anchor, identity->GetInDataAnchor(0));
  191. if (ret != GRAPH_SUCCESS) {
  192. REPORT_CALL_ERROR("E19999", "Add edge between op:%s(%s)(index:%d) and op:%s(%s)(inde:0) failed",
  193. in_node->GetName().c_str(), in_node->GetType().c_str(), in_node_anchor->GetIdx(),
  194. identity->GetName().c_str(), identity->GetType().c_str());
  195. GELOGE(INTERNAL_ERROR, "Failed to add edge, from node %s to node %s.", in_node->GetName().c_str(),
  196. identity->GetName().c_str());
  197. return INTERNAL_ERROR;
  198. }
  199. GELOGI("Create new identity node success.");
  200. ret = GraphUtils::AddEdge(identity->GetOutControlAnchor(), node->GetInControlAnchor());
  201. if (ret != GRAPH_SUCCESS) {
  202. REPORT_CALL_ERROR("E19999", "Add control edge between op:%s(%s) and op:%s(%s) failed",
  203. identity->GetName().c_str(), identity->GetType().c_str(),
  204. node->GetName().c_str(), node->GetType().c_str());
  205. GELOGE(INTERNAL_ERROR, "Failed to add edge, from node %s to node %s.", in_node->GetName().c_str(),
  206. node->GetName().c_str());
  207. return INTERNAL_ERROR;
  208. }
  209. }
  210. }
  211. return SUCCESS;
  212. }
  213. Status FoldingPass::AddConstNode(NodePtr &node, IndexsToAnchors indexes_to_anchors,
  214. std::vector<GeTensorPtr> &v_weight) {
  215. if (node == nullptr) {
  216. REPORT_INNER_ERROR("E19999", "Param node is nullptr, check invalid");
  217. GELOGE(PARAM_INVALID, "node is null");
  218. return FAILED;
  219. }
  220. auto graph = node->GetOwnerComputeGraph();
  221. for (auto &index_to_anchors : indexes_to_anchors) {
  222. auto index = static_cast<size_t>(index_to_anchors.first);
  223. if (index >= v_weight.size()) {
  224. REPORT_INNER_ERROR("E19999", "Index:%lu in param index_to_anchors >= param v_weight.size:%zu, "
  225. "check invalid", index, v_weight.size());
  226. GELOGE(INTERNAL_ERROR,
  227. "Failed to constant fold on node %s type %s, "
  228. "the out nodes num %lu calculated is less than the node out anchor index %zu",
  229. node->GetName().c_str(), node->GetType().c_str(), v_weight.size(), index);
  230. return INTERNAL_ERROR;
  231. }
  232. GeTensorPtr weight = v_weight[index];
  233. if (weight == nullptr) {
  234. REPORT_INNER_ERROR("E19999", "Index:%lu in param v_weight is nullptr check invalid",
  235. index);
  236. GELOGE(INTERNAL_ERROR, "Failed to constant fold on node %s type %s, the %lust node calculated is null",
  237. node->GetName().c_str(), node->GetType().c_str(), index);
  238. return INTERNAL_ERROR;
  239. }
  240. auto const_node = AddConstNodeToGraph(weight, graph);
  241. if (const_node == nullptr) {
  242. GELOGE(INTERNAL_ERROR, "Failed to add dynamic const node, node name:%s, index:%zu.",
  243. node->GetName().c_str(), index);
  244. return INTERNAL_ERROR;
  245. }
  246. GELOGI("add const_node:%s, replace node %s, type %s, index %zu.", const_node->GetName().c_str(),
  247. node->GetName().c_str(), node->GetType().c_str(), index);
  248. // add new const to re-pass node
  249. for (auto &in_anchor : index_to_anchors.second) {
  250. if (in_anchor == nullptr) {
  251. REPORT_INNER_ERROR("E19999", "Index:%lu in param index_to_anchors has nullptr member in_anchor, "
  252. "check invalid", index);
  253. GELOGE(INTERNAL_ERROR, "In anchor is nullptr.");
  254. return INTERNAL_ERROR;
  255. }
  256. auto ret = ConnectNodeToInAnchor(in_anchor, const_node, 0);
  257. if (ret != SUCCESS) {
  258. return ret;
  259. }
  260. NodeUtils::UpdateIsInputConst(*(in_anchor->GetOwnerNode()));
  261. }
  262. Status ret = GraphUtils::AddEdge(node->GetOutControlAnchor(), const_node->GetInControlAnchor());
  263. if (ret != GRAPH_SUCCESS) {
  264. REPORT_CALL_ERROR("E19999", "Add control edge between op:%s(%s) and op:%s(%s) failed",
  265. node->GetName().c_str(), node->GetType().c_str(),
  266. const_node->GetName().c_str(), const_node->GetType().c_str());
  267. GELOGE(INTERNAL_ERROR, "Failed to add control edge, from node %s to const node %s.", node->GetName().c_str(),
  268. const_node->GetName().c_str());
  269. return INTERNAL_ERROR;
  270. }
  271. GE_CHECK_NOTNULL(node->GetOpDesc());
  272. std::string stream_label;
  273. if (AttrUtils::GetStr(node->GetOpDesc(), ATTR_NAME_STREAM_LABEL, stream_label)) {
  274. GE_CHECK_NOTNULL(const_node->GetOpDesc());
  275. if (!AttrUtils::SetStr(const_node->GetOpDesc(), ATTR_NAME_STREAM_LABEL, stream_label)) {
  276. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed",
  277. ATTR_NAME_STREAM_LABEL.c_str(),
  278. const_node->GetName().c_str(), const_node->GetType().c_str());
  279. GELOGE(INTERNAL_ERROR, "Failed to set stream label on dynamic const node %s, with stream label:%s.",
  280. const_node->GetName().c_str(), stream_label.c_str());
  281. return INTERNAL_ERROR;
  282. }
  283. }
  284. GELOGD("Add control edge when insert dynamic const, from node %s to const node %s, with stream label:%s.",
  285. node->GetName().c_str(), const_node->GetName().c_str(), stream_label.c_str());
  286. }
  287. return SUCCESS;
  288. }
  289. Status FoldingPass::RemoveNodeKeepingCtrlEdges(NodePtr &node) {
  290. GE_IF_BOOL_EXEC(node == nullptr, GELOGE(PARAM_INVALID, "node is null"); return PARAM_INVALID);
  291. auto ret = GraphUtils::IsolateNode(node, {});
  292. if (ret != GRAPH_SUCCESS) {
  293. REPORT_CALL_ERROR("E19999", "Isolate node:%s(%s) in graph failed",
  294. node->GetName().c_str(), node->GetType().c_str());
  295. GELOGE(INTERNAL_ERROR, "Failed to isolate the folding-node %s type %s", node->GetName().c_str(),
  296. node->GetType().c_str());
  297. return INTERNAL_ERROR;
  298. }
  299. auto graph = node->GetOwnerComputeGraph();
  300. ret = GraphUtils::RemoveNodeWithoutRelink(graph, node);
  301. if (ret != GRAPH_SUCCESS) {
  302. REPORT_CALL_ERROR("E19999", "Remove node:%s(%s) without relink in graph:%s failed",
  303. node->GetName().c_str(), node->GetType().c_str(), graph->GetName().c_str());
  304. GELOGE(INTERNAL_ERROR, "Failed to remove node %s from graph", node->GetName().c_str());
  305. return INTERNAL_ERROR;
  306. }
  307. AddNodeDeleted(node);
  308. return SUCCESS;
  309. }
  310. Status FoldingPass::ConnectNodeToInAnchor(InDataAnchorPtr &in_anchor, NodePtr &node, int node_index) {
  311. // the origin edge must be removed before add
  312. if (in_anchor == nullptr || node == nullptr) {
  313. REPORT_INNER_ERROR("E19999", "Param node or in_anchor is nullptr, check invalid");
  314. GELOGE(PARAM_INVALID, "in anchor or node is null");
  315. return PARAM_INVALID;
  316. }
  317. auto peer_out_anchor = in_anchor->GetPeerOutAnchor();
  318. if (peer_out_anchor != nullptr) {
  319. if (ge::GraphUtils::RemoveEdge(peer_out_anchor, in_anchor) != GRAPH_SUCCESS) {
  320. GELOGW("RemoveEdge failed.");
  321. }
  322. }
  323. auto new_out_anchor = node->GetOutDataAnchor(node_index);
  324. if (new_out_anchor == nullptr) {
  325. REPORT_INNER_ERROR("E19999", "Param out index:%d data anchor of node:%s(%s) is nullptr, check invalid",
  326. node_index, node->GetName().c_str(), node->GetType().c_str());
  327. GELOGE(INTERNAL_ERROR,
  328. "Failed to add node to in anchor,"
  329. " the index %d for node %s, type %s is invalid",
  330. node_index, node->GetName().c_str(), node->GetType().c_str());
  331. return INTERNAL_ERROR;
  332. }
  333. if (GraphUtils::AddEdge(new_out_anchor, in_anchor) != GRAPH_SUCCESS) {
  334. REPORT_CALL_ERROR("E19999", "Add edge between op:%s(%s)(index:%d) and op:%s(%s)(index:%d) failed",
  335. node->GetName().c_str(), node->GetType().c_str(), node_index,
  336. in_anchor->GetOwnerNode()->GetName().c_str(), in_anchor->GetOwnerNode()->GetType().c_str(),
  337. in_anchor->GetIdx());
  338. GELOGE(INTERNAL_ERROR,
  339. "Failed to add edge between anchors,"
  340. " new node %s, type %s",
  341. node->GetName().c_str(), node->GetType().c_str());
  342. return INTERNAL_ERROR;
  343. }
  344. AddRePassNodesWithInOut(node);
  345. return SUCCESS;
  346. }
  347. } // namespace ge

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