7 #ifndef XGBOOST_TREE_MODEL_H_
8 #define XGBOOST_TREE_MODEL_H_
14 #include <xgboost/logging.h>
74 std::unique_ptr<T> ptr_{
nullptr};
81 ptr_ = std::make_unique<T>(*that);
84 T*
get() const noexcept {
return ptr_.get(); }
92 explicit operator bool()
const {
return static_cast<bool>(ptr_); }
94 void reset(T* ptr) { ptr_.reset(ptr); }
114 static_assert(
sizeof(
Node) == 4 *
sizeof(
int) +
sizeof(Info),
"Node: 64 bit align");
116 Node(int32_t cleft, int32_t cright, int32_t parent, uint32_t split_ind,
float split_cond,
118 : parent_{parent}, cleft_{cleft}, cright_{cright} {
120 this->
SetSplit(split_ind, split_cond, default_left);
133 static_assert(!std::is_signed_v<bst_feature_t>);
134 return sindex_ & ((1U << 31) - 1U);
173 bool default_left =
false) {
174 if (default_left) split_index |= (1U << 31);
175 this->sindex_ = split_index;
176 (this->info_).split_cond = split_cond;
185 (this->info_).leaf_value = value;
187 this->cright_ = right;
199 if (is_left_child) pidx |= (1U << 31);
200 this->parent_ = pidx;
203 return parent_ == b.parent_ && cleft_ == b.cleft_ &&
204 cright_ == b.cright_ && sindex_ == b.sindex_ &&
205 info_.leaf_value == b.info_.leaf_value;
236 this->DeleteNode(nodes_[rid].
LeftChild());
238 nodes_[rid].SetLeaf(value);
246 if (nodes_[rid].
IsLeaf())
return;
260 split_categories_segments_.resize(param_.
num_nodes);
261 for (
int i = 0; i < param_.
num_nodes; i++) {
262 nodes_[i].SetLeaf(0.0f);
287 [[nodiscard]]
const std::vector<Node>&
GetNodes()
const {
return nodes_; }
290 [[nodiscard]]
const std::vector<RTreeNodeStat>&
GetStats()
const {
return stats_; }
305 return nodes_ == b.nodes_ && stats_ == b.stats_ &&
306 deleted_nodes_ == b.deleted_nodes_ && param_ == b.param_;
313 template <
typename Func>
void WalkTree(Func func)
const {
314 std::stack<bst_node_t> nodes;
317 while (!nodes.empty()) {
318 auto nidx = nodes.top();
323 auto left =
self.LeftChild(nidx);
324 auto right =
self.RightChild(nidx);
359 bool default_left,
bst_float base_weight,
361 bst_float loss_change,
float sum_hess,
float left_sum,
391 float left_sum,
float right_sum);
399 [[nodiscard]]
bool IsMultiTarget()
const {
return static_cast<bool>(p_mt_tree_); }
409 return p_mt_tree_.get();
441 return this->p_mt_tree_->Depth(nid);
444 while (!nodes_[nid].
IsRoot()) {
446 nid = nodes_[nid].Parent();
455 return this->p_mt_tree_->SetLeaf(nidx, weight);
463 if (nodes_[nid].
IsLeaf())
return 0;
481 void Init(
size_t size);
497 [[nodiscard]]
size_t Size()
const;
509 [[nodiscard]]
bool IsMissing(
size_t i)
const;
511 void HasMissing(
bool has_missing) { this->has_missing_ = has_missing; }
521 std::vector<float> data_;
533 std::string format)
const;
547 return split_categories_;
555 auto segment = node_ptr[nidx];
556 auto node_cats = categories.
subspan(segment.beg, segment.size);
588 return this->p_mt_tree_->SplitIndex(nidx);
590 return (*
this)[nidx].SplitIndex();
594 return this->p_mt_tree_->SplitCond(nidx);
596 return (*
this)[nidx].SplitCond();
600 return this->p_mt_tree_->DefaultLeft(nidx);
602 return (*
this)[nidx].DefaultLeft();
609 return nidx ==
kRoot;
611 return (*
this)[nidx].IsRoot();
615 return this->p_mt_tree_->IsLeaf(nidx);
617 return (*
this)[nidx].IsLeaf();
621 return this->p_mt_tree_->Parent(nidx);
623 return (*
this)[nidx].Parent();
627 return this->p_mt_tree_->LeftChild(nidx);
629 return (*
this)[nidx].LeftChild();
633 return this->p_mt_tree_->RightChild(nidx);
635 return (*
this)[nidx].RightChild();
639 CHECK_NE(nidx,
kRoot);
640 auto p = this->p_mt_tree_->Parent(nidx);
641 return nidx == this->p_mt_tree_->LeftChild(p);
643 return (*
this)[nidx].IsLeftChild();
647 return this->p_mt_tree_->Size();
649 return this->nodes_.size();
653 template <
bool typed>
654 void LoadCategoricalSplit(
Json const& in);
655 void SaveCategoricalSplit(
Json* p_out)
const;
659 std::vector<Node> nodes_;
661 std::vector<int> deleted_nodes_;
663 std::vector<RTreeNodeStat> stats_;
664 std::vector<FeatureType> split_types_;
667 std::vector<uint32_t> split_categories_;
669 std::vector<CategoricalSplitMatrix::Segment> split_categories_segments_;
676 int nid = deleted_nodes_.back();
677 deleted_nodes_.pop_back();
683 CHECK_LT(param_.
num_nodes, std::numeric_limits<int>::max())
684 <<
"number of nodes in the tree exceed 2^31";
688 split_categories_segments_.resize(param_.
num_nodes);
692 void DeleteNode(
int nid) {
694 auto pid = (*this)[nid].Parent();
701 deleted_nodes_.push_back(nid);
702 nodes_[nid].MarkDelete();
709 std::fill(data_.begin(), data_.end(), std::numeric_limits<float>::quiet_NaN());
714 auto p_data = inst.
data();
715 auto p_out = data_.data();
717 for (std::size_t i = 0, n = inst.
size(); i < n; ++i) {
718 auto const& entry = p_data[i];
719 p_out[entry.index] = entry.fvalue;
721 has_missing_ = data_.size() != inst.
size();
740 return " support for multi-target tree is not yet implemented.";
Defines configuration macros and basic types for xgboost.
#define XGBOOST_DEVICE
Tag function as usable by device.
Definition: base.h:64
Helper for defining copyable data structure that contains unique pointers.
Definition: tree_model.h:73
T const * operator->() const noexcept
Definition: tree_model.h:90
T * get() const noexcept
Definition: tree_model.h:84
bool operator!() const
Definition: tree_model.h:93
CopyUniquePtr(CopyUniquePtr const &that)
Definition: tree_model.h:78
T * operator->() noexcept
Definition: tree_model.h:87
T & operator*()
Definition: tree_model.h:86
T const & operator*() const
Definition: tree_model.h:89
void reset(T *ptr)
Definition: tree_model.h:94
Feature map data structure to help text model dump. TODO(tqchen) consider make it even more lightweig...
Definition: feature_map.h:22
Data structure representing JSON format.
Definition: json.h:392
Tree structure for multi-target model.
Definition: multi_target_tree_model.h:69
static constexpr bst_node_t InvalidNodeId()
Definition: multi_target_tree_model.h:71
tree node
Definition: tree_model.h:110
XGBOOST_DEVICE int Parent() const
get parent of the node
Definition: tree_model.h:145
XGBOOST_DEVICE void MarkDelete()
mark that this node is deleted
Definition: tree_model.h:190
XGBOOST_DEVICE bool IsRoot() const
whether current node is root
Definition: tree_model.h:151
XGBOOST_DEVICE int RightChild() const
index of right child
Definition: tree_model.h:126
XGBOOST_DEVICE float LeafValue() const
Definition: tree_model.h:141
XGBOOST_DEVICE Node()
Definition: tree_model.h:112
XGBOOST_DEVICE void SetParent(int pidx, bool is_left_child=true)
Definition: tree_model.h:198
XGBOOST_DEVICE void SetLeaf(bst_float value, int right=kInvalidNodeId)
set the leaf value of the node
Definition: tree_model.h:184
XGBOOST_DEVICE bool IsLeftChild() const
whether current node is left child
Definition: tree_model.h:147
XGBOOST_DEVICE void SetSplit(unsigned split_index, SplitCondT split_cond, bool default_left=false)
set split condition of current node
Definition: tree_model.h:172
XGBOOST_DEVICE void SetLeftChild(int nid)
set the left child
Definition: tree_model.h:156
XGBOOST_DEVICE bst_feature_t SplitIndex() const
feature index of split condition
Definition: tree_model.h:132
XGBOOST_DEVICE bool IsDeleted() const
whether this node is deleted
Definition: tree_model.h:149
XGBOOST_DEVICE bool IsLeaf() const
whether current node is leaf node
Definition: tree_model.h:139
bool operator==(const Node &b) const
Definition: tree_model.h:202
Node(int32_t cleft, int32_t cright, int32_t parent, uint32_t split_ind, float split_cond, bool default_left)
Definition: tree_model.h:116
XGBOOST_DEVICE void Reuse()
Reuse this deleted node.
Definition: tree_model.h:194
XGBOOST_DEVICE void SetRightChild(int nid)
set the right child
Definition: tree_model.h:163
XGBOOST_DEVICE bool DefaultLeft() const
when feature is unknown, whether goes to left child
Definition: tree_model.h:137
XGBOOST_DEVICE int LeftChild() const
index of left child
Definition: tree_model.h:124
XGBOOST_DEVICE int DefaultChild() const
index of default child when feature is missing
Definition: tree_model.h:128
XGBOOST_DEVICE SplitCondT SplitCond() const
Definition: tree_model.h:143
define regression tree to be the most common tree model.
Definition: tree_model.h:102
int MaxDepth(int nid) const
get maximum depth
Definition: tree_model.h:462
void SaveModel(Json *out) const override
saves the model config to a JSON object
bst_target_t NumTargets() const
The size of leaf weight.
Definition: tree_model.h:403
void WalkTree(Func func) const
Definition: tree_model.h:313
bool IsLeaf(bst_node_t nidx) const
Definition: tree_model.h:613
bool operator==(const RegTree &b) const
Definition: tree_model.h:304
const RTreeNodeStat & Stat(int nid) const
get node statistics given nid
Definition: tree_model.h:297
void ExpandNode(bst_node_t nidx, bst_feature_t split_index, float split_cond, bool default_left, linalg::VectorView< float const > base_weight, linalg::VectorView< float const > left_weight, linalg::VectorView< float const > right_weight)
Expands a leaf node into two additional leaf nodes for a multi-target tree.
bst_node_t Parent(bst_node_t nidx) const
Definition: tree_model.h:619
bst_node_t NumNodes() const noexcept
Get the total number of nodes including deleted ones in this tree.
Definition: tree_model.h:418
const Node & operator[](int nid) const
get node given nid
Definition: tree_model.h:282
bst_node_t DefaultChild(bst_node_t nidx) const
Definition: tree_model.h:604
void ExpandNode(bst_node_t nid, unsigned split_index, bst_float split_value, bool default_left, bst_float base_weight, bst_float left_leaf_weight, bst_float right_leaf_weight, bst_float loss_change, float sum_hess, float left_sum, float right_sum, bst_node_t leaf_right_child=kInvalidNodeId)
Expands a leaf node into two additional leaf nodes.
Node & operator[](int nid)
get node given nid
Definition: tree_model.h:278
RegTree()
Definition: tree_model.h:256
static constexpr bst_node_t kInvalidNodeId
Definition: tree_model.h:105
bst_feature_t SplitIndex(bst_node_t nidx) const
Definition: tree_model.h:586
bool IsRoot(bst_node_t nidx) const
Definition: tree_model.h:607
static constexpr uint32_t kDeletedNodeMarker
Definition: tree_model.h:106
bool IsMultiTarget() const
Whether this is a multi-target tree.
Definition: tree_model.h:399
bst_node_t NumExtraNodes() const noexcept
number of extra nodes besides the root
Definition: tree_model.h:428
bool DefaultLeft(bst_node_t nidx) const
Definition: tree_model.h:598
auto GetMultiTargetTree() const
Get the underlying implementaiton of multi-target tree.
Definition: tree_model.h:407
bst_node_t LeftChild(bst_node_t nidx) const
Definition: tree_model.h:625
bst_node_t GetNumLeaves() const
RegTree(bst_target_t n_targets, bst_feature_t n_features)
Constructor that initializes the tree model with shape.
Definition: tree_model.h:269
bst_node_t RightChild(bst_node_t nidx) const
Definition: tree_model.h:631
common::Span< uint32_t const > NodeCats(bst_node_t nidx) const
Get the bit storage for categories.
Definition: tree_model.h:552
bool IsLeftChild(bst_node_t nidx) const
Definition: tree_model.h:637
CategoricalSplitMatrix GetCategoriesMatrix() const
Definition: tree_model.h:578
RTreeNodeStat & Stat(int nid)
get node statistics given nid
Definition: tree_model.h:293
bst_float SplitCondT
Definition: tree_model.h:104
void ExpandCategorical(bst_node_t nid, bst_feature_t split_index, common::Span< const uint32_t > split_cat, bool default_left, bst_float base_weight, bst_float left_leaf_weight, bst_float right_leaf_weight, bst_float loss_change, float sum_hess, float left_sum, float right_sum)
Expands a leaf node with categories.
bool Equal(const RegTree &b) const
Compares whether 2 trees are equal from a user's perspective. The equality compares only non-deleted ...
std::vector< FeatureType > const & GetSplitTypes() const
Get split types for all nodes.
Definition: tree_model.h:543
void CollapseToLeaf(int rid, bst_float value)
collapse a non leaf node to a leaf node, delete its children
Definition: tree_model.h:245
bst_node_t NumValidNodes() const noexcept
Get the total number of valid nodes in this tree.
Definition: tree_model.h:422
void ChangeToLeaf(int rid, bst_float value)
change a non leaf node to a leaf node, delete its children
Definition: tree_model.h:233
const std::vector< RTreeNodeStat > & GetStats() const
get const reference to stats
Definition: tree_model.h:290
void SetLeaf(bst_node_t nidx, linalg::VectorView< float const > weight)
Set the leaf weight for a multi-target tree.
Definition: tree_model.h:453
const std::vector< Node > & GetNodes() const
get const reference to nodes
Definition: tree_model.h:287
void LoadModel(Json const &in) override
load the model from a JSON object
std::string DumpModel(const FeatureMap &fmap, bool with_stats, std::string format) const
dump the model in the requested format as a text string
FeatureType NodeSplitType(bst_node_t nidx) const
Get split type for a node.
Definition: tree_model.h:539
bst_feature_t NumFeatures() const noexcept
Get the number of features.
Definition: tree_model.h:414
common::Span< uint32_t const > GetSplitCategories() const
Definition: tree_model.h:546
bool HasCategoricalSplit() const
Whether this tree has categorical split.
Definition: tree_model.h:395
std::int32_t GetDepth(bst_node_t nid) const
get current depth
Definition: tree_model.h:439
static constexpr bst_node_t kRoot
Definition: tree_model.h:107
bst_node_t GetNumSplitNodes() const
auto const & GetSplitCategoriesPtr() const
Definition: tree_model.h:559
float SplitCond(bst_node_t nidx) const
Definition: tree_model.h:592
int MaxDepth()
get maximum depth
Definition: tree_model.h:470
bst_node_t Size() const
Definition: tree_model.h:645
span class implementation, based on ISO++20 span<T>. The interface should be the same.
Definition: span.h:431
constexpr XGBOOST_DEVICE pointer data() const __span_noexcept
Definition: span.h:550
XGBOOST_DEVICE auto subspan() const -> Span< element_type, detail::ExtentValue< Extent, Offset, Count >::value >
Definition: span.h:597
constexpr XGBOOST_DEVICE index_type size() const __span_noexcept
Definition: span.h:555
A tensor view with static type and dimension. It implements indexing and slicing.
Definition: linalg.h:277
The input data structure of xgboost.
Feature map data structure to help visualization and model dump.
Linear algebra related utilities.
Defines the abstract interface for different components in XGBoost.
Learner interface that integrates objective, gbm and evaluation together. This is the user facing XGB...
Definition: base.h:97
std::int32_t bst_node_t
Type for tree node index.
Definition: base.h:119
FeatureType
Definition: data.h:41
std::uint32_t bst_target_t
Type for indexing into output targets.
Definition: base.h:127
std::uint32_t bst_feature_t
Type for data column (feature) index.
Definition: base.h:107
float bst_float
float type, used for storing statistics
Definition: base.h:103
StringView MTNotImplemented()
Definition: tree_model.h:739
node statistics used in regression tree
Definition: tree_model.h:50
RTreeNodeStat(float loss_chg, float sum_hess, float weight)
Definition: tree_model.h:61
bst_float loss_chg
loss change caused by current split
Definition: tree_model.h:52
int leaf_child_cnt
number of child that is leaf node known up to now
Definition: tree_model.h:58
bst_float sum_hess
sum of hessian values, used to measure coverage of data
Definition: tree_model.h:54
bool operator==(const RTreeNodeStat &b) const
Definition: tree_model.h:63
bst_float base_weight
weight of current node
Definition: tree_model.h:56
Definition: tree_model.h:569
std::size_t size
Definition: tree_model.h:571
std::size_t beg
Definition: tree_model.h:570
CSR-like matrix for categorical splits.
Definition: tree_model.h:568
common::Span< uint32_t const > categories
Definition: tree_model.h:574
common::Span< Segment const > node_ptr
Definition: tree_model.h:575
common::Span< FeatureType const > split_type
Definition: tree_model.h:573
dense feature vector that can be taken by RegTree and can be construct from sparse feature vector.
Definition: tree_model.h:476
void HasMissing(bool has_missing)
Definition: tree_model.h:511
void Drop()
drop the trace after fill, must be called after fill.
Definition: tree_model.h:724
bool HasMissing() const
Definition: tree_model.h:736
bool IsMissing(size_t i) const
check whether i-th entry is missing
Definition: tree_model.h:734
size_t Size() const
returns the size of the feature vector
Definition: tree_model.h:726
void Init(size_t size)
initialize the vector with size vector
Definition: tree_model.h:707
common::Span< float > Data()
Definition: tree_model.h:513
void Fill(SparsePage::Inst const &inst)
fill the vector with sparse vector
Definition: tree_model.h:713
bst_float GetFvalue(size_t i) const
get ith value
Definition: tree_model.h:730
Definition: string_view.h:16
meta parameters of the tree
Definition: tree_model.h:30
bst_node_t num_deleted
The number of deleted nodes.
Definition: tree_model.h:34
bst_feature_t num_feature
The number of features used for tree construction.
Definition: tree_model.h:36
bool operator==(const TreeParam &b) const
Definition: tree_model.h:40
bst_node_t num_nodes
The number of nodes.
Definition: tree_model.h:32
void ToJson(Json *p_out) const
void FromJson(Json const &in)
bst_target_t size_leaf_vector
leaf vector size. Used by the vector leaf.
Definition: tree_model.h:38