Refactor tests with data generator. (#5439)

This commit is contained in:
Jiaming Yuan
2020-03-27 06:44:44 +08:00
committed by GitHub
parent 7146b91d5a
commit 4942da64ae
26 changed files with 334 additions and 259 deletions

View File

@@ -24,11 +24,11 @@ TEST(CpuPredictor, Basic) {
gbm::GBTreeModel model = CreateTestModel(&param);
auto dmat = CreateDMatrix(kRows, kCols, 0);
auto dmat = RandomDataGenerator(kRows, kCols, 0).GenerateDMatix();
// Test predict batch
PredictionCacheEntry out_predictions;
cpu_predictor->PredictBatch((*dmat).get(), &out_predictions, model, 0);
cpu_predictor->PredictBatch(dmat.get(), &out_predictions, model, 0);
ASSERT_EQ(model.trees.size(), out_predictions.version);
std::vector<float>& out_predictions_h = out_predictions.predictions.HostVector();
for (size_t i = 0; i < out_predictions.predictions.Size(); i++) {
@@ -36,7 +36,7 @@ TEST(CpuPredictor, Basic) {
}
// Test predict instance
auto &batch = *(*dmat)->GetBatches<xgboost::SparsePage>().begin();
auto const &batch = *dmat->GetBatches<xgboost::SparsePage>().begin();
for (size_t i = 0; i < batch.Size(); i++) {
std::vector<float> instance_out_predictions;
cpu_predictor->PredictInstance(batch[i], &instance_out_predictions, model);
@@ -45,14 +45,14 @@ TEST(CpuPredictor, Basic) {
// Test predict leaf
std::vector<float> leaf_out_predictions;
cpu_predictor->PredictLeaf((*dmat).get(), &leaf_out_predictions, model);
cpu_predictor->PredictLeaf(dmat.get(), &leaf_out_predictions, model);
for (auto v : leaf_out_predictions) {
ASSERT_EQ(v, 0);
}
// Test predict contribution
std::vector<float> out_contribution;
cpu_predictor->PredictContribution((*dmat).get(), &out_contribution, model);
cpu_predictor->PredictContribution(dmat.get(), &out_contribution, model);
ASSERT_EQ(out_contribution.size(), kRows * (kCols + 1));
for (size_t i = 0; i < out_contribution.size(); ++i) {
auto const& contri = out_contribution[i];
@@ -64,7 +64,7 @@ TEST(CpuPredictor, Basic) {
}
}
// Test predict contribution (approximate method)
cpu_predictor->PredictContribution((*dmat).get(), &out_contribution, model, 0, nullptr, true);
cpu_predictor->PredictContribution(dmat.get(), &out_contribution, model, 0, nullptr, true);
for (size_t i = 0; i < out_contribution.size(); ++i) {
auto const& contri = out_contribution[i];
// shift 1 for bias, as test tree is a decision dump, only global bias is filled with LeafValue().
@@ -74,8 +74,6 @@ TEST(CpuPredictor, Basic) {
ASSERT_EQ(contri, 0);
}
}
delete dmat;
}
TEST(CpuPredictor, ExternalMemory) {

View File

@@ -30,7 +30,7 @@ TEST(GPUPredictor, Basic) {
for (size_t i = 1; i < 33; i *= 2) {
int n_row = i, n_col = i;
auto dmat = CreateDMatrix(n_row, n_col, 0);
auto dmat = RandomDataGenerator(n_row, n_col, 0).GenerateDMatix();
LearnerModelParam param;
param.num_feature = n_col;
@@ -43,9 +43,9 @@ TEST(GPUPredictor, Basic) {
PredictionCacheEntry gpu_out_predictions;
PredictionCacheEntry cpu_out_predictions;
gpu_predictor->PredictBatch((*dmat).get(), &gpu_out_predictions, model, 0);
gpu_predictor->PredictBatch(dmat.get(), &gpu_out_predictions, model, 0);
ASSERT_EQ(model.trees.size(), gpu_out_predictions.version);
cpu_predictor->PredictBatch((*dmat).get(), &cpu_out_predictions, model, 0);
cpu_predictor->PredictBatch(dmat.get(), &cpu_out_predictions, model, 0);
std::vector<float>& gpu_out_predictions_h = gpu_out_predictions.predictions.HostVector();
std::vector<float>& cpu_out_predictions_h = cpu_out_predictions.predictions.HostVector();
@@ -53,7 +53,6 @@ TEST(GPUPredictor, Basic) {
for (int j = 0; j < gpu_out_predictions.predictions.Size(); j++) {
ASSERT_NEAR(gpu_out_predictions_h[j], cpu_out_predictions_h[j], abs_tolerance);
}
delete dmat;
}
}

View File

@@ -21,11 +21,9 @@ TEST(Predictor, PredictionCache) {
DMatrix* m;
// Add a cache that is immediately expired.
auto add_cache = [&]() {
auto *pp_dmat = CreateDMatrix(kRows, kCols, 0);
auto p_dmat = *pp_dmat;
auto p_dmat = RandomDataGenerator(kRows, kCols, 0).GenerateDMatix();
container.Cache(p_dmat, GenericParameter::kCpuId);
m = p_dmat.get();
delete pp_dmat;
};
add_cache();
@@ -42,8 +40,7 @@ void TestTrainingPrediction(size_t rows, std::string tree_method) {
std::unique_ptr<Learner> learner;
auto train = [&](std::string predictor, HostDeviceVector<float>* out) {
auto pp_m = CreateDMatrix(rows, kCols, 0);
auto p_m = *pp_m;
auto p_m = RandomDataGenerator(rows, kCols, 0).GenerateDMatix();
auto &h_label = p_m->Info().labels_.HostVector();
h_label.resize(rows);
@@ -64,7 +61,6 @@ void TestTrainingPrediction(size_t rows, std::string tree_method) {
learner->UpdateOneIter(i, p_m);
}
learner->Predict(p_m, false, out);
delete pp_m;
};
// Alternate the predictor, CPU predictor can not use ellpack while GPU predictor can
// not use CPU histogram index. So it's guaranteed one of the following is not

View File

@@ -25,15 +25,13 @@ void TestPredictionFromGradientIndex(std::string name, size_t rows, int32_t bins
gbm::GBTreeModel model = CreateTestModel(&param, kClasses);
{
auto pp_ellpack = CreateDMatrix(rows, kCols, 0);
auto p_ellpack = *pp_ellpack;
auto p_ellpack = RandomDataGenerator(rows, kCols, 0).GenerateDMatix();
// Use same number of bins as rows.
for (auto const &page DMLC_ATTRIBUTE_UNUSED :
p_ellpack->GetBatches<Page>({0, static_cast<int32_t>(bins), 0})) {
}
auto pp_precise = CreateDMatrix(rows, kCols, 0);
auto p_precise = *pp_precise;
auto p_precise = RandomDataGenerator(rows, kCols, 0).GenerateDMatix();
PredictionCacheEntry approx_out_predictions;
predictor->PredictBatch(p_ellpack.get(), &approx_out_predictions, model, 0);
@@ -45,21 +43,16 @@ void TestPredictionFromGradientIndex(std::string name, size_t rows, int32_t bins
CHECK_EQ(approx_out_predictions.predictions.HostVector()[i],
precise_out_predictions.predictions.HostVector()[i]);
}
delete pp_precise;
delete pp_ellpack;
}
{
// Predictor should never try to create the histogram index by itself. As only
// histogram index from training data is valid and predictor doesn't known which
// matrix is used for training.
auto pp_dmat = CreateDMatrix(rows, kCols, 0);
auto p_dmat = *pp_dmat;
auto p_dmat = RandomDataGenerator(rows, kCols, 0).GenerateDMatix();
PredictionCacheEntry precise_out_predictions;
predictor->PredictBatch(p_dmat.get(), &precise_out_predictions, model, 0);
ASSERT_FALSE(p_dmat->PageExists<Page>());
delete pp_dmat;
}
}