Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion R-package/src/Makevars.in
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ OBJECTS= \
$(PKGROOT)/src/data/iterative_dmatrix.o \
$(PKGROOT)/src/predictor/predictor.o \
$(PKGROOT)/src/predictor/cpu_predictor.o \
$(PKGROOT)/src/predictor/cpu_treeshap.o \
$(PKGROOT)/src/predictor/treeshap.o \
$(PKGROOT)/src/tree/constraints.o \
$(PKGROOT)/src/tree/param.o \
$(PKGROOT)/src/tree/fit_stump.o \
Expand Down
2 changes: 1 addition & 1 deletion R-package/src/Makevars.win.in
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ OBJECTS= \
$(PKGROOT)/src/data/iterative_dmatrix.o \
$(PKGROOT)/src/predictor/predictor.o \
$(PKGROOT)/src/predictor/cpu_predictor.o \
$(PKGROOT)/src/predictor/cpu_treeshap.o \
$(PKGROOT)/src/predictor/treeshap.o \
$(PKGROOT)/src/tree/constraints.o \
$(PKGROOT)/src/tree/param.o \
$(PKGROOT)/src/tree/fit_stump.o \
Expand Down
4 changes: 2 additions & 2 deletions include/xgboost/linalg.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

#include <algorithm>
#include <cassert>
#include <cinttypes> // for int32_t
#include <cstddef> // for size_t
#include <cstddef> // for size_t
#include <cstdint> // for int32_t
#include <limits>
#include <string>
#include <tuple> // for make_tuple
Expand Down
107 changes: 16 additions & 91 deletions include/xgboost/tree_model.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
/**
* Copyright 2014-2025, XGBoost Contributors
* \file tree_model.h
*
* \brief model structure for tree
* \author Tianqi Chen
*/
#ifndef XGBOOST_TREE_MODEL_H_
#define XGBOOST_TREE_MODEL_H_

