From a7207e26bee59af4ae488490ca0da27d857f0d7e Mon Sep 17 00:00:00 2001 From: fis Date: Sat, 30 Jan 2021 17:21:39 +0800 Subject: [PATCH 1/3] Align device id in predict transform with predictor. --- src/objective/aft_obj.cu | 2 +- src/objective/hinge.cu | 2 +- src/objective/multiclass_obj.cu | 2 +- src/objective/regression_obj.cu | 8 ++++---- tests/cpp/objective/test_aft_obj.cc | 2 +- tests/cpp/objective/test_objective.cc | 22 ++++++++++++++++++++++ 6 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/objective/aft_obj.cu b/src/objective/aft_obj.cu index da55a7fe4589..fadd449dc538 100644 --- a/src/objective/aft_obj.cu +++ b/src/objective/aft_obj.cu @@ -108,7 +108,7 @@ class AFTObj : public ObjFunction { [] XGBOOST_DEVICE(size_t _idx, common::Span _preds) { _preds[_idx] = exp(_preds[_idx]); }, common::Range{0, static_cast(io_preds->Size())}, - tparam_->gpu_id) + io_preds->DeviceIdx()) .Eval(io_preds); } diff --git a/src/objective/hinge.cu b/src/objective/hinge.cu index b889cb81d23e..8e37aa0dbd1d 100644 --- a/src/objective/hinge.cu +++ b/src/objective/hinge.cu @@ -74,7 +74,7 @@ class HingeObj : public ObjFunction { _preds[_idx] = _preds[_idx] > 0.0 ? 1.0 : 0.0; }, common::Range{0, static_cast(io_preds->Size()), 1}, - tparam_->gpu_id) + io_preds->DeviceIdx()) .Eval(io_preds); } diff --git a/src/objective/multiclass_obj.cu b/src/objective/multiclass_obj.cu index d39c5a460303..4286b6c40f24 100644 --- a/src/objective/multiclass_obj.cu +++ b/src/objective/multiclass_obj.cu @@ -136,7 +136,7 @@ class SoftmaxMultiClassObj : public ObjFunction { const auto ndata = static_cast(io_preds->Size() / nclass); max_preds_.Resize(ndata); - auto device = tparam_->gpu_id; + auto device = io_preds->DeviceIdx(); if (prob) { common::Transform<>::Init( [=] XGBOOST_DEVICE(size_t _idx, common::Span _preds) { diff --git a/src/objective/regression_obj.cu b/src/objective/regression_obj.cu index 764850d1938f..c94b90aa7fb1 100644 --- a/src/objective/regression_obj.cu +++ b/src/objective/regression_obj.cu @@ -113,7 +113,7 @@ class RegLossObj : public ObjFunction { [] XGBOOST_DEVICE(size_t _idx, common::Span _preds) { _preds[_idx] = Loss::PredTransform(_preds[_idx]); }, common::Range{0, static_cast(io_preds->Size())}, - tparam_->gpu_id) + io_preds->DeviceIdx()) .Eval(io_preds); } @@ -238,7 +238,7 @@ class PoissonRegression : public ObjFunction { _preds[_idx] = expf(_preds[_idx]); }, common::Range{0, static_cast(io_preds->Size())}, - tparam_->gpu_id) + io_preds->DeviceIdx()) .Eval(io_preds); } void EvalTransform(HostDeviceVector *io_preds) override { @@ -426,7 +426,7 @@ class GammaRegression : public ObjFunction { _preds[_idx] = expf(_preds[_idx]); }, common::Range{0, static_cast(io_preds->Size())}, - tparam_->gpu_id) + io_preds->DeviceIdx()) .Eval(io_preds); } void EvalTransform(HostDeviceVector *io_preds) override { @@ -529,7 +529,7 @@ class TweedieRegression : public ObjFunction { _preds[_idx] = expf(_preds[_idx]); }, common::Range{0, static_cast(io_preds->Size())}, - tparam_->gpu_id) + io_preds->DeviceIdx()) .Eval(io_preds); } diff --git a/tests/cpp/objective/test_aft_obj.cc b/tests/cpp/objective/test_aft_obj.cc index ff1193f9e930..3dc26e89d79a 100644 --- a/tests/cpp/objective/test_aft_obj.cc +++ b/tests/cpp/objective/test_aft_obj.cc @@ -49,7 +49,7 @@ static inline void CheckGPairOverGridPoints( const int num_point = 20; const double log_y_low = 1.0; const double log_y_high = 15.0; - + obj->Configure({ {"aft_loss_distribution", dist_type}, {"aft_loss_distribution_scale", "1"} }); diff --git a/tests/cpp/objective/test_objective.cc b/tests/cpp/objective/test_objective.cc index 72c6c271db0e..3879964f83d4 100644 --- a/tests/cpp/objective/test_objective.cc +++ b/tests/cpp/objective/test_objective.cc @@ -17,3 +17,25 @@ TEST(Objective, UnknownFunction) { delete obj; } } + +namespace xgboost { +TEST(Objective, PredTransform) { + // Test that show PredTransform uses the same device with predictor. + xgboost::GenericParameter tparam; + Args args{{"gpu_id", "0"}}; // set to deviec + tparam.UpdateAllowUnknown(args); + size_t n = 100; + + for (const auto &entry : + ::dmlc::Registry<::xgboost::ObjFunctionReg>::List()) { + std::unique_ptr obj{ + xgboost::ObjFunction::Create(entry->name, &tparam)}; + HostDeviceVector predts; + predts.Resize(n); // prediction is performed on host. + ASSERT_FALSE(predts.DeviceCanRead()) << entry->name; + obj->PredTransform(&predts); + ASSERT_FALSE(predts.DeviceCanRead()) << entry->name; + ASSERT_TRUE(predts.HostCanWrite()); + } +} +} // namespace xgboost From 06ed0b0cf5b1f7593f390d7ff0f51ef36de24e14 Mon Sep 17 00:00:00 2001 From: fis Date: Tue, 2 Feb 2021 01:00:34 +0800 Subject: [PATCH 2/3] Try removing the ref. --- tests/cpp/objective/test_objective.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/cpp/objective/test_objective.cc b/tests/cpp/objective/test_objective.cc index 3879964f83d4..bb9859fb1092 100644 --- a/tests/cpp/objective/test_objective.cc +++ b/tests/cpp/objective/test_objective.cc @@ -26,8 +26,7 @@ TEST(Objective, PredTransform) { tparam.UpdateAllowUnknown(args); size_t n = 100; - for (const auto &entry : - ::dmlc::Registry<::xgboost::ObjFunctionReg>::List()) { + for (const auto entry : ::dmlc::Registry<::xgboost::ObjFunctionReg>::List()) { std::unique_ptr obj{ xgboost::ObjFunction::Create(entry->name, &tparam)}; HostDeviceVector predts; From 3ab6b79e87e022058b75058adeed6e60bd7321eb Mon Sep 17 00:00:00 2001 From: fis Date: Tue, 2 Feb 2021 01:55:39 +0800 Subject: [PATCH 3/3] Fix num_class. --- tests/cpp/objective/test_objective.cc | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tests/cpp/objective/test_objective.cc b/tests/cpp/objective/test_objective.cc index bb9859fb1092..fd110deb1f2e 100644 --- a/tests/cpp/objective/test_objective.cc +++ b/tests/cpp/objective/test_objective.cc @@ -22,18 +22,19 @@ namespace xgboost { TEST(Objective, PredTransform) { // Test that show PredTransform uses the same device with predictor. xgboost::GenericParameter tparam; - Args args{{"gpu_id", "0"}}; // set to deviec - tparam.UpdateAllowUnknown(args); + tparam.UpdateAllowUnknown(Args{{"gpu_id", "0"}}); size_t n = 100; - for (const auto entry : ::dmlc::Registry<::xgboost::ObjFunctionReg>::List()) { + for (const auto &entry : + ::dmlc::Registry<::xgboost::ObjFunctionReg>::List()) { std::unique_ptr obj{ xgboost::ObjFunction::Create(entry->name, &tparam)}; + obj->Configure(Args{{"num_class", "2"}}); HostDeviceVector predts; - predts.Resize(n); // prediction is performed on host. - ASSERT_FALSE(predts.DeviceCanRead()) << entry->name; + predts.Resize(n, 3.14f); // prediction is performed on host. + ASSERT_FALSE(predts.DeviceCanRead()); obj->PredTransform(&predts); - ASSERT_FALSE(predts.DeviceCanRead()) << entry->name; + ASSERT_FALSE(predts.DeviceCanRead()); ASSERT_TRUE(predts.HostCanWrite()); } }