Commit 1f88721a authored by Chris Smith's avatar Chris Smith Committed by Guolin Ke
Browse files

Fix Integer Overflow #2357 (#2400)

* Fix integer overflow https://github.com/microsoft/LightGBM/issues/2357

* Use 2 spaces not 4

* Move constant the config.h
Move check outside the max_depth check

* Move the max leaves check to config.h

* Remove unnecessary check
parent d064019f
......@@ -135,7 +135,7 @@ Core Parameters
- in ``dart``, it also affects on normalization weights of dropped trees
- ``num_leaves`` :raw-html:`<a id="num_leaves" title="Permalink to this parameter" href="#num_leaves">&#x1F517;&#xFE0E;</a>`, default = ``31``, type = int, aliases: ``num_leaf``, ``max_leaves``, ``max_leaf``, constraints: ``num_leaves > 1``
- ``num_leaves`` :raw-html:`<a id="num_leaves" title="Permalink to this parameter" href="#num_leaves">&#x1F517;&#xFE0E;</a>`, default = ``31``, type = int, aliases: ``num_leaf``, ``max_leaves``, ``max_leaf``, constraints: ``1 < num_leaves <= 131072``
- max number of leaves in one tree
......
......@@ -166,6 +166,7 @@ struct Config {
// default = 31
// alias = num_leaf, max_leaves, max_leaf
// check = >1
// check = <=131072
// desc = max number of leaves in one tree
int num_leaves = kDefaultNumLeaves;
......
......@@ -320,12 +320,16 @@ void Config::CheckParamConflict() {
}
// Check max_depth and num_leaves
if (max_depth > 0) {
int full_num_leaves = static_cast<int>(std::pow(2, max_depth));
double full_num_leaves = std::pow(2, max_depth);
if (full_num_leaves > num_leaves
&& num_leaves == kDefaultNumLeaves) {
Log::Warning("Accuracy may be bad since you didn't set num_leaves and 2^max_depth > num_leaves");
}
num_leaves = std::min(num_leaves, 2 << max_depth);
if (full_num_leaves < num_leaves) {
// Fits in an int, and is more restrictive than the current num_leaves
num_leaves = static_cast<int>(full_num_leaves);
}
}
}
......
......@@ -301,6 +301,7 @@ void Config::GetMembersFromString(const std::unordered_map<std::string, std::str
GetInt(params, "num_leaves", &num_leaves);
CHECK(num_leaves >1);
CHECK(num_leaves <=131072);
GetInt(params, "num_threads", &num_threads);
......
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