← Back to Pull requests

fix(kmeans): sum the GPU CSR centroids in a fixed order #4083

Closed
unknown closed this pull request without merging on Dec 04, 2026 14:55 UTC
View on GitHub
unknown unknown commented on Dec 02, 2026 09:36 UTC
Author

Description

GPU K-means on CSR data was not deterministic: identical train calls returned centroids that differed in the last bits, and with a zero or tiny tolerance a different number of iterations. This is what keeps test_kmeans_elkan_results failing 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_centroids in kmeans/backend/gpu/kernels_csr_impl.hpp accumulated the centroid sums with float atomics, atomic_local_add within a work-group and then atomic_global_add from 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.

  • Each work-group (cluster, row partition) adds its rows into local memory one row at a time, with a barrier after each row; within a row the lanes write distinct columns. The local atomic stays only for duplicate column indices in a row.

  • The partial sums go to a scratch buffer that train_kernel_lloyd_csr_dpc.cpp allocates 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.

  • The number of partitions is capped so the scratch buffer takes at most 128 MiB (get_centroid_partition_count). This also bounds the kernel range: on main, 256 clusters with 5000 columns throw Provided 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 when max_iteration_count is 0.

  • Data wider than the device local memory is summed in column tiles: a work-group owns one (cluster, partition, column tile) and every column still adds its rows in row order. With one tile, which covers half of the local memory (8192 float64 columns on a 128 KB device), the kernel does what it did before. On main, and on the first version of this PR, the per-work-group buffer had column_count values, so 20000 float64 columns failed with UR_RESULT_ERROR_OUT_OF_RESOURCES.
  • Tests.

  • KMeans sparse repeated runs are deterministic no longer skips GPU.

  • New KMeans sparse repeated runs on many rows are deterministic (20000 x 32, 8 clusters, tolerance 0, 5 runs). On GPU it fails on main at the iteration count and passes with the fix, 5 of 5 times with --runs_per_test=5.

  • New 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 with UR_RESULT_ERROR_OUT_OF_RESOURCES; with them two runs give identical, finite centroids.

  • New 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:tests passes.

  • On 200000 x 47236 (0.16%, k=64, 6 tiles in float64, 3 in float32) the GPU result matches the CPU one: the same labels, centroids within 1e-16 (float64) and 1e-7 (float32).

  • Through sklearnex (measured with Claude Code), the 16 GPU sparse cases of test_kmeans_elkan_results go from 5 failures to 0, and 20 identical fits of the test's data give the same n_iter_ every time (on main: 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 of main and this PR. Speedup over main (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 | main throws (range does not fit in int) |
    | 100000 x 20000, 0.2%, k=50 | main fails (out of resources) |
    | 200000 x 47236, 0.16%, k=64, float64 and float32 | main fails (out of resources) |

    In every case this PR returned identical centroids across the 8 fits and main did 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_count global 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

    Diff: 615 lines (+461 / -154) across 7 files Reviewer: Alexandr-Solovev Review duration: 3198m
    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
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 50.2% (MEDIUM) · Higher = larger blast radius
    50.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 19.8% · Safe to merge
    19.8%