| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#ifndef XGBOOST_MULTI_TARGET_TREE_MODEL_H_ |
| 7 |
#define XGBOOST_MULTI_TARGET_TREE_MODEL_H_ |
| 8 |
|
| 9 |
#include <xgboost/base.h> |
| 10 |
#include <xgboost/context.h> |
| 11 |
#include <xgboost/host_device_vector.h> |
| 12 |
#include <xgboost/linalg.h> |
| 13 |
#include <xgboost/model.h> |
| 14 |
#include <xgboost/span.h> |
| 15 |
|
| 16 |
#include <cstddef> |
| 17 |
#include <cstdint> |
| 18 |
#include <vector> |
| 19 |
|
| 20 |
namespace xgboost { |
| 21 |
namespace tree { |
| 22 |
struct MultiTargetTreeView; |
| 23 |
} |
| 24 |
struct TreeParam; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 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 |
|
| 46 |
HostDeviceVector<bst_node_t> left_; |
| 47 |
|
| 48 |
HostDeviceVector<bst_node_t> right_; |
| 49 |
|
| 50 |
HostDeviceVector<bst_node_t> parent_; |
| 51 |
|
| 52 |
HostDeviceVector<bst_feature_t> split_index_; |
| 53 |
|
| 54 |
HostDeviceVector<std::uint8_t> default_left_; |
| 55 |
|
| 56 |
HostDeviceVector<float> split_conds_; |
| 57 |
|
| 58 |
HostDeviceVector<float> weights_; |
| 59 |
|
| 60 |
HostDeviceVector<float> leaf_weights_; |
| 61 |
|
| 62 |
HostDeviceVector<float> loss_chg_; |
| 63 |
|
| 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 |
|
| 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 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
void SetRoot(linalg::VectorView<float const> weight, float sum_hess); |
| 94 |
|
| 95 |
|
| 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 |
|
| 103 |
void SetLeaves(std::vector<bst_node_t> leaves, common::Span<float const> weights); |
| 104 |
|
| 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 |
|
| 118 |
|
| 119 |
[[nodiscard]] bst_target_t NumTargets() const; |
| 120 |
|
| 121 |
|
| 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 |
} |
| 154 |
#endif |