Eager Compilation in Numba

Overview

  • Time: 10 min

  1. Learn the difference between eager and lazy compilation.

  2. Learn how to use eager compilation.

Typically, Numba operates in lazy compilation mode.

1@jit
2def f(x, y):
3    return x + y

In this mode, the compilation is postponed until the function is executed for the first time. During execution, Numba infers the argument types and generates optimized code based on this information. Additionally, Numba can create separate specialized versions of the code depending on the input types.

In eager compilation mode you can also tell Numba the function signature you are expecting.

1@jit(int32(int32, int32))
2def f(x, y):
3    return x + y

The function signature is int32(int32, int32). In this case, the @jit decorator will compile the specific specialization for this signature, and no other specializations will be permitted. If you omit the return type and use (int32, int32) instead of int32(int32, int32), Numba will infer the return type automatically.

Key Points

  1. Generally we go for lazy compilation in Numba.

  2. Eager compilation gives more control over types chosen by the compiler.