From 3310e208fdc846e3e929f42009345e90c7841205 Mon Sep 17 00:00:00 2001 From: Jiaming Yuan Date: Wed, 28 Oct 2020 13:13:59 +0800 Subject: [PATCH] Fix inplace prediction interval. (#6259) * Add back the interval in call. * Make the interval non-optional. --- include/xgboost/learner.h | 2 +- src/c_api/c_api.cc | 12 ++++++------ src/c_api/c_api.cu | 4 ++-- src/gbm/gbtree.h | 4 ++-- src/learner.cc | 2 +- tests/python/test_predict.py | 5 +++++ 6 files changed, 17 insertions(+), 12 deletions(-) diff --git a/include/xgboost/learner.h b/include/xgboost/learner.h index 1b4d12d59..50b20de67 100644 --- a/include/xgboost/learner.h +++ b/include/xgboost/learner.h @@ -133,7 +133,7 @@ class Learner : public Model, public Configurable, public dmlc::Serializable { virtual void InplacePredict(dmlc::any const& x, std::string const& type, float missing, HostDeviceVector **out_preds, - uint32_t layer_begin = 0, uint32_t layer_end = 0) = 0; + uint32_t layer_begin, uint32_t layer_end) = 0; void LoadModel(Json const& in) override = 0; void SaveModel(Json* out) const override = 0; diff --git a/src/c_api/c_api.cc b/src/c_api/c_api.cc index 4e5aff625..d91b179f0 100644 --- a/src/c_api/c_api.cc +++ b/src/c_api/c_api.cc @@ -528,8 +528,8 @@ XGB_DLL int XGBoosterPredictFromDense(BoosterHandle handle, float *values, xgboost::bst_ulong n_rows, xgboost::bst_ulong n_cols, float missing, - unsigned, - unsigned, + unsigned iteration_begin, + unsigned iteration_end, char const* c_type, xgboost::bst_ulong cache_id, xgboost::bst_ulong *out_len, @@ -543,7 +543,7 @@ XGB_DLL int XGBoosterPredictFromDense(BoosterHandle handle, float *values, new xgboost::data::DenseAdapter(values, n_rows, n_cols)}; HostDeviceVector* p_predt { nullptr }; std::string type { c_type }; - learner->InplacePredict(x, type, missing, &p_predt); + learner->InplacePredict(x, type, missing, &p_predt, iteration_begin, iteration_end); CHECK(p_predt); *out_result = dmlc::BeginPtr(p_predt->HostVector()); @@ -560,8 +560,8 @@ XGB_DLL int XGBoosterPredictFromCSR(BoosterHandle handle, size_t nelem, size_t num_col, float missing, - unsigned, - unsigned, + unsigned iteration_begin, + unsigned iteration_end, char const *c_type, xgboost::bst_ulong cache_id, xgboost::bst_ulong *out_len, @@ -575,7 +575,7 @@ XGB_DLL int XGBoosterPredictFromCSR(BoosterHandle handle, new xgboost::data::CSRAdapter(indptr, indices, data, nindptr - 1, nelem, num_col)}; HostDeviceVector* p_predt { nullptr }; std::string type { c_type }; - learner->InplacePredict(x, type, missing, &p_predt); + learner->InplacePredict(x, type, missing, &p_predt, iteration_begin, iteration_end); CHECK(p_predt); *out_result = dmlc::BeginPtr(p_predt->HostVector()); diff --git a/src/c_api/c_api.cu b/src/c_api/c_api.cu index 5af04894d..ecebb0e98 100644 --- a/src/c_api/c_api.cu +++ b/src/c_api/c_api.cu @@ -49,7 +49,7 @@ XGB_DLL int XGBoosterPredictFromArrayInterfaceColumns(BoosterHandle handle, auto x = std::make_shared(json_str); HostDeviceVector* p_predt { nullptr }; std::string type { c_type }; - learner->InplacePredict(x, type, missing, &p_predt); + learner->InplacePredict(x, type, missing, &p_predt, iteration_begin, iteration_end); CHECK(p_predt); CHECK(p_predt->DeviceCanRead()); @@ -77,7 +77,7 @@ XGB_DLL int XGBoosterPredictFromArrayInterface(BoosterHandle handle, auto x = std::make_shared(json_str); HostDeviceVector* p_predt { nullptr }; std::string type { c_type }; - learner->InplacePredict(x, type, missing, &p_predt); + learner->InplacePredict(x, type, missing, &p_predt, iteration_begin, iteration_end); CHECK(p_predt); CHECK(p_predt->DeviceCanRead()); diff --git a/src/gbm/gbtree.h b/src/gbm/gbtree.h index fbb227d2b..f96a895ae 100644 --- a/src/gbm/gbtree.h +++ b/src/gbm/gbtree.h @@ -207,8 +207,8 @@ class GBTree : public GradientBooster { void InplacePredict(dmlc::any const &x, float missing, PredictionCacheEntry *out_preds, - uint32_t layer_begin = 0, - unsigned layer_end = 0) const override { + uint32_t layer_begin, + unsigned layer_end) const override { CHECK(configured_); // From here on, layer becomes concrete trees. bst_group_t groups = model_.learner_model_param->num_output_group; diff --git a/src/learner.cc b/src/learner.cc index f6af88e9e..85ca3a503 100644 --- a/src/learner.cc +++ b/src/learner.cc @@ -1098,7 +1098,7 @@ class LearnerImpl : public LearnerIO { void InplacePredict(dmlc::any const &x, std::string const &type, float missing, HostDeviceVector **out_preds, - uint32_t layer_begin = 0, uint32_t layer_end = 0) override { + uint32_t layer_begin, uint32_t layer_end) override { this->Configure(); auto& out_predictions = this->GetThreadLocal().prediction_entry; this->gbm_->InplacePredict(x, missing, &out_predictions, layer_begin, diff --git a/tests/python/test_predict.py b/tests/python/test_predict.py index 088cf2912..1fff11d9e 100644 --- a/tests/python/test_predict.py +++ b/tests/python/test_predict.py @@ -44,6 +44,11 @@ class TestInplacePredict(unittest.TestCase): np.testing.assert_allclose(predt_from_dmatrix, predt_from_array) + predt_from_array = booster.inplace_predict(X[:10, ...], iteration_range=(0, 4)) + predt_from_dmatrix = booster.predict(test, ntree_limit=4) + + np.testing.assert_allclose(predt_from_dmatrix, predt_from_array) + def predict_dense(x): inplace_predt = booster.inplace_predict(x) d = xgb.DMatrix(x)