Unverified Commit da811d46 authored by jcipar's avatar jcipar Committed by GitHub
Browse files

Config static initialization (#2743)

* Change static map/set to functions that return static variables

* Fixed newline

* testing CI

* removed commented-out code fixed formatting

* return const references to parameter_set and alias_table

* fixup whitespace

* Too many requests error when running CI on docs. Triggering a rebuild.
parent 63c8c036
...@@ -269,15 +269,24 @@ def gen_parameter_code(config_hpp, config_out_cpp): ...@@ -269,15 +269,24 @@ def gen_parameter_code(config_hpp, config_out_cpp):
""" """
str_to_write += "#include<LightGBM/config.h>\nnamespace LightGBM {\n" str_to_write += "#include<LightGBM/config.h>\nnamespace LightGBM {\n"
# alias table # alias table
str_to_write += "std::unordered_map<std::string, std::string> Config::alias_table({\n" str_to_write += "const std::unordered_map<std::string, std::string>& Config::alias_table() {\n"
str_to_write += " static std::unordered_map<std::string, std::string> aliases({\n"
for pair in alias: for pair in alias:
str_to_write += " {\"%s\", \"%s\"},\n" % (pair[0], pair[1]) str_to_write += " {\"%s\", \"%s\"},\n" % (pair[0], pair[1])
str_to_write += "});\n\n" str_to_write += " });\n"
str_to_write += " return aliases;\n"
str_to_write += "}\n\n"
# names # names
str_to_write += "std::unordered_set<std::string> Config::parameter_set({\n" str_to_write += "const std::unordered_set<std::string>& Config::parameter_set() {\n"
str_to_write += " static std::unordered_set<std::string> params({\n"
for name in names: for name in names:
str_to_write += " \"%s\",\n" % (name) str_to_write += " \"%s\",\n" % (name)
str_to_write += "});\n\n" str_to_write += " });\n"
str_to_write += " return params;\n"
str_to_write += "}\n\n"
# from strings # from strings
str_to_write += "void Config::GetMembersFromString(const std::unordered_map<std::string, std::string>& params) {\n" str_to_write += "void Config::GetMembersFromString(const std::unordered_map<std::string, std::string>& params) {\n"
str_to_write += " std::string tmp_str = \"\";\n" str_to_write += " std::string tmp_str = \"\";\n"
......
...@@ -890,8 +890,8 @@ struct Config { ...@@ -890,8 +890,8 @@ struct Config {
bool is_parallel = false; bool is_parallel = false;
bool is_parallel_find_bin = false; bool is_parallel_find_bin = false;
LIGHTGBM_EXPORT void Set(const std::unordered_map<std::string, std::string>& params); LIGHTGBM_EXPORT void Set(const std::unordered_map<std::string, std::string>& params);
static std::unordered_map<std::string, std::string> alias_table; static const std::unordered_map<std::string, std::string>& alias_table();
static std::unordered_set<std::string> parameter_set; static const std::unordered_set<std::string>& parameter_set();
std::vector<std::vector<double>> auc_mu_weights_matrix; std::vector<std::vector<double>> auc_mu_weights_matrix;
private: private:
...@@ -960,8 +960,8 @@ struct ParameterAlias { ...@@ -960,8 +960,8 @@ struct ParameterAlias {
static void KeyAliasTransform(std::unordered_map<std::string, std::string>* params) { static void KeyAliasTransform(std::unordered_map<std::string, std::string>* params) {
std::unordered_map<std::string, std::string> tmp_map; std::unordered_map<std::string, std::string> tmp_map;
for (const auto& pair : *params) { for (const auto& pair : *params) {
auto alias = Config::alias_table.find(pair.first); auto alias = Config::alias_table().find(pair.first);
if (alias != Config::alias_table.end()) { // found alias if (alias != Config::alias_table().end()) { // found alias
auto alias_set = tmp_map.find(alias->second); auto alias_set = tmp_map.find(alias->second);
if (alias_set != tmp_map.end()) { // alias already set if (alias_set != tmp_map.end()) { // alias already set
// set priority by length & alphabetically to ensure reproducible behavior // set priority by length & alphabetically to ensure reproducible behavior
...@@ -979,7 +979,7 @@ struct ParameterAlias { ...@@ -979,7 +979,7 @@ struct ParameterAlias {
} else { // alias not set } else { // alias not set
tmp_map.emplace(alias->second, pair.first); tmp_map.emplace(alias->second, pair.first);
} }
} else if (Config::parameter_set.find(pair.first) == Config::parameter_set.end()) { } else if (Config::parameter_set().find(pair.first) == Config::parameter_set().end()) {
Log::Warning("Unknown parameter: %s", pair.first.c_str()); Log::Warning("Unknown parameter: %s", pair.first.c_str());
} }
} }
......
...@@ -7,7 +7,8 @@ ...@@ -7,7 +7,8 @@
*/ */
#include<LightGBM/config.h> #include<LightGBM/config.h>
namespace LightGBM { namespace LightGBM {
std::unordered_map<std::string, std::string> Config::alias_table({ const std::unordered_map<std::string, std::string>& Config::alias_table() {
static std::unordered_map<std::string, std::string> aliases({
{"config_file", "config"}, {"config_file", "config"},
{"task_type", "task"}, {"task_type", "task"},
{"objective_type", "objective"}, {"objective_type", "objective"},
...@@ -165,9 +166,12 @@ std::unordered_map<std::string, std::string> Config::alias_table({ ...@@ -165,9 +166,12 @@ std::unordered_map<std::string, std::string> Config::alias_table({
{"mlist", "machine_list_filename"}, {"mlist", "machine_list_filename"},
{"workers", "machines"}, {"workers", "machines"},
{"nodes", "machines"}, {"nodes", "machines"},
}); });
return aliases;
}
std::unordered_set<std::string> Config::parameter_set({ const std::unordered_set<std::string>& Config::parameter_set() {
static std::unordered_set<std::string> params({
"config", "config",
"task", "task",
"objective", "objective",
...@@ -287,7 +291,9 @@ std::unordered_set<std::string> Config::parameter_set({ ...@@ -287,7 +291,9 @@ std::unordered_set<std::string> Config::parameter_set({
"gpu_platform_id", "gpu_platform_id",
"gpu_device_id", "gpu_device_id",
"gpu_use_dp", "gpu_use_dp",
}); });
return params;
}
void Config::GetMembersFromString(const std::unordered_map<std::string, std::string>& params) { void Config::GetMembersFromString(const std::unordered_map<std::string, std::string>& params) {
std::string tmp_str = ""; std::string tmp_str = "";
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment