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 the source code, design documents, or content of the product. The open-source model is a decentralized so ...
JIT compiler that translates a subset of
Python and
NumPy into fast machine code using
LLVM
LLVM is a set of compiler and toolchain technologies that can be used to develop a front end for any programming language and a back end for any instruction set architecture. LLVM is designed around a language-independent intermediate repre ...
, 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
Travis Oliphant is an American data scientist and businessman. He is a co-founder of NumFOCUS, 501(c)(3) nonprofit charity in the United States, and sits on its advisory board. He is also a founder of technology startup Anaconda (''previously ...
in 2012 and has since been under active development at https://github.com/numba/numba with 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 A ...
, the
Gordon and Betty Moore Foundation,
Intel
Intel Corporation is an American multinational corporation and technology company headquartered in Santa Clara, California. It is the world's largest semiconductor chip manufacturer by revenue, and is one of the developers of the x86 ser ...
,
Nvidia
Nvidia CorporationOfficially written as NVIDIA and stylized in its logo as VIDIA with the lowercase "n" the same height as the uppercase "VIDIA"; formerly stylized as VIDIA with a large italicized lowercase "n" on products from the mid 1990s to ...
and
AMD, and a community of contributors on
GitHub
GitHub, Inc. () is an Internet hosting service for software development and version control using Git. It provides the distributed version control of Git plus access control, bug tracking, software feature requests, task management, cont ...
.
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):
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 happens transparently when the function is called:
>>> monte_carlo_pi(1000000)
3.14
The Numba website at https://numba.pydata.org 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. Currently two backends are available:
*
NVIDIA
Nvidia CorporationOfficially written as NVIDIA and stylized in its logo as VIDIA with the lowercase "n" the same height as the uppercase "VIDIA"; formerly stylized as VIDIA with a large italicized lowercase "n" on products from the mid 1990s to ...
CUDA
CUDA (or Compute Unified Device Architecture) is a parallel computing platform and application programming interface (API) that allows software to use certain types of graphics processing units (GPUs) for general purpose processing, an approach ...
, see
*
AMD ROCm
HSA, see
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,
TensorFlow
TensorFlow is a free and open-source software library for machine learning and artificial intelligence. It can be used across a range of tasks but has a particular focus on training and inference of deep neural networks. "It is machine learnin ...
,
PyTorch,
ChainerPythran and
PyPy.
References
{{reflist
Python (programming language)
Python (programming language) implementations
Articles with example Python (programming language) code