Deterministic result for element-wise/mclass metrics. (#7303)
Remove openmp reduction.
This commit is contained in:
@@ -2,9 +2,44 @@
|
||||
* Copyright 2018-2019 XGBoost contributors
|
||||
*/
|
||||
#include <xgboost/metric.h>
|
||||
#include <xgboost/json.h>
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
|
||||
#include "../helpers.h"
|
||||
|
||||
namespace xgboost {
|
||||
namespace {
|
||||
inline void CheckDeterministicMetricElementWise(StringView name, int32_t device) {
|
||||
auto lparam = CreateEmptyGenericParam(device);
|
||||
std::unique_ptr<Metric> metric{Metric::Create(name.c_str(), &lparam)};
|
||||
|
||||
HostDeviceVector<float> predts;
|
||||
MetaInfo info;
|
||||
auto &h_labels = info.labels_.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);
|
||||
|
||||
for (size_t i = 0; i < n_samples; ++i) {
|
||||
h_predts[i] = dist(&lcg);
|
||||
h_labels[i] = dist(&lcg);
|
||||
}
|
||||
|
||||
auto result = metric->Eval(predts, info, false);
|
||||
for (size_t i = 0; i < 8; ++i) {
|
||||
ASSERT_EQ(metric->Eval(predts, info, false), result);
|
||||
}
|
||||
}
|
||||
} // anonymous namespace
|
||||
} // namespace xgboost
|
||||
|
||||
TEST(Metric, DeclareUnifiedTest(RMSE)) {
|
||||
auto lparam = xgboost::CreateEmptyGenericParam(GPUIDX);
|
||||
xgboost::Metric * metric = xgboost::Metric::Create("rmse", &lparam);
|
||||
@@ -26,6 +61,8 @@ TEST(Metric, DeclareUnifiedTest(RMSE)) {
|
||||
{ 1, 2, 9, 8}),
|
||||
0.6708f, 0.001f);
|
||||
delete metric;
|
||||
|
||||
xgboost::CheckDeterministicMetricElementWise(xgboost::StringView{"rmse"}, GPUIDX);
|
||||
}
|
||||
|
||||
TEST(Metric, DeclareUnifiedTest(RMSLE)) {
|
||||
@@ -49,6 +86,8 @@ TEST(Metric, DeclareUnifiedTest(RMSLE)) {
|
||||
{ 0, 1, 2, 9, 8}),
|
||||
0.2415f, 1e-4);
|
||||
delete metric;
|
||||
|
||||
xgboost::CheckDeterministicMetricElementWise(xgboost::StringView{"rmsle"}, GPUIDX);
|
||||
}
|
||||
|
||||
TEST(Metric, DeclareUnifiedTest(MAE)) {
|
||||
@@ -72,6 +111,8 @@ TEST(Metric, DeclareUnifiedTest(MAE)) {
|
||||
{ 1, 2, 9, 8}),
|
||||
0.54f, 0.001f);
|
||||
delete metric;
|
||||
|
||||
xgboost::CheckDeterministicMetricElementWise(xgboost::StringView{"mae"}, GPUIDX);
|
||||
}
|
||||
|
||||
TEST(Metric, DeclareUnifiedTest(MAPE)) {
|
||||
@@ -95,6 +136,8 @@ TEST(Metric, DeclareUnifiedTest(MAPE)) {
|
||||
{ 1, 2, 9, 8}),
|
||||
1.3250f, 0.001f);
|
||||
delete metric;
|
||||
|
||||
xgboost::CheckDeterministicMetricElementWise(xgboost::StringView{"mape"}, GPUIDX);
|
||||
}
|
||||
|
||||
TEST(Metric, DeclareUnifiedTest(MPHE)) {
|
||||
@@ -118,6 +161,8 @@ TEST(Metric, DeclareUnifiedTest(MPHE)) {
|
||||
{ 1, 2, 9, 8}),
|
||||
0.1922f, 1e-4);
|
||||
delete metric;
|
||||
|
||||
xgboost::CheckDeterministicMetricElementWise(xgboost::StringView{"mphe"}, GPUIDX);
|
||||
}
|
||||
|
||||
TEST(Metric, DeclareUnifiedTest(LogLoss)) {
|
||||
@@ -145,6 +190,8 @@ TEST(Metric, DeclareUnifiedTest(LogLoss)) {
|
||||
{ 1, 2, 9, 8}),
|
||||
1.3138f, 0.001f);
|
||||
delete metric;
|
||||
|
||||
xgboost::CheckDeterministicMetricElementWise(xgboost::StringView{"logloss"}, GPUIDX);
|
||||
}
|
||||
|
||||
TEST(Metric, DeclareUnifiedTest(Error)) {
|
||||
@@ -197,6 +244,8 @@ TEST(Metric, DeclareUnifiedTest(Error)) {
|
||||
{ 1, 2, 9, 8}),
|
||||
0.45f, 0.001f);
|
||||
delete metric;
|
||||
|
||||
xgboost::CheckDeterministicMetricElementWise(xgboost::StringView{"error@0.5"}, GPUIDX);
|
||||
}
|
||||
|
||||
TEST(Metric, DeclareUnifiedTest(PoissionNegLogLik)) {
|
||||
@@ -224,4 +273,6 @@ TEST(Metric, DeclareUnifiedTest(PoissionNegLogLik)) {
|
||||
{ 1, 2, 9, 8}),
|
||||
1.5783f, 0.001f);
|
||||
delete metric;
|
||||
|
||||
xgboost::CheckDeterministicMetricElementWise(xgboost::StringView{"mphe"}, GPUIDX);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,43 @@
|
||||
|
||||
#include "../helpers.h"
|
||||
|
||||
namespace xgboost {
|
||||
inline void CheckDeterministicMetricMultiClass(StringView name, int32_t device) {
|
||||
auto lparam = CreateEmptyGenericParam(device);
|
||||
std::unique_ptr<Metric> metric{Metric::Create(name.c_str(), &lparam)};
|
||||
|
||||
HostDeviceVector<float> predts;
|
||||
MetaInfo info;
|
||||
auto &h_labels = info.labels_.HostVector();
|
||||
auto &h_predts = predts.HostVector();
|
||||
|
||||
SimpleLCG lcg;
|
||||
|
||||
size_t n_samples = 2048, n_classes = 4;
|
||||
h_labels.resize(n_samples);
|
||||
h_predts.resize(n_samples * n_classes);
|
||||
|
||||
{
|
||||
SimpleRealUniformDistribution<float> dist{0.0f, static_cast<float>(n_classes)};
|
||||
for (size_t i = 0; i < n_samples; ++i) {
|
||||
h_labels[i] = dist(&lcg);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
SimpleRealUniformDistribution<float> dist{0.0f, 1.0f};
|
||||
for (size_t i = 0; i < n_samples * n_classes; ++i) {
|
||||
h_predts[i] = dist(&lcg);
|
||||
}
|
||||
}
|
||||
|
||||
auto result = metric->Eval(predts, info, false);
|
||||
for (size_t i = 0; i < 8; ++i) {
|
||||
ASSERT_EQ(metric->Eval(predts, info, false), result);
|
||||
}
|
||||
}
|
||||
} // namespace xgboost
|
||||
|
||||
inline void TestMultiClassError(int device) {
|
||||
auto lparam = xgboost::CreateEmptyGenericParam(device);
|
||||
lparam.gpu_id = device;
|
||||
@@ -17,12 +54,12 @@ inline void TestMultiClassError(int device) {
|
||||
{0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f},
|
||||
{0, 1, 2}),
|
||||
0.666f, 0.001f);
|
||||
|
||||
delete metric;
|
||||
}
|
||||
|
||||
TEST(Metric, DeclareUnifiedTest(MultiClassError)) {
|
||||
TestMultiClassError(GPUIDX);
|
||||
xgboost::CheckDeterministicMetricMultiClass(xgboost::StringView{"merror"}, GPUIDX);
|
||||
}
|
||||
|
||||
inline void TestMultiClassLogLoss(int device) {
|
||||
@@ -44,6 +81,7 @@ inline void TestMultiClassLogLoss(int device) {
|
||||
|
||||
TEST(Metric, DeclareUnifiedTest(MultiClassLogLoss)) {
|
||||
TestMultiClassLogLoss(GPUIDX);
|
||||
xgboost::CheckDeterministicMetricMultiClass(xgboost::StringView{"mlogloss"}, GPUIDX);
|
||||
}
|
||||
|
||||
#if defined(XGBOOST_USE_NCCL) && defined(__CUDACC__)
|
||||
|
||||
Reference in New Issue
Block a user