fix(kmeans): compute the GPU objective function from the final labels and centroids #3983
All Vouch review confidence checks passed
Residual risk 0.19 = Change Risk (0.48) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.48 · 555 lines diff across 6 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.378
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #3983"
general
weight: 0.35
Vika-F
Description
On the GPU, k-means reported the objective function as the sum of the closest distances that the assignment step computes in the expanded form
|x|^2 - 2 x.c + |c|^2. In float that form cancels for points close to their centroid, so the reported value can differ from the real inertia in every digit:0.0instead of4.0e-8;n_initin scikit-learn-intelex picked a worse clustering because it ranks the inits by this value.The CPU kernel does not have this problem: the DAAL parameter defaults include
computeExactObjectiveFunction, and the objective is recomputed from the final labels and centroids after the last assignment. This change makes the GPU dense train and infer kernels do the same.kernels_fp::compute_exact_distancessums the squared coordinate differences of every row to its assigned centroid (one work-group of sub-group width per row); rows the assignment left without a valid cluster (all distances NaN) keep their assignment-step distance, and the objective is the reduction of those distances. The cluster assignment itself is unchanged, and so are the SPMD allreduce and the empty-cluster handling.Not changed here: the GPU CSR kernel (
train_kernel_lloyd_csr_dpc.cpp) still reports the expanded-form sum; it needs a sparse variant of the same pass. scikit-learn-intelex mapsalgorithm="elkan"to this Lloyd kernel, so there is no other GPU path.Related: the original report of this behavior is from the daal4py era (inertia computed with the expanded formula). The 4-point example from that report is the regression test added here.
Validation
kmeans objective function is exact on close points([kmeans][batch], dense methods, float and double): without the fix it fails on the GPU withobjective := 0.0,expected := 4.0013e-08; with the fix it passes. It also checksinferwithcompute_exact_objective_function.kmeans exact objective function with a NaN row: infer with the exact objective on a NaN row returns NaN on CPU and GPU without reading outside the centroids.kmeans gives the same result on the host and the deviceand... the same objective on the host and the device far from the origin(DPC++ builds, dense methods, float and double): train from the same initial centroids on the host and on the device and compare the objective function and the centroids, and the responses for blobs near the origin. Without this change the float64 GPU objective differs from the host's by about 2e-9 relative and the test fails; with it both pass. The device can take one more iteration than the host, because its convergence check still uses the expanded distances; the tests record the counts but do not compare them.bazel test --config=dpc --device=gpu //cpp/oneapi/dal/algo/kmeans:tests: 6 of 6 pass on an Intel GPU.bazel test --config=host //cpp/oneapi/dal/algo/kmeans:tests: 4 of 4 pass.---
<details>
<summary>Checklist:</summary>
Completeness and readability
Testing
Performance
trainand perinferwith the exact objective requested; the cost is that ofcompute_squares, which already runs once per iteration.</details>
🤖 Generated with Claude Code