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

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

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