Skip to content

POC Add array API support to KMeans(init="k-means++") and KMeans(init="random") - #35382

Draft
virchan wants to merge 7 commits into
scikit-learn:mainfrom
virchan:issues/35238-kmeans-array-api
Draft

virchan wants to merge 7 commits into
scikit-learn:mainfrom
virchan:issues/35238-kmeans-array-api

Conversation

@virchan

@virchan virchan commented Nov 11, 2026

Copy link
Copy Markdown
Member

Reference Issues/PRs

Towards #35238, and a follow-up to the array API meeting on November 9, 2026

What does this implement/fix? Explain your changes.

This PR adds array API support and tests to KMeans(init="k-means++") and KMeans(init="random").

This is a proof of concept (PoC) intended to support the ongoing benchmarking efforts: https://github.com/probabl-ai/scikit-learn-benchmarks.

AI usage disclosure

I used AI assistance for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Research and understanding

Any other comments?

N / A

@virchan virchan changed the title POC array API support for KMeans(init="k-means++") and KMeans(init="random") POC Add array API support to KMeans(init="k-means++") and KMeans(init="random") Nov 11, 2026
@github-actions github-actions Bot removed the CUDA CI label Nov 11, 2026
@betatim

betatim commented Nov 12, 2026

Copy link
Copy Markdown
Member

Just to make 100% sure: do you know about ogrisel/cursor-sandbox#17? I haven't had time to dig through it but I think we'd be able to find a lot of good stuff in that conversation and in Olivier's head (as always :D :D)

@virchan

virchan commented Nov 13, 2026

Copy link
Copy Markdown
Member Author

Just to make 100% sure: do you know about ogrisel/cursor-sandbox#17? I haven't had time to dig through it but I think we'd be able to find a lot of good stuff in that conversation and in Olivier's head (as always :D :D)

I was aware of the sandbox before starting this POC, but I haven't gone through it in detail yet. I'll read it and come back with how this draft compares, and whether it's still useful alongside Olivier's work.

@ogrisel

ogrisel commented Nov 13, 2026

Copy link
Copy Markdown
Member

Look at the "What the branch adds" section of the PR description in particular.

@ogrisel

ogrisel commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

In particular, my experiments used tiling which can significantly reduce memory usage. Memory efficiency is important to optimize, especially when using a GPU. Furthermore tiling can lead up to 3x speed-ups compared to the code in the current PR according to quick experiments run by my agent.

My sandbox also includes a numerically stable fallback path in case of potential catastrophic cancellation of the GEMM-based euclidean distance computation. This is especially important when working on float32 data, especially if the data is not centered.

Finally, after relocation, Cython's _average_centers moves a center that is still empty onto the heaviest remaining center while _update_centers_xp keeps the previous center. I think we should ensure that the Cython code path and the array API codepath stay consistent in that respect.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants