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.

label_maker.cc 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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/label/label_maker.h"
  17. #include "common/util.h"
  18. #include "common/ge_inner_error_codes.h"
  19. #include "framework/common/types.h"
  20. #include "framework/common/op/ge_op_utils.h"
  21. #include "graph/debug/ge_attr_define.h"
  22. #include "graph/utils/graph_utils.h"
  23. namespace ge {
  24. /**
  25. * @ingroup ge
  26. * @brief Link node to graph head.
  27. * @param [in] graph: graph for add node.
  28. * @param [in] node: Node add to graph head.
  29. * @return: void
  30. */
  31. void LabelMaker::LinkToGraphHead(const ComputeGraphPtr &graph, const NodePtr &node) {
  32. static const std::set<std::string> non_calc_types = { DATA, CONSTANT, CONSTANTOP, VARIABLE };
  33. for (auto &n : graph->GetDirectNode()) {
  34. if (non_calc_types.count(n->GetType()) > 0) {
  35. continue;
  36. }
  37. const auto nodes = n->GetInDataNodes();
  38. if (nodes.empty()) {
  39. continue;
  40. }
  41. bool is_head_node = true;
  42. for (auto &in_node : nodes) {
  43. if (non_calc_types.count(in_node->GetType()) == 0) {
  44. is_head_node = false;
  45. break;
  46. }
  47. }
  48. if (!is_head_node) {
  49. continue;
  50. }
  51. if (GraphUtils::AddEdge(node->GetOutControlAnchor(), n->GetInControlAnchor()) != SUCCESS) {
  52. GELOGE(INTERNAL_ERROR, "Add ctrl edge from %s to %s failed.", node->GetName().c_str(), n->GetName().c_str());
  53. }
  54. }
  55. }
  56. /**
  57. * @ingroup ge
  58. * @brief Link node to graph tail.
  59. * @param [in] graph: graph for add node.
  60. * @param [in] node: Node add to graph tail.
  61. * @return: void
  62. */
  63. void LabelMaker::LinkToGraphTail(const ComputeGraphPtr &graph, const NodePtr &node) {
  64. auto tail = graph->FindFirstNodeMatchType(NETOUTPUT);
  65. while (tail != nullptr) {
  66. auto nodes = tail->GetOutControlNodes();
  67. if (!nodes.empty()) {
  68. tail = nodes.at(0);
  69. continue;
  70. }
  71. if (GraphUtils::AddEdge(tail->GetOutControlAnchor(), node->GetInControlAnchor()) != SUCCESS) {
  72. GELOGE(INTERNAL_ERROR, "Add ctrl edge from %s to %s failed.", tail->GetName().c_str(), node->GetName().c_str());
  73. }
  74. return;
  75. }
  76. }
  77. /**
  78. * @ingroup ge
  79. * @brief Add StreamActive node at graph front.
  80. * @param [in] graph: graph for add node.
  81. * @param [in] name: stream active node name.
  82. * @return: NodePtr for success / nullptr for fail
  83. */
  84. NodePtr LabelMaker::AddStreamActive(const ComputeGraphPtr &graph, const std::string &name) {
  85. GE_CHECK_NOTNULL_EXEC(graph, return nullptr);
  86. const auto &node_list = graph->GetDirectNode();
  87. if (node_list.empty()) {
  88. GELOGE(INTERNAL_ERROR, "LabelSet: Graph %s node is empty.", graph->GetName().c_str());
  89. return nullptr;
  90. }
  91. OpDescPtr op_desc = MakeShared<OpDesc>(name, STREAMACTIVE);
  92. GE_CHECK_NOTNULL_EXEC(op_desc, return nullptr);
  93. (void)AttrUtils::SetBool(op_desc, ATTR_NAME_RTS_LABEL_NODE, true);
  94. GELOGI("StreamActive: Create node %s.", op_desc->GetName().c_str());
  95. vector<uint32_t> active_streams;
  96. (void)AttrUtils::SetStr(op_desc, ATTR_NAME_SWITCH_BRANCH_NODE_LABEL, op_desc->GetName());
  97. (void)AttrUtils::SetListInt(op_desc, ATTR_NAME_ACTIVE_STREAM_LIST, active_streams);
  98. (void)AttrUtils::SetBool(op_desc, ATTR_NAME_SUBGRAPH_FIRST_ACTIVE, true);
  99. NodePtr stream_active = graph->AddNodeFront(op_desc);
  100. GE_CHECK_NOTNULL_EXEC(stream_active, return nullptr);
  101. LinkToGraphHead(graph, stream_active);
  102. return stream_active;
  103. }
  104. /**
  105. * @ingroup ge
  106. * @brief Add LabelSet node at graph front.
  107. * @param [in] graph: graph for add node.
  108. * @param [in] name: label set node name.
  109. * @param [in] index: label id for set.
  110. * @return: NodePtr for success / nullptr for fail
  111. */
  112. NodePtr LabelMaker::AddLabelSetEnter(const ComputeGraphPtr &graph, const std::string &name, uint32_t index,
  113. NodePtr &stream_active) {
  114. GE_CHECK_NOTNULL_EXEC(graph, return nullptr);
  115. GE_CHECK_NOTNULL_EXEC(stream_active, return nullptr);
  116. const auto &node_list = graph->GetDirectNode();
  117. if (node_list.empty()) {
  118. GELOGE(INTERNAL_ERROR, "LabelSet: Graph %s node is empty.", graph->GetName().c_str());
  119. return nullptr;
  120. }
  121. OpDescPtr op_desc = MakeShared<OpDesc>(name, LABELSET);
  122. GE_CHECK_NOTNULL_EXEC(op_desc, return nullptr);
  123. (void)AttrUtils::SetBool(op_desc, ATTR_NAME_RTS_LABEL_NODE, true);
  124. GELOGI("LabelSet: Create node %s.", op_desc->GetName().c_str());
  125. (void)AttrUtils::SetInt(op_desc, ATTR_NAME_LABEL_SWITCH_INDEX, index);
  126. NodePtr label_set = graph->AddNodeFront(op_desc);
  127. GE_CHECK_NOTNULL_EXEC(label_set, return nullptr);
  128. if (GraphUtils::AddEdge(label_set->GetOutControlAnchor(), stream_active->GetInControlAnchor()) != SUCCESS) {
  129. GELOGE(INTERNAL_ERROR, "Add ctrl edge from %s to %s failed.", label_set->GetName().c_str(),
  130. stream_active->GetName().c_str());
  131. return nullptr;
  132. }
  133. return label_set;
  134. }
  135. /**
  136. * @ingroup ge
  137. * @brief Add LabelSet node at graph back.
  138. * @param [in] graph: graph for add node.
  139. * @param [in] name: label set node name.
  140. * @param [in] index: label id for set.
  141. * @return: NodePtr for success / nullptr for fail
  142. */
  143. NodePtr LabelMaker::AddLabelSetLeave(const ComputeGraphPtr &graph, const std::string &name, uint32_t index) {
  144. GE_CHECK_NOTNULL_EXEC(graph, return nullptr);
  145. OpDescPtr op_desc = MakeShared<OpDesc>(name, LABELSET);
  146. GE_CHECK_NOTNULL_EXEC(op_desc, return nullptr);
  147. (void)AttrUtils::SetBool(op_desc, ATTR_NAME_RTS_LABEL_NODE, true);
  148. GELOGI("LabelSet: Create node %s.", op_desc->GetName().c_str());
  149. (void)AttrUtils::SetInt(op_desc, ATTR_NAME_LABEL_SWITCH_INDEX, index);
  150. (void)AttrUtils::SetBool(op_desc, ATTR_NAME_SUBGRAPH_END_NODE, true);
  151. NodePtr label_set = graph->AddNode(op_desc);
  152. GE_CHECK_NOTNULL_EXEC(label_set, return nullptr);
  153. // Link control edge to graph tail.
  154. LinkToGraphTail(graph, label_set);
  155. return label_set;
  156. }
  157. /**
  158. * @ingroup ge
  159. * @brief Add LabelGoto node at graph front.
  160. * @param [in] graph: graph for add node.
  161. * @param [in] name: label goto node name.
  162. * @param [in] index: label id for goto.
  163. * @return: NodePtr for success / nullptr for fail
  164. */
  165. NodePtr LabelMaker::AddLabelGotoEnter(const ComputeGraphPtr &graph, const std::string &name, uint32_t index) {
  166. GE_CHECK_NOTNULL_EXEC(graph, return nullptr);
  167. const auto &node_list = graph->GetDirectNode();
  168. auto it = node_list.begin();
  169. if (it == node_list.end()) {
  170. GELOGE(INTERNAL_ERROR, "LabelGoto: Graph %s node is empty.", graph->GetName().c_str());
  171. return nullptr;
  172. }
  173. OpDescPtr op_desc = MakeShared<OpDesc>(name, LABELGOTOEX);
  174. GE_CHECK_NOTNULL_EXEC(op_desc, return nullptr);
  175. (void)AttrUtils::SetBool(op_desc, ATTR_NAME_RTS_LABEL_NODE, true);
  176. GELOGI("LabelGoto: Create node %s.", op_desc->GetName().c_str());
  177. (void)AttrUtils::SetInt(op_desc, ATTR_NAME_LABEL_SWITCH_INDEX, index);
  178. NodePtr label_goto = graph->AddNodeFront(op_desc);
  179. if (label_goto == nullptr) {
  180. GELOGE(INTERNAL_ERROR, "LabelGoto: Add to graph %s failed.", graph->GetName().c_str());
  181. return nullptr;
  182. }
  183. return label_goto;
  184. }
  185. /**
  186. * @ingroup ge
  187. * @brief Add LabelGoto node at graph back.
  188. * @param [in] graph: graph for add node.
  189. * @param [in] name: label goto node name.
  190. * @param [in] index: label id for goto.
  191. * @return: NodePtr for success / nullptr for fail
  192. */
  193. NodePtr LabelMaker::AddLabelGotoLeave(const ComputeGraphPtr &graph, const std::string &name, uint32_t index) {
  194. GE_CHECK_NOTNULL_EXEC(graph, return nullptr);
  195. OpDescPtr op_desc = MakeShared<OpDesc>(name, LABELGOTOEX);
  196. GE_CHECK_NOTNULL_EXEC(op_desc, return nullptr);
  197. (void)AttrUtils::SetBool(op_desc, ATTR_NAME_RTS_LABEL_NODE, true);
  198. GELOGI("LabelGoto: Create node %s.", op_desc->GetName().c_str());
  199. (void)AttrUtils::SetInt(op_desc, ATTR_NAME_LABEL_SWITCH_INDEX, index);
  200. NodePtr label_goto = graph->AddNode(op_desc);
  201. GE_CHECK_NOTNULL_EXEC(label_goto, return nullptr);
  202. // Link control edge to graph tail.
  203. LinkToGraphTail(graph, label_goto);
  204. return label_goto;
  205. }
  206. /**
  207. * @ingroup ge
  208. * @brief Add LabelSwitch node at graph front.
  209. * @param [in] graph: graph for add node.
  210. * @param [in] name: label switch node name.
  211. * @param [in] desc: label index data desc.
  212. * @param [in] labels: label id for switch.
  213. * @return: NodePtr for success / nullptr for fail
  214. */
  215. NodePtr LabelMaker::AddLabelSwitchEnter(const ComputeGraphPtr &graph, const std::string &name, const GeTensorDesc &desc,
  216. const std::vector<uint32_t> &labels) {
  217. GE_CHECK_NOTNULL_EXEC(graph, return nullptr);
  218. const auto &node_list = graph->GetDirectNode();
  219. auto it = node_list.begin();
  220. if (it == node_list.end()) {
  221. GELOGE(INTERNAL_ERROR, "LabelSwitchByIndex: Graph %s node is empty.", graph->GetName().c_str());
  222. return nullptr;
  223. }
  224. OpDescPtr op_desc = MakeShared<OpDesc>(name, LABELSWITCHBYINDEX);
  225. GE_CHECK_NOTNULL_EXEC(op_desc, return nullptr);
  226. (void)AttrUtils::SetBool(op_desc, ATTR_NAME_RTS_LABEL_NODE, true);
  227. GELOGI("LabelSwitchByIndex: Create node %s.", op_desc->GetName().c_str());
  228. if (op_desc->AddInputDesc(desc) != GRAPH_SUCCESS) {
  229. GELOGE(INTERNAL_ERROR, "LabelSwitchByIndex: Add input desc failed.");
  230. return nullptr;
  231. }
  232. if (!AttrUtils::SetListInt(op_desc, ATTR_NAME_LABEL_SWITCH_LIST, labels)) {
  233. GELOGE(INTERNAL_ERROR, "LabelSwitchByIndex: Add %s failed.", ATTR_NAME_LABEL_SWITCH_INDEX.c_str());
  234. return nullptr;
  235. }
  236. NodePtr label_switch = graph->AddNodeFront(op_desc);
  237. if (label_switch == nullptr) {
  238. GELOGE(INTERNAL_ERROR, "LabelSwitchByIndex: Add to graph %s failed.", graph->GetName().c_str());
  239. return nullptr;
  240. }
  241. return label_switch;
  242. }
  243. /**
  244. * @ingroup ge
  245. * @brief Add LabelSwitch node at graph back.
  246. * @param [in] graph: graph for add node.
  247. * @param [in] name: label switch node name.
  248. * @param [in] desc: label index data desc.
  249. * @param [in] labels: label id for switch.
  250. * @return: NodePtr for success / nullptr for fail
  251. */
  252. NodePtr LabelMaker::AddLabelSwitchLeave(const ComputeGraphPtr &graph, const std::string &name, const GeTensorDesc &desc,
  253. const std::vector<uint32_t> &labels) {
  254. GE_CHECK_NOTNULL_EXEC(graph, return nullptr);
  255. OpDescPtr op_desc = MakeShared<OpDesc>(name, LABELSWITCHBYINDEX);
  256. GE_CHECK_NOTNULL_EXEC(op_desc, return nullptr);
  257. (void)AttrUtils::SetBool(op_desc, ATTR_NAME_RTS_LABEL_NODE, true);
  258. GELOGI("LabelSwitchByIndex: Create node %s.", op_desc->GetName().c_str());
  259. if (op_desc->AddInputDesc(desc) != GRAPH_SUCCESS) {
  260. GELOGE(INTERNAL_ERROR, "LabelSwitchByIndex: Add input desc failed.");
  261. return nullptr;
  262. }
  263. if (!AttrUtils::SetListInt(op_desc, ATTR_NAME_LABEL_SWITCH_LIST, labels)) {
  264. GELOGE(INTERNAL_ERROR, "LabelSwitchByIndex: Add %s failed.", ATTR_NAME_LABEL_SWITCH_INDEX.c_str());
  265. return nullptr;
  266. }
  267. NodePtr label_switch = graph->AddNode(op_desc);
  268. GE_CHECK_NOTNULL_EXEC(label_switch, return nullptr);
  269. // Link control edge to graph tail.
  270. LinkToGraphTail(graph, label_switch);
  271. return label_switch;
  272. }
  273. /**
  274. * @ingroup ge
  275. * @brief Add Data node at graph front for switch input.
  276. * @param [in] graph: graph for add node.
  277. * @param [in] name: label switch node name.
  278. * @param [in] desc: label index data desc.
  279. * @param [in] sw_node: switch node for add input.
  280. * @param [in] parent_index: index for parent node.
  281. * @return: NodePtr for success / nullptr for fail
  282. */
  283. NodePtr LabelMaker::AddLabelSwitchIndex(const ComputeGraphPtr &graph, const std::string &name, const GeTensorDesc &desc,
  284. const NodePtr &sw_node, uint32_t parent_index) {
  285. GE_CHECK_NOTNULL_EXEC(graph, return nullptr);
  286. OpDescPtr op_desc = MakeShared<OpDesc>(name, DATA);
  287. GE_CHECK_NOTNULL_EXEC(op_desc, return nullptr);
  288. GELOGI("Data: Create node %s.", op_desc->GetName().c_str());
  289. if (op_desc->AddInputDesc(desc) != GRAPH_SUCCESS) {
  290. GELOGE(INTERNAL_ERROR, "LabelSwitchByIndex: Add data input desc failed.");
  291. return nullptr;
  292. }
  293. if (op_desc->AddOutputDesc(desc) != GRAPH_SUCCESS) {
  294. GELOGE(INTERNAL_ERROR, "LabelSwitchByIndex: Add data output desc failed.");
  295. return nullptr;
  296. }
  297. if (!AttrUtils::SetInt(op_desc, ATTR_NAME_PARENT_NODE_INDEX, parent_index)) {
  298. GELOGE(INTERNAL_ERROR, "LabelSwitchByIndex: Add %s failed.", ATTR_NAME_PARENT_NODE_INDEX.c_str());
  299. return nullptr;
  300. }
  301. NodePtr op_data = graph->AddNodeFront(op_desc);
  302. GE_CHECK_NOTNULL_EXEC(op_data, return nullptr);
  303. GE_CHECK_NOTNULL_EXEC(graph->AddInputNode(op_data), return nullptr); // take as input node for memory assign.
  304. // Link control edge to graph head.
  305. if (GraphUtils::AddEdge(op_data->GetOutDataAnchor(0), sw_node->GetInDataAnchor(0)) != SUCCESS) {
  306. GELOGE(INTERNAL_ERROR, "LabelSwitchByIndex: Add input edge to %s failed.", op_data->GetName().c_str());
  307. return nullptr;
  308. }
  309. return op_data;
  310. }
  311. } // namespace ge

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