add namespace aliases to reduce code
This commit is contained in:
@@ -25,6 +25,12 @@
|
||||
#include "xgboost/data.h"
|
||||
#include "xgboost/span.h"
|
||||
|
||||
#if defined(XGBOOST_USE_HIP)
|
||||
namespace thrust {
|
||||
namespace cuda = thrust::hip;
|
||||
}
|
||||
#endif
|
||||
|
||||
namespace xgboost {
|
||||
namespace metric {
|
||||
// tag the this file, used by force static link later.
|
||||
@@ -127,17 +133,10 @@ GPUBinaryAUC(common::Span<float const> predts, MetaInfo const &info,
|
||||
thrust::make_counting_iterator(0),
|
||||
[=] XGBOOST_DEVICE(size_t i) { return predts[d_sorted_idx[i]]; });
|
||||
|
||||
#if defined(XGBOOST_USE_CUDA)
|
||||
auto end_unique = thrust::unique_by_key_copy(
|
||||
thrust::cuda::par(alloc), uni_key, uni_key + d_sorted_idx.size(),
|
||||
dh::tbegin(d_unique_idx), thrust::make_discard_iterator(),
|
||||
dh::tbegin(d_unique_idx));
|
||||
#elif defined(XGBOOST_USE_HIP)
|
||||
auto end_unique = thrust::unique_by_key_copy(
|
||||
thrust::hip::par(alloc), uni_key, uni_key + d_sorted_idx.size(),
|
||||
dh::tbegin(d_unique_idx), thrust::make_discard_iterator(),
|
||||
dh::tbegin(d_unique_idx));
|
||||
#endif
|
||||
|
||||
d_unique_idx = d_unique_idx.subspan(0, end_unique.second - dh::tbegin(d_unique_idx));
|
||||
|
||||
@@ -179,11 +178,7 @@ GPUBinaryAUC(common::Span<float const> predts, MetaInfo const &info,
|
||||
|
||||
Pair last = cache->fptp.back();
|
||||
|
||||
#if defined(XGBOOST_USE_CUDA)
|
||||
double auc = thrust::reduce(thrust::cuda::par(alloc), in, in + d_unique_idx.size());
|
||||
#elif defined(XGBOOST_USE_HIP)
|
||||
double auc = thrust::reduce(thrust::hip::par(alloc), in, in + d_unique_idx.size());
|
||||
#endif
|
||||
|
||||
return std::make_tuple(last.first, last.second, auc);
|
||||
}
|
||||
@@ -239,15 +234,9 @@ double ScaleClasses(common::Span<double> results, common::Span<double> local_are
|
||||
double tp_sum;
|
||||
double auc_sum;
|
||||
|
||||
#if defined(XGBOOST_USE_CUDA)
|
||||
thrust::tie(auc_sum, tp_sum) =
|
||||
thrust::reduce(thrust::cuda::par(alloc), reduce_in, reduce_in + n_classes,
|
||||
Pair{0.0, 0.0}, PairPlus<double, double>{});
|
||||
#elif defined(XGBOOST_USE_HIP)
|
||||
thrust::tie(auc_sum, tp_sum) =
|
||||
thrust::reduce(thrust::hip::par(alloc), reduce_in, reduce_in + n_classes,
|
||||
Pair{0.0, 0.0}, PairPlus<double, double>{});
|
||||
#endif
|
||||
|
||||
if (tp_sum != 0 && !std::isnan(auc_sum)) {
|
||||
auc_sum /= tp_sum;
|
||||
@@ -329,15 +318,9 @@ void SegmentedReduceAUC(common::Span<size_t const> d_unique_idx,
|
||||
return auc;
|
||||
});
|
||||
|
||||
#if defined(XGBOOST_USE_CUDA)
|
||||
thrust::reduce_by_key(thrust::cuda::par(alloc), key_in,
|
||||
key_in + d_unique_idx.size(), val_in,
|
||||
thrust::make_discard_iterator(), dh::tbegin(d_auc));
|
||||
#elif defined(XGBOOST_USE_HIP)
|
||||
thrust::reduce_by_key(thrust::hip::par(alloc), key_in,
|
||||
key_in + d_unique_idx.size(), val_in,
|
||||
thrust::make_discard_iterator(), dh::tbegin(d_auc));
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -410,7 +393,6 @@ double GPUMultiClassAUCOVR(MetaInfo const &info, DeviceOrd device,
|
||||
dh::TemporaryArray<uint32_t> unique_class_ptr(d_class_ptr.size());
|
||||
auto d_unique_class_ptr = dh::ToSpan(unique_class_ptr);
|
||||
|
||||
#if defined(XGBOOST_USE_CUDA)
|
||||
auto n_uniques = dh::SegmentedUniqueByKey(
|
||||
thrust::cuda::par(alloc),
|
||||
dh::tbegin(d_class_ptr),
|
||||
@@ -421,18 +403,6 @@ double GPUMultiClassAUCOVR(MetaInfo const &info, DeviceOrd device,
|
||||
d_unique_class_ptr.data(),
|
||||
dh::tbegin(d_unique_idx),
|
||||
thrust::equal_to<thrust::pair<uint32_t, float>>{});
|
||||
#elif defined(XGBOOST_USE_HIP)
|
||||
auto n_uniques = dh::SegmentedUniqueByKey(
|
||||
thrust::hip::par(alloc),
|
||||
dh::tbegin(d_class_ptr),
|
||||
dh::tend(d_class_ptr),
|
||||
uni_key,
|
||||
uni_key + d_sorted_idx.size(),
|
||||
dh::tbegin(d_unique_idx),
|
||||
d_unique_class_ptr.data(),
|
||||
dh::tbegin(d_unique_idx),
|
||||
thrust::equal_to<thrust::pair<uint32_t, float>>{});
|
||||
#endif
|
||||
|
||||
d_unique_idx = d_unique_idx.subspan(0, n_uniques);
|
||||
|
||||
@@ -553,15 +523,9 @@ std::pair<double, std::uint32_t> GPURankingAUC(Context const *ctx, common::Span<
|
||||
thrust::make_counting_iterator(0),
|
||||
[=] XGBOOST_DEVICE(size_t i) { return d_group_ptr[i + 1] - d_group_ptr[i]; });
|
||||
|
||||
#if defined(XGBOOST_USE_CUDA)
|
||||
size_t n_valid = thrust::count_if(
|
||||
thrust::cuda::par(alloc), check_it, check_it + group_ptr.size() - 1,
|
||||
[=] XGBOOST_DEVICE(size_t len) { return len >= 3; });
|
||||
#elif defined(XGBOOST_USE_HIP)
|
||||
size_t n_valid = thrust::count_if(
|
||||
thrust::hip::par(alloc), check_it, check_it + group_ptr.size() - 1,
|
||||
[=] XGBOOST_DEVICE(size_t len) { return len >= 3; });
|
||||
#endif
|
||||
|
||||
if (n_valid < info.group_ptr_.size() - 1) {
|
||||
InvalidGroupAUC();
|
||||
@@ -659,13 +623,8 @@ std::pair<double, std::uint32_t> GPURankingAUC(Context const *ctx, common::Span<
|
||||
/**
|
||||
* Scale the AUC with number of items in each group.
|
||||
*/
|
||||
#if defined(XGBOOST_USE_CUDA)
|
||||
double auc = thrust::reduce(thrust::cuda::par(alloc), dh::tbegin(s_d_auc),
|
||||
dh::tend(s_d_auc), 0.0);
|
||||
#elif defined(XGBOOST_USE_HIP)
|
||||
double auc = thrust::reduce(thrust::hip::par(alloc), dh::tbegin(s_d_auc),
|
||||
dh::tend(s_d_auc), 0.0);
|
||||
#endif
|
||||
|
||||
return std::make_pair(auc, n_valid);
|
||||
}
|
||||
@@ -694,15 +653,9 @@ std::tuple<double, double, double> GPUBinaryPRAUC(common::Span<float const> pred
|
||||
dh::XGBCachingDeviceAllocator<char> alloc;
|
||||
double total_pos, total_neg;
|
||||
|
||||
#if defined(XGBOOST_USE_CUDA)
|
||||
thrust::tie(total_pos, total_neg) =
|
||||
thrust::reduce(thrust::cuda::par(alloc), it, it + labels.Size(),
|
||||
Pair{0.0, 0.0}, PairPlus<double, double>{});
|
||||
#elif defined(XGBOOST_USE_HIP)
|
||||
thrust::tie(total_pos, total_neg) =
|
||||
thrust::reduce(thrust::hip::par(alloc), it, it + labels.Size(),
|
||||
Pair{0.0, 0.0}, PairPlus<double, double>{});
|
||||
#endif
|
||||
|
||||
if (total_pos <= 0.0 || total_neg <= 0.0) {
|
||||
return {0.0f, 0.0f, 0.0f};
|
||||
@@ -755,17 +708,10 @@ double GPUMultiClassPRAUC(Context const *ctx, common::Span<float const> predts,
|
||||
});
|
||||
dh::XGBCachingDeviceAllocator<char> alloc;
|
||||
|
||||
#if defined(XGBOOST_USE_CUDA)
|
||||
thrust::reduce_by_key(thrust::cuda::par(alloc), key_it,
|
||||
key_it + predts.size(), val_it,
|
||||
thrust::make_discard_iterator(), totals.begin(),
|
||||
thrust::equal_to<size_t>{}, PairPlus<double, double>{});
|
||||
#elif defined(XGBOOST_USE_HIP)
|
||||
thrust::reduce_by_key(thrust::hip::par(alloc), key_it,
|
||||
key_it + predts.size(), val_it,
|
||||
thrust::make_discard_iterator(), totals.begin(),
|
||||
thrust::equal_to<size_t>{}, PairPlus<double, double>{});
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Calculate AUC
|
||||
@@ -834,7 +780,6 @@ GPURankingPRAUCImpl(common::Span<float const> predts, MetaInfo const &info,
|
||||
dh::TemporaryArray<uint32_t> unique_class_ptr(d_group_ptr.size());
|
||||
auto d_unique_class_ptr = dh::ToSpan(unique_class_ptr);
|
||||
|
||||
#if defined(XGBOOST_USE_CUDA)
|
||||
auto n_uniques = dh::SegmentedUniqueByKey(
|
||||
thrust::cuda::par(alloc),
|
||||
dh::tbegin(d_group_ptr),
|
||||
@@ -845,18 +790,6 @@ GPURankingPRAUCImpl(common::Span<float const> predts, MetaInfo const &info,
|
||||
d_unique_class_ptr.data(),
|
||||
dh::tbegin(d_unique_idx),
|
||||
thrust::equal_to<thrust::pair<uint32_t, float>>{});
|
||||
#elif defined(XGBOOST_USE_HIP)
|
||||
auto n_uniques = dh::SegmentedUniqueByKey(
|
||||
thrust::hip::par(alloc),
|
||||
dh::tbegin(d_group_ptr),
|
||||
dh::tend(d_group_ptr),
|
||||
uni_key,
|
||||
uni_key + d_sorted_idx.size(),
|
||||
dh::tbegin(d_unique_idx),
|
||||
d_unique_class_ptr.data(),
|
||||
dh::tbegin(d_unique_idx),
|
||||
thrust::equal_to<thrust::pair<uint32_t, float>>{});
|
||||
#endif
|
||||
|
||||
d_unique_idx = d_unique_idx.subspan(0, n_uniques);
|
||||
|
||||
@@ -909,15 +842,9 @@ GPURankingPRAUCImpl(common::Span<float const> predts, MetaInfo const &info,
|
||||
return thrust::make_pair(0.0, static_cast<uint32_t>(1));
|
||||
});
|
||||
|
||||
#if defined(XGBOOST_USE_CUDA)
|
||||
thrust::tie(auc, invalid_groups) = thrust::reduce(
|
||||
thrust::cuda::par(alloc), it, it + n_groups,
|
||||
thrust::pair<double, uint32_t>(0.0, 0), PairPlus<double, uint32_t>{});
|
||||
#elif defined(XGBOOST_USE_HIP)
|
||||
thrust::tie(auc, invalid_groups) = thrust::reduce(
|
||||
thrust::hip::par(alloc), it, it + n_groups,
|
||||
thrust::pair<double, uint32_t>(0.0, 0), PairPlus<double, uint32_t>{});
|
||||
#endif
|
||||
}
|
||||
return std::make_pair(auc, n_groups - invalid_groups);
|
||||
}
|
||||
@@ -949,17 +876,10 @@ std::pair<double, std::uint32_t> GPURankingPRAUC(Context const *ctx,
|
||||
dh::XGBDeviceAllocator<char> alloc;
|
||||
auto labels = info.labels.View(ctx->Device());
|
||||
|
||||
#if defined(XGBOOST_USE_CUDA)
|
||||
if (thrust::any_of(thrust::cuda::par(alloc), dh::tbegin(labels.Values()),
|
||||
dh::tend(labels.Values()), PRAUCLabelInvalid{})) {
|
||||
InvalidLabels();
|
||||
}
|
||||
#elif defined(XGBOOST_USE_HIP)
|
||||
if (thrust::any_of(thrust::hip::par(alloc), dh::tbegin(labels.Values()),
|
||||
dh::tend(labels.Values()), PRAUCLabelInvalid{})) {
|
||||
InvalidLabels();
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Get total positive/negative for each group.
|
||||
@@ -981,17 +901,10 @@ std::pair<double, std::uint32_t> GPURankingPRAUC(Context const *ctx,
|
||||
return thrust::make_pair(y * w, (1.0 - y) * w);
|
||||
});
|
||||
|
||||
#if defined(XGBOOST_USE_CUDA)
|
||||
thrust::reduce_by_key(thrust::cuda::par(alloc), key_it,
|
||||
key_it + predts.size(), val_it,
|
||||
thrust::make_discard_iterator(), totals.begin(),
|
||||
thrust::equal_to<size_t>{}, PairPlus<double, double>{});
|
||||
#elif defined(XGBOOST_USE_HIP)
|
||||
thrust::reduce_by_key(thrust::hip::par(alloc), key_it,
|
||||
key_it + predts.size(), val_it,
|
||||
thrust::make_discard_iterator(), totals.begin(),
|
||||
thrust::equal_to<size_t>{}, PairPlus<double, double>{});
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Calculate AUC
|
||||
|
||||
@@ -30,6 +30,12 @@
|
||||
#include "../common/device_helpers.cuh"
|
||||
#endif // defined(XGBOOST_USE_CUDA) || defined(XGBOOST_USE_HIP)
|
||||
|
||||
#if defined(XGBOOST_USE_HIP)
|
||||
namespace thrust {
|
||||
namespace cuda = thrust::hip;
|
||||
}
|
||||
#endif
|
||||
|
||||
namespace xgboost {
|
||||
namespace metric {
|
||||
// tag the this file, used by force static link later.
|
||||
@@ -47,7 +53,7 @@ PackedReduceResult Reduce(Context const* ctx, MetaInfo const& info, Fn&& loss) {
|
||||
PackedReduceResult result;
|
||||
auto labels = info.labels.View(ctx->Device());
|
||||
if (ctx->IsCUDA()) {
|
||||
#if defined(XGBOOST_USE_CUDA)
|
||||
#if defined(XGBOOST_USE_CUDA) || defined(XGBOOST_USE_HIP)
|
||||
dh::XGBCachingDeviceAllocator<char> alloc;
|
||||
thrust::counting_iterator<size_t> begin(0);
|
||||
thrust::counting_iterator<size_t> end = begin + labels.Size();
|
||||
@@ -62,21 +68,6 @@ PackedReduceResult Reduce(Context const* ctx, MetaInfo const& info, Fn&& loss) {
|
||||
return PackedReduceResult{v, wt};
|
||||
},
|
||||
PackedReduceResult{}, thrust::plus<PackedReduceResult>());
|
||||
#elif defined(XGBOOST_USE_HIP)
|
||||
dh::XGBCachingDeviceAllocator<char> alloc;
|
||||
thrust::counting_iterator<size_t> begin(0);
|
||||
thrust::counting_iterator<size_t> end = begin + labels.Size();
|
||||
result = thrust::transform_reduce(
|
||||
thrust::hip::par(alloc), begin, end,
|
||||
[=] XGBOOST_DEVICE(size_t i) {
|
||||
auto idx = linalg::UnravelIndex(i, labels.Shape());
|
||||
auto sample_id = std::get<0>(idx);
|
||||
auto target_id = std::get<1>(idx);
|
||||
auto res = loss(i, sample_id, target_id);
|
||||
float v{std::get<0>(res)}, wt{std::get<1>(res)};
|
||||
return PackedReduceResult{v, wt};
|
||||
},
|
||||
PackedReduceResult{}, thrust::plus<PackedReduceResult>());
|
||||
#else
|
||||
common::AssertGPUSupport();
|
||||
#endif // defined(XGBOOST_USE_CUDA)
|
||||
|
||||
@@ -24,6 +24,12 @@
|
||||
#include "../common/device_helpers.cuh"
|
||||
#endif // XGBOOST_USE_CUDA || XGBOOST_USE_HIP
|
||||
|
||||
#if defined(XGBOOST_USE_HIP)
|
||||
namespace thrust {
|
||||
namespace cuda = thrust::hip;
|
||||
}
|
||||
#endif
|
||||
|
||||
namespace xgboost {
|
||||
namespace metric {
|
||||
// tag the this file, used by force static link later.
|
||||
@@ -104,7 +110,6 @@ class MultiClassMetricsReduction {
|
||||
|
||||
dh::XGBCachingDeviceAllocator<char> alloc;
|
||||
|
||||
#if defined(XGBOOST_USE_CUDA)
|
||||
PackedReduceResult result = thrust::transform_reduce(
|
||||
thrust::cuda::par(alloc),
|
||||
begin, end,
|
||||
@@ -122,25 +127,6 @@ class MultiClassMetricsReduction {
|
||||
},
|
||||
PackedReduceResult(),
|
||||
thrust::plus<PackedReduceResult>());
|
||||
#elif defined(XGBOOST_USE_HIP)
|
||||
PackedReduceResult result = thrust::transform_reduce(
|
||||
thrust::hip::par(alloc),
|
||||
begin, end,
|
||||
[=] XGBOOST_DEVICE(size_t idx) {
|
||||
bst_float weight = is_null_weight ? 1.0f : s_weights[idx];
|
||||
bst_float residue = 0;
|
||||
auto label = static_cast<int>(s_labels[idx]);
|
||||
if (label >= 0 && label < static_cast<int32_t>(n_class)) {
|
||||
residue = EvalRowPolicy::EvalRow(
|
||||
label, &s_preds[idx * n_class], n_class) * weight;
|
||||
} else {
|
||||
s_label_error[0] = label;
|
||||
}
|
||||
return PackedReduceResult{ residue, weight };
|
||||
},
|
||||
PackedReduceResult(),
|
||||
thrust::plus<PackedReduceResult>());
|
||||
#endif
|
||||
|
||||
CheckLabelError(s_label_error[0], n_class);
|
||||
|
||||
|
||||
@@ -25,6 +25,12 @@
|
||||
#include "../common/device_helpers.cuh"
|
||||
#endif // XGBOOST_USE_CUDA || XGBOOST_USE_HIP
|
||||
|
||||
#if defined(XGBOOST_USE_HIP)
|
||||
namespace thrust {
|
||||
namespace cuda = thrust::hip;
|
||||
}
|
||||
#endif
|
||||
|
||||
using AFTParam = xgboost::common::AFTParam;
|
||||
using ProbabilityDistributionType = xgboost::common::ProbabilityDistributionType;
|
||||
template <typename Distribution>
|
||||
@@ -103,7 +109,6 @@ class ElementWiseSurvivalMetricsReduction {
|
||||
|
||||
dh::XGBCachingDeviceAllocator<char> alloc;
|
||||
|
||||
#if defined(XGBOOST_USE_CUDA)
|
||||
PackedReduceResult result = thrust::transform_reduce(
|
||||
thrust::cuda::par(alloc),
|
||||
begin, end,
|
||||
@@ -118,22 +123,6 @@ class ElementWiseSurvivalMetricsReduction {
|
||||
},
|
||||
PackedReduceResult(),
|
||||
thrust::plus<PackedReduceResult>());
|
||||
#elif defined(XGBOOST_USE_HIP)
|
||||
PackedReduceResult result = thrust::transform_reduce(
|
||||
thrust::hip::par(alloc),
|
||||
begin, end,
|
||||
[=] XGBOOST_DEVICE(size_t idx) {
|
||||
double weight = is_null_weight ? 1.0 : static_cast<double>(s_weights[idx]);
|
||||
double residue = d_policy.EvalRow(
|
||||
static_cast<double>(s_label_lower_bound[idx]),
|
||||
static_cast<double>(s_label_upper_bound[idx]),
|
||||
static_cast<double>(s_preds[idx]));
|
||||
residue *= weight;
|
||||
return PackedReduceResult{residue, weight};
|
||||
},
|
||||
PackedReduceResult(),
|
||||
thrust::plus<PackedReduceResult>());
|
||||
#endif
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user