Skip to content

Coalesced small-nkeys kernel for reduce_rows_by_key - #2

Closed
cakedev0 wants to merge 1 commit into
mainfrom
small-nkeys-coalesced
Closed

cakedev0 wants to merge 1 commit into
mainfrom
small-nkeys-coalesced

Conversation

@cakedev0

@cakedev0 cakedev0 commented Oct 28, 2026 •

Copy link
Copy Markdown
Owner

Hi! I work on CPU performance in scikit-learn. I was comparing @ogrisel's GPU k-means experiments with recent CPU k-means optimizations in scikit-learn (mainly rewriting the k-means++ init in Cython instead of single-threaded NumPy), and I was surprised that in some cases (e.g. n_clusters=3 on 1000 features) cuML k-means wasn't much faster than our CPU version on a laptop CPU. So I asked Claude to check whether cuML k-means could be improved in those cases.

It found that with n_clusters <= 4 on wide data, most of the time goes to the nkeys <= 4 kernel of reduce_rows_by_key, and it came up with two fixes: a cheap one (#1) and a more ambitious one (this PR: a new kernel, faster in more cases, but a bigger change to review).

I don't plan to work on this further, so feel free to take over either of them if you think it's worth it.


The rest of this description was written by Claude (Opus 5.5).

For nkeys <= 4, reduce_rows_by_key uses a kernel that gives one column to each block, and the threads of a warp read consecutive rows of that column (d_A[irow * lda + idim]). On row-major data the loads aren't coalesced, so as soon as there are more than ~16 columns it's much slower than the kernel used for nkeys > 4 (~13x slower at 256 columns in float). #1 only restricts that kernel to few columns, with thresholds measured on an L4. This PR replaces it with a kernel that reads row-major data coalesced instead.

The new kernel: each block sums a tile of up to 256 contiguous columns over a chunk of rows. The lanes of a warp read contiguous columns of the same rows (narrower tiles for narrow rows), and each thread keeps one running sum per key in registers. The block then reduces them in shared memory and does one global atomic per key and column. It's within ~1.2x of the faster of the two current kernels on every input I tried, and much faster than both from 16 to 256 columns. So there's no threshold to tune per GPU.

Two behavior changes:

  • The keys don't need to be converted to char anymore, so d_keys_char is unused. I kept the parameter (and the raft::device_vector_view overload) for API compatibility and documented it as unused.
  • Callers that pass d_keys_char = nullptr with nkeys <= 4 (cuVS balanced k-means) now also use the new kernel, instead of the row-major one. It's faster for them too, see below.

This hits cuVS k-means: compute_centroid_adjustments (cpp/src/cluster/detail/kmeans_common.cuh) passes a workspace, so every Lloyd iteration with n_clusters <= 4 uses the small-nkeys kernel. With cuML 26.10, KMeans(n_clusters=3) on 950000 x 1000 float32 data is ~4x slower than n_clusters=5, and an ncu profile shows 84% of the GPU time in sum_rows_by_key_small_nkeys_kernel:

import time

import cupy as cp
from cuml.cluster import KMeans

X = cp.random.default_rng(0).standard_normal((950_000, 1000), dtype=cp.float32)

for k in [3, 4, 5, 8]:
    km = KMeans(n_clusters=k, init="random", n_init=1, max_iter=12, tol=1e-8, random_state=0)
    km.fit(X[:10_000])  # warm-up
    cp.cuda.runtime.deviceSynchronize()
    tic = time.perf_counter()
    km.fit(X)
    cp.cuda.runtime.deviceSynchronize()
    print(f"n_clusters={k}: {time.perf_counter() - tic:.2f} s ({km.n_iter_} iterations)")
n_clusters=3: 3.61 s (12 iterations)
n_clusters=4: 3.63 s (12 iterations)
n_clusters=5: 0.92 s (12 iterations)
n_clusters=8: 0.92 s (12 iterations)

The 26.10 wheels don't have NVIDIA#3133 yet, which makes the current small-nkeys kernel much faster with few columns in float. The measurements below use the kernels of main.

Benchmarks

detail::reduce_rows_by_key on main (with a keys workspace for the small-nkeys kernel, with d_keys_char = nullptr for the row-major kernel) and on this branch. Row-major data, unit weights, best of several runs, on an NVIDIA L4. The benchmark is in the details below.

1000000 rows, 3 uniformly random keys:

columns float, main small-nkeys float, main row-major float, this PR double, main small-nkeys double, main row-major double, this PR
1 0.037 ms 0.74 ms 0.011 ms 0.066 ms 1.04 ms 0.041 ms
4 0.051 ms 0.81 ms 0.018 ms 0.16 ms 2.36 ms 0.12 ms
16 0.47 ms 1.64 ms 0.29 ms 1.03 ms 5.98 ms 0.55 ms
32 3.17 ms 1.95 ms 0.54 ms 2.70 ms 11.95 ms 1.05 ms
64 8.60 ms 3.85 ms 1.04 ms 6.88 ms 11.95 ms 2.03 ms
128 18.79 ms 4.07 ms 2.01 ms 20.13 ms 12.17 ms 4.04 ms
256 54.47 ms 4.27 ms 3.95 ms 53.12 ms 12.41 ms 8.01 ms

With 100000 rows and 1000 columns: 3.95 ms (main small-nkeys), 1.59 ms (main row-major), 1.81 ms (this PR) in float, and 7.69 ms, 4.50 ms, 3.39 ms in double.

Over a sweep of 1000 to 10M rows, 1 to 4096 columns, 1 to 4 keys, uniform keys or a single key (configurations above 1 ms), the worst slowdown against the fastest of the three kernels:

float double
main 13.8x 6.9x
#1 (column threshold) 5.5x 5.4x
this PR 1.18x 1.16x

Compared to #1, this PR is 2.2x faster in the median case in float (0.85x to 5.5x) and 1.5x in double (0.87x to 5.4x). It reads the data at ~240 GB/s at every width, where the L4 peaks at ~300 GB/s.

Where it's slower: wide float rows whose size isn't a multiple of 128 bytes. With 1000 columns (4000-byte rows), the 128-byte segment read by a warp spans one extra 32-byte sector, so it's ~1.15x slower than the row-major kernel. With 1024 columns, it's on par. I don't see a cheap way around it while keeping the per-column sums in registers.

I only measured on an L4. I'd expect the same picture on other GPUs since the kernel is bandwidth bound everywhere, but I can't check it. I added a few nkeys = 3 cases to the reduce_rows_by_key benchmark.

Tests

The existing small-nkeys test only used 128 rows and 32 columns. I added float cases with 10000 rows, 1 to 4 keys and 1 to 1000 columns (all the tile widths, with and without weights), and a double test. I ran cpp/tests/linalg/reduce_rows_by_key.cu on the L4 (compiled with nvcc 13.2 against the headers of this branch and the RMM/rapids_logger of the 26.10 wheels): 25 tests pass. I didn't build cuVS against this branch, so the k-means speed-up is estimated from the kernel timings, not measured.

AI usage disclosure

I did the investigation, the benchmarks and the change with Claude Code (closely guided).

Benchmark (bench_rrbk.cu), compiled once against the headers of main and once against this branch
// RAFT reduce_rows_by_key with k <= 4: small-k kernel (keys workspace given, as
// cuVS kmeans_common does) vs row-major atomics kernel (nullptr workspace, as
// cuVS kmeans_balanced does). Weighted sums, row-major X, as in cuVS k-means.
//
// Output: one CSV line per (dtype, keys distribution, n, d, k):
// dtype,labels,n,d,k,small_k_ms,atomics_ms,max_rel_diff

#include <raft/linalg/detail/reduce_rows_by_key.cuh>

#include <cuda_runtime.h>

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <random>
#include <vector>

#define CHECK(x)                                                          \
  do {                                                                    \
    cudaError_t e = (x);                                                  \
    if (e != cudaSuccess) {                                               \
      std::fprintf(stderr, "%s:%d %s\n", __FILE__, __LINE__, cudaGetErrorString(e)); \
      std::exit(1);                                                       \
    }                                                                     \
  } while (0)

template <typename T>
float time_ms(int repeat, T&& fn)
{
  cudaEvent_t start, stop;
  CHECK(cudaEventCreate(&start));
  CHECK(cudaEventCreate(&stop));
  fn();  // warm-up
  float best = 1e30f;
  for (int r = 0; r < repeat; r++) {
    CHECK(cudaEventRecord(start));
    fn();
    CHECK(cudaEventRecord(stop));
    CHECK(cudaEventSynchronize(stop));
    float ms;
    CHECK(cudaEventElapsedTime(&ms, start, stop));
    best = std::min(best, ms);
  }
  CHECK(cudaEventDestroy(start));
  CHECK(cudaEventDestroy(stop));
  return best;
}

template <typename T>
void run(const char* dtype, int n, int d, int k, bool one_label, const T* X, const T* w, int* keys,
         char* keys_char, T* sums_a, T* sums_b, std::vector<int>& h_keys)
{
  std::mt19937 rng(n + d + k);
  std::uniform_int_distribution<int> dist(0, k - 1);
  for (int i = 0; i < n; i++)
    h_keys[i] = one_label ? 0 : dist(rng);
  CHECK(cudaMemcpy(keys, h_keys.data(), n * sizeof(int), cudaMemcpyHostToDevice));

  auto small_k = [&]() {
    raft::linalg::detail::reduce_rows_by_key(
      X, d, keys, w, keys_char, n, d, k, sums_a, cudaStream_t{0}, true);
  };
  auto atomics = [&]() {
    raft::linalg::detail::reduce_rows_by_key(
      X, d, keys, w, (char*)nullptr, n, d, k, sums_b, cudaStream_t{0}, true);
  };
  long long bytes = (long long)n * d * sizeof(T);
  int repeat      = bytes > (1ll << 28) ? 3 : 10;
  float ta        = time_ms(repeat, small_k);
  float tb        = time_ms(repeat, atomics);
  CHECK(cudaGetLastError());

  std::vector<T> a(k * d), b(k * d);
  CHECK(cudaMemcpy(a.data(), sums_a, k * d * sizeof(T), cudaMemcpyDeviceToHost));
  CHECK(cudaMemcpy(b.data(), sums_b, k * d * sizeof(T), cudaMemcpyDeviceToHost));
  double num = 0, den = 0;
  for (int i = 0; i < k * d; i++) {
    num = std::max(num, (double)std::abs(a[i] - b[i]));
    den = std::max(den, (double)std::abs(a[i]));
  }
  std::printf("%s,%s,%d,%d,%d,%.4f,%.4f,%.1e\n", dtype, one_label ? "one" : "uniform", n, d, k, ta,
              tb, den > 0 ? num / den : 0.0);
  std::fflush(stdout);
}

template <typename T>
void sweep(const char* dtype)
{
  const long long max_elems = 1ll << 28;  // 1 GiB of float, 2 GiB of double
  std::vector<int> ns{1000, 10000, 100000, 1000000, 10000000};
  std::vector<int> ds{1, 2, 4, 8, 16, 32, 64, 128, 256, 1000, 4096};
  int max_n = ns.back();
  T *X, *w, *sums_a, *sums_b;
  int* keys;
  char* keys_char;
  CHECK(cudaMalloc(&X, max_elems * sizeof(T)));
  CHECK(cudaMalloc(&w, max_n * sizeof(T)));
  CHECK(cudaMalloc(&keys, max_n * sizeof(int)));
  CHECK(cudaMalloc(&keys_char, max_n));
  CHECK(cudaMalloc(&sums_a, 4 * 4096 * sizeof(T)));
  CHECK(cudaMalloc(&sums_b, 4 * 4096 * sizeof(T)));
  {
    std::vector<T> h(max_elems);
    std::mt19937 rng(0);
    std::normal_distribution<double> nd;
    for (auto& v : h)
      v = (T)nd(rng);
    CHECK(cudaMemcpy(X, h.data(), max_elems * sizeof(T), cudaMemcpyHostToDevice));
    std::vector<T> hw(max_n, (T)1);
    CHECK(cudaMemcpy(w, hw.data(), max_n * sizeof(T), cudaMemcpyHostToDevice));
  }
  std::vector<int> h_keys(max_n);
  for (int one = 0; one < 2; one++)
    for (int n : ns)
      for (int d : ds) {
        if ((long long)n * d > max_elems) continue;
        for (int k = 1; k <= 4; k++)
          run<T>(dtype, n, d, k, one == 1, X, w, keys, keys_char, sums_a, sums_b, h_keys);
      }
  CHECK(cudaFree(X));
  CHECK(cudaFree(w));
  CHECK(cudaFree(keys));
  CHECK(cudaFree(keys_char));
  CHECK(cudaFree(sums_a));
  CHECK(cudaFree(sums_b));
}

int main()
{
  std::printf("dtype,labels,n,d,k,small_k_ms,atomics_ms,max_rel_diff\n");
  sweep<float>("float");
  sweep<double>("double");
  return 0;
}

🤖 Generated with Claude Code

The nkeys <= 4 kernel gave one column to each block, so its loads weren't
coalesced on row-major data: it was up to ~14x slower than the row-major
kernel on wide inputs. The new kernel reads tiles of contiguous columns,
keeps one running sum per key in registers, and does one atomic per key
and column per block. It no longer needs the keys converted to char.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cakedev0

Copy link
Copy Markdown
Owner Author

Opened upstream as NVIDIA#3195.

@cakedev0 cakedev0 closed this Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant