Sketching from adapters (#5365)
* Sketching from adapters * Add weights test
This commit is contained in:
@@ -28,6 +28,34 @@ inline std::vector<float> GenerateRandom(int num_rows, int num_columns) {
|
||||
return x;
|
||||
}
|
||||
|
||||
inline std::vector<float> GenerateRandomWeights(int num_rows) {
|
||||
std::vector<float> w(num_rows);
|
||||
std::mt19937 rng(1);
|
||||
std::uniform_real_distribution<float> dist(0.0, 1.0);
|
||||
std::generate(w.begin(), w.end(), [&]() { return dist(rng); });
|
||||
return w;
|
||||
}
|
||||
|
||||
#ifdef __CUDACC__
|
||||
inline data::CupyAdapter AdapterFromData(const thrust::device_vector<float> &x,
|
||||
int num_rows, int num_columns) {
|
||||
Json array_interface{Object()};
|
||||
std::vector<Json> shape = {Json(static_cast<Integer::Int>(num_rows)),
|
||||
Json(static_cast<Integer::Int>(num_columns))};
|
||||
array_interface["shape"] = Array(shape);
|
||||
std::vector<Json> j_data{
|
||||
Json(Integer(reinterpret_cast<Integer::Int>(x.data().get()))),
|
||||
Json(Boolean(false))};
|
||||
array_interface["data"] = j_data;
|
||||
array_interface["version"] = Integer(static_cast<Integer::Int>(1));
|
||||
array_interface["typestr"] = String("<f4");
|
||||
std::stringstream ss;
|
||||
Json::Dump(array_interface, &ss);
|
||||
std::string str = ss.str();
|
||||
return data::CupyAdapter(str);
|
||||
}
|
||||
#endif
|
||||
|
||||
inline std::vector<float> GenerateRandomCategoricalSingleColumn(int n,
|
||||
int num_categories) {
|
||||
std::vector<float> x(n);
|
||||
@@ -69,21 +97,22 @@ 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>& column,
|
||||
const std::vector<float>& sorted_column,const std::vector<float >&sorted_weights,
|
||||
int num_bins) {
|
||||
std::map<int, int> counts;
|
||||
for (auto& v : column) {
|
||||
counts[cuts.SearchBin(v, column_idx)]++;
|
||||
std::map<int, int> bin_weights;
|
||||
for (auto i = 0ull; i < sorted_column.size(); i++) {
|
||||
bin_weights[cuts.SearchBin(sorted_column[i], column_idx)] += sorted_weights[i];
|
||||
}
|
||||
int local_num_bins = cuts.Ptrs()[column_idx + 1] - cuts.Ptrs()[column_idx];
|
||||
int expected_num_elements = column.size() / local_num_bins;
|
||||
// Allow about 30% deviation. This test is not very strict, it only ensures
|
||||
auto total_weight = std::accumulate(sorted_weights.begin(), sorted_weights.end(),0);
|
||||
int expected_bin_weight = total_weight / local_num_bins;
|
||||
// Allow up to 30% deviation. This test is not very strict, it only ensures
|
||||
// roughly equal distribution
|
||||
int allowable_error = std::max(2, int(expected_num_elements * 0.3));
|
||||
int allowable_error = std::max(2, int(expected_bin_weight * 0.3));
|
||||
|
||||
// First and last bin can have smaller
|
||||
for (auto& kv : counts) {
|
||||
EXPECT_LE(std::abs(counts[kv.first] - expected_num_elements),
|
||||
for (auto& kv : bin_weights) {
|
||||
EXPECT_LE(std::abs(bin_weights[kv.first] - expected_bin_weight),
|
||||
allowable_error );
|
||||
}
|
||||
}
|
||||
@@ -91,26 +120,29 @@ inline void TestBinDistribution(const HistogramCuts& cuts, int column_idx,
|
||||
// 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) {
|
||||
float eps = 0.05;
|
||||
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 (auto i = 0; i < cuts.size() - 1; i++) {
|
||||
int expected_rank = ((i+1) * sorted_x.size()) / cuts.size();
|
||||
while (cuts[i] > sorted_x[j]) {
|
||||
sum_weight += sorted_weights[j];
|
||||
j++;
|
||||
}
|
||||
int actual_rank = j;
|
||||
int acceptable_error = std::max(2, int(sorted_x.size() * eps));
|
||||
ASSERT_LE(std::abs(expected_rank - actual_rank), acceptable_error);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
inline void ValidateColumn(const HistogramCuts& cuts, int column_idx,
|
||||
const std::vector<float>& column,
|
||||
const std::vector<float>& sorted_column,
|
||||
const std::vector<float>& sorted_weights,
|
||||
int num_bins) {
|
||||
std::vector<float> sorted_column(column);
|
||||
std::sort(sorted_column.begin(), sorted_column.end());
|
||||
|
||||
// Check the endpoints are correct
|
||||
EXPECT_LT(cuts.MinValues()[column_idx], sorted_column.front());
|
||||
@@ -126,40 +158,60 @@ inline void ValidateColumn(const HistogramCuts& cuts, int column_idx,
|
||||
EXPECT_EQ(std::set<float>(cuts_begin, cuts_end).size(),
|
||||
cuts_end - cuts_begin);
|
||||
|
||||
if (sorted_column.size() <= num_bins) {
|
||||
auto unique = std::set<float>(sorted_column.begin(), sorted_column.end());
|
||||
if (unique.size() <= num_bins) {
|
||||
// Less unique values than number of bins
|
||||
// Each value should get its own bin
|
||||
|
||||
// First check the inputs are unique
|
||||
int num_unique =
|
||||
std::set<float>(sorted_column.begin(), sorted_column.end()).size();
|
||||
EXPECT_EQ(num_unique, sorted_column.size());
|
||||
for (auto i = 0ull; i < sorted_column.size(); i++) {
|
||||
ASSERT_EQ(cuts.SearchBin(sorted_column[i], column_idx),
|
||||
cuts.Ptrs()[column_idx] + i);
|
||||
int i = 0;
|
||||
for (auto v : unique) {
|
||||
ASSERT_EQ(cuts.SearchBin(v, column_idx), cuts.Ptrs()[column_idx] + i);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
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, num_bins);
|
||||
TestRank(column_cuts, sorted_column);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
// x is dense and row major
|
||||
inline void ValidateCuts(const HistogramCuts& cuts, std::vector<float>& x,
|
||||
int num_rows, int num_columns,
|
||||
inline void ValidateCuts(const HistogramCuts& cuts, DMatrix* dmat,
|
||||
int num_bins) {
|
||||
for (auto i = 0; i < num_columns; i++) {
|
||||
// Extract the column
|
||||
std::vector<float> column(num_rows);
|
||||
for (auto j = 0; j < num_rows; j++) {
|
||||
column[j] = x[j*num_columns + i];
|
||||
}
|
||||
ValidateColumn(cuts,i, column, num_bins);
|
||||
}
|
||||
// Collect data into columns
|
||||
std::vector<std::vector<float>> columns(dmat->Info().num_col_);
|
||||
for (auto& batch : dmat->GetBatches<SparsePage>()) {
|
||||
for (auto i = 0ull; i < batch.Size(); i++) {
|
||||
for (auto e : batch[i]) {
|
||||
columns[e.index].push_back(e.fvalue);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Sort
|
||||
for (auto i = 0ull; i < columns.size(); i++) {
|
||||
auto& col = columns.at(i);
|
||||
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::vector<float> sorted_column(col.size());
|
||||
std::vector<float> sorted_weights(col.size(), 1.0);
|
||||
for (auto j = 0ull; j < col.size(); j++) {
|
||||
sorted_column[j] = col[index[j]];
|
||||
if (w.size() == col.size()) {
|
||||
sorted_weights[j] = w[index[j]];
|
||||
}
|
||||
}
|
||||
|
||||
ValidateColumn(cuts, i, sorted_column, sorted_weights, num_bins);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace common
|
||||
|
||||
Reference in New Issue
Block a user