← Back to Pull requests

fix(kmeans): compute the CSR block products without MKL sparse mm #4048

Closed
unknown closed this pull request without merging on Nov 30, 2026 14:36 UTC
View on GitHub
unknown unknown commented on Nov 20, 2026 06:37 UTC
Author

Description

The CSR Lloyd training loop, the final CSR assignment and the k-means|| CSR candidate update (TaskParallelPlusUpdateDist) computed the row-by-centroid inner products with mkl_sparse_?_mm inside static_threader_for. Under MKL's TBB threading layer that call still runs in parallel after mkl_set_num_threads_local(1), which has two effects:

  • its sums differ in the last bits between runs, so near-tie assignments and, with tol=0, the iteration count change from run to run;

  • a thread that waits inside the call picks up another block of the outer threader_for. PostProcessing<lloydCSR>::computeAssignments keeps the products in daal::tls, so that block overwrites the buffer and the returned labels are wrong for whole blocks.
  • All three call sites now use csrRowsTimesCentroids. It sums each product over the stored values of the row in order. The centroids (for k-means||, the new candidates) are transposed once per call with BlasInst::xomatcopy, outside the parallel region. transposeCentroids and csrRowsTimesCentroids live in the new kmeans_csr_helper.h, shared by Lloyd and init. The k-means|| update was the last production caller of SpBlasInst::xxcsrmm.

    The products are now stored row-major, blockSize x nClusters; mkl_sparse_?_mm wrote them column-major. That is why the nearest-centroid search in addNTToTaskThreadedCSR (kmeans_lloyd_impl.i) reads products[j] for row i instead of x_clusters[i + j * blockSize]. In k-means|| the CSR block helper's getGemmResult reads the products row-major for the same reason. Only the indexing changed. The dense paths are unchanged.

    In k-means|| the products buffer comes from static_tls indexed by the range index of static_threader_for, so a stolen block does not overwrite it, and no failure was seen there on main. It is moved to the same helper so CSR K-means has one product implementation and no MKL sparse call inside the threading layer.

    GetRowsCSR now initializes its members in declaration order: the new k-means|| test instantiates it and builds with -Werror=reorder-ctor.

    This unblocks uxlfoundation/scikit-learn-intelex#3527, which removes the test_kmeans_elkan_results[...sparse...] and test_kmeans_relocated_clusters deselections. With this change those tests and test_float_precision pass in every run at 2, 4, 8, 16 and 224 CPUs. On main the sparse cases fail in most runs at 4 and 8 CPUs.

    Test. KMeans CSR responses are the nearest centroids in every run (cpp/oneapi/dal/algo/kmeans/test/batch.cpp) trains twice for 5 iterations on the same 5000 x 4 CSR data. It checks that every response is the nearest centroid and that centroids, responses, objective and iteration count match between the runs. On main it fails in 1-6 of 10 runs at 4 CPUs, 1-10 of 10 at 8 and 7-8 of 10 at 224 (the rate varies between rounds); with this change it passes 20 of 20 at 4, 8, 32 and 224 CPUs.

    kmeans|| CSR update assigns every row to its nearest candidate (cpp/oneapi/dal/algo/kmeans_init/test/internal.cpp) runs TaskParallelPlusUpdateDist::updateMinDist once on 2000 x 4 CSR data and checks every nearest candidate. It covers the row-major products in the k-means|| CSR path, which oneAPI does not expose; it fails if getGemmResult reads them column-major. It passes on main as well, see above.

    Performance (sklearnex KMeans fit on scipy CSR, n_init=1, max_iter=20, Intel CPU). Speedup over main, medians of three interleaved rounds of 15 fits each:

    | rows x cols, density, k | 16 cores | 56 cores |
    |---|---|---|
    | 200k x 1000, 1%, 10 | 1.24x | 1.14x |
    | 200k x 1000, 1%, 100 | 2.50x | 1.62x |
    | 100k x 20000, 0.2%, 50 | 1.46x | 1.00x |
    | 5k x 10, 50%, 5 | 1.28x | 1.33x |

    The table was measured before the k-means|| change and covers the Lloyd paths. The covariance CSR path moves to a sequential syrkd in #4055. A TODO at csrRowsTimesCentroids marks the return to mkl_sparse_?_mm once MKL keeps it sequential under mkl_set_num_threads_local(1).

    ---

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

    Completeness and readability

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

  • Git commit message contains an appropriate signed-off-by string.

  • The unit tests pass successfully.

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

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

  • I have added a respective unit test.
  • Performance

  • I have measured performance for affected algorithms using scikit-learn_bench and provided at least a summary table with measured data, if performance change is expected.
  • </details>

    🤖 Generated with Claude Code

    Diff: 595 lines (+446 / -149) across 7 files Reviewer: david-cortes-intel Review duration: 14879m
    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 · 595 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.382
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #4048"
    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% (MEDIUM) · Higher = larger blast radius
    50%
    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%