sync Mar 27 2023

This commit is contained in:
amdsc21
2023-03-27 18:54:41 +02:00
43 changed files with 643 additions and 508 deletions

View File

@@ -1,5 +1,5 @@
/*!
* Copyright (c) by Contributors 2019-2022
/**
* Copyright (c) 2019-2023, XGBoost Contributors
*/
#include <gtest/gtest.h>
@@ -8,7 +8,8 @@
#include "../../../src/common/charconv.h"
#include "../../../src/common/io.h"
#include "../filesystem.h" // dmlc::TemporaryDirectory
#include "../../../src/common/threading_utils.h" // for ParallelFor
#include "../filesystem.h" // dmlc::TemporaryDirectory
#include "../helpers.h"
#include "dmlc/logging.h"
#include "xgboost/json.h"

View File

@@ -505,7 +505,7 @@ TEST(GBTree, PredictRange) {
auto h_out_predt_full = out_predt->HostVector();
ASSERT_TRUE(std::equal(h_out_predt.begin(), h_out_predt.end(), h_out_predt_full.begin()));
// Out of range.
ASSERT_THROW(learner->InplacePredict(x, PredictionType::kValue,
std::numeric_limits<float>::quiet_NaN(), &out_predt, 0, 3),
dmlc::Error);

View File

@@ -557,23 +557,6 @@ std::unique_ptr<DMatrix> CreateSparsePageDMatrixWithRC(
return dmat;
}
gbm::GBTreeModel CreateTestModel(LearnerModelParam const* param, Context const* ctx,
size_t n_classes) {
gbm::GBTreeModel model(param, ctx);
for (size_t i = 0; i < n_classes; ++i) {
std::vector<std::unique_ptr<RegTree>> trees;
trees.push_back(std::unique_ptr<RegTree>(new RegTree));
if (i == 0) {
(*trees.back())[0].SetLeaf(1.5f);
(*trees.back()).Stat(0).sum_hess = 1.0f;
}
model.CommitModel(std::move(trees), i);
}
return model;
}
std::unique_ptr<GradientBooster> CreateTrainedGBM(std::string name, Args kwargs, size_t kRows,
size_t kCols,
LearnerModelParam const* learner_model_param,

View File

@@ -9,8 +9,10 @@
#include <xgboost/base.h>
#include <xgboost/context.h>
#include <xgboost/json.h>
#include <xgboost/learner.h> // for LearnerModelParam
#include <xgboost/model.h> // for Configurable
#include <cstdint> // std::int32_t
#include <cstdint> // std::int32_t
#include <cstdio>
#include <fstream>
#include <iostream>
@@ -22,7 +24,6 @@
#include "../../src/collective/communicator-inl.h"
#include "../../src/common/common.h"
#include "../../src/data/array_interface.h"
#include "../../src/gbm/gbtree_model.h"
#include "filesystem.h" // dmlc::TemporaryDirectory
#include "xgboost/linalg.h"
@@ -362,9 +363,6 @@ std::unique_ptr<DMatrix> CreateSparsePageDMatrixWithRC(
size_t n_rows, size_t n_cols, size_t page_size, bool deterministic,
const dmlc::TemporaryDirectory& tempdir = dmlc::TemporaryDirectory());
gbm::GBTreeModel CreateTestModel(LearnerModelParam const* param, Context const* ctx,
size_t n_classes = 1);
std::unique_ptr<GradientBooster> CreateTrainedGBM(std::string name, Args kwargs, size_t kRows,
size_t kCols,
LearnerModelParam const* learner_model_param,

View File

@@ -1,5 +1,5 @@
/*!
* Copyright 2017-2022 XGBoost contributors
/**
* Copyright 2017-2023, XGBoost contributors
*/
#include <gtest/gtest.h>
#include <xgboost/c_api.h>
@@ -159,7 +159,7 @@ TEST(GPUPredictor, ShapStump) {
std::vector<std::unique_ptr<RegTree>> trees;
trees.push_back(std::unique_ptr<RegTree>(new RegTree));
model.CommitModel(std::move(trees), 0);
model.CommitModelGroup(std::move(trees), 0);
auto gpu_lparam = CreateEmptyGenericParam(0);
std::unique_ptr<Predictor> gpu_predictor = std::unique_ptr<Predictor>(
@@ -187,7 +187,7 @@ TEST(GPUPredictor, Shap) {
std::vector<std::unique_ptr<RegTree>> trees;
trees.push_back(std::unique_ptr<RegTree>(new RegTree));
trees[0]->ExpandNode(0, 0, 0.5, true, 1.0, -1.0, 1.0, 0.0, 5.0, 2.0, 3.0);
model.CommitModel(std::move(trees), 0);
model.CommitModelGroup(std::move(trees), 0);
auto gpu_lparam = CreateEmptyGenericParam(0);
auto cpu_lparam = CreateEmptyGenericParam(-1);

View File

@@ -209,7 +209,7 @@ void GBTreeModelForTest(gbm::GBTreeModel *model, uint32_t split_ind,
p_tree->ExpandCategorical(0, split_ind, split_cats, true, 1.5f,
left_weight, right_weight,
3.0f, 2.2f, 7.0f, 9.0f);
model->CommitModel(std::move(trees), 0);
model->CommitModelGroup(std::move(trees), 0);
}
void TestCategoricalPrediction(std::string name) {
@@ -445,7 +445,7 @@ void TestVectorLeafPrediction(Context const *ctx) {
ASSERT_TRUE(mparam.IsVectorLeaf());
gbm::GBTreeModel model{&mparam, ctx};
model.CommitModel(std::move(trees), 0);
model.CommitModelGroup(std::move(trees), 0);
auto run_test = [&](float expected, HostDeviceVector<float> *p_data) {
{

View File

@@ -14,6 +14,23 @@
#include "../helpers.h"
namespace xgboost {
inline gbm::GBTreeModel CreateTestModel(LearnerModelParam const* param, Context const* ctx,
size_t n_classes = 1) {
gbm::GBTreeModel model(param, ctx);
for (size_t i = 0; i < n_classes; ++i) {
std::vector<std::unique_ptr<RegTree>> trees;
trees.push_back(std::unique_ptr<RegTree>(new RegTree));
if (i == 0) {
(*trees.back())[0].SetLeaf(1.5f);
(*trees.back()).Stat(0).sum_hess = 1.0f;
}
model.CommitModelGroup(std::move(trees), i);
}
return model;
}
template <typename Page>
void TestPredictionFromGradientIndex(std::string name, size_t rows, size_t cols,
std::shared_ptr<DMatrix> p_hist) {

View File

@@ -1,7 +1,10 @@
// Copyright (c) 2019-2022 by Contributors
/**
* Copyright (c) 2019-2023, XGBoost Contributors
*/
#include <gtest/gtest.h>
#include <xgboost/base.h>
#include <xgboost/data.h>
#include <xgboost/feature_map.h> // for FeatureMap
#include <xgboost/json.h>
#include <xgboost/learner.h>

View File

@@ -256,7 +256,7 @@ void UpdateTree(HostDeviceVector<GradientPair>* gpair, DMatrix* dmat,
std::vector<HostDeviceVector<bst_node_t>> position(1);
hist_maker.Update(&param, gpair, dmat, common::Span<HostDeviceVector<bst_node_t>>{position},
{tree});
auto cache = linalg::VectorView<float>{preds->DeviceSpan(), {preds->Size()}, 0};
auto cache = linalg::MakeTensorView(&ctx, preds->DeviceSpan(), preds->Size(), 1);
hist_maker.UpdatePredictionCache(dmat, cache);
}

View File

@@ -15,15 +15,17 @@ namespace xgboost {
class TestPredictionCache : public ::testing::Test {
std::shared_ptr<DMatrix> Xy_;
size_t n_samples_{2048};
std::size_t n_samples_{2048};
protected:
void SetUp() override {
size_t n_features = 13;
Xy_ = RandomDataGenerator{n_samples_, n_features, 0}.GenerateDMatrix(true);
std::size_t n_features = 13;
bst_target_t n_targets = 3;
Xy_ = RandomDataGenerator{n_samples_, n_features, 0}.Targets(n_targets).GenerateDMatrix(true);
}
void RunLearnerTest(std::string updater_name, float subsample, std::string grow_policy) {
void RunLearnerTest(std::string updater_name, float subsample, std::string const& grow_policy,
std::string const& strategy) {
std::unique_ptr<Learner> learner{Learner::Create({Xy_})};
if (updater_name == "grow_gpu_hist") {
// gpu_id setup
@@ -31,6 +33,7 @@ class TestPredictionCache : public ::testing::Test {
} else {
learner->SetParam("updater", updater_name);
}
learner->SetParam("multi_strategy", strategy);
learner->SetParam("grow_policy", grow_policy);
learner->SetParam("subsample", std::to_string(subsample));
learner->SetParam("nthread", "0");
@@ -62,7 +65,7 @@ class TestPredictionCache : public ::testing::Test {
}
}
void RunTest(std::string updater_name) {
void RunTest(std::string const& updater_name, std::string const& strategy) {
{
Context ctx;
ctx.InitAllowUnknown(Args{{"nthread", "8"}});
@@ -85,28 +88,31 @@ class TestPredictionCache : public ::testing::Test {
HostDeviceVector<float> out_prediction_cached;
out_prediction_cached.SetDevice(ctx.gpu_id);
out_prediction_cached.Resize(n_samples_);
auto cache = linalg::VectorView<float>{ctx.gpu_id == Context::kCpuId
? out_prediction_cached.HostSpan()
: out_prediction_cached.DeviceSpan(),
{out_prediction_cached.Size()},
ctx.gpu_id};
auto cache =
linalg::MakeTensorView(&ctx, &out_prediction_cached, out_prediction_cached.Size(), 1);
ASSERT_TRUE(updater->UpdatePredictionCache(Xy_.get(), cache));
}
for (auto policy : {"depthwise", "lossguide"}) {
for (auto subsample : {1.0f, 0.4f}) {
this->RunLearnerTest(updater_name, subsample, policy);
this->RunLearnerTest(updater_name, subsample, policy);
this->RunLearnerTest(updater_name, subsample, policy, strategy);
this->RunLearnerTest(updater_name, subsample, policy, strategy);
}
}
}
};
TEST_F(TestPredictionCache, Approx) { this->RunTest("grow_histmaker"); }
TEST_F(TestPredictionCache, Approx) { this->RunTest("grow_histmaker", "one_output_per_tree"); }
TEST_F(TestPredictionCache, Hist) { this->RunTest("grow_quantile_histmaker"); }
TEST_F(TestPredictionCache, Hist) {
this->RunTest("grow_quantile_histmaker", "one_output_per_tree");
}
TEST_F(TestPredictionCache, HistMulti) {
this->RunTest("grow_quantile_histmaker", "multi_output_tree");
}
#if defined(XGBOOST_USE_CUDA) || defined(XGBOOST_USE_HIP)
TEST_F(TestPredictionCache, GpuHist) { this->RunTest("grow_gpu_hist"); }
TEST_F(TestPredictionCache, GpuHist) { this->RunTest("grow_gpu_hist", "one_output_per_tree"); }
#endif // defined(XGBOOST_USE_CUDA) || defined(XGBOOST_USE_HIP)
} // namespace xgboost