Debian Codegraph @

src/include/xgboost/multi_target_tree_model.h

raw symbols SCIP
1 /**
2 * Copyright 2023-2026, XGBoost contributors
3 *
4 * @brief Core data structure for multi-target trees.
5 */
6 #ifndef XGBOOST_MULTI_TARGET_TREE_MODEL_H_
7 #define XGBOOST_MULTI_TARGET_TREE_MODEL_H_
8
9 #include <xgboost/base.h> // for bst_node_t, bst_target_t, bst_feature_t
10 #include <xgboost/context.h> // for Context
11 #include <xgboost/host_device_vector.h> // for HostDeviceVector
12 #include <xgboost/linalg.h> // for VectorView, MatrixView
13 #include <xgboost/model.h> // for Model
14 #include <xgboost/span.h> // for Span
15
16 #include <cstddef> // for size_t
17 #include <cstdint> // for uint8_t
18 #include <vector> // for vector
19
20 namespace xgboost {
21 namespace tree {
22 struct MultiTargetTreeView;
23 }
24 struct TreeParam;
25
26 /**
27 * @brief Tree structure for multi-target model.
28 *
29 * In order to support reduced gradient, the internal storage distinguishes weights
30 * between base weights and leaf weights. The former is the weight calculated from split
31 * gradient, and the later is the weight calculated from value gradient and used as
32 * outputs. Every node has a base weight, but only leaves have leaf weights.
33 *
34 * To access the leaf weights, we re-use the right child to store leaf indices. For split
35 * nodes, the `right_` member stores their right child node indices, for leaf nodes, the
36 * `right_` member stores the corresponding leaf weight indices.
37 */
38 class MultiTargetTree : public Model {
39 public:
40 static bst_node_t constexpr InvalidNodeId() { return -1; }
41 friend struct tree::MultiTargetTreeView;
42
43 private:
44 TreeParam const* param_;
45 // Mapping from node index to its left child. -1 for a leaf node.
46 HostDeviceVector<bst_node_t> left_;
47 // Mapping from node index to its right child. Maps to leaf weight for a leaf node.
48 HostDeviceVector<bst_node_t> right_;
49 // Mapping from node index to its parent.
50 HostDeviceVector<bst_node_t> parent_;
51 // Feature index for node split.
52 HostDeviceVector<bst_feature_t> split_index_;
53 // Whether the left child is the default node when split feature is missing.
54 HostDeviceVector<std::uint8_t> default_left_;
55 // Threshold for splitting a node.
56 HostDeviceVector<float> split_conds_;
57 // Internal base weights.
58 HostDeviceVector<float> weights_;
59 // Output weights.
60 HostDeviceVector<float> leaf_weights_;
61 // Loss change for each node.
62 HostDeviceVector<float> loss_chg_;
63 // Sum of hessians for each node (coverage).
64 HostDeviceVector<float> sum_hess_;
65
66 [[nodiscard]] linalg::VectorView<float const> NodeWeight(bst_node_t nidx) const {
67 auto beg = nidx * this->NumSplitTargets();
68 auto v = this->weights_.ConstHostSpan().subspan(beg, this->NumSplitTargets());
69 return linalg::MakeTensorView(DeviceOrd::CPU(), v, v.size());
70 }
71 // Unlike the const version, `NumSplitTargets` is not reliable if the tree can change.
72 [[nodiscard]] linalg::VectorView<float> NodeWeight(bst_node_t nidx,
73 bst_target_t n_split_targets) {
74 auto beg = nidx * n_split_targets;
75 auto v = this->weights_.HostSpan().subspan(beg, n_split_targets);
76 return linalg::MakeTensorView(DeviceOrd::CPU(), v, v.size());
77 }
78 [[nodiscard]] bst_node_t LeafIdx(bst_node_t nidx) const { return this->RightChild(nidx); }
79
80 public:
81 explicit MultiTargetTree(TreeParam const* param);
82 MultiTargetTree(MultiTargetTree const& that);
83 MultiTargetTree& operator=(MultiTargetTree const& that) = delete;
84 MultiTargetTree(MultiTargetTree&& that) = delete;
85 MultiTargetTree& operator=(MultiTargetTree&& that) = delete;
86
87 /**
88 * @brief Set the weight and statistics for the root.
89 *
90 * @param weight The weight vector for the root node.
91 * @param sum_hess The sum of hessians for the root node (coverage).
92 */
93 void SetRoot(linalg::VectorView<float const> weight, float sum_hess);
94 /**
95 * @brief Expand a leaf into split node.
96 */
97 void Expand(bst_node_t nidx, bst_feature_t split_idx, float split_cond, bool default_left,
98 linalg::VectorView<float const> base_weight,
99 linalg::VectorView<float const> left_weight,
100 linalg::VectorView<float const> right_weight, float loss_chg, float sum_hess,
101 float left_sum, float right_sum);
102 /** @see RegTree::SetLeaves */
103 void SetLeaves(std::vector<bst_node_t> leaves, common::Span<float const> weights);
104 /** @brief Copy base weight into leaf weight for a non-reduced multi-target tree. */
105 void SetLeaves();
106
107 [[nodiscard]] bool IsLeaf(bst_node_t nidx) const {
108 return left_.ConstHostVector()[nidx] == InvalidNodeId();
109 }
110 [[nodiscard]] bst_node_t LeftChild(bst_node_t nidx) const {
111 return left_.ConstHostVector().at(nidx);
112 }
113 [[nodiscard]] bst_node_t RightChild(bst_node_t nidx) const {
114 return right_.ConstHostVector().at(nidx);
115 }
116 /**
117 * @brief Number of targets (size of a leaf).
118 */
119 [[nodiscard]] bst_target_t NumTargets() const;
120 /**
121 * @brief Number of reduced targets.
122 */
123 [[nodiscard]] bst_target_t NumSplitTargets() const;
124 [[nodiscard]] auto NumLeaves() const { return this->leaf_weights_.Size() / this->NumTargets(); }
125
126 [[nodiscard]] std::size_t Size() const;
127 [[nodiscard]] MultiTargetTree* Copy(TreeParam const* param) const;
128
129 common::Span<float const> LeafWeights(DeviceOrd device) const {
130 if (device.IsCPU()) {
131 return this->leaf_weights_.ConstHostSpan();
132 }
133 this->leaf_weights_.SetDevice(device);
134 return this->leaf_weights_.ConstDeviceSpan();
135 }
136
137 [[nodiscard]] linalg::VectorView<float const> LeafValue(bst_node_t nidx) const {
138 CHECK(IsLeaf(nidx));
139 auto n_targets = this->NumTargets();
140 auto h_leaf_mapping = this->right_.ConstHostSpan();
141 auto h_leaf_weights = this->leaf_weights_.ConstHostSpan();
142 auto lidx = h_leaf_mapping[nidx];
143 CHECK_NE(lidx, InvalidNodeId());
144 auto weight = h_leaf_weights.subspan(lidx * n_targets, n_targets);
145 return linalg::MakeVec(DeviceOrd::CPU(), weight);
146 }
147
148 void LoadModel(Json const& in) override;
149 void SaveModel(Json* out) const override;
150
151 [[nodiscard]] std::size_t MemCostBytes() const;
152 };
153 } // namespace xgboost
154 #endif // XGBOOST_MULTI_TARGET_TREE_MODEL_H_