Implement unified update prediction cache for (gpu_)hist. (#6860)

* Implement utilites for linalg.
* Unify the update prediction cache functions.
* Implement update prediction cache for multi-class gpu hist.
This commit is contained in:
Jiaming Yuan
2021-04-17 00:29:34 +08:00
committed by GitHub
parent 1b26a2a561
commit 556a83022d
10 changed files with 246 additions and 68 deletions

View File

@@ -0,0 +1,38 @@
#include <gtest/gtest.h>
#include <xgboost/linalg.h>
#include <numeric>
namespace xgboost {
auto MakeMatrixFromTest(HostDeviceVector<float> *storage, size_t n_rows, size_t n_cols) {
storage->Resize(n_rows * n_cols);
auto& h_storage = storage->HostVector();
std::iota(h_storage.begin(), h_storage.end(), 0);
auto m = MatrixView<float>{storage, {n_cols, 1}, {n_rows, n_cols}, -1};
return m;
}
TEST(Linalg, Matrix) {
size_t kRows = 31, kCols = 77;
HostDeviceVector<float> storage;
auto m = MakeMatrixFromTest(&storage, kRows, kCols);
ASSERT_EQ(m.DeviceIdx(), GenericParameter::kCpuId);
ASSERT_EQ(m(0, 0), 0);
ASSERT_EQ(m(kRows - 1, kCols - 1), storage.Size() - 1);
}
TEST(Linalg, Vector) {
size_t kRows = 31, kCols = 77;
HostDeviceVector<float> storage;
auto m = MakeMatrixFromTest(&storage, kRows, kCols);
auto v = VectorView<float>(m, 3);
for (size_t i = 0; i < v.Size(); ++i) {
ASSERT_EQ(v[i], m(i, 3));
}
ASSERT_EQ(v[0], 3);
}
} // namespace xgboost

View File

@@ -294,6 +294,13 @@ TEST(Learner, GPUConfiguration) {
learner->UpdateOneIter(0, p_dmat);
ASSERT_EQ(learner->GetGenericParameter().gpu_id, 0);
}
{
std::unique_ptr<Learner> learner {Learner::Create(mat)};
learner->SetParams({Arg{"tree_method", "gpu_hist"},
Arg{"gpu_id", "-1"}});
learner->UpdateOneIter(0, p_dmat);
ASSERT_EQ(learner->GetGenericParameter().gpu_id, 0);
}
{
// with CPU algorithm
std::unique_ptr<Learner> learner {Learner::Create(mat)};

View File

@@ -390,7 +390,10 @@ void UpdateTree(HostDeviceVector<GradientPair>* gpair, DMatrix* dmat,
hist_maker.Configure(args, &generic_param);
hist_maker.Update(gpair, dmat, {tree});
hist_maker.UpdatePredictionCache(dmat, preds);
hist_maker.UpdatePredictionCache(
dmat,
VectorView<float>{
MatrixView<float>(preds, {preds->Size(), 1}, preds->DeviceIdx()), 0});
}
TEST(GpuHist, UniformSampling) {