fix(kmeans): compute the CSR block products without MKL sparse mm #4048
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
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.382
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #4048"
general
weight: 0.35
david-cortes-intel
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 withmkl_sparse_?_mminsidestatic_threader_for. Under MKL's TBB threading layer that call still runs in parallel aftermkl_set_num_threads_local(1), which has two effects:tol=0, the iteration count change from run to run;threader_for.PostProcessing<lloydCSR>::computeAssignmentskeeps the products indaal::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 withBlasInst::xomatcopy, outside the parallel region.transposeCentroidsandcsrRowsTimesCentroidslive in the newkmeans_csr_helper.h, shared by Lloyd and init. The k-means|| update was the last production caller ofSpBlasInst::xxcsrmm.The products are now stored row-major,
blockSize x nClusters;mkl_sparse_?_mmwrote them column-major. That is why the nearest-centroid search inaddNTToTaskThreadedCSR(kmeans_lloyd_impl.i) readsproducts[j]for rowiinstead ofx_clusters[i + j * blockSize]. In k-means|| the CSR block helper'sgetGemmResultreads 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_tlsindexed by the range index ofstatic_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.GetRowsCSRnow 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...]andtest_kmeans_relocated_clustersdeselections. With this change those tests andtest_float_precisionpass 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) runsTaskParallelPlusUpdateDist::updateMinDistonce 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 ifgetGemmResultreads them column-major. It passes on main as well, see above.Performance (sklearnex
KMeansfit 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
syrkdin #4055. ATODOatcsrRowsTimesCentroidsmarks the return tomkl_sparse_?_mmonce MKL keeps it sequential undermkl_set_num_threads_local(1).---
<details>
<summary>Checklist:</summary>
Completeness and readability
Testing
Performance
</details>
🤖 Generated with Claude Code