← Back to Pull requests

fix(kmeans): compute the GPU objective function from the final labels and centroids #3983

Closed
unknown closed this pull request without merging on Nov 18, 2026 09:48 UTC
View on GitHub
unknown unknown commented on Nov 03, 2026 16:12 UTC
Author

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:

  • four points 2e-4 away from -1 and +1 gave an objective of 0.0 instead of 4.0e-8;

  • float32 data with a mean offset of 1e4 gave objectives unrelated to the real ones (for example 8448 reported against 29091 real), so n_init in 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_distances sums 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 maps algorithm="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

  • New test kmeans objective function is exact on close points ([kmeans][batch], dense methods, float and double): without the fix it fails on the GPU with objective := 0.0, expected := 4.0013e-08; with the fix it passes. It also checks infer with compute_exact_objective_function.

  • New test 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.

  • New tests kmeans gives the same result on the host and the device and ... 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.

  • clang-format and pre-commit pass on the changed files.
  • ---

    <details>
    <summary>Checklist:</summary>

    Completeness and readability

  • I have commented my code, particularly in hard-to-understand areas.

  • I have updated the documentation to reflect the changes or created a separate PR with updates and provided its number in the description, if necessary: no documentation change; the objective function was always documented as the sum of squared distances.

  • Git commit message contains an appropriate signed-off-by string _(see CONTRIBUTING.md for details)_.

  • I have resolved any merge conflicts that might occur with the base branch.
  • Testing

  • I have run it locally and tested the changes extensively.

  • All CI jobs are green or I have provided justification why they aren't: draft, CI pending.
  • Performance

  • I have provided justification why performance and/or quality metrics have changed or why changes are not expected: one extra pass over the data per train and per infer with the exact objective requested; the cost is that of compute_squares, which already runs once per iteration.
  • </details>

    🤖 Generated with Claude Code

    Diff: 555 lines (+416 / -139) across 6 files Reviewer: Vika-F Review duration: 21215m
    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
    0.48
    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.378
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #3983"
    general weight: 0.35
    Reviewers
    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: 48.1% (MEDIUM) · Higher = larger blast radius
    48.1%
    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% · Safe to merge
    19%