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_continuous_memcpy_pass.cc 16 kB

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

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