Skip to content

Commit d9a4779

Browse files
trivialfishcho3
authored andcommitted
Fix CPU hist init for sparse dataset. (dmlc#4625)
* Fix CPU hist init for sparse dataset. * Implement sparse histogram cut. * Allow empty features. * Fix windows build, don't use sparse in distributed environment. * Comments. * Smaller threshold. * Fix windows omp. * Fix msvc lambda capture. * Fix MSVC macro. * Fix MSVC initialization list. * Fix MSVC initialization list x2. * Preserve categorical feature behavior. * Rename matrix to sparse cuts. * Reuse UseGroup. * Check for categorical data when adding cut. Co-Authored-By: Philip Hyunsu Cho <[email protected]> * Sanity check. * Fix comments. * Fix comment.
1 parent b7a1f22 commit d9a4779

33 files changed

+677
-295
lines changed

.clang-tidy

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Checks: 'modernize-*,-modernize-make-*,-modernize-use-auto,-modernize-raw-string-literal,google-*,-google-default-arguments,-clang-diagnostic-#pragma-messages,readability-identifier-naming'
1+
Checks: 'modernize-*,-modernize-make-*,-modernize-use-auto,-modernize-raw-string-literal,-modernize-avoid-c-arrays,google-*,-google-default-arguments,-clang-diagnostic-#pragma-messages,readability-identifier-naming'
22
CheckOptions:
33
- { key: readability-identifier-naming.ClassCase, value: CamelCase }
44
- { key: readability-identifier-naming.StructCase, value: CamelCase }

include/xgboost/data.h

+1
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@ class DMatrix {
437437
bool load_row_split,
438438
const std::string& file_format = "auto",
439439
const size_t page_size = kPageSize);
440+
440441
/*!
441442
* \brief create a new DMatrix, by wrapping a row_iterator, and meta info.
442443
* \param source The source iterator of the data, the create function takes ownership of the source.

src/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ if (USE_CUDA)
5959

6060
# OpenMP is mandatory for cuda version
6161
find_package(OpenMP REQUIRED)
62-
target_compile_options(objxgboost PRIVATE
62+
target_compile_options(objxgboost PRIVATE
6363
$<$<COMPILE_LANGUAGE:CUDA>:-Xcompiler=${OpenMP_CXX_FLAGS}>
6464
)
6565

src/c_api/c_api.cc

+3-4
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,9 @@ class NativeDataIter : public dmlc::Parser<uint32_t> {
119119
}
120120

121121
bool Next() override {
122-
if ((*next_callback_)(
123-
data_handle_,
124-
XGBoostNativeDataIterSetData,
125-
this) != 0) {
122+
if ((*next_callback_)(data_handle_,
123+
XGBoostNativeDataIterSetData,
124+
this) != 0) {
126125
at_first_ = false;
127126
return true;
128127
} else {

src/common/column_matrix.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class ColumnMatrix {
7575
// construct column matrix from GHistIndexMatrix
7676
inline void Init(const GHistIndexMatrix& gmat,
7777
double sparse_threshold) {
78-
const int32_t nfeature = static_cast<int32_t>(gmat.cut.row_ptr.size() - 1);
78+
const int32_t nfeature = static_cast<int32_t>(gmat.cut.Ptrs().size() - 1);
7979
const size_t nrow = gmat.row_ptr.size() - 1;
8080

8181
// identify type of each column
@@ -85,7 +85,7 @@ class ColumnMatrix {
8585

8686
uint32_t max_val = std::numeric_limits<uint32_t>::max();
8787
for (int32_t fid = 0; fid < nfeature; ++fid) {
88-
CHECK_LE(gmat.cut.row_ptr[fid + 1] - gmat.cut.row_ptr[fid], max_val);
88+
CHECK_LE(gmat.cut.Ptrs()[fid + 1] - gmat.cut.Ptrs()[fid], max_val);
8989
}
9090

9191
gmat.GetFeatureCounts(&feature_counts_[0]);
@@ -123,7 +123,7 @@ class ColumnMatrix {
123123
// store least bin id for each feature
124124
index_base_.resize(nfeature);
125125
for (int32_t fid = 0; fid < nfeature; ++fid) {
126-
index_base_[fid] = gmat.cut.row_ptr[fid];
126+
index_base_[fid] = gmat.cut.Ptrs()[fid];
127127
}
128128

129129
// pre-fill index_ for dense columns
@@ -150,9 +150,9 @@ class ColumnMatrix {
150150
size_t fid = 0;
151151
for (size_t i = ibegin; i < iend; ++i) {
152152
const uint32_t bin_id = gmat.index[i];
153-
while (bin_id >= gmat.cut.row_ptr[fid + 1]) {
154-
++fid;
155-
}
153+
auto iter = std::upper_bound(gmat.cut.Ptrs().cbegin() + fid,
154+
gmat.cut.Ptrs().cend(), bin_id);
155+
fid = std::distance(gmat.cut.Ptrs().cbegin(), iter) - 1;
156156
if (type_[fid] == kDenseColumn) {
157157
uint32_t* begin = &index_[boundary_[fid].index_begin];
158158
begin[rid] = bin_id - index_base_[fid];

src/common/common.h

+5
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ inline std::string ToString(const T& data) {
7272
return os.str();
7373
}
7474

75+
template <typename T1, typename T2>
76+
XGBOOST_DEVICE T1 DivRoundUp(const T1 a, const T2 b) {
77+
return static_cast<T1>(std::ceil(static_cast<double>(a) / b));
78+
}
79+
7580
/*
7681
* Range iterator
7782
*/

src/common/config.h

+4-5
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ class ConfigParser {
3030
* \param path path to configuration file
3131
*/
3232
explicit ConfigParser(const std::string& path)
33-
: line_comment_regex_("^#"),
33+
: path_(path),
34+
line_comment_regex_("^#"),
3435
key_regex_(R"rx(^([^#"'=\r\n\t ]+)[\t ]*=)rx"),
3536
key_regex_escaped_(R"rx(^(["'])([^"'=\r\n]+)\1[\t ]*=)rx"),
3637
value_regex_(R"rx(^([^#"'=\r\n\t ]+)[\t ]*(?:#.*){0,1}$)rx"),
37-
value_regex_escaped_(R"rx(^(["'])([^"'=\r\n]+)\1[\t ]*(?:#.*){0,1}$)rx"),
38-
path_(path) {}
38+
value_regex_escaped_(R"rx(^(["'])([^"'=\r\n]+)\1[\t ]*(?:#.*){0,1}$)rx")
39+
{}
3940

4041
std::string LoadConfigFile(const std::string& path) {
4142
std::ifstream fin(path, std::ios_base::in | std::ios_base::binary);
@@ -77,8 +78,6 @@ class ConfigParser {
7778
content = NormalizeConfigEOL(content);
7879
std::stringstream ss { content };
7980
std::vector<std::pair<std::string, std::string>> results;
80-
char delimiter = '=';
81-
char comment = '#';
8281
std::string line;
8382
std::string key, value;
8483
// Loop over every line of the configuration file

src/common/device_helpers.cuh

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* Copyright 2017 XGBoost contributors
2+
* Copyright 2017-2019 XGBoost contributors
33
*/
44
#pragma once
55
#include <thrust/device_ptr.h>
@@ -183,11 +183,6 @@ __device__ void BlockFill(IterT begin, size_t n, ValueT value) {
183183
* Kernel launcher
184184
*/
185185

186-
template <typename T1, typename T2>
187-
T1 DivRoundUp(const T1 a, const T2 b) {
188-
return static_cast<T1>(ceil(static_cast<double>(a) / b));
189-
}
190-
191186
template <typename L>
192187
__global__ void LaunchNKernel(size_t begin, size_t end, L lambda) {
193188
for (auto i : GridStrideRange(begin, end)) {
@@ -211,7 +206,7 @@ inline void LaunchN(int device_idx, size_t n, cudaStream_t stream, L lambda) {
211206
safe_cuda(cudaSetDevice(device_idx));
212207

213208
const int GRID_SIZE =
214-
static_cast<int>(DivRoundUp(n, ITEMS_PER_THREAD * BLOCK_THREADS));
209+
static_cast<int>(xgboost::common::DivRoundUp(n, ITEMS_PER_THREAD * BLOCK_THREADS));
215210
LaunchNKernel<<<GRID_SIZE, BLOCK_THREADS, 0, stream>>>(static_cast<size_t>(0),
216211
n, lambda);
217212
}
@@ -619,7 +614,7 @@ struct CubMemory {
619614
if (this->IsAllocated()) {
620615
XGBDeviceAllocator<uint8_t> allocator;
621616
allocator.deallocate(thrust::device_ptr<uint8_t>(static_cast<uint8_t *>(d_temp_storage)),
622-
temp_storage_bytes);
617+
temp_storage_bytes);
623618
d_temp_storage = nullptr;
624619
temp_storage_bytes = 0;
625620
}
@@ -738,7 +733,7 @@ void SparseTransformLbs(int device_idx, dh::CubMemory *temp_memory,
738733
const int BLOCK_THREADS = 256;
739734
const int ITEMS_PER_THREAD = 1;
740735
const int TILE_SIZE = BLOCK_THREADS * ITEMS_PER_THREAD;
741-
auto num_tiles = dh::DivRoundUp(count + num_segments, BLOCK_THREADS);
736+
auto num_tiles = xgboost::common::DivRoundUp(count + num_segments, BLOCK_THREADS);
742737
CHECK(num_tiles < std::numeric_limits<unsigned int>::max());
743738

744739
temp_memory->LazyAllocate(sizeof(CoordinateT) * (num_tiles + 1));
@@ -1158,7 +1153,7 @@ class AllReducer {
11581153
};
11591154

11601155
/**
1161-
* \brief Synchronizes the device
1156+
* \brief Synchronizes the device
11621157
*
11631158
* \param device_id Identifier for the device.
11641159
*/

0 commit comments

Comments
 (0)