From a144daf0340357bc8fc39628451319e215b1b905 Mon Sep 17 00:00:00 2001 From: Jiaming Yuan Date: Sat, 22 Aug 2020 19:34:52 +0800 Subject: [PATCH] Limit tree depth for GPU hist. (#6045) --- python-package/xgboost/core.py | 2 +- python-package/xgboost/sklearn.py | 4 ++-- src/tree/param.h | 4 ++++ tests/cpp/tree/test_gpu_hist.cu | 12 ++++++++++++ 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/python-package/xgboost/core.py b/python-package/xgboost/core.py index 2a69fea51..c8d046082 100644 --- a/python-package/xgboost/core.py +++ b/python-package/xgboost/core.py @@ -40,7 +40,7 @@ class EarlyStopException(Exception): """ def __init__(self, best_iteration): - super(EarlyStopException, self).__init__() + super().__init__() self.best_iteration = best_iteration diff --git a/python-package/xgboost/sklearn.py b/python-package/xgboost/sklearn.py index 4721533b4..96d358128 100644 --- a/python-package/xgboost/sklearn.py +++ b/python-package/xgboost/sklearn.py @@ -1025,7 +1025,7 @@ class XGBRFClassifier(XGBClassifier): **kwargs) def get_xgb_params(self): - params = super(XGBRFClassifier, self).get_xgb_params() + params = super().get_xgb_params() params['num_parallel_tree'] = self.n_estimators return params @@ -1057,7 +1057,7 @@ class XGBRFRegressor(XGBRegressor): reg_lambda=reg_lambda, **kwargs) def get_xgb_params(self): - params = super(XGBRFRegressor, self).get_xgb_params() + params = super().get_xgb_params() params['num_parallel_tree'] = self.n_estimators return params diff --git a/src/tree/param.h b/src/tree/param.h index 280f06066..dedc2a7f0 100644 --- a/src/tree/param.h +++ b/src/tree/param.h @@ -239,6 +239,10 @@ struct TrainParam : public XGBoostParameter { if (this->max_leaves > 0) { n_nodes = this->max_leaves * 2 - 1; } else { + // bst_node_t will overflow. + CHECK_LE(this->max_depth, 31) + << "max_depth can not be greater than 31 as that might generate 2 ** " + "32 - 1 nodes."; n_nodes = (1 << (this->max_depth + 1)) - 1; } CHECK_NE(n_nodes, 0); diff --git a/tests/cpp/tree/test_gpu_hist.cu b/tests/cpp/tree/test_gpu_hist.cu index 153cafb88..5199a27d2 100644 --- a/tests/cpp/tree/test_gpu_hist.cu +++ b/tests/cpp/tree/test_gpu_hist.cu @@ -505,5 +505,17 @@ TEST(GpuHist, ConfigIO) { ASSERT_EQ(j_updater, j_updater_roundtrip); } +TEST(GpuHist, MaxDepth) { + GenericParameter generic_param(CreateEmptyGenericParam(0)); + size_t constexpr kRows = 16; + size_t constexpr kCols = 4; + auto p_mat = RandomDataGenerator{kRows, kCols, 0}.GenerateDMatrix(); + + auto learner = std::unique_ptr(Learner::Create({p_mat})); + learner->SetParam("max_depth", "32"); + learner->Configure(); + + ASSERT_THROW({learner->UpdateOneIter(0, p_mat);}, dmlc::Error); +} } // namespace tree } // namespace xgboost