xgboost
predictor.h
Go to the documentation of this file.
1 
7 #pragma once
8 #include <xgboost/base.h>
9 #include <xgboost/cache.h> // for DMatrixCache
10 #include <xgboost/context.h> // for Context
11 #include <xgboost/context.h>
12 #include <xgboost/data.h>
14 
15 #include <functional> // for function
16 #include <memory> // for shared_ptr
17 #include <string>
18 #include <vector>
19 
20 // Forward declarations
21 namespace xgboost::gbm {
22 struct GBTreeModel;
23 } // namespace xgboost::gbm
24 
25 namespace xgboost {
30  // A storage for caching prediction values
32  // The version of current cache, corresponding number of layers of trees
33  std::uint32_t version{0};
34 
35  PredictionCacheEntry() = default;
41  void Update(std::uint32_t v) { version += v; }
42  void Reset() { version = 0; }
43 };
44 
48 class PredictionContainer : public DMatrixCache<PredictionCacheEntry> {
49  // We cache up to 64 DMatrix for all threads
50  std::size_t static constexpr DefaultSize() { return 64; }
51 
52  public:
54  std::shared_ptr<PredictionCacheEntry> Cache(std::shared_ptr<DMatrix> m, DeviceOrd device) {
55  auto p_cache = this->CacheItem(m);
56  if (!device.IsCPU()) {
57  p_cache->predictions.SetDevice(device);
58  }
59  return p_cache;
60  }
61 };
62 
71 class Predictor {
72  protected:
73  Context const* ctx_;
74 
75  public:
76  explicit Predictor(Context const* ctx) : ctx_{ctx} {}
77 
78  virtual ~Predictor() = default;
79 
85  virtual void Configure(Args const&);
86 
94  virtual void InitOutPredictions(const MetaInfo& info, HostDeviceVector<float>* out_predt,
95  const gbm::GBTreeModel& model) const;
96 
107  virtual void PredictBatch(DMatrix* dmat, PredictionCacheEntry* out_preds,
108  gbm::GBTreeModel const& model, bst_tree_t tree_begin,
109  bst_tree_t tree_end = 0) const = 0;
110 
124  virtual bool InplacePredict(std::shared_ptr<DMatrix> p_fmat, const gbm::GBTreeModel& model,
125  float missing, PredictionCacheEntry* out_preds,
126  bst_tree_t tree_begin = 0, bst_tree_t tree_end = 0) const = 0;
127 
138  virtual void PredictLeaf(DMatrix* dmat, HostDeviceVector<float>* out_preds,
139  gbm::GBTreeModel const& model, bst_tree_t tree_end = 0) const = 0;
140 
156  virtual void PredictContribution(DMatrix* dmat, HostDeviceVector<float>* out_contribs,
157  gbm::GBTreeModel const& model, bst_tree_t tree_end = 0,
158  std::vector<float> const* tree_weights = nullptr,
159  bool approximate = false, int condition = 0,
160  unsigned condition_feature = 0) const = 0;
161 
163  gbm::GBTreeModel const& model,
164  bst_tree_t tree_end = 0,
165  std::vector<float> const* tree_weights = nullptr,
166  bool approximate = false) const = 0;
167 
174  static Predictor* Create(std::string const& name, Context const* ctx);
175 };
176 
181  : public dmlc::FunctionRegEntryBase<PredictorReg, std::function<Predictor*(Context const*)>> {};
182 
183 #define XGBOOST_REGISTER_PREDICTOR(UniqueId, Name) \
184  static DMLC_ATTRIBUTE_UNUSED ::xgboost::PredictorReg& \
185  __make_##PredictorReg##_##UniqueId##__ = \
186  ::dmlc::Registry<::xgboost::PredictorReg>::Get()->__REGISTER__(Name)
187 } // namespace xgboost
Defines configuration macros and basic types for xgboost.
Thread-aware FIFO cache for DMatrix related data.
Definition: cache.h:26
std::shared_ptr< PredictionCacheEntry > CacheItem(std::shared_ptr< DMatrix > m, Args const &... args)
Cache a new DMatrix if it's not in the cache already.
Definition: cache.h:145
Internal data structured used by XGBoost to hold all external data.
Definition: data.h:549
Meta information about dataset, always sit in memory.
Definition: data.h:48
A container for managed prediction caches.
Definition: predictor.h:48
std::shared_ptr< PredictionCacheEntry > Cache(std::shared_ptr< DMatrix > m, DeviceOrd device)
Definition: predictor.h:54
PredictionContainer()
Definition: predictor.h:53
Performs prediction on individual training instances or batches of instances for GBTree....
Definition: predictor.h:71
virtual void InitOutPredictions(const MetaInfo &info, HostDeviceVector< float > *out_predt, const gbm::GBTreeModel &model) const
Initialize output prediction.
virtual void Configure(Args const &)
Configure and register input matrices in prediction cache.
virtual bool InplacePredict(std::shared_ptr< DMatrix > p_fmat, const gbm::GBTreeModel &model, float missing, PredictionCacheEntry *out_preds, bst_tree_t tree_begin=0, bst_tree_t tree_end=0) const =0
Inplace prediction.
Predictor(Context const *ctx)
Definition: predictor.h:76
virtual void PredictContribution(DMatrix *dmat, HostDeviceVector< float > *out_contribs, gbm::GBTreeModel const &model, bst_tree_t tree_end=0, std::vector< float > const *tree_weights=nullptr, bool approximate=false, int condition=0, unsigned condition_feature=0) const =0
feature contributions to individual predictions; the output will be a vector of length (nfeats + 1) *...
virtual void PredictBatch(DMatrix *dmat, PredictionCacheEntry *out_preds, gbm::GBTreeModel const &model, bst_tree_t tree_begin, bst_tree_t tree_end=0) const =0
Generate batch predictions for a given feature matrix. May use cached predictions if available instea...
Context const * ctx_
Definition: predictor.h:73
static Predictor * Create(std::string const &name, Context const *ctx)
Creates a new Predictor*.
virtual ~Predictor()=default
virtual void PredictLeaf(DMatrix *dmat, HostDeviceVector< float > *out_preds, gbm::GBTreeModel const &model, bst_tree_t tree_end=0) const =0
predict the leaf index of each tree, the output will be nsample * ntree vector this is only valid in ...
virtual void PredictInteractionContributions(DMatrix *dmat, HostDeviceVector< float > *out_contribs, gbm::GBTreeModel const &model, bst_tree_t tree_end=0, std::vector< float > const *tree_weights=nullptr, bool approximate=false) const =0
The input data structure of xgboost.
A device-and-host vector abstraction layer.
Definition: linear_updater.h:23
Core data structure for multi-target trees.
Definition: base.h:89
std::vector< std::pair< std::string, std::string > > Args
Definition: base.h:316
std::int32_t bst_tree_t
Type for indexing trees.
Definition: base.h:127
Runtime context for XGBoost. Contains information like threads and device.
Definition: context.h:133
A type for device ordinal. The type is packed into 32-bit for efficient use in viewing types like lin...
Definition: context.h:34
bool IsCPU() const
Definition: context.h:45
Contains pointer to input matrix and associated cached predictions.
Definition: predictor.h:29
std::uint32_t version
Definition: predictor.h:33
HostDeviceVector< float > predictions
Definition: predictor.h:31
void Reset()
Definition: predictor.h:42
void Update(std::uint32_t v)
Update the cache entry by number of versions.
Definition: predictor.h:41
Registry entry for predictor.
Definition: predictor.h:181