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.

hccl_memcpy_pass.cc 14 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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/passes/hccl_memcpy_pass.h"
  17. #include <string>
  18. #include "common/debug/log.h"
  19. #include "framework/common/debug/ge_log.h"
  20. #include "common/ge_inner_error_codes.h"
  21. #include "common/ge/ge_util.h"
  22. #include "framework/common/types.h"
  23. #include "graph/utils/graph_utils.h"
  24. namespace {
  25. const int32_t kAnchorSize = 1;
  26. const int kAnchorNum = 0;
  27. const int32_t kAnchorAssignRefIndex = 0;
  28. const int32_t kAnchorAssignValueIndex = 1;
  29. const char *const kInputMutable = "_input_mutable";
  30. } // namespace
  31. namespace ge {
  32. Status HcclMemcpyPass::Run(ge::ComputeGraphPtr graph) {
  33. GE_CHECK_NOTNULL(graph);
  34. for (const auto &node : graph->GetDirectNode()) {
  35. auto op_desc = node->GetOpDesc();
  36. if (op_desc == nullptr) {
  37. GELOGE(INTERNAL_ERROR, "node has no op_desc, node_name : %s.", node->GetName().c_str());
  38. return INTERNAL_ERROR;
  39. }
  40. Status ret = MutableInputProcess(graph, node);
  41. if (ret != SUCCESS) {
  42. GELOGE(INTERNAL_ERROR, "failed MutableInputProcess, node_name:%s.", node->GetName().c_str());
  43. return ret;
  44. }
  45. }
  46. return SUCCESS;
  47. }
  48. // If node has _input_mutable attr, means input mem may be modified when op execute.
  49. // In order to avoid to affect another op execute with same input when data modified,
  50. // need to inset memcpy node between.
  51. // also works on situation that input is variable or const.
  52. Status HcclMemcpyPass::MutableInputProcess(const ComputeGraphPtr &graph, const NodePtr node) {
  53. auto op_desc = node->GetOpDesc();
  54. bool node_input_mutable = false;
  55. if (!AttrUtils::HasAttr(op_desc, kInputMutable)) {
  56. return SUCCESS;
  57. }
  58. if (!AttrUtils::GetBool(op_desc, kInputMutable, node_input_mutable)) {
  59. GELOGE(INTERNAL_ERROR, "node:%s get attr:_input_mutable failed.", node->GetName().c_str());
  60. return FAILED;
  61. }
  62. if (!node_input_mutable) {
  63. return SUCCESS;
  64. }
  65. GELOGI("input mutable hcom op is:%s.", op_desc->GetName().c_str());
  66. for (auto &hccl_in_anchor : node->GetAllInDataAnchors()) {
  67. if (hccl_in_anchor == nullptr) {
  68. continue;
  69. }
  70. auto src_out_anchor = hccl_in_anchor->GetPeerOutAnchor();
  71. GE_CHECK_NOTNULL(src_out_anchor);
  72. int32_t src_out_anchor_size = src_out_anchor->GetPeerInDataAnchors().size();
  73. if (src_out_anchor_size == kAnchorSize) {
  74. // Identity needs to be inserted between constant (/data) and hcomallreduce to avoid constant being cleared.
  75. if (IsDataNode(src_out_anchor->GetOwnerNode()->GetType())) {
  76. Status ret = ModifyEdgeConnection(graph, src_out_anchor, hccl_in_anchor);
  77. if (ret != SUCCESS) {
  78. GELOGE(INTERNAL_ERROR, "Failed to modify the connection.");
  79. return ret;
  80. }
  81. }
  82. continue;
  83. }
  84. Status ret = ModifyEdgeConnection(graph, src_out_anchor, hccl_in_anchor);
  85. if (ret != SUCCESS) {
  86. GELOGE(INTERNAL_ERROR, "Failed to modify the connection.");
  87. return ret;
  88. }
  89. }
  90. return SUCCESS;
  91. }
  92. bool HcclMemcpyPass::IsDataNode(const std::string& node_type) {
  93. return (node_type == CONSTANTOP) || (node_type == VARIABLE) || (node_type == DATA) || (node_type == CONSTANT);
  94. }
  95. ///
  96. /// @brief Add Identity Node
  97. /// @param [in] ge::ComputeGraphPtr graph
  98. /// @param [in] ge::OutDataAnchorPtr in_node
  99. /// @return ge::NodePtr
  100. ///
  101. NodePtr HcclMemcpyPass::CreateIdentityNode(const ComputeGraphPtr &graph, const OutDataAnchorPtr &out_data_anchor) {
  102. GE_CHECK_NOTNULL_EXEC(graph, return nullptr);
  103. NodePtr pre_node = out_data_anchor->GetOwnerNode();
  104. OpDescPtr pre_op_desc = pre_node->GetOpDesc();
  105. if (pre_op_desc == nullptr) {
  106. GELOGE(INTERNAL_ERROR, "OpDesc of pre node is invalid.");
  107. return nullptr;
  108. }
  109. std::string node_name = pre_node->GetName() + "_" + IDENTITY;
  110. node_name = CheckDuplicateName(node_name);
  111. OpDescPtr op_desc = MakeShared<OpDesc>(node_name.c_str(), IDENTITY);
  112. if (op_desc == nullptr) {
  113. GELOGE(INTERNAL_ERROR, "Create Identity op: MakeShared op_desc fail.");
  114. return nullptr;
  115. }
  116. GELOGI("Create Identity op:%s.", op_desc->GetName().c_str());
  117. graphStatus ret = op_desc->AddInputDesc("x", pre_op_desc->GetOutputDesc(out_data_anchor->GetIdx()));
  118. if (ret != GRAPH_SUCCESS) {
  119. GELOGE(INTERNAL_ERROR, "Create Identity op: add input desc fail.");
  120. return nullptr;
  121. }
  122. ret = op_desc->AddOutputDesc("y", pre_op_desc->GetOutputDesc(out_data_anchor->GetIdx()));
  123. if (ret != GRAPH_SUCCESS) {
  124. GELOGE(INTERNAL_ERROR, "Create Identity op: add output desc fail.");
  125. return nullptr;
  126. }
  127. // because history reason ,this pass can not do work after constant fold so mark it
  128. (void)AttrUtils::SetBool(op_desc, ATTR_NO_NEED_CONSTANT_FOLDING, false);
  129. NodePtr memcpy_node = graph->AddNode(op_desc);
  130. if (memcpy_node == nullptr) {
  131. GELOGE(INTERNAL_ERROR, "Insert Identity node fail.");
  132. return nullptr;
  133. }
  134. return memcpy_node;
  135. }
  136. ///
  137. /// @brief Check duplicate node_name
  138. /// @param [in] std::string& node_name
  139. /// @return std::string
  140. ///
  141. std::string HcclMemcpyPass::CheckDuplicateName(const std::string &node_name) {
  142. std::string tmp_name = node_name;
  143. auto iter = node_num_map_.find(tmp_name);
  144. if (iter != node_num_map_.end()) {
  145. tmp_name = tmp_name + "_" + std::to_string(iter->second);
  146. (iter->second)++;
  147. } else {
  148. node_num_map_[tmp_name] = 1;
  149. }
  150. return tmp_name;
  151. }
  152. ///
  153. /// @brief Modify edge connection
  154. /// @param [in] ComputeGraphPtr graph
  155. /// @param [in] OutDataAnchorPtr src_out_anchor
  156. /// @param [in] InDataAnchorPtr hccl_in_anchor
  157. /// @return status
  158. ///
  159. Status HcclMemcpyPass::ModifyEdgeConnection(const ComputeGraphPtr &graph, const OutDataAnchorPtr &src_out_anchor,
  160. const InDataAnchorPtr &hccl_in_anchor) {
  161. GE_CHECK_NOTNULL(src_out_anchor->GetOwnerNode());
  162. GE_CHECK_NOTNULL(hccl_in_anchor->GetOwnerNode());
  163. Status ret = InsertIdentityBeforeHccl(graph, src_out_anchor, hccl_in_anchor);
  164. if (ret != SUCCESS) {
  165. GELOGE(INTERNAL_ERROR, "add identity failed, var_node:%s, hccl_node:%s.",
  166. src_out_anchor->GetOwnerNode()->GetName().c_str(),
  167. hccl_in_anchor->GetOwnerNode()->GetName().c_str());
  168. return ret;
  169. }
  170. ret = InsertAssignAfterBroadcastIfNeed(graph, src_out_anchor, hccl_in_anchor);
  171. if (ret != SUCCESS) {
  172. GELOGE(INTERNAL_ERROR, "add assign failed, var_node:%s, hccl_node:%s.",
  173. src_out_anchor->GetOwnerNode()->GetName().c_str(),
  174. hccl_in_anchor->GetOwnerNode()->GetName().c_str());
  175. return ret;
  176. }
  177. return SUCCESS;
  178. }
  179. ///
  180. /// @brief Insert Identity node Between Hccl node and variable
  181. /// @param [in] ComputeGraphPtr graph
  182. /// @param [in] OutDataAnchorPtr src_out_anchor
  183. /// @param [in] InDataAnchorPtr hccl_in_anchor
  184. /// @return status
  185. ///
  186. Status HcclMemcpyPass::InsertIdentityBeforeHccl(const ComputeGraphPtr &graph, const OutDataAnchorPtr &src_out_anchor,
  187. const InDataAnchorPtr &hccl_in_anchor) {
  188. GELOGI("Between op %s and op %s need insert memcpy async op.", src_out_anchor->GetOwnerNode()->GetName().c_str(),
  189. hccl_in_anchor->GetOwnerNode()->GetName().c_str());
  190. NodePtr memcpy_node = CreateIdentityNode(graph, src_out_anchor);
  191. GE_CHECK_NOTNULL(memcpy_node);
  192. Status ret1 = src_out_anchor->Unlink(hccl_in_anchor);
  193. if (ret1 != SUCCESS) {
  194. GELOGE(INTERNAL_ERROR, "The op %s Unlink anchor %s fail.", src_out_anchor->GetOwnerNode()->GetName().c_str(),
  195. hccl_in_anchor->GetOwnerNode()->GetName().c_str());
  196. return FAILED;
  197. }
  198. auto out_data_anchor_0 = memcpy_node->GetOutDataAnchor(kAnchorNum);
  199. GE_CHECK_NOTNULL(out_data_anchor_0);
  200. ret1 = out_data_anchor_0->LinkTo(hccl_in_anchor);
  201. if (ret1 != SUCCESS) {
  202. GELOGE(INTERNAL_ERROR, "The op %s link anchor %s fail.", memcpy_node->GetName().c_str(),
  203. hccl_in_anchor->GetOwnerNode()->GetName().c_str());
  204. return FAILED;
  205. }
  206. Status ret = src_out_anchor->LinkTo(memcpy_node->GetInDataAnchor(kAnchorNum));
  207. if (ret != SUCCESS) {
  208. GELOGE(INTERNAL_ERROR, "The op %s link anchor %s fail.", src_out_anchor->GetOwnerNode()->GetName().c_str(),
  209. memcpy_node->GetName().c_str());
  210. return FAILED;
  211. }
  212. return SUCCESS;
  213. }
  214. ///
  215. /// @brief Insert assign node after broadcast node and variable to refresh variable data
  216. /// @param [in] ComputeGraphPtr graph
  217. /// @param [in] OutDataAnchorPtr var_out_anchor
  218. /// @param [in] InDataAnchorPtr hccl_in_anchor
  219. /// @return status
  220. ///
  221. Status HcclMemcpyPass::InsertAssignAfterBroadcastIfNeed(const ComputeGraphPtr &graph,
  222. const OutDataAnchorPtr &var_out_anchor,
  223. const InDataAnchorPtr &hccl_in_anchor) {
  224. if (hccl_in_anchor->GetOwnerNode()->GetType() != HCOMBROADCAST) {
  225. GELOGD("%s not broadcast, no need to insert assign node", hccl_in_anchor->GetOwnerNode()->GetName().c_str());
  226. return SUCCESS;
  227. }
  228. if (var_out_anchor->GetOwnerNode()->GetType() != VARIABLE) {
  229. GELOGD("%s not variable, no need to insert assign node", var_out_anchor->GetOwnerNode()->GetName().c_str());
  230. return SUCCESS;
  231. }
  232. GELOGI("after op %s and op %s need insert assign op.", var_out_anchor->GetOwnerNode()->GetName().c_str(),
  233. hccl_in_anchor->GetOwnerNode()->GetName().c_str());
  234. for (auto peer_in_anchor : var_out_anchor->GetPeerInDataAnchors()) {
  235. if (peer_in_anchor->GetOwnerNode()->GetType() == ASSIGN) {
  236. GELOGD("variable %s out assign node is exist.", var_out_anchor->GetOwnerNode()->GetName().c_str());
  237. return SUCCESS;
  238. }
  239. }
  240. NodePtr assign_node = CreateAssignNode(graph, var_out_anchor);
  241. GE_CHECK_NOTNULL(assign_node);
  242. OutDataAnchorPtr hccl_out_anchor = hccl_in_anchor->GetOwnerNode()->GetOutDataAnchor(hccl_in_anchor->GetIdx());
  243. GE_CHECK_NOTNULL(hccl_out_anchor);
  244. Status ret = hccl_out_anchor->LinkTo(assign_node->GetInDataAnchor(kAnchorAssignValueIndex));
  245. if (ret != SUCCESS) {
  246. GELOGE(INTERNAL_ERROR, "The op %s link anchor %s fail.", hccl_out_anchor->GetOwnerNode()->GetName().c_str(),
  247. assign_node->GetName().c_str());
  248. return FAILED;
  249. }
  250. ret = var_out_anchor->LinkTo(assign_node->GetInDataAnchor(kAnchorAssignRefIndex));
  251. if (ret != SUCCESS) {
  252. GELOGE(INTERNAL_ERROR, "The op %s link anchor %s fail.", var_out_anchor->GetOwnerNode()->GetName().c_str(),
  253. assign_node->GetName().c_str());
  254. return FAILED;
  255. }
  256. // add control edge between assign node and node after broadcast node
  257. OutControlAnchorPtr assign_out_control_anchor = assign_node->GetOutControlAnchor();
  258. GE_CHECK_NOTNULL(assign_out_control_anchor);
  259. for (auto in_data_anchor : hccl_out_anchor->GetPeerInDataAnchors()) {
  260. if (in_data_anchor->GetOwnerNode()->GetName() == assign_node->GetName()) {
  261. continue;
  262. }
  263. ret = assign_out_control_anchor->LinkTo(in_data_anchor->GetOwnerNode()->GetInControlAnchor());
  264. if (ret != SUCCESS) {
  265. GELOGE(INTERNAL_ERROR, "The op %s link control anchor %s fail.",
  266. assign_out_control_anchor->GetOwnerNode()->GetName().c_str(),
  267. in_data_anchor->GetOwnerNode()->GetName().c_str());
  268. return FAILED;
  269. }
  270. }
  271. for (auto in_control_anchor : hccl_out_anchor->GetOwnerNode()->GetOutControlAnchor()->GetPeerInControlAnchors()) {
  272. if (in_control_anchor->GetOwnerNode()->GetName() == assign_node->GetName()) {
  273. continue;
  274. }
  275. ret = assign_out_control_anchor->LinkTo(in_control_anchor);
  276. if (ret != SUCCESS) {
  277. GELOGE(INTERNAL_ERROR, "The op %s link control anchor %s fail.",
  278. assign_out_control_anchor->GetOwnerNode()->GetName().c_str(),
  279. in_control_anchor->GetOwnerNode()->GetName().c_str());
  280. return FAILED;
  281. }
  282. }
  283. return SUCCESS;
  284. }
  285. ///
  286. /// @brief create assign Node, add to graph
  287. /// @param [in] ge::ComputeGraphPtr graph
  288. /// @param [in] ge::OutDataAnchorPtr variable node out anchor
  289. /// @return ge::NodePtr
  290. ///
  291. NodePtr HcclMemcpyPass::CreateAssignNode(const ComputeGraphPtr &graph, const OutDataAnchorPtr &out_data_anchor) {
  292. GE_CHECK_NOTNULL_EXEC(graph, return nullptr);
  293. NodePtr pre_node = out_data_anchor->GetOwnerNode();
  294. OpDescPtr pre_op_desc = pre_node->GetOpDesc();
  295. if (pre_op_desc == nullptr) {
  296. GELOGE(INTERNAL_ERROR, "OpDesc of pre node is invalid.");
  297. return nullptr;
  298. }
  299. std::string node_name = pre_node->GetName() + "_" + ASSIGN;
  300. node_name = CheckDuplicateName(node_name);
  301. OpDescPtr op_desc = MakeShared<OpDesc>(node_name.c_str(), ASSIGN);
  302. if (op_desc == nullptr) {
  303. GELOGE(INTERNAL_ERROR, "Create Assign op: MakeShared op_desc fail.");
  304. return nullptr;
  305. }
  306. GELOGI("Create Assign op:%s.", op_desc->GetName().c_str());
  307. graphStatus ret = op_desc->AddInputDesc("ref", pre_op_desc->GetOutputDesc(out_data_anchor->GetIdx()));
  308. if (ret != GRAPH_SUCCESS) {
  309. GELOGE(INTERNAL_ERROR, "Create Assign op: add ref input desc fail.");
  310. return nullptr;
  311. }
  312. ret = op_desc->AddInputDesc("value", pre_op_desc->GetOutputDesc(out_data_anchor->GetIdx()));
  313. if (ret != GRAPH_SUCCESS) {
  314. GELOGE(INTERNAL_ERROR, "Create Assign op: add value input desc fail.");
  315. return nullptr;
  316. }
  317. ret = op_desc->AddOutputDesc("ref", pre_op_desc->GetOutputDesc(out_data_anchor->GetIdx()));
  318. if (ret != GRAPH_SUCCESS) {
  319. GELOGE(INTERNAL_ERROR, "Create Assign op: add output desc fail.");
  320. return nullptr;
  321. }
  322. NodePtr assign_node = graph->AddNode(op_desc);
  323. if (assign_node == nullptr) {
  324. GELOGE(INTERNAL_ERROR, "Insert Identity node fail.");
  325. return nullptr;
  326. }
  327. return assign_node;
  328. }
  329. ///
  330. /// @brief Clear Status, used for subgraph pass
  331. /// @return SUCCESS
  332. ///
  333. Status HcclMemcpyPass::ClearStatus() {
  334. node_num_map_.clear();
  335. return SUCCESS;
  336. }
  337. } // namespace ge

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