xgboost
data.h
Go to the documentation of this file.
1 
7 #ifndef XGBOOST_DATA_H_
8 #define XGBOOST_DATA_H_
9 
10 #include <dmlc/base.h>
11 #include <dmlc/io.h> // for Stream
12 #include <dmlc/serializer.h> // for Handler
13 #include <xgboost/base.h>
15 #include <xgboost/linalg.h>
16 #include <xgboost/span.h>
17 #include <xgboost/string_view.h>
18 
19 #include <algorithm>
20 #include <array> // for array
21 #include <cstdint> // for int32_t, uint8_t
22 #include <limits>
23 #include <memory>
24 #include <string>
25 #include <utility>
26 #include <vector>
27 
28 namespace xgboost {
29 // forward declare dmatrix.
30 class DMatrix;
31 struct Context;
32 
34 enum class DataType : uint8_t { kFloat32 = 1, kDouble = 2, kUInt32 = 3, kUInt64 = 4, kStr = 5 };
35 
36 enum class FeatureType : uint8_t { kNumerical = 0, kCategorical = 1 };
37 
38 enum class DataSplitMode : int { kRow = 0, kCol = 1 };
39 
40 // Forward declaration of the container used by the meta info.
41 class CatContainer;
42 
44 struct TypedArrayRef {
46  // 2-dim is the maximum for return type, we can use larger ones if needed.
47  using Shape = std::array<SizeType, 2>;
48 
50  Shape shape{0, 0};
52  void const* data{nullptr};
53 
54  [[nodiscard]] std::string ArrayInterfaceStr() const;
55  [[nodiscard]] SizeType Size() const {
56  if (ndim == 1) {
57  return shape[0];
58  } else {
59  return shape[0] * shape[1];
60  }
61  }
62 };
63 
67 class MetaInfo {
68  public:
70  static constexpr uint64_t kNumField = 13;
71 
73  bst_idx_t num_row_{0}; // NOLINT
75  uint64_t num_col_{0}; // NOLINT
77  uint64_t num_nonzero_{0}; // NOLINT
86  std::vector<bst_group_t> group_ptr_; // NOLINT
103 
107  std::vector<std::string> feature_type_names;
111  std::vector<std::string> feature_names;
112  /*
113  * \brief Type of each feature. Automatically set when feature_type_names is specifed.
114  */
116  /*
117  * \brief Weight of each feature, used to define the probability of each feature being
118  * selected when using column sampling.
119  */
121 
123  MetaInfo(MetaInfo&& that) = default;
124  MetaInfo(MetaInfo const& that) = delete;
125  MetaInfo& operator=(MetaInfo&& that) = default;
126  MetaInfo& operator=(MetaInfo const& that) = delete;
127 
131  void Validate(DeviceOrd device) const;
141 
142  MetaInfo Copy() const;
146  bool IsDense() const { return num_col_ * num_row_ == num_nonzero_; }
152  inline bst_float GetWeight(size_t i) const {
153  return weights_.Size() != 0 ? weights_.HostVector()[i] : 1.0f;
154  }
156  const std::vector<size_t>& LabelAbsSort(Context const* ctx) const;
158  void Clear();
163  void LoadBinary(dmlc::Stream* fi);
168  void SaveBinary(dmlc::Stream* fo) const;
175  void SetInfo(Context const& ctx, StringView key, StringView in_array);
177  [[nodiscard]] TypedArrayRef GetInfo(Context const* ctx, StringView key) const;
178 
179  void SetFeatureInfo(const char* key, const char** info, const bst_ulong size);
180  void GetFeatureInfo(const char* field, std::vector<std::string>* out_str_vecs) const;
181 
193  void Extend(MetaInfo const& that, bool accumulate_rows, bool check_column);
201  void SynchronizeNumberOfColumns(Context const* ctx, DataSplitMode split_mode);
202 
204  [[nodiscard]] bool IsRowSplit() const { return data_split_mode == DataSplitMode::kRow; }
206  [[nodiscard]] bool IsColumnSplit() const { return data_split_mode == DataSplitMode::kCol; }
208  [[nodiscard]] bool IsRanking() const { return !group_ptr_.empty(); }
209 
214  [[nodiscard]] bool IsVerticalFederated() const;
215 
222  bool ShouldHaveLabels() const;
226  bool HasCategorical() const { return has_categorical_; }
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);
237 
238  private:
239  void SetInfoFromHost(Context const* ctx, StringView key, Json arr);
240  void SetInfoFromCUDA(Context const* ctx, StringView key, Json arr);
241 
243  mutable std::vector<size_t> label_order_cache_;
244  bool has_categorical_{false};
245 
246  std::shared_ptr<CatContainer> cats_;
247 };
248 
250 struct Entry {
256  Entry() = default;
264  inline static bool CmpValue(const Entry& a, const Entry& b) { return a.fvalue < b.fvalue; }
265  static bool CmpIndex(Entry const& a, Entry const& b) { return a.index < b.index; }
266  inline bool operator==(const Entry& other) const {
267  return (this->index == other.index && this->fvalue == other.fvalue);
268  }
269 };
270 
274 struct BatchParam {
287  bool regen{false};
291  bool forbid_regen{false};
295  double sparse_thresh{std::numeric_limits<double>::quiet_NaN()};
301  bool prefetch_copy{true};
305  std::int32_t n_prefetch_batches{3};
309  BatchParam() = default;
322  : max_bin{max_bin}, hess{hessian}, regen{regenerate} {}
323 
324  [[nodiscard]] bool ParamNotEqual(BatchParam const& other) const {
325  // Check non-floating parameters.
326  bool cond = max_bin != other.max_bin;
327  // Check sparse thresh.
328  bool l_nan = std::isnan(sparse_thresh);
329  bool r_nan = std::isnan(other.sparse_thresh);
330  bool st_chg = (l_nan != r_nan) || (!l_nan && !r_nan && (sparse_thresh != other.sparse_thresh));
331  cond |= st_chg;
332 
333  return cond;
334  }
335  [[nodiscard]] bool Initialized() const { return max_bin != 0; }
339  [[nodiscard]] BatchParam MakeCache() const {
340  auto p = *this;
341  // These parameters have nothing to do with how the gradient index was generated in the
342  // first place.
343  p.regen = false;
344  p.forbid_regen = false;
345  return p;
346  }
347 };
348 
351 
354 
355  [[nodiscard]] Inst operator[](std::size_t i) const {
356  auto size = *(offset.data() + i + 1) - *(offset.data() + i);
357  return {data.data() + *(offset.data() + i), static_cast<Inst::index_type>(size)};
358  }
359 
360  [[nodiscard]] size_t Size() const { return offset.size() == 0 ? 0 : offset.size() - 1; }
361 };
362 
366 class SparsePage {
367  public:
368  // Offset for each row.
372 
373  size_t base_rowid{0};
374 
377 
378  [[nodiscard]] HostSparsePageView GetView() const {
379  return {offset.ConstHostSpan(), data.ConstHostSpan()};
380  }
381 
383  SparsePage() { this->Clear(); }
384 
385  SparsePage(SparsePage const& that) = delete;
386  SparsePage(SparsePage&& that) = default;
387  SparsePage& operator=(SparsePage const& that) = delete;
388  SparsePage& operator=(SparsePage&& that) = default;
389  virtual ~SparsePage() = default;
390 
392  [[nodiscard]] size_t Size() const { return offset.Size() == 0 ? 0 : offset.Size() - 1; }
393 
395  [[nodiscard]] size_t MemCostBytes() const {
396  return offset.Size() * sizeof(size_t) + data.Size() * sizeof(Entry);
397  }
398 
400  inline void Clear() {
401  base_rowid = 0;
402  auto& offset_vec = offset.HostVector();
403  offset_vec.clear();
404  offset_vec.push_back(0);
405  data.HostVector().clear();
406  }
407 
409  inline void SetBaseRowId(size_t row_id) { base_rowid = row_id; }
410 
411  [[nodiscard]] SparsePage GetTranspose(int num_columns, int32_t n_threads) const;
412 
416  void SortIndices(int32_t n_threads);
420  [[nodiscard]] bool IsIndicesSorted(int32_t n_threads) const;
424  void Reindex(uint64_t feature_offset, int32_t n_threads);
425 
426  void SortRows(int32_t n_threads);
427 
438  template <typename AdapterBatchT>
439  bst_idx_t Push(AdapterBatchT const& batch, float missing, std::int32_t nthread);
440 
445  void Push(const SparsePage& batch);
450  void PushCSC(const SparsePage& batch);
451 };
452 
453 class CSCPage : public SparsePage {
454  public:
456  explicit CSCPage(SparsePage page) : SparsePage(std::move(page)) {}
457 };
458 
464  public:
465  std::shared_ptr<SparsePage const> page;
466  explicit ExtSparsePage(std::shared_ptr<SparsePage const> p) : page{std::move(p)} {}
467 };
468 
469 class SortedCSCPage : public SparsePage {
470  public:
472  explicit SortedCSCPage(SparsePage page) : SparsePage(std::move(page)) {}
473 };
474 
475 class EllpackPage;
476 class GHistIndexMatrix;
477 
478 template <typename T>
480  public:
481  using iterator_category = std::forward_iterator_tag; // NOLINT
482  virtual ~BatchIteratorImpl() = default;
483  virtual const T& operator*() const = 0;
485  [[nodiscard]] virtual bool AtEnd() const = 0;
486  virtual std::shared_ptr<T const> Page() const = 0;
487 };
488 
489 template <typename T>
491  public:
492  using iterator_category = std::forward_iterator_tag; // NOLINT
493  explicit BatchIterator(BatchIteratorImpl<T>* impl) { impl_.reset(impl); }
494  explicit BatchIterator(std::shared_ptr<BatchIteratorImpl<T>> impl) { impl_ = impl; }
495 
497  CHECK(impl_ != nullptr);
498  ++(*impl_);
499  return *this;
500  }
501 
502  const T& operator*() const {
503  CHECK(impl_ != nullptr);
504  return *(*impl_);
505  }
506 
507  [[nodiscard]] bool operator!=(const BatchIterator&) const { return !this->AtEnd(); }
508 
509  [[nodiscard]] bool AtEnd() const {
510  CHECK(impl_ != nullptr);
511  return impl_->AtEnd();
512  }
513 
514  [[nodiscard]] std::shared_ptr<T const> Page() const { return impl_->Page(); }
515 
516  private:
517  std::shared_ptr<BatchIteratorImpl<T>> impl_;
518 };
519 
520 template <typename T>
521 class BatchSet {
522  public:
523  explicit BatchSet(BatchIterator<T> begin_iter) : begin_iter_(std::move(begin_iter)) {}
524  BatchIterator<T> begin() { return begin_iter_; } // NOLINT
525  BatchIterator<T> end() { return BatchIterator<T>(nullptr); } // NOLINT
526 
527  private:
528  BatchIterator<T> begin_iter_;
529 };
530 
531 struct XGBAPIThreadLocalEntry;
532 
533 // Configuration for external memoroy DMatrix.
534 struct ExtMemConfig {
535  // Cache prefix, not used if the cache is in the host memory. (on_host is true)
536  std::string cache;
537  // Whether the ellpack page is stored in the host memory.
538  bool on_host;
539  // Host cache/Total cache for the GPU impl.
541  // Minimum number of of bytes for each ellpack page in cache. Only used for in-host
542  // ExtMemQdm.
543  std::int64_t min_cache_page_bytes;
544  // Missing value.
545  float missing;
546  // The number of CPU threads.
547  std::int32_t n_threads{0};
548  // The ratio of the cache that can be compressed. Used for testing.
549  float hw_decomp_ratio{std::numeric_limits<float>::quiet_NaN()};
550  // Fallback to using nvcomp. Used for testing.
552 
553  ExtMemConfig() = delete;
554  ExtMemConfig(std::string cache, bool on_host, float h_ratio, std::int64_t min_cache,
555  float missing, std::int32_t n_threads)
556  : cache{std::move(cache)},
557  on_host{on_host},
558  cache_host_ratio{h_ratio},
559  min_cache_page_bytes{min_cache},
560  missing{missing},
561  n_threads{n_threads} {}
562 
563  ExtMemConfig& SetParamsForTest(float _hw_decomp_ratio, bool _allow_decomp_fallback) {
564  this->hw_decomp_ratio = _hw_decomp_ratio;
565  this->allow_decomp_fallback = _allow_decomp_fallback;
566  return *this;
567  }
568 };
569 
577 class DMatrix {
578  public:
580  DMatrix() = default;
582  [[nodiscard]] virtual MetaInfo& Info() = 0;
583  virtual void SetInfo(const char* key, std::string const& interface_str) {
584  auto const& ctx = *this->Ctx();
585  this->Info().SetInfo(ctx, key, StringView{interface_str});
586  }
588  [[nodiscard]] virtual const MetaInfo& Info() const = 0;
589 
591  [[nodiscard]] XGBAPIThreadLocalEntry& GetThreadLocal() const;
596  [[nodiscard]] virtual Context const* Ctx() const = 0;
597 
601  template <typename T>
603  template <typename T>
605  template <typename T>
606  BatchSet<T> GetBatches(Context const* ctx, const BatchParam& param);
607  template <typename T>
608  [[nodiscard]] bool PageExists() const;
609 
615  [[nodiscard]] bool SingleColBlock() const { return this->NumBatches() == 1; }
616  [[nodiscard]] virtual std::int32_t NumBatches() const { return 1; }
617 
618  virtual ~DMatrix();
619 
623  [[nodiscard]] bool IsDense() const { return this->Info().IsDense(); }
624 
633  static DMatrix* Load(const std::string& uri, bool silent = true,
634  DataSplitMode data_split_mode = DataSplitMode::kRow);
635 
648  template <typename AdapterT>
649  static DMatrix* Create(AdapterT* adapter, float missing, int nthread,
650  const std::string& cache_prefix = "",
651  DataSplitMode data_split_mode = DataSplitMode::kRow);
652 
672  template <typename DataIterHandle, typename DMatrixHandle, typename DataIterResetCallback,
673  typename XGDMatrixCallbackNext>
674  static DMatrix* Create(DataIterHandle iter, DMatrixHandle proxy, std::shared_ptr<DMatrix> ref,
675  DataIterResetCallback* reset, XGDMatrixCallbackNext* next, float missing,
676  std::int32_t nthread, bst_bin_t max_bin);
677 
694  template <typename DataIterHandle, typename DMatrixHandle, typename DataIterResetCallback,
695  typename XGDMatrixCallbackNext>
697  XGDMatrixCallbackNext* next, ExtMemConfig const& config);
698 
706  template <typename DataIterHandle, typename DMatrixHandle, typename DataIterResetCallback,
707  typename XGDMatrixCallbackNext>
708  static DMatrix* Create(DataIterHandle iter, DMatrixHandle proxy, std::shared_ptr<DMatrix> ref,
710  bst_bin_t max_bin, ExtMemConfig const& config);
711 
713 
721  virtual DMatrix* SliceCol(int num_slices, int slice_id) = 0;
725  [[nodiscard]] CatContainer const* Cats() const { return this->CatsShared().get(); }
726  [[nodiscard]] std::shared_ptr<CatContainer const> CatsShared() const {
727  return this->Info().CatsShared();
728  }
729 
730  protected:
732  virtual BatchSet<CSCPage> GetColumnBatches(Context const* ctx) = 0;
734  virtual BatchSet<EllpackPage> GetEllpackBatches(Context const* ctx, BatchParam const& param) = 0;
736  BatchParam const& param) = 0;
737  virtual BatchSet<ExtSparsePage> GetExtBatches(Context const* ctx, BatchParam const& param) = 0;
738 
739  [[nodiscard]] virtual bool EllpackExists() const = 0;
740  [[nodiscard]] virtual bool GHistIndexExists() const = 0;
741  [[nodiscard]] virtual bool SparsePageExists() const = 0;
742 };
743 
744 template <>
746  return GetRowBatches();
747 }
748 
749 template <>
750 inline bool DMatrix::PageExists<EllpackPage>() const {
751  return this->EllpackExists();
752 }
753 
754 template <>
755 inline bool DMatrix::PageExists<GHistIndexMatrix>() const {
756  return this->GHistIndexExists();
757 }
758 
759 template <>
760 inline bool DMatrix::PageExists<SparsePage>() const {
761  return this->SparsePageExists();
762 }
763 
764 template <>
766  return GetRowBatches();
767 }
768 
769 template <>
770 inline BatchSet<CSCPage> DMatrix::GetBatches(Context const* ctx) {
771  return GetColumnBatches(ctx);
772 }
773 
774 template <>
775 inline BatchSet<SortedCSCPage> DMatrix::GetBatches(Context const* ctx) {
776  return GetSortedColumnBatches(ctx);
777 }
778 
779 template <>
781  return GetEllpackBatches(ctx, param);
782 }
783 
784 template <>
785 inline BatchSet<GHistIndexMatrix> DMatrix::GetBatches(Context const* ctx, BatchParam const& param) {
786  return GetGradientIndex(ctx, param);
787 }
788 
789 template <>
790 inline BatchSet<ExtSparsePage> DMatrix::GetBatches(Context const* ctx, BatchParam const& param) {
791  return GetExtBatches(ctx, param);
792 }
793 } // namespace xgboost
794 
796 
797 namespace dmlc {
799 
800 namespace serializer {
801 
802 template <>
803 struct Handler<xgboost::Entry> {
804  inline static void Write(Stream* strm, const xgboost::Entry& data) {
805  strm->Write(data.index);
806  strm->Write(data.fvalue);
807  }
808 
809  inline static bool Read(Stream* strm, xgboost::Entry* data) {
810  return strm->Read(&data->index) && strm->Read(&data->fvalue);
811  }
812 };
813 
814 } // namespace serializer
815 } // namespace dmlc
816 #endif // XGBOOST_DATA_H_
Defines configuration macros and basic types for xgboost.
#define XGBOOST_DEVICE
Tag function as usable by device.
Definition: base.h:57
Definition: data.h:479
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
Definition: data.h:490
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
Definition: data.h:521
BatchSet(BatchIterator< T > begin_iter)
Definition: data.h:523
BatchIterator< T > begin()
Definition: data.h:524
BatchIterator< T > end()
Definition: data.h:525
Definition: data.h:453
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 &param)=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 &param)=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 &param)=0
bool PageExists() const
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 ~DMatrix()
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 &param)
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
std::size_t Size() const
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
Meta information about dataset, always sit in memory.
Definition: data.h:67
std::vector< std::string > feature_names
Name for each feature.
Definition: data.h:111
std::shared_ptr< CatContainer const > CatsShared() const
MetaInfo(MetaInfo &&that)=default
HostDeviceVector< bst_float > labels_upper_bound_
upper bound of the label, to be used for survival analysis (censored regression)
Definition: data.h:102
MetaInfo Slice(Context const *ctx, common::Span< bst_idx_t const > ridxs, bst_idx_t nnz) const
Slice the meta info.
void SynchronizeNumberOfColumns(Context const *ctx, DataSplitMode split_mode)
Synchronize the number of columns across all workers.
uint64_t num_col_
number of columns in the data
Definition: data.h:75
std::vector< std::string > feature_type_names
Name of type for each feature provided by users. Eg. "int"/"float"/"i"/"q".
Definition: data.h:107
HostDeviceVector< bst_float > weights_
weights of each instance, optional
Definition: data.h:88
CatContainer * Cats()
bool IsVerticalFederated() const
A convenient method to check if we are doing vertical federated learning, which requires some special...
TypedArrayRef GetInfo(Context const *ctx, StringView key) const
Return an array reference for a meta info.
MetaInfo & operator=(MetaInfo const &that)=delete
bool IsColumnSplit() const
Whether the data is split column-wise.
Definition: data.h:206
bst_float GetWeight(size_t i) const
Get weight of each instances.
Definition: data.h:152
HostDeviceVector< FeatureType > feature_types
Definition: data.h:115
DataSplitMode data_split_mode
data split mode
Definition: data.h:81
void LoadBinary(dmlc::Stream *fi)
Load the Meta info from binary stream.
std::vector< bst_group_t > group_ptr_
the index of begin and end of a group needed when the learning task is ranking.
Definition: data.h:86
void Validate(DeviceOrd device) const
Validate all metainfo.
HostDeviceVector< float > feature_weights
Definition: data.h:120
void GetFeatureInfo(const char *field, std::vector< std::string > *out_str_vecs) const
bst_idx_t num_row_
number of rows in the data
Definition: data.h:73
bool HasCategorical() const
Flag for whether the DMatrix has categorical features.
Definition: data.h:226
void Cats(std::shared_ptr< CatContainer > cats)
Setter for categories.
CatContainer const * Cats() const
Getters for categories.
MetaInfo & operator=(MetaInfo &&that)=default
bool IsDense() const
Whether the matrix is dense.
Definition: data.h:146
void Clear()
clear all the information
void Extend(MetaInfo const &that, bool accumulate_rows, bool check_column)
Extend with other MetaInfo.
bool IsRanking() const
Whether this is a learning to rank data.
Definition: data.h:208
uint64_t num_nonzero_
number of nonzero entries in the data
Definition: data.h:77
MetaInfo Copy() const
linalg::Tensor< float, 2 > labels
label of each instance
Definition: data.h:79
void SaveBinary(dmlc::Stream *fo) const
Save the Meta info to binary stream.
MetaInfo(MetaInfo const &that)=delete
bool ShouldHaveLabels() const
A convenient method to check if the MetaInfo should contain labels.
static constexpr uint64_t kNumField
number of data fields in MetaInfo
Definition: data.h:70
bool IsRowSplit() const
Whether the data is split row-wise.
Definition: data.h:204
void SetInfo(Context const &ctx, StringView key, StringView in_array)
Set information in the meta info with array interface.
linalg::Matrix< float > base_margin_
initialized margins, if specified, xgboost will start from this init margin can be used to specify in...
Definition: data.h:94
HostDeviceVector< bst_float > labels_lower_bound_
lower bound of the label, to be used for survival analysis (censored regression)
Definition: data.h:98
const std::vector< size_t > & LabelAbsSort(Context const *ctx) const
get sorted indexes (argsort) of labels by absolute value (used by cox loss)
void SetFeatureInfo(const char *key, const char **info, const bst_ulong size)
Definition: data.h:469
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.
Definition: data.h:797
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
Definition: data.h:534
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
Definition: data.h:349
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