Skip to content

Commit da811d4

Browse files
authored
Config static initialization (microsoft#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.
1 parent 63c8c03 commit da811d4

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

helpers/parameter_generator.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,15 +269,24 @@ def gen_parameter_code(config_hpp, config_out_cpp):
269269
"""
270270
str_to_write += "#include<LightGBM/config.h>\nnamespace LightGBM {\n"
271271
# alias table
272-
str_to_write += "std::unordered_map<std::string, std::string> Config::alias_table({\n"
272+
str_to_write += "const std::unordered_map<std::string, std::string>& Config::alias_table() {\n"
273+
str_to_write += " static std::unordered_map<std::string, std::string> aliases({\n"
274+
273275
for pair in alias:
274276
str_to_write += " {\"%s\", \"%s\"},\n" % (pair[0], pair[1])
275-
str_to_write += "});\n\n"
277+
str_to_write += " });\n"
278+
str_to_write += " return aliases;\n"
279+
str_to_write += "}\n\n"
280+
276281
# names
277-
str_to_write += "std::unordered_set<std::string> Config::parameter_set({\n"
282+
str_to_write += "const std::unordered_set<std::string>& Config::parameter_set() {\n"
283+
str_to_write += " static std::unordered_set<std::string> params({\n"
284+
278285
for name in names:
279286
str_to_write += " \"%s\",\n" % (name)
280-
str_to_write += "});\n\n"
287+
str_to_write += " });\n"
288+
str_to_write += " return params;\n"
289+
str_to_write += "}\n\n"
281290
# from strings
282291
str_to_write += "void Config::GetMembersFromString(const std::unordered_map<std::string, std::string>& params) {\n"
283292
str_to_write += " std::string tmp_str = \"\";\n"

include/LightGBM/config.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -890,8 +890,8 @@ struct Config {
890890
bool is_parallel = false;
891891
bool is_parallel_find_bin = false;
892892
LIGHTGBM_EXPORT void Set(const std::unordered_map<std::string, std::string>& params);
893-
static std::unordered_map<std::string, std::string> alias_table;
894-
static std::unordered_set<std::string> parameter_set;
893+
static const std::unordered_map<std::string, std::string>& alias_table();
894+
static const std::unordered_set<std::string>& parameter_set();
895895
std::vector<std::vector<double>> auc_mu_weights_matrix;
896896

897897
private:
@@ -960,8 +960,8 @@ struct ParameterAlias {
960960
static void KeyAliasTransform(std::unordered_map<std::string, std::string>* params) {
961961
std::unordered_map<std::string, std::string> tmp_map;
962962
for (const auto& pair : *params) {
963-
auto alias = Config::alias_table.find(pair.first);
964-
if (alias != Config::alias_table.end()) { // found alias
963+
auto alias = Config::alias_table().find(pair.first);
964+
if (alias != Config::alias_table().end()) { // found alias
965965
auto alias_set = tmp_map.find(alias->second);
966966
if (alias_set != tmp_map.end()) { // alias already set
967967
// set priority by length & alphabetically to ensure reproducible behavior
@@ -979,7 +979,7 @@ struct ParameterAlias {
979979
} else { // alias not set
980980
tmp_map.emplace(alias->second, pair.first);
981981
}
982-
} else if (Config::parameter_set.find(pair.first) == Config::parameter_set.end()) {
982+
} else if (Config::parameter_set().find(pair.first) == Config::parameter_set().end()) {
983983
Log::Warning("Unknown parameter: %s", pair.first.c_str());
984984
}
985985
}

src/io/config_auto.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
*/
88
#include<LightGBM/config.h>
99
namespace LightGBM {
10-
std::unordered_map<std::string, std::string> Config::alias_table({
10+
const std::unordered_map<std::string, std::string>& Config::alias_table() {
11+
static std::unordered_map<std::string, std::string> aliases({
1112
{"config_file", "config"},
1213
{"task_type", "task"},
1314
{"objective_type", "objective"},
@@ -165,9 +166,12 @@ std::unordered_map<std::string, std::string> Config::alias_table({
165166
{"mlist", "machine_list_filename"},
166167
{"workers", "machines"},
167168
{"nodes", "machines"},
168-
});
169+
});
170+
return aliases;
171+
}
169172

170-
std::unordered_set<std::string> Config::parameter_set({
173+
const std::unordered_set<std::string>& Config::parameter_set() {
174+
static std::unordered_set<std::string> params({
171175
"config",
172176
"task",
173177
"objective",
@@ -287,7 +291,9 @@ std::unordered_set<std::string> Config::parameter_set({
287291
"gpu_platform_id",
288292
"gpu_device_id",
289293
"gpu_use_dp",
290-
});
294+
});
295+
return params;
296+
}
291297

292298
void Config::GetMembersFromString(const std::unordered_map<std::string, std::string>& params) {
293299
std::string tmp_str = "";

0 commit comments

Comments
 (0)