Debian Codegraph @

src/include/xgboost/tree_updater.h

raw symbols SCIP
1 /**
2 * Copyright 2014-2026, XGBoost Contributors
3 *
4 * @brief General primitive for tree learning,
5 * Updating a collection of trees given the information.
6 * \author Tianqi Chen
7 */
8 #ifndef XGBOOST_TREE_UPDATER_H_
9 #define XGBOOST_TREE_UPDATER_H_
10
11 #include <dmlc/registry.h>
12 #include <xgboost/base.h> // for Args, GradientPair
13 #include <xgboost/data.h> // for DMatrix
14 #include <xgboost/gradient.h> // for GradientContainer
15 #include <xgboost/host_device_vector.h> // for HostDeviceVector
16 #include <xgboost/linalg.h> // for VectorView
17 #include <xgboost/model.h> // for Configurable
18 #include <xgboost/span.h> // for Span
19 #include <xgboost/tree_model.h> // for RegTree
20
21 #include <functional> // for function
22 #include <string> // for string
23 #include <vector> // for vector
24
25 namespace xgboost {
26 namespace tree {
27 struct TrainParam;
28 }
29
30 class Json;
31 struct Context;
32 struct ObjInfo;
33
34 /**
35 * @brief interface of tree update module, that performs update of a tree.
36 */
37 class TreeUpdater : public Configurable {
38 protected:
39 Context const* ctx_ = nullptr;
40
41 public:
42 explicit TreeUpdater(const Context* ctx) : ctx_(ctx) {}
43 ~TreeUpdater() override = default;
44 /**
45 * @brief Initialize the updater with given arguments.
46 * @param args arguments to the objective function.
47 */
48 virtual void Configure(const Args& args) = 0;
49 /**
50 * @brief Whether this updater can be used for updating existing trees.
51 *
52 * Some updaters are used for building new trees (like `hist`), while some others are
53 * used for modifying existing trees (like `prune`). Return true if it can modify
54 * existing trees.
55 */
56 [[nodiscard]] virtual bool CanModifyTree() const { return false; }
57 /**
58 * @brief Whether the out_position in `Update` is valid. This determines whether adaptive
59 * tree can be used.
60 */
61 [[nodiscard]] virtual bool HasNodePosition() const { return false; }
62 /**
63 * @brief perform update to the tree models
64 *
65 * @param param Hyper-parameter for constructing trees.
66 * @param gpair The gradient pair statistics of the data
67 * @param p_fmat The data matrix passed to the updater.
68 * @param out_position The leaf index for each row. The index is negated if that row is
69 * removed during sampling. So the 3th node is ~3.
70 * @param out_trees references the trees to be updated, updater will change the content of trees
71 *
72 * @note All the trees in the vector are updated, with the same statistics, but maybe
73 * different random seeds, usually one tree is passed in at a time, there can be
74 * multiple trees when we train a random forest style model.
75 */
76 virtual void Update(tree::TrainParam const* param, GradientContainer* gpair, DMatrix* p_fmat,
77 common::Span<HostDeviceVector<bst_node_t>> out_position,
78 std::vector<RegTree*> const& out_trees) = 0;
79
80 /**
81 * @brief Determines whether updater has enough knowledge about a given dataset to
82 * quickly update prediction cache for the training data and performs the update
83 * if possible.
84 *
85 * @param p_fmat data matrix
86 * @param out_preds prediction cache to be updated
87 *
88 * @return boolean indicating whether updater has capability to update the prediction
89 * cache. If true, the prediction cache will have been updated by the time this
90 * function returns.
91 */
92 virtual bool UpdatePredictionCache(DMatrix const* /*data*/,
93 common::Span<HostDeviceVector<bst_node_t>> /*out_position*/,
94 linalg::MatrixView<float> /*out_preds*/) {
95 return false;
96 }
97
98 [[nodiscard]] virtual char const* Name() const = 0;
99
100 /**
101 * @brief Create a tree updater given name
102 *
103 * @param name Name of the tree updater.
104 * @param ctx A global runtime parameter
105 * @param task Infomation about the objective.
106 */
107 static TreeUpdater* Create(const std::string& name, Context const* ctx, ObjInfo const* task);
108 };
109
110 /**
111 * @brief Registry entry for tree updater.
112 */
113 struct TreeUpdaterReg
114 : public dmlc::FunctionRegEntryBase<
115 TreeUpdaterReg, std::function<TreeUpdater*(Context const* ctx, ObjInfo const* task)>> {};
116
117 /*!
118 * \brief Macro to register tree updater.
119 *
120 * \code
121 * // example of registering a objective ndcg@k
122 * XGBOOST_REGISTER_TREE_UPDATER(ColMaker, "colmaker")
123 * .describe("Column based tree maker.")
124 * .set_body([]() {
125 * return new ColMaker<TStats>();
126 * });
127 * \endcode
128 */
129 #define XGBOOST_REGISTER_TREE_UPDATER(UniqueId, Name) \
130 static DMLC_ATTRIBUTE_UNUSED ::xgboost::TreeUpdaterReg& \
131 __make_ ## TreeUpdaterReg ## _ ## UniqueId ## __ = \
132 ::dmlc::Registry< ::xgboost::TreeUpdaterReg>::Get()->__REGISTER__(Name)
133
134 } // namespace xgboost
135 #endif // XGBOOST_TREE_UPDATER_H_