Repository navigation
Conversation
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.
2 of 5 tasks
Member
Author
|
Waiting to see where the discussion in #34604 goes before investing more here |
5 of 10 tasks
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
PCAfor all solvers. Onmain, 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
fitit 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_datameans 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):This fails because we pass a dtype object to
torch.asarraythat isn't a torch dtype.Another example is using
PCA(svd_solver='arpack'):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_dataconvert 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 likePCAwhich 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 vianp.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 callnp.asarrayand 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:
Related to #33444
Supersedes #34238