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.

var_is_initialized_op_pass.cc 14 kB

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
4 years ago
5 years ago
4 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
4 years ago
5 years ago
4 years ago
5 years ago
4 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
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
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/var_is_initialized_op_pass.h"
  17. #include <memory>
  18. #include <utility>
  19. #include "framework/common/debug/ge_log.h"
  20. #include "common/ge/ge_util.h"
  21. #include "graph/anchor.h"
  22. #include "graph/debug/ge_attr_define.h"
  23. #include "graph/manager/graph_var_manager.h"
  24. #include "graph/node.h"
  25. #include "graph/utils/graph_utils.h"
  26. #include "graph/utils/node_utils.h"
  27. namespace ge {
  28. namespace {
  29. const int kAssignVarRefIndex = 0;
  30. const int kVarIsInitializedIOCnt = 1;
  31. const int kVarIsInitVarInputIndex = 0;
  32. } // namespace
  33. Status VarIsInitializedOpPass::Run(NodePtr &node) {
  34. GE_CHECK_NOTNULL(node);
  35. auto ret = UpdateInitedVars(node);
  36. if (ret != SUCCESS) {
  37. GELOGE(ret, "Failed to run var is init pass on node %s", node->GetName().c_str());
  38. return ret;
  39. }
  40. if (node->GetType() != VARISINITIALIZEDOP) {
  41. return SUCCESS;
  42. }
  43. bool inited = false;
  44. if (CheckSrcNode(node, inited) != SUCCESS) {
  45. return FAILED;
  46. }
  47. GELOGI("The variable inited status %s on node %s",
  48. inited ? "true" : "false", node->GetName().c_str());
  49. ret = ChangeNodeToConstant(node, inited);
  50. GELOGI("Change VarIsInitializedOp %s to be Constant %s end.",
  51. node->GetName().c_str(), inited ? "true" : "false");
  52. return ret;
  53. }
  54. Status VarIsInitializedOpPass::CheckSrcNode(const NodePtr &node, bool &inited) const {
  55. GE_CHECK_NOTNULL(node);
  56. auto input_nodes = node->GetInDataNodes();
  57. if (input_nodes.size() != kVarIsInitializedIOCnt) {
  58. REPORT_INNER_ERROR("E19999", "In data node num:%zu of node:%s(%s) not equal to %d, check invalid",
  59. input_nodes.size(), node->GetName().c_str(), node->GetType().c_str(), kVarIsInitializedIOCnt);
  60. GELOGE(FAILED,
  61. "[%s] Node input data nodes size [%zu] is not equal 1.",
  62. node->GetName().c_str(),
  63. input_nodes.size());
  64. return FAILED;
  65. }
  66. auto &input_node = input_nodes.at(kVarIsInitVarInputIndex);
  67. GE_CHECK_NOTNULL(input_node);
  68. auto input_node_name = input_node->GetName();
  69. auto input_node_type = input_node->GetType();
  70. if (input_node_type != VARIABLE) {
  71. REPORT_INNER_ERROR("E19999", "Index:%d In data node of node:%s(%s), type:%s not %s, check invalid",
  72. kVarIsInitVarInputIndex, node->GetName().c_str(), node->GetType().c_str(),
  73. input_node_type.c_str(), VARIABLE);
  74. GELOGE(FAILED, "[%s] Src node %s is not Variable, is %s.", node->GetName().c_str(), input_node_name.c_str(),
  75. input_node_type.c_str());
  76. return FAILED;
  77. }
  78. // initialized and initialized check graph must not be in the same graph
  79. ComputeGraphPtr compute_graph = node->GetOwnerComputeGraph();
  80. auto session_id = compute_graph->GetSessionID();
  81. if (VarManager::Instance(session_id)->IsVarExist(input_node_name)) {
  82. inited = true;
  83. return SUCCESS;
  84. }
  85. GE_CHECK_NOTNULL(input_node->GetOpDesc());
  86. inited = IsVarInitedOnTheGraphAndNode(node, input_node->GetOpDesc()->GetId());
  87. return SUCCESS;
  88. }
  89. Status VarIsInitializedOpPass::CreateConstant(NodePtr &node, OpDescPtr &op_desc, bool inited) {
  90. GE_CHECK_NOTNULL(node);
  91. // 1. create Constant OpDesc
  92. op_desc = MakeShared<OpDesc>(node->GetName().c_str(), CONSTANT);
  93. if (op_desc == nullptr) {
  94. REPORT_CALL_ERROR("E19999", "New OpDesc failed");
  95. GELOGE(FAILED, "[%s] Make shared of Constant op desc failed.", node->GetName().c_str());
  96. return FAILED;
  97. }
  98. // 2. get OpDesc of VarIsInitializedOp
  99. OpDescPtr original_op_desc = node->GetOpDesc();
  100. if (original_op_desc == nullptr) {
  101. REPORT_INNER_ERROR("E19999", "OpDesc in node is nullptr, check invalid");
  102. GELOGE(FAILED, "[%s] Op desc must not be null.", node->GetName().c_str());
  103. return FAILED;
  104. }
  105. GeTensorDesc original_desc = original_op_desc->GetOutputDesc(0);
  106. // 3. create attr value of Constant, is a tensor
  107. bool val = inited;
  108. GeTensorPtr const_tensor_ptr = MakeShared<GeTensor>(original_desc, reinterpret_cast<uint8_t *>(&val), sizeof(bool));
  109. if (const_tensor_ptr == nullptr) {
  110. REPORT_CALL_ERROR("E19999", "New GeTensor failed");
  111. GELOGE(FAILED, "[%s] Make shared of Constant tensor failed.", node->GetName().c_str());
  112. return FAILED;
  113. }
  114. if (!AttrUtils::SetTensor(op_desc, ATTR_NAME_WEIGHTS, const_tensor_ptr)) {
  115. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed", ATTR_NAME_WEIGHTS.c_str(),
  116. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  117. GELOGE(INTERNAL_ERROR, "get ATTR_NAME_WEIGHTS failed");
  118. return FAILED;
  119. }
  120. // 4. set Constant output desc
  121. GE_CHK_STATUS_RET(op_desc->AddOutputDesc(original_desc), "add out put desc failed");
  122. return SUCCESS;
  123. }
  124. Status VarIsInitializedOpPass::ProcessInAnchor(NodePtr &node, NodePtr &new_node) {
  125. GE_CHECK_NOTNULL(node);
  126. GE_CHECK_NOTNULL(new_node);
  127. auto in_anchors = node->GetAllInDataAnchors();
  128. auto out_anchors = node->GetAllOutDataAnchors();
  129. if ((in_anchors.size() != kVarIsInitializedIOCnt) ||
  130. (out_anchors.size() != kVarIsInitializedIOCnt)) {
  131. REPORT_INNER_ERROR("E19999", "In data anchor num:%zu and out data anchor num:%zu of node:%s(%s), "
  132. "must botch equal to %d, check invalid", in_anchors.size(), out_anchors.size(),
  133. node->GetName().c_str(), node->GetType().c_str(), kVarIsInitializedIOCnt);
  134. GELOGE(FAILED,
  135. "[%s] Node input/output data anchors"
  136. " size [%lu][%lu] is not all equal 1.",
  137. node->GetName().c_str(), in_anchors.size(), out_anchors.size());
  138. return FAILED;
  139. }
  140. // 1. delete in data anchor of VarIsInitializedOp node
  141. auto &in_anchor = in_anchors.at(kVarIsInitVarInputIndex);
  142. GE_CHECK_NOTNULL(in_anchor);
  143. auto peer_out_anchor = in_anchor->GetPeerOutAnchor();
  144. GE_CHECK_NOTNULL(peer_out_anchor);
  145. if (GraphUtils::RemoveEdge(in_anchor, peer_out_anchor) != GRAPH_SUCCESS) {
  146. REPORT_CALL_ERROR("E19999", "Remove edge between op:%s(%s)(index:%d) and op:%s(%s)(index:%d) failed",
  147. in_anchor->GetOwnerNode()->GetName().c_str(), in_anchor->GetOwnerNode()->GetType().c_str(),
  148. in_anchor->GetIdx(),
  149. peer_out_anchor->GetOwnerNode()->GetName().c_str(),
  150. peer_out_anchor->GetOwnerNode()->GetType().c_str(), peer_out_anchor->GetIdx());
  151. GELOGE(FAILED, "[%s] Remove in data edge failed.", node->GetName().c_str());
  152. return FAILED;
  153. }
  154. auto src_node = peer_out_anchor->GetOwnerNode();
  155. if (GraphUtils::AddEdge(src_node->GetOutControlAnchor(), new_node->GetInControlAnchor()) != GRAPH_SUCCESS) {
  156. REPORT_CALL_ERROR("E19999", "Add control edge between op:%s(%s) and op:%s(%s) failed",
  157. src_node->GetName().c_str(), src_node->GetType().c_str(),
  158. new_node->GetName().c_str(), new_node->GetType().c_str());
  159. GELOGE(FAILED, "Failed to link control edges from var %s to new const %s",
  160. src_node->GetName().c_str(), new_node->GetName().c_str());
  161. return FAILED;
  162. }
  163. if (GraphUtils::MoveInCtrlEdges(node, new_node) != GRAPH_SUCCESS) {
  164. REPORT_CALL_ERROR("E19999", "Move in control edge from node:%s(%s) to node:%s(%s) failed",
  165. node->GetName().c_str(), node->GetType().c_str(),
  166. new_node->GetName().c_str(), new_node->GetType().c_str());
  167. GELOGE(FAILED, "Failed to move in ctrl edges from %s to new const", node->GetName().c_str());
  168. return FAILED;
  169. }
  170. if (GraphUtils::MoveOutCtrlEdges(node, new_node) != GRAPH_SUCCESS) {
  171. REPORT_CALL_ERROR("E19999", "Move out control edge from node:%s(%s) to node:%s(%s) failed",
  172. node->GetName().c_str(), node->GetType().c_str(),
  173. new_node->GetName().c_str(), new_node->GetType().c_str());
  174. GELOGE(FAILED, "Failed to move out ctrl edges from %s to new const", node->GetName().c_str());
  175. return FAILED;
  176. }
  177. return SUCCESS;
  178. }
  179. Status VarIsInitializedOpPass::ChangeNodeToConstant(NodePtr &node, bool inited) {
  180. GE_CHECK_NOTNULL(node);
  181. ComputeGraphPtr graph = node->GetOwnerComputeGraph();
  182. OpDescPtr constant_op_desc = nullptr;
  183. if (CreateConstant(node, constant_op_desc, inited) != SUCCESS) {
  184. return FAILED;
  185. }
  186. NodePtr const_node = graph->AddNodeFront(constant_op_desc);
  187. if (const_node == nullptr) {
  188. REPORT_CALL_ERROR("E19999", "Add node:%s(%s) to graph:%s front failed",
  189. constant_op_desc->GetName().c_str(), constant_op_desc->GetType().c_str(),
  190. graph->GetName().c_str());
  191. return FAILED;
  192. }
  193. if (ProcessInAnchor(node, const_node) != SUCCESS) {
  194. return FAILED;
  195. }
  196. if (NodeUtils::MoveOutputEdges(node, const_node) != GRAPH_SUCCESS) {
  197. REPORT_CALL_ERROR("E19999", "Move out edge from node:%s(%s) to node:%s(%s) failed",
  198. node->GetName().c_str(), node->GetType().c_str(),
  199. const_node->GetName().c_str(), const_node->GetType().c_str());
  200. GELOGE(FAILED, "[%s] Move output edges to new node failed.", node->GetName().c_str());
  201. return FAILED;
  202. }
  203. if (GraphUtils::RemoveNodeWithoutRelink(graph, node) != SUCCESS) {
  204. REPORT_CALL_ERROR("E19999", "Remove node:%s(%s) without relink in graph:%s failed",
  205. node->GetName().c_str(), node->GetType().c_str(), graph->GetName().c_str());
  206. GELOGE(FAILED, "[%s] RemoveNodeWithoutRelink failed.", node->GetName().c_str());
  207. return FAILED;
  208. }
  209. AddRePassNodesWithInOut(const_node);
  210. // delete VarIsInitializedOp node from the graph
  211. AddNodeDeleted(node);
  212. return SUCCESS;
  213. }
  214. Status VarIsInitializedOpPass::UpdateInitedVars(const NodePtr &node) {
  215. GE_CHECK_NOTNULL(node);
  216. std::set<int64_t> *inited_vars = nullptr;
  217. bool inited_vars_merged = false;
  218. bool init_var = false;
  219. int64_t inited_var_id;
  220. auto ret = CheckAndSetVarInited(node, init_var, inited_var_id);
  221. if (ret != SUCCESS) {
  222. return ret;
  223. }
  224. if (init_var) {
  225. inited_vars = CreateInitedVars();
  226. if (inited_vars == nullptr) {
  227. return OUT_OF_MEMORY;
  228. }
  229. inited_vars_merged = true;
  230. inited_vars->insert(inited_var_id);
  231. }
  232. for (auto &in_node : node->GetInNodes()) {
  233. GE_CHECK_NOTNULL(in_node->GetOpDesc());
  234. auto iter = nodes_to_inited_vars_.find(in_node->GetOpDesc()->GetId());
  235. if (iter == nodes_to_inited_vars_.end()) {
  236. continue;
  237. }
  238. if (inited_vars == nullptr) {
  239. inited_vars = iter->second;
  240. continue;
  241. }
  242. if (inited_vars == iter->second) {
  243. continue;
  244. }
  245. // if there are multiple different inited_vars set, we should merge them to a new one
  246. if (inited_vars_merged) {
  247. inited_vars->insert(iter->second->begin(), iter->second->end());
  248. } else {
  249. auto origin_inited_vars = inited_vars;
  250. inited_vars = CreateInitedVars();
  251. if (inited_vars == nullptr) {
  252. return OUT_OF_MEMORY;
  253. }
  254. inited_vars_merged = true;
  255. inited_vars->insert(origin_inited_vars->begin(), origin_inited_vars->end());
  256. inited_vars->insert(iter->second->begin(), iter->second->end());
  257. }
  258. }
  259. if (inited_vars != nullptr) {
  260. GE_CHECK_NOTNULL(node->GetOpDesc());
  261. nodes_to_inited_vars_[node->GetOpDesc()->GetId()] = inited_vars;
  262. GELOGD("Inited vars on this graph when node %s, inited vars count %zu",
  263. node->GetName().c_str(), inited_vars->size());
  264. }
  265. return SUCCESS;
  266. }
  267. std::set<int64_t> *VarIsInitializedOpPass::CreateInitedVars() {
  268. std::unique_ptr<std::set<int64_t>> inited_vars_keeper(new(std::nothrow) std::set<int64_t>());
  269. if (inited_vars_keeper == nullptr) {
  270. REPORT_CALL_ERROR("E19999", "New set failed");
  271. GELOGE(OUT_OF_MEMORY, "Failed to alloc set memory");
  272. return nullptr;
  273. }
  274. auto inited_vars = inited_vars_keeper.get();
  275. var_inited_keeper_.emplace_back(std::move(inited_vars_keeper));
  276. return inited_vars;
  277. }
  278. bool VarIsInitializedOpPass::IsVarInitedOnTheGraphAndNode(const NodePtr &node, int64_t var_id) const {
  279. if (node == nullptr || node->GetOpDesc() == nullptr) {
  280. return false;
  281. }
  282. auto iter = nodes_to_inited_vars_.find(node->GetOpDesc()->GetId());
  283. if (iter == nodes_to_inited_vars_.end()) {
  284. return false;
  285. }
  286. return iter->second->count(var_id) > 0;
  287. }
  288. Status VarIsInitializedOpPass::CheckAndSetVarInited(const NodePtr &node, bool &inited, int64_t &inited_var) {
  289. GE_CHECK_NOTNULL(node);
  290. inited = false;
  291. if (node->GetType() != ASSIGN) {
  292. return SUCCESS;
  293. }
  294. auto ref_in_anchor = node->GetInDataAnchor(kAssignVarRefIndex);
  295. if (ref_in_anchor == nullptr) {
  296. GELOGW("Invalid assign node on graph, no ref input. name %s", node->GetName().c_str());
  297. return PARAM_INVALID;
  298. }
  299. auto var_out_anchor = ref_in_anchor->GetPeerOutAnchor();
  300. if (var_out_anchor == nullptr) {
  301. GELOGW("Invalid assign node on graph, no variable peer. name %s", node->GetName().c_str());
  302. return PARAM_INVALID;
  303. }
  304. auto var = var_out_anchor->GetOwnerNode();
  305. if (var == nullptr) {
  306. GELOGW("Invalid assign node on graph, no variable peer. name %s", node->GetName().c_str());
  307. return PARAM_INVALID;
  308. }
  309. inited = true;
  310. GE_CHECK_NOTNULL(var->GetOpDesc());
  311. inited_var = var->GetOpDesc()->GetId();
  312. return SUCCESS;
  313. }
  314. } // namespace ge

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