Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Allow libraries to negotiate that they understand e.g. NumPy arrays? #586

Closed
seberg opened this issue Jan 26, 2023 · 4 comments
Closed

Allow libraries to negotiate that they understand e.g. NumPy arrays? #586

seberg opened this issue Jan 26, 2023 · 4 comments
Labels
API extension Adds new functions or objects to the API.

Comments

@seberg
Copy link
Contributor

seberg commented Jan 26, 2023

Today dask came up a few times and also the possibility of a fallback to __array_function__ for transitioning at least.

This must have come up before, but did we ever discuss an API like:

class myarray:
    def __get_namespace__(self, types):
        for t in types:
            if t is myarray:
                 continue
            if t is np.ndarray:  # we know about ndarray!
                 continue
            return NotImplemeneted
         return mynamespace

which is not really important if you think about numpy, jax, torch, tensorflow: I don't think you should mix them really! But it seems more becomes interesting if we start talking about dask or quantities which are designed as wrappers?

The answer can be that this is not our problem have to coerce manually. In the NumPy world, we could actually just write get_namespace as:

@array_function_dispatched(lambda *args: args)
def get_namespace(*args):
    return numpy

(and assume others will return their namespace, since in practice everyone has it de-facto)

@asmeurer
Copy link
Member

Doesn't this require n-way multiple dispatch in general? How does array_function_dispatched handle that?

@seberg
Copy link
Contributor Author

seberg commented Apr 13, 2023

(As a correction from the above, accepting NumPy and maybe CuPy arrays can be interesting also for JAX/torch/...)

It came up again in SciPy now, so going to list some references:

IMO it is a mistake not to provision for this and there is a reason everyone seems to have some (basic) solution. Yes, I also think that this should be very restrictive/conservative (but that mainly a documentation thing in most schemes).

Of course what is actually important is the downstream need/ask, because array-api should adapt to those needs. From my side, I think there is enough evidence. We can say its not yet enough, but that can clearly change at this point in time.

@rgommers rgommers added API extension Adds new functions or objects to the API. use case labels Apr 19, 2023
@rgommers
Copy link
Member

There's several questions one could ask here:

  1. Is it important for adoption/usability?
  2. Should mixing array libraries, some kind of hierarchy or dispatching be supported by the standard itself?
  3. Or should it be supported in array_api_compat?
  4. Or is it numpy-specific, and should it therefore be defined by numpy only?

Doesn't this require n-way multiple dispatch in general? How does array_function_dispatched handle that?

In general, yes. It's pretty well explained in this section of NEP 18. Basically " subclasses before superclasses, and otherwise left to right", and then let each handler decide whether it can deal with foreign array objects or if it wants to return NotImplemented.

@leofang
Copy link
Contributor

leofang commented Apr 20, 2023

I thought we decided that this kind of usage is UB (#399) but it might actually be good to at least revisit and reconfirm this. Another use case is pint+dask+cupy+xarray: pydata/xarray#7721

@data-apis data-apis locked and limited conversation to collaborators Apr 4, 2024
@kgryte kgryte converted this issue into discussion #774 Apr 4, 2024
@kgryte kgryte removed the use case label Apr 4, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
API extension Adds new functions or objects to the API.
Projects
None yet
Development

No branches or pull requests

5 participants