Repository navigation
Description
Warning
This issue is not yet ready for a PR. If you are interested in contributing to scikit-learn, please have a look at our contributing guidelines, and in particular the sections for new contributors and the "Needs triage" label.
I prototyped some array API support for KMeans with the help of a coding agent. I did that by iteratively running experiments and logging results in an HTML journal maintained in an branch of an external repo that I dedicate to de-novo experiments:
The descriptions of the repo has an up to date detailed summary of what was explored. There are even more details with experimentation results and plots in the agent's managed journal but the journal entries are iterative and therefore harder to interpret than the high level summary in the linked PR's description.
Main results
- on a single NVIDIA L4 GPU, a generic array API implementation of kmeans++ and Lloyd iterations can be competitive with cuML and both can be much faster (up to 7x) than the best CPU implementation on that AWS host;
- the same array API code on CPU is not competitive with our existing Cython implementation and even less so when taking optimizations proposed by @cakedev0 (in particular PRF Speed up k-means++ initialization #35140) into account;
- I also explored Torch- and CuPy-specific optimizations on top of the generic array API code path: those can improve performance a bit for some cases (large numbers of clusters) with moderate added complexity.
My take
- I think I would be in favor of expanding our KMeans class to include a array API codepath for non-NumPy inputs, in particular to leverage the capabilities non-CPU devices.
- I don't think it's worth bothering with the Torch and CuPy specific codepaths in a first PR. Making a decision about backend specific array API code branches should be discussed in follow-up issues and PRs if people feel the need for them.
- I think we should keep the Cython codepath for the CPU case. It means that we will have a bit of code duplication to maintain but I think it's ok. It's similar to what we have for the Cython/array API optimized loss module used by linear models with array API support.
- I observed that CuPy is a bit slower than PyTorch CUDA (1.1x to 2.x slower depending on the data and number of clusters).
Limitations
I have not experimented with the multiple GPU setting. I think the "scalable-kmeans++" init of cuML would make it harder for a simple array API implementation to be competitive in that more advanced setting.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsTodo
Activity
ogrisel commentedon Oct 26, 2026
@parthrana-27 Contribution are not welcomed at this stage: as said in the description, this is request for feedback about the tradeoff between add maintenance complexity / improvement performance, mostly from other project maintainers but also for heavy users of
KMeansat scale.Once maintainers agree that we want to do this, I planned to ask my agent to extract the relevant part from my experiment repo as I am already familiar with it.
ogrisel commentedon Oct 28, 2026
For information, I also tried PyTorch XPU on Intel GPU on a Ultra X7 laptop but on this host using the CPU with @cakedev0's PRs is still competitive. I will extend with dpnp and push the consolidated results on the experiment journal of the linked sandbox repo.