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

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
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. GELOGE(FAILED,
  59. "[%s] Node input data nodes size [%zu] is not equal 1.",
  60. node->GetName().c_str(),
  61. input_nodes.size());
  62. return FAILED;
  63. }
  64. auto &input_node = input_nodes.at(kVarIsInitVarInputIndex);
  65. GE_CHECK_NOTNULL(input_node);
  66. auto input_node_name = input_node->GetName();
  67. auto input_node_type = input_node->GetType();
  68. if (input_node_type != VARIABLE) {
  69. GELOGE(FAILED, "[%s] Src node %s is not Variable, is %s.", node->GetName().c_str(), input_node_name.c_str(),
  70. input_node_type.c_str());
  71. return FAILED;
  72. }
  73. // initialized and initialized check graph must not be in the same graph
  74. ComputeGraphPtr compute_graph = node->GetOwnerComputeGraph();
  75. auto session_id = compute_graph->GetSessionID();
  76. if (VarManager::Instance(session_id)->IsVarExist(input_node_name)) {
  77. inited = true;
  78. return SUCCESS;
  79. }
  80. GE_CHECK_NOTNULL(input_node->GetOpDesc());
  81. inited = IsVarInitedOnTheGraphAndNode(node, input_node->GetOpDesc()->GetId());
  82. return SUCCESS;
  83. }
  84. Status VarIsInitializedOpPass::CreateConstant(NodePtr &node, OpDescPtr &op_desc, bool inited) {
  85. GE_CHECK_NOTNULL(node);
  86. // 1. create Constant OpDesc
  87. op_desc = MakeShared<OpDesc>(node->GetName().c_str(), CONSTANT);
  88. if (op_desc == nullptr) {
  89. GELOGE(FAILED, "[%s] Make shared of Constant op desc failed.", node->GetName().c_str());
  90. return FAILED;
  91. }
  92. // 2. get OpDesc of VarIsInitializedOp
  93. OpDescPtr original_op_desc = node->GetOpDesc();
  94. if (original_op_desc == nullptr) {
  95. GELOGE(FAILED, "[%s] Op desc must not be null.", node->GetName().c_str());
  96. return FAILED;
  97. }
  98. GeTensorDesc original_desc = original_op_desc->GetOutputDesc(0);
  99. // 3. create attr value of Constant, is a tensor
  100. bool val = inited;
  101. GeTensorPtr const_tensor_ptr = MakeShared<GeTensor>(original_desc, reinterpret_cast<uint8_t *>(&val), sizeof(bool));
  102. if (const_tensor_ptr == nullptr) {
  103. GELOGE(FAILED, "[%s] Make shared of Constant tensor failed.", node->GetName().c_str());
  104. return FAILED;
  105. }
  106. if (!AttrUtils::SetTensor(op_desc, ATTR_NAME_WEIGHTS, const_tensor_ptr)) {
  107. GELOGE(INTERNAL_ERROR, "get ATTR_NAME_WEIGHTS failed");
  108. return FAILED;
  109. }
  110. // 4. set Constant output desc
  111. GE_CHK_STATUS_RET(op_desc->AddOutputDesc(original_desc), "add out put desc failed");
  112. return SUCCESS;
  113. }
  114. Status VarIsInitializedOpPass::ProcessInAnchor(NodePtr &node, NodePtr &new_node) {
  115. GE_CHECK_NOTNULL(node);
  116. GE_CHECK_NOTNULL(new_node);
  117. auto in_anchors = node->GetAllInDataAnchors();
  118. auto out_anchors = node->GetAllOutDataAnchors();
  119. if ((in_anchors.size() != kVarIsInitializedIOCnt) ||
  120. (out_anchors.size() != kVarIsInitializedIOCnt)) {
  121. GELOGE(FAILED,
  122. "[%s] Node input/output data anchors"
  123. " size [%lu][%lu] is not all equal 1.",
  124. node->GetName().c_str(), in_anchors.size(), out_anchors.size());
  125. return FAILED;
  126. }
  127. // 1. delete in data anchor of VarIsInitializedOp node
  128. auto &in_anchor = in_anchors.at(kVarIsInitVarInputIndex);
  129. GE_CHECK_NOTNULL(in_anchor);
  130. auto peer_out_anchor = in_anchor->GetPeerOutAnchor();
  131. GE_CHECK_NOTNULL(peer_out_anchor);
  132. if (GraphUtils::RemoveEdge(in_anchor, peer_out_anchor) != GRAPH_SUCCESS) {
  133. GELOGE(FAILED, "[%s] Remove in data edge failed.", node->GetName().c_str());
  134. return FAILED;
  135. }
  136. auto src_node = peer_out_anchor->GetOwnerNode();
  137. if (GraphUtils::AddEdge(src_node->GetOutControlAnchor(), new_node->GetInControlAnchor()) != GRAPH_SUCCESS) {
  138. GELOGE(FAILED, "Failed to link control edges from var %s to new const %s",
  139. src_node->GetName().c_str(), new_node->GetName().c_str());
  140. return FAILED;
  141. }
  142. if (GraphUtils::MoveInCtrlEdges(node, new_node) != GRAPH_SUCCESS) {
  143. GELOGE(FAILED, "Failed to move in ctrl edges from %s to new const", node->GetName().c_str());
  144. return FAILED;
  145. }
  146. if (GraphUtils::MoveOutCtrlEdges(node, new_node) != GRAPH_SUCCESS) {
  147. GELOGE(FAILED, "Failed to move out ctrl edges from %s to new const", node->GetName().c_str());
  148. return FAILED;
  149. }
  150. return SUCCESS;
  151. }
  152. Status VarIsInitializedOpPass::ChangeNodeToConstant(NodePtr &node, bool inited) {
  153. GE_CHECK_NOTNULL(node);
  154. ComputeGraphPtr graph = node->GetOwnerComputeGraph();
  155. OpDescPtr constant_op_desc = nullptr;
  156. if (CreateConstant(node, constant_op_desc, inited) != SUCCESS) {
  157. return FAILED;
  158. }
  159. NodePtr const_node = graph->AddNodeFront(constant_op_desc);
  160. if (const_node == nullptr) {
  161. return FAILED;
  162. }
  163. if (ProcessInAnchor(node, const_node) != SUCCESS) {
  164. return FAILED;
  165. }
  166. if (NodeUtils::MoveOutputEdges(node, const_node) != GRAPH_SUCCESS) {
  167. GELOGE(FAILED, "[%s] Move output edges to new node failed.", node->GetName().c_str());
  168. return FAILED;
  169. }
  170. if (GraphUtils::RemoveNodeWithoutRelink(graph, node) != SUCCESS) {
  171. GELOGE(FAILED, "[%s] RemoveNodeWithoutRelink failed.", node->GetName().c_str());
  172. return FAILED;
  173. }
  174. AddRePassNodesWithInOut(const_node);
  175. // delete VarIsInitializedOp node from the graph
  176. AddNodeDeleted(node);
  177. return SUCCESS;
  178. }
  179. Status VarIsInitializedOpPass::UpdateInitedVars(const NodePtr &node) {
  180. GE_CHECK_NOTNULL(node);
  181. std::set<int64_t> *inited_vars = nullptr;
  182. bool inited_vars_merged = false;
  183. bool init_var = false;
  184. int64_t inited_var_id;
  185. auto ret = CheckAndSetVarInited(node, init_var, inited_var_id);
  186. if (ret != SUCCESS) {
  187. return ret;
  188. }
  189. if (init_var) {
  190. inited_vars = CreateInitedVars();
  191. if (inited_vars == nullptr) {
  192. return OUT_OF_MEMORY;
  193. }
  194. inited_vars_merged = true;
  195. inited_vars->insert(inited_var_id);
  196. }
  197. for (auto &in_node : node->GetInNodes()) {
  198. GE_CHECK_NOTNULL(in_node->GetOpDesc());
  199. auto iter = nodes_to_inited_vars_.find(in_node->GetOpDesc()->GetId());
  200. if (iter == nodes_to_inited_vars_.end()) {
  201. continue;
  202. }
  203. if (inited_vars == nullptr) {
  204. inited_vars = iter->second;
  205. continue;
  206. }
  207. if (inited_vars == iter->second) {
  208. continue;
  209. }
  210. // if there are multiple different inited_vars set, we should merge them to a new one
  211. if (inited_vars_merged) {
  212. inited_vars->insert(iter->second->begin(), iter->second->end());
  213. } else {
  214. auto origin_inited_vars = inited_vars;
  215. inited_vars = CreateInitedVars();
  216. if (inited_vars == nullptr) {
  217. return OUT_OF_MEMORY;
  218. }
  219. inited_vars_merged = true;
  220. inited_vars->insert(origin_inited_vars->begin(), origin_inited_vars->end());
  221. inited_vars->insert(iter->second->begin(), iter->second->end());
  222. }
  223. }
  224. if (inited_vars != nullptr) {
  225. GE_CHECK_NOTNULL(node->GetOpDesc());
  226. nodes_to_inited_vars_[node->GetOpDesc()->GetId()] = inited_vars;
  227. GELOGD("Inited vars on this graph when node %s, inited vars count %zu",
  228. node->GetName().c_str(), inited_vars->size());
  229. }
  230. return SUCCESS;
  231. }
  232. std::set<int64_t> *VarIsInitializedOpPass::CreateInitedVars() {
  233. std::unique_ptr<std::set<int64_t>> inited_vars_keeper(new(std::nothrow) std::set<int64_t>());
  234. if (inited_vars_keeper == nullptr) {
  235. GELOGE(OUT_OF_MEMORY, "Failed to alloc set memory");
  236. return nullptr;
  237. }
  238. auto inited_vars = inited_vars_keeper.get();
  239. var_inited_keeper_.emplace_back(std::move(inited_vars_keeper));
  240. return inited_vars;
  241. }
  242. bool VarIsInitializedOpPass::IsVarInitedOnTheGraphAndNode(const NodePtr &node, int64_t var_id) const {
  243. if (node == nullptr || node->GetOpDesc() == nullptr) {
  244. return false;
  245. }
  246. auto iter = nodes_to_inited_vars_.find(node->GetOpDesc()->GetId());
  247. if (iter == nodes_to_inited_vars_.end()) {
  248. return false;
  249. }
  250. return iter->second->count(var_id) > 0;
  251. }
  252. Status VarIsInitializedOpPass::CheckAndSetVarInited(const NodePtr &node, bool &inited, int64_t &inited_var) {
  253. GE_CHECK_NOTNULL(node);
  254. inited = false;
  255. if (node->GetType() != ASSIGN) {
  256. return SUCCESS;
  257. }
  258. auto ref_in_anchor = node->GetInDataAnchor(kAssignVarRefIndex);
  259. if (ref_in_anchor == nullptr) {
  260. GELOGW("Invalid assign node on graph, no ref input. name %s", node->GetName().c_str());
  261. return PARAM_INVALID;
  262. }
  263. auto var_out_anchor = ref_in_anchor->GetPeerOutAnchor();
  264. if (var_out_anchor == nullptr) {
  265. GELOGW("Invalid assign node on graph, no variable peer. name %s", node->GetName().c_str());
  266. return PARAM_INVALID;
  267. }
  268. auto var = var_out_anchor->GetOwnerNode();
  269. if (var == nullptr) {
  270. GELOGW("Invalid assign node on graph, no variable peer. name %s", node->GetName().c_str());
  271. return PARAM_INVALID;
  272. }
  273. inited = true;
  274. GE_CHECK_NOTNULL(var->GetOpDesc());
  275. inited_var = var->GetOpDesc()->GetId();
  276. return SUCCESS;
  277. }
  278. } // namespace ge

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