Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split up test helpers header. #5455

Merged
merged 1 commit into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tests/cpp/data/test_ellpack_page.cu
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <utility>

#include "../helpers.h"
#include "../histogram_helpers.h"
#include "gtest/gtest.h"

#include "../../../src/common/hist_util.h"
Expand Down
53 changes: 1 addition & 52 deletions tests/cpp/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@
#include <xgboost/base.h>
#include <xgboost/json.h>
#include <xgboost/generic_parameters.h>
#include <xgboost/c_api.h>

#include "../../src/common/common.h"
#include "../../src/gbm/gbtree_model.h"
#if defined(__CUDACC__)
#include "../../src/data/ellpack_page.cuh"
#endif

#if defined(__CUDACC__)
#define DeclareUnifiedTest(name) GPU ## name
Expand All @@ -42,6 +38,7 @@ namespace xgboost {
class ObjFunction;
class Metric;
struct LearnerModelParam;
class GradientBooster;
}

bool FileExists(const std::string& filename);
Expand Down Expand Up @@ -271,53 +268,5 @@ inline HostDeviceVector<GradientPair> GenerateRandomGradients(const size_t n_row
HostDeviceVector<GradientPair> gpair(h_gpair);
return gpair;
}

#if defined(__CUDACC__)
namespace {
class HistogramCutsWrapper : public common::HistogramCuts {
public:
using SuperT = common::HistogramCuts;
void SetValues(std::vector<float> cuts) {
SuperT::cut_values_.HostVector() = std::move(cuts);
}
void SetPtrs(std::vector<uint32_t> ptrs) {
SuperT::cut_ptrs_.HostVector() = std::move(ptrs);
}
void SetMins(std::vector<float> mins) {
SuperT::min_vals_.HostVector() = std::move(mins);
}
};
} // anonymous namespace

inline std::unique_ptr<EllpackPageImpl> BuildEllpackPage(
int n_rows, int n_cols, bst_float sparsity= 0) {
auto dmat = RandomDataGenerator(n_rows, n_cols, sparsity).Seed(3).GenerateDMatix();
const SparsePage& batch = *dmat->GetBatches<xgboost::SparsePage>().begin();

HistogramCutsWrapper cmat;
cmat.SetPtrs({0, 3, 6, 9, 12, 15, 18, 21, 24});
// 24 cut fields, 3 cut fields for each feature (column).
cmat.SetValues({0.30f, 0.67f, 1.64f,
0.32f, 0.77f, 1.95f,
0.29f, 0.70f, 1.80f,
0.32f, 0.75f, 1.85f,
0.18f, 0.59f, 1.69f,
0.25f, 0.74f, 2.00f,
0.26f, 0.74f, 1.98f,
0.26f, 0.71f, 1.83f});
cmat.SetMins({0.1f, 0.2f, 0.3f, 0.1f, 0.2f, 0.3f, 0.2f, 0.2f});

bst_row_t row_stride = 0;
const auto &offset_vec = batch.offset.ConstHostVector();
for (size_t i = 1; i < offset_vec.size(); ++i) {
row_stride = std::max(row_stride, offset_vec[i] - offset_vec[i-1]);
}

auto page = std::unique_ptr<EllpackPageImpl>(
new EllpackPageImpl(0, cmat, batch, dmat->IsDense(), row_stride));

return page;
}
#endif
} // namespace xgboost
#endif
53 changes: 53 additions & 0 deletions tests/cpp/histogram_helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#if defined(__CUDACC__)
#include "../../src/data/ellpack_page.cuh"
#endif

namespace xgboost {
#if defined(__CUDACC__)
namespace {
class HistogramCutsWrapper : public common::HistogramCuts {
public:
using SuperT = common::HistogramCuts;
void SetValues(std::vector<float> cuts) {
SuperT::cut_values_.HostVector() = std::move(cuts);
}
void SetPtrs(std::vector<uint32_t> ptrs) {
SuperT::cut_ptrs_.HostVector() = std::move(ptrs);
}
void SetMins(std::vector<float> mins) {
SuperT::min_vals_.HostVector() = std::move(mins);
}
};
} // anonymous namespace

inline std::unique_ptr<EllpackPageImpl> BuildEllpackPage(
int n_rows, int n_cols, bst_float sparsity= 0) {
auto dmat = RandomDataGenerator(n_rows, n_cols, sparsity).Seed(3).GenerateDMatix();
const SparsePage& batch = *dmat->GetBatches<xgboost::SparsePage>().begin();

HistogramCutsWrapper cmat;
cmat.SetPtrs({0, 3, 6, 9, 12, 15, 18, 21, 24});
// 24 cut fields, 3 cut fields for each feature (column).
cmat.SetValues({0.30f, 0.67f, 1.64f,
0.32f, 0.77f, 1.95f,
0.29f, 0.70f, 1.80f,
0.32f, 0.75f, 1.85f,
0.18f, 0.59f, 1.69f,
0.25f, 0.74f, 2.00f,
0.26f, 0.74f, 1.98f,
0.26f, 0.71f, 1.83f});
cmat.SetMins({0.1f, 0.2f, 0.3f, 0.1f, 0.2f, 0.3f, 0.2f, 0.2f});

bst_row_t row_stride = 0;
const auto &offset_vec = batch.offset.ConstHostVector();
for (size_t i = 1; i < offset_vec.size(); ++i) {
row_stride = std::max(row_stride, offset_vec[i] - offset_vec[i-1]);
}

auto page = std::unique_ptr<EllpackPageImpl>(
new EllpackPageImpl(0, cmat, batch, dmat->IsDense(), row_stride));

return page;
}
#endif
} // namespace xgboost
3 changes: 2 additions & 1 deletion tests/cpp/tree/test_gpu_hist.cu
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*!
* Copyright 2017-2020 XGBoost contributors
*/
#include <gtest/gtest.h>
#include <thrust/device_vector.h>
#include <dmlc/filesystem.h>
#include <xgboost/base.h>
Expand All @@ -9,7 +10,7 @@
#include <vector>

#include "../helpers.h"
#include "gtest/gtest.h"
#include "../histogram_helpers.h"

#include "xgboost/json.h"
#include "../../../src/data/sparse_page_source.h"
Expand Down