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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 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
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 char *const kInputMutable = "_input_mutable";
  28. } // namespace
  29. namespace ge {
  30. Status HcclMemcpyPass::Run(ge::ComputeGraphPtr graph) {
  31. Status ret = SUCCESS;
  32. GE_IF_BOOL_EXEC(graph == nullptr, GELOGE(PARAM_INVALID, "param [graph] must not be null."); return PARAM_INVALID);
  33. for (const auto &node : graph->GetDirectNode()) {
  34. auto op_desc = node->GetOpDesc();
  35. if (op_desc == nullptr) {
  36. GELOGE(INTERNAL_ERROR, "node has no op_desc, node_name : %s.", node->GetName().c_str());
  37. return INTERNAL_ERROR;
  38. }
  39. ret = ContinuousInputProcess(graph, node);
  40. if (ret != SUCCESS) {
  41. GELOGE(INTERNAL_ERROR, "failed ProcessBroadcastMemcpy, node_name:%s.", node->GetName().c_str());
  42. return ret;
  43. }
  44. ret = MutableInputProcess(graph, node);
  45. if (ret != SUCCESS) {
  46. GELOGE(INTERNAL_ERROR, "failed MutableInputProcess, node_name:%s.", node->GetName().c_str());
  47. return ret;
  48. }
  49. ret = P2pmemInputProcess(graph, node);
  50. if (ret != SUCCESS) {
  51. GELOGE(INTERNAL_ERROR, "failed P2pmemInputProcess, node_name:%s.", node->GetName().c_str());
  52. return ret;
  53. }
  54. }
  55. return ret;
  56. }
  57. // If node has _input_mutable attr, means input mem may be modified when op execute.
  58. // In order to avoid to affect another op execute with same input when data modified,
  59. // need to inset memcpy node between.
  60. // also works on situation that input is variable or const.
  61. Status HcclMemcpyPass::MutableInputProcess(const ComputeGraphPtr &graph, const NodePtr node) {
  62. auto op_desc = node->GetOpDesc();
  63. bool node_input_mutable = false;
  64. if (!AttrUtils::HasAttr(op_desc, kInputMutable)) {
  65. return SUCCESS;
  66. }
  67. if (!AttrUtils::GetBool(op_desc, kInputMutable, node_input_mutable)) {
  68. GELOGE(INTERNAL_ERROR, "node:%s get attr:_input_mutable failed.", node->GetName().c_str());
  69. return FAILED;
  70. }
  71. if (!node_input_mutable) {
  72. return SUCCESS;
  73. }
  74. GELOGI("input mutable hcom op is:%s.", op_desc->GetName().c_str());
  75. for (auto &hccl_in_anchor : node->GetAllInDataAnchors()) {
  76. if (hccl_in_anchor == nullptr) {
  77. continue;
  78. }
  79. auto src_out_anchor = hccl_in_anchor->GetPeerOutAnchor();
  80. GE_CHECK_NOTNULL(src_out_anchor);
  81. int32_t src_out_anchor_size = src_out_anchor->GetPeerInDataAnchors().size();
  82. if (src_out_anchor_size == kAnchorSize) {
  83. // Identity needs to be inserted between constant (/data) and hcomallreduce to avoid constant being cleared.
  84. if (IsDataNode(src_out_anchor->GetOwnerNode()->GetType())) {
  85. Status ret = ModifyEdgeConnection(graph, src_out_anchor, hccl_in_anchor);
  86. if (ret != SUCCESS) {
  87. GELOGE(INTERNAL_ERROR, "Failed to modify the connection.");
  88. return ret;
  89. }
  90. }
  91. continue;
  92. }
  93. Status ret = ModifyEdgeConnection(graph, src_out_anchor, hccl_in_anchor);
  94. if (ret != SUCCESS) {
  95. GELOGE(INTERNAL_ERROR, "Failed to modify the connection.");
  96. return ret;
  97. }
  98. }
  99. return SUCCESS;
  100. }
  101. // If broadcast input size is bigger than 1, and input from variable,
  102. // cause by broadcast input memory should be continuous,
  103. // another featuremap mem will be allocated for broadcast input.
  104. // In this condition, move data from variable mem to broadcast input featuremap mem will be executed each step.
  105. // In order to avoid move action out of model, use memcpy node instead of move action code.
  106. Status HcclMemcpyPass::ContinuousInputProcess(const ComputeGraphPtr &graph, const NodePtr node) {
  107. auto op_desc = node->GetOpDesc();
  108. bool is_input_continuous = false;
  109. (void)ge::AttrUtils::GetBool(op_desc, ATTR_NAME_CONTINUOUS_INPUT, is_input_continuous);
  110. if (is_input_continuous && op_desc->GetInputsSize() > 1) {
  111. GELOGI("continuous input op is:%s.", op_desc->GetName().c_str());
  112. // if input size bigger than one, insert memcpy between var data for support continous mem alloc
  113. for (auto &hccl_in_anchor : node->GetAllInDataAnchors()) {
  114. if (hccl_in_anchor == nullptr) {
  115. continue;
  116. }
  117. auto src_out_anchor = hccl_in_anchor->GetPeerOutAnchor();
  118. if (src_out_anchor == nullptr) {
  119. GELOGE(INTERNAL_ERROR, "hcom op input has no peer anchor, node_name:%s", node->GetName().c_str());
  120. return INTERNAL_ERROR;
  121. }
  122. if (IsDataNode(src_out_anchor->GetOwnerNode()->GetType())) {
  123. Status ret = ModifyEdgeConnection(graph, src_out_anchor, hccl_in_anchor);
  124. if (ret != SUCCESS) {
  125. GELOGE(INTERNAL_ERROR, "Failed to modify the connection.");
  126. return ret;
  127. }
  128. }
  129. }
  130. }
  131. return SUCCESS;
  132. }
  133. // if input is var type, and node input need p2p mem, then memcpy should be insert between the two
  134. Status HcclMemcpyPass::P2pmemInputProcess(const ComputeGraphPtr &graph, const NodePtr node) {
  135. auto op_desc = node->GetOpDesc();
  136. vector<int64_t> input_memory_types;
  137. (void) ge::AttrUtils::GetListInt(op_desc, ATTR_NAME_INPUT_MEM_TYPE_LIST, input_memory_types);
  138. if (input_memory_types.empty()) {
  139. return SUCCESS;
  140. }
  141. for (uint32_t index = 0; index < input_memory_types.size() && index < op_desc->GetInputsSize(); index++) {
  142. if (input_memory_types[index] != RT_MEMORY_P2P_DDR) {
  143. continue;
  144. }
  145. GELOGI("p2p input op is:%s.", op_desc->GetName().c_str());
  146. auto hccl_in_anchor = node->GetInDataAnchor(index);
  147. if (hccl_in_anchor == nullptr) {
  148. continue;
  149. }
  150. auto src_out_anchor = hccl_in_anchor->GetPeerOutAnchor();
  151. if (src_out_anchor == nullptr) {
  152. GELOGE(INTERNAL_ERROR, "hcom op input has no peer anchor, node_name:%s", node->GetName().c_str());
  153. return INTERNAL_ERROR;
  154. }
  155. if (IsDataNode(src_out_anchor->GetOwnerNode()->GetType())) {
  156. Status ret = ModifyEdgeConnection(graph, src_out_anchor, hccl_in_anchor);
  157. if (ret != SUCCESS) {
  158. GELOGE(INTERNAL_ERROR, "Failed to modify the connection.");
  159. return ret;
  160. }
  161. }
  162. }
  163. return SUCCESS;
  164. }
  165. bool HcclMemcpyPass::IsDataNode(const std::string& node_type) {
  166. return (node_type == CONSTANTOP) || (node_type == VARIABLE) || (node_type == DATA) || (node_type == CONSTANT);
  167. }
  168. ///
  169. /// @brief Add Identity Node
  170. /// @param [in] ge::ComputeGraphPtr graph
  171. /// @param [in] ge::OutDataAnchorPtr in_node
  172. /// @return ge::NodePtr
  173. ///
  174. NodePtr HcclMemcpyPass::CreateIdentityNode(const ComputeGraphPtr &graph, const OutDataAnchorPtr &out_data_anchor) {
  175. GE_IF_BOOL_EXEC(graph == nullptr, return nullptr);
  176. NodePtr pre_node = out_data_anchor->GetOwnerNode();
  177. OpDescPtr pre_op_desc = pre_node->GetOpDesc();
  178. if (pre_op_desc == nullptr) {
  179. GELOGE(INTERNAL_ERROR, "OpDesc of pre node is invalid.");
  180. return nullptr;
  181. }
  182. std::string node_name = pre_node->GetName() + "_" + IDENTITY;
  183. node_name = CheckDuplicateName(node_name);
  184. OpDescPtr op_desc = MakeShared<OpDesc>(node_name.c_str(), IDENTITY);
  185. if (op_desc == nullptr) {
  186. GELOGE(INTERNAL_ERROR, "Create Identity op: MakeShared op_desc fail.");
  187. return nullptr;
  188. }
  189. GELOGI("Create Identity op:%s.", op_desc->GetName().c_str());
  190. graphStatus ret = op_desc->AddInputDesc("x", pre_op_desc->GetOutputDesc(out_data_anchor->GetIdx()));
  191. if (ret != GRAPH_SUCCESS) {
  192. GELOGE(INTERNAL_ERROR, "Create Identity op: add input desc fail.");
  193. return nullptr;
  194. }
  195. ret = op_desc->AddOutputDesc("y", pre_op_desc->GetOutputDesc(out_data_anchor->GetIdx()));
  196. if (ret != GRAPH_SUCCESS) {
  197. GELOGE(INTERNAL_ERROR, "Create Identity op: add output desc fail.");
  198. return nullptr;
  199. }
  200. // because history reason ,this pass can not do work after constant fold so mark it
  201. (void)AttrUtils::SetBool(op_desc, ATTR_NO_NEED_CONSTANT_FOLDING, false);
  202. NodePtr memcpy_node = graph->AddNode(op_desc);
  203. if (memcpy_node == nullptr) {
  204. GELOGE(INTERNAL_ERROR, "Insert Identity node fail.");
  205. return nullptr;
  206. }
  207. return memcpy_node;
  208. }
  209. ///
  210. /// @brief Check duplicate node_name
  211. /// @param [in] std::string& node_name
  212. /// @return std::string
  213. ///
  214. std::string HcclMemcpyPass::CheckDuplicateName(const std::string &node_name) {
  215. std::string tmp_name = node_name;
  216. auto iter = node_num_map_.find(tmp_name);
  217. if (iter != node_num_map_.end()) {
  218. tmp_name = tmp_name + "_" + std::to_string(iter->second);
  219. (iter->second)++;
  220. } else {
  221. node_num_map_[tmp_name] = 1;
  222. }
  223. return tmp_name;
  224. }
  225. ///
  226. /// @brief Modify edge connection
  227. /// @param [in] ComputeGraphPtr graph
  228. /// @param [in] OutDataAnchorPtr src_out_anchor
  229. /// @param [in] InDataAnchorPtr hccl_in_anchor
  230. /// @return status
  231. ///
  232. Status HcclMemcpyPass::ModifyEdgeConnection(const ComputeGraphPtr &graph, const OutDataAnchorPtr &src_out_anchor,
  233. const InDataAnchorPtr &hccl_in_anchor) {
  234. GELOGI("Between op %s and op %s need insert memcpy async op.", src_out_anchor->GetOwnerNode()->GetName().c_str(),
  235. hccl_in_anchor->GetOwnerNode()->GetName().c_str());
  236. NodePtr memcpy_node = CreateIdentityNode(graph, src_out_anchor);
  237. GE_CHECK_NOTNULL(memcpy_node);
  238. Status ret1 = src_out_anchor->Unlink(hccl_in_anchor);
  239. if (ret1 != SUCCESS) {
  240. GELOGE(INTERNAL_ERROR, "The op %s Unlink anchor %s fail.", src_out_anchor->GetOwnerNode()->GetName().c_str(),
  241. hccl_in_anchor->GetOwnerNode()->GetName().c_str());
  242. return FAILED;
  243. }
  244. auto out_data_anchor_0 = memcpy_node->GetOutDataAnchor(kAnchorNum);
  245. GE_CHECK_NOTNULL(out_data_anchor_0);
  246. ret1 = out_data_anchor_0->LinkTo(hccl_in_anchor);
  247. if (ret1 != SUCCESS) {
  248. GELOGE(INTERNAL_ERROR, "The op %s link anchor %s fail.", memcpy_node->GetName().c_str(),
  249. hccl_in_anchor->GetOwnerNode()->GetName().c_str());
  250. return FAILED;
  251. }
  252. Status ret = src_out_anchor->LinkTo(memcpy_node->GetInDataAnchor(kAnchorNum));
  253. if (ret != SUCCESS) {
  254. GELOGE(INTERNAL_ERROR, "The op %s link anchor %s fail.", src_out_anchor->GetOwnerNode()->GetName().c_str(),
  255. memcpy_node->GetName().c_str());
  256. return FAILED;
  257. }
  258. return SUCCESS;
  259. }
  260. ///
  261. /// @brief Clear Status, used for subgraph pass
  262. /// @return SUCCESS
  263. ///
  264. Status HcclMemcpyPass::ClearStatus() {
  265. node_num_map_.clear();
  266. return SUCCESS;
  267. }
  268. } // namespace ge

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