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

Support for Python array API standard #197

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

IsaacBreen
Copy link

@IsaacBreen IsaacBreen commented Jul 17, 2022

Description

This PR adds support for libraries that conform to the Python array API standard. It discovers complying modules upon import of opt_einsum.

Eventually (and hopefully soon), once the array API standard becomes ubiquitous, this should largely eliminate the need for backend-specific code.

Note that to test this you'll need a version of an array library which implements the API. Right now AFAIK closest thing to this is latest (dev) Numpy which you can install using

pip install --upgrade git+https://github.com/numpy/numpy

Todos

  • Tests
  • Update docs

Status

  • Ready to go

Copy link
Owner

@dgasmith dgasmith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to take a bit to go through and I'll be out next week. Looks a truly fantastic PR!

@codecov
Copy link

codecov bot commented Jul 18, 2022

Codecov Report

Merging #197 (13ec280) into master (3824e4f) will decrease coverage by 0.03%.
The diff coverage is 94.59%.

@dgasmith
Copy link
Owner

Looks like plenty of errors for Py3.6 where importlib.metadata is not yet extant.

@IsaacBreen
Copy link
Author

IsaacBreen commented Jul 18, 2022

Thanks Daniel! The errors for Python <= 3.7 should be fixed. I've set it to bypass array API backend discovery altogether in such cases. Numpy doesn't support <= 3.7 as-is, so I don't think this will be missed.

I've fixed up the formatting too.


def build_expression(_, expr): # pragma: no cover
"""Build an array API function based on ``arrays`` and ``expr``."""

def array_api_contract(*arrays):
return expr._contract([make_to_array_function(array_api)(x) for x in arrays], backend=array_api.__name__)
return expr._contract([_to_array_api(x) for x in arrays], backend=array_api.__name__)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jcmgray How about this?

Copy link
Owner

@dgasmith dgasmith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies for the delay here, this appears quite polished. From your view, what needs to happen to get this in?

from ..sharing import to_backend_cache_wrap


def discover_array_apis():
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible to type this function?

@@ -122,6 +123,7 @@ def has_tensordot(backend: str) -> bool:
"cupy": _cupy.build_expression,
"torch": _torch.build_expression,
"jax": _jax.build_expression,
**_array_api.build_expression,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way to check if Jax adds an array interface so that we can catch it and test before hand?

return [ep.load() for ep in eps]
else:
# importlib.metadata was introduced in Python 3.8, so it isn't available here. Unable to discover any array APIs.
return []
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Numpy does not officially support below 3.8: https://numpy.org/neps/nep-0029-deprecation_policy.html

It would be worth considering dropping Python 3.7 as well. @jcmgray what do you think?

@dgasmith
Copy link
Owner

dgasmith commented May 5, 2024

@IsaacBreen I'm looking to push out another release in the next month or so. Is this a PR which you would like to support?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants