7 #ifndef XGBOOST_DATA_H_
8 #define XGBOOST_DATA_H_
10 #include <dmlc/base.h>
12 #include <dmlc/serializer.h>
47 using Shape = std::array<SizeType, 2>;
180 void GetFeatureInfo(
const char* field, std::vector<std::string>* out_str_vecs)
const;
230 [[nodiscard]] CatContainer
const*
Cats()
const;
231 [[nodiscard]] CatContainer*
Cats();
232 [[nodiscard]] std::shared_ptr<CatContainer const>
CatsShared()
const;
236 void Cats(std::shared_ptr<CatContainer> cats);
243 mutable std::vector<size_t> label_order_cache_;
244 bool has_categorical_{
false};
246 std::shared_ptr<CatContainer> cats_;
267 return (this->index == other.
index && this->fvalue == other.
fvalue);
344 p.forbid_regen =
false;
404 offset_vec.push_back(0);
405 data.HostVector().clear();
424 void Reindex(uint64_t feature_offset, int32_t n_threads);
438 template <
typename AdapterBatchT>
439 bst_idx_t Push(AdapterBatchT
const& batch,
float missing, std::int32_t nthread);
465 std::shared_ptr<SparsePage const>
page;
476 class GHistIndexMatrix;
478 template <
typename T>
485 [[nodiscard]]
virtual bool AtEnd()
const = 0;
486 virtual std::shared_ptr<T const>
Page()
const = 0;
489 template <
typename T>
497 CHECK(impl_ !=
nullptr);
503 CHECK(impl_ !=
nullptr);
510 CHECK(impl_ !=
nullptr);
511 return impl_->AtEnd();
514 [[nodiscard]] std::shared_ptr<T const>
Page()
const {
return impl_->Page(); }
517 std::shared_ptr<BatchIteratorImpl<T>> impl_;
520 template <
typename T>
531 struct XGBAPIThreadLocalEntry;
583 virtual void SetInfo(
const char* key, std::string
const& interface_str) {
584 auto const& ctx = *this->
Ctx();
601 template <
typename T>
603 template <
typename T>
605 template <
typename T>
607 template <
typename T>
616 [[nodiscard]]
virtual std::int32_t
NumBatches()
const {
return 1; }
648 template <
typename AdapterT>
650 const std::string& cache_prefix =
"",
676 std::int32_t nthread,
bst_bin_t max_bin);
725 [[nodiscard]] CatContainer
const*
Cats()
const {
return this->
CatsShared().get(); }
726 [[nodiscard]] std::shared_ptr<CatContainer const>
CatsShared()
const {
750 inline bool DMatrix::PageExists<EllpackPage>()
const {
755 inline bool DMatrix::PageExists<GHistIndexMatrix>()
const {
760 inline bool DMatrix::PageExists<SparsePage>()
const {
790 inline BatchSet<ExtSparsePage>
DMatrix::GetBatches(Context
const* ctx, BatchParam
const& param) {
800 namespace serializer {
805 strm->Write(data.
index);
810 return strm->Read(&data->
index) && strm->Read(&data->
fvalue);
Defines configuration macros and basic types for xgboost.
#define XGBOOST_DEVICE
Tag function as usable by device.
Definition: base.h:57
virtual BatchIteratorImpl & operator++()=0
std::forward_iterator_tag iterator_category
Definition: data.h:481
virtual std::shared_ptr< T const > Page() const =0
virtual bool AtEnd() const =0
virtual const T & operator*() const =0
virtual ~BatchIteratorImpl()=default
BatchIterator(std::shared_ptr< BatchIteratorImpl< T >> impl)
Definition: data.h:494
std::forward_iterator_tag iterator_category
Definition: data.h:492
BatchIterator(BatchIteratorImpl< T > *impl)
Definition: data.h:493
const T & operator*() const
Definition: data.h:502
std::shared_ptr< T const > Page() const
Definition: data.h:514
BatchIterator & operator++()
Definition: data.h:496
bool operator!=(const BatchIterator &) const
Definition: data.h:507
bool AtEnd() const
Definition: data.h:509
BatchSet(BatchIterator< T > begin_iter)
Definition: data.h:523
BatchIterator< T > begin()
Definition: data.h:524
BatchIterator< T > end()
Definition: data.h:525
CSCPage()
Definition: data.h:455
CSCPage(SparsePage page)
Definition: data.h:456
Internal data structured used by XGBoost to hold all external data.
Definition: data.h:577
static DMatrix * Create(DataIterHandle iter, DMatrixHandle proxy, std::shared_ptr< DMatrix > ref, DataIterResetCallback *reset, XGDMatrixCallbackNext *next, bst_bin_t max_bin, ExtMemConfig const &config)
Create an external memory quantile DMatrix with callbacks.
CatContainer const * Cats() const
Accessor for the string representation of the categories.
Definition: data.h:725
virtual BatchSet< EllpackPage > GetEllpackBatches(Context const *ctx, BatchParam const ¶m)=0
static DMatrix * Load(const std::string &uri, bool silent=true, DataSplitMode data_split_mode=DataSplitMode::kRow)
Load DMatrix from URI.
virtual BatchSet< SparsePage > GetRowBatches()=0
virtual BatchSet< GHistIndexMatrix > GetGradientIndex(Context const *ctx, BatchParam const ¶m)=0
static DMatrix * Create(DataIterHandle iter, DMatrixHandle proxy, DataIterResetCallback *reset, XGDMatrixCallbackNext *next, ExtMemConfig const &config)
Create an external memory DMatrix with callbacks.
virtual void SetInfo(const char *key, std::string const &interface_str)
Definition: data.h:583
virtual BatchSet< ExtSparsePage > GetExtBatches(Context const *ctx, BatchParam const ¶m)=0
static DMatrix * Create(DataIterHandle iter, DMatrixHandle proxy, std::shared_ptr< DMatrix > ref, DataIterResetCallback *reset, XGDMatrixCallbackNext *next, float missing, std::int32_t nthread, bst_bin_t max_bin)
Create a new Quantile based DMatrix used for histogram based algorithm.
BatchSet< T > GetBatches(Context const *ctx)
virtual MetaInfo & Info()=0
meta information of the dataset
static DMatrix * Create(AdapterT *adapter, float missing, int nthread, const std::string &cache_prefix="", DataSplitMode data_split_mode=DataSplitMode::kRow)
Creates a new DMatrix from an external data adapter.
virtual DMatrix * SliceCol(int num_slices, int slice_id)=0
Slice a DMatrix by columns.
virtual bool GHistIndexExists() const =0
XGBAPIThreadLocalEntry & GetThreadLocal() const
Get thread local memory for returning data from DMatrix.
virtual bool SparsePageExists() const =0
virtual DMatrix * Slice(common::Span< int32_t const > ridxs)=0
virtual Context const * Ctx() const =0
Get the context object of this DMatrix. The context is created during construction of DMatrix with us...
BatchSet< T > GetBatches()
Gets batches. Use range based for loop over BatchSet to access individual batches.
virtual const MetaInfo & Info() const =0
meta information of the dataset
virtual bool EllpackExists() const =0
bool SingleColBlock() const
Definition: data.h:615
virtual BatchSet< CSCPage > GetColumnBatches(Context const *ctx)=0
virtual BatchSet< SortedCSCPage > GetSortedColumnBatches(Context const *ctx)=0
BatchSet< T > GetBatches(Context const *ctx, const BatchParam ¶m)
std::shared_ptr< CatContainer const > CatsShared() const
Definition: data.h:726
virtual std::int32_t NumBatches() const
Definition: data.h:616
bool IsDense() const
Whether the matrix is dense.
Definition: data.h:623
DMatrix()=default
default constructor
Sparse page for exporting DMatrix. Same as SparsePage, just a different type to prevent being used in...
Definition: data.h:463
ExtSparsePage(std::shared_ptr< SparsePage const > p)
Definition: data.h:466
std::shared_ptr< SparsePage const > page
Definition: data.h:465
common::Span< T const > ConstHostSpan() const
Definition: host_device_vector.h:118
std::vector< T > & HostVector()
Data structure representing JSON format.
Definition: json.h:396
SortedCSCPage(SparsePage page)
Definition: data.h:472
SortedCSCPage()
Definition: data.h:471
In-memory storage unit of sparse batch, stored in CSR format.
Definition: data.h:366
void Push(const SparsePage &batch)
Push a sparse page.
SparsePage()
constructor
Definition: data.h:383
SparsePage GetTranspose(int num_columns, int32_t n_threads) const
void SetBaseRowId(size_t row_id)
Set the base row id for this page.
Definition: data.h:409
void Reindex(uint64_t feature_offset, int32_t n_threads)
Reindex the column index with an offset.
void PushCSC(const SparsePage &batch)
Push a SparsePage stored in CSC format.
bool IsIndicesSorted(int32_t n_threads) const
Check wether the column index is sorted.
virtual ~SparsePage()=default
void SortIndices(int32_t n_threads)
Sort the column index.
HostDeviceVector< Entry > data
the data of the segments
Definition: data.h:371
HostSparsePageView GetView() const
Definition: data.h:378
SparsePage & operator=(SparsePage const &that)=delete
size_t MemCostBytes() const
Definition: data.h:395
void Clear()
clear the page
Definition: data.h:400
SparsePage(SparsePage const &that)=delete
size_t Size() const
Definition: data.h:392
void SortRows(int32_t n_threads)
bst_idx_t Push(AdapterBatchT const &batch, float missing, std::int32_t nthread)
Pushes external data batch onto this page.
HostDeviceVector< bst_idx_t > offset
Definition: data.h:369
SparsePage & operator=(SparsePage &&that)=default
SparsePage(SparsePage &&that)=default
size_t base_rowid
Definition: data.h:373
span class implementation, based on ISO++20 span<T>. The interface should be the same.
Definition: span.h:435
constexpr XGBOOST_DEVICE pointer data() const __span_noexcept
Definition: span.h:554
std::size_t index_type
Definition: span.h:439
constexpr XGBOOST_DEVICE index_type size() const __span_noexcept
Definition: span.h:559
std::size_t SizeType
Definition: linalg.h:282
DECLARE_FIELD_ENUM_CLASS(xgboost::DataSplitMode)
void * DMatrixHandle
Handle to the DMatrix.
Definition: c_api.h:49
int XGDMatrixCallbackNext(DataIterHandle iter)
Callback function prototype for getting next batch of data.
Definition: c_api.h:472
void * DataIterHandle
handle to a external data iterator
Definition: c_api.h:374
void DataIterResetCallback(DataIterHandle handle)
Callback function prototype for resetting the external iterator.
Definition: c_api.h:477
A device-and-host vector abstraction layer.
Linear algebra related utilities.
DMLC_DECLARE_TRAITS(is_pod, xgboost::Entry, true)
Definition: intrusive_ptr.h:207
Learner interface that integrates objective, gbm and evaluation together. This is the user facing XGB...
Definition: base.h:89
FeatureType
Definition: data.h:36
std::int32_t bst_bin_t
Type for histogram bin index. We sometimes use -1 to indicate invalid bin.
Definition: base.h:103
std::uint64_t bst_idx_t
Type for data row index (sample).
Definition: base.h:107
DataSplitMode
Definition: data.h:38
std::uint32_t bst_feature_t
Type for data column (feature) index.
Definition: base.h:99
DataType
data type accepted by xgboost interface
Definition: data.h:34
std::uint64_t bst_ulong
unsigned long integers
Definition: base.h:93
float bst_float
float type, used for storing statistics
Definition: base.h:95
static void Write(Stream *strm, const xgboost::Entry &data)
Definition: data.h:804
static bool Read(Stream *strm, xgboost::Entry *data)
Definition: data.h:809
Parameters for constructing histogram index batches.
Definition: data.h:274
bool forbid_regen
Forbid regenerating the gradient index. Used for internal validation.
Definition: data.h:291
bst_bin_t max_bin
Maximum number of bins per feature for histograms.
Definition: data.h:278
common::Span< float const > hess
Hessian, used for sketching with future approx implementation.
Definition: data.h:282
bool regen
Whether should we force DMatrix to regenerate the batch. Only used for GHistIndex.
Definition: data.h:287
bool ParamNotEqual(BatchParam const &other) const
Definition: data.h:324
BatchParam()=default
Exact or others that don't need histogram.
bool prefetch_copy
Used for GPU external memory. Whether to copy the data into device.
Definition: data.h:301
double sparse_thresh
Parameter used to generate column matrix for hist.
Definition: data.h:295
bool Initialized() const
Definition: data.h:335
BatchParam(bst_bin_t max_bin, common::Span< float const > hessian, bool regenerate)
Used by the approx tree method.
Definition: data.h:321
BatchParam MakeCache() const
Make a copy of self for DMatrix to describe how its existing index was generated.
Definition: data.h:339
BatchParam(bst_bin_t max_bin, double sparse_thresh)
Used by the hist tree method.
Definition: data.h:313
std::int32_t n_prefetch_batches
The number of batches to pre-fetch for external memory.
Definition: data.h:305
Runtime context for XGBoost. Contains information like threads and device.
Definition: context.h:142
A type for device ordinal. The type is packed into 32-bit for efficient use in viewing types like lin...
Definition: context.h:40
Element from a sparse vector.
Definition: data.h:250
XGBOOST_DEVICE Entry(bst_feature_t index, bst_float fvalue)
constructor with index and value
Definition: data.h:262
Entry()=default
default constructor
bst_feature_t index
feature index
Definition: data.h:252
static bool CmpIndex(Entry const &a, Entry const &b)
Definition: data.h:265
bst_float fvalue
feature value
Definition: data.h:254
bool operator==(const Entry &other) const
Definition: data.h:266
static bool CmpValue(const Entry &a, const Entry &b)
reversely compare feature values
Definition: data.h:264
ExtMemConfig(std::string cache, bool on_host, float h_ratio, std::int64_t min_cache, float missing, std::int32_t n_threads)
Definition: data.h:554
ExtMemConfig & SetParamsForTest(float _hw_decomp_ratio, bool _allow_decomp_fallback)
Definition: data.h:563
std::int32_t n_threads
Definition: data.h:547
std::int64_t min_cache_page_bytes
Definition: data.h:543
bool on_host
Definition: data.h:538
bool allow_decomp_fallback
Definition: data.h:551
float cache_host_ratio
Definition: data.h:540
float missing
Definition: data.h:545
std::string cache
Definition: data.h:536
float hw_decomp_ratio
Definition: data.h:549
size_t Size() const
Definition: data.h:360
Inst operator[](std::size_t i) const
Definition: data.h:355
common::Span< bst_idx_t const > offset
Definition: data.h:352
common::Span< Entry const > data
Definition: data.h:353
Definition: string_view.h:16
Used as a reference to a linalg::Matrix, or a vector.
Definition: data.h:44
DataType dtype
Definition: data.h:49
linalg::VectorView< float >::SizeType SizeType
Definition: data.h:45
SizeType ndim
Definition: data.h:51
Shape shape
Definition: data.h:50
SizeType Size() const
Definition: data.h:55
std::string ArrayInterfaceStr() const
std::array< SizeType, 2 > Shape
Definition: data.h:47
void const * data
Definition: data.h:52