Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
666a617
Optimize sum_rows_by_key kernel
huuanhhuyn Oct 15, 2026
7658a18
Keep the old kernel for K <=32. It is more optimized for that range.
huuanhhuyn Oct 15, 2026
82fa848
Extra kernel - Sort rows by key before summing
huuanhhuyn Oct 16, 2026
eb08f52
Merge remote-tracking branch 'origin/main' into improve_kmeans_fit
huuanhhuyn Oct 16, 2026
a9ae4fa
Merge branch 'main' into improve_kmeans_fit
divyegala Oct 16, 2026
cad9556
Merge branch 'main' into improve_kmeans_fit
huuanhhuyn Oct 19, 2026
d6e7877
Add vectorized load kernel - improve latency-bound at K < 64, given 1…
huuanhhuyn Oct 19, 2026
f1f3b67
Single kernel selection point. Tune params.
huuanhhuyn Oct 19, 2026
31e3608
Fix code rabbit
huuanhhuyn Oct 19, 2026
cf53129
Static assert weight and sum types
huuanhhuyn Oct 27, 2026
cc4af47
Reuse / extend libraries.
huuanhhuyn Oct 27, 2026
30da49c
Dry-run compliance. No dry-run tests
huuanhhuyn Oct 27, 2026
68538fe
Clang-format
huuanhhuyn Oct 27, 2026
18d4d8a
Merge branch 'main' into improve_kmeans_fit
huuanhhuyn Oct 27, 2026
1113f2b
Dry-run unit test for allocating path (sorted variant)
huuanhhuyn Oct 29, 2026
2ef39df
Merge branch 'main' into improve_kmeans_fit
huuanhhuyn Oct 29, 2026
d858619
Batching
huuanhhuyn Oct 30, 2026
e226e00
New kernels: few keys and warp reduce. Simplify kernel selection. Ker…
huuanhhuyn Nov 4, 2026
3179117
Merge remote-tracking branch 'origin/main' into improve_kmeans_fit
huuanhhuyn Nov 4, 2026
9860e64
Tune: Few-keys kernel available up to 128 features (incl. DEEP dataset)
huuanhhuyn Nov 4, 2026
fcf2349
Remove used colmajor code
huuanhhuyn Nov 5, 2026
99e7bc2
Merge branch 'main' into improve_kmeans_fit
huuanhhuyn Nov 5, 2026
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
Dry-run compliance. No dry-run tests
  • Loading branch information
