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.

tuning_utils.h 5.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #ifndef MAIN_TUNING_UTILS_H
  17. #define MAIN_TUNING_UTILS_H
  18. #include <fcntl.h>
  19. #include <sys/stat.h>
  20. #include <sys/types.h>
  21. #include <unistd.h>
  22. #include <algorithm>
  23. #include <cstring>
  24. #include <fstream>
  25. #include <iomanip>
  26. #include <queue>
  27. #include <mutex>
  28. #include <graph/anchor.h>
  29. #include <graph/detail/attributes_holder.h>
  30. #include <graph/ge_tensor.h>
  31. #include <graph/graph.h>
  32. #include <graph/model.h>
  33. #include <graph/node.h>
  34. #include <graph/utils/graph_utils.h>
  35. #include <graph/utils/type_utils.h>
  36. #include "framework/common/debug/ge_log.h"
  37. #include "utils/attr_utils.h"
  38. #include "utils/node_utils.h"
  39. #include "external/ge/ge_api_types.h"
  40. #include "graph/debug/ge_attr_define.h"
  41. #include "graph/utils/op_desc_utils.h"
  42. #include "graph/utils/tensor_utils.h"
  43. namespace ge {
  44. // Configure build mode, default value is "normal"
  45. const char *const BUILD_MODE = "ge.buildMode";
  46. const char *const BUILD_STEP = "ge.buildStep";
  47. // Configure tuning path
  48. const char *const TUNING_PATH = "ge.tuningPath";
  49. // for interface: aclgrphBuildModel
  50. const std::set<std::string> ir_builder_supported_options_for_lx_fusion = {BUILD_MODE, BUILD_STEP, TUNING_PATH};
  51. // Build model
  52. const char *const BUILD_MODE_NORMAL = "normal";
  53. const char *const BUILD_MODE_TUNING = "tuning";
  54. const char *const BUILD_MODE_BASELINE = "baseline";
  55. const std::set<std::string> build_mode_options = {BUILD_MODE_NORMAL, BUILD_MODE_TUNING, BUILD_MODE_BASELINE};
  56. // Build step
  57. const char *const BUILD_STEP_BEFORE_UB_MATCH = "before_ub_match";
  58. const char *const BUILD_STEP_AFTER_UB_MATCH = "after_ub_match";
  59. const char *const BUILD_STEP_AFTER_BUILDER = "after_builder";
  60. const char *const BUILD_STEP_AFTER_BUILDER_SUB = "after_builder_sub";
  61. const char *const BUILD_STEP_AFTER_MERGE = "after_merge";
  62. const std::set<std::string> build_step_options = {BUILD_STEP_BEFORE_UB_MATCH, BUILD_STEP_AFTER_UB_MATCH,
  63. BUILD_STEP_AFTER_BUILDER, BUILD_STEP_AFTER_BUILDER_SUB,
  64. BUILD_STEP_AFTER_MERGE};
  65. using SubgraphCreateOutNode = std::unordered_map<ComputeGraphPtr, NodePtr>;
  66. using NodetoNodeMap = std::unordered_map<NodePtr, NodePtr>;
  67. using NodeVec = std::vector<NodePtr>;
  68. using NodeNametoNodeNameMap = std::unordered_map<std::string, std::string>;
  69. using NodetoNodeNameMap = std::unordered_map<NodePtr, std::string>;
  70. class TuningUtils {
  71. public:
  72. TuningUtils() = default;
  73. ~TuningUtils() = default;
  74. // Dump all the subgraphs and modify
  75. // the subgraphs in them to be executable subgraphs if exe_flag is true
  76. // `tuning_path` means path to save the graphs
  77. static graphStatus ConvertGraphToFile(std::vector<ComputeGraphPtr> tuning_subgraphs,
  78. std::vector<ComputeGraphPtr> non_tuning_subgraphs = {}, bool exe_flag = false,
  79. const std::string &path = "", const std::string &user_path = "");
  80. // Recovery `graph` from graph dump files configured in options
  81. static graphStatus ConvertFileToGraph(const map<int64_t, string> &options, ge::Graph &graph);
  82. private:
  83. // part 1
  84. struct HelpInfo {
  85. int64_t index;
  86. bool exe_flag;
  87. bool is_tuning_graph;
  88. const std::string &path;
  89. const std::string &user_path;
  90. };
  91. static graphStatus MakeExeGraph(ComputeGraphPtr &exe_graph, const HelpInfo &help_info);
  92. static graphStatus HandlePld(NodePtr &node);
  93. static graphStatus HandleEnd(NodePtr &node);
  94. static graphStatus ChangePld2Data(NodePtr &node, NodePtr &data_node);
  95. static graphStatus ChangeEnd2NetOutput(NodePtr &node, NodePtr &out_node);
  96. static graphStatus LinkEnd2NetOutput(NodePtr &node, NodePtr &out_node);
  97. static graphStatus CreateDataNode(NodePtr &node, NodePtr &data_node);
  98. static graphStatus CreateNetOutput(NodePtr &node, NodePtr &out_node);
  99. static graphStatus AddAttrToDataNodeForMergeGraph(const NodePtr &pld, NodePtr &data_node);
  100. static graphStatus AddAttrToNetOutputForMergeGraph(const NodePtr &end, NodePtr &out_node);
  101. static void DumpGraphToPath(ComputeGraphPtr &exe_graph, int64_t index, bool is_tuning_graph, std::string path);
  102. static SubgraphCreateOutNode create_output_;
  103. // part 2
  104. static graphStatus MergeAllSubGraph(std::vector<ComputeGraphPtr> &graphs, ComputeGraphPtr &graph);
  105. static graphStatus MergeSubGraph(ComputeGraphPtr &graph);
  106. // Deletes new data and output nodes added by call `MakeExeGraph()` func in part 1
  107. static graphStatus RemoveDataNetoutputEdge(ComputeGraphPtr &graph);
  108. static graphStatus GetInAndOutAnchorPair(NodePtr &data_node, NodePtr &out_node, AnchorPtr &dest_in_anchor,
  109. AnchorPtr &src_out_anchor);
  110. static NodeNametoNodeNameMap data_2_netoutput_;
  111. static NodetoNodeNameMap data_node_2_netoutput_;
  112. static NodetoNodeMap data_node_2_netoutput_node_;
  113. static NodeVec netoutput_nodes_;
  114. static NodeVec merged_graph_nodes_;
  115. static std::mutex mutex_;
  116. // for debug
  117. static std::string PrintCheckLog();
  118. static std::string GetNodeNameByAnchor(const Anchor *anchor);
  119. };
  120. } // namespace ge
  121. #endif // MAIN_TUNING_UTILS_H

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