Skip to content

Make validate_data convert inputs when array_api_dispatch=True - #34324

Draft
betatim wants to merge 6 commits into
scikit-learn:mainfrom
betatim:array-api-handling-default-on
Draft

betatim wants to merge 6 commits into
scikit-learn:mainfrom
betatim:array-api-handling-default-on

Conversation

@betatim

@betatim betatim commented Jun 17, 2026

Copy link
Copy Markdown
Member

When array API is enabled some inputs need to be converted to Numpy arrays. This is to make sure that code that works today continues to work with array API enabled.

Right now (array API dispatch turned off, current default) you can pass a torch CPU array to PCA for all solvers. On main, when you enable array API dispatching you will get an error for the same input. This is not something we want.

I tried to come up with a solution that will detect if the input would have worked with dispatching turned off and if yes converts it to a numpy array. Then at the end of fit it converts all fitted attributes to the input namespace, to be consistent with how things work when you pick a solver that is supported in the array API setting.

Putting the conversion inside validate_data means that estimators that are not aware of the array API at all (either not yet converted or others that will never be converted) will also work when dispatching is enabled.

For example, the following code works with array API dispatch disabled, but fails when you enable it (on main):

import torch, sklearn
from sklearn.datasets import make_classification, make_regression
from sklearn.decomposition import PCA
from sklearn.svm import SVC

# Toggle array API here
sklearn.set_config(array_api_dispatch=False)

X_np, y_np = make_classification(
    n_samples=40, n_features=8, n_informative=5, n_redundant=0,
    n_classes=3, n_clusters_per_class=1, random_state=0,
)
X = torch.asarray(X_np)
y = y_np.tolist()
est = SVC()
est.fit(X, y)

This fails because we pass a dtype object to torch.asarray that isn't a torch dtype.

Another example is using PCA(svd_solver='arpack'):

est = PCA(svd_solver='arpack')
est.fit(X, y)

This works with dispatch turned off, but fails with it turned on. The reason it fails is that we use "is array API?" as a guard to decide which solvers are supported.

This PR fixes both cases by making validate_data convert inputs to Numpy, when needed. The behaviour is gated on the array API flag of an estimator and if the input array is on the CPU. An estimator's array API support tag is computed based on the hyper-parameters, that is how we get cases like PCA which sometimes supports array API and sometimes doesn't. When an estimator does not support the array API we check if the input array can be consumed by Numpy, and if yes it is converted via np.asarray(). This mirrors the current behaviour and explains why torch-CPU inputs already work today.

Determining if np.asarray(a) will succeed or not is a bit tricky. The obvious way of finding out would be to call np.asarray and catch exceptions. However, JAX arrays on the GPU will successfully convert to a Numpy array and I think we don't want that behaviour. Instead we use DLPack to check if the array is in CPU memory, and if yes then assume it can be converted.

AI usage disclosure

I used AI assistance for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Research and understanding

Related to #33444

Supersedes #34238

When array API is enabled some inputs need to be converted to Numpy
arrays. This is to make sure that code that works today continues to
work with array API enabled.
@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label Jun 17, 2026
@github-actions github-actions Bot added the CI:Linter failure The linter CI is failing on this PR label Jun 22, 2026
@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label Jun 22, 2026
@betatim

betatim commented Aug 3, 2026

Copy link
Copy Markdown
Member Author

Waiting to see where the discussion in #34604 goes before investing more here

This branch has not been deployed

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

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

1 participant