fix(kmeans): sum the GPU CSR centroids in a fixed order #4083
All Vouch review confidence checks passed
Residual risk 0.20 = Change Risk (0.50) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.50 · 615 lines diff across 7 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.384
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #4083"
general
weight: 0.35
Alexandr-Solovev
Description
GPU K-means on CSR data was not deterministic: identical
traincalls returned centroids that differed in the last bits, and with a zero or tiny tolerance a different number of iterations. This is what keepstest_kmeans_elkan_resultsfailing for sparse inputs on GPU in uxlfoundation/scikit-learn-intelex#3527: "elkan" falls back to the same Lloyd call, so the two fits in that test are two identical calls.Cause.
update_centroidsinkmeans/backend/gpu/kernels_csr_impl.hppaccumulated the centroid sums with float atomics,atomic_local_addwithin a work-group and thenatomic_global_addfrom every work-group of a cluster, so the order of the additions changed from run to run. The dense GPU path and both CPU paths are deterministic.Change.
train_kernel_lloyd_csr_dpc.cppallocates once before the iterations; a second kernel adds the partitions in index order and divides by the cluster size. Empty clusters keep their centroid, as before.get_centroid_partition_count). This also bounds the kernel range: onmain, 256 clusters with 5000 columns throwProvided range and/or offset does not fit in int. When even two partitions would exceed the cap, there is one partition and it writes the means straight into the centroids, with no scratch buffer and no second pass. No buffer is allocated whenmax_iteration_countis 0.main, and on the first version of this PR, the per-work-group buffer hadcolumn_countvalues, so 20000 float64 columns failed withUR_RESULT_ERROR_OUT_OF_RESOURCES.Tests.
KMeans sparse repeated runs are deterministicno longer skips GPU.KMeans sparse repeated runs on many rows are deterministic(20000 x 32, 8 clusters, tolerance 0, 5 runs). On GPU it fails onmainat the iteration count and passes with the fix, 5 of 5 times with--runs_per_test=5.KMeans sparse trains on data wider than the GPU local memory(256 x 40009, 4 clusters; the prime column count leaves a shorter last tile on every device): without the tiles it aborts withUR_RESULT_ERROR_OUT_OF_RESOURCES; with them two runs give identical, finite centroids.KMeans sparse centroids are the means of their rows when the GPU scratch is capped(2048 x 40009, 512 clusters, one partition in float32 and float64): after one iteration every non-empty centroid is the mean of the rows assigned to the initial centroids.bazel test --config=dpc --device=gpu //cpp/oneapi/dal/algo/kmeans:testspasses.test_kmeans_elkan_resultsgo from 5 failures to 0, and 20 identical fits of the test's data give the samen_iter_every time (onmain: 6, 7 or 8).Performance (measured with Claude Code). Intel GPU, sklearnex
KMeans(n_init=1, max_iter=20, tol=0)on CSR float64 unless noted, median of 7 fits, 3 interleaved rounds ofmainand this PR. Speedup overmain(the three rounds agree within 1%, except where a range is given):| rows x columns, density, clusters | speedup |
|---|---|
| 200000 x 100, 5%, k=32 | 9.8x |
| 200000 x 100, 5%, k=32, float32 | 13.6x |
| 100000 x 1000, 1%, k=128 | 24.9x |
| 500000 x 20, 20%, k=8 | 1.75x |
| 1000000 x 50, 10%, k=2 | 0.97x (0.92 to 1.00) |
| 5000 x 10, 50%, k=5 | 0.88x |
| 50000 x 5000, 0.2%, k=256 |
mainthrows (range does not fit in int) || 100000 x 20000, 0.2%, k=50 |
mainfails (out of resources) || 200000 x 47236, 0.16%, k=64, float64 and float32 |
mainfails (out of resources) |In every case this PR returned identical centroids across the 8 fits and
maindid not. The small case is slower because every iteration now launches the second pass and uses the scratch buffer; with only two clusters there are too few work-groups for the fixed order to pay off.The old kernel ended with one work-item per work-group issuing
column_countglobal atomics in sequence; the second pass replaces that with one work-item per (cluster, column).No public API or ABI changes.
🤖 Generated with Claude Code