#include <dmlc/io.h>
#include <dmlc/parameter.h>
#include <xgboost/base.h>
#include <xgboost/data.h>
#include <xgboost/feature_map.h>
Expand All @@ -28,67 +26,24 @@
namespace xgboost {
class Json;

// FIXME(trivialfis): Once binary IO is gone, make this parameter internal as it should
// not be configured by users.
/*! \brief meta parameters of the tree */
struct TreeParam : public dmlc::Parameter<TreeParam> {
/*! \brief (Deprecated) number of start root */
int deprecated_num_roots{1};
/*! \brief total number of nodes */
int num_nodes{1};
/*!\brief number of deleted nodes */
int num_deleted{0};
/*! \brief maximum depth, this is a statistics of the tree */
int deprecated_max_depth{0};
/*! \brief number of features used for tree construction */
/** @brief meta parameters of the tree */
struct TreeParam {
/** @brief The number of nodes */
bst_node_t num_nodes{1};
/** @brief The number of deleted nodes */
bst_node_t num_deleted{0};
/** @brief The number of features used for tree construction */
bst_feature_t num_feature{0};
/*!
* \brief leaf vector size, used for vector tree
* used to store more than one dimensional information in tree
*/
/** @brief leaf vector size. Used by the vector leaf. */
bst_target_t size_leaf_vector{1};
/*! \brief reserved part, make sure alignment works for 64bit */
int reserved[31];
/*! \brief constructor */
TreeParam() {
// assert compact alignment
static_assert(sizeof(TreeParam) == (31 + 6) * sizeof(int), "TreeParam: 64 bit align");
std::memset(reserved, 0, sizeof(reserved));
}

// Swap byte order for all fields. Useful for transporting models between machines with different
// endianness (big endian vs little endian)
[[nodiscard]] TreeParam ByteSwap() const {
TreeParam x = *this;
dmlc::ByteSwap(&x.deprecated_num_roots, sizeof(x.deprecated_num_roots), 1);
dmlc::ByteSwap(&x.num_nodes, sizeof(x.num_nodes), 1);
dmlc::ByteSwap(&x.num_deleted, sizeof(x.num_deleted), 1);
dmlc::ByteSwap(&x.deprecated_max_depth, sizeof(x.deprecated_max_depth), 1);
dmlc::ByteSwap(&x.num_feature, sizeof(x.num_feature), 1);
dmlc::ByteSwap(&x.size_leaf_vector, sizeof(x.size_leaf_vector), 1);
dmlc::ByteSwap(x.reserved, sizeof(x.reserved[0]), sizeof(x.reserved) / sizeof(x.reserved[0]));
return x;
}

// declare the parameters
DMLC_DECLARE_PARAMETER(TreeParam) {
// only declare the parameters that can be set by the user.
// other arguments are set by the algorithm.
DMLC_DECLARE_FIELD(num_nodes).set_lower_bound(1).set_default(1);
DMLC_DECLARE_FIELD(num_feature)
.set_default(0)
.describe("Number of features used in tree construction.");
DMLC_DECLARE_FIELD(num_deleted).set_default(0);
DMLC_DECLARE_FIELD(size_leaf_vector)
.set_lower_bound(0)
.set_default(1)
.describe("Size of leaf vector, reserved for vector tree");
}

bool operator==(const TreeParam& b) const {
return num_nodes == b.num_nodes && num_deleted == b.num_deleted &&
num_feature == b.num_feature && size_leaf_vector == b.size_leaf_vector;
}

void FromJson(Json const& in);
void ToJson(Json* p_out) const;
};

/*! \brief node statistics used in regression tree */
Expand All @@ -109,16 +64,6 @@ struct RTreeNodeStat {
return loss_chg == b.loss_chg && sum_hess == b.sum_hess &&
base_weight == b.base_weight && leaf_child_cnt == b.leaf_child_cnt;
}
// Swap byte order for all fields. Useful for transporting models between machines with different
// endianness (big endian vs little endian)
[[nodiscard]] RTreeNodeStat ByteSwap() const {
RTreeNodeStat x = *this;
dmlc::ByteSwap(&x.loss_chg, sizeof(x.loss_chg), 1);
dmlc::ByteSwap(&x.sum_hess, sizeof(x.sum_hess), 1);
dmlc::ByteSwap(&x.base_weight, sizeof(x.base_weight), 1);
dmlc::ByteSwap(&x.leaf_child_cnt, sizeof(x.leaf_child_cnt), 1);
return x;
}
};

/**
Expand Down Expand Up @@ -166,12 +111,11 @@ class RegTree : public Model {
public:
XGBOOST_DEVICE Node() {
// assert compact alignment
static_assert(sizeof(Node) == 4 * sizeof(int) + sizeof(Info),
"Node: 64 bit align");
static_assert(sizeof(Node) == 4 * sizeof(int) + sizeof(Info), "Node: 64 bit align");
}
Node(int32_t cleft, int32_t cright, int32_t parent,
uint32_t split_ind, float split_cond, bool default_left) :
parent_{parent}, cleft_{cleft}, cright_{cright} {
Node(int32_t cleft, int32_t cright, int32_t parent, uint32_t split_ind, float split_cond,
bool default_left)
: parent_{parent}, cleft_{cleft}, cright_{cright} {
this->SetParent(parent_);
this->SetSplit(split_ind, split_cond, default_left);
}
Expand Down Expand Up @@ -261,16 +205,6 @@ class RegTree : public Model {
info_.leaf_value == b.info_.leaf_value;
}

[[nodiscard]] Node ByteSwap() const {
Node x = *this;
dmlc::ByteSwap(&x.parent_, sizeof(x.parent_), 1);
dmlc::ByteSwap(&x.cleft_, sizeof(x.cleft_), 1);
dmlc::ByteSwap(&x.cright_, sizeof(x.cright_), 1);
dmlc::ByteSwap(&x.sindex_, sizeof(x.sindex_), 1);
dmlc::ByteSwap(&x.info_, sizeof(x.info_), 1);
return x;
}

private:
/*!
* \brief in leaf node, we have weights, in non-leaf nodes,
Expand Down Expand Up @@ -320,7 +254,6 @@ class RegTree : public Model {
}

RegTree() {
param_.Init(Args{});
nodes_.resize(param_.num_nodes);
stats_.resize(param_.num_nodes);
split_types_.resize(param_.num_nodes, FeatureType::kNumerical);
Expand Down Expand Up @@ -589,14 +522,6 @@ class RegTree : public Model {
bool has_missing_;
};

/*!
* \brief calculate the approximate feature contributions for the given root
* \param feat dense feature vector, if the feature is missing the field is set to NaN
* \param out_contribs output vector to hold the contributions
*/
void CalculateContributionsApprox(const RegTree::FVec& feat,
std::vector<float>* mean_values,
bst_float* out_contribs) const;
/*!
* \brief dump the model in the requested format as a text string
* \param fmap feature map that may help give interpretations of feature
Expand Down
9 changes: 2 additions & 7 deletions src/gbm/gblinear_model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
#include "xgboost/json.h"
#include "gblinear_model.h"

namespace xgboost {
namespace gbm {

namespace xgboost::gbm {
void GBLinearModel::SaveModel(Json* p_out) const {
auto& out = *p_out;

Expand Down Expand Up @@ -42,7 +40,4 @@ void GBLinearModel::LoadModel(Json const& in) {
this->num_boosted_rounds = 0;
}
}

DMLC_REGISTER_PARAMETER(DeprecatedGBLinearModelParam);
} // namespace gbm
} // namespace xgboost
} // namespace xgboost::gbm
28 changes: 1 addition & 27 deletions src/gbm/gblinear_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,14 @@
#include "xgboost/feature_map.h"
#include "xgboost/model.h"
#include "xgboost/json.h"
#include "xgboost/parameter.h"

namespace xgboost {
class Json;
namespace gbm {
// Deprecated in 1.0.0. model parameter. Only staying here for compatible binary model IO.
struct DeprecatedGBLinearModelParam : public dmlc::Parameter<DeprecatedGBLinearModelParam> {
// number of feature dimension
uint32_t deprecated_num_feature;
// deprecated. use learner_model_param_->num_output_group.
int32_t deprecated_num_output_group;
// reserved field
int32_t reserved[32];
// constructor
DeprecatedGBLinearModelParam() {
static_assert(sizeof(*this) == sizeof(int32_t) * 34,
"Model parameter size can not be changed.");
std::memset(this, 0, sizeof(DeprecatedGBLinearModelParam));
}

DMLC_DECLARE_PARAMETER(DeprecatedGBLinearModelParam) {
DMLC_DECLARE_FIELD(deprecated_num_feature);
DMLC_DECLARE_FIELD(deprecated_num_output_group);
}
};

// model for linear booster
class GBLinearModel : public Model {
private:
// Deprecated in 1.0.0
DeprecatedGBLinearModelParam param_;

public:
int32_t num_boosted_rounds{0};
std::int32_t num_boosted_rounds{0};
LearnerModelParam const* learner_model_param;

public:
Expand Down
26 changes: 4 additions & 22 deletions src/gbm/gbtree_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,13 @@ struct GBTreeModelParam : public dmlc::Parameter<GBTreeModelParam> {
/**
* \brief number of trees
*/
std::int32_t num_trees;
std::int32_t num_trees{0};
/**
* \brief Number of trees for a forest.
*/
std::int32_t num_parallel_tree;
/*! \brief reserved parameters */
int32_t reserved[38];

/*! \brief constructor */
GBTreeModelParam() {
std::memset(this, 0, sizeof(GBTreeModelParam)); // FIXME(trivialfis): Why?
static_assert(sizeof(GBTreeModelParam) == (4 + 2 + 2 + 32) * sizeof(int32_t),
"64/32 bit compatibility issue");
num_parallel_tree = 1;
}
std::int32_t num_parallel_tree{1};

GBTreeModelParam() = default;

// declare parameters, only declare those that need to be set.
DMLC_DECLARE_PARAMETER(GBTreeModelParam) {
Expand All @@ -69,16 +61,6 @@ struct GBTreeModelParam : public dmlc::Parameter<GBTreeModelParam> {
"Number of parallel trees constructed during each iteration."
" This option is used to support boosted random forest.");
}

// Swap byte order for all fields. Useful for transporting models between machines with different
// endianness (big endian vs little endian)
GBTreeModelParam ByteSwap() const {
GBTreeModelParam x = *this;
dmlc::ByteSwap(&x.num_trees, sizeof(x.num_trees), 1);
dmlc::ByteSwap(&x.num_parallel_tree, sizeof(x.num_parallel_tree), 1);
dmlc::ByteSwap(x.reserved, sizeof(x.reserved[0]), sizeof(x.reserved) / sizeof(x.reserved[0]));
return x;
}
};

