Skip to content
Merged
Changes from 2 commits
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
84 changes: 74 additions & 10 deletions cpp/include/raft/linalg/detail/reduce_rows_by_key.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
#include <cub/block/block_reduce.cuh>
#include <cub/util_ptx.cuh>

#include <algorithm>
#include <cstdint>
#include <limits>
#include <type_traits>

#define MAX_BLOCKS 65535u
namespace raft {
Expand Down Expand Up @@ -276,18 +279,22 @@ void sum_rows_by_key_large_nkeys_colmajor(const DataIteratorT d_A,
d_sums);
}

/**
* Large-nkeys reduction with one thread per element, used for rows narrower than a warp
* (ncols < WarpSize), where a warp per row would leave lanes idle.
*/
template <typename DataIteratorT,
typename KeysIteratorT,
typename WeightT,
typename SumsT,
typename IdxT>
RAFT_KERNEL sum_rows_by_key_large_nkeys_kernel_rowmajor(const DataIteratorT d_A,
IdxT lda,
const WeightT* d_weights,
KeysIteratorT d_keys,
IdxT nrows,
IdxT ncols,
SumsT* d_sums)
RAFT_KERNEL sum_rows_by_key_large_nkeys_kernel_rowmajor_per_element(const DataIteratorT d_A,
IdxT lda,
const WeightT* d_weights,
KeysIteratorT d_keys,
IdxT nrows,
IdxT ncols,
SumsT* d_sums)
{
IdxT gid = threadIdx.x + (blockDim.x * static_cast<IdxT>(blockIdx.x));
IdxT j = gid % ncols;
Expand All @@ -299,6 +306,44 @@ RAFT_KERNEL sum_rows_by_key_large_nkeys_kernel_rowmajor(const DataIteratorT d_A,
raft::myAtomicAdd(&d_sums[j + ncols * l], val);
}

/**
* Large-nkeys reduction with one warp per row: the key and weight are loaded once per row and the
* warp's lanes add consecutive columns of the row into d_sums[key].
*/
template <typename DataIteratorT,
typename KeysIteratorT,
typename WeightT,
typename SumsT,
Comment thread
huuanhhuyn marked this conversation as resolved.
Resolved
typename IdxT>
RAFT_KERNEL sum_rows_by_key_large_nkeys_kernel_rowmajor_per_warp(const DataIteratorT d_A,
IdxT lda,
const WeightT* d_weights,
KeysIteratorT d_keys,
IdxT nrows,
IdxT ncols,
SumsT* d_sums)
{
// Plain pointers are read through the read-only cache; other iterators (e.g. a transform
// iterator converting the input type) are dereferenced as is.
constexpr bool kIsPlainPointer =
std::is_same_v<DataIteratorT, const SumsT*> || std::is_same_v<DataIteratorT, SumsT*>;
const IdxT warp_id = (static_cast<IdxT>(blockIdx.x) * blockDim.x + threadIdx.x) / WarpSize;
const IdxT n_warps = static_cast<IdxT>(gridDim.x) * blockDim.x / WarpSize;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
for (IdxT i = warp_id; i < nrows; i += n_warps) {
const auto key = static_cast<IdxT>(d_keys[i]);
const SumsT w = d_weights != nullptr ? static_cast<SumsT>(d_weights[i]) : SumsT(1);
for (IdxT j = laneId(); j < ncols; j += WarpSize) {
SumsT a;
if constexpr (kIsPlainPointer) {
a = __ldg(d_A + lda * i + j);
} else {
a = static_cast<SumsT>(d_A[lda * i + j]);
}
raft::myAtomicAdd(d_sums + ncols * key + j, a * w);
}
}
}

template <typename DataIteratorT,
typename KeysIteratorT,
typename WeightT,
Expand All @@ -313,12 +358,31 @@ void sum_rows_by_key_large_nkeys_rowmajor(const DataIteratorT d_A,
SumsT* d_sums,
cudaStream_t st)
{
uint32_t block_dim = 128;
auto grid_dim = static_cast<uint32_t>(ceildiv<IdxT>(nrows * ncols, (IdxT)block_dim));
constexpr uint32_t block_dim = 128;
if (ncols < WarpSize) {
// Rows narrower than a warp: one thread per element keeps every lane busy.
const auto grid_dim = static_cast<uint32_t>(ceildiv<IdxT>(nrows * ncols, IdxT{block_dim}));
raft::launch_kernel(st,
grid_dim,
block_dim,
sum_rows_by_key_large_nkeys_kernel_rowmajor_per_element,
d_A,
lda,
d_weights,
d_keys,
nrows,
ncols,
d_sums);
return;
}
constexpr uint32_t warps_per_block = block_dim / WarpSize;
// Keep gridDim.x within the launch limit; the kernel's grid-stride loop covers extra rows.
const auto grid_dim =
static_cast<uint32_t>(std::min<IdxT>(ceildiv<IdxT>(nrows, warps_per_block), IdxT{1} << 30));
raft::launch_kernel(st,
grid_dim,
block_dim,
sum_rows_by_key_large_nkeys_kernel_rowmajor,
sum_rows_by_key_large_nkeys_kernel_rowmajor_per_warp,
d_A,
lda,
d_weights,
Expand Down
Loading