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: main
Choose a base branch
from
Open
7 changes: 5 additions & 2 deletions opt_einsum/backends/array_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,26 @@ def to_array(array): # pragma: no cover

def make_build_expression_function(array_api: ModuleType) -> Callable:
"""Make a ``build_expression`` function for the given array API."""
_to_array_api = to_array_api[array_api.__name__]

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

@IsaacBreen IsaacBreen Jul 18, 2022

Choose a reason for hiding this comment

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

@jcmgray How about this?


return array_api_contract

return build_expression


def make_evaluate_constants_function(array_api: ModuleType) -> Callable:
_to_array_api = to_array_api[array_api.__name__]

def evaluate_constants(const_arrays, expr): # pragma: no cover
"""Convert constant arguments to cupy arrays, and perform any possible constant contractions."""
return expr(
*[make_to_array_function(array_api)(x) for x in const_arrays],
*[_to_array_api(x) for x in const_arrays],
backend=array_api.__name__,
evaluate_constants=True,
)
Expand Down