Group aware GPU sketching. (#5551)

* Group aware GPU weighted sketching.

* Distribute group weights to each data point.
* Relax the test.
* Validate input meta info.
* Fix metainfo copy ctor.
This commit is contained in:
Jiaming Yuan
2020-04-20 17:18:52 +08:00
committed by GitHub
parent 397d8f0ee7
commit 29a4cfe400
9 changed files with 296 additions and 124 deletions

View File

@@ -3,22 +3,19 @@
#include <algorithm>
#include <cmath>
#include <thrust/device_vector.h>
#include "xgboost/c_api.h"
#include <xgboost/data.h>
#include <xgboost/c_api.h>
#include "test_hist_util.h"
#include "../helpers.h"
#include "../data/test_array_interface.h"
#include "../../../src/common/device_helpers.cuh"
#include "../../../src/common/hist_util.h"
#include "../helpers.h"
#include <xgboost/data.h>
#include "../../../src/data/device_adapter.cuh"
#include "../data/test_array_interface.h"
#include "../../../src/common/math.h"
#include "../../../src/data/simple_dmatrix.h"
#include "test_hist_util.h"
#include "../../../include/xgboost/logging.h"
namespace xgboost {
@@ -143,7 +140,6 @@ TEST(HistUtil, DeviceSketchMultipleColumns) {
ValidateCuts(cuts, dmat.get(), num_bins);
}
}
}
TEST(HistUtil, DeviceSketchMultipleColumnsWeights) {
@@ -161,6 +157,29 @@ TEST(HistUtil, DeviceSketchMultipleColumnsWeights) {
}
}
TEST(HistUitl, DeviceSketchWeights) {
int bin_sizes[] = {2, 16, 256, 512};
int 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);
auto weighted_dmat = GetDMatrixFromData(x, num_rows, num_columns);
auto& h_weights = weighted_dmat->Info().weights_.HostVector();
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);
ASSERT_EQ(cuts.MinValues(), wcuts.MinValues());
ASSERT_EQ(cuts.Ptrs(), wcuts.Ptrs());
ASSERT_EQ(cuts.Values(), wcuts.Values());
ValidateCuts(cuts, dmat.get(), num_bins);
ValidateCuts(wcuts, weighted_dmat.get(), num_bins);
}
}
}
TEST(HistUtil, DeviceSketchBatches) {
int num_bins = 256;
int num_rows = 5000;
@@ -190,8 +209,7 @@ TEST(HistUtil, DeviceSketchMultipleColumnsExternal) {
}
}
TEST(HistUtil, AdapterDeviceSketch)
{
TEST(HistUtil, AdapterDeviceSketch) {
int rows = 5;
int cols = 1;
int num_bins = 4;
@@ -235,7 +253,7 @@ TEST(HistUtil, AdapterDeviceSketchMemory) {
bytes_num_elements + bytes_cuts + bytes_num_columns + bytes_constant);
}
TEST(HistUtil, AdapterDeviceSketchCategorical) {
TEST(HistUtil, AdapterDeviceSketchCategorical) {
int categorical_sizes[] = {2, 6, 8, 12};
int num_bins = 256;
int sizes[] = {25, 100, 1000};
@@ -268,6 +286,7 @@ TEST(HistUtil, AdapterDeviceSketchMultipleColumns) {
}
}
}
TEST(HistUtil, AdapterDeviceSketchBatches) {
int num_bins = 256;
int num_rows = 5000;
@@ -305,7 +324,38 @@ TEST(HistUtil, SketchingEquivalent) {
EXPECT_EQ(dmat_cuts.MinValues(), adapter_cuts.MinValues());
}
}
}
TEST(HistUtil, DeviceSketchFromGroupWeights) {
size_t constexpr kRows = 3000, kCols = 200, kBins = 256;
size_t constexpr kGroups = 10;
auto m = RandomDataGenerator {kRows, kCols, 0}.GenerateDMatrix();
auto& h_weights = m->Info().weights_.HostVector();
h_weights.resize(kRows);
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->Info().SetInfo("group", groups.data(), DataType::kUInt32, kGroups);
HistogramCuts weighted_cuts = DeviceSketch(0, m.get(), kBins, 0);
h_weights.clear();
HistogramCuts cuts = DeviceSketch(0, m.get(), kBins, 0);
ASSERT_EQ(cuts.Values().size(), weighted_cuts.Values().size());
ASSERT_EQ(cuts.MinValues().size(), weighted_cuts.MinValues().size());
ASSERT_EQ(cuts.Ptrs().size(), weighted_cuts.Ptrs().size());
for (size_t i = 0; i < cuts.Values().size(); ++i) {
EXPECT_EQ(cuts.Values()[i], weighted_cuts.Values()[i]) << "i:"<< i;
}
for (size_t i = 0; i < cuts.MinValues().size(); ++i) {
ASSERT_EQ(cuts.MinValues()[i], weighted_cuts.MinValues()[i]);
}
for (size_t i = 0; i < cuts.Ptrs().size(); ++i) {
ASSERT_EQ(cuts.Ptrs().at(i), weighted_cuts.Ptrs().at(i));
}
}
} // namespace common
} // namespace xgboost

