Repository navigation
Description
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=Trueas 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:
- coordinate with upstream on their own plans to move out of experimental.
- SciPy has an open discussion about this here: https://discuss.scientific-python.org/t/making-array-types-support-public-scipy-2-0/2355
I think that for point b. we at least need to implement the following:
-
finalize TST Add common test for mixed array API inputs for metrics #32755 to ensure consistency in mixed namespace inputs for metric functions;
-
at least the common testing infrastructure for Automatically move
y(andsample_weight) to the same device and namespace asX#28668 (to make sure that we agree on the semantics) and possibly also upgrade all our existing estimators to ensure that they pass this new common test. -
Raise if fit and predict use different array API namespaces or devices (continued) #33076
-
TST Add array API scorer and cross_validate integration tests #34824
-
Example on how to use array API to accelerate a complex pipeline on a GPU #35420
Then we need to make sure we can run the full test suite with array API dispatch enabled. This would entails either:
- Make
validate_dataconvert inputs whenarray_api_dispatch=True#34324 (but this sounds complex and ugly to maintain)
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?
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDiscussion
Activity
github-actions commentedon Mar 3, 2026
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.
StefanieSenger commentedon Mar 3, 2026
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?
ogrisel commentedon Mar 3, 2026
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 setSCIPY_ARRAY_API=1explicitly.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.
betatim commentedon Mar 3, 2026
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.
ogrisel commentedon Mar 3, 2026
And also:
ogrisel commentedon Mar 3, 2026
I agree. I think we can document an exception explicitly for this.
lucyleeow commentedon Mar 4, 2026
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_capabilitiesdecorator 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.
betatim commentedon Mar 4, 2026
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).
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.
ogrisel commentedon Mar 5, 2026
+1
lorentzenchr commentedon Mar 5, 2026
I would like to revert the "making official" of pytorch being array-like. It is not tested and and works rather accidentally.
IMO, we have to wait for scipy. Asking a user to set an environment variable might not be possible to her:him.
4 remaining items
ogrisel commentedon Apr 2, 2026
We could also introduce
sklearn.set_config(array_api_dispatch="auto")that would enable array API dispatch automatically if theSCIPY_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).betatim commentedon Apr 9, 2026
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.
@lorentzenchr and @StefanieSenger
betatim commentedon Apr 9, 2026
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":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=Falsethere 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=Falsebehaviour 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.ogrisel commentedon Apr 13, 2026
For the sake of backward compat,
array_api_dispatch="auto"could mean that:np.asarrayinternally.Otherwise, we would never have a way to enable array API dispatch for estimators that support it by default without either:
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.
betatim commentedon Apr 14, 2026
I think a few words got lost in your first bullet point. I think it should be:
np.asarrayinternally.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)
LinearRegressionwould 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
LinearRegressionmakes 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.lorentzenchr commentedon Apr 14, 2026
Now we are discussing 2 separate issues:
config_context(array_api_dispatch=True)out of experimentalNote 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.array_api_dispatch="auto"because it becomes very unclear when what happens (but it is a valid idea).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.)lucyleeow commentedon Apr 15, 2026
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
_NotAnArrayandcheck_estimators_data_not_an_arrayandcheck_transformer_data_not_an_array.We 'inadvertently' test this for metrics we check lists in
test_format_invariance_with_1d_vectorsandtest_multilabel_representation_invariance- more specific tests would be desirable, maybe with_NotAnArray?lorentzenchr commentedon Apr 15, 2026
I think we should have a common test with_NotAnArray(or_ArrayLikeButNotNumpy) for all estimators.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 !ogrisel commentedon Apr 15, 2026
This is what we have always said! For instance in 1.1:
https://scikit-learn.org/1.1/glossary.html#term-array-like
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.
betatim commentedon Apr 15, 2026
I would welcome a separate issue regarding the torch-is-array-like-or-not topic.
lorentzenchr commentedon Apr 15, 2026
I would have. Now there is nothing more to discuss 😄
lorentzenchr commentedon Apr 15, 2026
Back to the OP.
IMO:
array_api_dispatch=Trueas default.A prerequisite for this is:
SCIPY_ARRAY_API=1is not necessary.GaelVaroquaux commentedon Apr 15, 2026
betatim commentedon May 18, 2026
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.
ogrisel commentedon Jun 22, 2026
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).