Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use `UpdateAllowUnknown' for non-model related parameter. #4961

Merged
merged 2 commits into from
Oct 23, 2019
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 include/xgboost/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ using GradientPairInteger = detail::GradientPairInternal<int64_t>;
using Args = std::vector<std::pair<std::string, std::string> >;

/*! \brief small eps gap for minimum split decision. */
const bst_float kRtEps = 1e-6f;
constexpr bst_float kRtEps = 1e-6f;

/*! \brief define unsigned long for openmp loop */
using omp_ulong = dmlc::omp_ulong; // NOLINT
Expand Down
3 changes: 1 addition & 2 deletions include/xgboost/generic_parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
#ifndef XGBOOST_GENERIC_PARAMETERS_H_
#define XGBOOST_GENERIC_PARAMETERS_H_

#include <dmlc/parameter.h>
#include <xgboost/logging.h>
#include <xgboost/parameter.h>

#include <string>

namespace xgboost {
struct GenericParameter : public dmlc::Parameter<GenericParameter> {
struct GenericParameter : public XGBoostParameter<GenericParameter> {
// stored random seed
int seed;
// whether seed the PRNG each iteration
Expand Down
7 changes: 4 additions & 3 deletions include/xgboost/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#define XGBOOST_JSON_H_

#include <xgboost/logging.h>
#include <xgboost/parameter.h>
#include <string>

#include <map>
Expand Down Expand Up @@ -533,7 +534,7 @@ using Null = JsonNull;
// Utils tailored for XGBoost.

template <typename Type>
Object toJson(dmlc::Parameter<Type> const& param) {
Object toJson(XGBoostParameter<Type> const& param) {
Object obj;
for (auto const& kv : param.__DICT__()) {
obj[kv.first] = kv.second;
Expand All @@ -542,13 +543,13 @@ Object toJson(dmlc::Parameter<Type> const& param) {
}

template <typename Type>
void fromJson(Json const& obj, dmlc::Parameter<Type>* param) {
void fromJson(Json const& obj, XGBoostParameter<Type>* param) {
auto const& j_param = get<Object const>(obj);
std::map<std::string, std::string> m;
for (auto const& kv : j_param) {
m[kv.first] = get<String const>(kv.second);
}
param->InitAllowUnknown(m);
param->UpdateAllowUnknown(m);
}
} // namespace xgboost
#endif // XGBOOST_JSON_H_
5 changes: 3 additions & 2 deletions include/xgboost/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
#define XGBOOST_LOGGING_H_

#include <dmlc/logging.h>
#include <dmlc/parameter.h>
#include <dmlc/thread_local.h>

#include <xgboost/base.h>
#include <xgboost/parameter.h>

#include <sstream>
#include <map>
Expand All @@ -35,7 +36,7 @@ class BaseLogger {
};

// Parsing both silent and debug_verbose is to provide backward compatibility.
struct ConsoleLoggerParam : public dmlc::Parameter<ConsoleLoggerParam> {
struct ConsoleLoggerParam : public XGBoostParameter<ConsoleLoggerParam> {
bool silent; // deprecated.
int verbosity;

Expand Down
16 changes: 12 additions & 4 deletions include/xgboost/tree_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ struct TreeParam : public dmlc::Parameter<TreeParam> {
// other arguments are set by the algorithm.
DMLC_DECLARE_FIELD(num_roots).set_lower_bound(1).set_default(1)
.describe("Number of start root of trees.");
DMLC_DECLARE_FIELD(num_nodes).set_lower_bound(1).set_default(1);
DMLC_DECLARE_FIELD(num_feature)
.describe("Number of features used in tree construction.");
DMLC_DECLARE_FIELD(size_leaf_vector).set_lower_bound(0).set_default(0)
Expand All @@ -83,7 +84,7 @@ struct RTreeNodeStat {
/*! \brief weight of current node */
bst_float base_weight;
/*! \brief number of child that is leaf node known up to now */
int leaf_child_cnt;
int leaf_child_cnt {0};
bool operator==(const RTreeNodeStat& b) const {
return loss_chg == b.loss_chg && sum_hess == b.sum_hess &&
base_weight == b.base_weight && leaf_child_cnt == b.leaf_child_cnt;
Expand All @@ -98,6 +99,7 @@ class RegTree : public Model {
public:
/*! \brief auxiliary statistics of node to help tree building */
using SplitCondT = bst_float;
static constexpr int32_t kInvalidNodeId {-1};
/*! \brief tree node */
class Node {
public:
Expand All @@ -106,6 +108,12 @@ class RegTree : public Model {
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} {
this->SetSplit(split_ind, split_cond, default_left);
}

/*! \brief index of left child */
XGBOOST_DEVICE int LeftChild() const {
return this->cleft_;
Expand Down Expand Up @@ -219,11 +227,11 @@ class RegTree : public Model {
};
// pointer to parent, highest bit is used to
// indicate whether it's a left child or not
int parent_;
int32_t parent_{kInvalidNodeId};
// pointer to left, right
int cleft_, cright_;
int32_t cleft_{kInvalidNodeId}, cright_{kInvalidNodeId};
// split feature index, left split or right split depends on the highest bit
unsigned sindex_{0};
uint32_t sindex_{0};
// extra info
Info info_;
};
Expand Down
6 changes: 3 additions & 3 deletions plugin/example/custom_obj.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* This plugin defines the additional metric function.
*/
#include <xgboost/base.h>
#include <dmlc/parameter.h>
#include <xgboost/parameter.h>
#include <xgboost/objective.h>
#include <xgboost/json.h>

Expand All @@ -16,7 +16,7 @@ namespace obj {
// You do not have to use it.
// see http://dmlc-core.readthedocs.org/en/latest/parameter.html
// for introduction of this module.
struct MyLogisticParam : public dmlc::Parameter<MyLogisticParam> {
struct MyLogisticParam : public XGBoostParameter<MyLogisticParam> {
float scale_neg_weight;
// declare parameters
DMLC_DECLARE_PARAMETER(MyLogisticParam) {
Expand All @@ -32,7 +32,7 @@ DMLC_REGISTER_PARAMETER(MyLogisticParam);
class MyLogistic : public ObjFunction {
public:
void Configure(const std::vector<std::pair<std::string, std::string> >& args) override {
param_.InitAllowUnknown(args);
param_.UpdateAllowUnknown(args);
}
void GetGradient(const HostDeviceVector<bst_float> &preds,
const MetaInfo &info,
Expand Down
5 changes: 3 additions & 2 deletions plugin/lz4/sparse_page_lz4_format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
* \file sparse_page_lz4_format.cc
* XGBoost Plugin to enable LZ4 compressed format on the external memory pages.
*/
#include <dmlc/registry.h>

#include <xgboost/data.h>
#include <xgboost/logging.h>
#include <dmlc/registry.h>
#include <dmlc/parameter.h>
#include <xgboost/parameter.h>
#include <lz4.h>
#include <lz4hc.h>
#include "../../src/data/sparse_page_writer.h"
Expand Down
17 changes: 8 additions & 9 deletions python-package/xgboost/compat.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# coding: utf-8
# pylint: disable= invalid-name, unused-import
"""For compatibility"""
"""For compatibility and optional dependencies."""

from __future__ import absolute_import

Expand All @@ -16,22 +16,22 @@
# pylint: disable=invalid-name, redefined-builtin
STRING_TYPES = (str,)


def py_str(x):
"""convert c string back to python string"""
return x.decode('utf-8')
else:
STRING_TYPES = (basestring,) # pylint: disable=undefined-variable


def py_str(x):
"""convert c string back to python string"""
return x

########################################################################################

###############################################################################
# START NUMPY PATHLIB ATTRIBUTION
########################################################################################
# os.PathLike compatibility used in Numpy: https://github.com/numpy/numpy/tree/v1.17.0
###############################################################################
# os.PathLike compatibility used in Numpy:
# https://github.com/numpy/numpy/tree/v1.17.0
# Attribution:
# https://github.com/numpy/numpy/blob/v1.17.0/numpy/compat/py3k.py#L188-L247
# Backport os.fs_path, os.PathLike, and PurePath.__fspath__
Expand All @@ -56,7 +56,6 @@ def __subclasshook__(cls, subclass):
return True
return hasattr(subclass, '__fspath__')


def os_fspath(path):
"""Return the path representation of a path-like object.
If str or bytes is passed in, it is returned unchanged. Otherwise the
Expand Down Expand Up @@ -84,9 +83,9 @@ def os_fspath(path):
raise TypeError("expected {}.__fspath__() to return str or bytes, "
"not {}".format(path_type.__name__,
type(path_repr).__name__))
########################################################################################
###############################################################################
# END NUMPY PATHLIB ATTRIBUTION
########################################################################################
###############################################################################

# pickle
try:
Expand Down
6 changes: 4 additions & 2 deletions src/cli_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <xgboost/learner.h>
#include <xgboost/data.h>
#include <xgboost/logging.h>
#include <xgboost/parameter.h>

#include <dmlc/timer.h>
#include <iomanip>
#include <ctime>
Expand All @@ -30,7 +32,7 @@ enum CLITask {
kPredict = 2
};

struct CLIParam : public dmlc::Parameter<CLIParam> {
struct CLIParam : public XGBoostParameter<CLIParam> {
/*! \brief the task name */
int task;
/*! \brief whether evaluate training statistics */
Expand Down Expand Up @@ -123,7 +125,7 @@ struct CLIParam : public dmlc::Parameter<CLIParam> {
// customized configure function of CLIParam
inline void Configure(const std::vector<std::pair<std::string, std::string> >& _cfg) {
this->cfg = _cfg;
this->InitAllowUnknown(_cfg);
this->UpdateAllowUnknown(_cfg);
for (const auto& kv : _cfg) {
if (!strncmp("eval[", kv.first.c_str(), 5)) {
char evname[256];
Expand Down
4 changes: 2 additions & 2 deletions src/gbm/gblinear.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace gbm {
DMLC_REGISTRY_FILE_TAG(gblinear);

// training parameters
struct GBLinearTrainParam : public dmlc::Parameter<GBLinearTrainParam> {
struct GBLinearTrainParam : public XGBoostParameter<GBLinearTrainParam> {
std::string updater;
float tolerance;
size_t max_row_perbatch;
Expand Down Expand Up @@ -64,7 +64,7 @@ class GBLinear : public GradientBooster {
if (model_.weight.size() == 0) {
model_.param.InitAllowUnknown(cfg);
}
param_.InitAllowUnknown(cfg);
param_.UpdateAllowUnknown(cfg);
updater_.reset(LinearUpdater::Create(param_.updater, learner_param_));
updater_->Configure(cfg);
monitor_.Init("GBLinear");
Expand Down
4 changes: 2 additions & 2 deletions src/gbm/gbtree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ DMLC_REGISTRY_FILE_TAG(gbtree);

void GBTree::Configure(const Args& cfg) {
this->cfg_ = cfg;
tparam_.InitAllowUnknown(cfg);
tparam_.UpdateAllowUnknown(cfg);

model_.Configure(cfg);

Expand Down Expand Up @@ -295,7 +295,7 @@ class Dart : public GBTree {
void Configure(const Args& cfg) override {
GBTree::Configure(cfg);
if (model_.trees.size() == 0) {
dparam_.InitAllowUnknown(cfg);
dparam_.UpdateAllowUnknown(cfg);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/gbm/gbtree.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ namespace xgboost {
namespace gbm {

/*! \brief training parameters */
struct GBTreeTrainParam : public dmlc::Parameter<GBTreeTrainParam> {
struct GBTreeTrainParam : public XGBoostParameter<GBTreeTrainParam> {
/*!
* \brief number of parallel trees constructed each iteration
* use this option to support boosted random forest
Expand Down Expand Up @@ -95,7 +95,7 @@ struct GBTreeTrainParam : public dmlc::Parameter<GBTreeTrainParam> {
};

/*! \brief training parameters */
struct DartTrainParam : public dmlc::Parameter<DartTrainParam> {
struct DartTrainParam : public XGBoostParameter<DartTrainParam> {
/*! \brief type of sampling algorithm */
int sample_type;
/*! \brief type of normalization algorithm */
Expand Down
25 changes: 13 additions & 12 deletions src/learner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@
* \author Tianqi Chen
*/
#include <dmlc/io.h>
#include <dmlc/timer.h>
#include <dmlc/any.h>
#include <xgboost/feature_map.h>
#include <xgboost/learner.h>
#include <xgboost/base.h>
#include <xgboost/logging.h>
#include <xgboost/generic_parameters.h>
#include <dmlc/parameter.h>

#include <algorithm>
#include <iomanip>
#include <limits>
Expand All @@ -21,6 +16,12 @@
#include <utility>
#include <vector>

#include "xgboost/feature_map.h"
#include "xgboost/learner.h"
#include "xgboost/base.h"
#include "xgboost/parameter.h"
#include "xgboost/logging.h"
#include "xgboost/generic_parameters.h"
#include "xgboost/host_device_vector.h"
#include "common/common.h"
#include "common/io.h"
Expand Down Expand Up @@ -103,7 +104,7 @@ struct LearnerModelParam : public dmlc::Parameter<LearnerModelParam> {
}
};

struct LearnerTrainParam : public dmlc::Parameter<LearnerTrainParam> {
struct LearnerTrainParam : public XGBoostParameter<LearnerTrainParam> {
// data split mode, can be row, col, or none.
DataSplitMode dsplit;
// flag to disable default metric
Expand Down Expand Up @@ -155,9 +156,9 @@ class LearnerImpl : public Learner {
auto old_tparam = tparam_;
Args args = {cfg_.cbegin(), cfg_.cend()};

tparam_.InitAllowUnknown(args);
tparam_.UpdateAllowUnknown(args);

generic_param_.InitAllowUnknown(args);
generic_param_.UpdateAllowUnknown(args);
generic_param_.CheckDeprecated();

ConsoleLogger::Configure(args);
Expand Down Expand Up @@ -208,7 +209,7 @@ class LearnerImpl : public Learner {
}

void Load(dmlc::Stream* fi) override {
generic_param_.InitAllowUnknown(Args{});
generic_param_.UpdateAllowUnknown(Args{});
tparam_.Init(std::vector<std::pair<std::string, std::string>>{});
// TODO(tqchen) mark deprecation of old format.
common::PeekableInStream fp(fi);
Expand Down Expand Up @@ -314,7 +315,7 @@ class LearnerImpl : public Learner {
cfg_.insert(n.cbegin(), n.cend());

Args args = {cfg_.cbegin(), cfg_.cend()};
generic_param_.InitAllowUnknown(args);
generic_param_.UpdateAllowUnknown(args);
gbm_->Configure(args);
obj_->Configure({cfg_.begin(), cfg_.end()});

Expand Down
3 changes: 2 additions & 1 deletion src/linear/coordinate_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
#include <limits>

#include "xgboost/data.h"
#include "xgboost/parameter.h"
#include "./param.h"
#include "../gbm/gblinear_model.h"
#include "../common/random.h"

namespace xgboost {
namespace linear {

struct CoordinateParam : public dmlc::Parameter<CoordinateParam> {
struct CoordinateParam : public XGBoostParameter<CoordinateParam> {
int top_k;
DMLC_DECLARE_PARAMETER(CoordinateParam) {
DMLC_DECLARE_FIELD(top_k)
Expand Down
Loading