Numba is an
open-source
Open source is source code that is made freely available for possible modification and redistribution. Products include permission to use and view the source code, design documents, or content of the product. The open source model is a decentrali ...
JIT compiler
In computing, just-in-time (JIT) compilation (also dynamic translation or run-time compilations) is compiler, compilation (of Source code, computer code) during execution of a program (at run time (program lifecycle phase), run time) rather than b ...
that translates a subset of
Python and
NumPy into fast machine code using
LLVM
LLVM, also called LLVM Core, is a target-independent optimizer and code generator. It can be used to develop a Compiler#Front end, frontend for any programming language and a Compiler#Back end, backend for any instruction set architecture. LLVM i ...
, via the llvmlite Python package. It offers a range of options for parallelising Python code for CPUs and GPUs, often with only minor code changes.
Numba was started by
Travis Oliphant in 2012 and has since been under active development a
its repository in GitHubwith frequent releases. The project is driven by developers at Anaconda, Inc., with support by
DARPA
The Defense Advanced Research Projects Agency (DARPA) is a research and development agency of the United States Department of Defense responsible for the development of emerging technologies for use by the military. Originally known as the Adva ...
, the
Gordon and Betty Moore Foundation,
Intel
Intel Corporation is an American multinational corporation and technology company headquartered in Santa Clara, California, and Delaware General Corporation Law, incorporated in Delaware. Intel designs, manufactures, and sells computer compo ...
,
Nvidia
Nvidia Corporation ( ) is an American multinational corporation and technology company headquartered in Santa Clara, California, and incorporated in Delaware. Founded in 1993 by Jensen Huang (president and CEO), Chris Malachowsky, and Curti ...
and
AMD
Advanced Micro Devices, Inc. (AMD) is an American multinational corporation and technology company headquartered in Santa Clara, California and maintains significant operations in Austin, Texas. AMD is a hardware and fabless company that de ...
, and a community of contributors on
GitHub
GitHub () is a Proprietary software, proprietary developer platform that allows developers to create, store, manage, and share their code. It uses Git to provide distributed version control and GitHub itself provides access control, bug trackin ...
.
Example
Numba can be used by simply applying the
numba.jit
decorator to a Python function that does numerical computations:
import numba
import random
@numba.jit
def monte_carlo_pi(n_samples: int) -> float:
"""Monte Carlo"""
acc = 0
for i in range(n_samples):
x = random.random()
y = random.random()
if (x**2 + y**2) < 1.0:
acc += 1
return 4.0 * acc / n_samples
The
just-in-time compilation
In computing, just-in-time (JIT) compilation (also dynamic translation or run-time compilations) is compilation (of computer code) during execution of a program (at run time) rather than before execution. This may consist of source code transl ...
happens transparently when the function is called:
>>> monte_carlo_pi(1000000)
3.14
Numba's website contains many more examples, as well as information on how to get good performance from Numba.
GPU support
Numba can compile Python functions to GPU code. Initially two backends are available:
*
NVIDIA
Nvidia Corporation ( ) is an American multinational corporation and technology company headquartered in Santa Clara, California, and incorporated in Delaware. Founded in 1993 by Jensen Huang (president and CEO), Chris Malachowsky, and Curti ...
CUDA
In computing, CUDA (Compute Unified Device Architecture) is a proprietary parallel computing platform and application programming interface (API) that allows software to use certain types of graphics processing units (GPUs) for accelerated gene ...
, see
*
AMD
Advanced Micro Devices, Inc. (AMD) is an American multinational corporation and technology company headquartered in Santa Clara, California and maintains significant operations in Austin, Texas. AMD is a hardware and fabless company that de ...
ROCm HSA, see
Since release 0.56.4,
AMD ROCm HSA has been officially moved to unmaintained status and
separate repository stubhas been created for it.
Alternative approaches
Numba is one approach to make Python fast, by compiling specific functions that contain
Python and NumPy code. Many alternative approaches for fast numeric computing with Python exist, such as
Cython
Cython () is a superset of the programming language Python, which allows developers to write Python code (with optional, C-inspired syntax extensions) that yields performance comparable to that of C.
Cython is a compiled language that is ty ...
Pythran and
PyPy.
References
{{reflist
Python (programming language)
Python (programming language) implementations
Articles with example Python (programming language) code