• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2024 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 "utils/compile_config.h"
18 #include <string>
19 #include <utility>
20 #include "utils/log_adapter.h"
21 
22 namespace mindspore {
GetInstance()23 CompileConfigManager &CompileConfigManager::GetInstance() noexcept {
24   static CompileConfigManager instance;
25   return instance;
26 }
27 
CollectCompileConfig()28 void CompileConfigManager::CollectCompileConfig() {
29   if (collect_finished_) {
30     return;
31   }
32   if (collect_func_ == nullptr) {
33     MS_LOG(INTERNAL_EXCEPTION) << "Compile config not registered.";
34   }
35   MS_LOG(DEBUG) << "To collect all compile configs.";
36   compile_config_ = collect_func_();
37   collect_finished_ = true;
38 }
39 
SetConfig(const std::string & config_name,const std::string & value,bool overwrite)40 void CompileConfigManager::SetConfig(const std::string &config_name, const std::string &value, bool overwrite) {
41   if (!overwrite && compile_config_.find(config_name) != compile_config_.end()) {
42     return;
43   }
44   compile_config_.insert_or_assign(config_name, value);
45 }
46 
GetConfig(const std::string & config_name)47 std::string CompileConfigManager::GetConfig(const std::string &config_name) {
48   if (compile_config_.empty()) {
49     MS_LOG(INFO) << "The compile config is empty when getting config '" << config_name << "'.";
50     return "";
51   }
52   auto iter = compile_config_.find(config_name);
53   if (iter == compile_config_.end()) {
54     MS_LOG(INTERNAL_EXCEPTION) << "'" << config_name << "' is not a compile config.";
55   }
56   MS_LOG(DEBUG) << "Get Compile Config. " << config_name << ": " << iter->second;
57   return iter->second;
58 }
59 
60 namespace common {
GetCompileConfig(const std::string & config_name)61 std::string GetCompileConfig(const std::string &config_name) {
62   return CompileConfigManager::GetInstance().GetConfig(config_name);
63 }
64 
SetCompileConfig(const std::string & config_name,const std::string & value,bool overwrite)65 void SetCompileConfig(const std::string &config_name, const std::string &value, bool overwrite) {
66   CompileConfigManager::GetInstance().SetConfig(config_name, value, overwrite);
67 }
68 }  // namespace common
69 }  // namespace mindspore
70