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

5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed", kVarAttrVarIsSave,
  47. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  48. GELOGE(INTERNAL_ERROR, "[Set][Attr] %s to op:%s(%s) failed", kVarAttrVarIsSave,
  49. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  50. return INTERNAL_ERROR);
  51. }
  52. }
  53. }
  54. del_nodes.emplace_back(node);
  55. }
  56. }
  57. // add output nodes for save
  58. std::vector<std::pair<NodePtr, int32_t>> out_nodes_info{};
  59. for (size_t i = 0; i < front_nodes.size(); i++) {
  60. out_nodes_info.emplace_back(pair<NodePtr, int32_t>(front_nodes[i], out_index[i]));
  61. }
  62. graph->AppendGraphOutNodesInfo(out_nodes_info);
  63. // delete save node
  64. for (auto &node_ptr : del_nodes) {
  65. auto ret = graph->RemoveNode(node_ptr);
  66. if (ret != SUCCESS) {
  67. REPORT_CALL_ERROR("E19999", "Remove node:%s(%s) from graph:%s failed",
  68. node_ptr->GetName().c_str(), node_ptr->GetType().c_str(), graph->GetName().c_str());
  69. GELOGE(ret, "[Remove][Node] %s(%s) from graph:%s failed",
  70. node_ptr->GetName().c_str(), node_ptr->GetType().c_str(), graph->GetName().c_str());
  71. return ret;
  72. }
  73. // update Target list
  74. vector<NodePtr> graph_target = graph->GetGraphTargetNodesInfo();
  75. auto iter = find(graph_target.begin(), graph_target.end(), node_ptr);
  76. if (iter != graph_target.end()) {
  77. GELOGI("Current node %s is as Target, remove it from target vector.", node_ptr->GetName().c_str());
  78. graph_target.erase(iter);
  79. graph->SetGraphTargetNodesInfo(graph_target);
  80. }
  81. }
  82. return SUCCESS;
  83. }
  84. } // namespace ge

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