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

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