Skip to content

Commit d32132d

Browse files
authored
Remove min_val from quantiles (#12067)
1 parent 0219159 commit d32132d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+346
-557
lines changed

plugin/sycl/data/gradient_index.h

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
#ifndef PLUGIN_SYCL_DATA_GRADIENT_INDEX_H_
66
#define PLUGIN_SYCL_DATA_GRADIENT_INDEX_H_
77

8+
#include <sycl/sycl.hpp>
89
#include <vector>
910

10-
#include "../data.h"
1111
#include "../../src/common/hist_util.h"
12-
13-
#include <sycl/sycl.hpp>
12+
#include "../data.h"
1413

1514
namespace xgboost {
1615
namespace sycl {
@@ -22,52 +21,40 @@ using BinTypeSize = ::xgboost::common::BinTypeSize;
2221
* \brief Index data and offsets stored in USM buffers to provide access from device kernels
2322
*/
2423
struct Index {
25-
Index() {
26-
SetBinTypeSize(binTypeSize_);
27-
}
24+
Index() { SetBinTypeSize(binTypeSize_); }
2825
Index(const Index& i) = delete;
2926
Index& operator=(Index i) = delete;
3027
Index(Index&& i) = delete;
3128
Index& operator=(Index&& i) = delete;
3229
void SetBinTypeSize(BinTypeSize binTypeSize) {
3330
binTypeSize_ = binTypeSize;
34-
CHECK(binTypeSize == BinTypeSize::kUint8BinsTypeSize ||
31+
CHECK(binTypeSize == BinTypeSize::kUint8BinsTypeSize ||
3532
binTypeSize == BinTypeSize::kUint16BinsTypeSize ||
3633
binTypeSize == BinTypeSize::kUint32BinsTypeSize);
3734
}
38-
BinTypeSize GetBinTypeSize() const {
39-
return binTypeSize_;
40-
}
35+
BinTypeSize GetBinTypeSize() const { return binTypeSize_; }
4136

42-
template<typename T>
37+
template <typename T>
4338
T* data() {
4439
return reinterpret_cast<T*>(data_.Data());
4540
}
4641

47-
template<typename T>
42+
template <typename T>
4843
const T* data() const {
4944
return reinterpret_cast<const T*>(data_.DataConst());
5045
}
5146

52-
size_t Size() const {
53-
return data_.Size() / (binTypeSize_);
54-
}
47+
size_t Size() const { return data_.Size() / (binTypeSize_); }
5548

56-
void Resize(::sycl::queue* qu, const size_t nBytesData) {
57-
data_.Resize(qu, nBytesData);
58-
}
49+
void Resize(::sycl::queue* qu, const size_t nBytesData) { data_.Resize(qu, nBytesData); }
5950

60-
uint8_t* begin() const {
61-
return data_.Begin();
62-
}
51+
uint8_t* begin() const { return data_.Begin(); }
6352

64-
uint8_t* end() const {
65-
return data_.End();
66-
}
53+
uint8_t* end() const { return data_.End(); }
6754

6855
private:
6956
USMVector<uint8_t, MemoryType::on_device> data_;
70-
BinTypeSize binTypeSize_ {BinTypeSize::kUint8BinsTypeSize};
57+
BinTypeSize binTypeSize_{BinTypeSize::kUint8BinsTypeSize};
7158
};
7259

7360
/*!
@@ -84,20 +71,18 @@ struct GHistIndexMatrix {
8471

8572
USMVector<uint8_t, MemoryType::on_device> sort_buff;
8673
/*! \brief The corresponding cuts */
87-
xgboost::common::HistogramCuts cut;
74+
xgboost::common::HistogramCuts cut{0};
8875
size_t max_num_bins;
8976
size_t min_num_bins;
9077
size_t nbins;
9178
size_t nfeatures;
9279
size_t row_stride;
9380

9481
// Create a global histogram matrix based on a given DMatrix device wrapper
95-
void Init(::sycl::queue* qu, Context const * ctx,
96-
DMatrix *dmat, int max_num_bins);
82+
void Init(::sycl::queue* qu, Context const* ctx, DMatrix* dmat, int max_num_bins);
9783

9884
template <typename BinIdxType, bool isDense>
99-
void SetIndexData(::sycl::queue* qu, Context const * ctx, BinIdxType* index_data,
100-
DMatrix *dmat);
85+
void SetIndexData(::sycl::queue* qu, Context const* ctx, BinIdxType* index_data, DMatrix* dmat);
10186

10287
void ResizeIndex(::sycl::queue* qu, size_t n_index);
10388

@@ -111,9 +96,7 @@ struct GHistIndexMatrix {
11196
}
11297
}
11398
}
114-
inline bool IsDense() const {
115-
return isDense_;
116-
}
99+
inline bool IsDense() const { return isDense_; }
117100

118101
private:
119102
bool isDense_;

src/c_api/c_api.cc

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -970,41 +970,25 @@ void GetCutImpl(Context const *ctx, std::shared_ptr<DMatrix> p_m,
970970
auto &data = *p_data;
971971
for (auto const &page : p_m->GetBatches<Page>(ctx, {})) {
972972
auto const &cut = page.Cuts();
973-
974973
auto const &ptrs = cut.Ptrs();
974+
auto const &vals = cut.Values();
975+
auto ft = p_m->Info().feature_types.ConstHostSpan();
976+
975977
indptr.resize(ptrs.size());
978+
data.clear();
976979

977-
auto const &vals = cut.Values();
978-
auto const &mins = cut.MinValues();
980+
for (bst_feature_t fidx = 0; fidx < p_m->Info().num_col_; ++fidx) {
981+
indptr[fidx] = data.size();
979982

980-
bst_feature_t n_features = p_m->Info().num_col_;
981-
auto ft = p_m->Info().feature_types.ConstHostSpan();
982-
std::size_t n_categories = std::count_if(ft.cbegin(), ft.cend(),
983-
[](auto t) { return t == FeatureType::kCategorical; });
984-
data.resize(vals.size() + n_features - n_categories); // |vals| + |mins|
985-
std::size_t i{0}, n_numeric{0};
986-
for (bst_feature_t fidx = 0; fidx < n_features; ++fidx) {
987-
CHECK_LT(i, data.size());
988-
bool is_numeric = !common::IsCat(ft, fidx);
989-
if (is_numeric) {
990-
data[i] = mins[fidx];
991-
i++;
983+
if (!common::IsCat(ft, fidx)) {
984+
data.push_back(common::HistogramCuts::NumericBinLowerBound(ptrs, vals, fidx, ptrs[fidx]));
992985
}
986+
993987
auto beg = ptrs[fidx];
994988
auto end = ptrs[fidx + 1];
995-
CHECK_LE(end, data.size());
996-
std::copy(vals.cbegin() + beg, vals.cbegin() + end, data.begin() + i);
997-
i += (end - beg);
998-
// shift by min values.
999-
indptr[fidx] = ptrs[fidx] + n_numeric;
1000-
if (is_numeric) {
1001-
n_numeric++;
1002-
}
989+
data.insert(data.end(), vals.cbegin() + beg, vals.cbegin() + end);
1003990
}
1004-
CHECK_EQ(n_numeric, n_features - n_categories);
1005-
1006991
indptr.back() = data.size();
1007-
CHECK_EQ(indptr.back(), vals.size() + mins.size() - n_categories);
1008992
break;
1009993
}
1010994
}

src/common/hist_util.cc

Lines changed: 35 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,40 @@
1717
#include "xgboost/data.h" // for SparsePage, SortedCSCPage
1818

1919
#if defined(XGBOOST_MM_PREFETCH_PRESENT)
20-
#include <xmmintrin.h>
21-
#define PREFETCH_READ_T0(addr) _mm_prefetch(reinterpret_cast<const char*>(addr), _MM_HINT_T0)
20+
#include <xmmintrin.h>
21+
#define PREFETCH_READ_T0(addr) _mm_prefetch(reinterpret_cast<const char *>(addr), _MM_HINT_T0)
2222
#elif defined(XGBOOST_BUILTIN_PREFETCH_PRESENT)
23-
#define PREFETCH_READ_T0(addr) __builtin_prefetch(reinterpret_cast<const char*>(addr), 0, 3)
23+
#define PREFETCH_READ_T0(addr) __builtin_prefetch(reinterpret_cast<const char *>(addr), 0, 3)
2424
#else // no SW pre-fetching available; PREFETCH_READ_T0 is no-op
25-
#define PREFETCH_READ_T0(addr) do {} while (0)
25+
#define PREFETCH_READ_T0(addr) \
26+
do { \
27+
} while (0)
2628
#endif // defined(XGBOOST_MM_PREFETCH_PRESENT)
2729

2830
namespace xgboost::common {
29-
HistogramCuts::HistogramCuts() {
30-
cut_ptrs_.HostVector().emplace_back(0);
31-
}
31+
HistogramCuts::HistogramCuts(bst_feature_t n_features)
32+
: cut_ptrs_(static_cast<std::size_t>(n_features) + 1, 0) {}
3233

3334
void HistogramCuts::Save(common::AlignedFileWriteStream *fo) const {
3435
auto const &ptrs = this->Ptrs();
3536
CHECK_LE(Span{ptrs}.size_bytes(), WriteVec(fo, ptrs));
3637
auto const &vals = this->Values();
3738
CHECK_LE(Span{vals}.size_bytes(), WriteVec(fo, vals));
38-
auto const &mins = this->MinValues();
39-
CHECK_LE(Span{mins}.size_bytes(), WriteVec(fo, mins));
4039
CHECK_GE(fo->Write(has_categorical_), sizeof(has_categorical_));
4140
CHECK_GE(fo->Write(max_cat_), sizeof(max_cat_));
4241
}
4342

4443
[[nodiscard]] HistogramCuts *HistogramCuts::Load(common::AlignedResourceReadStream *fi) {
45-
auto p_cuts = new HistogramCuts;
44+
auto p_cuts = new HistogramCuts{0};
4645
CHECK(ReadVec(fi, &p_cuts->cut_ptrs_.HostVector()));
4746
CHECK(ReadVec(fi, &p_cuts->cut_values_.HostVector()));
48-
CHECK(ReadVec(fi, &p_cuts->min_vals_.HostVector()));
4947
CHECK(fi->Read(&p_cuts->has_categorical_));
5048
CHECK(fi->Read(&p_cuts->max_cat_));
5149
return p_cuts;
5250
}
5351

5452
HistogramCuts SketchOnDMatrix(Context const *ctx, DMatrix *m, bst_bin_t max_bins, bool use_sorted,
5553
Span<float const> hessian) {
56-
HistogramCuts out;
5754
auto const &info = m->Info();
5855
auto n_threads = ctx->Threads();
5956
std::vector<bst_idx_t> reduced(info.num_col_, 0);
@@ -73,20 +70,15 @@ HistogramCuts SketchOnDMatrix(Context const *ctx, DMatrix *m, bst_bin_t max_bins
7370
for (auto const &page : m->GetBatches<SparsePage>()) {
7471
container.PushRowPage(page, info, hessian);
7572
}
76-
container.MakeCuts(ctx, m->Info(), &out);
73+
return container.MakeCuts(ctx, m->Info());
7774
} else {
78-
SortedSketchContainer container{ctx,
79-
max_bins,
80-
m->Info().feature_types.ConstHostSpan(),
81-
reduced,
75+
SortedSketchContainer container{ctx, max_bins, m->Info().feature_types.ConstHostSpan(), reduced,
8276
HostSketchContainer::UseGroup(info)};
8377
for (auto const &page : m->GetBatches<SortedCSCPage>(ctx)) {
8478
container.PushColPage(page, info, hessian);
8579
}
86-
container.MakeCuts(ctx, m->Info(), &out);
80+
return container.MakeCuts(ctx, m->Info());
8781
}
88-
89-
return out;
9082
}
9183

9284
/*!
@@ -118,9 +110,9 @@ void CopyHist(GHistRow dst, const GHistRow src, size_t begin, size_t end) {
118110
*/
119111
void SubtractionHist(GHistRow dst, const GHistRow src1, const GHistRow src2, size_t begin,
120112
size_t end) {
121-
double* pdst = reinterpret_cast<double*>(dst.data());
122-
const double* psrc1 = reinterpret_cast<const double*>(src1.data());
123-
const double* psrc2 = reinterpret_cast<const double*>(src2.data());
113+
double *pdst = reinterpret_cast<double *>(dst.data());
114+
const double *psrc1 = reinterpret_cast<const double *>(src1.data());
115+
const double *psrc2 = reinterpret_cast<const double *>(src2.data());
124116

125117
for (size_t i = 2 * begin; i < 2 * end; ++i) {
126118
pdst[i] = psrc1[i] - psrc2[i];
@@ -134,13 +126,10 @@ struct Prefetch {
134126

135127
private:
136128
static constexpr size_t kNoPrefetchSize =
137-
kPrefetchOffset + kCacheLineSize /
138-
sizeof(decltype(GHistIndexMatrix::row_ptr)::value_type);
129+
kPrefetchOffset + kCacheLineSize / sizeof(decltype(GHistIndexMatrix::row_ptr)::value_type);
139130

140131
public:
141-
static size_t NoPrefetchSize(size_t rows) {
142-
return std::min(rows, kNoPrefetchSize);
143-
}
132+
static size_t NoPrefetchSize(size_t rows) { return std::min(rows, kNoPrefetchSize); }
144133

145134
template <typename T>
146135
static constexpr size_t GetPrefetchStep() {
@@ -156,9 +145,7 @@ struct RuntimeFlags {
156145
const BinTypeSize bin_type_size;
157146
};
158147

159-
template <bool _any_missing,
160-
bool _first_page = false,
161-
bool _read_by_column = false,
148+
template <bool _any_missing, bool _first_page = false, bool _read_by_column = false,
162149
typename BinIdxTypeName = uint8_t>
163150
class GHistBuildingManager {
164151
public:
@@ -192,7 +179,7 @@ class GHistBuildingManager {
192179
* and forward the call there.
193180
*/
194181
template <typename Fn>
195-
static void DispatchAndExecute(const RuntimeFlags& flags, Fn&& fn) {
182+
static void DispatchAndExecute(const RuntimeFlags &flags, Fn &&fn) {
196183
if (flags.first_page != kFirstPage) {
197184
SetFirstPage<true>::Type::DispatchAndExecute(flags, std::forward<Fn>(fn));
198185
} else if (flags.read_by_column != kReadByColumn) {
@@ -247,22 +234,19 @@ void RowsWiseBuildHistKernel(Span<GradientPair const> gpair, Span<bst_idx_t cons
247234
// to work with gradient pairs as a singe row FP array
248235

249236
for (std::size_t i = 0; i < size; ++i) {
250-
const size_t icol_start =
251-
kAnyMissing ? get_row_ptr(rid[i]) : get_rid(rid[i]) * n_features;
252-
const size_t icol_end =
253-
kAnyMissing ? get_row_ptr(rid[i] + 1) : icol_start + n_features;
237+
const size_t icol_start = kAnyMissing ? get_row_ptr(rid[i]) : get_rid(rid[i]) * n_features;
238+
const size_t icol_end = kAnyMissing ? get_row_ptr(rid[i] + 1) : icol_start + n_features;
254239

255240
const size_t row_size = icol_end - icol_start;
256241
const size_t idx_gh = two * rid[i];
257242

258243
if (do_prefetch) {
259244
const size_t icol_start_prefetch =
260-
kAnyMissing
261-
? get_row_ptr(rid[i + Prefetch::kPrefetchOffset])
262-
: get_rid(rid[i + Prefetch::kPrefetchOffset]) * n_features;
263-
const size_t icol_end_prefetch =
264-
kAnyMissing ? get_row_ptr(rid[i + Prefetch::kPrefetchOffset] + 1)
265-
: icol_start_prefetch + n_features;
245+
kAnyMissing ? get_row_ptr(rid[i + Prefetch::kPrefetchOffset])
246+
: get_rid(rid[i + Prefetch::kPrefetchOffset]) * n_features;
247+
const size_t icol_end_prefetch = kAnyMissing
248+
? get_row_ptr(rid[i + Prefetch::kPrefetchOffset] + 1)
249+
: icol_start_prefetch + n_features;
266250

267251
PREFETCH_READ_T0(p_gpair + two * rid[i + Prefetch::kPrefetchOffset]);
268252
for (size_t j = icol_start_prefetch; j < icol_end_prefetch;
@@ -301,7 +285,9 @@ void ColsWiseBuildHistKernel(Span<GradientPair const> gpair, Span<bst_idx_t cons
301285
auto get_row_ptr = [&](bst_idx_t ridx) {
302286
return kFirstPage ? row_ptr[ridx] : row_ptr[ridx - base_rowid];
303287
};
304-
auto get_rid = [&](bst_idx_t ridx) { return kFirstPage ? ridx : (ridx - base_rowid); };
288+
auto get_rid = [&](bst_idx_t ridx) {
289+
return kFirstPage ? ridx : (ridx - base_rowid);
290+
};
305291

306292
const size_t n_features = gmat.cut.Ptrs().size() - 1;
307293
const size_t n_columns = n_features;
@@ -314,10 +300,8 @@ void ColsWiseBuildHistKernel(Span<GradientPair const> gpair, Span<bst_idx_t cons
314300
const uint32_t offset = kAnyMissing ? 0 : offsets[cid];
315301
for (size_t i = 0; i < size; ++i) {
316302
const size_t row_id = rid[i];
317-
const size_t icol_start =
318-
kAnyMissing ? get_row_ptr(row_id) : get_rid(row_id) * n_features;
319-
const size_t icol_end =
320-
kAnyMissing ? get_row_ptr(rid[i] + 1) : icol_start + n_features;
303+
const size_t icol_start = kAnyMissing ? get_row_ptr(row_id) : get_rid(row_id) * n_features;
304+
const size_t icol_end = kAnyMissing ? get_row_ptr(rid[i] + 1) : icol_start + n_features;
321305

322306
if (cid < icol_end - icol_start) {
323307
const BinIdxType *gr_index_local = gradient_index + icol_start;
@@ -327,7 +311,7 @@ void ColsWiseBuildHistKernel(Span<GradientPair const> gpair, Span<bst_idx_t cons
327311
const size_t idx_gh = two * row_id;
328312
// The trick with pgh_t buffer helps the compiler to generate faster binary.
329313
const float pgh_t[] = {pgh[idx_gh], pgh[idx_gh + 1]};
330-
*(hist_local) += pgh_t[0];
314+
*(hist_local) += pgh_t[0];
331315
*(hist_local + 1) += pgh_t[1];
332316
}
333317
}
@@ -369,7 +353,7 @@ void BuildHistDispatch(Span<GradientPair const> gpair, Span<bst_idx_t const> row
369353
template <bool any_missing>
370354
void BuildHist(Span<GradientPair const> gpair, Span<bst_idx_t const> row_indices,
371355
const GHistIndexMatrix &gmat, GHistRow hist, bool read_by_column) {
372-
bool first_page = gmat.base_rowid == 0;;
356+
bool first_page = gmat.base_rowid == 0;
373357
auto bin_type_size = gmat.index.GetBinTypeSize();
374358

375359
GHistBuildingManager<any_missing>::DispatchAndExecute(
@@ -380,10 +364,8 @@ void BuildHist(Span<GradientPair const> gpair, Span<bst_idx_t const> row_indices
380364
}
381365

382366
template void BuildHist<true>(Span<GradientPair const> gpair, Span<bst_idx_t const> row_indices,
383-
const GHistIndexMatrix &gmat, GHistRow hist,
384-
bool read_by_column);
367+
const GHistIndexMatrix &gmat, GHistRow hist, bool read_by_column);
385368

386369
template void BuildHist<false>(Span<GradientPair const> gpair, Span<bst_idx_t const> row_indices,
387-
const GHistIndexMatrix &gmat, GHistRow hist,
388-
bool read_by_column);
370+
const GHistIndexMatrix &gmat, GHistRow hist, bool read_by_column);
389371
} // namespace xgboost::common

0 commit comments

Comments
 (0)