1 /** 2 * Copyright 2023 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 17 #ifndef MINDSPORE_CCSRC_INCLUDE_COMMON_UTILS_COMPILE_CACHE_CONTEXT_H_ 18 #define MINDSPORE_CCSRC_INCLUDE_COMMON_UTILS_COMPILE_CACHE_CONTEXT_H_ 19 20 #include <string> 21 #include <map> 22 #include <vector> 23 24 #include "include/common/utils/utils.h" 25 #include "include/common/visible.h" 26 #include "ir/anf.h" 27 28 namespace mindspore { 29 constexpr char kGraphCacheSubDir[] = "graph_cache"; 30 constexpr char kBackendGraphCacheSubDir[] = "backend_graph_cache"; 31 constexpr char kCompileCacheFileName[] = "compile_cache"; 32 constexpr char kBackendCompileCacheFileName[] = "backend_compile_cache"; 33 constexpr char kMindIrSuffix[] = ".mindir"; 34 constexpr char kJsonSuffix[] = ".json"; 35 constexpr char kDepFilesHashPath[] = "compile_dependency.hash"; 36 constexpr char kRoleServer[] = "server_"; 37 constexpr char kRolePServer[] = "pserver_"; 38 constexpr char kRolePScheduler[] = "pscheduler_"; 39 constexpr char kGroupCkptFileName[] = "group.ckpt"; 40 constexpr char kDataQueueNameCacheFileName[] = "data_queue_name.json"; 41 42 struct CachedIOSizeInfo { 43 std::string json_name; 44 std::vector<size_t> input_size_list; 45 std::vector<size_t> output_size_list; 46 }; 47 48 COMMON_EXPORT bool CompileCacheEnable(); 49 50 COMMON_EXPORT std::string NormalizeString(const std::string &name); 51 52 class COMMON_EXPORT CompileCacheContext { 53 public: 54 CompileCacheContext(const CompileCacheContext &) = delete; 55 CompileCacheContext &operator=(const CompileCacheContext &) = delete; 56 static CompileCacheContext &GetInstance() noexcept; SetFrontNameToFrontNode(const HashMap<std::string,AnfNodePtr> & map)57 void SetFrontNameToFrontNode(const HashMap<std::string, AnfNodePtr> &map) { front_name_to_front_node_ = map; } 58 AnfNodePtr FindFrontNodeByFrontName(const std::string &name) const; ClearFrontNameToFrontNode()59 void ClearFrontNameToFrontNode() { front_name_to_front_node_.clear(); } 60 void InsertFrontNameToFrontNode(const std::string &name, const AnfNodePtr &node); 61 SetBackNameToBackNode(const HashMap<std::string,AnfNodePtr> & map)62 void SetBackNameToBackNode(const HashMap<std::string, AnfNodePtr> &map) { back_name_to_back_node_ = map; } 63 AnfNodePtr FindBackNodeByBackName(const std::string &name) const; ClearBackNameToBackNode()64 void ClearBackNameToBackNode() { back_name_to_back_node_.clear(); } 65 void InsertBackNameToBackNode(const std::string &name, const AnfNodePtr &node); 66 UseCompileCache()67 bool UseCompileCache() const { return use_compile_cache_; } SetUseCompileCache(bool use_compile_cache)68 void SetUseCompileCache(bool use_compile_cache) { use_compile_cache_ = use_compile_cache; } 69 FusionOpBuildInfoFlag()70 bool FusionOpBuildInfoFlag() const { return fusion_op_build_info_flag_; } 71 void SetFusionOpBuildInfoFlag(bool fusion_op_build_info_flag); 72 SetFrontGraph(const FuncGraphPtr & graph)73 void SetFrontGraph(const FuncGraphPtr &graph) { front_graph_ = graph; } FrontGraph()74 FuncGraphPtr FrontGraph() const { return front_graph_; } 75 76 void SetChildGraphs(const std::vector<FuncGraphPtr> &child_graphs); GetChileGraphs()77 std::vector<FuncGraphPtr> GetChileGraphs() const { return child_graphs_; } ClearChildGraphs()78 void ClearChildGraphs() { child_graphs_.clear(); } 79 80 // acquire backend graph cache path according to its correspond front_graph 81 std::string GetBackendGraphCachePath(const FuncGraphPtr &front_graph) const; 82 void InsertBackendGraphCachePath(const FuncGraphPtr &front_graph, const std::string &path); 83 84 void AddBackendGraphToFrontendGraph(const FuncGraphPtr &backend_graph, FuncGraph *frontend_graph); 85 FuncGraph *GetFrontendGraphByBackendGraph(const FuncGraphPtr &graph) const; 86 87 void PushFullnameIoSizeInfo(const std::string &fullname, const CachedIOSizeInfo &io_size); 88 CachedIOSizeInfo GetIOSizeInfo(const std::string &fullname) const; 89 90 void InsertBackendParamGenFromFrontendParam(const AnfNodePtr &node); IsBackendParamGenFromFrontendParam(const AnfNodePtr & node)91 bool IsBackendParamGenFromFrontendParam(const AnfNodePtr &node) const { 92 return backend_param_gen_from_frontend_param_.count(node) != 0; 93 } RestrictedScenarios()94 bool RestrictedScenarios() const { return restricted_scenarios_; } SetRestrictedScenarios(bool restricted_scenarios)95 void SetRestrictedScenarios(bool restricted_scenarios) { restricted_scenarios_ = restricted_scenarios; } 96 void Clear(); 97 SetCompileCacheDepFilesHash(const std::string & compile_cache_dep_files_hash)98 void SetCompileCacheDepFilesHash(const std::string &compile_cache_dep_files_hash) { 99 compile_cache_dep_files_hash_ = compile_cache_dep_files_hash; 100 } CompileCacheDepFilesHash()101 std::string CompileCacheDepFilesHash() { return compile_cache_dep_files_hash_; } 102 set_has_cached_queue_name(bool cached)103 void set_has_cached_queue_name(bool cached) { has_cached_queue_name_ = cached; } has_cached_queue_name()104 bool has_cached_queue_name() const { return has_cached_queue_name_; } 105 set_init_compile_cache(const bool & init)106 void set_init_compile_cache(const bool &init) { init_compile_cache_ = init; } init_compile_cache()107 bool init_compile_cache() const { return init_compile_cache_; } 108 109 private: 110 CompileCacheContext() = default; 111 ~CompileCacheContext() = default; 112 // name is unique for node here. 113 HashMap<std::string, AnfNodePtr> front_name_to_front_node_; 114 HashMap<std::string, AnfNodePtr> back_name_to_back_node_; 115 116 // The number of front-end and back-end graphs is one-to-many per compile. 117 FuncGraphPtr front_graph_; 118 bool use_compile_cache_{false}; 119 bool fusion_op_build_info_flag_{false}; 120 // key is parent funcgarph, values is child funcgraph. 121 std::vector<FuncGraphPtr> child_graphs_; 122 HashMap<FuncGraphPtr, FuncGraph *> backend_graph_to_frontend_graph_; 123 HashMap<FuncGraphPtr, std::string> front_graph_to_backend_graph_cache_path_; 124 std::map<std::string, CachedIOSizeInfo> fullname_io_size; 125 // param is a backend node but we can find its correspond frontend param. 126 mindspore::HashSet<AnfNodePtr> backend_param_gen_from_frontend_param_; 127 bool restricted_scenarios_{false}; 128 std::string compile_cache_dep_files_hash_ = ""; 129 bool has_cached_queue_name_{false}; 130 bool init_compile_cache_{false}; 131 }; 132 } // namespace mindspore 133 134 #endif // MINDSPORE_CCSRC_INCLUDE_COMMON_UTILS_COMPILE_CACHE_CONTEXT_H_ 135