Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Keep Inf when the finite bin total overflows float
  • Loading branch information
bdice committed Nov 15, 2026
commit 96b1c26dad641542fd8a634a71661d0e5be5fe0b
4 changes: 3 additions & 1 deletion cpp/include/raft/linalg/detail/reduce_rows_by_key.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,9 @@ RAFT_KERNEL rrbk_bins_finish_kernel(
for (int b = pl.nbins - 1; b >= 0; --b) {
s += static_cast<double>(bins[b]) * rrbk_pow2(b * pl.bw - pl.off - 149);
}
d_sums[i] += static_cast<float>(s);
// An Inf or NaN already added by the add kernel absorbs any finite total; adding a finite total
// that overflows float could otherwise turn +Inf into NaN.
if (isfinite(d_sums[i])) { d_sums[i] += static_cast<float>(s); }
}

template <typename DataIteratorT, typename KeysIteratorT, typename WeightT, typename IdxT>
Expand Down
30 changes: 30 additions & 0 deletions cpp/tests/linalg/reduce_rows_by_key.cu
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,36 @@ TEST(ReduceRowsByKey, NonFinite)
}
}

TEST(ReduceRowsByKey, InfWithOverflowingFiniteSum)
{
// +Inf with finite terms whose sum overflows float must stay +Inf, not become NaN.
raft::resources handle;
auto stream = resource::get_cuda_stream(handle).get();
constexpr int nrows = 3;
constexpr int nkeys = 10000;
const float max = std::numeric_limits<float>::max();
std::vector<float> h_in = {std::numeric_limits<float>::infinity(), -max, -max};
std::vector<uint32_t> h_keys = {0, 0, 0};
rmm::device_uvector<float> in(nrows, stream), sums(nkeys, stream);
rmm::device_uvector<uint32_t> keys(nrows, stream);
raft::update_device(in.data(), h_in.data(), nrows, stream);
raft::update_device(keys.data(), h_keys.data(), nrows, stream);
reduce_rows_by_key(in.data(),
1,
keys.data(),
static_cast<char*>(nullptr),
nrows,
1,
nkeys,
sums.data(),
stream,
true);
float out = 0;
raft::update_host(&out, sums.data(), 1, stream);
resource::sync_stream(handle, stream);
EXPECT_EQ(out, std::numeric_limits<float>::infinity());
}

TEST(ReduceRowsByKeyDryRun, Allocations)
{
raft::resources handle;
Expand Down
Loading