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 arguments that are functions in (n)jit #4570

Closed
mroeschke opened this issue Sep 15, 2019 · 3 comments
Closed

Support arguments that are functions in (n)jit #4570

mroeschke opened this issue Sep 15, 2019 · 3 comments
Labels
question Notes an issue as a question

Comments

@mroeschke
Copy link

Feature request

Would it be possible to support arguments that are functions when using jit that don't execute in object mode?

In [19]: @jit
    ...: def f(arg, func):
    ...:     return func(arg)
    ...:

In [20]: f([1], sum)
Compilation is falling back to object mode WITH looplifting enabled because Function "f" failed type inference due to: non-precise type pyobject
[1] During: typing of argument at <ipython-input-19-32db0f8a0a3e> (3)

File "<ipython-input-19-32db0f8a0a3e>", line 3:
def f(arg, func):
    return func(arg)
    ^
...
Out[20]: 1

In [21]: @njit
    ...: def f(arg, func):
    ...:     return func(arg)
    ...:

In [22]: f([1], sum)
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
non-precise type pyobject
[1] During: typing of argument at <ipython-input-21-76dc76439c5b> (3)

File "<ipython-input-21-76dc76439c5b>", line 3:
def f(arg, func):
    return func(arg)
    ^

@stuartarchibald
Copy link
Contributor

Thanks for the query. Numba supports JIT compiled functions as arguments, e.g.:

from numba import njit
from numpy import sum
import numpy as np

@njit
def f(arg, func):
    return func(arg)

jit_sum = njit(lambda x: sum(np.asarray(x)))

f([1], jit_sum)

does this help?

@stuartarchibald stuartarchibald added the question Notes an issue as a question label Sep 16, 2019
@mroeschke
Copy link
Author

Thanks @stuartarchibald! Just what I was looking for. Thanks for answering all my usage questions.

@stuartarchibald
Copy link
Contributor

@mroeschke no problem, glad to be of help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Notes an issue as a question
Projects
None yet
Development

No branches or pull requests

2 participants