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:
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -141,3 +141,17 @@ TEST(MetaInfo, LoadQid) {
|
||||
CHECK(batch.data.HostVector() == expected_data);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(MetaInfo, Validate) {
|
||||
xgboost::MetaInfo info;
|
||||
info.num_row_ = 10;
|
||||
info.num_nonzero_ = 12;
|
||||
info.num_col_ = 3;
|
||||
std::vector<xgboost::bst_group_t> groups (11);
|
||||
info.SetInfo("group", groups.data(), xgboost::DataType::kUInt32, 11);
|
||||
EXPECT_THROW(info.Validate(), dmlc::Error);
|
||||
|
||||
std::vector<float> labels(info.num_row_ + 1);
|
||||
info.SetInfo("label", labels.data(), xgboost::DataType::kFloat32, info.num_row_ + 1);
|
||||
EXPECT_THROW(info.Validate(), dmlc::Error);
|
||||
}
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
import numpy as np
|
||||
from scipy.sparse import csr_matrix
|
||||
import xgboost
|
||||
import os
|
||||
import math
|
||||
import unittest
|
||||
import itertools
|
||||
import shutil
|
||||
import urllib.request
|
||||
import zipfile
|
||||
|
||||
|
||||
class TestRanking(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
@@ -22,7 +21,7 @@ class TestRanking(unittest.TestCase):
|
||||
target = cls.dpath + '/MQ2008.zip'
|
||||
|
||||
if os.path.exists(cls.dpath) and os.path.exists(target):
|
||||
print ("Skipping dataset download...")
|
||||
print("Skipping dataset download...")
|
||||
else:
|
||||
urllib.request.urlretrieve(url=src, filename=target)
|
||||
with zipfile.ZipFile(target, 'r') as f:
|
||||
@@ -50,17 +49,30 @@ class TestRanking(unittest.TestCase):
|
||||
cls.qid_test = qid_test
|
||||
cls.qid_valid = qid_valid
|
||||
|
||||
def setup_weighted(x, y, groups):
|
||||
# Setup weighted data
|
||||
data = xgboost.DMatrix(x, y)
|
||||
groups_segment = [len(list(items))
|
||||
for _key, items in itertools.groupby(groups)]
|
||||
data.set_group(groups_segment)
|
||||
n_groups = len(groups_segment)
|
||||
weights = np.ones((n_groups,))
|
||||
data.set_weight(weights)
|
||||
return data
|
||||
|
||||
cls.dtrain_w = setup_weighted(x_train, y_train, qid_train)
|
||||
cls.dtest_w = setup_weighted(x_test, y_test, qid_test)
|
||||
cls.dvalid_w = setup_weighted(x_valid, y_valid, qid_valid)
|
||||
|
||||
# model training parameters
|
||||
cls.params = {'booster': 'gbtree',
|
||||
'tree_method': 'gpu_hist',
|
||||
'gpu_id': 0,
|
||||
'predictor': 'gpu_predictor'
|
||||
}
|
||||
'predictor': 'gpu_predictor'}
|
||||
cls.cpu_params = {'booster': 'gbtree',
|
||||
'tree_method': 'hist',
|
||||
'gpu_id': -1,
|
||||
'predictor': 'cpu_predictor'
|
||||
}
|
||||
'predictor': 'cpu_predictor'}
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
@@ -81,30 +93,46 @@ class TestRanking(unittest.TestCase):
|
||||
# specify validations set to watch performance
|
||||
watchlist = [(cls.dtest, 'eval'), (cls.dtrain, 'train')]
|
||||
|
||||
num_trees=2500
|
||||
check_metric_improvement_rounds=10
|
||||
num_trees = 2500
|
||||
check_metric_improvement_rounds = 10
|
||||
|
||||
evals_result = {}
|
||||
cls.params['objective'] = rank_objective
|
||||
cls.params['eval_metric'] = metric_name
|
||||
bst = xgboost.train(cls.params, cls.dtrain, num_boost_round=num_trees,
|
||||
early_stopping_rounds=check_metric_improvement_rounds,
|
||||
evals=watchlist, evals_result=evals_result)
|
||||
bst = xgboost.train(
|
||||
cls.params, cls.dtrain, num_boost_round=num_trees,
|
||||
early_stopping_rounds=check_metric_improvement_rounds,
|
||||
evals=watchlist, evals_result=evals_result)
|
||||
gpu_map_metric = evals_result['train'][metric_name][-1]
|
||||
|
||||
evals_result = {}
|
||||
cls.cpu_params['objective'] = rank_objective
|
||||
cls.cpu_params['eval_metric'] = metric_name
|
||||
bstc = xgboost.train(cls.cpu_params, cls.dtrain, num_boost_round=num_trees,
|
||||
early_stopping_rounds=check_metric_improvement_rounds,
|
||||
evals=watchlist, evals_result=evals_result)
|
||||
bstc = xgboost.train(
|
||||
cls.cpu_params, cls.dtrain, num_boost_round=num_trees,
|
||||
early_stopping_rounds=check_metric_improvement_rounds,
|
||||
evals=watchlist, evals_result=evals_result)
|
||||
cpu_map_metric = evals_result['train'][metric_name][-1]
|
||||
|
||||
print("{0} gpu {1} metric {2}".format(rank_objective, metric_name, gpu_map_metric))
|
||||
print("{0} cpu {1} metric {2}".format(rank_objective, metric_name, cpu_map_metric))
|
||||
print("gpu best score {0} cpu best score {1}".format(bst.best_score, bstc.best_score))
|
||||
assert np.allclose(gpu_map_metric, cpu_map_metric, tolerance, tolerance)
|
||||
assert np.allclose(bst.best_score, bstc.best_score, tolerance, tolerance)
|
||||
assert np.allclose(gpu_map_metric, cpu_map_metric, tolerance,
|
||||
tolerance)
|
||||
assert np.allclose(bst.best_score, bstc.best_score, tolerance,
|
||||
tolerance)
|
||||
|
||||
evals_result_weighted = {}
|
||||
watchlist = [(cls.dtest_w, 'eval'), (cls.dtrain_w, 'train')]
|
||||
bst_w = xgboost.train(
|
||||
cls.params, cls.dtrain_w, num_boost_round=num_trees,
|
||||
early_stopping_rounds=check_metric_improvement_rounds,
|
||||
evals=watchlist, evals_result=evals_result_weighted)
|
||||
weighted_metric = evals_result_weighted['train'][metric_name][-1]
|
||||
# GPU Ranking is not deterministic due to `AtomicAddGpair`,
|
||||
# remove tolerance once the issue is resolved.
|
||||
# https://github.com/dmlc/xgboost/issues/5561
|
||||
assert np.allclose(bst_w.best_score, bst.best_score,
|
||||
tolerance, tolerance)
|
||||
assert np.allclose(weighted_metric, gpu_map_metric,
|
||||
tolerance, tolerance)
|
||||
|
||||
def test_training_rank_pairwise_map_metric(self):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user