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.

save_pass.cc 2.9 kB

5 years ago
5 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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/save_pass.h"
  17. #include <string>
  18. #include <utility>
  19. #include <vector>
  20. #include "framework/common/debug/ge_log.h"
  21. #include "common/ge_inner_error_codes.h"
  22. #include "graph/utils/graph_utils.h"
  23. namespace ge {
  24. namespace {
  25. const char *const kSave = "Save";
  26. const char *const kVar = "Variable";
  27. const char *const kVarIsSave = "save_checkpoint";
  28. const char *const kVarAttrVarIsSave = "_var_is_save";
  29. } // namespace
  30. Status SavePass::Run(ge::ComputeGraphPtr graph) {
  31. GE_CHECK_NOTNULL(graph);
  32. vector<NodePtr> front_nodes;
  33. vector<uint8_t> out_index;
  34. vector<NodePtr> del_nodes;
  35. for (auto &node : graph->GetDirectNode()) {
  36. if (node->GetType() == kSave) {
  37. for (auto &in : node->GetAllInDataAnchors()) {
  38. auto out_anchor = in->GetPeerOutAnchor();
  39. if (out_anchor != nullptr) {
  40. ge::NodePtr peer_node = out_anchor->GetOwnerNode();
  41. if (peer_node->GetType() == kVar) {
  42. front_nodes.emplace_back(peer_node);
  43. out_index.emplace_back(out_anchor->GetIdx());
  44. ge::OpDescPtr op_desc = peer_node->GetOpDesc();
  45. GE_IF_BOOL_EXEC(!ge::AttrUtils::SetStr(op_desc, kVarAttrVarIsSave, kVarIsSave),
  46. GELOGE(INTERNAL_ERROR, "get kVarAttrVarIsSave failed"); return INTERNAL_ERROR);
  47. }
  48. }
  49. }
  50. del_nodes.emplace_back(node);
  51. }
  52. }
  53. // add output nodes for save
  54. std::vector<std::pair<NodePtr, int32_t>> out_nodes_info{};
  55. for (size_t i = 0; i < front_nodes.size(); i++) {
  56. out_nodes_info.emplace_back(pair<NodePtr, int32_t>(front_nodes[i], out_index[i]));
  57. }
  58. graph->AppendGraphOutNodesInfo(out_nodes_info);
  59. // delete save node
  60. for (auto &node_ptr : del_nodes) {
  61. auto ret = graph->RemoveNode(node_ptr);
  62. if (ret != SUCCESS) {
  63. GELOGE(ret, "GraphUtils::RemoveNodeWithoutRelink failed.");
  64. return ret;
  65. }
  66. // update Target list
  67. vector<NodePtr> graph_target = graph->GetGraphTargetNodesInfo();
  68. auto iter = find(graph_target.begin(), graph_target.end(), node_ptr);
  69. if (iter != graph_target.end()) {
  70. GELOGI("Current node %s is as Target, remove it from target vector.", node_ptr->GetName().c_str());
  71. graph_target.erase(iter);
  72. graph->SetGraphTargetNodesInfo(graph_target);
  73. }
  74. }
  75. return SUCCESS;
  76. }
  77. } // namespace ge

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