Skip to content

RFC a plan to move array API support out of experimental #33444

Open

Description

@ogrisel

Now that we have added array API support to several important models and tools (including classifiers, regressors, transformers, meta estimators, pipelines, evaluation and model selection tools...), it might be important to discuss what are our plans and conditions to remove the "experimental" marker for that set of features.

To me, moving out of experimental means the following things in particular:

  • a. Setting array_api_dispatch=True as the default value in the config (or maybe even deprecate this configuration parameter to always have it enabled and simplify the code as a result);
  • b. Enforcing backward compatibility guarantees with array API related behavior change (the requirement for 2 release deprecation cycle and the introduction of FutureWarning).

For a., I think we would need to:

  • ensure that all estimators and public functions/classes that have not yet been updated to support array API inputs do not raise confusing error messages on non-(NumPy + CPU) inputs when array API dispatch is enabled. I have no idea if this is the case or not. We should probably extend the common test to check that.

Furthermore, for a. to make sense we might need to wait for SciPy to also enable array API support without having the user to manually set SCIPY_ARRAY_API=1, and therefore:

I think that for point b. we at least need to implement the following:

Then we need to make sure we can run the full test suite with array API dispatch enabled. This would entails either:

or alternatively, we could agree on:

Just before moving out of experimental:

The purpose of this issue is to agree with a shared plan and maybe milestone this as a goal for 1.9?

