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

* Use `UpdateAllowUnknown' for non-model related parameter.

Model parameter can not pack an additional boolean value due to binary IO
format.  This commit deals only with non-model related parameter configuration.

* Add tidy command line arg for use-dmlc-gtest.
This commit is contained in:
Jiaming Yuan
2019-10-23 05:50:12 -04:00
committed by GitHub
parent f24be2efb4
commit ac457c56a2
44 changed files with 189 additions and 112 deletions

View File

@@ -16,6 +16,8 @@ TEST(Monitor, Logging) {
Args args = {std::make_pair("verbosity", "3")};
ConsoleLogger::Configure(args);
ASSERT_EQ(ConsoleLogger::GlobalVerbosity(), ConsoleLogger::LogVerbosity::kDebug);
testing::internal::CaptureStderr();
run_monitor();
std::string output = testing::internal::GetCapturedStderr();
@@ -28,6 +30,8 @@ TEST(Monitor, Logging) {
run_monitor();
output = testing::internal::GetCapturedStderr();
ASSERT_EQ(output.size(), 0);
ConsoleLogger::Configure(Args{{"verbosity", "1"}});
}
} // namespace common
} // namespace xgboost

View File

@@ -1,5 +1,9 @@
#include <dmlc/parameter.h>
/*!
* Copyright (c) by Contributors 2019
*/
#include <gtest/gtest.h>
#include <xgboost/base.h>
#include <xgboost/parameter.h>
enum class Foo : int {
@@ -8,10 +12,10 @@ enum class Foo : int {
DECLARE_FIELD_ENUM_CLASS(Foo);
struct MyParam : dmlc::Parameter<MyParam> {
struct MyEnumParam : xgboost::XGBoostParameter<MyEnumParam> {
Foo foo;
int bar;
DMLC_DECLARE_PARAMETER(MyParam) {
DMLC_DECLARE_PARAMETER(MyEnumParam) {
DMLC_DECLARE_FIELD(foo)
.set_default(Foo::kBar)
.add_enum("bar", Foo::kBar)
@@ -23,10 +27,10 @@ struct MyParam : dmlc::Parameter<MyParam> {
}
};
DMLC_REGISTER_PARAMETER(MyParam);
DMLC_REGISTER_PARAMETER(MyEnumParam);
TEST(EnumClassParam, Basic) {
MyParam param;
MyEnumParam param;
std::map<std::string, std::string> kwargs{
{"foo", "frog"}, {"bar", "10"}
};
@@ -53,3 +57,44 @@ TEST(EnumClassParam, Basic) {
kwargs["foo"] = "human";
ASSERT_THROW(param.Init(kwargs), dmlc::ParamError);
}
struct UpdatableParam : xgboost::XGBoostParameter<UpdatableParam> {
float f { 0.0f };
double d { 0.0 };
DMLC_DECLARE_PARAMETER(UpdatableParam) {
DMLC_DECLARE_FIELD(f)
.set_default(11.0f);
DMLC_DECLARE_FIELD(d)
.set_default(2.71828f);
}
};
DMLC_REGISTER_PARAMETER(UpdatableParam);
TEST(XGBoostParameter, Update) {
{
UpdatableParam p;
auto constexpr kRtEps = xgboost::kRtEps;
p.UpdateAllowUnknown(xgboost::Args{});
// When it's not initialized, perform set_default.
ASSERT_NEAR(p.f, 11.0f, kRtEps);
ASSERT_NEAR(p.d, 2.71828f, kRtEps);
p.d = 3.14149;
p.UpdateAllowUnknown(xgboost::Args{{"f", "2.71828"}});
ASSERT_NEAR(p.f, 2.71828f, kRtEps);
// p.d is un-effected by the update.
ASSERT_NEAR(p.d, 3.14149, kRtEps);
}
{
UpdatableParam p;
auto constexpr kRtEps = xgboost::kRtEps;
p.UpdateAllowUnknown(xgboost::Args{{"f", "2.71828"}});
ASSERT_NEAR(p.f, 2.71828f, kRtEps);
ASSERT_NEAR(p.d, 2.71828, kRtEps); // default
}
}

View File

@@ -11,7 +11,7 @@ TEST(GBTree, SelectTreeMethod) {
size_t constexpr kCols = 10;
GenericParameter generic_param;
generic_param.InitAllowUnknown(Args{});
generic_param.UpdateAllowUnknown(Args{});
std::unique_ptr<GradientBooster> p_gbm{
GradientBooster::Create("gbtree", &generic_param, {}, 0)};
auto& gbtree = dynamic_cast<gbm::GBTree&> (*p_gbm);
@@ -36,7 +36,7 @@ TEST(GBTree, SelectTreeMethod) {
ASSERT_EQ(tparam.predictor, "cpu_predictor");
#ifdef XGBOOST_USE_CUDA
generic_param.InitAllowUnknown(Args{{"gpu_id", "0"}});
generic_param.UpdateAllowUnknown(Args{{"gpu_id", "0"}});
gbtree.Configure({{"tree_method", "gpu_hist"}, {"num_feature", n_feat}});
ASSERT_EQ(tparam.updater_seq, "grow_gpu_hist");
ASSERT_EQ(tparam.predictor, "gpu_predictor");
@@ -64,7 +64,7 @@ TEST(GBTree, ChoosePredictor) {
std::string n_feat = std::to_string(kCols);
Args args {{"tree_method", "approx"}, {"num_feature", n_feat}};
GenericParameter generic_param;
generic_param.InitAllowUnknown(Args{{"gpu_id", "0"}});
generic_param.UpdateAllowUnknown(Args{{"gpu_id", "0"}});
auto& data = (*(p_mat->GetBatches<SparsePage>().begin())).data;

View File

@@ -9,7 +9,7 @@ TEST(Objective, UnknownFunction) {
xgboost::ObjFunction* obj = nullptr;
xgboost::GenericParameter tparam;
std::vector<std::pair<std::string, std::string>> args;
tparam.InitAllowUnknown(args);
tparam.UpdateAllowUnknown(args);
EXPECT_ANY_THROW(obj = xgboost::ObjFunction::Create("unknown_name", &tparam));
EXPECT_NO_THROW(obj = xgboost::ObjFunction::Create("reg:squarederror", &tparam));

View File

@@ -38,7 +38,7 @@ TEST(Objective, DeclareUnifiedTest(PairwiseRankingGPair)) {
TEST(Objective, DeclareUnifiedTest(NDCG_Json_IO)) {
xgboost::GenericParameter tparam;
tparam.InitAllowUnknown(Args{});
tparam.UpdateAllowUnknown(Args{});
std::unique_ptr<xgboost::ObjFunction> obj {
xgboost::ObjFunction::Create("rank:ndcg", &tparam)

View File

@@ -53,6 +53,7 @@ TEST(Logging, Basic) {
output = testing::internal::GetCapturedStderr();
ASSERT_NE(output.find("Test Log Console"), std::string::npos);
args["silent"] = "False";
args["verbosity"] = "1"; // restore
ConsoleLogger::Configure({args.cbegin(), args.cend()});
}

View File

@@ -33,7 +33,7 @@ TEST(Updater, Prune) {
// prepare tree
RegTree tree = RegTree();
tree.param.InitAllowUnknown(cfg);
tree.param.UpdateAllowUnknown(cfg);
std::vector<RegTree*> trees {&tree};
// prepare pruner
std::unique_ptr<TreeUpdater> pruner(TreeUpdater::Create("prune", &lparam));

View File

@@ -262,7 +262,7 @@ class QuantileHistMock : public QuantileHistMaker {
gmat.Init((*dmat_).get(), kMaxBins);
RegTree tree = RegTree();
tree.param.InitAllowUnknown(cfg_);
tree.param.UpdateAllowUnknown(cfg_);
std::vector<GradientPair> gpair =
{ {0.23f, 0.24f}, {0.23f, 0.24f}, {0.23f, 0.24f}, {0.23f, 0.24f},
@@ -273,7 +273,7 @@ class QuantileHistMock : public QuantileHistMaker {
void TestBuildHist() {
RegTree tree = RegTree();
tree.param.InitAllowUnknown(cfg_);
tree.param.UpdateAllowUnknown(cfg_);
size_t constexpr kMaxBins = 4;
common::GHistIndexMatrix gmat;
@@ -284,7 +284,7 @@ class QuantileHistMock : public QuantileHistMaker {
void TestEvaluateSplit() {
RegTree tree = RegTree();
tree.param.InitAllowUnknown(cfg_);
tree.param.UpdateAllowUnknown(cfg_);
builder_->TestEvaluateSplit(gmatb_, tree);
}

View File

@@ -28,7 +28,7 @@ TEST(Updater, Refresh) {
RegTree tree = RegTree();
auto lparam = CreateEmptyGenericParam(GPUIDX);
tree.param.InitAllowUnknown(cfg);
tree.param.UpdateAllowUnknown(cfg);
std::vector<RegTree*> trees {&tree};
std::unique_ptr<TreeUpdater> refresher(TreeUpdater::Create("refresh", &lparam));