Skip to content

Commit

Permalink
Fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
trivialfis committed Jul 28, 2022
1 parent 1e4d161 commit be31d98
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
2 changes: 1 addition & 1 deletion include/xgboost/linalg.h
Expand Up @@ -679,7 +679,7 @@ class Tensor {
}
if (device >= 0) {
data_.SetDevice(device);
data_.DevicePointer(); // Pull to device;
data_.ConstDevicePointer(); // Pull to device;
}
CHECK_EQ(data_.Size(), detail::CalcSize(shape_));
}
Expand Down
29 changes: 15 additions & 14 deletions src/learner.cc
Expand Up @@ -416,22 +416,11 @@ class LearnerConfiguration : public Learner {

this->ConfigureTargets();

// Before 1.0.0, we save `base_score` into binary as a transformed value by objective.
// After 1.0.0 we save the value provided by user and keep it immutable instead. To
// keep the stability, we initialize it in binary LoadModel instead of configuration.
// Under what condition should we omit the transformation:
//
// - base_score is loaded from old binary model.
//
// What are the other possible conditions:
//
// - model loaded from new binary or JSON.
// - model is created from scratch.
// - model is configured second time due to change of parameter
this->ConfigureLearnerParam(p_fmat);

learner_model_param_.task = obj_->Task(); // required by gbm configuration.
this->ConfigureGBM(old_tparam, args);
ctx_.ConfigureGpuId(this->gbm_->UseGPU());
this->ConfigureLearnerParam(p_fmat);

this->ConfigureMetrics(args);

this->need_configuration_ = false;
Expand All @@ -447,6 +436,18 @@ class LearnerConfiguration : public Learner {
* \brief Calculate the `base_score` based on input data.
*/
void ConfigureLearnerParam(DMatrix const* p_fmat) {
// Before 1.0.0, we save `base_score` into binary as a transformed value by objective.
// After 1.0.0 we save the value provided by user and keep it immutable instead. To
// keep the stability, we initialize it in binary LoadModel instead of configuration.
// Under what condition should we omit the transformation:
//
// - base_score is loaded from old binary model.
//
// What are the other possible conditions:
//
// - model loaded from new binary or JSON.
// - model is created from scratch.
// - model is configured second time due to change of parameter
CHECK(obj_);
float world = rabit::GetWorldSize();
if (base_score_.Size() != 0) {
Expand Down
6 changes: 4 additions & 2 deletions tests/cpp/helpers.h
Expand Up @@ -455,9 +455,11 @@ RMMAllocatorPtr SetUpRMMResourceForCppTests(int argc, char** argv);
/*
* \brief Make learner model param
*/
inline LearnerModelParam MakeMP(bst_feature_t n_features, float base_score, uint32_t n_groups) {
inline LearnerModelParam MakeMP(bst_feature_t n_features, float base_score, uint32_t n_groups,
int32_t device = Context::kCpuId) {
size_t shape[1]{1};
LearnerModelParam mparam(n_features, linalg::Tensor<float, 1>{{base_score}, shape}, n_groups);
LearnerModelParam mparam(n_features, linalg::Tensor<float, 1>{{base_score}, shape, device},
n_groups);
return mparam;
}

Expand Down
16 changes: 10 additions & 6 deletions tests/cpp/predictor/test_gpu_predictor.cu
Expand Up @@ -35,8 +35,9 @@ TEST(GPUPredictor, Basic) {
int n_row = i, n_col = i;
auto dmat = RandomDataGenerator(n_row, n_col, 0).GenerateDMatrix();

LearnerModelParam mparam{MakeMP(n_col, .5, 1)};
GenericParameter ctx;
Context ctx;
ctx.gpu_id = 0;
LearnerModelParam mparam{MakeMP(n_col, .5, 1, ctx.gpu_id)};
gbm::GBTreeModel model = CreateTestModel(&mparam, &ctx);

// Test predict batch
Expand Down Expand Up @@ -90,9 +91,10 @@ TEST(GPUPredictor, ExternalMemoryTest) {
gpu_predictor->Configure({});

const int n_classes = 3;
LearnerModelParam mparam{MakeMP(5, .5, n_classes)};

Context ctx;
ctx.gpu_id = 0;
LearnerModelParam mparam{MakeMP(5, .5, n_classes, ctx.gpu_id)};

gbm::GBTreeModel model = CreateTestModel(&mparam, &ctx, n_classes);
std::vector<std::unique_ptr<DMatrix>> dmats;

Expand Down Expand Up @@ -163,8 +165,9 @@ TEST(GpuPredictor, LesserFeatures) {
TEST(GPUPredictor, ShapStump) {
cudaSetDevice(0);

LearnerModelParam mparam{MakeMP(1, .5, 1)};
Context ctx;
ctx.gpu_id = 0;
LearnerModelParam mparam{MakeMP(1, .5, 1, ctx.gpu_id)};
gbm::GBTreeModel model(&mparam, &ctx);

std::vector<std::unique_ptr<RegTree>> trees;
Expand All @@ -189,8 +192,9 @@ TEST(GPUPredictor, ShapStump) {
}

TEST(GPUPredictor, Shap) {
LearnerModelParam mparam{MakeMP(1, .5, 1)};
Context ctx;
ctx.gpu_id = 0;
LearnerModelParam mparam{MakeMP(1, .5, 1, ctx.gpu_id)};
gbm::GBTreeModel model(&mparam, &ctx);

std::vector<std::unique_ptr<RegTree>> trees;
Expand Down

0 comments on commit be31d98

Please sign in to comment.