xgboost
predictor.h
前往此文件文档。
1 
7 #pragma once
8 #include <dmlc/registry.h> // for FunctionRegEntryBase
9 #include <xgboost/base.h> // for bst_tree_t
10 #include <xgboost/cache.h> // for DMatrixCache
11 #include <xgboost/context.h> // for Context
12 #include <xgboost/context.h>
13 #include <xgboost/data.h>
15 
16 #include <functional> // for function
17 #include <memory> // for shared_ptr
18 #include <string>
19 #include <vector>
20 
21 // 前向声明
22 namespace xgboost::gbm {
23 struct GBTreeModel;
24 } // namespace xgboost::gbm
25 
26 namespace xgboost {
31  // 用于缓存预测值的存储
33  // 当前缓存的版本,对应树的层数
34  std::uint32_t version{0};
35 
36  PredictionCacheEntry() = default;
42  void Update(std::uint32_t v) { version += v; }
43  void Reset() { version = 0; }
44 };
45 
49 class PredictionContainer : public DMatrixCache<PredictionCacheEntry> {
50  // 我们为所有线程缓存多达 64 个 DMatrix
51  std::size_t static constexpr DefaultSize() { return 64; }
52 
53  public
55  std::shared_ptr<PredictionCacheEntry> Cache(std::shared_ptr<DMatrix> m, DeviceOrd device) {
56  auto p_cache = this->CacheItem(m);
57  if (!device.IsCPU()) {
58  p_cache->predictions.SetDevice(device);
59  }
60  return p_cache;
61  }
62 };
63 
72 class Predictor {
73  protected
74  Context const* ctx_;
75 
76  public
77  explicit Predictor(Context const* ctx) : ctx_{ctx} {}
78 
79  virtual ~Predictor() = default;
80 
86  virtual void Configure(Args const&);
87 
95  virtual void InitOutPredictions(const MetaInfo& info, HostDeviceVector<float>* out_predt,
96  const gbm::GBTreeModel& model) const;
97 
108  virtual void PredictBatch(DMatrix* dmat, PredictionCacheEntry* out_preds,
109  gbm::GBTreeModel const& model, bst_tree_t tree_begin,
110  bst_tree_t tree_end = 0) const = 0;
111 
125  virtual bool InplacePredict(std::shared_ptr<DMatrix> p_fmat, const gbm::GBTreeModel& model,
126  float missing, PredictionCacheEntry* out_preds,
127  bst_tree_t tree_begin = 0, bst_tree_t tree_end = 0) const = 0;
128 
139  virtual void PredictLeaf(DMatrix* dmat, HostDeviceVector<float>* out_preds,
140  gbm::GBTreeModel const& model, bst_tree_t tree_end = 0) const = 0;
141 
157  virtual void PredictContribution(DMatrix* dmat, HostDeviceVector<float>* out_contribs,
158  gbm::GBTreeModel const& model, bst_tree_t tree_end = 0,
159  std::vector<float> const* tree_weights = nullptr,
160  bool approximate = false, int condition = 0,
161  unsigned condition_feature = 0) const = 0;
162 
164  gbm::GBTreeModel const& model,
165  bst_tree_t tree_end = 0,
166  std::vector<float> const* tree_weights = nullptr,
167  bool approximate = false) const = 0;
168 
175  static Predictor* Create(std::string const& name, Context const* ctx);
176 };
177 
182  : public dmlc::FunctionRegEntryBase<PredictorReg, std::function<Predictor*(Context const*)>> {};
183 
184 #define XGBOOST_REGISTER_PREDICTOR(UniqueId, Name) \
185  static DMLC_ATTRIBUTE_UNUSED ::xgboost::PredictorReg& \
186  __make_##PredictorReg##_##UniqueId##__ = \
187  ::dmlc::Registry<::xgboost::PredictorReg>::Get()->__REGISTER__(Name)
188 } // namespace xgboost
为 xgboost 定义配置宏和基本类型。
DMatrix 相关数据的线程感知 FIFO 缓存。
定义: cache.h:26
std::shared_ptr< PredictionCacheEntry > CacheItem(std::shared_ptr< DMatrix > m, Args const &... args)
如果 DMatrix 不在缓存中,则缓存新的 DMatrix。
定义: cache.h:145
内部数据结构,由XGBoost用于保存所有外部数据。
Definition: data.h:573
数据集的元信息,始终存储在内存中。
Definition: data.h:51
用于管理预测缓存的容器。
定义: predictor.h:49
std::shared_ptr< PredictionCacheEntry > Cache(std::shared_ptr< DMatrix > m, DeviceOrd device)
定义: predictor.h:55
PredictionContainer()
定义: predictor.h:54
对 GBTree 的单个训练实例或实例批次执行预测。
定义: predictor.h:72
virtual void InitOutPredictions(const MetaInfo &info, HostDeviceVector< float > *out_predt, const gbm::GBTreeModel &model) const
初始化输出预测。
virtual void Configure(Args const &)
配置并在预测缓存中注册输入矩阵。
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
原地预测。
Predictor(Context const *ctx)
定义: predictor.h:77
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
特征对单个预测的贡献;输出将是一个长度为 (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
为给定特征矩阵生成批量预测。如果可用,可以使用缓存的预测...
Context const * ctx_
定义: predictor.h:74
static Predictor * Create(std::string const &name, Context const *ctx)
创建一个新的 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
预测每棵树的叶子索引,输出将是 nsample * ntree 向量,这只在 ... 中有效
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
xgboost 的输入数据结构。
设备与主机向量抽象层。
定义: linear_updater.h:23
集成目标、gbm和评估的学习器接口。这是用户面临的XGB...
Definition: base.h:97
std::vector< std::pair< std::string, std::string > > Args
定义: base.h:324
std::int32_t bst_tree_t
用于索引树的类型。
定义: base.h:135
XGBoost的运行时上下文。包含线程和设备等信息。
Definition: context.h:133
设备序号的类型。该类型被打包成32位,以便在查看类型(如lin...)时高效使用
Definition: context.h:34
bool IsCPU() const
定义: context.h:45
包含指向输入矩阵和相关缓存预测的指针。
定义: predictor.h:30
std::uint32_t version
定义: predictor.h:34
HostDeviceVector< float > predictions
定义: predictor.h:32
void Reset()
定义: predictor.h:43
void Update(std::uint32_t v)
按版本数量更新缓存条目。
定义: predictor.h:42
预测器的注册表条目。
定义: predictor.h:182