Skip to content

Expose get_namespace_and_device and move_estimator_to in public API #34135

Open

Description

@StefanieSenger

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 just stumbled over the error message of the new ValueError added in #33076:

ValueError: Inputs passed to LinearRegression.predict() must use the same namespace and the same device as those passed to fit(). Array namespaces used during fit (sklearn.externals.array_api_compat.numpy) and predict (sklearn.externals.array_api_compat.torch) differ. You can move the estimator to the same namespace and device as X with: 'from sklearn.utils._array_api import move_estimator_to; xp, _, device = get_namespace_and_device(X); estimator = move_estimator_to(estimator, xp, device)'

For instance triggered by:

Details
import sklearn
import numpy as np
import torch
from sklearn.linear_model import LinearRegression

rng = np.random.default_rng(0)
X = rng.normal(size=(10, 5))
y = rng.normal(size=10)

reg = LinearRegression().fit(X, y)
X_xp = torch.asarray(X)
reg.predict(X_xp)

with sklearn.config_context(array_api_dispatch=True):
    reg.predict(X_xp)

The recommended action is to run:

from sklearn.utils._array_api import move_estimator_to
xp, _, device = get_namespace_and_device(X)
estimator = move_estimator_to(estimator, xp, device)

Both methods are also mentioned in https://scikit-learn.org/dev/modules/array_api.html.

I am wondering why move_estimator_to and get_namespace_and_device are not exposed in the public API then. Did we forget to do that or was that a conscious choice? (I tried to find discussion on that but couldn't find any.)

If we expose both methods in the API, users can access the docstrings from the website and import via from sklearn.utils import move_estimator_to, get_namespace_and_device.
Also, we may want to expose get_namespace and get_device, though I don't find that so important.

Activity

  1. StefanieSenger commented on May 28, 2026

    @StefanieSenger
    MemberAuthor

    Clarifying if these two methods were not added to the API on purpose or by accident may be of interest for the 1.9 release.
    CC @betatim @ogrisel @jeremiedbb

  2. ogrisel commented on May 28, 2026

    @ogrisel
    Member

    This is definitely something we need to tackle on our way to #33444.

    I think move_estimator_to (and maybe move_to) should be public.

    I think we could keep sklearn.utils._array_api.get_namespace_and_device private and instead recommend using the array_api_compat.array_namespace and array_api_compat.device which are already public (but would require scikit-learn users to add a dependency or vendor array-api-compat to/in their own project).

    Alternatively, we could expand move_estimator_to to accept being called as move_estimator_to(estimator, X) in addition to move_estimator_to(estimator, xp=xp, device=device) and then we can simplify the recommendation in the exception message and not have to make sklearn.utils._array_api.get_namespace_and_device public.

  3. moved this to Discussion in Array APIon May 28, 2026
  4. StefanieSenger commented on May 28, 2026

    @StefanieSenger
    MemberAuthor

    Yes, that's a good plan (both alternatives).

    And do we want to make move_estimator_to public in 1.9? We mention it in the changelog (doc/whats_new/v1.9.rst), but it doesn't get rendered like API because we did not expose it, see: https://scikit-learn.org/dev/whats_new/v1.10.html (link will change path to https://scikit-learn.org/dev/whats_new/v1.9.html in a few days). Because of that in the error message mentioned above, we give users an private import path.

    I propose to expose move_estimator_to for the release.

  5. betatim commented on May 28, 2026

    @betatim
    Member

    I like the idea of changing to move_estimator_to(estimator, X). That feels like the better API for users.

    The reason these functions live in a private namespace at the moment is that we thought this is a good way to signal that they are part of the experimental nature of array API. For example, it allows us to change the signature of move_estimator_to without deprecation cycles. So my immediate thought is that we should keep them "private" or use a different way of marking them as experimental (use the sklearn.experimental namespace??) until we make array API support generally available.

  6. StefanieSenger commented on May 28, 2026

    @StefanieSenger
    MemberAuthor

    The reason these functions live in a private namespace at the moment is that we thought this is a good way to signal that they are part of the experimental nature of array API.

    Okay, then we're not in a rush for the 1.9 release. That's wonderful. :)

    I also like the move_estimator_to(estimator, X) idea.

  7. ogrisel commented on May 28, 2026

    @ogrisel
    Member

    I think we should keep them private as long as possible and only open a PR to make move_estimator_to public once all the other items of #33444 have been fixed.

    But we can already open a PR to implement support for move_estimator_to(estimator, X) and update the exception message to simplify it without making that function public since people feel that this would result in a net usability improvement.

  8. SahilKumar75 commented on Jun 15, 2026

    @SahilKumar75
  9. StefanieSenger commented on Jun 15, 2026

    @StefanieSenger
    MemberAuthor

    Hi @SahilKumar75, we don't need genAI suggestions here, because these are areas where you need a lot of context knowledge about internal decisions we had discussed before and the directions and next steps that we want to take in Array API. Not sure if your input helps here. Also, @ogrisel had already suggested an action that four people that are longtime concerned with Array API posted a thumps up to.

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
      Discussion

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions