Repository navigation
Conversation
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>
Owner
Author
|
Opened upstream as NVIDIA#3195. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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=3on 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 <= 4on wide data, most of the time goes to thenkeys <= 4kernel ofreduce_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_keyuses 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 fornkeys > 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:
charanymore, sod_keys_charis unused. I kept the parameter (and theraft::device_vector_viewoverload) for API compatibility and documented it as unused.d_keys_char = nullptrwithnkeys <= 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 withn_clusters <= 4uses the small-nkeys kernel. With cuML 26.10,KMeans(n_clusters=3)on 950000 x 1000 float32 data is ~4x slower thann_clusters=5, and an ncu profile shows 84% of the GPU time insum_rows_by_key_small_nkeys_kernel: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_keyonmain(with a keys workspace for the small-nkeys kernel, withd_keys_char = nullptrfor 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:
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:
mainCompared 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 = 3cases to thereduce_rows_by_keybenchmark.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.cuon 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
mainand once against this branch🤖 Generated with Claude Code