Skip to content

RFC Automatically choose best namespace/device within an estimator #34152

Open

Description

@betatim

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.

This is a RFC related to array API.

The central question is: who knows best when to run a computation on the CPU vs on a GPU/accelerated hardware?

So far we've been assuming that the user will know when to move data from one device to the other. As a result we need something like the MoveTo transformer. There are probably other things that result from this assumption, but I can't think of them right now.

Inspired by using sentence-transformers and realising that it automatically uses my GPU when I run the code on a machine with a GPU, the MPS device of my MacBook and otherwise stays on the CPU. This was a cool experience because I didn't have to go and edit my code. The input to sentence-transformers was always a Numpy array but it knew that if there was a CUDA device it made sense to use it. From talking about this with Olivier I learnt that TabICL also does this (the processing starts on the CPU but it then moves to the GPU).

Roughly at the same time I investigated how long it takes to move different amounts of data from CPU memory to GPU memory (and back). Some results and the script used to measure it https://gist.github.com/betatim/f4128b961525eda49df9b76c7c339388 (ignore the "predicted" column, it isn't that useful). The main thing I took away from this is that even fairly large datasets take about half a second. Combined with my assumption that the work that happens in the estimator takes at least a second or two and sometimes much longer it made me think "if this is true then moving data is 'cheap'". I'm not 100% there isn't a mistake or logic error here. Let me know what you think.

The result from that discussion and the transfer time experiment is this RFC.

The proposal is that an estimator is allowed to move from one namespace/device to another if it thinks that it makes sense to do so. So for example you feed a numpy array to Ridge which then decides to move to pytorch and the GPU to do the computation. This behaviour would be controlled by a global setting (opt-in at the start). Each estimator probably also needs to have some public API to control its behaviour.

It would remove the need for having a MoveTo transformer and free the user from having to think about this/know when best to transition from one namespace/device to another. It would also make it easier to have code for a pipeline that works on a machine with and without a GPU - no need to edit the code.

We already kinda do this for some (parts of some) estimators. Mostly we do it because there is no array API function to do what we want to do, but there is a Numpy one. Internally the estimator moves the data to numpy, performs the computation and then moves the result back (if needed).

We'd have to have some "order of precedence" function that knows how to check if cupy, pytorch, dpnp are installed, if there is an accelerator present that is supported by the installed library and then decided what to do. I think in practice this is not that complicated as most machines will only have one type of accelerator and one of these libraries installed. I think sentence-transformers uses PyTorch and then: CUDA > MPS > CPU (for example here and then used here)

Overall I think this would be a nice UX for users if it doesn't become "too magical".

What do people think?

Activity

  1. thomasjpfan commented on May 29, 2026

    @thomasjpfan
    Member

    What would be the behavior when the data does not fit the GPU? Some options are:

    1. Check if the data fits in GPU memory before choosing it as the "best device".
    2. Try to move the data into the GPU and fallback to CPU. (Adds overhead when we end up running on CPU anyways.
    3. Try to move the data into the GPU and raise an informative error.
  2. betatim commented on Jul 30, 2026

    @betatim
    MemberAuthor
    1. Rely on batching to only ever move a "small amount" of data to the device

    My feeling is that predicting "will this fit" (option 1) is quite hard to get right. In particular because it isn't just about answering the question "will X fit?" but also if there is enough memory for the whole of fit() to succeed. This makes me think we shouldn't investigate this option too much.

    I'm not sure it is feasible to implement batching for all estimators. So I think that option 4 should also be low priority.

    My favourite is probably option 3 - try to run on the GPU and raise an error that explains how to remove the device that was chosen from the list of eligible devices. This feels the most similar to what we do right now: we run fit() and if that runs out of memory an exception is raised.

  3. thomasjpfan commented on Jul 31, 2026

    @thomasjpfan
    Member

    If there is a memory error running on the GPU, what is the API to go back to CPU only? Will there be a device parameter on estimators that can run on different hardware?

  4. betatim commented on Aug 3, 2026

    @betatim
    MemberAuthor

    From the top comment

    This behaviour would be controlled by a global setting (opt-in at the start). Each estimator probably also needs to have some public API to control its behaviour.

    There has to be a way to control this behaviour, but I am not sure if this should be a device= parameter, a method like set_preferred_device or something else. I think at the estimator level you'd most likely want to exclude some devices from the "device resolution" process, so having an API that is ergonomic for that task makes sense. As opposed to one that is geared towards selecting a particular device. For example, I think explicitly setting "run on the GPU!" should be a less frequent task than "exclude all the low memory GPU like devices" (also known as "run on the CPU please").

  5. david-cortes-intel commented on Aug 26, 2026

    @david-cortes-intel
    Contributor

    Some comments here:

    • If the data is float64 and the user expects float64 precision, it might not always be advantageous to offload to a GPU.
    • Even when it comes to float32, if the user has an integrated GPU (esp. older generations), that GPU oftentimes will be slower than the CPU at operations like matrix multiplications.
    • When it comes to libraries for statistical modeling, this kind behavior would be quite unusual. For instance, major GBT libraries don't automatically use GPU, nor do smaller libraries like stumpy for instance.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    • Status
      No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions