Most Numba errors and slowdowns are type problems, so you need to see what the compiler inferred.
Numba's type system is richer than NumPy dtypes: an array type records dtype, number of dimensions and layout, written like float64[:, ::1] (2D, C-contiguous) or int32[:] (1D, any layout). Scalars are int64, float64, boolean, complex128, and so on. Every variable in a compiled function gets exactly one type, unified across all assignments.
Normally the dispatcher compiles lazily for whatever arguments it sees. You can instead pass explicit signatures to compile eagerly, at decoration time:
@njit("float64(float64[:], float64)")or with type objects, @njit(float64(float64[::1], float64)). With signatures, only those types are accepted; calling with anything else raises TypeError instead of compiling a new version. Signatures are useful for ahead-of-time warm-up, for libraries that want predictable behaviour, and for cfunc and @vectorize.
To inspect what happened:
f.signatureslists compiled signatures.numba.typeof(value)shows the Numba type of a Python value.f.inspect_types()prints the source annotated with inferred types, revealing unexpectedfloat64toint64mixes orpyobjectfallbacks.
Integer semantics differ from Python: Numba integers are fixed width (int64 by default), so they can overflow silently instead of growing like Python ints.
Example
import numpy as np
from numba import njit, typeof, float64
print(typeof(np.zeros((2, 3)))) # array(float64, 2d, C)
print(typeof(np.zeros(4)[::2])) # array(float64, 1d, A)
print(typeof(3), typeof(2.5)) # int64 float64
@njit(float64(float64[::1], float64)) # compiled eagerly, at definition
def scale_sum(a, k):
return (a * k).sum()
print(scale_sum(np.ones(3), 2.0)) # 6.0
print(scale_sum.signatures) # [(Array(float64, 1, 'C', False, aligned=True), float64)]
try:
scale_sum(np.ones(3, dtype=np.int64), 2.0)
except TypeError as e:
print("TypeError:", str(e)[:40]) # No matching definition for argument ...
@njit
def overflow(n):
return n * n
print(overflow(2**40)) # 0 (int64 wrap-around, Python gives 1208925819614629174706176)Remember
- Array types include dtype, ndim and layout (C, F or A)
- Explicit signatures compile eagerly and reject other types
- Numba integers are fixed width and can overflow silently
Watch out for
- Assuming Python's unlimited integers inside compiled code.