Skip to content

[RFC]: resolving an array namespace from a dtype #1021

Backlog

Description

@ntessore

The following natural way to precompute values for a given dtype is not doable right now:

# should be
table = tabulate(size=10, dtype=np.float16)

# has to be
table = tabulate(size=10, dtype=np.float16, array_namespace=np)

The reason is that there is no strict API-compatible way to go from a given dtype to its array namespace; types only support comparison.

Possible solutions:

  • dtype.__array_namespace(), which I don't believe any backend supports (__array_namespace__() is not a classmethod)
  • dtype(0).__array_namespace(), which I believe all backends support

Activity

  1. lucascolley commented on Nov 26, 2026

    @lucascolley
    Member

    I think that in multiple cases, the dtype class is not unique to the namespace — IIRC CuPy and Dask just expose the NumPy dtype classes as aliases.

    I don't mind the second option tbh, although I would spell the parameter xp.

  2. ntessore commented on Nov 27, 2026

    @ntessore
    Author

    Ah, that would be a pity. I should probably have tested that the returned __array_namespace__() is actually the one I want in the second proposal 😄

    The second form has the issue that the dtype= and array_namespace= arguments can be incompatible. But even just in general terms, "where does this dtype come from?" seems a perfectly reasonable question to ask.

  3. rgommers commented on Nov 27, 2026

    @rgommers
    Member

    This is known and isn't solvable. SciPy has adopted the xp= keyword for the limited number of functions that need this (see for example the scipy.fft.fftfreq docs), so it isn't many characters.

    This is not possible to address, both because of the shared NumPy dtype issues Lucas pointed out, and because it will add unacceptable overhead to dtypes (and possibly other implementation issues) so PyTorch et al. won't want to implement this. So I suggest we close this issue.

  4. changed the title [-]From dtype to array namespace[/-] [+][RFC]: resolving an array namespace from a dtype[/+] on Nov 27, 2026
  5. added
    RFC
    Needs Discussion
    on Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs DiscussionRFC

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions