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.

atomic_addr_clean_pass.cc 13 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
4 years ago
5 years ago
4 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
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 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
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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/atomic_addr_clean_pass.h"
  17. #include <map>
  18. #include <memory>
  19. #include <string>
  20. #include <sstream>
  21. #include <vector>
  22. #include "common/ge_inner_error_codes.h"
  23. #include "common/ge/ge_util.h"
  24. #include "graph/common/ge_call_wrapper.h"
  25. #include "graph/debug/ge_attr_define.h"
  26. #include "graph/utils/node_utils.h"
  27. #include "init/gelib.h"
  28. namespace ge {
  29. Status AtomicAddrCleanPass::Run(ComputeGraphPtr graph) {
  30. GE_CHECK_NOTNULL(graph);
  31. GELOGD("AtomicAddrCleanPass begin.");
  32. // 1.Recoginze atomic and loop mark
  33. vector<NodePtr> atomic_node_vec;
  34. for (NodePtr &node : graph->GetDirectNode()) {
  35. if (IsAtomicOp(node)) {
  36. atomic_node_vec.push_back(node);
  37. }
  38. if (!is_loop_graph_ && node->GetType() == LOOPCOND) {
  39. // there is loop in this graph
  40. GELOGD("There is no loop node. It will insert clean node follow atomic node.");
  41. is_loop_graph_ = true;
  42. }
  43. }
  44. if (atomic_node_vec.empty()) {
  45. GELOGD("There is no atomic node. Ignore atomicAddrClean pass.");
  46. return SUCCESS;
  47. }
  48. bool is_unknown_graph = graph->GetGraphUnknownFlag();
  49. if (is_unknown_graph) {
  50. GELOGD("Graph[%s] is unknown graph. It will call fe interface to compile op.", graph->GetName().c_str());
  51. GE_CHK_STATUS_RET(CompileUnknownGraphOp(atomic_node_vec));
  52. return SUCCESS;
  53. }
  54. // 2.Insert clean node and link to atomic node
  55. Status ret;
  56. if (is_loop_graph_) {
  57. ret = HandleLoopGraph(graph, atomic_node_vec);
  58. if (ret != SUCCESS) {
  59. return ret;
  60. }
  61. } else {
  62. ret = HandleNormalGraph(graph, atomic_node_vec);
  63. if (ret != SUCCESS) {
  64. return ret;
  65. }
  66. }
  67. GELOGD("AtomicAddrCleanPass end.");
  68. return SUCCESS;
  69. }
  70. Status AtomicAddrCleanPass::HandleLoopGraph(ComputeGraphPtr &graph, const vector<NodePtr> &atomic_node_vec) {
  71. // Loop graph , insert clean node follow atomic node
  72. int index = 0;
  73. for (const auto &node : atomic_node_vec) {
  74. // Insert atomic clean op
  75. NodePtr clean_addr_node = InsertAtomicAddrCleanNode(graph);
  76. if (clean_addr_node == nullptr) {
  77. GELOGE(FAILED, "Insert AtomicAddrClean node failed. Ignore atomicAddrClean pass.");
  78. return FAILED;
  79. }
  80. GE_CHECK_NOTNULL(clean_addr_node->GetOpDesc());
  81. string node_name = clean_addr_node->GetOpDesc()->GetName();
  82. std::ostringstream oss;
  83. oss << node_name << index;
  84. node_name = oss.str();
  85. clean_addr_node->GetOpDesc()->SetName(node_name); // [Cascade Pointer]
  86. GELOGD("Inserted atomic clean node name is %s", node_name.c_str());
  87. auto ret = LinkToAtomicNode(node, clean_addr_node);
  88. if (ret != SUCCESS) {
  89. GELOGE(ret, "Link control anchor failed from atomic node to atomic_addr_clean node.");
  90. return ret;
  91. }
  92. index++;
  93. }
  94. return SUCCESS;
  95. }
  96. Status AtomicAddrCleanPass::HandleNormalGraph(ComputeGraphPtr &graph, const vector<NodePtr> &atomic_node_vec) {
  97. GELOGD("Not loop graph and unknown graph. It will insert only 1 clean node.");
  98. vector<NodePtr> common_atomic_nodes;
  99. auto ret = HandleDispersedAtomicNodes(graph, atomic_node_vec, common_atomic_nodes);
  100. if (ret != SUCCESS) {
  101. GELOGE(ret, "Handle dispersed atomic nodes failed, graph name is %s.", graph->GetName().c_str());
  102. return ret;
  103. }
  104. if (common_atomic_nodes.empty()) {
  105. GELOGI("common_atomic_nodes is empty");
  106. return SUCCESS;
  107. }
  108. // not loop graph , insert only one clean node in graph
  109. NodePtr clean_addr_node = InsertAtomicAddrCleanNode(graph);
  110. if (clean_addr_node == nullptr) {
  111. GELOGE(FAILED, "Insert AtomicAddrClean node failed. Ignore atomicAddrClean pass.");
  112. return FAILED;
  113. }
  114. for (const auto &node : common_atomic_nodes) {
  115. ret = LinkToAtomicNode(node, clean_addr_node);
  116. if (ret != SUCCESS) {
  117. GELOGE(ret, "Link control anchor failed from atomic node to atomic_addr_clean node.");
  118. return ret;
  119. }
  120. }
  121. // for HCOM atomic node, add one more control link to peer-in node
  122. for (auto &node : hcom_node_vec_) {
  123. for (auto &in_anchor : node->GetAllInDataAnchors()) {
  124. GE_CHECK_NOTNULL(in_anchor->GetPeerOutAnchor());
  125. NodePtr peer_in_node = in_anchor->GetPeerOutAnchor()->GetOwnerNode();
  126. ret = LinkToAtomicNode(peer_in_node, clean_addr_node);
  127. if (ret != SUCCESS) {
  128. GELOGE(ret, "Link failed, %s : %s", peer_in_node->GetName().c_str(), clean_addr_node->GetName().c_str());
  129. return ret;
  130. }
  131. }
  132. }
  133. return SUCCESS;
  134. }
  135. Status AtomicAddrCleanPass::HandleDispersedAtomicNodes(ComputeGraphPtr &graph,
  136. const std::vector<NodePtr> &atomic_node_vec,
  137. std::vector<NodePtr> &common_atomic_nodes) {
  138. int index = 0;
  139. for (const auto &node : atomic_node_vec) {
  140. vector<int> node_anchors_connect_netoutput;
  141. // If GetBool fail, attr is_connect_netoutput is an empty vector.
  142. (void)ge::AttrUtils::GetListInt(node->GetOpDesc(), ATTR_NAME_NODE_CONNECT_OUTPUT, node_anchors_connect_netoutput);
  143. if (!node_anchors_connect_netoutput.empty()) {
  144. NodePtr dispersed_clean_addr_node = InsertAtomicAddrCleanNode(graph);
  145. if (dispersed_clean_addr_node == nullptr) {
  146. GELOGE(FAILED, "Insert AtomicAddrClean node failed. Ignore atomicAddrClean pass.");
  147. return FAILED;
  148. }
  149. auto dispersed_node_op_desc = dispersed_clean_addr_node->GetOpDesc();
  150. GE_CHECK_NOTNULL(dispersed_node_op_desc);
  151. string node_name = dispersed_node_op_desc->GetName();
  152. std::ostringstream oss;
  153. oss << node_name << "_" << index;
  154. node_name = oss.str();
  155. dispersed_node_op_desc->SetName(node_name);
  156. GELOGD("Inserted dispersed atomic clean node name is %s", node_name.c_str());
  157. ++index;
  158. Status ret = LinkToAtomicNode(node, dispersed_clean_addr_node);
  159. if (ret != SUCCESS) {
  160. GELOGE(ret, "Link control anchor failed from atomic node: %s to atomic_addr_clean node: %s.",
  161. node->GetName().c_str(), dispersed_clean_addr_node->GetName().c_str());
  162. return ret;
  163. }
  164. } else {
  165. common_atomic_nodes.emplace_back(node);
  166. }
  167. }
  168. return SUCCESS;
  169. }
  170. NodePtr AtomicAddrCleanPass::InsertAtomicAddrCleanNode(ComputeGraphPtr &graph) {
  171. OpDescPtr op_desc = MakeShared<OpDesc>(NODE_NAME_ATOMIC_ADDR_CLEAN, ATOMICADDRCLEAN);
  172. if (op_desc == nullptr) {
  173. GELOGE(INTERNAL_ERROR, "Make shared atomic addr clean op failed.");
  174. return nullptr;
  175. }
  176. string session_graph_id;
  177. if (!AttrUtils::GetStr(*graph, ATTR_NAME_SESSION_GRAPH_ID, session_graph_id)) {
  178. GELOGW("Get graph session_graph_id attr failed.");
  179. }
  180. if (!session_graph_id.empty()) {
  181. (void) AttrUtils::SetStr(op_desc, ATTR_NAME_SESSION_GRAPH_ID, session_graph_id);
  182. }
  183. string node_name = op_desc->GetName();
  184. // Only flush subgraph name
  185. if (graph->GetParentGraph() != nullptr) {
  186. node_name = graph->GetName() + "_" + node_name;
  187. }
  188. string name = node_name + session_graph_id;
  189. op_desc->SetName(name);
  190. GELOGI("Create cleanAddr op:%s.", op_desc->GetName().c_str());
  191. // To avoid same name between graphs, set session graph id to this node
  192. NodePtr clean_addr_node = graph->AddNodeFront(op_desc);
  193. return clean_addr_node;
  194. }
  195. Status AtomicAddrCleanPass::LinkToAtomicNode(const NodePtr &atomic_node, NodePtr &atomic_clean_node) {
  196. GE_IF_BOOL_EXEC(atomic_node == nullptr || atomic_clean_node == nullptr,
  197. DOMI_LOGE("param [atomic_node][atomic_clean_node] must not be null."); return PARAM_INVALID);
  198. InControlAnchorPtr in_ctrl_anchor = atomic_node->GetInControlAnchor();
  199. OutControlAnchorPtr out_ctrl_anchor = atomic_clean_node->GetOutControlAnchor();
  200. if (in_ctrl_anchor == nullptr || out_ctrl_anchor == nullptr) {
  201. GELOGE(INTERNAL_ERROR,
  202. "Get control anchor faild, dst node: %s.",
  203. atomic_node->GetName().c_str());
  204. return INTERNAL_ERROR;
  205. }
  206. graphStatus status = GraphUtils::AddEdge(out_ctrl_anchor, in_ctrl_anchor);
  207. if (status != GRAPH_SUCCESS) {
  208. GELOGE(INTERNAL_ERROR,
  209. "Graph add cleanAddrNode op out ctrl edge fail, dst node: %s.",
  210. atomic_node->GetName().c_str());
  211. return INTERNAL_ERROR;
  212. }
  213. GELOGD("Graph add cleanAddrNode op out ctrl edge, dst node: %s.", atomic_node->GetName().c_str());
  214. std::string stream_label;
  215. if (is_loop_graph_ && AttrUtils::GetStr(atomic_node->GetOpDesc(), ATTR_NAME_STREAM_LABEL, stream_label)) {
  216. if (!AttrUtils::SetStr(atomic_clean_node->GetOpDesc(), ATTR_NAME_STREAM_LABEL, stream_label)) {
  217. GELOGW("LinkToAtomicNode: SetStr failed");
  218. return INTERNAL_ERROR;
  219. }
  220. }
  221. return SUCCESS;
  222. }
  223. bool AtomicAddrCleanPass::IsAtomicOp(const NodePtr &node) {
  224. GE_IF_BOOL_EXEC(node == nullptr, GELOGE(FAILED, "node is null."); return false);
  225. OpDescPtr op_desc = node->GetOpDesc();
  226. if (op_desc == nullptr) {
  227. return false;
  228. }
  229. // 1.Check if isAtomic attrs exist for HCOM
  230. std::shared_ptr<GELib> instance_ptr = GELib::GetInstance();
  231. if ((instance_ptr == nullptr) || (!instance_ptr->InitFlag())) {
  232. GELOGW("GELib not initialized");
  233. return false;
  234. }
  235. OpsKernelManager &ops_kernel_manager = instance_ptr->OpsKernelManagerObj();
  236. vector<OpInfo> op_info_vec = ops_kernel_manager.GetOpsKernelInfo(op_desc->GetType());
  237. for (const auto &op_info : op_info_vec) {
  238. if (op_info.isAtomic) {
  239. GELOGI("Recognized atomic op %s from DNN_HCCL engine.", op_desc->GetName().c_str());
  240. // check peer input is DATA
  241. for (auto &in_data_anchor : node->GetAllInDataAnchors()) {
  242. if (in_data_anchor->GetPeerOutAnchor() != nullptr &&
  243. in_data_anchor->GetPeerOutAnchor()->GetOwnerNode() != nullptr) {
  244. auto peer_in_node = in_data_anchor->GetPeerOutAnchor()->GetOwnerNode();
  245. if (peer_in_node->GetType() == DATA) {
  246. GELOGI("Recognized atomic op %s from DNN_HCCL engine and input is DATA.", op_desc->GetName().c_str());
  247. return false;
  248. }
  249. }
  250. }
  251. hcom_node_vec_.push_back(node);
  252. return true;
  253. }
  254. }
  255. // 2.Check atomic attr in node
  256. std::map<string, std::map<int, int>> node_workspace_offset;
  257. bool has_atomic_input = op_desc->HasAttr(ATOMIC_ATTR_INPUT_INDEX);
  258. bool has_atomic_output = op_desc->HasAttr(ATOMIC_ATTR_OUTPUT_INDEX);
  259. node_workspace_offset = op_desc->TryGetExtAttr(EXT_ATTR_ATOMIC_WORKSPACE_OFFSET, node_workspace_offset);
  260. if (!has_atomic_input && !has_atomic_output && node_workspace_offset.empty()) {
  261. return false;
  262. }
  263. graphStatus ret = op_desc->SetAttr(ATOMIC_ATTR_IS_ATOMIC_NODE, GeAttrValue::CreateFrom<GeAttrValue::BOOL>(true));
  264. if (ret != GRAPH_SUCCESS) {
  265. GELOGW("set attr ATOMIC_ATTR_IS_ATOMIC_NODE fail.");
  266. }
  267. GELOGD("Recognized atomic op %s from FE engine.", op_desc->GetName().c_str());
  268. return true;
  269. }
  270. ///
  271. /// @brief Clear Status, used for subgraph pass
  272. /// @return SUCCESS
  273. ///
  274. Status AtomicAddrCleanPass::ClearStatus() {
  275. hcom_node_vec_.clear();
  276. return SUCCESS;
  277. }
  278. Status AtomicAddrCleanPass::CompileUnknownGraphOp(const vector<NodePtr> &atomic_node_vec) {
  279. GE_TIMESTAMP_CALLNUM_START(UnknownGraphCompileOp);
  280. std::unordered_map<string, vector<ge::NodePtr>> node_vector_map;
  281. std::shared_ptr<GELib> instance = ge::GELib::GetInstance();
  282. if ((instance == nullptr) || !instance->InitFlag()) {
  283. GELOGE(ge::GE_CLI_GE_NOT_INITIALIZED, "CompileSingleOp failed.");
  284. return ge::GE_CLI_GE_NOT_INITIALIZED;
  285. }
  286. for (auto &atomic_node: atomic_node_vec) {
  287. auto op_desc = atomic_node->GetOpDesc();
  288. if (op_desc == nullptr) {
  289. GELOGW("op desc is nullptr.");
  290. continue;
  291. }
  292. string kernel_lib_name = op_desc->GetOpKernelLibName();
  293. if (kernel_lib_name.empty()) {
  294. GELOGE(ge::INTERNAL_ERROR, "Get atomic node:%s(%s) kernel lib failed.", atomic_node->GetName().c_str(),
  295. atomic_node->GetType().c_str());
  296. return ge::INTERNAL_ERROR;
  297. }
  298. OpsKernelInfoStorePtr kernel_info = instance->OpsKernelManagerObj().GetOpsKernelInfoStore(kernel_lib_name);
  299. GE_CHECK_NOTNULL(kernel_info);
  300. node_vector_map[kernel_lib_name].emplace_back(atomic_node);
  301. }
  302. for (auto &it : node_vector_map) {
  303. auto &kernel_lib_name = it.first;
  304. auto &node_vector = it.second;
  305. OpsKernelInfoStorePtr kernel_info = instance->OpsKernelManagerObj().GetOpsKernelInfoStore(kernel_lib_name);
  306. GE_CHECK_NOTNULL(kernel_info);
  307. GE_TIMESTAMP_RESTART(UnknownGraphCompileOp);
  308. auto ret = kernel_info->CompileOp(node_vector);
  309. GELOGI("The atomic node size of compile op of %s is %zu", kernel_lib_name.c_str(), node_vector.size());
  310. GE_TIMESTAMP_ADD(UnknownGraphCompileOp);
  311. if (ret != ge::SUCCESS) {
  312. GELOGE(ret, "Compile atomic op failed, kernel lib name is %s", kernel_lib_name.c_str());
  313. return ret;
  314. }
  315. }
  316. GE_TIMESTAMP_CALLNUM_END(UnknownGraphCompileOp, "AtomicAddrCleanPass::CompileUnknownGraphOp");
  317. return SUCCESS;
  318. }
  319. } // namespace ge

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