Skip to content

Dask Gufunc signatures should ignore whitespace #7972

Closed

Description

@thomasaarholt

What happened:
Today I was trying to write an da.as_gufunc wrapper for np.linalg.inv, since the dask version doesn't support having loop dimensions (e.g. 2D input works, 3D input, where the first dimension should be iterated over, doesn't work.)

The following was my attempt:

import numpy as np
import dask.array as da
a = da.random.random((6,5,5))

@da.as_gufunc(signature="(n, n)->(n, n)", output_dtypes=float, vectorize=True)
def gufoo(x):
    return np.linalg.inv(x)

y = gufoo(b)

and for some reason I kept getting the error
ValueError: not a valid gufunc signature: (n, n)->(n, n) (click for full error)

The issue was the whitespace in my signature: "(n, n)->(n, n)" had to be "(n,n)->(n,n)". This is not mentioned in the [dask gufunc documentation](numba signature).

What you expected to happen:
Numpy gufunc documentation states that

  1. White spaces are ignored.

I would expect that the signature should work regardless of whitespace, since it is common to write, e.g. da.random.random((6, 5, 5)), and not be forced to random((6,5,5))`.

Including whitespace does work in Numba:

import numpy as np
from numba import guvectorize, int64
x = np.random.randint(1, 5, size=(5,5), dtype=np.int64)
y = 1
res = np.zeros((5,5), dtype=np.int64)

@guvectorize([(int64[:, :], int64, int64[:, :])], '(n,      n),()->(n, n)')
def g(x, y, res):
    for i in range(x.shape[0]):
        res[i] = x[i] + y
        
g(x, y, res)

Anything else we need to know?:
I will point out that this was surprisingly difficult to figure out, since I wasn't thinking about whitespace - it would suddenly work (no whitespace) and then I would try a moment later (with whitespace) and it didn't work ("Am I going crazy?").

Environment:

  • Dask version:'2021.07.0'
  • Python version: 3.9
  • Operating System: OS X
  • Install method (conda, pip, source): Conda

Activity

  1. changed the title [-]Dask Gufunc signatures should not ignore whitespace[/-] [+]Dask Gufunc signatures should ignore whitespace[/+] on Aug 1, 2021
  2. jrbourbeau commented on Aug 2, 2021

    @jrbourbeau
    Member

    Thanks for raising an issue and providing a nice minimal reproducer @thomasaarholt!

    It looks like the issue is arising here

    dask/dask/array/gufunc.py

    Lines 307 to 308 in 264c628

    if vectorize:
    tempfunc = np.vectorize(func, signature=signature)

    where we pass the signature to np.vectorize. This is probably a bug in NumPy (xref numpy/numpy#19597) though I think we could remove whitespace characters ourselves as a simple workaround. Is this something you're interested in contributing? (no obligation though)

  3. jrbourbeau commented on Aug 16, 2021

    @jrbourbeau
    Member

    Just noting that the upstream issue in NumPy was resolved in numpy/numpy#19627

  4. GenevieveBuckley commented on Oct 14, 2021

    @GenevieveBuckley
    Contributor

    I'm still observing this issue on Dask's main branch, fyi

  5. GenevieveBuckley commented on Oct 14, 2021

    @GenevieveBuckley
    Contributor

    ...ah, but my numpy version is 1.21.2

    That's the latest version of numpy, but the fix numpy/numpy#19627 was introduced was merged the day after it was released.

    So we can wait for the next numpy release to come out, at which point we should be able to close this issue.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions