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 njitting partial functions #4587

Open
2 tasks done
mroeschke opened this issue Sep 18, 2019 · 4 comments
Open
2 tasks done

Support njitting partial functions #4587

mroeschke opened this issue Sep 18, 2019 · 4 comments

Comments

@mroeschke
Copy link

Reporting a bug

#2916 describes that **kwargs is not supported in njit functions.

In [10]: from numba import njit

In [11]: @njit
    ...: def f(func, args, kwargs):
    ...:     return func(*args, **kwargs)
    ...:

In [12]: @njit
    ...: def my_func(*args, **kwargs):
    ...:     return list(*args)
    ...:

In [13]: f(my_func, (1, 2, 3), {'a': 1, 'b': 2})
NotImplementedError: Failed in nopython mode pipeline (step: analyzing bytecode)
CALL_FUNCTION_EX with **kwargs not supported

In [14]: numba.__version__
Out[14]: '0.45.1'

Related to #4570, I am trying to use partial to bind the kwargs earlier.

In [23]: from functools import partial

In [24]: def my_func(*args, **kwargs):
    ...:     return list(args)
    ...:

In [25]: my_func_partial = partial(my_func, **{'a':2})

In [26]: njit(my_func_partial)

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-26-6bdaac92077c> in <module>
----> 1 njit(my_func_partial)

/anaconda3/envs/pandas-2s-dev/lib/python3.7/site-packages/numba/decorators.py in njit(*args, **kws)
    222         warnings.warn('forceobj is set for njit and is ignored', RuntimeWarning)
    223     kws.update({'nopython': True})
--> 224     return jit(*args, **kws)
    225
    226

/anaconda3/envs/pandas-2s-dev/lib/python3.7/site-packages/numba/decorators.py in jit(signature_or_function, locals, target, cache, pipeline_class, **options)
    159                    targetoptions=options, **dispatcher_args)
    160     if pyfunc is not None:
--> 161         return wrapper(pyfunc)
    162     else:
    163         return wrapper

/anaconda3/envs/pandas-2s-dev/lib/python3.7/site-packages/numba/decorators.py in wrapper(func)
    175         disp = dispatcher(py_func=func, locals=locals,
    176                           targetoptions=targetoptions,
--> 177                           **dispatcher_args)
    178         if cache:
    179             disp.enable_caching()

/anaconda3/envs/pandas-2s-dev/lib/python3.7/site-packages/numba/dispatcher.py in __init__(self, py_func, locals, targetoptions, impl_kind, pipeline_class)
    581
    582         _DispatcherBase.__init__(self, arg_count, py_func, pysig, can_fallback,
--> 583                                  exact_match_required=False)
    584
    585         functools.update_wrapper(self, py_func)

/anaconda3/envs/pandas-2s-dev/lib/python3.7/site-packages/numba/dispatcher.py in __init__(self, arg_count, py_func, pysig, can_fallback, exact_match_required)
    207
    208         argnames = tuple(pysig.parameters)
--> 209         default_values = self.py_func.__defaults__ or ()
    210         defargs = tuple(OmittedArg(val) for val in default_values)
    211         try:

AttributeError: 'functools.partial' object has no attribute '__defaults__'

Is there a better workaround for what I am trying to accomplish?

@stuartarchibald
Copy link
Contributor

Thanks for the report. I'm not sure there's a way around this at present. We'll discuss at the next core developer meeting.

@mroeschke
Copy link
Author

Thanks @stuartarchibald.

@MetinSa
Copy link

MetinSa commented Oct 7, 2022

Any updates on this?

I am trying to njit a function which takes as argument a tuple of partial functions. Running numba.typeof(my_partial_func) results in the following value error: ValueError: Cannot determine Numba type of <class 'functools.partial'>. If I try to re-njit the partial function: numba.njit(my_partial_func) I get the following type error: TypeError: The decorated object is not a function (got type <class 'functools.partial'>). Is there a way to achieve this with numba as of now?

@AlonSpinner
Copy link

Here for the same feature aswell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants