from numba import jit
import numpy as npmul_numba in the previous notebook worked because @ and elementwise arithmetic both happen to be on Numba’s list of things it knows how to compile. That list is not “all of NumPy” — Numba’s nopython mode doesn’t call into NumPy’s C implementation at all. It re-implements a chosen subset of the NumPy API as typed operations its own compiler understands, and only that subset is legal inside a jitted function. The canonical reference is https://numba.readthedocs.io/en/stable/reference/numpysupported.html — everything not listed there is, by definition, off limits.
This matters because the boundary is invisible from the Python source. mul_numpy and mul_numba’s undecorated bodies were textually identical; nothing about reading @jit tells you in advance which of your function bodies will make it through. The only way to find out is to try, and to be able to read what comes back when it fails.
Take a one-line dot product, written the way anyone reaching for np.einsum would write it — it’s the general tool for “contract these arrays along these axes”, so it’s a natural first instinct even for a plain 1D·1D product.
def dot_numpy(x, y):
return np.einsum("i,i->", x, y)
x = np.arange(10.0)
y = np.arange(10.0) * 2
dot_numpy(x, y)np.float64(570.0)
dot_numba_naive = jit(nopython=True, nogil=True)(dot_numpy)That line alone doesn’t fail — jit without an explicit signature compiles lazily, on first call, once it has seen the argument types. The failure shows up here:
try:
dot_numba_naive(x, y)
except Exception as e:
print(f"{type(e).__name__}: {e}")TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Use of unsupported NumPy function 'numpy.einsum' or unsupported use of the function.
File "../../../../../../../../../../../tmp/ipykernel_531442/696719712.py", line 2:
<source missing, REPL/exec in use?>
During: typing of get attribute at /tmp/ipykernel_531442/696719712.py (2)
File "../../../../../../../../../../../tmp/ipykernel_531442/696719712.py", line 2:
<source missing, REPL/exec in use?>
During: Pass nopython_type_inference
Use of unsupported NumPy function 'numpy.einsum'. That’s the whole diagnosis, and it’s about as good as this class of error gets: np.einsum never appears in the supported-functions list, so Numba’s typing pass doesn’t recognise the attribute lookup np.einsum at all, and says so immediately.
Notice what’s missing from that trace, though: no chain of “during handling of the above exception, another exception occurred”, no CPython call stack through Numba’s own compiler internals. Numba’s dispatcher catches the internal TypingError and re-raises it with raise e.with_traceback(None) — deliberately discarding the traceback context so a user isn’t staring at a wall of compiler-internal frames. That’s a kindness here, where the answer is a one-line “not implemented”. It stops being a kindness the moment the function is implemented but was called in a way Numba’s overloads don’t cover — then this same trimmed format still fires, but instead of a name it prints out an equivalent for every overload Numba tried and rejected (kind= on np.sort is a real example: it types, resolves to np.sort, and only then rejects the keyword argument several frames into the overload’s own type checking). Same shape of error, same suppressed traceback, more digging required to see why nothing matched.
Either way, the fix is the same: swap the unsupported call for one that is on the list. np.dot covers exactly the 1D·1D case above and is supported by name:
def dot_numpy_supported(x, y):
return np.dot(x, y)
dot_numba = jit(nopython=True, nogil=True)(dot_numpy_supported)
dot_numba(x, y)570.0
Same result as dot_numpy, and this time the function actually compiled — dot_numba(x, y) above returned a number instead of raising. The lesson isn’t “avoid einsum” specifically; it’s that porting a NumPy function into @jit is a lookup against a fixed support table, not a guarantee, and the table is worth checking before writing the jitted version rather than after.