Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array conversion methods #9823

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

Array conversion methods #9823

wants to merge 8 commits into from

Conversation

slevang
Copy link
Contributor

@slevang slevang commented Nov 25, 2024

This implements a convenience method to convert data between duck array types, and another to check the current type of your data. Basically identical to cupy-xarray's as_cupy()/is_cupy() methods, but exposed in a more general way.

The signature I have here takes a callable plus any kwargs, so it's quite flexible:

ds.as_array_type(cp.asarray)
ds.as_array_type(jnp.from_dlpack)
ds.as_array_type(jnp.asarray, device=jax.devices("gpu")[0])
ds.as_array_type(pint.Quantity, units="m/s")

Then:

ds.is_array_type(cp.ndarray) # -> True

I'm not sure about the naming. There are also other ways we could go about this, e.g. string matching for supported arrays.

Follow up to #9798.

@TomNicholas TomNicholas added API design topic-arrays related to flexible array support array API standard Support for the Python array API standard labels Nov 25, 2024
@slevang
Copy link
Contributor Author

slevang commented Dec 3, 2024

@shoyer @dcherian any further thoughts on this API?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API design array API standard Support for the Python array API standard topic-arrays related to flexible array support
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants