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 12 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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->GetType() == SWITCH) || (in_node->GetType() == REFSWITCH) || (in_node->GetType() == SWITCHN)) {
  157. GELOGI("The in_node name is %s, and node type is %s.", in_node->GetName().c_str(), in_node->GetType().c_str());
  158. auto ret = in_node_anchor->Unlink(in_data_anchor);
  159. if (ret != SUCCESS) {
  160. GELOGE(INTERNAL_ERROR, "Failed to unlink anchor between const node %s to constant-folding-node %s, type %s.",
  161. in_node->GetName().c_str(), node->GetName().c_str(), node->GetType().c_str());
  162. return INTERNAL_ERROR;
  163. }
  164. GELOGI("Unlink anchor between in_node %s and node %s success.", in_node->GetName().c_str(),
  165. node->GetName().c_str());
  166. auto identity_name = node->GetName() + "_ctrl_identity_" + std::to_string(in_data_anchor->GetIdx());
  167. auto identity =
  168. AddIdentityNodeToGraph(identity_name, node->GetOpDesc()->GetInputDesc(in_data_anchor->GetIdx()), graph);
  169. if (identity == nullptr) {
  170. GELOGE(INTERNAL_ERROR, "Failed to add identity node to graph.");
  171. return INTERNAL_ERROR;
  172. }
  173. ret = GraphUtils::AddEdge(in_node_anchor, identity->GetInDataAnchor(0));
  174. if (ret != GRAPH_SUCCESS) {
  175. GELOGE(INTERNAL_ERROR, "Failed to add edge, from node %s to node %s.", in_node->GetName().c_str(),
  176. identity->GetName().c_str());
  177. return INTERNAL_ERROR;
  178. }
  179. GELOGI("Create new identity node success.");
  180. ret = GraphUtils::AddEdge(identity->GetOutControlAnchor(), node->GetInControlAnchor());
  181. if (ret != GRAPH_SUCCESS) {
  182. GELOGE(INTERNAL_ERROR, "Failed to add edge, from node %s to node %s.", in_node->GetName().c_str(),
  183. node->GetName().c_str());
  184. return INTERNAL_ERROR;
  185. }
  186. }
  187. }
  188. return SUCCESS;
  189. }
  190. Status FoldingPass::AddConstNode(NodePtr &node, IndexsToAnchors indexes_to_anchors,
  191. std::vector<GeTensorPtr> &v_weight) {
  192. if (node == nullptr) {
  193. GELOGE(PARAM_INVALID, "node is null");
  194. return FAILED;
  195. }
  196. auto graph = node->GetOwnerComputeGraph();
  197. for (auto &index_to_anchors : indexes_to_anchors) {
  198. auto index = static_cast<size_t>(index_to_anchors.first);
  199. if (index >= v_weight.size()) {
  200. GELOGE(INTERNAL_ERROR,
  201. "Failed to constant fold on node %s type %s, "
  202. "the out nodes num %lu calculated is less than the node out anchor index %zu",
  203. node->GetName().c_str(), node->GetType().c_str(), v_weight.size(), index);
  204. return INTERNAL_ERROR;
  205. }
  206. GeTensorPtr weight = v_weight[index];
  207. if (weight == nullptr) {
  208. GELOGE(INTERNAL_ERROR, "Failed to constant fold on node %s type %s, the %lust node calculated is null",
  209. node->GetName().c_str(), node->GetType().c_str(), index);
  210. return INTERNAL_ERROR;
  211. }
  212. auto const_node = AddConstNodeToGraph(weight, graph);
  213. if (const_node == nullptr) {
  214. GELOGE(INTERNAL_ERROR, "Failed to add dynamic const node, node name:%s, index:%zu.",
  215. node->GetName().c_str(), index);
  216. return INTERNAL_ERROR;
  217. }
  218. GELOGI("add const_node:%s, replace node %s, type %s, index %zu.", const_node->GetName().c_str(),
  219. node->GetName().c_str(), node->GetType().c_str(), index);
  220. // add new const to re-pass node
  221. for (auto &in_anchor : index_to_anchors.second) {
  222. if (in_anchor == nullptr) {
  223. GELOGE(INTERNAL_ERROR, "In anchor is nullptr.");
  224. return INTERNAL_ERROR;
  225. }
  226. auto ret = ConnectNodeToInAnchor(in_anchor, const_node, 0);
  227. if (ret != SUCCESS) {
  228. return ret;
  229. }
  230. NodeUtils::UpdateIsInputConst(*(in_anchor->GetOwnerNode()));
  231. }
  232. Status ret = GraphUtils::AddEdge(node->GetOutControlAnchor(), const_node->GetInControlAnchor());
  233. if (ret != GRAPH_SUCCESS) {
  234. GELOGE(INTERNAL_ERROR, "Failed to add control edge, from node %s to const node %s.", node->GetName().c_str(),
  235. const_node->GetName().c_str());
  236. return INTERNAL_ERROR;
  237. }
  238. GE_CHECK_NOTNULL(node->GetOpDesc());
  239. std::string stream_label;
  240. if (AttrUtils::GetStr(node->GetOpDesc(), ATTR_NAME_STREAM_LABEL, stream_label)) {
  241. GE_CHECK_NOTNULL(const_node->GetOpDesc());
  242. if (!AttrUtils::SetStr(const_node->GetOpDesc(), ATTR_NAME_STREAM_LABEL, stream_label)) {
  243. GELOGE(INTERNAL_ERROR, "Failed to set stream label on dynamic const node %s, with stream label:%s.",
  244. const_node->GetName().c_str(), stream_label.c_str());
  245. return INTERNAL_ERROR;
  246. }
  247. }
  248. GELOGD("Add control edge when insert dynamic const, from node %s to const node %s, with stream label:%s.",
  249. node->GetName().c_str(), const_node->GetName().c_str(), stream_label.c_str());
  250. }
  251. return SUCCESS;
  252. }
  253. Status FoldingPass::RemoveNodeKeepingCtrlEdges(NodePtr &node) {
  254. GE_IF_BOOL_EXEC(node == nullptr, GELOGE(PARAM_INVALID, "node is null"); return PARAM_INVALID);
  255. auto ret = GraphUtils::IsolateNode(node, {});
  256. if (ret != GRAPH_SUCCESS) {
  257. GELOGE(INTERNAL_ERROR, "Failed to isolate the folding-node %s type %s", node->GetName().c_str(),
  258. node->GetType().c_str());
  259. return INTERNAL_ERROR;
  260. }
  261. auto graph = node->GetOwnerComputeGraph();
  262. ret = GraphUtils::RemoveNodeWithoutRelink(graph, node);
  263. if (ret != GRAPH_SUCCESS) {
  264. GELOGE(INTERNAL_ERROR, "Failed to remove node %s from graph", node->GetName().c_str());
  265. return INTERNAL_ERROR;
  266. }
  267. AddNodeDeleted(node);
  268. return SUCCESS;
  269. }
  270. Status FoldingPass::ConnectNodeToInAnchor(InDataAnchorPtr &in_anchor, NodePtr &node, int node_index) {
  271. // the origin edge must be removed before add
  272. if (in_anchor == nullptr || node == nullptr) {
  273. GELOGE(PARAM_INVALID, "in anchor or node is null");
  274. return PARAM_INVALID;
  275. }
  276. auto peer_out_anchor = in_anchor->GetPeerOutAnchor();
  277. if (peer_out_anchor != nullptr) {
  278. if (ge::GraphUtils::RemoveEdge(peer_out_anchor, in_anchor) != GRAPH_SUCCESS) {
  279. GELOGW("RemoveEdge failed.");
  280. }
  281. }
  282. auto new_out_anchor = node->GetOutDataAnchor(node_index);
  283. if (new_out_anchor == nullptr) {
  284. GELOGE(INTERNAL_ERROR,
  285. "Failed to add node to in anchor,"
  286. " the index %d for node %s, type %s is invalid",
  287. node_index, node->GetName().c_str(), node->GetType().c_str());
  288. return INTERNAL_ERROR;
  289. }
  290. if (GraphUtils::AddEdge(new_out_anchor, in_anchor) != GRAPH_SUCCESS) {
  291. GELOGE(INTERNAL_ERROR,
  292. "Failed to add edge between anchors,"
  293. " new node %s, type %s",
  294. node->GetName().c_str(), node->GetType().c_str());
  295. return INTERNAL_ERROR;
  296. }
  297. AddRePassNodesWithInOut(node);
  298. return SUCCESS;
  299. }
  300. } // namespace ge

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