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.
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
thomasjpfan commentedon May 29, 2026
What would be the behavior when the data does not fit the GPU? Some options are:
betatim commentedon Jul 30, 2026
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
Xfit?" but also if there is enough memory for the whole offit()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.thomasjpfan commentedon Jul 31, 2026
If there is a memory error running on the GPU, what is the API to go back to CPU only? Will there be a
deviceparameter on estimators that can run on different hardware?betatim commentedon Aug 3, 2026
From the top comment
There has to be a way to control this behaviour, but I am not sure if this should be a
device=parameter, a method likeset_preferred_deviceor 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").david-cortes-intel commentedon Aug 26, 2026
Some comments here: