Skip to content

Commit 3423869

Browse files
authored
[backport] Avoid overflow in rounding estimation. (dmlc#11910) (dmlc#11911)
1 parent f32de03 commit 3423869

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

src/common/deterministic.cuh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/**
2-
* Copyright 2020-2023 by XGBoost Contributors
2+
* Copyright 2020-2026, XGBoost Contributors
33
*/
44
#ifndef XGBOOST_COMMON_DETERMINISTIC_CUH_
55
#define XGBOOST_COMMON_DETERMINISTIC_CUH_
66

7-
#include <cmath>
8-
#include <limits> // std::numeric_limits
7+
#include <cmath> // for frexp, ldexp
8+
#include <limits> // for numeric_limits
99

1010
#include "xgboost/base.h" // XGBOOST_DEVICE
1111

@@ -22,7 +22,7 @@ namespace common {
2222
* Summation' by Demmel and Nguyen.
2323
*/
2424
template <typename T>
25-
XGBOOST_DEVICE T CreateRoundingFactor(T max_abs, int n) {
25+
XGBOOST_DEVICE T CreateRoundingFactor(T max_abs, bst_idx_t n) {
2626
T delta = max_abs / (static_cast<T>(1.0) -
2727
static_cast<T>(2.0) * static_cast<T>(n) * std::numeric_limits<T>::epsilon());
2828

src/tree/gpu_hist/histogram.cu

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
namespace xgboost::tree {
1818
namespace {
1919
struct Pair {
20-
GradientPair first;
21-
GradientPair second;
20+
GradientPairPrecise first;
21+
GradientPairPrecise second;
2222
};
2323
__host__ XGBOOST_DEV_INLINE Pair operator+(Pair const& lhs, Pair const& rhs) {
2424
return {lhs.first + rhs.first, lhs.second + rhs.second};
@@ -53,7 +53,7 @@ struct Clip {
5353
auto ng = Nclip(x.GetGrad());
5454
auto nh = Nclip(x.GetHess());
5555

56-
return {GradientPair{pg, ph}, GradientPair{ng, nh}};
56+
return {GradientPairPrecise{pg, ph}, GradientPairPrecise{ng, nh}};
5757
}
5858
};
5959

@@ -81,7 +81,7 @@ GradientQuantiser::GradientQuantiser(Context const* ctx, common::Span<GradientPa
8181
auto rc = collective::GlobalSum(ctx, info, linalg::MakeVec(reinterpret_cast<ReduceT*>(&p), 4));
8282
collective::SafeColl(rc);
8383

84-
GradientPair positive_sum{p.first}, negative_sum{p.second};
84+
GradientSumT positive_sum{p.first}, negative_sum{p.second};
8585

8686
std::size_t total_rows = gpair.size();
8787
rc = collective::GlobalSum(ctx, info, linalg::MakeVec(&total_rows, 1));
@@ -111,8 +111,8 @@ GradientQuantiser::GradientQuantiser(Context const* ctx, common::Span<GradientPa
111111
* rounding is calcuated as exp(m), see the rounding factor calcuation for
112112
* details.
113113
*/
114-
to_fixed_point_ = GradientSumT(static_cast<T>(1) / to_floating_point_.GetGrad(),
115-
static_cast<T>(1) / to_floating_point_.GetHess());
114+
to_fixed_point_ = GradientSumT{static_cast<T>(1) / to_floating_point_.GetGrad(),
115+
static_cast<T>(1) / to_floating_point_.GetHess()};
116116
}
117117

118118
XGBOOST_DEV_INLINE void AtomicAddGpairShared(xgboost::GradientPairInt64* dest,

0 commit comments

Comments
 (0)