struct GBTreeModel : public Model {
Expand Down
47 changes: 10 additions & 37 deletions src/learner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
#include "common/random.h" // for GlobalRandom
#include "common/timer.h" // for Monitor
#include "common/version.h" // for Version
#include "dmlc/endian.h" // for ByteSwap, DMLC_IO_NO_ENDIAN_SWAP
#include "xgboost/base.h" // for Args, bst_float, GradientPair, bst_feature_t, ...
#include "xgboost/context.h" // for Context
#include "xgboost/data.h" // for DMatrix, MetaInfo
Expand Down Expand Up @@ -84,22 +83,22 @@ T& UsePtr(T& ptr) { // NOLINT
*/
struct LearnerModelParamLegacy : public dmlc::Parameter<LearnerModelParamLegacy> {
/* \brief global bias */
bst_float base_score;
bst_float base_score{ObjFunction::DefaultBaseScore()};
/* \brief number of features */
bst_feature_t num_feature;
bst_feature_t num_feature{0};
/* \brief number of classes, if it is multi-class classification */
std::int32_t num_class;
std::int32_t num_class{0};
/*! \brief Model contain additional properties */
int32_t contain_extra_attrs;
int32_t contain_extra_attrs{0};
/*! \brief Model contain eval metrics */
int32_t contain_eval_metrics;
int32_t contain_eval_metrics{0};
/*! \brief the version of XGBoost. */
std::uint32_t major_version;
std::uint32_t minor_version;
std::int32_t major_version{std::get<0>(Version::Self())};
std::int32_t minor_version{std::get<1>(Version::Self())};
/**
* \brief Number of target variables.
*/
bst_target_t num_target;
bst_target_t num_target{1};
/**
* \brief Whether we should calculate the base score from training data.
*
Expand All @@ -110,19 +109,8 @@ struct LearnerModelParamLegacy : public dmlc::Parameter<LearnerModelParamLegacy>
* of bool for the ease of serialization.
*/
std::int32_t boost_from_average{true};
/*! \brief reserved field */
int reserved[25];
/*! \brief constructor */
LearnerModelParamLegacy() {
std::memset(this, 0, sizeof(LearnerModelParamLegacy));
base_score = ObjFunction::DefaultBaseScore();
num_target = 1;
major_version = std::get<0>(Version::Self());
minor_version = std::get<1>(Version::Self());
boost_from_average = true;
static_assert(sizeof(LearnerModelParamLegacy) == 136,
"Do not change the size of this struct, as it will break binary IO.");
}

LearnerModelParamLegacy() = default;

// Skip other legacy fields.
[[nodiscard]] Json ToJson() const {
Expand Down Expand Up @@ -175,21 +163,6 @@ struct LearnerModelParamLegacy : public dmlc::Parameter<LearnerModelParamLegacy>
from_chars(str.c_str(), str.c_str() + str.size(), base_score);
}

[[nodiscard]] LearnerModelParamLegacy ByteSwap() const {
LearnerModelParamLegacy x = *this;
dmlc::ByteSwap(&x.base_score, sizeof(x.base_score), 1);
dmlc::ByteSwap(&x.num_feature, sizeof(x.num_feature), 1);
dmlc::ByteSwap(&x.num_class, sizeof(x.num_class), 1);
dmlc::ByteSwap(&x.contain_extra_attrs, sizeof(x.contain_extra_attrs), 1);
dmlc::ByteSwap(&x.contain_eval_metrics, sizeof(x.contain_eval_metrics), 1);
dmlc::ByteSwap(&x.major_version, sizeof(x.major_version), 1);
dmlc::ByteSwap(&x.minor_version, sizeof(x.minor_version), 1);
dmlc::ByteSwap(&x.num_target, sizeof(x.num_target), 1);
dmlc::ByteSwap(&x.boost_from_average, sizeof(x.boost_from_average), 1);
dmlc::ByteSwap(x.reserved, sizeof(x.reserved[0]), sizeof(x.reserved) / sizeof(x.reserved[0]));
return x;
}

template <typename Container>
Args UpdateAllowUnknown(Container const& kwargs) {
// Detect whether user has made their own base score.
Expand Down
Loading
Loading