Require context in aggregators. (#10075)

This commit is contained in:
Jiaming Yuan
2024-02-28 03:12:42 +08:00
committed by GitHub
parent 761845f594
commit 5ac233280e
23 changed files with 190 additions and 144 deletions

View File

@@ -1,5 +1,5 @@
/**
* Copyright 2021-2023 by XGBoost Contributors
* Copyright 2021-2024, XGBoost Contributors
*/
#include "auc.h"
@@ -112,7 +112,9 @@ double MultiClassOVR(Context const *ctx, common::Span<float const> predts, MetaI
// we have 2 averages going in here, first is among workers, second is among
// classes. allreduce sums up fp/tp auc for each class.
collective::GlobalSum(info, &results.Values());
auto rc = collective::GlobalSum(ctx, info, results);
collective::SafeColl(rc);
double auc_sum{0};
double tp_sum{0};
for (size_t c = 0; c < n_classes; ++c) {
@@ -286,7 +288,7 @@ class EvalAUC : public MetricNoCache {
InvalidGroupAUC();
}
auc = collective::GlobalRatio(info, auc, static_cast<double>(valid_groups));
auc = collective::GlobalRatio(ctx_, info, auc, static_cast<double>(valid_groups));
if (!std::isnan(auc)) {
CHECK_LE(auc, 1) << "Total AUC across groups: " << auc * valid_groups
<< ", valid groups: " << valid_groups;
@@ -307,7 +309,7 @@ class EvalAUC : public MetricNoCache {
std::tie(fp, tp, auc) =
static_cast<Curve *>(this)->EvalBinary(preds, info);
}
auc = collective::GlobalRatio(info, auc, fp * tp);
auc = collective::GlobalRatio(ctx_, info, auc, fp * tp);
if (!std::isnan(auc)) {
CHECK_LE(auc, 1.0);
}

View File

@@ -1,5 +1,5 @@
/**
* Copyright 2015-2023 by XGBoost Contributors
* Copyright 2015-2024, XGBoost Contributors
* \file elementwise_metric.cu
* \brief evaluation metrics for elementwise binary or regression.
* \author Kailong Chen, Tianqi Chen
@@ -12,13 +12,14 @@
#include <cmath>
#include "../collective/communicator-inl.h"
#include "../common/common.h" // MetricNoCache
#include "../common/common.h" // MetricNoCache
#include "../common/math.h"
#include "../common/optional_weight.h" // OptionalWeights
#include "../common/pseudo_huber.h"
#include "../common/quantile_loss_utils.h" // QuantileLossParam
#include "../common/threading_utils.h"
#include "metric_common.h"
#include "xgboost/collective/result.h" // for SafeColl
#include "xgboost/metric.h"
#if defined(XGBOOST_USE_CUDA)
@@ -30,8 +31,7 @@
#include "../common/device_helpers.cuh"
#endif // XGBOOST_USE_CUDA
namespace xgboost {
namespace metric {
namespace xgboost::metric {
// tag the this file, used by force static link later.
DMLC_REGISTRY_FILE_TAG(elementwise_metric);
@@ -199,7 +199,8 @@ class PseudoErrorLoss : public MetricNoCache {
return std::make_tuple(v, wt);
});
std::array<double, 2> dat{result.Residue(), result.Weights()};
collective::GlobalSum(info, &dat);
auto rc = collective::GlobalSum(ctx_, info, linalg::MakeVec(dat.data(), dat.size()));
collective::SafeColl(rc);
return EvalRowMAPE::GetFinal(dat[0], dat[1]);
}
};
@@ -243,11 +244,11 @@ struct EvalError {
};
struct EvalPoissonNegLogLik {
const char *Name() const {
[[nodiscard]] const char *Name() const {
return "poisson-nloglik";
}
XGBOOST_DEVICE bst_float EvalRow(bst_float y, bst_float py) const {
[[nodiscard]] XGBOOST_DEVICE bst_float EvalRow(bst_float y, bst_float py) const {
const bst_float eps = 1e-16f;
if (py < eps) py = eps;
return common::LogGamma(y + 1.0f) + py - std::log(py) * y;
@@ -266,9 +267,9 @@ struct EvalPoissonNegLogLik {
* predt >= 0
*/
struct EvalGammaDeviance {
const char *Name() const { return "gamma-deviance"; }
[[nodiscard]] const char *Name() const { return "gamma-deviance"; }
XGBOOST_DEVICE bst_float EvalRow(bst_float label, bst_float predt) const {
[[nodiscard]] XGBOOST_DEVICE bst_float EvalRow(bst_float label, bst_float predt) const {
predt += kRtEps;
label += kRtEps;
return std::log(predt / label) + label / predt - 1;
@@ -287,7 +288,7 @@ struct EvalGammaNLogLik {
return "gamma-nloglik";
}
XGBOOST_DEVICE bst_float EvalRow(bst_float y, bst_float py) const {
[[nodiscard]] XGBOOST_DEVICE bst_float EvalRow(bst_float y, bst_float py) const {
py = std::max(py, 1e-6f);
// hardcoded dispersion.
float constexpr kPsi = 1.0;
@@ -313,7 +314,7 @@ struct EvalTweedieNLogLik {
CHECK(rho_ < 2 && rho_ >= 1)
<< "tweedie variance power must be in interval [1, 2)";
}
const char *Name() const {
[[nodiscard]] const char *Name() const {
static thread_local std::string name;
std::ostringstream os;
os << "tweedie-nloglik@" << rho_;
@@ -321,7 +322,7 @@ struct EvalTweedieNLogLik {
return name.c_str();
}
XGBOOST_DEVICE bst_float EvalRow(bst_float y, bst_float p) const {
[[nodiscard]] XGBOOST_DEVICE bst_float EvalRow(bst_float y, bst_float p) const {
bst_float a = y * std::exp((1 - rho_) * std::log(p)) / (1 - rho_);
bst_float b = std::exp((2 - rho_) * std::log(p)) / (2 - rho_);
return -a + b;
@@ -366,7 +367,8 @@ struct EvalEWiseBase : public MetricNoCache {
});
std::array<double, 2> dat{result.Residue(), result.Weights()};
collective::GlobalSum(info, &dat);
auto rc = collective::GlobalSum(ctx_, info, linalg::MakeVec(dat.data(), dat.size()));
collective::SafeColl(rc);
return Policy::GetFinal(dat[0], dat[1]);
}
@@ -438,7 +440,8 @@ class QuantileError : public MetricNoCache {
if (info.num_row_ == 0) {
// empty DMatrix on distributed env
std::array<double, 2> dat{0.0, 0.0};
collective::GlobalSum(info, &dat);
auto rc = collective::GlobalSum(ctx_, info, linalg::MakeVec(dat.data(), dat.size()));
collective::SafeColl(rc);
CHECK_GT(dat[1], 0);
return dat[0] / dat[1];
}
@@ -476,7 +479,8 @@ class QuantileError : public MetricNoCache {
return std::make_tuple(l, w);
});
std::array<double, 2> dat{result.Residue(), result.Weights()};
collective::GlobalSum(info, &dat);
auto rc = collective::GlobalSum(ctx, info, linalg::MakeVec(dat.data(), dat.size()));
collective::SafeColl(rc);
CHECK_GT(dat[1], 0);
return dat[0] / dat[1];
}
@@ -501,5 +505,4 @@ class QuantileError : public MetricNoCache {
XGBOOST_REGISTER_METRIC(QuantileError, "quantile")
.describe("Quantile regression error.")
.set_body([](const char*) { return new QuantileError{}; });
} // namespace metric
} // namespace xgboost
} // namespace xgboost::metric

View File

@@ -1,6 +1,5 @@
/*!
* Copyright 2018-2022 by Contributors
* \file metric_common.h
/**
* Copyright 2018-2024, Contributors
*/
#ifndef XGBOOST_METRIC_METRIC_COMMON_H_
#define XGBOOST_METRIC_METRIC_COMMON_H_
@@ -24,7 +23,7 @@ class MetricNoCache : public Metric {
double Evaluate(HostDeviceVector<float> const &predts, std::shared_ptr<DMatrix> p_fmat) final {
double result{0.0};
auto const &info = p_fmat->Info();
collective::ApplyWithLabels(info, &result, sizeof(double),
collective::ApplyWithLabels(ctx_, info, &result, sizeof(double),
[&] { result = this->Eval(predts, info); });
return result;
}

View File

@@ -1,5 +1,5 @@
/**
* Copyright 2015-2023 by XGBoost Contributors
* Copyright 2015-2024, XGBoost Contributors
* \file multiclass_metric.cc
* \brief evaluation metrics for multiclass classification.
* \author Kailong Chen, Tianqi Chen
@@ -24,8 +24,7 @@
#include "../common/device_helpers.cuh"
#endif // XGBOOST_USE_CUDA
namespace xgboost {
namespace metric {
namespace xgboost::metric {
// tag the this file, used by force static link later.
DMLC_REGISTRY_FILE_TAG(multiclass_metric);
@@ -40,11 +39,10 @@ class MultiClassMetricsReduction {
public:
MultiClassMetricsReduction() = default;
PackedReduceResult
CpuReduceMetrics(const HostDeviceVector<bst_float> &weights,
const HostDeviceVector<bst_float> &labels,
const HostDeviceVector<bst_float> &preds,
const size_t n_class, int32_t n_threads) const {
[[nodiscard]] PackedReduceResult CpuReduceMetrics(const HostDeviceVector<bst_float>& weights,
const HostDeviceVector<bst_float>& labels,
const HostDeviceVector<bst_float>& preds,
const size_t n_class, int32_t n_threads) const {
size_t ndata = labels.Size();
const auto& h_labels = labels.HostVector();
@@ -182,7 +180,8 @@ struct EvalMClassBase : public MetricNoCache {
dat[0] = result.Residue();
dat[1] = result.Weights();
}
collective::GlobalSum(info, &dat);
auto rc = collective::GlobalSum(ctx_, info, linalg::MakeVec(dat.data(), dat.size()));
collective::SafeColl(rc);
return Derived::GetFinal(dat[0], dat[1]);
}
/*!
@@ -245,5 +244,4 @@ XGBOOST_REGISTER_METRIC(MatchError, "merror")
XGBOOST_REGISTER_METRIC(MultiLogLoss, "mlogloss")
.describe("Multiclass negative loglikelihood.")
.set_body([](const char*) { return new EvalMultiLogLoss(); });
} // namespace metric
} // namespace xgboost
} // namespace xgboost::metric

View File

@@ -101,7 +101,7 @@ struct EvalAMS : public MetricNoCache {
}
}
const char* Name() const override {
[[nodiscard]] const char* Name() const override {
return name_.c_str();
}
@@ -159,7 +159,7 @@ struct EvalRank : public MetricNoCache, public EvalRankConfig {
exc.Rethrow();
}
return collective::GlobalRatio(info, sum_metric, static_cast<double>(ngroups));
return collective::GlobalRatio(ctx_, info, sum_metric, static_cast<double>(ngroups));
}
[[nodiscard]] const char* Name() const override {
@@ -274,7 +274,7 @@ class EvalRankWithCache : public Metric {
double Evaluate(HostDeviceVector<float> const& preds, std::shared_ptr<DMatrix> p_fmat) override {
double result{0.0};
auto const& info = p_fmat->Info();
collective::ApplyWithLabels(info, &result, sizeof(double), [&] {
collective::ApplyWithLabels(ctx_, info, &result, sizeof(double), [&] {
auto p_cache = cache_.CacheItem(p_fmat, ctx_, info, param_);
if (p_cache->Param() != param_) {
p_cache = cache_.ResetItem(p_fmat, ctx_, info, param_);
@@ -294,9 +294,10 @@ class EvalRankWithCache : public Metric {
};
namespace {
double Finalize(Context const*, MetaInfo const& info, double score, double sw) {
double Finalize(Context const* ctx, MetaInfo const& info, double score, double sw) {
std::array<double, 2> dat{score, sw};
collective::GlobalSum(info, &dat);
auto rc = collective::GlobalSum(ctx, info, linalg::MakeVec(dat.data(), 2));
collective::SafeColl(rc);
std::tie(score, sw) = std::tuple_cat(dat);
if (sw > 0.0) {
score = score / sw;

View File

@@ -1,5 +1,5 @@
/**
* Copyright 2020-2023 by XGBoost Contributors
* Copyright 2020-2024, XGBoost Contributors
*/
#include <dmlc/registry.h>
#include <thrust/iterator/counting_iterator.h> // for make_counting_iterator

View File

@@ -1,5 +1,5 @@
/**
* Copyright 2019-2023 by Contributors
* Copyright 2019-2024, Contributors
* \file survival_metric.cu
* \brief Metrics for survival analysis
* \author Avinash Barnwal, Hyunsu Cho and Toby Hocking
@@ -30,8 +30,7 @@ using ProbabilityDistributionType = xgboost::common::ProbabilityDistributionType
template <typename Distribution>
using AFTLoss = xgboost::common::AFTLoss<Distribution>;
namespace xgboost {
namespace metric {
namespace xgboost::metric {
// tag the this file, used by force static link later.
DMLC_REGISTRY_FILE_TAG(survival_metric);
@@ -43,12 +42,11 @@ class ElementWiseSurvivalMetricsReduction {
policy_ = policy;
}
PackedReduceResult
CpuReduceMetrics(const HostDeviceVector<bst_float> &weights,
const HostDeviceVector<bst_float> &labels_lower_bound,
const HostDeviceVector<bst_float> &labels_upper_bound,
const HostDeviceVector<bst_float> &preds,
int32_t n_threads) const {
[[nodiscard]] PackedReduceResult CpuReduceMetrics(
const HostDeviceVector<bst_float>& weights,
const HostDeviceVector<bst_float>& labels_lower_bound,
const HostDeviceVector<bst_float>& labels_upper_bound,
const HostDeviceVector<bst_float>& preds, int32_t n_threads) const {
size_t ndata = labels_lower_bound.Size();
CHECK_EQ(ndata, labels_upper_bound.Size());
@@ -155,7 +153,7 @@ class ElementWiseSurvivalMetricsReduction {
struct EvalIntervalRegressionAccuracy {
void Configure(const Args&) {}
const char* Name() const {
[[nodiscard]] const char* Name() const {
return "interval-regression-accuracy";
}
@@ -177,7 +175,7 @@ struct EvalAFTNLogLik {
param_.UpdateAllowUnknown(args);
}
const char* Name() const {
[[nodiscard]] const char* Name() const {
return "aft-nloglik";
}
@@ -213,7 +211,8 @@ struct EvalEWiseSurvivalBase : public MetricNoCache {
info.labels_upper_bound_, preds);
std::array<double, 2> dat{result.Residue(), result.Weights()};
collective::GlobalSum(info, &dat);
auto rc = collective::GlobalSum(ctx_, info, linalg::MakeVec(dat.data(), dat.size()));
collective::SafeColl(rc);
return Policy::GetFinal(dat[0], dat[1]);
}
@@ -230,7 +229,7 @@ struct EvalEWiseSurvivalBase : public MetricNoCache {
// This class exists because we want to perform dispatch according to the distribution type at
// configuration time, not at prediction time.
struct AFTNLogLikDispatcher : public MetricNoCache {
const char* Name() const override {
[[nodiscard]] const char* Name() const override {
return "aft-nloglik";
}
@@ -282,5 +281,4 @@ XGBOOST_REGISTER_METRIC(IntervalRegressionAccuracy, "interval-regression-accurac
return new EvalEWiseSurvivalBase<EvalIntervalRegressionAccuracy>();
});
} // namespace metric
} // namespace xgboost
} // namespace xgboost::metric