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.

memcpy_addr_async_pass.cc 15 kB

4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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/memcpy_addr_async_pass.h"
  17. #include "common/ge/ge_util.h"
  18. #include "framework/common/debug/log.h"
  19. #include "graph/utils/node_utils.h"
  20. #include "graph/utils/op_desc_utils.h"
  21. #include "graph/utils/tensor_utils.h"
  22. namespace ge {
  23. Status MemcpyAddrAsyncPass::Run(ComputeGraphPtr graph) {
  24. GE_CHECK_NOTNULL(graph);
  25. int64_t value = 0;
  26. rtError_t rt_ret = rtGetRtCapability(FEATURE_TYPE_MEMCPY, MEMCPY_INFO_SUPPORT_ZEROCOPY, &value);
  27. if (rt_ret != RT_ERROR_NONE) {
  28. GELOGE(RT_FAILED, "rtGetRtCapability failed, error=0x%x.", rt_ret);
  29. return RT_FAILED;
  30. }
  31. for (auto &node : graph->GetAllNodes()) {
  32. auto op_desc = node->GetOpDesc();
  33. GE_IF_BOOL_EXEC(op_desc == nullptr, continue);
  34. if (op_desc->GetType() == STREAMSWITCHN || op_desc->GetType() == STREAMMERGE) {
  35. Status ret = AddMemcpyAddrAsyncNode(graph, node);
  36. if (ret != SUCCESS) {
  37. GELOGE(ret, "AddMemcpyAddrAsyncNode failed.");
  38. return ret;
  39. }
  40. }
  41. // handle data->netoutput, const->netoutput in root graph, use mem_addr_async to improve performance
  42. if (op_desc->GetType() == NETOUTPUT) {
  43. // check this netoutput is on root graph
  44. if (node->GetOwnerComputeGraph()->GetParentNode() == nullptr) {
  45. Status ret = InsertMemAddrAsyncNodeBeforeNetoutput(node->GetOwnerComputeGraph(), node);
  46. if (ret != SUCCESS) {
  47. GELOGE(ret, "AddMemcpyAddrAsyncNode failed.");
  48. return ret;
  49. }
  50. }
  51. }
  52. }
  53. return SUCCESS;
  54. }
  55. Status MemcpyAddrAsyncPass::AddMemcpyAddrAsyncNode(const ComputeGraphPtr &graph, const NodePtr &node) {
  56. GELOGI("Start AddMemcpyAddrAsyncNode for %s.", node->GetName().c_str());
  57. for (InDataAnchorPtr &in_data_anchor : node->GetAllInDataAnchors()) {
  58. OutDataAnchorPtr peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  59. GE_IF_BOOL_EXEC(peer_out_anchor == nullptr, continue);
  60. NodePtr in_node = peer_out_anchor->GetOwnerNode();
  61. if (in_node->GetType() == DATA) {
  62. ComputeGraphPtr owner_graph = in_node->GetOwnerComputeGraph();
  63. GE_CHECK_NOTNULL(owner_graph);
  64. // Data is in parent_graph
  65. if (owner_graph->GetParentGraph() == nullptr) {
  66. GELOGI("Need to insert MemcpyAddrAsync directly when data in parent graph.");
  67. NodePtr memcpy_addr_async_node = CreateMemcpyAddrAsyncNode(graph, peer_out_anchor, node);
  68. GE_IF_BOOL_EXEC(memcpy_addr_async_node == nullptr, GELOGE(INTERNAL_ERROR, "CreateMemcpyAddrAsyncNode failed.");
  69. return INTERNAL_ERROR);
  70. Status ret = InsertMemcpyAddrAsyncNode(peer_out_anchor, in_data_anchor, memcpy_addr_async_node);
  71. GE_IF_BOOL_EXEC(ret != SUCCESS, GELOGE(ret, "InsertMemcpyAddrAsyncNode failed."); return ret);
  72. } else {
  73. uint32_t parent_index = 0;
  74. if (!AttrUtils::GetInt(in_node->GetOpDesc(), ATTR_NAME_PARENT_NODE_INDEX, parent_index)) {
  75. GELOGE(INTERNAL_ERROR, "Failed to get parent index of %s", in_node->GetName().c_str());
  76. return INTERNAL_ERROR;
  77. }
  78. // Data is in sub_graph
  79. GELOGI("Need to find data in parent graph, then insert MemcpyAddrAsync.");
  80. NodePtr parent_node = owner_graph->GetParentNode();
  81. user_data_for_known_ = in_node;
  82. out_of_user_data_for_known_ = node;
  83. peer_out_anchor_for_known_ = peer_out_anchor;
  84. in_anchor_for_known_ = in_data_anchor;
  85. FindUserData(parent_node, parent_index);
  86. if (find_user_data_) {
  87. GELOGI("Insert memcpy_addr_async for non_dynamic.");
  88. GE_CHECK_NOTNULL(peer_out_anchor_);
  89. NodePtr memcpy_addr_async_node = CreateMemcpyAddrAsyncNode(graph, peer_out_anchor_, out_of_user_data_);
  90. GE_IF_BOOL_EXEC(memcpy_addr_async_node == nullptr,
  91. GELOGE(INTERNAL_ERROR, "CreateMemcpyAddrAsyncNode failed.");
  92. return INTERNAL_ERROR);
  93. Status ret = InsertMemcpyAddrAsyncNode(peer_out_anchor_, in_anchor_, memcpy_addr_async_node);
  94. GE_IF_BOOL_EXEC(ret != SUCCESS, GELOGE(ret, "InsertMemcpyAddrAsyncNode failed."); return ret);
  95. }
  96. if (find_user_data_for_known_) {
  97. GELOGI("Insert memcpy_addr_async for known graph.");
  98. auto sub_graph = user_data_for_known_->GetOwnerComputeGraph();
  99. NodePtr memcpy_addr_async_node =
  100. CreateMemcpyAddrAsyncNode(sub_graph, peer_out_anchor_for_known_, out_of_user_data_for_known_);
  101. GE_IF_BOOL_EXEC(memcpy_addr_async_node == nullptr,
  102. GELOGE(INTERNAL_ERROR, "CreateMemcpyAddrAsyncNode for known failed.");
  103. return INTERNAL_ERROR);
  104. Status ret =
  105. InsertMemcpyAddrAsyncNode(peer_out_anchor_for_known_, in_anchor_for_known_, memcpy_addr_async_node);
  106. GE_IF_BOOL_EXEC(ret != SUCCESS, GELOGE(ret, "InsertMemcpyAddrAsyncNode for known failed."); return ret);
  107. }
  108. }
  109. }
  110. }
  111. return SUCCESS;
  112. }
  113. void MemcpyAddrAsyncPass::FindUserDataForKnown(const NodePtr &parent_node, uint32_t &parent_index) {
  114. GELOGI("Start FindUserDataForKnown of %s.", parent_node->GetName().c_str());
  115. if (user_data_for_known_->GetOpDesc() == nullptr) {
  116. GELOGI("Cannot get op_desc of %s.", user_data_for_known_->GetName().c_str());
  117. return;
  118. }
  119. string src_var_name;
  120. if (ge::AttrUtils::GetStr(user_data_for_known_->GetOpDesc(), REF_VAR_SRC_VAR_NAME, src_var_name)) {
  121. GELOGI("The data in known graph is variable, no need to insert memcpy_addr_async.");
  122. find_user_data_for_known_ = false;
  123. return;
  124. } else {
  125. find_user_data_for_known_ = true;
  126. }
  127. }
  128. void MemcpyAddrAsyncPass::FindUserDataForNonDynamic(const ge::NodePtr &parent_node, uint32_t &parent_index) {
  129. GELOGI("Start to FindUserDataForNonDynamic of %s.", parent_node->GetName().c_str());
  130. InDataAnchorPtr in_data_anchor = parent_node->GetInDataAnchor(parent_index);
  131. OutDataAnchorPtr out_anchor = in_data_anchor->GetPeerOutAnchor();
  132. GE_IF_BOOL_EXEC(out_anchor == nullptr,
  133. GELOGE(INTERNAL_ERROR, "Cannot find out_anchor of %s.", parent_node->GetName().c_str());
  134. return);
  135. NodePtr in_node = out_anchor->GetOwnerNode();
  136. GELOGI("in_node of parent_node is %s.", in_node->GetName().c_str());
  137. if (in_node->GetType() == DATA) {
  138. if (in_node->GetOwnerComputeGraph()->GetParentGraph() != nullptr) {
  139. // DATA is in sub graph again, update user_data of known firstly
  140. user_data_for_known_ = in_node;
  141. out_of_user_data_for_known_ = parent_node;
  142. peer_out_anchor_for_known_ = out_anchor;
  143. in_anchor_for_known_ = in_data_anchor;
  144. NodePtr pre_in_node = in_node->GetOwnerComputeGraph()->GetParentNode();
  145. if (!AttrUtils::GetInt(in_node->GetOpDesc(), ATTR_NAME_PARENT_NODE_INDEX, parent_index)) {
  146. GELOGE(INTERNAL_ERROR, "Failed to refresh parent index of %s", in_node->GetName().c_str());
  147. return;
  148. }
  149. FindUserData(pre_in_node, parent_index);
  150. } else {
  151. // DATA is in parent graph and not has input
  152. user_data_ = in_node;
  153. out_of_user_data_ = parent_node;
  154. peer_out_anchor_ = out_anchor;
  155. in_anchor_ = in_data_anchor;
  156. find_user_data_ = true;
  157. GELOGI("%s connect with %s, will insert memcpyaddr.", user_data_->GetName().c_str(),
  158. out_of_user_data_->GetName().c_str());
  159. }
  160. } else if (in_node->GetType() == IF || in_node->GetType() == WHILE || in_node->GetType() == CASE) {
  161. if (!AttrUtils::GetInt(parent_node->GetOpDesc(), ATTR_NAME_PARENT_NODE_INDEX, parent_index)) {
  162. GELOGE(INTERNAL_ERROR, "Failed to refresh parent index of %s", in_node->GetName().c_str());
  163. return;
  164. }
  165. FindUserData(in_node, parent_index);
  166. } else {
  167. GELOGI("%s connect with %s, which is not user_data.", parent_node->GetName().c_str(), in_node->GetName().c_str());
  168. find_user_data_ = false;
  169. }
  170. }
  171. void MemcpyAddrAsyncPass::FindUserData(const NodePtr &parent_node, uint32_t &parent_index) {
  172. auto parent_op_desc = parent_node->GetOpDesc();
  173. if (parent_op_desc == nullptr) {
  174. GELOGI("Cannot get op_desc of %s.", parent_node->GetName().c_str());
  175. return;
  176. }
  177. bool is_unknown_shape = false;
  178. if (parent_node->GetType() == PARTITIONEDCALL &&
  179. AttrUtils::GetBool(parent_op_desc, ATTR_NAME_IS_UNKNOWN_SHAPE, is_unknown_shape) && !is_unknown_shape) {
  180. FindUserDataForKnown(parent_node, parent_index);
  181. } else {
  182. FindUserDataForNonDynamic(parent_node, parent_index);
  183. }
  184. }
  185. NodePtr MemcpyAddrAsyncPass::CreateMemcpyAddrAsyncNode(const ComputeGraphPtr &graph,
  186. const OutDataAnchorPtr &out_data_anchor,
  187. const NodePtr &out_of_user_data) {
  188. GELOGD("Start CreateMemcpyAddrAsyncNode.");
  189. OpDescPtr pre_op_desc = out_data_anchor->GetOwnerNode()->GetOpDesc();
  190. GE_CHK_BOOL_EXEC(pre_op_desc != nullptr, return nullptr, "Op_desc of pre node is invalid.");
  191. std::string node_name = pre_op_desc->GetName() + "_" + MEMCPYADDRASYNC;
  192. OpDescPtr op_desc = MakeShared<OpDesc>(node_name, MEMCPYADDRASYNC);
  193. GE_CHECK_NOTNULL_EXEC(op_desc, return nullptr);
  194. if (op_desc->AddInputDesc(pre_op_desc->GetOutputDesc(out_data_anchor->GetIdx())) != GRAPH_SUCCESS) {
  195. GELOGE(INTERNAL_ERROR, "Add memcpy_addr_async input desc failed.");
  196. return nullptr;
  197. }
  198. if (op_desc->AddOutputDesc(pre_op_desc->GetOutputDesc(out_data_anchor->GetIdx())) != GRAPH_SUCCESS) {
  199. GELOGE(INTERNAL_ERROR, "Add memcpy_addr_async output desc failed.");
  200. return nullptr;
  201. }
  202. string stream_label;
  203. if (AttrUtils::GetStr(out_of_user_data->GetOpDesc(), ATTR_NAME_STREAM_LABEL, stream_label)) {
  204. (void)AttrUtils::SetStr(op_desc, ATTR_NAME_STREAM_LABEL, stream_label);
  205. GELOGD("Node %s set stream label: %s", op_desc->GetName().c_str(), stream_label.c_str());
  206. }
  207. bool rts_label_node = false;
  208. if (AttrUtils::GetBool(out_of_user_data->GetOpDesc(), ATTR_NAME_RTS_LABEL_NODE, rts_label_node)) {
  209. (void)AttrUtils::SetBool(op_desc, ATTR_NAME_RTS_LABEL_NODE, rts_label_node);
  210. GELOGD("Node %s set rts label node attribute", op_desc->GetName().c_str());
  211. }
  212. bool labeled_input = false;
  213. (void)ge::AttrUtils::GetBool(out_of_user_data->GetOpDesc(), ATTR_NAME_NODE_CONNECT_INPUT, labeled_input);
  214. if (labeled_input) {
  215. if (!ge::AttrUtils::SetBool(out_of_user_data->GetOpDesc(), ATTR_NAME_NODE_CONNECT_INPUT, false)) {
  216. GELOGE(FAILED, "Failed to unset attr %s for node %s.", ATTR_NAME_NODE_CONNECT_INPUT.c_str(),
  217. out_of_user_data->GetName().c_str());
  218. return nullptr;
  219. }
  220. if (!ge::AttrUtils::SetBool(op_desc, ATTR_NAME_NODE_CONNECT_INPUT, true)) {
  221. GELOGE(FAILED, "Failed to set attr %s for node %s.", ATTR_NAME_NODE_CONNECT_INPUT.c_str(),
  222. op_desc->GetName().c_str());
  223. return nullptr;
  224. }
  225. }
  226. NodePtr memcpy_addr_async_node = graph->AddNodeAfter(op_desc, out_data_anchor->GetOwnerNode());
  227. GE_CHECK_NOTNULL_EXEC(memcpy_addr_async_node, return nullptr);
  228. return memcpy_addr_async_node;
  229. }
  230. Status MemcpyAddrAsyncPass::InsertMemcpyAddrAsyncNode(const OutDataAnchorPtr &out_anchor,
  231. const InDataAnchorPtr &in_anchor, const NodePtr &node) {
  232. // insert memcpy_addr of each user_data and out_of_user_data
  233. if (GraphUtils::RemoveEdge(out_anchor, in_anchor) != GRAPH_SUCCESS) {
  234. GELOGE(INTERNAL_ERROR, "Remove edge of %s and %s failed.", out_anchor->GetOwnerNode()->GetName().c_str(),
  235. in_anchor->GetOwnerNode()->GetName().c_str());
  236. return INTERNAL_ERROR;
  237. }
  238. if (GraphUtils::AddEdge(out_anchor, node->GetInDataAnchor(0)) != GRAPH_SUCCESS) {
  239. GELOGE(INTERNAL_ERROR, "Add edge of %s and %s failed.", out_anchor->GetOwnerNode()->GetName().c_str(),
  240. node->GetName().c_str());
  241. return INTERNAL_ERROR;
  242. }
  243. if (GraphUtils::AddEdge(node->GetOutDataAnchor(0), in_anchor) != GRAPH_SUCCESS) {
  244. GELOGE(INTERNAL_ERROR, "Add edge of %s and %s failed.", node->GetName().c_str(),
  245. in_anchor->GetOwnerNode()->GetName().c_str());
  246. return INTERNAL_ERROR;
  247. }
  248. return SUCCESS;
  249. }
  250. Status MemcpyAddrAsyncPass::InsertMemAddrAsyncNodeBeforeNetoutput(const ComputeGraphPtr &graph, const NodePtr &node) {
  251. GELOGI("Start AddMemcpyAddrAsyncNode for %s.", node->GetName().c_str());
  252. for (const auto &in_data_anchor : node->GetAllInDataAnchors()) {
  253. auto in_node = NodeUtils::GetInDataNodeByIndex(*node, in_data_anchor->GetIdx());
  254. GE_CHECK_NOTNULL(in_node);
  255. auto peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  256. if ((in_node->GetType() != CONSTANT) &&
  257. (in_node->GetType() != CONSTANTOP) &&
  258. (in_node->GetType() != DATA)) {
  259. continue;
  260. }
  261. auto desc = in_node->GetOpDesc();
  262. GE_CHECK_NOTNULL(desc);
  263. if (IsEmptyTenor(desc->GetOutputDesc(peer_out_anchor->GetIdx()).GetShape())) {
  264. continue;
  265. }
  266. GELOGI("Need to insert MemcpyAddrAsync before netoutput on parent graph.");
  267. NodePtr memcpy_addr_async_node = CreateMemcpyAddrAsyncNode(graph, peer_out_anchor, in_node);
  268. GE_IF_BOOL_EXEC(memcpy_addr_async_node == nullptr, GELOGE(INTERNAL_ERROR, "CreateMemcpyAddrAsyncNode failed.");
  269. return INTERNAL_ERROR);
  270. Status ret = InsertMemcpyAddrAsyncNode(peer_out_anchor, in_data_anchor, memcpy_addr_async_node);
  271. GE_IF_BOOL_EXEC(ret != SUCCESS, GELOGE(ret, "InsertMemcpyAddrAsyncNode failed."); return ret);
  272. GELOGI("Insert mem_addr_async node %s success between %s and %s.", memcpy_addr_async_node->GetName().c_str(),
  273. in_node->GetName().c_str(), node->GetName().c_str());
  274. // if src node is const, need to update attr and offset here because this pass process is after offset set.
  275. if ((in_node->GetType() == CONSTANT) || (in_node->GetType() == CONSTANTOP)) {
  276. NodeUtils::UpdateIsInputConst(memcpy_addr_async_node);
  277. auto output_desc = node->GetOpDesc();
  278. GE_CHECK_NOTNULL(output_desc);
  279. auto output_tensor_desc = output_desc->MutableInputDesc(static_cast<uint32_t>(in_data_anchor->GetIdx()));
  280. int64_t data_offset = 0;
  281. (void)TensorUtils::GetDataOffset(*output_tensor_desc, data_offset);
  282. auto input_tensor = memcpy_addr_async_node->GetOpDesc()->MutableInputDesc(0);
  283. GELOGI("Need update const Offset %ld to op [%s]", data_offset, memcpy_addr_async_node->GetName().c_str());
  284. TensorUtils::SetDataOffset(*input_tensor, data_offset);
  285. TensorUtils::SetDataOffset(*output_tensor_desc, 0);
  286. }
  287. }
  288. NodeUtils::UpdateIsInputConst(node);
  289. return SUCCESS;
  290. }
  291. bool MemcpyAddrAsyncPass::IsEmptyTenor(const GeShape &shape) const {
  292. for (const auto dim : shape.GetDims()) {
  293. if (dim == 0) {
  294. return true;
  295. }
  296. }
  297. return false;
  298. }
  299. } // namespace ge

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