#ifndef XGBOOST_OBJECTIVE_LAMBDARANK_OBJ_H_
#define XGBOOST_OBJECTIVE_LAMBDARANK_OBJ_H_
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstddef>
#include <functional>
#include <memory>
#include <random>
#include <vector>
#include "../common/algorithm.h"
#include "../common/math.h"
#include "../common/ranking_utils.h"
#include "../common/transform_iterator.h"
#include "xgboost/base.h"
#include "xgboost/context.h"
#include "xgboost/data.h"
#include "xgboost/host_device_vector.h"
#include "xgboost/linalg.h"
#include "xgboost/logging.h"
#include "xgboost/span.h"
namespace xgboost::obj {
double constexpr Eps64() { return 1e-16; }
template <bool exp>
XGBOOST_DEVICE double DeltaNDCG(float y_high, float y_low, std::size_t rank_high,
std::size_t rank_low, double inv_IDCG,
common::Span<double const> discount) {
double gain_high = exp ? ltr::CalcDCGGain(y_high) : y_high;
double discount_high = discount[rank_high];
double gain_low = exp ? ltr::CalcDCGGain(y_low) : y_low;
double discount_low = discount[rank_low];
double original = gain_high * discount_high + gain_low * discount_low;
double changed = gain_low * discount_high + gain_high * discount_low;
double delta_NDCG = (original - changed) * inv_IDCG;
assert(delta_NDCG >= -1.0);
assert(delta_NDCG <= 1.0);
return delta_NDCG;
}
XGBOOST_DEVICE inline double DeltaMAP(float y_high, float y_low, std::size_t rank_high,
std::size_t rank_low, common::Span<double const> n_rel,
common::Span<double const> acc) {
double r_h = static_cast<double>(rank_high) + 1.0;
double r_l = static_cast<double>(rank_low) + 1.0;
double delta{0.0};
double n_total_relevances = n_rel.back();
assert(n_total_relevances > 0.0);
auto m = n_rel[rank_low];
double n = n_rel[rank_high];
if (y_high < y_low) {
auto a = m / r_l - (n + 1.0) / r_h;
auto b = acc[rank_low - 1] - acc[rank_high];
delta = (a - b) / n_total_relevances;
} else {
auto a = n / r_h - m / r_l;
auto b = acc[rank_low - 1] - acc[rank_high];
delta = (a + b) / n_total_relevances;
}
return delta;
}
template <bool unbiased, bool norm_by_diff, typename Delta>
XGBOOST_DEVICE GradientPair
LambdaGrad(linalg::VectorView<float const> labels, common::Span<float const> predts,
common::Span<size_t const> sorted_idx,
std::size_t rank_high, std::size_t rank_low, Delta delta, linalg::VectorView<double const> t_plus, linalg::VectorView<double const> t_minus, double* p_cost) {
assert(sorted_idx.size() > 0 && "Empty sorted idx for a group.");
std::size_t idx_high = sorted_idx[rank_high];
std::size_t idx_low = sorted_idx[rank_low];
if (labels(idx_high) == labels(idx_low)) {
*p_cost = 0;
return {0.0f, 0.0f};
}
auto best_score = predts[sorted_idx.front()];
auto worst_score = predts[sorted_idx.back()];
auto y_high = labels(idx_high);
float s_high = predts[idx_high];
auto y_low = labels(idx_low);
float s_low = predts[idx_low];
double delta_score = std::abs(s_high - s_low);
double const sigmoid = common::Sigmoid(s_high - s_low);
double delta_metric = std::abs(delta(y_high, y_low, rank_high, rank_low));
if (norm_by_diff && best_score != worst_score) {
delta_metric /= (delta_score + 0.01);
}
if (unbiased) {
*p_cost = std::log(1.0 / (1.0 - sigmoid)) * delta_metric;
}
auto lambda_ij = (sigmoid - 1.0) * delta_metric;
auto hessian_ij = std::max(sigmoid * (1.0 - sigmoid), Eps64()) * delta_metric * 2.0;
auto k = t_plus.Size();
assert(t_minus.Size() == k && "Invalid size of position bias");
if (unbiased && idx_high < k && idx_low < k && t_minus(idx_low) >= Eps64() &&
t_plus(idx_high) >= Eps64()) {
lambda_ij /= (t_plus(idx_high) * t_minus(idx_low));
hessian_ij /= (t_plus(idx_high) * t_minus(idx_low));
}
auto pg = GradientPair{static_cast<float>(lambda_ij), static_cast<float>(hessian_ij)};
return pg;
}
XGBOOST_DEVICE inline GradientPair Repulse(GradientPair pg) {
auto ng = GradientPair{-pg.GetGrad(), pg.GetHess()};
return ng;
}
namespace cuda_impl {
void LambdaRankGetGradientNDCG(Context const* ctx, std::int32_t iter,
HostDeviceVector<float> const& preds, MetaInfo const& info,
std::shared_ptr<ltr::NDCGCache> p_cache,
linalg::VectorView<double const> t_plus, linalg::VectorView<double const> t_minus, linalg::VectorView<double> li, linalg::VectorView<double> lj,
linalg::Matrix<GradientPair>* out_gpair);
void MAPStat(Context const* ctx, MetaInfo const& info, common::Span<std::size_t const> d_rank_idx,
std::shared_ptr<ltr::MAPCache> p_cache);
void LambdaRankGetGradientMAP(Context const* ctx, std::int32_t iter,
HostDeviceVector<float> const& predt, MetaInfo const& info,
std::shared_ptr<ltr::MAPCache> p_cache,
linalg::VectorView<double const> t_plus, linalg::VectorView<double const> t_minus, linalg::VectorView<double> li, linalg::VectorView<double> lj,
linalg::Matrix<GradientPair>* out_gpair);
void LambdaRankGetGradientPairwise(Context const* ctx, std::int32_t iter,
HostDeviceVector<float> const& predt, const MetaInfo& info,
std::shared_ptr<ltr::RankingCache> p_cache,
linalg::VectorView<double const> ti_plus, linalg::VectorView<double const> tj_minus, linalg::VectorView<double> li, linalg::VectorView<double> lj,
linalg::Matrix<GradientPair>* out_gpair);
void LambdaRankUpdatePositionBias(Context const* ctx, linalg::VectorView<double const> li_full,
linalg::VectorView<double const> lj_full,
linalg::Vector<double>* p_ti_plus,
linalg::Vector<double>* p_tj_minus, linalg::Vector<double>* p_li,
linalg::Vector<double>* p_lj,
std::shared_ptr<ltr::RankingCache> p_cache);
}
namespace cpu_impl {
void MAPStat(Context const* ctx, linalg::VectorView<float const> label,
common::Span<std::size_t const> rank_idx, std::shared_ptr<ltr::MAPCache> p_cache);
}
template <typename Op>
void MakePairs(Context const* ctx, std::int32_t iter,
std::shared_ptr<ltr::RankingCache> const cache, bst_group_t g,
linalg::VectorView<float const> g_label, common::Span<std::size_t const> g_rank,
Op op) {
auto group_ptr = cache->DataGroupPtr(ctx);
ltr::position_t cnt = group_ptr[g + 1] - group_ptr[g];
if (cache->Param().HasTruncation()) {
for (std::size_t i = 0, n = std::min(cnt, cache->Param().NumPair()); i < n; ++i) {
for (std::size_t j = i + 1; j < cnt; ++j) {
op(i, j);
}
}
} else {
CHECK_EQ(g_rank.size(), g_label.Size());
std::uint32_t seed = iter * (static_cast<std::uint32_t>(group_ptr.size()) - 1) + g;
std::minstd_rand rnd(seed);
auto it = common::MakeIndexTransformIter(
[&g_rank, &g_label](std::size_t idx) { return g_label(g_rank[idx]); });
std::vector<std::size_t> y_sorted_idx =
common::ArgSort<std::size_t>(ctx, it, it + cnt, std::greater<>{});
auto rev_it = common::MakeIndexTransformIter(
[&](std::size_t idx) { return g_label(g_rank[y_sorted_idx[idx]]); });
for (std::size_t i = 0; i < cnt;) {
std::size_t j = i + 1;
while (j < cnt && rev_it[i] == rev_it[j]) {
++j;
}
std::size_t n_lefts = i, n_rights = static_cast<std::size_t>(cnt - j);
if (n_lefts + n_rights == 0) {
i = j;
continue;
}
auto n_samples = cache->Param().NumPair();
while (n_samples--) {
for (std::size_t pair_idx = i; pair_idx < j; ++pair_idx) {
std::size_t ridx = std::uniform_int_distribution<std::size_t>(
static_cast<std::size_t>(0), n_lefts + n_rights - 1)(rnd);
if (ridx >= n_lefts) {
ridx = ridx - i + j; }
auto idx0 = y_sorted_idx[pair_idx];
auto idx1 = y_sorted_idx[ridx];
op(idx0, idx1);
}
}
i = j;
}
}
}
} #endif