• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "include/common/utils/compile_cache_context.h"
18 #include "utils/ms_context.h"
19 #include "utils/log_adapter.h"
20 
21 namespace mindspore {
CompileCacheEnable()22 bool CompileCacheEnable() {
23   auto enable = mindspore::MsContext::GetInstance()->get_param<int>(mindspore::MS_CTX_ENABLE_COMPILE_CACHE);
24   // no set enable_compile_cache in context, then read it from env variable.
25   if (enable < 0) {
26     const auto &env_enable = mindspore::common::GetEnv(mindspore::kCompilerCacheEnable);
27     return !env_enable.empty() && env_enable == "1";
28   }
29   // set enable_compile_cache to false
30   if (enable == 0) {
31     return false;
32   }
33   // set enable_compile_cache to true
34   return true;
35 }
36 
37 // normalize name for ge regex check
NormalizeString(const std::string & name)38 std::string NormalizeString(const std::string &name) {
39   std::string norm_str;
40   std::for_each(name.begin(), name.end(), [&norm_str](const auto &a) {
41     if (isalpha(a) || isalnum(a) || a == '_' || a == '-') {
42       norm_str += a;
43     }
44   });
45   const size_t limit_len = 128;
46   if (norm_str.size() > limit_len) {
47     norm_str = norm_str.substr(norm_str.size() - limit_len);
48   }
49   return norm_str;
50 }
51 
GetInstance()52 CompileCacheContext &CompileCacheContext::GetInstance() noexcept {
53   static CompileCacheContext instance{};
54   return instance;
55 }
56 
FindFrontNodeByFrontName(const std::string & name) const57 AnfNodePtr CompileCacheContext::FindFrontNodeByFrontName(const std::string &name) const {
58   auto iter = front_name_to_front_node_.find(name);
59   if (iter != front_name_to_front_node_.end()) {
60     return iter->second;
61   }
62   return nullptr;
63 }
64 
InsertFrontNameToFrontNode(const std::string & name,const AnfNodePtr & node)65 void CompileCacheContext::InsertFrontNameToFrontNode(const std::string &name, const AnfNodePtr &node) {
66   front_name_to_front_node_[name] = node;
67 }
68 
FindBackNodeByBackName(const std::string & name) const69 AnfNodePtr CompileCacheContext::FindBackNodeByBackName(const std::string &name) const {
70   auto iter = back_name_to_back_node_.find(name);
71   if (iter != back_name_to_back_node_.end()) {
72     return iter->second;
73   }
74   return nullptr;
75 }
76 
InsertBackNameToBackNode(const std::string & name,const AnfNodePtr & node)77 void CompileCacheContext::InsertBackNameToBackNode(const std::string &name, const AnfNodePtr &node) {
78   back_name_to_back_node_[name] = node;
79 }
80 
SetFusionOpBuildInfoFlag(bool fusion_op_build_info_flag)81 void CompileCacheContext::SetFusionOpBuildInfoFlag(bool fusion_op_build_info_flag) {
82   fusion_op_build_info_flag_ = fusion_op_build_info_flag;
83 }
84 
SetChildGraphs(const std::vector<FuncGraphPtr> & child_graphs)85 void CompileCacheContext::SetChildGraphs(const std::vector<FuncGraphPtr> &child_graphs) {
86   child_graphs_ = child_graphs;
87 }
88 
GetBackendGraphCachePath(const FuncGraphPtr & front_graph) const89 std::string CompileCacheContext::GetBackendGraphCachePath(const FuncGraphPtr &front_graph) const {
90   auto iter = front_graph_to_backend_graph_cache_path_.find(front_graph);
91   if (iter != front_graph_to_backend_graph_cache_path_.end()) {
92     return iter->second;
93   }
94   return "";
95 }
96 
InsertBackendGraphCachePath(const FuncGraphPtr & front_graph,const std::string & path)97 void CompileCacheContext::InsertBackendGraphCachePath(const FuncGraphPtr &front_graph, const std::string &path) {
98   front_graph_to_backend_graph_cache_path_[front_graph] = path;
99 }
100 
AddBackendGraphToFrontendGraph(const FuncGraphPtr & backend_graph,FuncGraph * frontend_graph)101 void CompileCacheContext::AddBackendGraphToFrontendGraph(const FuncGraphPtr &backend_graph, FuncGraph *frontend_graph) {
102   backend_graph_to_frontend_graph_[backend_graph] = frontend_graph;
103 }
104 
GetFrontendGraphByBackendGraph(const FuncGraphPtr & graph) const105 FuncGraph *CompileCacheContext::GetFrontendGraphByBackendGraph(const FuncGraphPtr &graph) const {
106   auto iter = backend_graph_to_frontend_graph_.find(graph);
107   if (iter != backend_graph_to_frontend_graph_.end()) {
108     return iter->second;
109   }
110   return nullptr;
111 }
112 
InsertBackendParamGenFromFrontendParam(const AnfNodePtr & node)113 void CompileCacheContext::InsertBackendParamGenFromFrontendParam(const AnfNodePtr &node) {
114   (void)(backend_param_gen_from_frontend_param_.insert(node));
115 }
116 
PushFullnameIoSizeInfo(const std::string & fullname,const CachedIOSizeInfo & io_size)117 void CompileCacheContext::PushFullnameIoSizeInfo(const std::string &fullname, const CachedIOSizeInfo &io_size) {
118   fullname_io_size[fullname] = io_size;
119 }
120 
GetIOSizeInfo(const std::string & fullname) const121 CachedIOSizeInfo CompileCacheContext::GetIOSizeInfo(const std::string &fullname) const {
122   auto iter = fullname_io_size.find(fullname);
123   if (iter != fullname_io_size.end()) {
124     return iter->second;
125   }
126   return CachedIOSizeInfo();
127 }
128 
Clear()129 void CompileCacheContext::Clear() {
130   front_name_to_front_node_.clear();
131   back_name_to_back_node_.clear();
132   front_graph_ = nullptr;
133   use_compile_cache_ = false;
134   fusion_op_build_info_flag_ = false;
135   child_graphs_.clear();
136   backend_graph_to_frontend_graph_.clear();
137   fullname_io_size.clear();
138   front_graph_to_backend_graph_cache_path_.clear();
139   backend_param_gen_from_frontend_param_.clear();
140   restricted_scenarios_ = false;
141 }
142 }  // namespace mindspore
143