Implement slope for Pseduo-Huber. (#7727)

* Add objective and metric.
* Some refactoring for CPU/GPU dispatching using linalg module.
This commit is contained in:
Jiaming Yuan
2022-03-14 21:42:38 +08:00
committed by GitHub
parent 4dafb5fac8
commit 98d6faefd6
28 changed files with 456 additions and 290 deletions

View File

@@ -314,11 +314,11 @@ TEST(Linalg, Popc) {
TEST(Linalg, Stack) {
Tensor<float, 3> l{{2, 3, 4}, kCpuId};
ElementWiseKernelHost(l.View(kCpuId), omp_get_max_threads(),
[=](size_t i, float v) { return i; });
ElementWiseTransformHost(l.View(kCpuId), omp_get_max_threads(),
[=](size_t i, float v) { return i; });
Tensor<float, 3> r_0{{2, 3, 4}, kCpuId};
ElementWiseKernelHost(r_0.View(kCpuId), omp_get_max_threads(),
[=](size_t i, float v) { return i; });
ElementWiseTransformHost(r_0.View(kCpuId), omp_get_max_threads(),
[=](size_t i, float v) { return i; });
Stack(&l, r_0);

View File

@@ -1,5 +1,5 @@
/*!
* Copyright 2021 by XGBoost Contributors
* Copyright 2021-2022 by XGBoost Contributors
*/
#include <gtest/gtest.h>
@@ -19,7 +19,7 @@ void TestElementWiseKernel() {
// GPU view
auto t = l.View(0).Slice(linalg::All(), 1, linalg::All());
ASSERT_FALSE(t.CContiguous());
ElementWiseKernelDevice(t, [] __device__(size_t i, float) { return i; });
ElementWiseTransformDevice(t, [] __device__(size_t i, float) { return i; });
// CPU view
t = l.View(GenericParameter::kCpuId).Slice(linalg::All(), 1, linalg::All());
size_t k = 0;
@@ -30,10 +30,7 @@ void TestElementWiseKernel() {
}
t = l.View(0).Slice(linalg::All(), 1, linalg::All());
ElementWiseKernelDevice(t, [] __device__(size_t i, float v) {
SPAN_CHECK(v == i);
return v;
});
ElementWiseKernelDevice(t, [] XGBOOST_DEVICE(size_t i, float v) { SPAN_CHECK(v == i); });
}
{
@@ -41,7 +38,7 @@ void TestElementWiseKernel() {
* Contiguous
*/
auto t = l.View(0);
ElementWiseKernelDevice(t, [] __device__(size_t i, float) { return i; });
ElementWiseTransformDevice(t, [] XGBOOST_DEVICE(size_t i, float) { return i; });
ASSERT_TRUE(t.CContiguous());
// CPU view
t = l.View(GenericParameter::kCpuId);

View File

@@ -29,14 +29,13 @@ inline void TestMetaInfoStridedData(int32_t device) {
auto const& h_result = info.labels.View(-1);
ASSERT_EQ(h_result.Shape().size(), 2);
auto in_labels = labels.View(-1);
linalg::ElementWiseKernelHost(h_result, omp_get_max_threads(), [&](size_t i, float v_0) {
linalg::ElementWiseKernelHost(h_result, omp_get_max_threads(), [&](size_t i, float& v_0) {
auto tup = linalg::UnravelIndex(i, h_result.Shape());
auto i0 = std::get<0>(tup);
auto i1 = std::get<1>(tup);
// Sliced at second dimension.
auto v_1 = in_labels(i0, 0, i1);
CHECK_EQ(v_0, v_1);
return v_0;
});
}
{
@@ -71,7 +70,6 @@ inline void TestMetaInfoStridedData(int32_t device) {
// Sliced at second dimension.
auto v_1 = in_margin(i0, 0, i1);
CHECK_EQ(v_0, v_1);
return v_0;
});
}
}

View File

@@ -1,5 +1,5 @@
/*!
* Copyright 2016-2020 XGBoost contributors
* Copyright 2016-2022 by XGBoost contributors
*/
#include <dmlc/filesystem.h>
#include <xgboost/logging.h>
@@ -136,8 +136,8 @@ void CheckRankingObjFunction(std::unique_ptr<xgboost::ObjFunction> const& obj,
std::vector<xgboost::bst_float> out_hess) {
xgboost::MetaInfo info;
info.num_row_ = labels.size();
info.labels =
xgboost::linalg::Tensor<float, 2>{labels.cbegin(), labels.cend(), {labels.size()}, -1};
info.labels = xgboost::linalg::Tensor<float, 2>{
labels.cbegin(), labels.cend(), {labels.size(), static_cast<size_t>(1)}, -1};
info.weights_.HostVector() = weights;
info.group_ptr_ = groups;

View File

@@ -1,12 +1,13 @@
/*!
* Copyright 2018-2019 XGBoost contributors
* Copyright 2018-2022 by XGBoost contributors
*/
#include <xgboost/metric.h>
#include <xgboost/json.h>
#include <xgboost/metric.h>
#include <map>
#include <memory>
#include "../../../src/common/linalg_op.h"
#include "../helpers.h"
namespace xgboost {
@@ -16,14 +17,17 @@ inline void CheckDeterministicMetricElementWise(StringView name, int32_t device)
std::unique_ptr<Metric> metric{Metric::Create(name.c_str(), &lparam)};
HostDeviceVector<float> predts;
size_t n_samples = 2048;
MetaInfo info;
info.labels.Reshape(n_samples, 1);
info.num_row_ = n_samples;
auto &h_labels = info.labels.Data()->HostVector();
auto &h_predts = predts.HostVector();
SimpleLCG lcg;
SimpleRealUniformDistribution<float> dist{0.0f, 1.0f};
size_t n_samples = 2048;
h_labels.resize(n_samples);
h_predts.resize(n_samples);
@@ -145,27 +149,33 @@ TEST(Metric, DeclareUnifiedTest(MAPE)) {
TEST(Metric, DeclareUnifiedTest(MPHE)) {
auto lparam = xgboost::CreateEmptyGenericParam(GPUIDX);
xgboost::Metric * metric = xgboost::Metric::Create("mphe", &lparam);
std::unique_ptr<xgboost::Metric> metric{xgboost::Metric::Create("mphe", &lparam)};
metric->Configure({});
ASSERT_STREQ(metric->Name(), "mphe");
EXPECT_NEAR(GetMetricEval(metric, {0, 1}, {0, 1}), 0, 1e-10);
EXPECT_NEAR(GetMetricEval(metric,
EXPECT_NEAR(GetMetricEval(metric.get(), {0, 1}, {0, 1}), 0, 1e-10);
EXPECT_NEAR(GetMetricEval(metric.get(),
{0.1f, 0.9f, 0.1f, 0.9f},
{ 0, 0, 1, 1}),
0.1751f, 1e-4);
EXPECT_NEAR(GetMetricEval(metric,
EXPECT_NEAR(GetMetricEval(metric.get(),
{0.1f, 0.9f, 0.1f, 0.9f},
{ 0, 0, 1, 1},
{ -1, 1, 9, -9}),
3.4037f, 1e-4);
EXPECT_NEAR(GetMetricEval(metric,
EXPECT_NEAR(GetMetricEval(metric.get(),
{0.1f, 0.9f, 0.1f, 0.9f},
{ 0, 0, 1, 1},
{ 1, 2, 9, 8}),
0.1922f, 1e-4);
delete metric;
xgboost::CheckDeterministicMetricElementWise(xgboost::StringView{"mphe"}, GPUIDX);
metric->Configure({{"huber_slope", "0.1"}});
EXPECT_NEAR(GetMetricEval(metric.get(),
{0.1f, 0.9f, 0.1f, 0.9f},
{ 0, 0, 1, 1},
{ 1, 2, 9, 8}),
0.0461686f, 1e-4);
}
TEST(Metric, DeclareUnifiedTest(LogLoss)) {
@@ -277,7 +287,7 @@ TEST(Metric, DeclareUnifiedTest(PoissionNegLogLik)) {
1.5783f, 0.001f);
delete metric;
xgboost::CheckDeterministicMetricElementWise(xgboost::StringView{"mphe"}, GPUIDX);
xgboost::CheckDeterministicMetricElementWise(xgboost::StringView{"poisson-nloglik"}, GPUIDX);
}
TEST(Metric, DeclareUnifiedTest(MultiRMSE)) {
@@ -288,8 +298,8 @@ TEST(Metric, DeclareUnifiedTest(MultiRMSE)) {
HostDeviceVector<float> predt(n_samples * n_targets, 0);
auto lparam = xgboost::CreateEmptyGenericParam(GPUIDX);
std::unique_ptr<Metric> metric{Metric::Create("rmse", &lparam)};
auto ctx = xgboost::CreateEmptyGenericParam(GPUIDX);
std::unique_ptr<Metric> metric{Metric::Create("rmse", &ctx)};
metric->Configure({});
auto loss = GetMultiMetricEval(metric.get(), predt, y);

View File

@@ -57,25 +57,31 @@ TEST(Objective, DeclareUnifiedTest(SquaredLog)) {
TEST(Objective, DeclareUnifiedTest(PseudoHuber)) {
GenericParameter tparam = CreateEmptyGenericParam(GPUIDX);
std::vector<std::pair<std::string, std::string>> args;
Args args;
std::unique_ptr<ObjFunction> obj { ObjFunction::Create("reg:pseudohubererror", &tparam) };
std::unique_ptr<ObjFunction> obj{ObjFunction::Create("reg:pseudohubererror", &tparam)};
obj->Configure(args);
CheckConfigReload(obj, "reg:pseudohubererror");
CheckObjFunction(obj,
{0.1f, 0.2f, 0.4f, 0.8f, 1.6f}, // pred
{1.0f, 1.0f, 1.0f, 1.0f, 1.0f}, // labels
{1.0f, 1.0f, 1.0f, 1.0f, 1.0f}, // weights
{-0.668965f, -0.624695f, -0.514496f, -0.196116f, 0.514496f}, // out_grad
{ 0.410660f, 0.476140f, 0.630510f, 0.9428660f, 0.630510f}); // out_hess
CheckObjFunction(obj,
{0.1f, 0.2f, 0.4f, 0.8f, 1.6f}, // pred
{1.0f, 1.0f, 1.0f, 1.0f, 1.0f}, // labels
{}, // empty weights
{-0.668965f, -0.624695f, -0.514496f, -0.196116f, 0.514496f}, // out_grad
{ 0.410660f, 0.476140f, 0.630510f, 0.9428660f, 0.630510f}); // out_hess
CheckObjFunction(obj, {0.1f, 0.2f, 0.4f, 0.8f, 1.6f}, // pred
{1.0f, 1.0f, 1.0f, 1.0f, 1.0f}, // labels
{1.0f, 1.0f, 1.0f, 1.0f, 1.0f}, // weights
{-0.668965f, -0.624695f, -0.514496f, -0.196116f, 0.514496f}, // out_grad
{0.410660f, 0.476140f, 0.630510f, 0.9428660f, 0.630510f}); // out_hess
CheckObjFunction(obj, {0.1f, 0.2f, 0.4f, 0.8f, 1.6f}, // pred
{1.0f, 1.0f, 1.0f, 1.0f, 1.0f}, // labels
{}, // empty weights
{-0.668965f, -0.624695f, -0.514496f, -0.196116f, 0.514496f}, // out_grad
{0.410660f, 0.476140f, 0.630510f, 0.9428660f, 0.630510f}); // out_hess
ASSERT_EQ(obj->DefaultEvalMetric(), std::string{"mphe"});
obj->Configure({{"huber_slope", "0.1"}});
CheckConfigReload(obj, "reg:pseudohubererror");
CheckObjFunction(obj, {0.1f, 0.2f, 0.4f, 0.8f, 1.6f}, // pred
{1.0f, 1.0f, 1.0f, 1.0f, 1.0f}, // labels
{1.0f, 1.0f, 1.0f, 1.0f, 1.0f}, // weights
{-0.099388f, -0.099228f, -0.098639f, -0.089443f, 0.098639f}, // out_grad
{0.0013467f, 0.001908f, 0.004443f, 0.089443f, 0.004443f}); // out_hess
}
TEST(Objective, DeclareUnifiedTest(LogisticRegressionGPair)) {
@@ -131,7 +137,6 @@ TEST(Objective, DeclareUnifiedTest(LogisticRawGPair)) {
std::unique_ptr<ObjFunction> obj {
ObjFunction::Create("binary:logitraw", &lparam)
};
obj->Configure(args);
CheckObjFunction(obj,
@@ -373,5 +378,4 @@ TEST(Objective, CoxRegressionGPair) {
{ 0, 0, 0, 0.160f, 0.186f, 0.348f, 0.610f, 0.639f});
}
#endif
} // namespace xgboost

View File

@@ -430,8 +430,8 @@ TEST(Learner, MultiTarget) {
size_t constexpr kRows{128}, kCols{10}, kTargets{3};
auto m = RandomDataGenerator{kRows, kCols, 0}.GenerateDMatrix();
m->Info().labels.Reshape(kRows, kTargets);
linalg::ElementWiseKernelHost(m->Info().labels.HostView(), omp_get_max_threads(),
[](auto i, auto) { return i; });
linalg::ElementWiseTransformHost(m->Info().labels.HostView(), omp_get_max_threads(),
[](auto i, auto) { return i; });
{
std::unique_ptr<Learner> learner{Learner::Create({m})};