Activity

  1. moved this to Discussion in Array APIon Mar 3, 2026
  2. github-actions commented on Mar 3, 2026

    @github-actions

    Thanks for the work you've done so far. The goal of this comment is to set expectations.

    Deciding on new features or substantial changes is a lengthy process. It frequently happens that no maintainer is available to take on this task right now.

    Please do not create a Pull Request before a decision has been made regarding the proposed work. Making this decision can often take a significant amount of time and effort.

  3. StefanieSenger commented on Mar 3, 2026

    @StefanieSenger
    Member

    Furthermore, for a. to make sense we might need to wait for SciPy to also enable array API support without having the user to manually set SCIPY_ARRAY_API=1, and therefore coordinate with upstream on their own plans to move out of experimental.

    Why do we need to wait for Scipy to enable array api support by default, @ogrisel? Can we not set this globally for users of scikit-learn?

  4. ogrisel commented on Mar 3, 2026

    @ogrisel
    MemberAuthor

    I would rather not have a line such as from sklearn import ... silently change the behavior of scipy functions. I think it's preferable to users to always set SCIPY_ARRAY_API=1 explicitly.

    This kind of import-time side effects would be a hell to debug for third party projects that use scipy but also import sklearn in their code base.

  5. betatim commented on Mar 3, 2026

    @betatim
    Member

    For a., I think we would need to ensure that all estimators and public functions/classes that have not yet been updated to support array API inputs do not raise confusing error messages on non-(NumPy + CPU) inputs when array API dispatch is enabled. I have no idea if this is the case or not. We should probably extend the common test to check that.

    This is a good point. We probably need a dedicated exception that tells users that the estimator they are trying to use does not yet support array API.

    Having one or two workflows that use array API is something I'd add to the list of requirements. I think building these examples will surface bugs that we want to fix before inviting people to use array API support.

    #33076 is something we need as well I think

    We should take a moment to think of things we'd like to fix in the API/user experience before we make array API non experimental because API changes will become quite costly with our backwards compat policy.

    Are we happy with the current policy that array API support requires the latest version of our dependencies? It would be different from the rest of scikit-learn, which isn't great. However, I think it is unrealistic to support all the versions that "normal" scikit-learn supports. Even supporting "medium old" versions of scipy/pytorch/numpy would be tricky.

  6. ogrisel commented on Mar 3, 2026

    @ogrisel
    MemberAuthor
  7. ogrisel commented on Mar 3, 2026

    @ogrisel
    MemberAuthor

    However, I think it is unrealistic to support all the versions that "normal" scikit-learn supports. Even supporting "medium old" versions of scipy/pytorch/numpy would be tricky.

    I agree. I think we can document an exception explicitly for this.

  8. lucyleeow commented on Mar 4, 2026

    @lucyleeow
    Member

    Maybe something else we want to reach consensus on is what happens to "array-like" inputs (e.g., PyTorch CPU array) that are currently being converted to NumPy arrays via np.asarray/np.array. See conversation thread starting here: #32676 (comment)

    Setting dispatch to be enabled by default will change behaviour here - though I don't fully comprehend the implications of this for users.

    Also do we want to have any automatic documentation of estimators/metrics that support array API (e.g., scipy's xp_capabilities decorator scipy/scipy#24130 - I think in our last meeting we didn't like the idea of adding a decorator), or ability to add specific implementation notes (e.g., our "(see Note on device support for float64)"), see: #33366 for details.

    Note: I edited the original comment to add #32873 and #33076 just for ease of reading.

  9. betatim commented on Mar 4, 2026

    @betatim
    Member

    Maybe something else we want to reach consensus on is what happens to "array-like" inputs (e.g., PyTorch CPU array) that are currently being converted to NumPy arrays via np.asarray/np.array. See conversation thread starting here: #32676 (comment)

    Agreed. I think step one here is probably deciding if what currently happens is officially supported or accidental. The implication is that if it is supported we need to maintain the current behaviour (maybe with a deprecation to change it).

    Also do we want to have any automatic documentation of estimators/metrics that support array API (e.g., scipy's xp_capabilities decorator scipy/scipy#24130 - I think in our last meeting we didn't like the idea of adding a decorator), or ability to add specific implementation notes (e.g., our "(see Note on device support for float64)"), see: #33366 for details.

    I'm not a fan of delegating the responsibility of documentation to some code trick. IMHO when we add array API support to an estimator we should take the time to write the documentation for it. In particular because good documentation is "free form" for each estimator, e.g. because of limitations it has. In the long term where array API support is "just a normal thing and no one remembers a time before it" we should use the "Notes" section of estimators to note things like "only supports shizzle when dazzle is true".

    I'd be interested in a unit test that checks that every estimator that has the array API tag also appears in the right places of our documentation. To make sure we don't forget/make it obvious to contributors that this needs doing.


    We could also discuss using the tags system to display things on the page of each estimator. Maybe a bigger discussion than array API though.

  10. ogrisel commented on Mar 5, 2026

    @ogrisel
    MemberAuthor

    I'd be interested in a unit test that checks that every estimator that has the array API tag also appears in the right places of our documentation. To make sure we don't forget/make it obvious to contributors that this needs doing.

    +1

  11. lorentzenchr commented on Mar 5, 2026

    @lorentzenchr
    Member

    Maybe something else we want to reach consensus on is what happens to "array-like" inputs (e.g., PyTorch CPU array) that are currently being converted to NumPy arrays via np.asarray/np.array. See conversation thread starting here: #32676 (comment)

    Agreed. I think step one here is probably deciding if what currently happens is officially supported or accidental.

    I would like to revert the "making official" of pytorch being array-like. It is not tested and and works rather accidentally.

    Furthermore, for a. to make sense we might need to wait for SciPy to also enable array API support without having the user to manually set SCIPY_ARRAY_API=1, and therefore coordinate with upstream on their own plans to move out of experimental.

    IMO, we have to wait for scipy. Asking a user to set an environment variable might not be possible to her:him.

  12. 4 remaining items

  13. ogrisel commented on Apr 2, 2026

    @ogrisel
    MemberAuthor

    We could also introduce sklearn.set_config(array_api_dispatch="auto") that would enable array API dispatch automatically if the SCIPY_ARRAY_API="1" env var has already been explicitly set by the user (or if the installed version of SciPy enables array API dispatch by default itself, which not the case at the moment).

  14. betatim commented on Apr 9, 2026

    @betatim
    Member

    Is a PyTorch CPU tensor array-like?

    The conclusion at the monthly meeting was that "yes it is array-like", because np.asarray() does the right thing on it. This has been used as the definition of "array-like" for many releases.

    For me it is ok to conclude the issue based on this. It does mean when we turn on array API support by default there will be a behaviour change for users of PyTorch CPU tensors. The fitted attributes will stop being numpy arrays and start being torch tensors.

    import os
    os.environ["SCIPY_ARRAY_API"] = "1"
    import torch
    from sklearn import config_context
    from sklearn.linear_model import Ridge
    
    X = torch.randn(100, 5, dtype=torch.float64)
    y = torch.randn(100, dtype=torch.float64)
    
    # Set to True/False to see the change in types
    with config_context(array_api_dispatch=True):
        model = Ridge(alpha=1.0, solver="svd")
        model.fit(X, y)
        y_pred = model.predict(X)
    
    print(type(y_pred), type(model.coef_))

    @lorentzenchr and @StefanieSenger

  15. betatim commented on Apr 9, 2026

    @betatim
    Member

    An interesting thing I observed while making the above snippet is that an estimator like LinearRegression, which has no array API support, errors with a obscure exception (ValueError: data type <class 'numpy.object_'> not inexact) when you turn on array API support and pass it a torch tensor. With array API support off it "just works":

    import torch
    from sklearn import config_context
    from sklearn.linear_model import LinearRegression
    
    X = torch.randn(100, 5, dtype=torch.float64)
    y = torch.randn(100, dtype=torch.float64)
    
    with config_context(array_api_dispatch=True):
        est = LinearRegression()
        est.fit(X, y)
        pred = est.predict(X)

    Maybe we should ponder what should happen in this case. Should it be an error to turn on array API support and pass a torch tensor to an estimator that doesn't have array API support? Should it work, because with array_api_dispatch=False there is no error?

    My immediate thought is that it should be an error. Because that is consistent with the fact that array API on changes things (like the type of fitted attributes). An alternative would be requiring that the array_api_dispatch=False behaviour would be preserved when you enable array API for estimators that do not support array API. This seems like it would create a lot more confusion.

  16. ogrisel commented on Apr 13, 2026

    @ogrisel
    MemberAuthor

    Should it be an error to turn on array API support and pass a torch tensor to an estimator that doesn't have array API support?

    For the sake of backward compat, array_api_dispatch="auto" could mean that:

    • estimators with explicit support for array API would;
    • estimators without explicit support would continue behaving as today and call np.asarray internally.

    Otherwise, we would never have a way to enable array API dispatch for estimators that support it by default without either:

    • waiting for all estimators to explicitly support array API (which seem unlikely to ever happen for Cython nested for loop heavy estimators such as tree-based models),
    • break backward compat.

    I think what is important is to provide an easy way for users to tell which part of their code is expected to dispatch to the input namespaces and which part is expected to convert to numpy array silently.

  17. betatim commented on Apr 14, 2026

    @betatim
    Member

    I think a few words got lost in your first bullet point. I think it should be:

    • estimators with explicit support for array API would dispatch;
    • estimators without explicit support would continue behaving as today and call np.asarray internally.

    This is the pragmatic way forward. It will still need a bit of work I think, for example (assuming for the sake of this argument that it doesn't get array API support) LinearRegression would need adjusting so that it continues to work with array API dispatch enabled and a torch tensor as input.

    The fact that we have to touch LinearRegression makes me wonder if we could also raise an exception. But it would break existing code which I think is a reason to not raise an exception.

  18. lorentzenchr commented on Apr 14, 2026

    @lorentzenchr
    Member

    Now we are discussing 2 separate issues:

    1. Move config_context(array_api_dispatch=True) out of experimental
      Note that the only mention of "experimental" is in the user guide, https://scikit-learn.org/1.8/modules/array_api.html. It is not in sklearn.experimental. So the only thing to do is to announce it.
      • I am in favor of an error when a torch tensor is passed to a not-yet-array-api-compliant estimator and dispatch is enabled.
      • I don't like the idea of array_api_dispatch="auto" because it becomes very unclear when what happens (but it is a valid idea).
    2. Declaring torch tensors as array-like
      Even if np.asarray(torch_tensor) is the definition of array-like, we should test what we guarantee. If we say we support torch tensors as array-like, we should have tests for that. Until we have it, I would like to remove that sentence from the glossary (it was included in the glossary in version 1.8, in my opinion a bit prematurely and, because it is an API guarantee, a bit without asking other devs. This it not to blame anybody, but to prevent it from happening again in the future.)
  19. lucyleeow commented on Apr 15, 2026

    @lucyleeow
    Member

    (it was included in the glossary in version 1.8, in my opinion a bit prematurely

    If we say we support torch tensors as array-like, we should have tests for that

    I think this is fair, my intention was to document this 'expected' behaviour that only a subset of people seemed to know about, but I agree that it is premature to promise this when it is not explicitly tested.

    What if we amend to simply state that array-like means any array that np.asarray (/np.array) works on? This seems more accurate and it would be better to test this more general case (since e.g., JAX arrays would also work) .

    For estimators we seem to have _NotAnArray and check_estimators_data_not_an_array and check_transformer_data_not_an_array.

    We 'inadvertently' test this for metrics we check lists in test_format_invariance_with_1d_vectors and test_multilabel_representation_invariance - more specific tests would be desirable, maybe with _NotAnArray?

  20. lorentzenchr commented on Apr 15, 2026

    @lorentzenchr
    Member

    I think we should have a common test with _NotAnArray (or _ArrayLikeButNotNumpy) for all estimators.

    For estimators we seem to have _NotAnArray and check_estimators_data_not_an_array and check_transformer_data_not_an_array.

    In that case everything is fine regarding the array-like support of torch tensors. I like @lucyleeow's suggestion of adding that array-like means np.asarray(x) works.

    Historic note: this check has its origin in 2014 (then named check_estimators_not_an_array), 7768a30 by @GaelVaroquaux !

  21. ogrisel commented on Apr 15, 2026

    @ogrisel
    MemberAuthor

    In that case everything is fine regarding the array-like support of torch tensors. I like @lucyleeow's suggestion of adding that array-like means np.asarray(x) works.

    This is what we have always said! For instance in 1.1:

    https://scikit-learn.org/1.1/glossary.html#term-array-like

    The most common data format for input to Scikit-learn estimators and functions, array-like is any type object for which [numpy.asarray](https://numpy.org/doc/stable/reference/generated/numpy.asarray.html#numpy.asarray) will produce an array of appropriate shape (usually 1 or 2-dimensional) of appropriate dtype (usually numeric).
    
     This includes:
        a numpy array
    
        a list of numbers
    
        a list of length-k lists of numbers for some fixed length k
    
        a [pandas.DataFrame](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html#pandas.DataFrame) with all columns numeric
    
        a numeric [pandas.Series](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.html#pandas.Series)
    
    It excludes:
    
        a [sparse matrix](https://scikit-learn.org/1.1/glossary.html#term-sparse-matrix)
    
        an iterator
    
        a generator
    
    

    and therefore, a torch tensor allocated on the CPU is a de-facto "array-like" but a torch tensor allocated on a non-CPU device is not. This is written in our current glossary but the phrasing is confusing and could be improved.

  22. betatim commented on Apr 15, 2026

    @betatim
    Member

    I would welcome a separate issue regarding the torch-is-array-like-or-not topic.

  23. lorentzenchr commented on Apr 15, 2026

    @lorentzenchr
    Member

    I would welcome a separate issue regarding the torch-is-array-like-or-not topic.

    I would have. Now there is nothing more to discuss 😄

  24. lorentzenchr commented on Apr 15, 2026

    @lorentzenchr
    Member

    Back to the OP.

    To me, moving out of experimental means the following things in particular:

    • a. Setting array_api_dispatch=True as the default value in the config (or maybe even deprecate this configuration parameter to always have it enabled and simplify the code as a result);
    • b. Enforcing backward compatibility guarantees with array API related behavior change (the requirement for 2 release deprecation cycle and the introduction of FutureWarning).

    IMO:

    • Moving it out of experimental just means we announce it and then keep backwards compat in the same way as for the rest, i.e. 2-releases deprecations.
    • A 2nd issue is making it the default, i.e. array_api_dispatch=True as default.
      A prerequisite for this is:
      • No environment variable needs to be set, SCIPY_ARRAY_API=1 is not necessary.
      • Enough of our estimators and functions support it.
  25. GaelVaroquaux commented on Apr 15, 2026

    @GaelVaroquaux
    Member
  26. betatim commented on May 18, 2026

    @betatim
    Member

    scipy/scipy#21529 (comment) is a good issue to track to see what SciPy is doing with respect to making array API support default to being switched on.

  27. ogrisel commented on Jun 22, 2026

    @ogrisel
    MemberAuthor

    The plan to remove array API feature flag in scipy is discussed here: https://discuss.scientific-python.org/t/making-array-types-support-public-scipy-2-0/2355 (for scipy 2.0 which might be the next scipy release).

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    • Status
      Discussion

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions