[GPU-Plugin] Add GPU accelerated prediction (#2593)
* [GPU-Plugin] Add GPU accelerated prediction * Improve allocation message * Update documentation * Resolve linker error for predictor * Add unit tests
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
#include "./helpers.h"
|
||||
#include "xgboost/c_api.h"
|
||||
#include <random>
|
||||
|
||||
std::string TempFileName() {
|
||||
return std::tmpnam(nullptr);
|
||||
@@ -60,3 +62,23 @@ xgboost::bst_float GetMetricEval(xgboost::Metric * metric,
|
||||
info.weights = weights;
|
||||
return metric->Eval(preds, info, false);
|
||||
}
|
||||
|
||||
std::shared_ptr<xgboost::DMatrix> CreateDMatrix(int rows, int columns,
|
||||
float sparsity, int seed) {
|
||||
const float missing_value = -1;
|
||||
std::vector<float> test_data(rows * columns);
|
||||
std::mt19937 gen(seed);
|
||||
std::uniform_real_distribution<float> dis(0.0f, 1.0f);
|
||||
for (auto &e : test_data) {
|
||||
if (dis(gen) < sparsity) {
|
||||
e = missing_value;
|
||||
} else {
|
||||
e = dis(gen);
|
||||
}
|
||||
}
|
||||
|
||||
DMatrixHandle handle;
|
||||
XGDMatrixCreateFromMat(test_data.data(), rows, columns, missing_value,
|
||||
&handle);
|
||||
return *static_cast<std::shared_ptr<xgboost::DMatrix> *>(handle);
|
||||
}
|
||||
|
||||
@@ -36,4 +36,19 @@ xgboost::bst_float GetMetricEval(
|
||||
std::vector<xgboost::bst_float> labels,
|
||||
std::vector<xgboost::bst_float> weights = std::vector<xgboost::bst_float> ());
|
||||
|
||||
/**
|
||||
* \fn std::shared_ptr<xgboost::DMatrix> CreateDMatrix(int rows, int columns, float sparsity, int seed);
|
||||
*
|
||||
* \brief Creates dmatrix with uniform random data between 0-1.
|
||||
*
|
||||
* \param rows The rows.
|
||||
* \param columns The columns.
|
||||
* \param sparsity The sparsity.
|
||||
* \param seed The seed.
|
||||
*
|
||||
* \return The new d matrix.
|
||||
*/
|
||||
|
||||
std::shared_ptr<xgboost::DMatrix> CreateDMatrix(int rows, int columns,
|
||||
float sparsity, int seed = 0);
|
||||
#endif
|
||||
|
||||
54
tests/cpp/predictor/test_cpu_predictor.cc
Normal file
54
tests/cpp/predictor/test_cpu_predictor.cc
Normal file
@@ -0,0 +1,54 @@
|
||||
// Copyright by Contributors
|
||||
#include <gtest/gtest.h>
|
||||
#include <xgboost/predictor.h>
|
||||
#include "../helpers.h"
|
||||
|
||||
namespace xgboost {
|
||||
TEST(cpu_predictor, Test) {
|
||||
std::unique_ptr<Predictor> cpu_predictor =
|
||||
std::unique_ptr<Predictor>(Predictor::Create("cpu_predictor"));
|
||||
|
||||
std::vector<std::unique_ptr<RegTree>> trees;
|
||||
trees.push_back(std::unique_ptr<RegTree>(new RegTree));
|
||||
trees.back()->InitModel();
|
||||
(*trees.back())[0].set_leaf(1.5f);
|
||||
gbm::GBTreeModel model(0.5);
|
||||
model.CommitModel(std::move(trees), 0);
|
||||
model.param.num_output_group = 1;
|
||||
model.base_margin = 0;
|
||||
|
||||
int n_row = 5;
|
||||
int n_col = 5;
|
||||
|
||||
auto dmat = CreateDMatrix(n_row, n_col, 0);
|
||||
|
||||
// Test predict batch
|
||||
std::vector<float> out_predictions;
|
||||
cpu_predictor->PredictBatch(dmat.get(), &out_predictions, model, 0);
|
||||
for (int i = 0; i < out_predictions.size(); i++) {
|
||||
ASSERT_EQ(out_predictions[i], 1.5);
|
||||
}
|
||||
|
||||
// Test predict instance
|
||||
auto batch = dmat->RowIterator()->Value();
|
||||
for (int i = 0; i < batch.size; i++) {
|
||||
std::vector<float> instance_out_predictions;
|
||||
cpu_predictor->PredictInstance(batch[i], &instance_out_predictions, model);
|
||||
ASSERT_EQ(instance_out_predictions[0], 1.5);
|
||||
}
|
||||
|
||||
// Test predict leaf
|
||||
std::vector<float> leaf_out_predictions;
|
||||
cpu_predictor->PredictLeaf(dmat.get(), &leaf_out_predictions, model);
|
||||
for (int i = 0; i < leaf_out_predictions.size(); i++) {
|
||||
ASSERT_EQ(leaf_out_predictions[i], 0);
|
||||
}
|
||||
|
||||
// Test predict contribution
|
||||
std::vector<float> out_contribution;
|
||||
cpu_predictor->PredictContribution(dmat.get(), &out_contribution, model);
|
||||
for (int i = 0; i < out_contribution.size(); i++) {
|
||||
ASSERT_EQ(out_contribution[i], 1.5);
|
||||
}
|
||||
}
|
||||
} // namespace xgboost
|
||||
14
tests/cpp/test_learner.cc
Normal file
14
tests/cpp/test_learner.cc
Normal file
@@ -0,0 +1,14 @@
|
||||
// Copyright by Contributors
|
||||
#include <gtest/gtest.h>
|
||||
#include "helpers.h"
|
||||
#include "xgboost/learner.h"
|
||||
|
||||
namespace xgboost {
|
||||
TEST(learner, Test) {
|
||||
typedef std::pair<std::string, std::string> arg;
|
||||
auto args = {arg("tree_method", "exact")};
|
||||
auto mat = {CreateDMatrix(10, 10, 0)};
|
||||
auto learner = std::unique_ptr<Learner>(Learner::Create(mat));
|
||||
learner->Configure(args);
|
||||
}
|
||||
} // namespace xgboost
|
||||
Reference in New Issue
Block a user