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

Using vectorize with nopython argument raises error when used without decorator syntax #8231

Closed
2 tasks done
timothymillar opened this issue Jul 7, 2022 · 4 comments
Closed
2 tasks done
Labels
no action required No action was needed to resolve. question Notes an issue as a question

Comments

@timothymillar
Copy link

Reporting a bug

When calling vectorize without decorator syntax any use of the nopython argument results in a type error due to unexpected keyword argument 'nopython'.

  • I have tried using the latest released version of Numba (most recent is
    visible in the change log (https://github.com/numba/numba/blob/main/CHANGE_LOG).
  • I have included a self contained code sample to reproduce the problem.
    i.e. it's possible to run as 'python bug.py'.

Minimal example:

from numba import vectorize

# this works
@vectorize(nopython=True)
def foo(x):
    return x + 1

# this doesn't
def bar(x):
    return x + 1
vectorize(bar, nopython=True)

Traceback:

TypeError                                 Traceback (most recent call last)
Input In [4], in <cell line: 1>()
----> 1 vectorize(bar, nopython=True)

File ~/miniconda3/envs/numba/lib/python3.9/site-packages/numba/np/ufunc/decorators.py:118, in vectorize(ftylist_or_function, **kws)
    116     ftylist = [ftylist_or_function]
    117 elif inspect.isfunction(ftylist_or_function):
--> 118     return dufunc.DUFunc(ftylist_or_function, **kws)
    119 elif ftylist_or_function is not None:
    120     ftylist = ftylist_or_function

TypeError: __init__() got an unexpected keyword argument 'nopython'

This doesn't seem to be an issue with the jit or guvectorize decorators and it's not an issue when using the cache argument for vectorize

@esc esc added the bug - failure to compile Bugs: failed to compile valid code label Jul 7, 2022
@esc
Copy link
Member

esc commented Jul 7, 2022

@timothymillar thank you for reporting this. I can confirm I am able to reproduce this.

@esc
Copy link
Member

esc commented Jul 7, 2022

FWIW: here is an example that attempts to execute the resulting ufunc:

import numpy as np
from numba import vectorize


# this works
@vectorize(nopython=True)
def foo(x):
    return x + 1


# this doesn't
def bar(x):
    return x + 1


bar = vectorize(bar, nopython=True)

a = np.ones(42)

print(foo(a))
print(bar(a))

Using the following patch:

diff --git i/numba/np/ufunc/decorators.py w/numba/np/ufunc/decorators.py
index 57776a466d..9fffd9f9d2 100644
--- i/numba/np/ufunc/decorators.py
+++ w/numba/np/ufunc/decorators.py
@@ -115,7 +115,8 @@ def vectorize(ftylist_or_function=(), **kws):
         # Common user mistake
         ftylist = [ftylist_or_function]
     elif inspect.isfunction(ftylist_or_function):
-        return dufunc.DUFunc(ftylist_or_function, **kws)
+        vec = Vectorize(ftylist_or_function, **kws)
+        return vec.build_ufunc()
     elif ftylist_or_function is not None:
         ftylist = ftylist_or_function

I can get the code to run and both print statements yield the same output, which is a good indication.

@stuartarchibald @sklam does this look like an appropriate fix? If yes, I can stage a PR with an accompanying test.

@esc
Copy link
Member

esc commented Jul 12, 2022

@timothymillar we discussed this in our weekly meeting today and it seems like the semantics of the vectorize decorator are different from the @njit decorator.

Doing the following instead will yield the result you are expecting:

import numpy as np
from numba import vectorize


# this works
@vectorize(nopython=True)
def foo(x):
    return x + 1


# this doesn't
def bar(x):
    return x + 1


bar = vectorize(nopython=True)(bar)

a = np.ones(42)

print(foo(a))
print(bar(a))

@esc esc added question Notes an issue as a question no action required No action was needed to resolve. and removed bug - failure to compile Bugs: failed to compile valid code labels Jul 12, 2022
@esc esc closed this as completed Jul 12, 2022
@timothymillar
Copy link
Author

Great, thanks @esc!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no action required No action was needed to resolve. question Notes an issue as a question
Projects
Archived in project
Development

No branches or pull requests

2 participants