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

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

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