添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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

Describe the bug 🐛

A function that is JIT-decorated and contains, e.g., numpy.var() raises an error when JIT compilation is disabled.

Minimal example:

import os
import numba
import numpy as np
@numba.njit
def compute_var(x: np.ndarray) -> float:
    return float(np.var(x))
np.random.seed(42)
x = np.random.normal(0, 1, 10)
os.environ["NUMBA_DISABLE_JIT"] = "1"
print(compute_var(x))

This produces:

Traceback (most recent call last):
  File "/Users/timothy/Library/Application Support/JetBrains/PyCharmCE2023.1/scratches/scratch_6.py", line 16, in <module>
    print(compute_var(x))
  File "/usr/local/anaconda3/envs/demo-project/lib/python3.10/site-packages/numba/core/dispatcher.py", line 468, in _compile_for_args
    error_rewrite(e, 'typing')
  File "/usr/local/anaconda3/envs/demo-project/lib/python3.10/site-packages/numba/core/dispatcher.py", line 409, in error_rewrite
    raise e.with_traceback(None)
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: native lowering)
Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<function real at 0x7f8ba8460040>) found for signature:
 >>> real(float64)
There are 2 candidate implementations:
  - Of which 2 did not match due to:
  Overload in function 'np_real': File: numba/np/arraymath.py: Line 3583.
    With argument(s): '(float64)':
   Rejected as the implementation raised a specific error:
     AttributeError: 'function' object has no attribute 'get_call_template'
  raised from /usr/local/anaconda3/envs/demo-project/lib/python3.10/site-packages/numba/core/types/functions.py:541
During: resolving callee type: Function(<function real at 0x7f8ba8460040>)
During: typing of call at /usr/local/anaconda3/envs/demo-project/lib/python3.10/site-packages/numba/np/arraymath.py (437)
File "../../../../../../../usr/local/anaconda3/envs/demo-project/lib/python3.10/site-packages/numba/np/arraymath.py", line 437:
    def array_var_impl(arr):
        <source elided>
            val = (v.item() - m)
            ssd += np.real(val * np.conj(val))
During: lowering "$10call_method.4 = call $6load_method.2(x, func=$6load_method.2, args=[Var(x, scratch_6.py:8)], kws=(), vararg=None, varkwarg=None, target=None)" at /Users/timothy/Library/Application Support/JetBrains/PyCharmCE2023.1/scratches/scratch_6.py (8)

The problem disappears if I remove the decorator or do not set NUMBA_DISABLE_JIT to true.

The problem is also present for other numpy functions, e.g., when using np.median() instead of np.var(). Other aggregation functions—e.g., np.sum() or np.mean()—seem to work fine.

Versions:

Python: 3.10.10
numba: 0.56.4
numpy: 1.23.5
OS: macOS 13.2.1 (22D68) running on a 2019 MBP (Intel Core i7)
JIT-decorated functions raise error when setting NUMBA_JIT_DISABLE=1
JIT-decorated functions raise error when setting NUMBA_DISABLE_JIT=1
      Apr 6, 2023
          

Hi @timothygebhard, thank you for submitting the issue.

I can confirm the same locally on MacOS. Seems like the NUMBA_DISABLE_JIT flag was not calibrated for the new @overload decorator that Numba now uses. A MWR for the issue is as follows:

import os
import numba
from numba.extending import overload
os.environ["NUMBA_DISABLE_JIT"] = "1"
def foo(x):
    return x
@overload(foo)
def foo_impl(x):
    def impl(x):
        return x
    return impl
@numba.njit
def compute_var(x):
    return foo(x)
print(compute_var(3))
          

@stuartarchibald Thanks, that's definitely some very useful information! However, I'm wondering how that would work in a unit test context. I originally ran into the above problem when trying to do something like this:

def test__my_function(monkeypatch: pytest.MonkeyPatch) -> None:
    # Case 1: Locally disable JIT compilation (e.g., to get test coverage right)
    with monkeypatch.context() as mpc:
        mpc.setenv("NUMBA_DISABLE_JIT", "1")
        assert my_function(...) == ...  # my_function is @njit-decorated
    # Case 2: Test functions with JIT compilation enabled
    with monkeypatch.context() as mpc:
        mpc.setenv("NUMBA_DISABLE_JIT", "0")
        assert my_function(...) == ...  # my_function is @njit-decorated

If I need to disable JIT compilation before importing numba (i.e., before importing my_function), does that mean that I will have to place case 1 and case 2 in separate files? [I could live with that, I just want to be sure I am not overlooking anything.]