Copy data from Ellpack to GHist. (#8215)
This commit is contained in:
@@ -547,4 +547,15 @@ EllpackDeviceAccessor EllpackPageImpl::GetDeviceAccessor(
|
||||
NumSymbols()),
|
||||
feature_types};
|
||||
}
|
||||
EllpackDeviceAccessor EllpackPageImpl::GetHostAccessor(
|
||||
common::Span<FeatureType const> feature_types) const {
|
||||
return {Context::kCpuId,
|
||||
cuts_,
|
||||
is_dense,
|
||||
row_stride,
|
||||
base_rowid,
|
||||
n_rows,
|
||||
common::CompressedIterator<uint32_t>(gidx_buffer.ConstHostPointer(), NumSymbols()),
|
||||
feature_types};
|
||||
}
|
||||
} // namespace xgboost
|
||||
|
||||
@@ -43,12 +43,18 @@ struct EllpackDeviceAccessor {
|
||||
base_rowid(base_rowid),
|
||||
n_rows(n_rows) ,gidx_iter(gidx_iter),
|
||||
feature_types{feature_types} {
|
||||
cuts.cut_values_.SetDevice(device);
|
||||
cuts.cut_ptrs_.SetDevice(device);
|
||||
cuts.min_vals_.SetDevice(device);
|
||||
gidx_fvalue_map = cuts.cut_values_.ConstDeviceSpan();
|
||||
feature_segments = cuts.cut_ptrs_.ConstDeviceSpan();
|
||||
min_fvalue = cuts.min_vals_.ConstDeviceSpan();
|
||||
if (device == Context::kCpuId) {
|
||||
gidx_fvalue_map = cuts.cut_values_.ConstHostSpan();
|
||||
feature_segments = cuts.cut_ptrs_.ConstHostSpan();
|
||||
min_fvalue = cuts.min_vals_.ConstHostSpan();
|
||||
} else {
|
||||
cuts.cut_values_.SetDevice(device);
|
||||
cuts.cut_ptrs_.SetDevice(device);
|
||||
cuts.min_vals_.SetDevice(device);
|
||||
gidx_fvalue_map = cuts.cut_values_.ConstDeviceSpan();
|
||||
feature_segments = cuts.cut_ptrs_.ConstDeviceSpan();
|
||||
min_fvalue = cuts.min_vals_.ConstDeviceSpan();
|
||||
}
|
||||
}
|
||||
// Get a matrix element, uses binary search for look up Return NaN if missing
|
||||
// Given a row index and a feature index, returns the corresponding cut value
|
||||
@@ -202,6 +208,7 @@ class EllpackPageImpl {
|
||||
EllpackDeviceAccessor
|
||||
GetDeviceAccessor(int device,
|
||||
common::Span<FeatureType const> feature_types = {}) const;
|
||||
EllpackDeviceAccessor GetHostAccessor(common::Span<FeatureType const> feature_types = {}) const;
|
||||
|
||||
private:
|
||||
/*!
|
||||
|
||||
@@ -53,7 +53,7 @@ GHistIndexMatrix::GHistIndexMatrix(DMatrix *p_fmat, bst_bin_t max_bins_per_feat,
|
||||
// hist
|
||||
CHECK(!sorted_sketch);
|
||||
for (auto const &page : p_fmat->GetBatches<SparsePage>()) {
|
||||
this->columns_->Init(page, *this, sparse_thresh, n_threads);
|
||||
this->columns_->InitFromSparse(page, *this, sparse_thresh, n_threads);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -66,6 +66,12 @@ GHistIndexMatrix::GHistIndexMatrix(MetaInfo const &info, common::HistogramCuts &
|
||||
max_num_bins(max_bin_per_feat),
|
||||
isDense_{info.num_col_ * info.num_row_ == info.num_nonzero_} {}
|
||||
|
||||
#if !defined(XGBOOST_USE_CUDA)
|
||||
GHistIndexMatrix::GHistIndexMatrix(Context const *, MetaInfo const &, EllpackPage const &,
|
||||
BatchParam const &) {
|
||||
common::AssertGPUSupport();
|
||||
}
|
||||
#endif // defined(XGBOOST_USE_CUDA)
|
||||
|
||||
GHistIndexMatrix::~GHistIndexMatrix() = default;
|
||||
|
||||
@@ -99,7 +105,7 @@ GHistIndexMatrix::GHistIndexMatrix(SparsePage const &batch, common::Span<Feature
|
||||
this->PushBatch(batch, ft, n_threads);
|
||||
this->columns_ = std::make_unique<common::ColumnMatrix>();
|
||||
if (!std::isnan(sparse_thresh)) {
|
||||
this->columns_->Init(batch, *this, sparse_thresh, n_threads);
|
||||
this->columns_->InitFromSparse(batch, *this, sparse_thresh, n_threads);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
111
src/data/gradient_index.cu
Normal file
111
src/data/gradient_index.cu
Normal file
@@ -0,0 +1,111 @@
|
||||
/*!
|
||||
* Copyright 2022 by XGBoost Contributors
|
||||
*/
|
||||
#include <memory> // std::unique_ptr
|
||||
|
||||
#include "../common/column_matrix.h"
|
||||
#include "../common/hist_util.h" // Index
|
||||
#include "ellpack_page.cuh"
|
||||
#include "gradient_index.h"
|
||||
#include "xgboost/data.h"
|
||||
|
||||
namespace xgboost {
|
||||
// Similar to GHistIndexMatrix::SetIndexData, but without the need for adaptor or bin
|
||||
// searching. Is there a way to unify the code?
|
||||
template <typename BinT, typename CompressOffset>
|
||||
void SetIndexData(Context const* ctx, EllpackPageImpl const* page,
|
||||
std::vector<size_t>* p_hit_count_tloc, CompressOffset&& get_offset,
|
||||
GHistIndexMatrix* out) {
|
||||
auto accessor = page->GetHostAccessor();
|
||||
auto const kNull = static_cast<bst_bin_t>(accessor.NullValue());
|
||||
|
||||
common::Span<BinT> index_data_span = {out->index.data<BinT>(), out->index.Size()};
|
||||
auto n_bins_total = page->Cuts().TotalBins();
|
||||
|
||||
auto& hit_count_tloc = *p_hit_count_tloc;
|
||||
hit_count_tloc.clear();
|
||||
hit_count_tloc.resize(ctx->Threads() * n_bins_total, 0);
|
||||
|
||||
common::ParallelFor(page->Size(), ctx->Threads(), [&](auto i) {
|
||||
auto tid = omp_get_thread_num();
|
||||
size_t in_rbegin = page->row_stride * i;
|
||||
size_t out_rbegin = out->row_ptr[i];
|
||||
auto r_size = out->row_ptr[i + 1] - out->row_ptr[i];
|
||||
for (size_t j = 0; j < r_size; ++j) {
|
||||
auto bin_idx = accessor.gidx_iter[in_rbegin + j];
|
||||
assert(bin_idx != kNull);
|
||||
index_data_span[out_rbegin + j] = get_offset(bin_idx, j);
|
||||
++hit_count_tloc[tid * n_bins_total + bin_idx];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void GetRowPtrFromEllpack(Context const* ctx, EllpackPageImpl const* page,
|
||||
std::vector<size_t>* p_out) {
|
||||
auto& row_ptr = *p_out;
|
||||
row_ptr.resize(page->Size() + 1, 0);
|
||||
if (page->is_dense) {
|
||||
std::fill(row_ptr.begin() + 1, row_ptr.end(), page->row_stride);
|
||||
} else {
|
||||
auto accessor = page->GetHostAccessor();
|
||||
auto const kNull = static_cast<bst_bin_t>(accessor.NullValue());
|
||||
|
||||
common::ParallelFor(page->Size(), ctx->Threads(), [&](auto i) {
|
||||
size_t ibegin = page->row_stride * i;
|
||||
for (size_t j = 0; j < page->row_stride; ++j) {
|
||||
bst_bin_t bin_idx = accessor.gidx_iter[ibegin + j];
|
||||
if (bin_idx != kNull) {
|
||||
row_ptr[i + 1]++;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
std::partial_sum(row_ptr.begin(), row_ptr.end(), row_ptr.begin());
|
||||
}
|
||||
|
||||
GHistIndexMatrix::GHistIndexMatrix(Context const* ctx, MetaInfo const& info,
|
||||
EllpackPage const& in_page, BatchParam const& p)
|
||||
: max_num_bins{p.max_bin} {
|
||||
auto page = in_page.Impl();
|
||||
isDense_ = page->is_dense;
|
||||
|
||||
CHECK_EQ(info.num_row_, in_page.Size());
|
||||
|
||||
this->cut = page->Cuts();
|
||||
// pull to host early, prevent race condition
|
||||
this->cut.Ptrs();
|
||||
this->cut.Values();
|
||||
this->cut.MinValues();
|
||||
|
||||
this->ResizeIndex(info.num_nonzero_, page->is_dense);
|
||||
if (page->is_dense) {
|
||||
this->index.SetBinOffset(page->Cuts().Ptrs());
|
||||
}
|
||||
|
||||
auto n_bins_total = page->Cuts().TotalBins();
|
||||
GetRowPtrFromEllpack(ctx, page, &this->row_ptr);
|
||||
if (page->is_dense) {
|
||||
common::DispatchBinType(this->index.GetBinTypeSize(), [&](auto dtype) {
|
||||
using T = decltype(dtype);
|
||||
::xgboost::SetIndexData<T>(ctx, page, &hit_count_tloc_, index.MakeCompressor<T>(), this);
|
||||
});
|
||||
} else {
|
||||
// no compression
|
||||
::xgboost::SetIndexData<uint32_t>(
|
||||
ctx, page, &hit_count_tloc_, [&](auto bin_idx, auto) { return bin_idx; }, this);
|
||||
}
|
||||
|
||||
this->hit_count.resize(n_bins_total, 0);
|
||||
this->GatherHitCount(ctx->Threads(), n_bins_total);
|
||||
|
||||
// sanity checks
|
||||
CHECK_EQ(this->Features(), info.num_col_);
|
||||
CHECK_EQ(this->Size(), info.num_row_);
|
||||
CHECK(this->cut.cut_ptrs_.HostCanRead());
|
||||
CHECK(this->cut.cut_values_.HostCanRead());
|
||||
CHECK(this->cut.min_vals_.HostCanRead());
|
||||
|
||||
this->columns_ = std::make_unique<common::ColumnMatrix>(*this, p.sparse_thresh);
|
||||
this->columns_->InitFromGHist(ctx, *this);
|
||||
}
|
||||
} // namespace xgboost
|
||||
@@ -69,7 +69,7 @@ class GHistIndexMatrix {
|
||||
if (is_valid(elem)) {
|
||||
bst_bin_t bin_idx{-1};
|
||||
if (common::IsCat(ft, elem.column_idx)) {
|
||||
bin_idx = cut.SearchCatBin(elem.value, elem.column_idx);
|
||||
bin_idx = cut.SearchCatBin(elem.value, elem.column_idx, ptrs, values);
|
||||
} else {
|
||||
bin_idx = cut.SearchBin(elem.value, elem.column_idx, ptrs, values);
|
||||
}
|
||||
@@ -81,6 +81,17 @@ class GHistIndexMatrix {
|
||||
});
|
||||
}
|
||||
|
||||
// Gather hit_count from all threads
|
||||
void GatherHitCount(int32_t n_threads, bst_bin_t n_bins_total) {
|
||||
CHECK_EQ(hit_count.size(), n_bins_total);
|
||||
common::ParallelFor(n_bins_total, n_threads, [&](bst_omp_uint idx) {
|
||||
for (int32_t tid = 0; tid < n_threads; ++tid) {
|
||||
hit_count[idx] += hit_count_tloc_[tid * n_bins_total + idx];
|
||||
hit_count_tloc_[tid * n_bins_total + idx] = 0; // reset for next batch
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
template <typename Batch, typename IsValid>
|
||||
void PushBatchImpl(int32_t n_threads, Batch const& batch, size_t rbegin, IsValid&& is_valid,
|
||||
common::Span<FeatureType const> ft) {
|
||||
@@ -95,33 +106,20 @@ class GHistIndexMatrix {
|
||||
if (isDense_) {
|
||||
index.SetBinOffset(cut.Ptrs());
|
||||
}
|
||||
uint32_t const* offsets = index.Offset();
|
||||
if (isDense_) {
|
||||
// Inside the lambda functions, bin_idx is the index for cut value across all
|
||||
// features. By subtracting it with starting pointer of each feature, we can reduce
|
||||
// it to smaller value and compress it to smaller types.
|
||||
common::DispatchBinType(index.GetBinTypeSize(), [&](auto dtype) {
|
||||
using T = decltype(dtype);
|
||||
common::Span<T> index_data_span = {index.data<T>(), index.Size()};
|
||||
SetIndexData(
|
||||
index_data_span, rbegin, ft, batch_threads, batch, is_valid, n_bins_total,
|
||||
[offsets](auto bin_idx, auto fidx) { return static_cast<T>(bin_idx - offsets[fidx]); });
|
||||
SetIndexData(index_data_span, rbegin, ft, batch_threads, batch, is_valid, n_bins_total,
|
||||
index.MakeCompressor<T>());
|
||||
});
|
||||
} else {
|
||||
/* For sparse DMatrix we have to store index of feature for each bin
|
||||
in index field to chose right offset. So offset is nullptr and index is
|
||||
not reduced */
|
||||
common::Span<uint32_t> index_data_span = {index.data<uint32_t>(), n_index};
|
||||
// no compression
|
||||
SetIndexData(index_data_span, rbegin, ft, batch_threads, batch, is_valid, n_bins_total,
|
||||
[](auto idx, auto) { return idx; });
|
||||
}
|
||||
|
||||
common::ParallelFor(n_bins_total, n_threads, [&](bst_omp_uint idx) {
|
||||
for (int32_t tid = 0; tid < n_threads; ++tid) {
|
||||
hit_count[idx] += hit_count_tloc_[tid * n_bins_total + idx];
|
||||
hit_count_tloc_[tid * n_bins_total + idx] = 0; // reset for next batch
|
||||
}
|
||||
});
|
||||
this->GatherHitCount(n_threads, n_bins_total);
|
||||
}
|
||||
|
||||
public:
|
||||
@@ -129,12 +127,12 @@ class GHistIndexMatrix {
|
||||
std::vector<size_t> row_ptr;
|
||||
/*! \brief The index data */
|
||||
common::Index index;
|
||||
/*! \brief hit count of each index */
|
||||
/*! \brief hit count of each index, used for constructing the ColumnMatrix */
|
||||
std::vector<size_t> hit_count;
|
||||
/*! \brief The corresponding cuts */
|
||||
common::HistogramCuts cut;
|
||||
/*! \brief max_bin for each feature. */
|
||||
size_t max_num_bins;
|
||||
bst_bin_t max_num_bins;
|
||||
/*! \brief base row index for current page (used by external memory) */
|
||||
size_t base_rowid{0};
|
||||
|
||||
@@ -149,6 +147,13 @@ class GHistIndexMatrix {
|
||||
* for push batch.
|
||||
*/
|
||||
GHistIndexMatrix(MetaInfo const& info, common::HistogramCuts&& cuts, bst_bin_t max_bin_per_feat);
|
||||
/**
|
||||
* \brief Constructor fro Iterative DMatrix where we might copy an existing ellpack page
|
||||
* to host gradient index.
|
||||
*/
|
||||
GHistIndexMatrix(Context const* ctx, MetaInfo const& info, EllpackPage const& page,
|
||||
BatchParam const& p);
|
||||
|
||||
/**
|
||||
* \brief Constructor for external memory.
|
||||
*/
|
||||
|
||||
@@ -205,12 +205,11 @@ void IterativeDMatrix::InitFromCPU(DataIterHandle iter_handle, float missing,
|
||||
|
||||
BatchSet<GHistIndexMatrix> IterativeDMatrix::GetGradientIndex(BatchParam const& param) {
|
||||
CheckParam(param);
|
||||
CHECK(ghist_) << R"(`QuantileDMatrix` is not initialized with CPU data but used for CPU training.
|
||||
Possible solutions:
|
||||
- Use `DMatrix` instead.
|
||||
- Use CPU input for `QuantileDMatrix`.
|
||||
- Run training on GPU.
|
||||
)";
|
||||
if (!ghist_) {
|
||||
CHECK(ellpack_);
|
||||
ghist_ = std::make_shared<GHistIndexMatrix>(&ctx_, Info(), *ellpack_, param);
|
||||
}
|
||||
|
||||
auto begin_iter =
|
||||
BatchIterator<GHistIndexMatrix>(new SimpleBatchIteratorImpl<GHistIndexMatrix>(ghist_));
|
||||
return BatchSet<GHistIndexMatrix>(begin_iter);
|
||||
|
||||
@@ -29,20 +29,17 @@ namespace data {
|
||||
* `QuantileDMatrix` is an intermediate storage for quantilization results including
|
||||
* quantile cuts and histogram index. Quantilization is designed to be performed on stream
|
||||
* of data (or batches of it). As a result, the `QuantileDMatrix` is also designed to work
|
||||
* with batches of data. During initializaion, it will walk through the data multiple
|
||||
* times iteratively in order to perform quantilization. This design can help us reduce
|
||||
* memory usage significantly by avoiding data concatenation along with removing the CSR
|
||||
* matrix `SparsePage`. However, it has its limitation (can be fixed if needed):
|
||||
* with batches of data. During initializaion, it walks through the data multiple times
|
||||
* iteratively in order to perform quantilization. This design helps us reduce memory
|
||||
* usage significantly by avoiding data concatenation along with removing the CSR matrix
|
||||
* `SparsePage`. However, it has its limitation (can be fixed if needed):
|
||||
*
|
||||
* - It's only supported by hist tree method (both CPU and GPU) since approx requires a
|
||||
* re-calculation of quantiles for each iteration. We can fix this by retaining a
|
||||
* reference to the callback if there are feature requests.
|
||||
*
|
||||
* - The CPU format and the GPU format are different, the former uses a CSR + CSC for
|
||||
* histogram index while the latter uses only Ellpack. This results into a design that
|
||||
* we can obtain the GPU format from CPU but the other way around is not yet
|
||||
* supported. We can search the bin value from ellpack to recover the feature index when
|
||||
* we support copying data from GPU to CPU.
|
||||
* histogram index while the latter uses only Ellpack.
|
||||
*/
|
||||
class IterativeDMatrix : public DMatrix {
|
||||
MetaInfo info_;
|
||||
|
||||
Reference in New Issue
Block a user