Implement sketching with Hessian on GPU. (#9399)
- Prepare for implementing approx on GPU. - Unify the code path between weighted and uniform sketching on DMatrix.
This commit is contained in:
@@ -3,17 +3,22 @@
|
||||
*/
|
||||
#include <gtest/gtest.h>
|
||||
#include <thrust/device_vector.h>
|
||||
#include <xgboost/base.h> // for bst_bin_t
|
||||
#include <xgboost/c_api.h>
|
||||
#include <xgboost/data.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <algorithm> // for transform
|
||||
#include <cmath> // for floor
|
||||
#include <cstddef> // for size_t
|
||||
#include <limits> // for numeric_limits
|
||||
#include <string> // for string, to_string
|
||||
#include <tuple> // for tuple, make_tuple
|
||||
#include <vector> // for vector
|
||||
|
||||
#include "../../../include/xgboost/logging.h"
|
||||
#include "../../../src/common/device_helpers.cuh"
|
||||
#include "../../../src/common/hist_util.cuh"
|
||||
#include "../../../src/common/hist_util.h"
|
||||
#include "../../../src/common/math.h"
|
||||
#include "../../../src/data/device_adapter.cuh"
|
||||
#include "../../../src/data/simple_dmatrix.h"
|
||||
#include "../data/test_array_interface.h"
|
||||
@@ -21,8 +26,7 @@
|
||||
#include "../helpers.h"
|
||||
#include "test_hist_util.h"
|
||||
|
||||
namespace xgboost {
|
||||
namespace common {
|
||||
namespace xgboost::common {
|
||||
|
||||
template <typename AdapterT>
|
||||
HistogramCuts GetHostCuts(Context const* ctx, AdapterT* adapter, int num_bins, float missing) {
|
||||
@@ -32,16 +36,17 @@ HistogramCuts GetHostCuts(Context const* ctx, AdapterT* adapter, int num_bins, f
|
||||
}
|
||||
|
||||
TEST(HistUtil, DeviceSketch) {
|
||||
auto ctx = MakeCUDACtx(0);
|
||||
int num_columns = 1;
|
||||
int num_bins = 4;
|
||||
std::vector<float> x = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 7.0f, -1.0f};
|
||||
int num_rows = x.size();
|
||||
auto dmat = GetDMatrixFromData(x, num_rows, num_columns);
|
||||
|
||||
auto device_cuts = DeviceSketch(0, dmat.get(), num_bins);
|
||||
auto device_cuts = DeviceSketch(&ctx, dmat.get(), num_bins);
|
||||
|
||||
Context ctx;
|
||||
HistogramCuts host_cuts = SketchOnDMatrix(&ctx, dmat.get(), num_bins);
|
||||
Context cpu_ctx;
|
||||
HistogramCuts host_cuts = SketchOnDMatrix(&cpu_ctx, dmat.get(), num_bins);
|
||||
|
||||
EXPECT_EQ(device_cuts.Values(), host_cuts.Values());
|
||||
EXPECT_EQ(device_cuts.Ptrs(), host_cuts.Ptrs());
|
||||
@@ -65,6 +70,7 @@ TEST(HistUtil, SketchBatchNumElements) {
|
||||
}
|
||||
|
||||
TEST(HistUtil, DeviceSketchMemory) {
|
||||
auto ctx = MakeCUDACtx(0);
|
||||
int num_columns = 100;
|
||||
int num_rows = 1000;
|
||||
int num_bins = 256;
|
||||
@@ -73,7 +79,7 @@ TEST(HistUtil, DeviceSketchMemory) {
|
||||
|
||||
dh::GlobalMemoryLogger().Clear();
|
||||
ConsoleLogger::Configure({{"verbosity", "3"}});
|
||||
auto device_cuts = DeviceSketch(0, dmat.get(), num_bins);
|
||||
auto device_cuts = DeviceSketch(&ctx, dmat.get(), num_bins);
|
||||
|
||||
size_t bytes_required = detail::RequiredMemory(
|
||||
num_rows, num_columns, num_rows * num_columns, num_bins, false);
|
||||
@@ -83,6 +89,7 @@ TEST(HistUtil, DeviceSketchMemory) {
|
||||
}
|
||||
|
||||
TEST(HistUtil, DeviceSketchWeightsMemory) {
|
||||
auto ctx = MakeCUDACtx(0);
|
||||
int num_columns = 100;
|
||||
int num_rows = 1000;
|
||||
int num_bins = 256;
|
||||
@@ -92,7 +99,7 @@ TEST(HistUtil, DeviceSketchWeightsMemory) {
|
||||
|
||||
dh::GlobalMemoryLogger().Clear();
|
||||
ConsoleLogger::Configure({{"verbosity", "3"}});
|
||||
auto device_cuts = DeviceSketch(0, dmat.get(), num_bins);
|
||||
auto device_cuts = DeviceSketch(&ctx, dmat.get(), num_bins);
|
||||
ConsoleLogger::Configure({{"verbosity", "0"}});
|
||||
|
||||
size_t bytes_required = detail::RequiredMemory(
|
||||
@@ -102,43 +109,44 @@ TEST(HistUtil, DeviceSketchWeightsMemory) {
|
||||
}
|
||||
|
||||
TEST(HistUtil, DeviceSketchDeterminism) {
|
||||
auto ctx = MakeCUDACtx(0);
|
||||
int num_rows = 500;
|
||||
int num_columns = 5;
|
||||
int num_bins = 256;
|
||||
auto x = GenerateRandom(num_rows, num_columns);
|
||||
auto dmat = GetDMatrixFromData(x, num_rows, num_columns);
|
||||
auto reference_sketch = DeviceSketch(0, dmat.get(), num_bins);
|
||||
auto reference_sketch = DeviceSketch(&ctx, dmat.get(), num_bins);
|
||||
size_t constexpr kRounds{ 100 };
|
||||
for (size_t r = 0; r < kRounds; ++r) {
|
||||
auto new_sketch = DeviceSketch(0, dmat.get(), num_bins);
|
||||
auto new_sketch = DeviceSketch(&ctx, dmat.get(), num_bins);
|
||||
ASSERT_EQ(reference_sketch.Values(), new_sketch.Values());
|
||||
ASSERT_EQ(reference_sketch.MinValues(), new_sketch.MinValues());
|
||||
}
|
||||
}
|
||||
|
||||
TEST(HistUtil, DeviceSketchCategoricalAsNumeric) {
|
||||
int categorical_sizes[] = {2, 6, 8, 12};
|
||||
auto ctx = MakeCUDACtx(0);
|
||||
auto categorical_sizes = {2, 6, 8, 12};
|
||||
int num_bins = 256;
|
||||
int sizes[] = {25, 100, 1000};
|
||||
auto sizes = {25, 100, 1000};
|
||||
for (auto n : sizes) {
|
||||
for (auto num_categories : categorical_sizes) {
|
||||
auto x = GenerateRandomCategoricalSingleColumn(n, num_categories);
|
||||
auto dmat = GetDMatrixFromData(x, n, 1);
|
||||
auto cuts = DeviceSketch(0, dmat.get(), num_bins);
|
||||
auto cuts = DeviceSketch(&ctx, dmat.get(), num_bins);
|
||||
ValidateCuts(cuts, dmat.get(), num_bins);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(HistUtil, DeviceSketchCategoricalFeatures) {
|
||||
TestCategoricalSketch(1000, 256, 32, false,
|
||||
[](DMatrix *p_fmat, int32_t num_bins) {
|
||||
return DeviceSketch(0, p_fmat, num_bins);
|
||||
});
|
||||
TestCategoricalSketch(1000, 256, 32, true,
|
||||
[](DMatrix *p_fmat, int32_t num_bins) {
|
||||
return DeviceSketch(0, p_fmat, num_bins);
|
||||
});
|
||||
auto ctx = MakeCUDACtx(0);
|
||||
TestCategoricalSketch(1000, 256, 32, false, [ctx](DMatrix* p_fmat, int32_t num_bins) {
|
||||
return DeviceSketch(&ctx, p_fmat, num_bins);
|
||||
});
|
||||
TestCategoricalSketch(1000, 256, 32, true, [ctx](DMatrix* p_fmat, int32_t num_bins) {
|
||||
return DeviceSketch(&ctx, p_fmat, num_bins);
|
||||
});
|
||||
}
|
||||
|
||||
void TestMixedSketch() {
|
||||
@@ -162,7 +170,8 @@ void TestMixedSketch() {
|
||||
m->Info().feature_types.HostVector().push_back(FeatureType::kCategorical);
|
||||
m->Info().feature_types.HostVector().push_back(FeatureType::kNumerical);
|
||||
|
||||
auto cuts = DeviceSketch(0, m.get(), n_bins);
|
||||
auto ctx = MakeCUDACtx(0);
|
||||
auto cuts = DeviceSketch(&ctx, m.get(), n_bins);
|
||||
ASSERT_EQ(cuts.Values().size(), n_bins + n_categories);
|
||||
}
|
||||
|
||||
@@ -234,37 +243,40 @@ TEST(HistUtil, RemoveDuplicatedCategories) {
|
||||
}
|
||||
|
||||
TEST(HistUtil, DeviceSketchMultipleColumns) {
|
||||
int bin_sizes[] = {2, 16, 256, 512};
|
||||
int sizes[] = {100, 1000, 1500};
|
||||
auto ctx = MakeCUDACtx(0);
|
||||
auto bin_sizes = {2, 16, 256, 512};
|
||||
auto sizes = {100, 1000, 1500};
|
||||
int num_columns = 5;
|
||||
for (auto num_rows : sizes) {
|
||||
auto x = GenerateRandom(num_rows, num_columns);
|
||||
auto dmat = GetDMatrixFromData(x, num_rows, num_columns);
|
||||
for (auto num_bins : bin_sizes) {
|
||||
auto cuts = DeviceSketch(0, dmat.get(), num_bins);
|
||||
auto cuts = DeviceSketch(&ctx, dmat.get(), num_bins);
|
||||
ValidateCuts(cuts, dmat.get(), num_bins);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(HistUtil, DeviceSketchMultipleColumnsWeights) {
|
||||
int bin_sizes[] = {2, 16, 256, 512};
|
||||
int sizes[] = {100, 1000, 1500};
|
||||
auto ctx = MakeCUDACtx(0);
|
||||
auto bin_sizes = {2, 16, 256, 512};
|
||||
auto sizes = {100, 1000, 1500};
|
||||
int num_columns = 5;
|
||||
for (auto num_rows : sizes) {
|
||||
auto x = GenerateRandom(num_rows, num_columns);
|
||||
auto dmat = GetDMatrixFromData(x, num_rows, num_columns);
|
||||
dmat->Info().weights_.HostVector() = GenerateRandomWeights(num_rows);
|
||||
for (auto num_bins : bin_sizes) {
|
||||
auto cuts = DeviceSketch(0, dmat.get(), num_bins);
|
||||
auto cuts = DeviceSketch(&ctx, dmat.get(), num_bins);
|
||||
ValidateCuts(cuts, dmat.get(), num_bins);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(HistUitl, DeviceSketchWeights) {
|
||||
int bin_sizes[] = {2, 16, 256, 512};
|
||||
int sizes[] = {100, 1000, 1500};
|
||||
auto ctx = MakeCUDACtx(0);
|
||||
auto bin_sizes = {2, 16, 256, 512};
|
||||
auto sizes = {100, 1000, 1500};
|
||||
int num_columns = 5;
|
||||
for (auto num_rows : sizes) {
|
||||
auto x = GenerateRandom(num_rows, num_columns);
|
||||
@@ -274,8 +286,8 @@ TEST(HistUitl, DeviceSketchWeights) {
|
||||
h_weights.resize(num_rows);
|
||||
std::fill(h_weights.begin(), h_weights.end(), 1.0f);
|
||||
for (auto num_bins : bin_sizes) {
|
||||
auto cuts = DeviceSketch(0, dmat.get(), num_bins);
|
||||
auto wcuts = DeviceSketch(0, weighted_dmat.get(), num_bins);
|
||||
auto cuts = DeviceSketch(&ctx, dmat.get(), num_bins);
|
||||
auto wcuts = DeviceSketch(&ctx, weighted_dmat.get(), num_bins);
|
||||
ASSERT_EQ(cuts.MinValues(), wcuts.MinValues());
|
||||
ASSERT_EQ(cuts.Ptrs(), wcuts.Ptrs());
|
||||
ASSERT_EQ(cuts.Values(), wcuts.Values());
|
||||
@@ -286,14 +298,15 @@ TEST(HistUitl, DeviceSketchWeights) {
|
||||
}
|
||||
|
||||
TEST(HistUtil, DeviceSketchBatches) {
|
||||
auto ctx = MakeCUDACtx(0);
|
||||
int num_bins = 256;
|
||||
int num_rows = 5000;
|
||||
int batch_sizes[] = {0, 100, 1500, 6000};
|
||||
auto batch_sizes = {0, 100, 1500, 6000};
|
||||
int num_columns = 5;
|
||||
for (auto batch_size : batch_sizes) {
|
||||
auto x = GenerateRandom(num_rows, num_columns);
|
||||
auto dmat = GetDMatrixFromData(x, num_rows, num_columns);
|
||||
auto cuts = DeviceSketch(0, dmat.get(), num_bins, batch_size);
|
||||
auto cuts = DeviceSketch(&ctx, dmat.get(), num_bins, batch_size);
|
||||
ValidateCuts(cuts, dmat.get(), num_bins);
|
||||
}
|
||||
|
||||
@@ -301,8 +314,8 @@ TEST(HistUtil, DeviceSketchBatches) {
|
||||
size_t batches = 16;
|
||||
auto x = GenerateRandom(num_rows * batches, num_columns);
|
||||
auto dmat = GetDMatrixFromData(x, num_rows * batches, num_columns);
|
||||
auto cuts_with_batches = DeviceSketch(0, dmat.get(), num_bins, num_rows);
|
||||
auto cuts = DeviceSketch(0, dmat.get(), num_bins, 0);
|
||||
auto cuts_with_batches = DeviceSketch(&ctx, dmat.get(), num_bins, num_rows);
|
||||
auto cuts = DeviceSketch(&ctx, dmat.get(), num_bins, 0);
|
||||
|
||||
auto const& cut_values_batched = cuts_with_batches.Values();
|
||||
auto const& cut_values = cuts.Values();
|
||||
@@ -313,15 +326,16 @@ TEST(HistUtil, DeviceSketchBatches) {
|
||||
}
|
||||
|
||||
TEST(HistUtil, DeviceSketchMultipleColumnsExternal) {
|
||||
int bin_sizes[] = {2, 16, 256, 512};
|
||||
int sizes[] = {100, 1000, 1500};
|
||||
auto ctx = MakeCUDACtx(0);
|
||||
auto bin_sizes = {2, 16, 256, 512};
|
||||
auto sizes = {100, 1000, 1500};
|
||||
int num_columns =5;
|
||||
for (auto num_rows : sizes) {
|
||||
auto x = GenerateRandom(num_rows, num_columns);
|
||||
dmlc::TemporaryDirectory temp;
|
||||
auto dmat = GetExternalMemoryDMatrixFromData(x, num_rows, num_columns, temp);
|
||||
for (auto num_bins : bin_sizes) {
|
||||
auto cuts = DeviceSketch(0, dmat.get(), num_bins);
|
||||
auto cuts = DeviceSketch(&ctx, dmat.get(), num_bins);
|
||||
ValidateCuts(cuts, dmat.get(), num_bins);
|
||||
}
|
||||
}
|
||||
@@ -329,8 +343,9 @@ TEST(HistUtil, DeviceSketchMultipleColumnsExternal) {
|
||||
|
||||
// See https://github.com/dmlc/xgboost/issues/5866.
|
||||
TEST(HistUtil, DeviceSketchExternalMemoryWithWeights) {
|
||||
int bin_sizes[] = {2, 16, 256, 512};
|
||||
int sizes[] = {100, 1000, 1500};
|
||||
auto ctx = MakeCUDACtx(0);
|
||||
auto bin_sizes = {2, 16, 256, 512};
|
||||
auto sizes = {100, 1000, 1500};
|
||||
int num_columns = 5;
|
||||
dmlc::TemporaryDirectory temp;
|
||||
for (auto num_rows : sizes) {
|
||||
@@ -338,7 +353,7 @@ TEST(HistUtil, DeviceSketchExternalMemoryWithWeights) {
|
||||
auto dmat = GetExternalMemoryDMatrixFromData(x, num_rows, num_columns, temp);
|
||||
dmat->Info().weights_.HostVector() = GenerateRandomWeights(num_rows);
|
||||
for (auto num_bins : bin_sizes) {
|
||||
auto cuts = DeviceSketch(0, dmat.get(), num_bins);
|
||||
auto cuts = DeviceSketch(&ctx, dmat.get(), num_bins);
|
||||
ValidateCuts(cuts, dmat.get(), num_bins);
|
||||
}
|
||||
}
|
||||
@@ -504,9 +519,9 @@ void TestCategoricalSketchAdapter(size_t n, size_t num_categories,
|
||||
}
|
||||
|
||||
TEST(HistUtil, AdapterDeviceSketchCategorical) {
|
||||
int categorical_sizes[] = {2, 6, 8, 12};
|
||||
auto categorical_sizes = {2, 6, 8, 12};
|
||||
int num_bins = 256;
|
||||
int sizes[] = {25, 100, 1000};
|
||||
auto sizes = {25, 100, 1000};
|
||||
for (auto n : sizes) {
|
||||
for (auto num_categories : categorical_sizes) {
|
||||
auto x = GenerateRandomCategoricalSingleColumn(n, num_categories);
|
||||
@@ -521,8 +536,8 @@ TEST(HistUtil, AdapterDeviceSketchCategorical) {
|
||||
}
|
||||
|
||||
TEST(HistUtil, AdapterDeviceSketchMultipleColumns) {
|
||||
int bin_sizes[] = {2, 16, 256, 512};
|
||||
int sizes[] = {100, 1000, 1500};
|
||||
auto bin_sizes = {2, 16, 256, 512};
|
||||
auto sizes = {100, 1000, 1500};
|
||||
int num_columns = 5;
|
||||
for (auto num_rows : sizes) {
|
||||
auto x = GenerateRandom(num_rows, num_columns);
|
||||
@@ -538,7 +553,7 @@ TEST(HistUtil, AdapterDeviceSketchMultipleColumns) {
|
||||
TEST(HistUtil, AdapterDeviceSketchBatches) {
|
||||
int num_bins = 256;
|
||||
int num_rows = 5000;
|
||||
int batch_sizes[] = {0, 100, 1500, 6000};
|
||||
auto batch_sizes = {0, 100, 1500, 6000};
|
||||
int num_columns = 5;
|
||||
for (auto batch_size : batch_sizes) {
|
||||
auto x = GenerateRandom(num_rows, num_columns);
|
||||
@@ -619,14 +634,15 @@ TEST(HistUtil, GetColumnSize) {
|
||||
// Check sketching from adapter or DMatrix results in the same answer
|
||||
// Consistency here is useful for testing and user experience
|
||||
TEST(HistUtil, SketchingEquivalent) {
|
||||
int bin_sizes[] = {2, 16, 256, 512};
|
||||
int sizes[] = {100, 1000, 1500};
|
||||
auto ctx = MakeCUDACtx(0);
|
||||
auto bin_sizes = {2, 16, 256, 512};
|
||||
auto sizes = {100, 1000, 1500};
|
||||
int num_columns = 5;
|
||||
for (auto num_rows : sizes) {
|
||||
auto x = GenerateRandom(num_rows, num_columns);
|
||||
auto dmat = GetDMatrixFromData(x, num_rows, num_columns);
|
||||
for (auto num_bins : bin_sizes) {
|
||||
auto dmat_cuts = DeviceSketch(0, dmat.get(), num_bins);
|
||||
auto dmat_cuts = DeviceSketch(&ctx, dmat.get(), num_bins);
|
||||
auto x_device = thrust::device_vector<float>(x);
|
||||
auto adapter = AdapterFromData(x_device, num_rows, num_columns);
|
||||
common::HistogramCuts adapter_cuts = MakeUnweightedCutsForTest(
|
||||
@@ -641,21 +657,25 @@ TEST(HistUtil, SketchingEquivalent) {
|
||||
}
|
||||
|
||||
TEST(HistUtil, DeviceSketchFromGroupWeights) {
|
||||
auto ctx = MakeCUDACtx(0);
|
||||
size_t constexpr kRows = 3000, kCols = 200, kBins = 256;
|
||||
size_t constexpr kGroups = 10;
|
||||
auto m = RandomDataGenerator{kRows, kCols, 0}.GenerateDMatrix();
|
||||
|
||||
// sketch with group weight
|
||||
auto& h_weights = m->Info().weights_.HostVector();
|
||||
h_weights.resize(kRows);
|
||||
h_weights.resize(kGroups);
|
||||
std::fill(h_weights.begin(), h_weights.end(), 1.0f);
|
||||
std::vector<bst_group_t> groups(kGroups);
|
||||
for (size_t i = 0; i < kGroups; ++i) {
|
||||
groups[i] = kRows / kGroups;
|
||||
}
|
||||
m->SetInfo("group", groups.data(), DataType::kUInt32, kGroups);
|
||||
HistogramCuts weighted_cuts = DeviceSketch(0, m.get(), kBins, 0);
|
||||
HistogramCuts weighted_cuts = DeviceSketch(&ctx, m.get(), kBins, 0);
|
||||
|
||||
// sketch with no weight
|
||||
h_weights.clear();
|
||||
HistogramCuts cuts = DeviceSketch(0, m.get(), kBins, 0);
|
||||
HistogramCuts cuts = DeviceSketch(&ctx, m.get(), kBins, 0);
|
||||
|
||||
ASSERT_EQ(cuts.Values().size(), weighted_cuts.Values().size());
|
||||
ASSERT_EQ(cuts.MinValues().size(), weighted_cuts.MinValues().size());
|
||||
@@ -723,9 +743,10 @@ void TestAdapterSketchFromWeights(bool with_group) {
|
||||
ASSERT_EQ(cuts.Ptrs().size(), kCols + 1);
|
||||
ValidateCuts(cuts, dmat.get(), kBins);
|
||||
|
||||
auto cuda_ctx = MakeCUDACtx(0);
|
||||
if (with_group) {
|
||||
dmat->Info().weights_ = decltype(dmat->Info().weights_)(); // remove weight
|
||||
HistogramCuts non_weighted = DeviceSketch(0, dmat.get(), kBins, 0);
|
||||
HistogramCuts non_weighted = DeviceSketch(&cuda_ctx, dmat.get(), kBins, 0);
|
||||
for (size_t i = 0; i < cuts.Values().size(); ++i) {
|
||||
ASSERT_EQ(cuts.Values()[i], non_weighted.Values()[i]);
|
||||
}
|
||||
@@ -760,5 +781,156 @@ TEST(HistUtil, AdapterSketchFromWeights) {
|
||||
TestAdapterSketchFromWeights(false);
|
||||
TestAdapterSketchFromWeights(true);
|
||||
}
|
||||
} // namespace common
|
||||
} // namespace xgboost
|
||||
|
||||
namespace {
|
||||
class DeviceSketchWithHessianTest
|
||||
: public ::testing::TestWithParam<std::tuple<bool, bst_row_t, bst_bin_t>> {
|
||||
bst_feature_t n_features_ = 5;
|
||||
bst_group_t n_groups_{3};
|
||||
|
||||
auto GenerateHessian(Context const* ctx, bst_row_t n_samples) const {
|
||||
HostDeviceVector<float> hessian;
|
||||
auto& h_hess = hessian.HostVector();
|
||||
h_hess = GenerateRandomWeights(n_samples);
|
||||
std::mt19937 rng(0);
|
||||
std::shuffle(h_hess.begin(), h_hess.end(), rng);
|
||||
hessian.SetDevice(ctx->Device());
|
||||
return hessian;
|
||||
}
|
||||
|
||||
void CheckReg(Context const* ctx, std::shared_ptr<DMatrix> p_fmat, bst_bin_t n_bins,
|
||||
HostDeviceVector<float> const& hessian, std::vector<float> const& w,
|
||||
std::size_t n_elements) const {
|
||||
auto const& h_hess = hessian.ConstHostVector();
|
||||
{
|
||||
auto& h_weight = p_fmat->Info().weights_.HostVector();
|
||||
h_weight = w;
|
||||
}
|
||||
|
||||
HistogramCuts cuts_hess =
|
||||
DeviceSketchWithHessian(ctx, p_fmat.get(), n_bins, hessian.ConstDeviceSpan(), n_elements);
|
||||
ValidateCuts(cuts_hess, p_fmat.get(), n_bins);
|
||||
|
||||
// merge hessian
|
||||
{
|
||||
auto& h_weight = p_fmat->Info().weights_.HostVector();
|
||||
ASSERT_EQ(h_weight.size(), h_hess.size());
|
||||
for (std::size_t i = 0; i < h_weight.size(); ++i) {
|
||||
h_weight[i] = w[i] * h_hess[i];
|
||||
}
|
||||
}
|
||||
|
||||
HistogramCuts cuts_wh = DeviceSketch(ctx, p_fmat.get(), n_bins, n_elements);
|
||||
ValidateCuts(cuts_wh, p_fmat.get(), n_bins);
|
||||
ASSERT_EQ(cuts_hess.Values().size(), cuts_wh.Values().size());
|
||||
for (std::size_t i = 0; i < cuts_hess.Values().size(); ++i) {
|
||||
ASSERT_NEAR(cuts_wh.Values()[i], cuts_hess.Values()[i], kRtEps);
|
||||
}
|
||||
|
||||
p_fmat->Info().weights_.HostVector() = w;
|
||||
}
|
||||
|
||||
protected:
|
||||
Context ctx_ = MakeCUDACtx(0);
|
||||
|
||||
void TestLTR(Context const* ctx, bst_row_t n_samples, bst_bin_t n_bins,
|
||||
std::size_t n_elements) const {
|
||||
auto x = GenerateRandom(n_samples, n_features_);
|
||||
|
||||
std::vector<bst_group_t> gptr;
|
||||
gptr.resize(n_groups_ + 1, 0);
|
||||
gptr[1] = n_samples / n_groups_;
|
||||
gptr[2] = n_samples / n_groups_ + gptr[1];
|
||||
gptr.back() = n_samples;
|
||||
|
||||
auto hessian = this->GenerateHessian(ctx, n_samples);
|
||||
auto const& h_hess = hessian.ConstHostVector();
|
||||
auto p_fmat = GetDMatrixFromData(x, n_samples, n_features_);
|
||||
p_fmat->Info().group_ptr_ = gptr;
|
||||
|
||||
// test with constant group weight
|
||||
std::vector<float> w(n_groups_, 1.0f);
|
||||
p_fmat->Info().weights_.HostVector() = w;
|
||||
HistogramCuts cuts_hess =
|
||||
DeviceSketchWithHessian(ctx, p_fmat.get(), n_bins, hessian.ConstDeviceSpan(), n_elements);
|
||||
// make validation easier by converting it into sample weight.
|
||||
p_fmat->Info().weights_.HostVector() = h_hess;
|
||||
p_fmat->Info().group_ptr_.clear();
|
||||
ValidateCuts(cuts_hess, p_fmat.get(), n_bins);
|
||||
// restore ltr properties
|
||||
p_fmat->Info().weights_.HostVector() = w;
|
||||
p_fmat->Info().group_ptr_ = gptr;
|
||||
|
||||
// test with random group weight
|
||||
w = GenerateRandomWeights(n_groups_);
|
||||
p_fmat->Info().weights_.HostVector() = w;
|
||||
cuts_hess =
|
||||
DeviceSketchWithHessian(ctx, p_fmat.get(), n_bins, hessian.ConstDeviceSpan(), n_elements);
|
||||
// make validation easier by converting it into sample weight.
|
||||
p_fmat->Info().weights_.HostVector() = h_hess;
|
||||
p_fmat->Info().group_ptr_.clear();
|
||||
ValidateCuts(cuts_hess, p_fmat.get(), n_bins);
|
||||
|
||||
// merge hessian with sample weight
|
||||
p_fmat->Info().weights_.Resize(n_samples);
|
||||
p_fmat->Info().group_ptr_.clear();
|
||||
for (std::size_t i = 0; i < h_hess.size(); ++i) {
|
||||
auto gidx = dh::SegmentId(Span{gptr.data(), gptr.size()}, i);
|
||||
p_fmat->Info().weights_.HostVector()[i] = w[gidx] * h_hess[i];
|
||||
}
|
||||
auto cuts = DeviceSketch(ctx, p_fmat.get(), n_bins, n_elements);
|
||||
ValidateCuts(cuts, p_fmat.get(), n_bins);
|
||||
ASSERT_EQ(cuts.Values().size(), cuts_hess.Values().size());
|
||||
for (std::size_t i = 0; i < cuts.Values().size(); ++i) {
|
||||
EXPECT_NEAR(cuts.Values()[i], cuts_hess.Values()[i], 1e-4f);
|
||||
}
|
||||
}
|
||||
|
||||
void TestRegression(Context const* ctx, bst_row_t n_samples, bst_bin_t n_bins,
|
||||
std::size_t n_elements) const {
|
||||
auto x = GenerateRandom(n_samples, n_features_);
|
||||
auto p_fmat = GetDMatrixFromData(x, n_samples, n_features_);
|
||||
std::vector<float> w = GenerateRandomWeights(n_samples);
|
||||
|
||||
auto hessian = this->GenerateHessian(ctx, n_samples);
|
||||
|
||||
this->CheckReg(ctx, p_fmat, n_bins, hessian, w, n_elements);
|
||||
}
|
||||
};
|
||||
|
||||
auto MakeParamsForTest() {
|
||||
std::vector<bst_row_t> sizes = {1, 2, 256, 512, 1000, 1500};
|
||||
std::vector<bst_bin_t> bin_sizes = {2, 16, 256, 512};
|
||||
std::vector<std::tuple<bool, bst_row_t, bst_bin_t>> configs;
|
||||
for (auto n_samples : sizes) {
|
||||
for (auto n_bins : bin_sizes) {
|
||||
configs.emplace_back(true, n_samples, n_bins);
|
||||
configs.emplace_back(false, n_samples, n_bins);
|
||||
}
|
||||
}
|
||||
return configs;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
TEST_P(DeviceSketchWithHessianTest, DeviceSketchWithHessian) {
|
||||
auto param = GetParam();
|
||||
auto n_samples = std::get<1>(param);
|
||||
auto n_bins = std::get<2>(param);
|
||||
if (std::get<0>(param)) {
|
||||
this->TestLTR(&ctx_, n_samples, n_bins, 0);
|
||||
this->TestLTR(&ctx_, n_samples, n_bins, 512);
|
||||
} else {
|
||||
this->TestRegression(&ctx_, n_samples, n_bins, 0);
|
||||
this->TestRegression(&ctx_, n_samples, n_bins, 512);
|
||||
}
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_SUITE_P(
|
||||
HistUtil, DeviceSketchWithHessianTest, ::testing::ValuesIn(MakeParamsForTest()),
|
||||
[](::testing::TestParamInfo<DeviceSketchWithHessianTest::ParamType> const& info) {
|
||||
auto task = std::get<0>(info.param) ? "ltr" : "reg";
|
||||
auto n_samples = std::to_string(std::get<1>(info.param));
|
||||
auto n_bins = std::to_string(std::get<2>(info.param));
|
||||
return std::string{task} + "_" + n_samples + "_" + n_bins;
|
||||
});
|
||||
} // namespace xgboost::common
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
/**
|
||||
* Copyright 2020-2023, XGBoost contributors
|
||||
*/
|
||||
#include <gtest/gtest.h>
|
||||
#include "test_quantile.h"
|
||||
#include "../helpers.h"
|
||||
|
||||
#include "../../../src/collective/communicator-inl.cuh"
|
||||
#include "../../../src/common/hist_util.cuh"
|
||||
#include "../../../src/common/quantile.cuh"
|
||||
#include "../../../src/data/device_adapter.cuh" // CupyAdapter
|
||||
#include "../helpers.h"
|
||||
#include "test_quantile.h"
|
||||
|
||||
namespace xgboost {
|
||||
namespace {
|
||||
@@ -437,13 +442,13 @@ void TestColumnSplitBasic() {
|
||||
}()};
|
||||
|
||||
// Generate cuts for distributed environment.
|
||||
auto const device = rank;
|
||||
HistogramCuts distributed_cuts = common::DeviceSketch(device, m.get(), kBins);
|
||||
auto ctx = MakeCUDACtx(rank);
|
||||
HistogramCuts distributed_cuts = common::DeviceSketch(&ctx, m.get(), kBins);
|
||||
|
||||
// Generate cuts for single node environment
|
||||
collective::Finalize();
|
||||
CHECK_EQ(collective::GetWorldSize(), 1);
|
||||
HistogramCuts single_node_cuts = common::DeviceSketch(device, m.get(), kBins);
|
||||
HistogramCuts single_node_cuts = common::DeviceSketch(&ctx, m.get(), kBins);
|
||||
|
||||
auto const& sptrs = single_node_cuts.Ptrs();
|
||||
auto const& dptrs = distributed_cuts.Ptrs();
|
||||
|
||||
Reference in New Issue
Block a user