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

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

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