View File

@@ -9,6 +9,11 @@
#include "../../../src/data/simple_dmatrix.h"
#include "../../../src/data/adapter.h"
#ifdef __CUDACC__
#include <xgboost/json.h>
#include "../../../src/data/device_adapter.cuh"
#endif // __CUDACC__
// Some helper functions used to test both GPU and CPU algorithms
//
namespace xgboost {
@@ -69,11 +74,11 @@ inline std::vector<float> GenerateRandomCategoricalSingleColumn(int n,
return x;
}
inline std::shared_ptr<data::SimpleDMatrix> GetDMatrixFromData(const std::vector<float>& x, int num_rows, int num_columns) {
inline std::shared_ptr<data::SimpleDMatrix>
GetDMatrixFromData(const std::vector<float> &x, int num_rows, int num_columns) {
data::DenseAdapter adapter(x.data(), num_rows, num_columns);
return std::shared_ptr<data::SimpleDMatrix>(new data::SimpleDMatrix(
&adapter, std::numeric_limits<float>::quiet_NaN(),
1));
&adapter, std::numeric_limits<float>::quiet_NaN(), 1));
}
inline std::shared_ptr<DMatrix> GetExternalMemoryDMatrixFromData(
@@ -96,8 +101,9 @@ inline std::shared_ptr<DMatrix> GetExternalMemoryDMatrixFromData(
}
// Test that elements are approximately equally distributed among bins
inline void TestBinDistribution(const HistogramCuts& cuts, int column_idx,
const std::vector<float>& sorted_column,const std::vector<float >&sorted_weights,
inline void TestBinDistribution(const HistogramCuts &cuts, int column_idx,
const std::vector<float> &sorted_column,
const std::vector<float> &sorted_weights,
int num_bins) {
std::map<int, int> bin_weights;
for (auto i = 0ull; i < sorted_column.size(); i++) {
@@ -113,29 +119,29 @@ inline void TestBinDistribution(const HistogramCuts& cuts, int column_idx,
// First and last bin can have smaller
for (auto& kv : bin_weights) {
EXPECT_LE(std::abs(bin_weights[kv.first] - expected_bin_weight),
allowable_error );
allowable_error);
}
}
// Test sketch quantiles against the real quantiles
// Not a very strict test
inline void TestRank(const std::vector<float>& cuts,
const std::vector<float>& sorted_x,
const std::vector<float>& sorted_weights) {
// Test sketch quantiles against the real quantiles Not a very strict
// test
inline void TestRank(const std::vector<float> &column_cuts,
const std::vector<float> &sorted_x,
const std::vector<float> &sorted_weights) {
double eps = 0.05;
auto total_weight =
std::accumulate(sorted_weights.begin(), sorted_weights.end(), 0.0);
// Ignore the last cut, its special
double sum_weight = 0.0;
size_t j = 0;
for (size_t i = 0; i < cuts.size() - 1; i++) {
while (cuts[i] > sorted_x[j]) {
for (size_t i = 0; i < column_cuts.size() - 1; i++) {
while (column_cuts[i] > sorted_x[j]) {
sum_weight += sorted_weights[j];
j++;
}
double expected_rank = ((i + 1) * total_weight) / cuts.size();
double acceptable_error = std::max(2.0, total_weight * eps);
ASSERT_LE(std::abs(expected_rank - sum_weight), acceptable_error);
double expected_rank = ((i + 1) * total_weight) / column_cuts.size();
double acceptable_error = std::max(2.9, total_weight * eps);
EXPECT_LE(std::abs(expected_rank - sum_weight), acceptable_error);
}
}
@@ -167,15 +173,14 @@ inline void ValidateColumn(const HistogramCuts& cuts, int column_idx,
ASSERT_EQ(cuts.SearchBin(v, column_idx), cuts.Ptrs()[column_idx] + i);
i++;
}
}
else {
} else {
int num_cuts_column = cuts.Ptrs()[column_idx + 1] - cuts.Ptrs()[column_idx];
std::vector<float> column_cuts(num_cuts_column);
std::copy(cuts.Values().begin() + cuts.Ptrs()[column_idx],
cuts.Values().begin() + cuts.Ptrs()[column_idx + 1],
column_cuts.begin());
TestBinDistribution(cuts, column_idx, sorted_column,sorted_weights, num_bins);
TestRank(column_cuts, sorted_column,sorted_weights);
TestBinDistribution(cuts, column_idx, sorted_column, sorted_weights, num_bins);
TestRank(column_cuts, sorted_column, sorted_weights);
}
}
@@ -196,10 +201,8 @@ inline void ValidateCuts(const HistogramCuts& cuts, DMatrix* dmat,
const auto& w = dmat->Info().weights_.HostVector();
std::vector<size_t > index(col.size());
std::iota(index.begin(), index.end(), 0);
std::sort(index.begin(), index.end(),[=](size_t a,size_t b)
{
return col[a] < col[b];
});
std::sort(index.begin(), index.end(),
[=](size_t a, size_t b) { return col[a] < col[b]; });
std::vector<float> sorted_column(col.size());
std::vector<float> sorted_weights(col.size(), 1.0);