Use matrix for gradient. (#9508)
- Use the `linalg::Matrix` for storing gradients. - New API for the custom objective. - Custom objective for multi-class/multi-target is now required to return the correct shape. - Custom objective for Python can accept arrays with any strides. (row-major, column-major)
This commit is contained in:
@@ -565,7 +565,7 @@ void TestXGDMatrixGetQuantileCut(Context const *ctx) {
|
||||
ASSERT_EQ(XGBoosterCreate(mats.data(), 1, &booster), 0);
|
||||
ASSERT_EQ(XGBoosterSetParam(booster, "max_bin", "16"), 0);
|
||||
if (ctx->IsCUDA()) {
|
||||
ASSERT_EQ(XGBoosterSetParam(booster, "tree_method", "gpu_hist"), 0);
|
||||
ASSERT_EQ(XGBoosterSetParam(booster, "device", ctx->DeviceName().c_str()), 0);
|
||||
}
|
||||
ASSERT_EQ(XGBoosterUpdateOneIter(booster, 0, p_fmat), 0);
|
||||
ASSERT_EQ(XGDMatrixGetQuantileCut(p_fmat, s_config.c_str(), &out_indptr, &out_data), 0);
|
||||
@@ -596,7 +596,7 @@ void TestXGDMatrixGetQuantileCut(Context const *ctx) {
|
||||
ASSERT_EQ(XGBoosterCreate(mats.data(), 1, &booster), 0);
|
||||
ASSERT_EQ(XGBoosterSetParam(booster, "max_bin", "16"), 0);
|
||||
if (ctx->IsCUDA()) {
|
||||
ASSERT_EQ(XGBoosterSetParam(booster, "tree_method", "gpu_hist"), 0);
|
||||
ASSERT_EQ(XGBoosterSetParam(booster, "device", ctx->DeviceName().c_str()), 0);
|
||||
}
|
||||
ASSERT_EQ(XGBoosterUpdateOneIter(booster, 0, p_fmat), 0);
|
||||
ASSERT_EQ(XGDMatrixGetQuantileCut(p_fmat, s_config.c_str(), &out_indptr, &out_data), 0);
|
||||
|
||||
@@ -65,7 +65,9 @@ TEST(GBTree, PredictionCache) {
|
||||
|
||||
gbtree.Configure({{"tree_method", "hist"}});
|
||||
auto p_m = RandomDataGenerator{kRows, kCols, 0}.GenerateDMatrix();
|
||||
auto gpair = GenerateRandomGradients(kRows);
|
||||
linalg::Matrix<GradientPair> gpair({kRows}, ctx.Ordinal());
|
||||
gpair.Data()->Copy(GenerateRandomGradients(kRows));
|
||||
|
||||
PredictionCacheEntry out_predictions;
|
||||
gbtree.DoBoost(p_m.get(), &gpair, &out_predictions, nullptr);
|
||||
|
||||
@@ -213,7 +215,8 @@ TEST(GBTree, ChooseTreeMethod) {
|
||||
}
|
||||
learner->Configure();
|
||||
for (std::int32_t i = 0; i < 3; ++i) {
|
||||
HostDeviceVector<GradientPair> gpair{GenerateRandomGradients(Xy->Info().num_row_)};
|
||||
linalg::Matrix<GradientPair> gpair{{Xy->Info().num_row_}, Context::kCpuId};
|
||||
gpair.Data()->Copy(GenerateRandomGradients(Xy->Info().num_row_));
|
||||
learner->BoostOneIter(0, Xy, &gpair);
|
||||
}
|
||||
|
||||
|
||||
@@ -96,9 +96,9 @@ void CheckObjFunctionImpl(std::unique_ptr<xgboost::ObjFunction> const& obj,
|
||||
std::vector<xgboost::bst_float> out_grad,
|
||||
std::vector<xgboost::bst_float> out_hess) {
|
||||
xgboost::HostDeviceVector<xgboost::bst_float> in_preds(preds);
|
||||
xgboost::HostDeviceVector<xgboost::GradientPair> out_gpair;
|
||||
obj->GetGradient(in_preds, info, 1, &out_gpair);
|
||||
std::vector<xgboost::GradientPair>& gpair = out_gpair.HostVector();
|
||||
xgboost::linalg::Matrix<xgboost::GradientPair> out_gpair;
|
||||
obj->GetGradient(in_preds, info, 0, &out_gpair);
|
||||
std::vector<xgboost::GradientPair>& gpair = out_gpair.Data()->HostVector();
|
||||
|
||||
ASSERT_EQ(gpair.size(), in_preds.Size());
|
||||
for (int i = 0; i < static_cast<int>(gpair.size()); ++i) {
|
||||
@@ -119,8 +119,8 @@ void CheckObjFunction(std::unique_ptr<xgboost::ObjFunction> const& obj,
|
||||
std::vector<xgboost::bst_float> out_hess) {
|
||||
xgboost::MetaInfo info;
|
||||
info.num_row_ = labels.size();
|
||||
info.labels =
|
||||
xgboost::linalg::Tensor<float, 2>{labels.cbegin(), labels.cend(), {labels.size()}, -1};
|
||||
info.labels = xgboost::linalg::Tensor<float, 2>{
|
||||
labels.cbegin(), labels.cend(), {labels.size(), static_cast<std::size_t>(1)}, -1};
|
||||
info.weights_.HostVector() = weights;
|
||||
|
||||
CheckObjFunctionImpl(obj, preds, labels, weights, info, out_grad, out_hess);
|
||||
@@ -155,8 +155,8 @@ void CheckRankingObjFunction(std::unique_ptr<xgboost::ObjFunction> const& obj,
|
||||
std::vector<xgboost::bst_float> out_hess) {
|
||||
xgboost::MetaInfo info;
|
||||
info.num_row_ = labels.size();
|
||||
info.labels = xgboost::linalg::Tensor<float, 2>{
|
||||
labels.cbegin(), labels.cend(), {labels.size(), static_cast<size_t>(1)}, -1};
|
||||
info.labels = xgboost::linalg::Matrix<float>{
|
||||
labels.cbegin(), labels.cend(), {labels.size(), static_cast<std::size_t>(1)}, -1};
|
||||
info.weights_.HostVector() = weights;
|
||||
info.group_ptr_ = groups;
|
||||
|
||||
@@ -645,11 +645,10 @@ std::unique_ptr<GradientBooster> CreateTrainedGBM(std::string name, Args kwargs,
|
||||
}
|
||||
p_dmat->Info().labels =
|
||||
linalg::Tensor<float, 2>{labels.cbegin(), labels.cend(), {labels.size()}, -1};
|
||||
HostDeviceVector<GradientPair> gpair;
|
||||
auto& h_gpair = gpair.HostVector();
|
||||
h_gpair.resize(kRows);
|
||||
linalg::Matrix<GradientPair> gpair({kRows}, ctx->Ordinal());
|
||||
auto h_gpair = gpair.HostView();
|
||||
for (size_t i = 0; i < kRows; ++i) {
|
||||
h_gpair[i] = GradientPair{static_cast<float>(i), 1};
|
||||
h_gpair(i) = GradientPair{static_cast<float>(i), 1};
|
||||
}
|
||||
|
||||
PredictionCacheEntry predts;
|
||||
|
||||
@@ -387,23 +387,6 @@ std::unique_ptr<GradientBooster> CreateTrainedGBM(std::string name, Args kwargs,
|
||||
LearnerModelParam const* learner_model_param,
|
||||
Context const* generic_param);
|
||||
|
||||
inline std::unique_ptr<HostDeviceVector<GradientPair>> GenerateGradients(
|
||||
std::size_t rows, bst_target_t n_targets = 1) {
|
||||
auto p_gradients = std::make_unique<HostDeviceVector<GradientPair>>(rows * n_targets);
|
||||
auto& h_gradients = p_gradients->HostVector();
|
||||
|
||||
xgboost::SimpleLCG gen;
|
||||
xgboost::SimpleRealUniformDistribution<bst_float> dist(0.0f, 1.0f);
|
||||
|
||||
for (std::size_t i = 0; i < rows * n_targets; ++i) {
|
||||
auto grad = dist(&gen);
|
||||
auto hess = dist(&gen);
|
||||
h_gradients[i] = GradientPair{grad, hess};
|
||||
}
|
||||
|
||||
return p_gradients;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Make a context that uses CUDA if device >= 0.
|
||||
*/
|
||||
@@ -415,11 +398,12 @@ inline Context MakeCUDACtx(std::int32_t device) {
|
||||
}
|
||||
|
||||
inline HostDeviceVector<GradientPair> GenerateRandomGradients(const size_t n_rows,
|
||||
float lower= 0.0f, float upper = 1.0f) {
|
||||
float lower = 0.0f,
|
||||
float upper = 1.0f) {
|
||||
xgboost::SimpleLCG gen;
|
||||
xgboost::SimpleRealUniformDistribution<bst_float> dist(lower, upper);
|
||||
std::vector<GradientPair> h_gpair(n_rows);
|
||||
for (auto &gpair : h_gpair) {
|
||||
for (auto& gpair : h_gpair) {
|
||||
bst_float grad = dist(&gen);
|
||||
bst_float hess = dist(&gen);
|
||||
gpair = GradientPair(grad, hess);
|
||||
@@ -428,6 +412,16 @@ inline HostDeviceVector<GradientPair> GenerateRandomGradients(const size_t n_row
|
||||
return gpair;
|
||||
}
|
||||
|
||||
inline linalg::Matrix<GradientPair> GenerateRandomGradients(Context const* ctx, bst_row_t n_rows,
|
||||
bst_target_t n_targets,
|
||||
float lower = 0.0f,
|
||||
float upper = 1.0f) {
|
||||
auto g = GenerateRandomGradients(n_rows * n_targets, lower, upper);
|
||||
linalg::Matrix<GradientPair> gpair({n_rows, static_cast<bst_row_t>(n_targets)}, ctx->Device());
|
||||
gpair.Data()->Copy(g);
|
||||
return gpair;
|
||||
}
|
||||
|
||||
typedef void *DMatrixHandle; // NOLINT(*);
|
||||
|
||||
class ArrayIterForTest {
|
||||
|
||||
@@ -24,8 +24,8 @@ TEST(Linear, Shotgun) {
|
||||
auto updater =
|
||||
std::unique_ptr<xgboost::LinearUpdater>(xgboost::LinearUpdater::Create("shotgun", &ctx));
|
||||
updater->Configure({{"eta", "1."}});
|
||||
xgboost::HostDeviceVector<xgboost::GradientPair> gpair(
|
||||
p_fmat->Info().num_row_, xgboost::GradientPair(-5, 1.0));
|
||||
linalg::Matrix<xgboost::GradientPair> gpair{
|
||||
linalg::Constant(&ctx, xgboost::GradientPair(-5, 1.0), p_fmat->Info().num_row_, 1)};
|
||||
xgboost::gbm::GBLinearModel model{&mparam};
|
||||
model.LazyInitModel();
|
||||
updater->Update(&gpair, p_fmat.get(), &model, gpair.Size());
|
||||
@@ -55,8 +55,8 @@ TEST(Linear, coordinate) {
|
||||
auto updater = std::unique_ptr<xgboost::LinearUpdater>(
|
||||
xgboost::LinearUpdater::Create("coord_descent", &ctx));
|
||||
updater->Configure({{"eta", "1."}});
|
||||
xgboost::HostDeviceVector<xgboost::GradientPair> gpair(
|
||||
p_fmat->Info().num_row_, xgboost::GradientPair(-5, 1.0));
|
||||
linalg::Matrix<xgboost::GradientPair> gpair{
|
||||
linalg::Constant(&ctx, xgboost::GradientPair(-5, 1.0), p_fmat->Info().num_row_, 1)};
|
||||
xgboost::gbm::GBLinearModel model{&mparam};
|
||||
model.LazyInitModel();
|
||||
updater->Update(&gpair, p_fmat.get(), &model, gpair.Size());
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
// Copyright by Contributors
|
||||
/**
|
||||
* Copyright 2018-2023, XGBoost Contributors
|
||||
*/
|
||||
#include <xgboost/linear_updater.h>
|
||||
#include <xgboost/gbm.h>
|
||||
|
||||
@@ -19,8 +21,7 @@ TEST(Linear, GPUCoordinate) {
|
||||
auto updater = std::unique_ptr<xgboost::LinearUpdater>(
|
||||
xgboost::LinearUpdater::Create("gpu_coord_descent", &ctx));
|
||||
updater->Configure({{"eta", "1."}});
|
||||
xgboost::HostDeviceVector<xgboost::GradientPair> gpair(
|
||||
mat->Info().num_row_, xgboost::GradientPair(-5, 1.0));
|
||||
auto gpair = linalg::Constant(&ctx, xgboost::GradientPair(-5, 1.0), mat->Info().num_row_, 1);
|
||||
xgboost::gbm::GBLinearModel model{&mparam};
|
||||
|
||||
model.LazyInitModel();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*!
|
||||
* Copyright (c) by Contributors 2020
|
||||
/**
|
||||
* Copyright 2020-2023, XGBoost Contributors
|
||||
*/
|
||||
#include <gtest/gtest.h>
|
||||
#include <memory>
|
||||
@@ -12,9 +12,7 @@
|
||||
#include "../helpers.h"
|
||||
#include "../../../src/common/survival_util.h"
|
||||
|
||||
namespace xgboost {
|
||||
namespace common {
|
||||
|
||||
namespace xgboost::common {
|
||||
TEST(Objective, DeclareUnifiedTest(AFTObjConfiguration)) {
|
||||
auto ctx = MakeCUDACtx(GPUIDX);
|
||||
std::unique_ptr<ObjFunction> objective(ObjFunction::Create("survival:aft", &ctx));
|
||||
@@ -65,14 +63,14 @@ static inline void CheckGPairOverGridPoints(
|
||||
preds[i] = std::log(std::pow(2.0, i * (log_y_high - log_y_low) / (num_point - 1) + log_y_low));
|
||||
}
|
||||
|
||||
HostDeviceVector<GradientPair> out_gpair;
|
||||
linalg::Matrix<GradientPair> out_gpair;
|
||||
obj->GetGradient(HostDeviceVector<bst_float>(preds), info, 1, &out_gpair);
|
||||
const auto& gpair = out_gpair.HostVector();
|
||||
const auto gpair = out_gpair.HostView();
|
||||
CHECK_EQ(num_point, expected_grad.size());
|
||||
CHECK_EQ(num_point, expected_hess.size());
|
||||
for (int i = 0; i < num_point; ++i) {
|
||||
EXPECT_NEAR(gpair[i].GetGrad(), expected_grad[i], ftol);
|
||||
EXPECT_NEAR(gpair[i].GetHess(), expected_hess[i], ftol);
|
||||
EXPECT_NEAR(gpair(i).GetGrad(), expected_grad[i], ftol);
|
||||
EXPECT_NEAR(gpair(i).GetHess(), expected_hess[i], ftol);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -169,5 +167,4 @@ TEST(Objective, DeclareUnifiedTest(AFTObjGPairIntervalCensoredLabels)) {
|
||||
0.2757f, 0.1776f, 0.1110f, 0.0682f, 0.0415f, 0.0251f, 0.0151f, 0.0091f, 0.0055f, 0.0033f });
|
||||
}
|
||||
|
||||
} // namespace common
|
||||
} // namespace xgboost
|
||||
} // namespace xgboost::common
|
||||
|
||||
@@ -74,35 +74,35 @@ void TestNDCGGPair(Context const* ctx) {
|
||||
info.labels = linalg::Tensor<float, 2>{{0, 1, 0, 1}, {4, 1}, GPUIDX};
|
||||
info.group_ptr_ = {0, 2, 4};
|
||||
info.num_row_ = 4;
|
||||
HostDeviceVector<GradientPair> gpairs;
|
||||
linalg::Matrix<GradientPair> gpairs;
|
||||
obj->GetGradient(predts, info, 0, &gpairs);
|
||||
ASSERT_EQ(gpairs.Size(), predts.Size());
|
||||
|
||||
{
|
||||
predts = {1, 0, 1, 0};
|
||||
HostDeviceVector<GradientPair> gpairs;
|
||||
linalg::Matrix<GradientPair> gpairs;
|
||||
obj->GetGradient(predts, info, 0, &gpairs);
|
||||
for (size_t i = 0; i < gpairs.Size(); ++i) {
|
||||
ASSERT_GT(gpairs.HostSpan()[i].GetHess(), 0);
|
||||
for (std::size_t i = 0; i < gpairs.Size(); ++i) {
|
||||
ASSERT_GT(gpairs.HostView()(i).GetHess(), 0);
|
||||
}
|
||||
ASSERT_LT(gpairs.HostSpan()[1].GetGrad(), 0);
|
||||
ASSERT_LT(gpairs.HostSpan()[3].GetGrad(), 0);
|
||||
ASSERT_LT(gpairs.HostView()(1).GetGrad(), 0);
|
||||
ASSERT_LT(gpairs.HostView()(3).GetGrad(), 0);
|
||||
|
||||
ASSERT_GT(gpairs.HostSpan()[0].GetGrad(), 0);
|
||||
ASSERT_GT(gpairs.HostSpan()[2].GetGrad(), 0);
|
||||
ASSERT_GT(gpairs.HostView()(0).GetGrad(), 0);
|
||||
ASSERT_GT(gpairs.HostView()(2).GetGrad(), 0);
|
||||
|
||||
info.weights_ = {2, 3};
|
||||
HostDeviceVector<GradientPair> weighted_gpairs;
|
||||
linalg::Matrix<GradientPair> weighted_gpairs;
|
||||
obj->GetGradient(predts, info, 0, &weighted_gpairs);
|
||||
auto const& h_gpairs = gpairs.ConstHostSpan();
|
||||
auto const& h_weighted_gpairs = weighted_gpairs.ConstHostSpan();
|
||||
auto const& h_gpairs = gpairs.HostView();
|
||||
auto const& h_weighted_gpairs = weighted_gpairs.HostView();
|
||||
for (size_t i : {0ul, 1ul}) {
|
||||
ASSERT_FLOAT_EQ(h_weighted_gpairs[i].GetGrad(), h_gpairs[i].GetGrad() * 2.0f);
|
||||
ASSERT_FLOAT_EQ(h_weighted_gpairs[i].GetHess(), h_gpairs[i].GetHess() * 2.0f);
|
||||
ASSERT_FLOAT_EQ(h_weighted_gpairs(i).GetGrad(), h_gpairs(i).GetGrad() * 2.0f);
|
||||
ASSERT_FLOAT_EQ(h_weighted_gpairs(i).GetHess(), h_gpairs(i).GetHess() * 2.0f);
|
||||
}
|
||||
for (size_t i : {2ul, 3ul}) {
|
||||
ASSERT_FLOAT_EQ(h_weighted_gpairs[i].GetGrad(), h_gpairs[i].GetGrad() * 3.0f);
|
||||
ASSERT_FLOAT_EQ(h_weighted_gpairs[i].GetHess(), h_gpairs[i].GetHess() * 3.0f);
|
||||
ASSERT_FLOAT_EQ(h_weighted_gpairs(i).GetGrad(), h_gpairs(i).GetGrad() * 3.0f);
|
||||
ASSERT_FLOAT_EQ(h_weighted_gpairs(i).GetHess(), h_gpairs(i).GetHess() * 3.0f);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,7 +125,7 @@ void TestUnbiasedNDCG(Context const* ctx) {
|
||||
std::sort(h_label.begin(), h_label.end(), std::greater<>{});
|
||||
HostDeviceVector<float> predt(p_fmat->Info().num_row_, 1.0f);
|
||||
|
||||
HostDeviceVector<GradientPair> out_gpair;
|
||||
linalg::Matrix<GradientPair> out_gpair;
|
||||
obj->GetGradient(predt, p_fmat->Info(), 0, &out_gpair);
|
||||
|
||||
Json config{Object{}};
|
||||
|
||||
@@ -42,20 +42,21 @@ void TestGPUMakePair() {
|
||||
auto d = dummy.View(ctx.gpu_id);
|
||||
linalg::Vector<GradientPair> dgpair;
|
||||
auto dg = dgpair.View(ctx.gpu_id);
|
||||
cuda_impl::KernelInputs args{d,
|
||||
d,
|
||||
d,
|
||||
d,
|
||||
p_cache->DataGroupPtr(&ctx),
|
||||
p_cache->CUDAThreadsGroupPtr(),
|
||||
rank_idx,
|
||||
info.labels.View(ctx.gpu_id),
|
||||
predt.ConstDeviceSpan(),
|
||||
{},
|
||||
dg,
|
||||
nullptr,
|
||||
y_sorted_idx,
|
||||
0};
|
||||
cuda_impl::KernelInputs args{
|
||||
d,
|
||||
d,
|
||||
d,
|
||||
d,
|
||||
p_cache->DataGroupPtr(&ctx),
|
||||
p_cache->CUDAThreadsGroupPtr(),
|
||||
rank_idx,
|
||||
info.labels.View(ctx.gpu_id),
|
||||
predt.ConstDeviceSpan(),
|
||||
linalg::MatrixView<GradientPair>{common::Span<GradientPair>{}, {0}, 0},
|
||||
dg,
|
||||
nullptr,
|
||||
y_sorted_idx,
|
||||
0};
|
||||
return args;
|
||||
};
|
||||
|
||||
|
||||
@@ -122,8 +122,8 @@ TEST(Objective, DeclareUnifiedTest(LogisticRegressionBasic)) {
|
||||
EXPECT_NEAR(obj->ProbToMargin(0.1f), -2.197f, 0.01f);
|
||||
EXPECT_NEAR(obj->ProbToMargin(0.5f), 0, 0.01f);
|
||||
EXPECT_NEAR(obj->ProbToMargin(0.9f), 2.197f, 0.01f);
|
||||
EXPECT_ANY_THROW(obj->ProbToMargin(10))
|
||||
<< "Expected error when base_score not in range [0,1f] for LogisticRegression";
|
||||
EXPECT_ANY_THROW((void)obj->ProbToMargin(10))
|
||||
<< "Expected error when base_score not in range [0,1f] for LogisticRegression";
|
||||
|
||||
// test PredTransform
|
||||
HostDeviceVector<bst_float> io_preds = {0, 0.1f, 0.5f, 0.9f, 1};
|
||||
@@ -282,9 +282,9 @@ TEST(Objective, DeclareUnifiedTest(TweedieRegressionGPair)) {
|
||||
TEST(Objective, CPU_vs_CUDA) {
|
||||
Context ctx = MakeCUDACtx(GPUIDX);
|
||||
|
||||
ObjFunction* obj = ObjFunction::Create("reg:squarederror", &ctx);
|
||||
HostDeviceVector<GradientPair> cpu_out_preds;
|
||||
HostDeviceVector<GradientPair> cuda_out_preds;
|
||||
std::unique_ptr<ObjFunction> obj{ObjFunction::Create("reg:squarederror", &ctx)};
|
||||
linalg::Matrix<GradientPair> cpu_out_preds;
|
||||
linalg::Matrix<GradientPair> cuda_out_preds;
|
||||
|
||||
constexpr size_t kRows = 400;
|
||||
constexpr size_t kCols = 100;
|
||||
@@ -300,7 +300,7 @@ TEST(Objective, CPU_vs_CUDA) {
|
||||
info.labels.Reshape(kRows);
|
||||
auto& h_labels = info.labels.Data()->HostVector();
|
||||
for (size_t i = 0; i < h_labels.size(); ++i) {
|
||||
h_labels[i] = 1 / (float)(i+1);
|
||||
h_labels[i] = 1 / static_cast<float>(i+1);
|
||||
}
|
||||
|
||||
{
|
||||
@@ -314,19 +314,17 @@ TEST(Objective, CPU_vs_CUDA) {
|
||||
obj->GetGradient(preds, info, 0, &cuda_out_preds);
|
||||
}
|
||||
|
||||
auto& h_cpu_out = cpu_out_preds.HostVector();
|
||||
auto& h_cuda_out = cuda_out_preds.HostVector();
|
||||
auto h_cpu_out = cpu_out_preds.HostView();
|
||||
auto h_cuda_out = cuda_out_preds.HostView();
|
||||
|
||||
float sgrad = 0;
|
||||
float shess = 0;
|
||||
for (size_t i = 0; i < kRows; ++i) {
|
||||
sgrad += std::pow(h_cpu_out[i].GetGrad() - h_cuda_out[i].GetGrad(), 2);
|
||||
shess += std::pow(h_cpu_out[i].GetHess() - h_cuda_out[i].GetHess(), 2);
|
||||
sgrad += std::pow(h_cpu_out(i).GetGrad() - h_cuda_out(i).GetGrad(), 2);
|
||||
shess += std::pow(h_cpu_out(i).GetHess() - h_cuda_out(i).GetHess(), 2);
|
||||
}
|
||||
ASSERT_NEAR(sgrad, 0.0f, kRtEps);
|
||||
ASSERT_NEAR(shess, 0.0f, kRtEps);
|
||||
|
||||
delete obj;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -189,11 +189,10 @@ void TestUpdatePredictionCache(bool use_subsampling) {
|
||||
|
||||
auto dmat = RandomDataGenerator(kRows, kCols, 0).GenerateDMatrix(true, true, kClasses);
|
||||
|
||||
HostDeviceVector<GradientPair> gpair;
|
||||
auto& h_gpair = gpair.HostVector();
|
||||
h_gpair.resize(kRows * kClasses);
|
||||
linalg::Matrix<GradientPair> gpair({kRows, kClasses}, ctx.Device());
|
||||
auto h_gpair = gpair.HostView();
|
||||
for (size_t i = 0; i < kRows * kClasses; ++i) {
|
||||
h_gpair[i] = {static_cast<float>(i), 1};
|
||||
std::apply(h_gpair, linalg::UnravelIndex(i, kRows, kClasses)) = {static_cast<float>(i), 1};
|
||||
}
|
||||
|
||||
PredictionCacheEntry predtion_cache;
|
||||
|
||||
@@ -68,10 +68,12 @@ class TestL1MultiTarget : public ::testing::Test {
|
||||
}
|
||||
}
|
||||
|
||||
void RunTest(std::string const& tree_method, bool weight) {
|
||||
void RunTest(Context const* ctx, std::string const& tree_method, bool weight) {
|
||||
auto p_fmat = weight ? Xyw_ : Xy_;
|
||||
std::unique_ptr<Learner> learner{Learner::Create({p_fmat})};
|
||||
learner->SetParams(Args{{"tree_method", tree_method}, {"objective", "reg:absoluteerror"}});
|
||||
learner->SetParams(Args{{"tree_method", tree_method},
|
||||
{"objective", "reg:absoluteerror"},
|
||||
{"device", ctx->DeviceName()}});
|
||||
learner->Configure();
|
||||
for (auto i = 0; i < 4; ++i) {
|
||||
learner->UpdateOneIter(i, p_fmat);
|
||||
@@ -87,7 +89,9 @@ class TestL1MultiTarget : public ::testing::Test {
|
||||
for (bst_target_t t{0}; t < p_fmat->Info().labels.Shape(1); ++t) {
|
||||
auto t_Xy = weight ? single_w_[t] : single_[t];
|
||||
std::unique_ptr<Learner> sl{Learner::Create({t_Xy})};
|
||||
sl->SetParams(Args{{"tree_method", tree_method}, {"objective", "reg:absoluteerror"}});
|
||||
sl->SetParams(Args{{"tree_method", tree_method},
|
||||
{"objective", "reg:absoluteerror"},
|
||||
{"device", ctx->DeviceName()}});
|
||||
sl->Configure();
|
||||
sl->UpdateOneIter(0, t_Xy);
|
||||
Json s_config{Object{}};
|
||||
@@ -104,20 +108,32 @@ class TestL1MultiTarget : public ::testing::Test {
|
||||
ASSERT_FLOAT_EQ(mean, base_score);
|
||||
}
|
||||
|
||||
void RunTest(std::string const& tree_method) {
|
||||
this->RunTest(tree_method, false);
|
||||
this->RunTest(tree_method, true);
|
||||
void RunTest(Context const* ctx, std::string const& tree_method) {
|
||||
this->RunTest(ctx, tree_method, false);
|
||||
this->RunTest(ctx, tree_method, true);
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(TestL1MultiTarget, Hist) { this->RunTest("hist"); }
|
||||
TEST_F(TestL1MultiTarget, Hist) {
|
||||
Context ctx;
|
||||
this->RunTest(&ctx, "hist");
|
||||
}
|
||||
|
||||
TEST_F(TestL1MultiTarget, Exact) { this->RunTest("exact"); }
|
||||
TEST_F(TestL1MultiTarget, Exact) {
|
||||
Context ctx;
|
||||
this->RunTest(&ctx, "exact");
|
||||
}
|
||||
|
||||
TEST_F(TestL1MultiTarget, Approx) { this->RunTest("approx"); }
|
||||
TEST_F(TestL1MultiTarget, Approx) {
|
||||
Context ctx;
|
||||
this->RunTest(&ctx, "approx");
|
||||
}
|
||||
|
||||
#if defined(XGBOOST_USE_CUDA)
|
||||
TEST_F(TestL1MultiTarget, GpuHist) { this->RunTest("gpu_hist"); }
|
||||
TEST_F(TestL1MultiTarget, GpuHist) {
|
||||
auto ctx = MakeCUDACtx(0);
|
||||
this->RunTest(&ctx, "hist");
|
||||
}
|
||||
#endif // defined(XGBOOST_USE_CUDA)
|
||||
|
||||
TEST(MultiStrategy, Configure) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Copyright 2022 by XGBoost Contributors
|
||||
* Copyright 2022-2023, XGBoost Contributors
|
||||
*/
|
||||
#include <gtest/gtest.h>
|
||||
#include <xgboost/linalg.h>
|
||||
@@ -8,17 +8,17 @@
|
||||
#include "../../src/tree/fit_stump.h"
|
||||
#include "../helpers.h"
|
||||
|
||||
namespace xgboost {
|
||||
namespace tree {
|
||||
namespace xgboost::tree {
|
||||
namespace {
|
||||
void TestFitStump(Context const *ctx, DataSplitMode split = DataSplitMode::kRow) {
|
||||
std::size_t constexpr kRows = 16, kTargets = 2;
|
||||
HostDeviceVector<GradientPair> gpair;
|
||||
auto &h_gpair = gpair.HostVector();
|
||||
h_gpair.resize(kRows * kTargets);
|
||||
linalg::Matrix<GradientPair> gpair;
|
||||
gpair.SetDevice(ctx->Device());
|
||||
gpair.Reshape(kRows, kTargets);
|
||||
auto h_gpair = gpair.HostView();
|
||||
for (std::size_t i = 0; i < kRows; ++i) {
|
||||
for (std::size_t t = 0; t < kTargets; ++t) {
|
||||
h_gpair.at(i * kTargets + t) = GradientPair{static_cast<float>(i), 1};
|
||||
h_gpair(i, t) = GradientPair{static_cast<float>(i), 1};
|
||||
}
|
||||
}
|
||||
linalg::Vector<float> out;
|
||||
@@ -53,6 +53,4 @@ TEST(InitEstimation, FitStumpColumnSplit) {
|
||||
auto constexpr kWorldSize{3};
|
||||
RunWithInMemoryCommunicator(kWorldSize, &TestFitStump, &ctx, DataSplitMode::kCol);
|
||||
}
|
||||
|
||||
} // namespace tree
|
||||
} // namespace xgboost
|
||||
} // namespace xgboost::tree
|
||||
|
||||
@@ -214,7 +214,7 @@ TEST(GpuHist, TestHistogramIndex) {
|
||||
TestHistogramIndexImpl();
|
||||
}
|
||||
|
||||
void UpdateTree(Context const* ctx, HostDeviceVector<GradientPair>* gpair, DMatrix* dmat,
|
||||
void UpdateTree(Context const* ctx, linalg::Matrix<GradientPair>* gpair, DMatrix* dmat,
|
||||
size_t gpu_page_size, RegTree* tree, HostDeviceVector<bst_float>* preds,
|
||||
float subsample = 1.0f, const std::string& sampling_method = "uniform",
|
||||
int max_bin = 2) {
|
||||
@@ -264,7 +264,8 @@ TEST(GpuHist, UniformSampling) {
|
||||
// Create an in-memory DMatrix.
|
||||
std::unique_ptr<DMatrix> dmat(CreateSparsePageDMatrixWithRC(kRows, kCols, 0, true));
|
||||
|
||||
auto gpair = GenerateRandomGradients(kRows);
|
||||
linalg::Matrix<GradientPair> gpair({kRows}, Context{}.MakeCUDA().Ordinal());
|
||||
gpair.Data()->Copy(GenerateRandomGradients(kRows));
|
||||
|
||||
// Build a tree using the in-memory DMatrix.
|
||||
RegTree tree;
|
||||
@@ -294,7 +295,8 @@ TEST(GpuHist, GradientBasedSampling) {
|
||||
// Create an in-memory DMatrix.
|
||||
std::unique_ptr<DMatrix> dmat(CreateSparsePageDMatrixWithRC(kRows, kCols, 0, true));
|
||||
|
||||
auto gpair = GenerateRandomGradients(kRows);
|
||||
linalg::Matrix<GradientPair> gpair({kRows}, MakeCUDACtx(0).Ordinal());
|
||||
gpair.Data()->Copy(GenerateRandomGradients(kRows));
|
||||
|
||||
// Build a tree using the in-memory DMatrix.
|
||||
RegTree tree;
|
||||
@@ -330,11 +332,12 @@ TEST(GpuHist, ExternalMemory) {
|
||||
// Create a single batch DMatrix.
|
||||
std::unique_ptr<DMatrix> dmat(CreateSparsePageDMatrix(kRows, kCols, 1, tmpdir.path + "/cache"));
|
||||
|
||||
auto gpair = GenerateRandomGradients(kRows);
|
||||
Context ctx(MakeCUDACtx(0));
|
||||
linalg::Matrix<GradientPair> gpair({kRows}, ctx.Ordinal());
|
||||
gpair.Data()->Copy(GenerateRandomGradients(kRows));
|
||||
|
||||
// Build a tree using the in-memory DMatrix.
|
||||
RegTree tree;
|
||||
Context ctx(MakeCUDACtx(0));
|
||||
HostDeviceVector<bst_float> preds(kRows, 0.0, 0);
|
||||
UpdateTree(&ctx, &gpair, dmat.get(), 0, &tree, &preds, 1.0, "uniform", kRows);
|
||||
// Build another tree using multiple ELLPACK pages.
|
||||
@@ -367,12 +370,13 @@ TEST(GpuHist, ExternalMemoryWithSampling) {
|
||||
std::unique_ptr<DMatrix> dmat_ext(
|
||||
CreateSparsePageDMatrix(kRows, kCols, kRows / kPageSize, tmpdir.path + "/cache"));
|
||||
|
||||
auto gpair = GenerateRandomGradients(kRows);
|
||||
Context ctx(MakeCUDACtx(0));
|
||||
linalg::Matrix<GradientPair> gpair({kRows}, ctx.Ordinal());
|
||||
gpair.Data()->Copy(GenerateRandomGradients(kRows));
|
||||
|
||||
// Build a tree using the in-memory DMatrix.
|
||||
auto rng = common::GlobalRandom();
|
||||
|
||||
Context ctx(MakeCUDACtx(0));
|
||||
RegTree tree;
|
||||
HostDeviceVector<bst_float> preds(kRows, 0.0, 0);
|
||||
UpdateTree(&ctx, &gpair, dmat.get(), 0, &tree, &preds, kSubsample, kSamplingMethod, kRows);
|
||||
|
||||
@@ -26,9 +26,11 @@ TEST(GrowHistMaker, InteractionConstraint) {
|
||||
auto constexpr kRows = 32;
|
||||
auto constexpr kCols = 16;
|
||||
auto p_dmat = GenerateDMatrix(kRows, kCols);
|
||||
auto p_gradients = GenerateGradients(kRows);
|
||||
|
||||
Context ctx;
|
||||
|
||||
linalg::Matrix<GradientPair> gpair({kRows}, ctx.Ordinal());
|
||||
gpair.Data()->Copy(GenerateRandomGradients(kRows));
|
||||
|
||||
ObjInfo task{ObjInfo::kRegression};
|
||||
{
|
||||
// With constraints
|
||||
@@ -40,7 +42,7 @@ TEST(GrowHistMaker, InteractionConstraint) {
|
||||
Args{{"interaction_constraints", "[[0, 1]]"}, {"num_feature", std::to_string(kCols)}});
|
||||
std::vector<HostDeviceVector<bst_node_t>> position(1);
|
||||
updater->Configure(Args{});
|
||||
updater->Update(¶m, p_gradients.get(), p_dmat.get(), position, {&tree});
|
||||
updater->Update(¶m, &gpair, p_dmat.get(), position, {&tree});
|
||||
|
||||
ASSERT_EQ(tree.NumExtraNodes(), 4);
|
||||
ASSERT_EQ(tree[0].SplitIndex(), 1);
|
||||
@@ -57,7 +59,7 @@ TEST(GrowHistMaker, InteractionConstraint) {
|
||||
TrainParam param;
|
||||
param.Init(Args{});
|
||||
updater->Configure(Args{});
|
||||
updater->Update(¶m, p_gradients.get(), p_dmat.get(), position, {&tree});
|
||||
updater->Update(¶m, &gpair, p_dmat.get(), position, {&tree});
|
||||
|
||||
ASSERT_EQ(tree.NumExtraNodes(), 10);
|
||||
ASSERT_EQ(tree[0].SplitIndex(), 1);
|
||||
@@ -70,9 +72,12 @@ TEST(GrowHistMaker, InteractionConstraint) {
|
||||
namespace {
|
||||
void VerifyColumnSplit(int32_t rows, bst_feature_t cols, bool categorical,
|
||||
RegTree const& expected_tree) {
|
||||
auto p_dmat = GenerateDMatrix(rows, cols, categorical);
|
||||
auto p_gradients = GenerateGradients(rows);
|
||||
Context ctx;
|
||||
auto p_dmat = GenerateDMatrix(rows, cols, categorical);
|
||||
linalg::Matrix<GradientPair> gpair({rows}, ctx.Ordinal());
|
||||
gpair.Data()->Copy(GenerateRandomGradients(rows));
|
||||
|
||||
|
||||
ObjInfo task{ObjInfo::kRegression};
|
||||
std::unique_ptr<TreeUpdater> updater{TreeUpdater::Create("grow_histmaker", &ctx, &task)};
|
||||
std::vector<HostDeviceVector<bst_node_t>> position(1);
|
||||
@@ -84,7 +89,7 @@ void VerifyColumnSplit(int32_t rows, bst_feature_t cols, bool categorical,
|
||||
TrainParam param;
|
||||
param.Init(Args{});
|
||||
updater->Configure(Args{});
|
||||
updater->Update(¶m, p_gradients.get(), sliced.get(), position, {&tree});
|
||||
updater->Update(¶m, &gpair, sliced.get(), position, {&tree});
|
||||
|
||||
Json json{Object{}};
|
||||
tree.SaveModel(&json);
|
||||
@@ -100,15 +105,16 @@ void TestColumnSplit(bool categorical) {
|
||||
RegTree expected_tree{1u, kCols};
|
||||
ObjInfo task{ObjInfo::kRegression};
|
||||
{
|
||||
auto p_dmat = GenerateDMatrix(kRows, kCols, categorical);
|
||||
auto p_gradients = GenerateGradients(kRows);
|
||||
Context ctx;
|
||||
auto p_dmat = GenerateDMatrix(kRows, kCols, categorical);
|
||||
linalg::Matrix<GradientPair> gpair({kRows}, ctx.Ordinal());
|
||||
gpair.Data()->Copy(GenerateRandomGradients(kRows));
|
||||
std::unique_ptr<TreeUpdater> updater{TreeUpdater::Create("grow_histmaker", &ctx, &task)};
|
||||
std::vector<HostDeviceVector<bst_node_t>> position(1);
|
||||
TrainParam param;
|
||||
param.Init(Args{});
|
||||
updater->Configure(Args{});
|
||||
updater->Update(¶m, p_gradients.get(), p_dmat.get(), position, {&expected_tree});
|
||||
updater->Update(¶m, &gpair, p_dmat.get(), position, {&expected_tree});
|
||||
}
|
||||
|
||||
auto constexpr kWorldSize = 2;
|
||||
|
||||
@@ -69,7 +69,7 @@ class TestPredictionCache : public ::testing::Test {
|
||||
std::unique_ptr<TreeUpdater> updater{TreeUpdater::Create(updater_name, ctx, &task)};
|
||||
RegTree tree;
|
||||
std::vector<RegTree*> trees{&tree};
|
||||
auto gpair = GenerateRandomGradients(n_samples_);
|
||||
auto gpair = GenerateRandomGradients(ctx, n_samples_, 1);
|
||||
tree::TrainParam param;
|
||||
param.UpdateAllowUnknown(Args{{"max_bin", "64"}});
|
||||
|
||||
|
||||
@@ -21,15 +21,13 @@ TEST(Updater, Prune) {
|
||||
std::vector<std::pair<std::string, std::string>> cfg;
|
||||
cfg.emplace_back("num_feature", std::to_string(kCols));
|
||||
cfg.emplace_back("min_split_loss", "10");
|
||||
Context ctx;
|
||||
|
||||
// These data are just place holders.
|
||||
HostDeviceVector<GradientPair> gpair =
|
||||
{ {0.50f, 0.25f}, {0.50f, 0.25f}, {0.50f, 0.25f}, {0.50f, 0.25f},
|
||||
{0.25f, 0.24f}, {0.25f, 0.24f}, {0.25f, 0.24f}, {0.25f, 0.24f} };
|
||||
std::shared_ptr<DMatrix> p_dmat {
|
||||
RandomDataGenerator{32, 10, 0}.GenerateDMatrix() };
|
||||
|
||||
Context ctx;
|
||||
linalg::Matrix<GradientPair> gpair
|
||||
{{ {0.50f, 0.25f}, {0.50f, 0.25f}, {0.50f, 0.25f}, {0.50f, 0.25f},
|
||||
{0.25f, 0.24f}, {0.25f, 0.24f}, {0.25f, 0.24f}, {0.25f, 0.24f} }, {8, 1}, ctx.Device()};
|
||||
std::shared_ptr<DMatrix> p_dmat{RandomDataGenerator{32, 10, 0}.GenerateDMatrix()};
|
||||
|
||||
// prepare tree
|
||||
RegTree tree = RegTree{1u, kCols};
|
||||
|
||||
@@ -202,13 +202,13 @@ TEST(QuantileHist, PartitionerColSplit) { TestColumnSplitPartitioner<CPUExpandEn
|
||||
TEST(QuantileHist, MultiPartitionerColSplit) { TestColumnSplitPartitioner<MultiExpandEntry>(3); }
|
||||
|
||||
namespace {
|
||||
void VerifyColumnSplit(bst_row_t rows, bst_feature_t cols, bst_target_t n_targets,
|
||||
void VerifyColumnSplit(Context const* ctx, bst_row_t rows, bst_feature_t cols, bst_target_t n_targets,
|
||||
RegTree const& expected_tree) {
|
||||
auto Xy = RandomDataGenerator{rows, cols, 0}.GenerateDMatrix(true);
|
||||
auto p_gradients = GenerateGradients(rows, n_targets);
|
||||
Context ctx;
|
||||
linalg::Matrix<GradientPair> gpair = GenerateRandomGradients(ctx, rows, n_targets);
|
||||
|
||||
ObjInfo task{ObjInfo::kRegression};
|
||||
std::unique_ptr<TreeUpdater> updater{TreeUpdater::Create("grow_quantile_histmaker", &ctx, &task)};
|
||||
std::unique_ptr<TreeUpdater> updater{TreeUpdater::Create("grow_quantile_histmaker", ctx, &task)};
|
||||
std::vector<HostDeviceVector<bst_node_t>> position(1);
|
||||
|
||||
std::unique_ptr<DMatrix> sliced{Xy->SliceCol(collective::GetWorldSize(), collective::GetRank())};
|
||||
@@ -217,7 +217,7 @@ void VerifyColumnSplit(bst_row_t rows, bst_feature_t cols, bst_target_t n_target
|
||||
TrainParam param;
|
||||
param.Init(Args{});
|
||||
updater->Configure(Args{});
|
||||
updater->Update(¶m, p_gradients.get(), sliced.get(), position, {&tree});
|
||||
updater->Update(¶m, &gpair, sliced.get(), position, {&tree});
|
||||
|
||||
Json json{Object{}};
|
||||
tree.SaveModel(&json);
|
||||
@@ -232,21 +232,21 @@ void TestColumnSplit(bst_target_t n_targets) {
|
||||
|
||||
RegTree expected_tree{n_targets, kCols};
|
||||
ObjInfo task{ObjInfo::kRegression};
|
||||
Context ctx;
|
||||
{
|
||||
auto Xy = RandomDataGenerator{kRows, kCols, 0}.GenerateDMatrix(true);
|
||||
auto p_gradients = GenerateGradients(kRows, n_targets);
|
||||
Context ctx;
|
||||
auto gpair = GenerateRandomGradients(&ctx, kRows, n_targets);
|
||||
std::unique_ptr<TreeUpdater> updater{
|
||||
TreeUpdater::Create("grow_quantile_histmaker", &ctx, &task)};
|
||||
std::vector<HostDeviceVector<bst_node_t>> position(1);
|
||||
TrainParam param;
|
||||
param.Init(Args{});
|
||||
updater->Configure(Args{});
|
||||
updater->Update(¶m, p_gradients.get(), Xy.get(), position, {&expected_tree});
|
||||
updater->Update(¶m, &gpair, Xy.get(), position, {&expected_tree});
|
||||
}
|
||||
|
||||
auto constexpr kWorldSize = 2;
|
||||
RunWithInMemoryCommunicator(kWorldSize, VerifyColumnSplit, kRows, kCols, n_targets,
|
||||
RunWithInMemoryCommunicator(kWorldSize, VerifyColumnSplit, &ctx, kRows, kCols, n_targets,
|
||||
std::cref(expected_tree));
|
||||
}
|
||||
} // anonymous namespace
|
||||
|
||||
@@ -17,10 +17,11 @@ namespace xgboost::tree {
|
||||
TEST(Updater, Refresh) {
|
||||
bst_row_t constexpr kRows = 8;
|
||||
bst_feature_t constexpr kCols = 16;
|
||||
Context ctx;
|
||||
|
||||
HostDeviceVector<GradientPair> gpair =
|
||||
{ {0.23f, 0.24f}, {0.23f, 0.24f}, {0.23f, 0.24f}, {0.23f, 0.24f},
|
||||
{0.27f, 0.29f}, {0.27f, 0.29f}, {0.27f, 0.29f}, {0.27f, 0.29f} };
|
||||
linalg::Matrix<GradientPair> gpair
|
||||
{{ {0.23f, 0.24f}, {0.23f, 0.24f}, {0.23f, 0.24f}, {0.23f, 0.24f},
|
||||
{0.27f, 0.29f}, {0.27f, 0.29f}, {0.27f, 0.29f}, {0.27f, 0.29f} }, {8, 1}, ctx.Device()};
|
||||
std::shared_ptr<DMatrix> p_dmat{
|
||||
RandomDataGenerator{kRows, kCols, 0.4f}.Seed(3).GenerateDMatrix()};
|
||||
std::vector<std::pair<std::string, std::string>> cfg{
|
||||
@@ -29,7 +30,6 @@ TEST(Updater, Refresh) {
|
||||
{"reg_lambda", "1"}};
|
||||
|
||||
RegTree tree = RegTree{1u, kCols};
|
||||
Context ctx;
|
||||
std::vector<RegTree*> trees{&tree};
|
||||
|
||||
ObjInfo task{ObjInfo::kRegression};
|
||||
|
||||
@@ -16,7 +16,7 @@ namespace xgboost {
|
||||
class UpdaterTreeStatTest : public ::testing::Test {
|
||||
protected:
|
||||
std::shared_ptr<DMatrix> p_dmat_;
|
||||
HostDeviceVector<GradientPair> gpairs_;
|
||||
linalg::Matrix<GradientPair> gpairs_;
|
||||
size_t constexpr static kRows = 10;
|
||||
size_t constexpr static kCols = 10;
|
||||
|
||||
@@ -24,8 +24,8 @@ class UpdaterTreeStatTest : public ::testing::Test {
|
||||
void SetUp() override {
|
||||
p_dmat_ = RandomDataGenerator(kRows, kCols, .5f).GenerateDMatrix(true);
|
||||
auto g = GenerateRandomGradients(kRows);
|
||||
gpairs_.Resize(kRows);
|
||||
gpairs_.Copy(g);
|
||||
gpairs_.Reshape(kRows, 1);
|
||||
gpairs_.Data()->Copy(g);
|
||||
}
|
||||
|
||||
void RunTest(std::string updater) {
|
||||
@@ -63,7 +63,7 @@ TEST_F(UpdaterTreeStatTest, Approx) { this->RunTest("grow_histmaker"); }
|
||||
class UpdaterEtaTest : public ::testing::Test {
|
||||
protected:
|
||||
std::shared_ptr<DMatrix> p_dmat_;
|
||||
HostDeviceVector<GradientPair> gpairs_;
|
||||
linalg::Matrix<GradientPair> gpairs_;
|
||||
size_t constexpr static kRows = 10;
|
||||
size_t constexpr static kCols = 10;
|
||||
size_t constexpr static kClasses = 10;
|
||||
@@ -71,8 +71,8 @@ class UpdaterEtaTest : public ::testing::Test {
|
||||
void SetUp() override {
|
||||
p_dmat_ = RandomDataGenerator(kRows, kCols, .5f).GenerateDMatrix(true, false, kClasses);
|
||||
auto g = GenerateRandomGradients(kRows);
|
||||
gpairs_.Resize(kRows);
|
||||
gpairs_.Copy(g);
|
||||
gpairs_.Reshape(kRows, 1);
|
||||
gpairs_.Data()->Copy(g);
|
||||
}
|
||||
|
||||
void RunTest(std::string updater) {
|
||||
@@ -125,14 +125,15 @@ TEST_F(UpdaterEtaTest, GpuHist) { this->RunTest("grow_gpu_hist"); }
|
||||
|
||||
class TestMinSplitLoss : public ::testing::Test {
|
||||
std::shared_ptr<DMatrix> dmat_;
|
||||
HostDeviceVector<GradientPair> gpair_;
|
||||
linalg::Matrix<GradientPair> gpair_;
|
||||
|
||||
void SetUp() override {
|
||||
constexpr size_t kRows = 32;
|
||||
constexpr size_t kCols = 16;
|
||||
constexpr float kSparsity = 0.6;
|
||||
dmat_ = RandomDataGenerator(kRows, kCols, kSparsity).Seed(3).GenerateDMatrix();
|
||||
gpair_ = GenerateRandomGradients(kRows);
|
||||
gpair_.Reshape(kRows, 1);
|
||||
gpair_.Data()->Copy(GenerateRandomGradients(kRows));
|
||||
}
|
||||
|
||||
std::int32_t Update(Context const* ctx, std::string updater, float gamma) {
|
||||
|
||||
Reference in New Issue
Block a user