Pass pointer to model parameters. (#5101)

* Pass pointer to model parameters.

This PR de-duplicates most of the model parameters except the one in
`tree_model.h`.  One difficulty is `base_score` is a model property but can be
changed at runtime by objective function.  Hence when performing model IO, we
need to save the one provided by users, instead of the one transformed by
objective.  Here we created an immutable version of `LearnerModelParam` that
represents the value of model parameter after configuration.
This commit is contained in:
Jiaming Yuan
2019-12-10 12:11:22 +08:00
committed by GitHub
parent 979f74d51a
commit e089e16e3d
33 changed files with 623 additions and 404 deletions

View File

@@ -112,7 +112,7 @@ using bst_float = float; // NOLINT
/*! \brief Type for data column (feature) index. */
using bst_feature_t = uint32_t; // NOLINT
/*! \breif Type for data row index.
/*! \brief Type for data row index.
*
* Be careful `std::size_t' is implementation-defined. Meaning that the binary
* representation of DMatrix might not be portable across platform. Booster model should

View File

@@ -22,8 +22,6 @@
#include <vector>
namespace xgboost {
// forward declare learner.
class LearnerImpl;
// forward declare dmatrix.
class DMatrix;

View File

@@ -11,10 +11,8 @@
#include <dmlc/registry.h>
#include <xgboost/base.h>
#include <xgboost/data.h>
#include <xgboost/objective.h>
#include <xgboost/feature_map.h>
#include <xgboost/generic_parameters.h>
#include <xgboost/host_device_vector.h>
#include <xgboost/model.h>
#include <vector>
#include <utility>
@@ -23,6 +21,14 @@
#include <memory>
namespace xgboost {
class Json;
class FeatureMap;
class ObjFunction;
struct GenericParameter;
struct LearnerModelParam;
/*!
* \brief interface of gradient boosting model.
*/
@@ -117,13 +123,14 @@ class GradientBooster {
* \param condition_feature feature to condition on (i.e. fix) during calculations
*/
virtual void PredictContribution(DMatrix* dmat,
std::vector<bst_float>* out_contribs,
unsigned ntree_limit = 0, bool approximate = false,
int condition = 0, unsigned condition_feature = 0) = 0;
std::vector<bst_float>* out_contribs,
unsigned ntree_limit = 0,
bool approximate = false, int condition = 0,
unsigned condition_feature = 0) = 0;
virtual void PredictInteractionContributions(DMatrix* dmat,
std::vector<bst_float>* out_contribs,
unsigned ntree_limit, bool approximate) = 0;
std::vector<bst_float>* out_contribs,
unsigned ntree_limit, bool approximate) = 0;
/*!
* \brief dump the model in the requested format
@@ -136,21 +143,22 @@ class GradientBooster {
bool with_stats,
std::string format) const = 0;
/*!
* \brief Whether the current booster use GPU.
* \brief Whether the current booster uses GPU.
*/
virtual bool UseGPU() const = 0;
/*!
* \brief create a gradient booster from given name
* \param name name of gradient booster
* \param generic_param Pointer to runtime parameters
* \param learner_model_param pointer to global model parameters
* \param cache_mats The cache data matrix of the Booster.
* \param base_margin The base margin of prediction.
* \return The created booster.
*/
static GradientBooster* Create(
const std::string& name,
GenericParameter const* gparam,
const std::vector<std::shared_ptr<DMatrix> >& cache_mats,
bst_float base_margin);
GenericParameter const* generic_param,
LearnerModelParam const* learner_model_param,
const std::vector<std::shared_ptr<DMatrix> >& cache_mats);
static void AssertGPUSupport() {
#ifndef XGBOOST_USE_CUDA
@@ -166,7 +174,7 @@ struct GradientBoosterReg
: public dmlc::FunctionRegEntryBase<
GradientBoosterReg,
std::function<GradientBooster* (const std::vector<std::shared_ptr<DMatrix> > &cached_mats,
bst_float base_margin)> > {
LearnerModelParam const* learner_model_param)> > {
};
/*!

View File

@@ -13,8 +13,9 @@
namespace xgboost {
struct GenericParameter : public XGBoostParameter<GenericParameter> {
// Constant representing the device ID of CPU.
static int constexpr kCpuId = -1;
static int32_t constexpr kCpuId = -1;
public:
// stored random seed
int seed;
// whether seed the PRNG each iteration
@@ -26,8 +27,7 @@ struct GenericParameter : public XGBoostParameter<GenericParameter> {
int gpu_id;
// gpu page size in external memory mode, 0 means using the default.
size_t gpu_page_size;
void ConfigureGpuId(bool require_gpu);
bool enable_experimental_json_serialization {false};
void CheckDeprecated() {
if (this->n_gpus != 0) {
@@ -36,6 +36,12 @@ struct GenericParameter : public XGBoostParameter<GenericParameter> {
<< this->__MANAGER__()->Find("n_gpus")->GetFieldInfo().description;
}
}
/*!
* \brief Configure the parameter `gpu_id'.
*
* \param require_gpu Whether GPU is explicitly required from user.
*/
void ConfigureGpuId(bool require_gpu);
// declare parameters
DMLC_DECLARE_PARAMETER(GenericParameter) {
@@ -60,6 +66,10 @@ struct GenericParameter : public XGBoostParameter<GenericParameter> {
.set_default(0)
.set_lower_bound(0)
.describe("GPU page size when running in external memory mode.");
DMLC_DECLARE_FIELD(enable_experimental_json_serialization)
.set_default(false)
.describe("Enable using JSON for memory serialization (Python Pickle, "
"rabit checkpoints etc.).");
DMLC_DECLARE_FIELD(n_gpus)
.set_default(0)
.set_range(0, 1)

View File

@@ -9,13 +9,10 @@
#define XGBOOST_LEARNER_H_
#include <rabit/rabit.h>
#include <xgboost/base.h>
#include <xgboost/gbm.h>
#include <xgboost/metric.h>
#include <xgboost/objective.h>
#include <xgboost/feature_map.h>
#include <xgboost/generic_parameters.h>
#include <xgboost/host_device_vector.h>
#include <xgboost/model.h>
#include <utility>
@@ -26,6 +23,12 @@
namespace xgboost {
class Metric;
class GradientBooster;
class ObjFunction;
class DMatrix;
class Json;
/*!
* \brief Learner class that does training and prediction.
* This is the user facing module of xgboost training.
@@ -45,7 +48,7 @@ namespace xgboost {
class Learner : public Model, public rabit::Serializable {
public:
/*! \brief virtual destructor */
~Learner() override = default;
~Learner() override;
/*!
* \brief Configure Learner based on set parameters.
*/
@@ -180,8 +183,6 @@ class Learner : public Model, public rabit::Serializable {
virtual const std::map<std::string, std::string>& GetConfigurationArguments() const = 0;
protected:
/*! \brief internal base score of the model */
bst_float base_score_;
/*! \brief objective function */
std::unique_ptr<ObjFunction> obj_;
/*! \brief The gradient booster used by the model*/
@@ -189,7 +190,26 @@ class Learner : public Model, public rabit::Serializable {
/*! \brief The evaluation metrics used to evaluate the model. */
std::vector<std::unique_ptr<Metric> > metrics_;
/*! \brief Training parameter. */
GenericParameter generic_param_;
GenericParameter generic_parameters_;
};
struct LearnerModelParamLegacy;
/*
* \brief Basic Model Parameters, used to describe the booster.
*/
struct LearnerModelParam {
/* \brief global bias */
bst_float base_score;
/* \brief number of features */
uint32_t num_feature;
/* \brief number of classes, if it is multi-class classification */
uint32_t num_output_group;
LearnerModelParam() : base_score {0.5}, num_feature{0}, num_output_group{0} {}
// As the old `LearnerModelParamLegacy` is still used by binary IO, we keep
// this one as an immutable copy.
LearnerModelParam(LearnerModelParamLegacy const& user_param, float base_margin);
};
} // namespace xgboost

View File

@@ -1,5 +1,5 @@
/*!
* Copyright 2014 by Contributors
* Copyright 2014-2019 by Contributors
* \file objective.h
* \brief interface of objective function used by xgboost.
* \author Tianqi Chen, Kailong Chen

View File

@@ -27,6 +27,9 @@ namespace xgboost {
struct PathElement; // forward declaration
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 */
@@ -36,7 +39,7 @@ struct TreeParam : public dmlc::Parameter<TreeParam> {
/*!\brief number of deleted nodes */
int num_deleted;
/*! \brief maximum depth, this is a statistics of the tree */
int max_depth;
int deprecated_max_depth;
/*! \brief number of features used for tree construction */
int num_feature;
/*!
@@ -67,7 +70,7 @@ struct TreeParam : public dmlc::Parameter<TreeParam> {
bool operator==(const TreeParam& b) const {
return num_nodes == b.num_nodes &&
num_deleted == b.num_deleted && max_depth == b.max_depth &&
num_deleted == b.num_deleted &&
num_feature == b.num_feature &&
size_leaf_vector == b.size_leaf_vector;
}

View File

@@ -22,6 +22,9 @@
#include <string>
namespace xgboost {
class Json;
/*!
* \brief interface of tree update module, that performs update of a tree.
*/