De-duplicate GPU parameters. (#4454)
* Only define `gpu_id` and `n_gpus` in `LearnerTrainParam` * Pass LearnerTrainParam through XGBoost vid factory method. * Disable all GPU usage when GPU related parameters are not specified (fixes XGBoost choosing GPU over aggressively). * Test learner train param io. * Fix gpu pickling.
This commit is contained in:
@@ -1,21 +1,14 @@
|
||||
/*!
|
||||
* Copyright 2018 XGBoost contributors
|
||||
* Copyright 2018-2019 XGBoost contributors
|
||||
*/
|
||||
#include <xgboost/metric.h>
|
||||
#include <map>
|
||||
#include "../helpers.h"
|
||||
|
||||
using Arg = std::pair<std::string, std::string>;
|
||||
|
||||
#if defined(__CUDACC__)
|
||||
#define N_GPU() Arg{"n_gpus", "1"}
|
||||
#else
|
||||
#define N_GPU() Arg{"n_gpus", "0"}
|
||||
#endif
|
||||
|
||||
TEST(Metric, DeclareUnifiedTest(RMSE)) {
|
||||
xgboost::Metric * metric = xgboost::Metric::Create("rmse");
|
||||
metric->Configure({N_GPU()});
|
||||
auto lparam = xgboost::CreateEmptyGenericParam(0, NGPUS);
|
||||
xgboost::Metric * metric = xgboost::Metric::Create("rmse", &lparam);
|
||||
metric->Configure({});
|
||||
ASSERT_STREQ(metric->Name(), "rmse");
|
||||
EXPECT_NEAR(GetMetricEval(metric, {0, 1}, {0, 1}), 0, 1e-10);
|
||||
EXPECT_NEAR(GetMetricEval(metric,
|
||||
@@ -27,8 +20,9 @@ TEST(Metric, DeclareUnifiedTest(RMSE)) {
|
||||
}
|
||||
|
||||
TEST(Metric, DeclareUnifiedTest(MAE)) {
|
||||
xgboost::Metric * metric = xgboost::Metric::Create("mae");
|
||||
metric->Configure({N_GPU()});
|
||||
auto lparam = xgboost::CreateEmptyGenericParam(0, NGPUS);
|
||||
xgboost::Metric * metric = xgboost::Metric::Create("mae", &lparam);
|
||||
metric->Configure({});
|
||||
ASSERT_STREQ(metric->Name(), "mae");
|
||||
EXPECT_NEAR(GetMetricEval(metric, {0, 1}, {0, 1}), 0, 1e-10);
|
||||
EXPECT_NEAR(GetMetricEval(metric,
|
||||
@@ -39,8 +33,9 @@ TEST(Metric, DeclareUnifiedTest(MAE)) {
|
||||
}
|
||||
|
||||
TEST(Metric, DeclareUnifiedTest(LogLoss)) {
|
||||
xgboost::Metric * metric = xgboost::Metric::Create("logloss");
|
||||
metric->Configure({N_GPU()});
|
||||
auto lparam = xgboost::CreateEmptyGenericParam(0, NGPUS);
|
||||
xgboost::Metric * metric = xgboost::Metric::Create("logloss", &lparam);
|
||||
metric->Configure({});
|
||||
ASSERT_STREQ(metric->Name(), "logloss");
|
||||
EXPECT_NEAR(GetMetricEval(metric, {0, 1}, {0, 1}), 0, 1e-10);
|
||||
EXPECT_NEAR(GetMetricEval(metric,
|
||||
@@ -51,8 +46,9 @@ TEST(Metric, DeclareUnifiedTest(LogLoss)) {
|
||||
}
|
||||
|
||||
TEST(Metric, DeclareUnifiedTest(Error)) {
|
||||
xgboost::Metric * metric = xgboost::Metric::Create("error");
|
||||
metric->Configure({N_GPU()});
|
||||
auto lparam = xgboost::CreateEmptyGenericParam(0, NGPUS);
|
||||
xgboost::Metric * metric = xgboost::Metric::Create("error", &lparam);
|
||||
metric->Configure({});
|
||||
ASSERT_STREQ(metric->Name(), "error");
|
||||
EXPECT_NEAR(GetMetricEval(metric, {0, 1}, {0, 1}), 0, 1e-10);
|
||||
EXPECT_NEAR(GetMetricEval(metric,
|
||||
@@ -60,17 +56,17 @@ TEST(Metric, DeclareUnifiedTest(Error)) {
|
||||
{ 0, 0, 1, 1}),
|
||||
0.5f, 0.001f);
|
||||
|
||||
EXPECT_ANY_THROW(xgboost::Metric::Create("error@abc"));
|
||||
EXPECT_ANY_THROW(xgboost::Metric::Create("error@abc", &lparam));
|
||||
delete metric;
|
||||
|
||||
metric = xgboost::Metric::Create("error@0.5f");
|
||||
metric->Configure({N_GPU()});
|
||||
metric = xgboost::Metric::Create("error@0.5f", &lparam);
|
||||
metric->Configure({});
|
||||
EXPECT_STREQ(metric->Name(), "error");
|
||||
|
||||
delete metric;
|
||||
|
||||
metric = xgboost::Metric::Create("error@0.1");
|
||||
metric->Configure({N_GPU()});
|
||||
metric = xgboost::Metric::Create("error@0.1", &lparam);
|
||||
metric->Configure({});
|
||||
ASSERT_STREQ(metric->Name(), "error@0.1");
|
||||
EXPECT_STREQ(metric->Name(), "error@0.1");
|
||||
EXPECT_NEAR(GetMetricEval(metric, {0, 1}, {0, 1}), 0, 1e-10);
|
||||
@@ -82,8 +78,9 @@ TEST(Metric, DeclareUnifiedTest(Error)) {
|
||||
}
|
||||
|
||||
TEST(Metric, DeclareUnifiedTest(PoissionNegLogLik)) {
|
||||
xgboost::Metric * metric = xgboost::Metric::Create("poisson-nloglik");
|
||||
metric->Configure({N_GPU()});
|
||||
auto lparam = xgboost::CreateEmptyGenericParam(0, NGPUS);
|
||||
xgboost::Metric * metric = xgboost::Metric::Create("poisson-nloglik", &lparam);
|
||||
metric->Configure({});
|
||||
ASSERT_STREQ(metric->Name(), "poisson-nloglik");
|
||||
EXPECT_NEAR(GetMetricEval(metric, {0, 1}, {0, 1}), 0.5f, 1e-10);
|
||||
EXPECT_NEAR(GetMetricEval(metric,
|
||||
@@ -96,8 +93,9 @@ TEST(Metric, DeclareUnifiedTest(PoissionNegLogLik)) {
|
||||
#if defined(XGBOOST_USE_NCCL) && defined(__CUDACC__)
|
||||
TEST(Metric, MGPU_RMSE) {
|
||||
{
|
||||
xgboost::Metric * metric = xgboost::Metric::Create("rmse");
|
||||
metric->Configure({Arg{"n_gpus", "-1"}});
|
||||
auto lparam = xgboost::CreateEmptyGenericParam(0, -1);
|
||||
xgboost::Metric * metric = xgboost::Metric::Create("rmse", &lparam);
|
||||
metric->Configure({});
|
||||
ASSERT_STREQ(metric->Name(), "rmse");
|
||||
EXPECT_NEAR(GetMetricEval(metric, {0}, {0}), 0, 1e-10);
|
||||
EXPECT_NEAR(GetMetricEval(metric,
|
||||
@@ -108,8 +106,8 @@ TEST(Metric, MGPU_RMSE) {
|
||||
}
|
||||
|
||||
{
|
||||
xgboost::Metric * metric = xgboost::Metric::Create("rmse");
|
||||
metric->Configure({Arg{"n_gpus", "-1"}, Arg{"gpu_id", "1"}});
|
||||
auto lparam = xgboost::CreateEmptyGenericParam(1, -1);
|
||||
xgboost::Metric * metric = xgboost::Metric::Create("rmse", &lparam);
|
||||
ASSERT_STREQ(metric->Name(), "rmse");
|
||||
EXPECT_NEAR(GetMetricEval(metric, {0, 1}, {0, 1}), 0, 1e-10);
|
||||
EXPECT_NEAR(GetMetricEval(metric,
|
||||
|
||||
Reference in New Issue
Block a user