11/* *
22 * Copyright 2014-2025, XGBoost Contributors
3- * \file tree_model.h
3+ *
44 * \brief model structure for tree
55 * \author Tianqi Chen
66 */
77#ifndef XGBOOST_TREE_MODEL_H_
88#define XGBOOST_TREE_MODEL_H_
99
10- #include < dmlc/io.h>
11- #include < dmlc/parameter.h>
1210#include < xgboost/base.h>
1311#include < xgboost/data.h>
1412#include < xgboost/feature_map.h>
2826namespace xgboost {
2927class Json ;
3028
31- // FIXME(trivialfis): Once binary IO is gone, make this parameter internal as it should
32- // not be configured by users.
33- /* ! \brief meta parameters of the tree */
34- struct TreeParam : public dmlc ::Parameter<TreeParam> {
35- /* ! \brief (Deprecated) number of start root */
36- int deprecated_num_roots{1 };
37- /* ! \brief total number of nodes */
38- int num_nodes{1 };
39- /* !\brief number of deleted nodes */
40- int num_deleted{0 };
41- /* ! \brief maximum depth, this is a statistics of the tree */
42- int deprecated_max_depth{0 };
43- /* ! \brief number of features used for tree construction */
29+ /* * @brief meta parameters of the tree */
30+ struct TreeParam {
31+ /* * @brief The number of nodes */
32+ bst_node_t num_nodes{1 };
33+ /* * @brief The number of deleted nodes */
34+ bst_node_t num_deleted{0 };
35+ /* * @brief The number of features used for tree construction */
4436 bst_feature_t num_feature{0 };
45- /* !
46- * \brief leaf vector size, used for vector tree
47- * used to store more than one dimensional information in tree
48- */
37+ /* * @brief leaf vector size. Used by the vector leaf. */
4938 bst_target_t size_leaf_vector{1 };
50- /* ! \brief reserved part, make sure alignment works for 64bit */
51- int reserved[31 ];
52- /* ! \brief constructor */
53- TreeParam () {
54- // assert compact alignment
55- static_assert (sizeof (TreeParam) == (31 + 6 ) * sizeof (int ), " TreeParam: 64 bit align" );
56- std::memset (reserved, 0 , sizeof (reserved));
57- }
58-
59- // Swap byte order for all fields. Useful for transporting models between machines with different
60- // endianness (big endian vs little endian)
61- [[nodiscard]] TreeParam ByteSwap () const {
62- TreeParam x = *this ;
63- dmlc::ByteSwap (&x.deprecated_num_roots , sizeof (x.deprecated_num_roots ), 1 );
64- dmlc::ByteSwap (&x.num_nodes , sizeof (x.num_nodes ), 1 );
65- dmlc::ByteSwap (&x.num_deleted , sizeof (x.num_deleted ), 1 );
66- dmlc::ByteSwap (&x.deprecated_max_depth , sizeof (x.deprecated_max_depth ), 1 );
67- dmlc::ByteSwap (&x.num_feature , sizeof (x.num_feature ), 1 );
68- dmlc::ByteSwap (&x.size_leaf_vector , sizeof (x.size_leaf_vector ), 1 );
69- dmlc::ByteSwap (x.reserved , sizeof (x.reserved [0 ]), sizeof (x.reserved ) / sizeof (x.reserved [0 ]));
70- return x;
71- }
72-
73- // declare the parameters
74- DMLC_DECLARE_PARAMETER (TreeParam) {
75- // only declare the parameters that can be set by the user.
76- // other arguments are set by the algorithm.
77- DMLC_DECLARE_FIELD (num_nodes).set_lower_bound (1 ).set_default (1 );
78- DMLC_DECLARE_FIELD (num_feature)
79- .set_default (0 )
80- .describe (" Number of features used in tree construction." );
81- DMLC_DECLARE_FIELD (num_deleted).set_default (0 );
82- DMLC_DECLARE_FIELD (size_leaf_vector)
83- .set_lower_bound (0 )
84- .set_default (1 )
85- .describe (" Size of leaf vector, reserved for vector tree" );
86- }
8739
8840 bool operator ==(const TreeParam& b) const {
8941 return num_nodes == b.num_nodes && num_deleted == b.num_deleted &&
9042 num_feature == b.num_feature && size_leaf_vector == b.size_leaf_vector ;
9143 }
44+
45+ void FromJson (Json const & in);
46+ void ToJson (Json* p_out) const ;
9247};
9348
9449/* ! \brief node statistics used in regression tree */
@@ -109,16 +64,6 @@ struct RTreeNodeStat {
10964 return loss_chg == b.loss_chg && sum_hess == b.sum_hess &&
11065 base_weight == b.base_weight && leaf_child_cnt == b.leaf_child_cnt ;
11166 }
112- // Swap byte order for all fields. Useful for transporting models between machines with different
113- // endianness (big endian vs little endian)
114- [[nodiscard]] RTreeNodeStat ByteSwap () const {
115- RTreeNodeStat x = *this ;
116- dmlc::ByteSwap (&x.loss_chg , sizeof (x.loss_chg ), 1 );
117- dmlc::ByteSwap (&x.sum_hess , sizeof (x.sum_hess ), 1 );
118- dmlc::ByteSwap (&x.base_weight , sizeof (x.base_weight ), 1 );
119- dmlc::ByteSwap (&x.leaf_child_cnt , sizeof (x.leaf_child_cnt ), 1 );
120- return x;
121- }
12267};
12368
12469/* *
@@ -166,12 +111,11 @@ class RegTree : public Model {
166111 public:
167112 XGBOOST_DEVICE Node () {
168113 // assert compact alignment
169- static_assert (sizeof (Node) == 4 * sizeof (int ) + sizeof (Info),
170- " Node: 64 bit align" );
114+ static_assert (sizeof (Node) == 4 * sizeof (int ) + sizeof (Info), " Node: 64 bit align" );
171115 }
172- Node (int32_t cleft, int32_t cright, int32_t parent,
173- uint32_t split_ind, float split_cond, bool default_left) :
174- parent_{parent}, cleft_{cleft}, cright_{cright} {
116+ Node (int32_t cleft, int32_t cright, int32_t parent, uint32_t split_ind, float split_cond,
117+ bool default_left)
118+ : parent_{parent}, cleft_{cleft}, cright_{cright} {
175119 this ->SetParent (parent_);
176120 this ->SetSplit (split_ind, split_cond, default_left);
177121 }
@@ -261,16 +205,6 @@ class RegTree : public Model {
261205 info_.leaf_value == b.info_ .leaf_value ;
262206 }
263207
264- [[nodiscard]] Node ByteSwap () const {
265- Node x = *this ;
266- dmlc::ByteSwap (&x.parent_ , sizeof (x.parent_ ), 1 );
267- dmlc::ByteSwap (&x.cleft_ , sizeof (x.cleft_ ), 1 );
268- dmlc::ByteSwap (&x.cright_ , sizeof (x.cright_ ), 1 );
269- dmlc::ByteSwap (&x.sindex_ , sizeof (x.sindex_ ), 1 );
270- dmlc::ByteSwap (&x.info_ , sizeof (x.info_ ), 1 );
271- return x;
272- }
273-
274208 private:
275209 /* !
276210 * \brief in leaf node, we have weights, in non-leaf nodes,
@@ -320,7 +254,6 @@ class RegTree : public Model {
320254 }
321255
322256 RegTree () {
323- param_.Init (Args{});
324257 nodes_.resize (param_.num_nodes );
325258 stats_.resize (param_.num_nodes );
326259 split_types_.resize (param_.num_nodes , FeatureType::kNumerical );
@@ -589,14 +522,6 @@ class RegTree : public Model {
589522 bool has_missing_;
590523 };
591524
592- /* !
593- * \brief calculate the approximate feature contributions for the given root
594- * \param feat dense feature vector, if the feature is missing the field is set to NaN
595- * \param out_contribs output vector to hold the contributions
596- */
597- void CalculateContributionsApprox (const RegTree::FVec& feat,
598- std::vector<float >* mean_values,
599- bst_float* out_contribs) const ;
600525 /* !
601526 * \brief dump the model in the requested format as a text string
602527 * \param fmap feature map that may help give interpretations of feature
0 commit comments