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

Running jit_module on an existing module? (And document calling jit(...)(func) manually) #7268

Open
Artoria2e5 opened this issue Aug 4, 2021 · 1 comment

Comments

@Artoria2e5
Copy link

Artoria2e5 commented Aug 4, 2021

Feature request

I have an extremely simple library on my hands that I want to try numba with, but to me it does not really make sense to add a dependency to it (it has zero right now). I figured that it might be a good idea to allow users to manually jit a module object they have imported, along the lines of:

import numba
import module as _module
module = numba.jit_module(module)

A similar case is the use of @jit outside of a decorator context, so an un-jitted-at-definition-time function gets JITed. This is already possible due to the way Python's decorator works, but IMO it would help to put them in examples:

from numba import jit
from module import some_cool_func
potentially_cooler_func = jit(some_cool_func)

(Huh, I like how jit's docstring doesn't actually occur in the online manual...)

@stuartarchibald
Copy link
Contributor

Thanks for the feature request. I'm wondering if this sort of thing would work for your use case (based on https://numba.readthedocs.io/en/stable/user/jit-module.html#example-usage)

File: issue7268.py

try:
    from numba import jit_module
    HAVE_NUMBA = True
except ImportError:
    HAVE_NUMBA = False

def inc(x):
   return x + 1

def add(x, y):
   return x + y

import numpy as np
# Use NumPy's mean function
mean = np.mean

def mul(a, b):
   return a * b

if HAVE_NUMBA:
    jit_module(nopython=True, error_model="numpy")

def div(a, b):
    return a / b

then:

In [1]: import issue7268

In [2]: issue7268.mul
Out[2]: CPUDispatcher(<function mul at 0x7f948f4f11f0>)

In [3]: issue7268.div
Out[3]: <function issue7268.div(a, b)>

this leads to no dependency on Numba, but if it is present and importable, it's used.

A similar case is the use of @jit outside of a decorator context, so an un-jitted-at-definition-time function gets JITed. This is already possible due to the way Python's decorator works, but IMO it would help to put them in examples:

It's a slightly unusual use case as it's atypical for a non-trivial function to be written that "just works" with Numba and the author not already having Numba in mind/designed the module/functions to work with Numba. Spelling out that it's possible for Numba to compile functions from pure python modules (so long as they are compilable) is probably something that should be explicitly mentioned, patches to the docs are welcomed.

(Huh, I like how jit's docstring doesn't actually occur in the online manual...)

I think the docstring probably needs an update to amalgamate it with the information in https://numba.readthedocs.io/en/stable/reference/jit-compilation.html#jit-functions and then the docs setup to render that. Patches are welcomed.

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

2 participants