huuanhhuyn committed Oct 27, 2026
commit 30da49cf2fdebd2e6071b906e4220ac286c52f9b
44 changes: 27 additions & 17 deletions cpp/include/raft/linalg/detail/reduce_rows_by_key.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -451,19 +451,11 @@ void sum_rows_by_key_large_nkeys_rowmajor_sorted(const DataIteratorT d_A,
IdxT ncols,
IdxT nkeys,
SumsT* d_sums,
cudaStream_t st)
cudaStream_t st,
bool dry_run)
{
rmm::device_uvector<uint32_t> keys(nrows, st), row_ids(nrows, st);
rmm::device_uvector<uint32_t> sorted_keys(nrows, st), sorted_row_ids(nrows, st);
constexpr uint32_t block_dim = 256;
raft::launch_kernel(st,
static_cast<uint32_t>(ceildiv<IdxT>(nrows, IdxT{block_dim})),
block_dim,
sum_rows_by_key_sort_input_kernel,
d_keys,
nrows,
keys.data(),
row_ids.data());
// Only the bits that can be set in a key in [0, nkeys) take part in the sort.
int end_bit = 1;
while (end_bit < 32 && (IdxT{1} << end_bit) < nkeys) {
Expand All @@ -481,6 +473,17 @@ void sum_rows_by_key_large_nkeys_rowmajor_sorted(const DataIteratorT d_A,
end_bit,
st));
rmm::device_buffer tmp(tmp_bytes, st);
Comment thread
achirkin marked this conversation as resolved.
Resolved
if (dry_run) { return; }

constexpr uint32_t block_dim = 256;
raft::launch_kernel(st,
static_cast<uint32_t>(ceildiv<IdxT>(nrows, IdxT{block_dim})),
block_dim,
sum_rows_by_key_sort_input_kernel,
d_keys,
nrows,
keys.data(),
row_ids.data());
RAFT_CUDA_TRY(cub::DeviceRadixSort::SortPairs(tmp.data(),
tmp_bytes,
keys.data(),
Expand Down Expand Up @@ -588,9 +591,11 @@ void sum_rows_by_key_large_nkeys_rowmajor(const DataIteratorT d_A,
IdxT ncols,
IdxT nkeys,
SumsT* d_sums,
cudaStream_t st)
cudaStream_t st,
bool dry_run)
{
const auto plan = sum_rows_by_key_select_kernel(d_A, lda, nrows, ncols, nkeys, d_sums);
if (dry_run && plan.kernel != sum_rows_by_key_kernel::sorted) { return; }
switch (plan.kernel) {
case sum_rows_by_key_kernel::per_float4:
if constexpr (std::is_same_v<SumsT, float> && (std::is_same_v<DataIteratorT, const float*> ||
Expand Down Expand Up @@ -626,7 +631,7 @@ void sum_rows_by_key_large_nkeys_rowmajor(const DataIteratorT d_A,
break;
case sum_rows_by_key_kernel::sorted:
sum_rows_by_key_large_nkeys_rowmajor_sorted(
d_A, lda, d_keys, d_weights, nrows, ncols, nkeys, d_sums, st);
d_A, lda, d_keys, d_weights, nrows, ncols, nkeys, d_sums, st, dry_run);
break;
case sum_rows_by_key_kernel::per_warp:
sum_rows_by_key_large_nkeys_rowmajor_per_warp(d_A,
Expand Down Expand Up @@ -679,7 +684,8 @@ void reduce_rows_by_key(const DataIteratorT d_A,
IdxT nkeys,
SumsT* d_sums,
cudaStream_t stream,
bool reset_sums)
bool reset_sums,
bool dry_run = false)
{
typedef typename std::iterator_traits<KeysIteratorT>::value_type KeyType;
static_assert(
Expand All @@ -689,9 +695,10 @@ void reduce_rows_by_key(const DataIteratorT d_A,
"weight precision, we throw an error here.");

// Following kernel needs memset
if (reset_sums) { cudaMemsetAsync(d_sums, 0, ncols * nkeys * sizeof(SumsT), stream); }
if (reset_sums && !dry_run) { cudaMemsetAsync(d_sums, 0, ncols * nkeys * sizeof(SumsT), stream); }

if (d_keys_char != nullptr && nkeys <= SUM_ROWS_BY_KEY_SMALL_K_MAX_K) {
if (dry_run) { return; }
// sum_rows_by_key_small_k is BW bounded. d_keys is loaded ncols time - avoiding wasting BW
// with doubles we have ~20% speed up - with floats we can hope something around 2x
// Converting d_keys to char
Expand All @@ -700,7 +707,7 @@ void reduce_rows_by_key(const DataIteratorT d_A,
d_A, lda, d_keys_char, d_weights, nrows, ncols, nkeys, d_sums, stream);
} else {
sum_rows_by_key_large_nkeys_rowmajor(
d_A, lda, d_keys, d_weights, nrows, ncols, nkeys, d_sums, stream);
d_A, lda, d_keys, d_weights, nrows, ncols, nkeys, d_sums, stream, dry_run);
}
}

Expand All @@ -721,6 +728,7 @@ void reduce_rows_by_key(const DataIteratorT d_A,
* @param[in] nkeys Number of unique keys in d_keys
* @param[out] d_sums Row sums by key (ncols x d_keys)
* @param[in] stream CUDA stream
* @param[in] reset_sums Whether to reset the output sums to zero before reducing
*/
template <typename DataIteratorT, typename KeysIteratorT, typename SumsT, typename IdxT>
void reduce_rows_by_key(const DataIteratorT d_A,
Expand All @@ -732,7 +740,8 @@ void reduce_rows_by_key(const DataIteratorT d_A,
IdxT nkeys,
SumsT* d_sums,
cudaStream_t stream,
bool reset_sums)
bool reset_sums,
bool dry_run = false)
{
typedef typename std::iterator_traits<DataIteratorT>::value_type DataType;
reduce_rows_by_key(d_A,
Expand All @@ -745,7 +754,8 @@ void reduce_rows_by_key(const DataIteratorT d_A,
nkeys,
d_sums,
stream,
reset_sums);
reset_sums,
dry_run);
}

}; // end namespace detail
Expand Down
46 changes: 24 additions & 22 deletions cpp/include/raft/linalg/reduce_rows_by_key.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -149,36 +149,38 @@ void reduce_rows_by_key(
std::optional<raft::device_vector_view<const WeightType, IndexType>> d_weights = std::nullopt,
bool reset_sums = true)
{
if (resource::get_dry_run_flag(handle)) { return; }
RAFT_EXPECTS(d_A.extent(0) == d_A.extent(0) && d_sums.extent(1) == n_unique_keys,
"Output is not of size ncols * n_unique_keys");
RAFT_EXPECTS(d_keys.extent(0) == d_A.extent(1), "Keys is not of size nrows");

const bool dry_run = resource::get_dry_run_flag(handle);
if (d_weights) {
RAFT_EXPECTS(d_weights.value().extent(0) == d_A.extent(1), "Weights is not of size nrows");

reduce_rows_by_key(d_A.data_handle(),
d_A.extent(0),
d_keys.data_handle(),
d_weights.value().data_handle(),
d_keys_char.data_handle(),
d_A.extent(1),
d_A.extent(0),
n_unique_keys,
d_sums.data_handle(),
resource::get_cuda_stream(handle).get(),
reset_sums);
detail::reduce_rows_by_key(d_A.data_handle(),
d_A.extent(0),
d_keys.data_handle(),
d_weights.value().data_handle(),
d_keys_char.data_handle(),
d_A.extent(1),
d_A.extent(0),
n_unique_keys,
d_sums.data_handle(),
resource::get_cuda_stream(handle).get(),
reset_sums,
dry_run);
} else {
reduce_rows_by_key(d_A.data_handle(),
d_A.extent(0),
d_keys.data_handle(),
d_keys_char.data_handle(),
d_A.extent(1),
d_A.extent(0),
n_unique_keys,
d_sums.data_handle(),
resource::get_cuda_stream(handle).get(),
reset_sums);
detail::reduce_rows_by_key(d_A.data_handle(),
d_A.extent(0),
d_keys.data_handle(),
d_keys_char.data_handle(),
d_A.extent(1),
d_A.extent(0),
n_unique_keys,
d_sums.data_handle(),
resource::get_cuda_stream(handle).get(),
reset_sums,
dry_run);
}
}

Expand Down
17 changes: 17 additions & 0 deletions cpp/tests/linalg/reduce_rows_by_key.cu
Original file line number Diff line number Diff line change
Expand Up @@ -262,5 +262,22 @@ INSTANTIATE_TEST_CASE_P(ReduceRowTests,
ReduceRowTestManyClusters,
::testing::ValuesIn(inputsf_many_cluster));

// ReduceRowTestVectorized
// 100000 Obs, 16 cols, 2048 clusters: Should trigger the vectorized kernel on GPU newer than 9.0.
const std::vector<ReduceRowsInputs<float>> inputsf_vectorized = {
{0.00001f, 100000, 16, 2048, 1234ULL, false},
{0.00001f, 100000, 16, 2048, 1234ULL, true, 32.0},
{0.00001f, 100000, 16, 2048, 1234ULL, true, 16.0}};
typedef ReduceRowTest<float> ReduceRowTestVectorized;
TEST_P(ReduceRowTestVectorized, Result)
{
ASSERT_TRUE(raft::devArrMatch(out_ref.data(),
out.data(),
params.cols * params.nkeys,
raft::CompareApprox<float>(params.tolerance)));
}
INSTANTIATE_TEST_CASE_P(ReduceRowTests,
ReduceRowTestVectorized,
::testing::ValuesIn(inputsf_vectorized));
} // end namespace linalg
} // end namespace raft
Loading