| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#pragma once |
| 8 |
#include <dmlc/registry.h> |
| 9 |
#include <xgboost/base.h> |
| 10 |
#include <xgboost/cache.h> |
| 11 |
#include <xgboost/context.h> |
| 12 |
#include <xgboost/context.h> |
| 13 |
#include <xgboost/data.h> |
| 14 |
#include <xgboost/host_device_vector.h> |
| 15 |
|
| 16 |
#include <functional> |
| 17 |
#include <memory> |
| 18 |
#include <string> |
| 19 |
#include <vector> |
| 20 |
|
| 21 |
|
| 22 |
namespace xgboost::gbm { |
| 23 |
struct GBTreeModel; |
| 24 |
} |
| 25 |
|
| 26 |
namespace xgboost { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
struct PredictionCacheEntry { |
| 31 |
|
| 32 |
HostDeviceVector<float> predictions; |
| 33 |
|
| 34 |
std::uint32_t version{0}; |
| 35 |
|
| 36 |
PredictionCacheEntry() = default; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
void Update(std::uint32_t v) { version += v; } |
| 43 |
void Reset() { version = 0; } |
| 44 |
}; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
class PredictionContainer : public DMatrixCache<PredictionCacheEntry> { |
| 50 |
|
| 51 |
std::size_t static constexpr DefaultSize() { return 64; } |
| 52 |
|
| 53 |
public: |
| 54 |
PredictionContainer() : DMatrixCache<PredictionCacheEntry>{DefaultSize()} {} |
| 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 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 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 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
virtual void Configure(Args const&); |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
virtual void InitOutPredictions(const MetaInfo& info, HostDeviceVector<float>* out_predt, |
| 96 |
const gbm::GBTreeModel& model) const; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 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 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 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 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
virtual void PredictLeaf(DMatrix* dmat, HostDeviceVector<float>* out_preds, |
| 140 |
gbm::GBTreeModel const& model, bst_tree_t tree_end = 0) const = 0; |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 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 |
|
| 163 |
virtual void PredictInteractionContributions(DMatrix* dmat, HostDeviceVector<float>* out_contribs, |
| 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 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
static Predictor* Create(std::string const& name, Context const* ctx); |
| 176 |
}; |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
struct PredictorReg |
| 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 |
} |