Repository navigation
Conversation
cakedev0
force-pushed
the
kmeans/lloyd_no_blocking_sync
branch
from
October 10, 2026 10:21
7ba635c to
d615355
Compare
The thread local buffers are now added to the result after the parallel region instead of under a lock inside it, and the chunks are passed as indices instead of memoryview slices (which made Cython acquire the GIL in every thread at the start and end of the parallel region). Both are serial costs that grow with the number of threads. Co-Authored-By: Claude Opus 5.5 <[email protected]>
cakedev0
force-pushed
the
kmeans/lloyd_no_blocking_sync
branch
from
October 10, 2026 10:51
d615355 to
c2d6622
Compare
cakedev0
commented
Oct 12, 2026
…ainst NumPy - Move _reduce_thread_buffers to _k_means_common so that Elkan can use it, zero the result in it, sum the buffers with flat loops, and call it without the GIL. - Raise MemoryError if the arrays of buffer pointers can't be allocated. - Get the thread index with omp_get_thread_num from _openmp_helpers. - Check one Lloyd iteration against a NumPy implementation, so that the test also fails when the reduction is wrong for both dense and sparse. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The sum of the n_threads buffers of n_clusters * n_features values is done by blocks in a prange, with the same team size as the Lloyd region. The buffers of the centers are not allocated when update_centers is False. The test limits BLAS to 1 thread as in KMeans.fit, and uses more samples and clusters so that it still covers several chunks per thread with bigger chunks, and several blocks in the reduction. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Slicing the sparse rows made Cython take the GIL in every thread of the parallel region. _euclidean_sparse_dense now takes start/end indices, which also removes it from Elkan's init_bounds_sparse. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The test now covers float32 and the predict path (no buffers). Co-Authored-By: Claude Opus 5.5 <[email protected]>
cakedev0
added a commit
that referenced
this pull request
Oct 13, 2026
# Conflicts: # sklearn/cluster/_k_means_common.pyx
The thread buffers are allocated once per fit instead of at each iteration. Buffers larger than glibc's mmap threshold (32 MiB) were new mappings at each iteration, and their page faults dominated sparse Lloyd with many threads. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… team Co-Authored-By: Claude Opus 5.5 <[email protected]>
cakedev0
added a commit
to probabl-ai/scikit-learn-benchmarks
that referenced
this pull request
Oct 19, 2026
For cakedev0/scikit-learn#26. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reference Issues/PRs
I found this while looking at why scikit-learn-intelex is much faster than scikit-learn on
KMeansin scikit-learn-benchmarks: on an Intel Xeon 6787P (2 x 86 cores), Lloyd iterations stop scaling at ~16 threads.What does this implement/fix? Explain your changes.
Current implementation has three costs per Lloyd iteration that grow with the number of threads:
n_clusters * n_featuresvalues) to the result under anomp_set_lock. The pairwise distances reductions already use a similar pattern with theparallel_on_Ystrategy.X[start:end], ...). Slicing can raise, so Cython wraps the wholewith nogil, parallel():block inPyGILState_Ensure()/PyGILState_Release(): all the threads take the GIL one after the other at the start and at the end of each iteration.callocs its buffers at each iteration. Above glibc's mmap threshold (32 MiB, e.g. sparse data with many features), they are new mappings each time, and their page faults dominate the iteration. For dense data, this is usually negligible.This PR:
start/endindices instead of slices, in Lloyd and in other places._kmeans_single_lloyd(_alloc_thread_buffers: one row per thread, aligned and padded to cache lines), and passes them to each Lloyd iteration. The reduction zeroes the buffers for the next iteration, but only the cache lines that hold a non-zero value: the pages that a thread never writes to stay unmapped, as after acalloc.The pairwise distances reductions already use this pattern with the
parallel_on_Ystrategy (_argkmin.pyx.tp: heaps allocated in each thread, indexed byomp_get_thread_num(), merged after the parallel region in aprangeover samples without locks).Some notes:
AI usage disclosure
I used AI assistance for:
The main things I trust:
Benchmarks
See probabl-ai/scikit-learn-benchmarks#154: 34 KMeans cases (sift, nytimes, fashion_mnist, road_network and CIFAR-10, plus 20newsgroups and RCV1 as CSR matrices), in float32 and float64, with
init="random"and up to 100 Lloyd iterations, 5 runs per case. Dashboards: intel-xeon, intel-laptop.Speed-up of the Lloyd time (
lloyd_time, main / this PR, medians of the 5 runs, as in the dashboards), over the 34 cases:On the laptop, the gains are on 20newsgroups and RCV1 in float64, where the thread buffers are large. The other cases are within the run-to-run spread, which is 10-50% on this laptop. The 0.78 is RCV1 in float32: its median fit runs 59 iterations instead of 52 (the float32 sums are in a different order), and the time per iteration is the same.