Numba

{{Primary sources|date=November 2024}}

{{Short description|JIT compiler for Python}}

{{Infobox software

| name = Numba

| title =

| logo = Numba logo.svg

| logo_caption = Numba logo

| screenshot =

| caption =

| collapsible =

| author = Continuum Analytics

| developer = Community project

| released = {{Start date and age|2012|08|15|df=yes}}

| discontinued =

| latest release version = {{wikidata|property|edit|reference|P348}}

| latest release date = {{start date and age|{{wikidata|qualifier|P348|P577}}}}

| programming language = Python, C

| operating system = Cross-platform

| platform = x86-64, ARM64, POWER

| size =

| language =

| genre = Technical computing

| license = BSD 2-clause

| website = {{URL|http://numba.pydata.org/}}

}}

{{Infobox software

| title = Numba CUDA

| logo =

| logo caption =

| logo alt =

| logo upright =

| logo size =

| collapsible =

| screenshot =

| screenshot upright =

| screenshot size =

| screenshot alt =

| caption =

| other_names =

| author =

| developer = NVIDIA

| released =

| ver layout =

| discontinued =

| latest release version = 0.4.0

| latest release date = {{Start date and age|2025|1|27}}{{cite web |url=https://github.com/NVIDIA/numba-cuda/tags |title=Tags · NVIDIA/numba-cuda |date= |author= |accessdate= February 16, 2025}}

| latest preview version =

| latest preview date =

| repo = {{URL|https://github.com/NVIDIA/numba-cuda}}

| qid =

| programming language =

| middleware =

| engine =

| operating system =

| platform = NVIDIA GPU

| included with =

| replaces =

| replaced_by =

| service_name =

| size =

| standard =

| language =

| language count =

| language footnote =

| genre =

| license = BSD 2-clause

| website = {{URL|https://nvidia.github.io/numba-cuda/}}

| AsOf =

}}

Numba is an open-source JIT compiler that translates a subset of Python and NumPy into fast machine code using LLVM, 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 at [https://github.com/numba/numba its repository in GitHub] with frequent releases. The project is driven by developers at Anaconda, Inc., with support by DARPA, the Gordon and Betty Moore Foundation, Intel, Nvidia and AMD, and a community of contributors on GitHub.

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 happens transparently when the function is called:

>>> monte_carlo_pi(1000000)

3.14

[https://numba.pydata.org 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 CUDA, see {{URL|https://numba.readthedocs.io/en/stable/cuda/index.html}}
  • AMD ROCm HSA, see {{URL|https://numba.pydata.org/numba-doc/dev/roc}}

Since release 0.56.4,{{cite web | url=https://numba.readthedocs.io/en/stable/release-notes.html#version-0-54-0-19-august-2021 | title=Release Notes — Numba 0.56.4+0.g288a38bbd.dirty-py3.7-linux-x86_64.egg documentation }} AMD ROCm HSA has been officially moved to unmaintained status and a [https://github.com/numba/numba-rocm separate repository stub] has 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, [https://pythran.readthedocs.io/en/latest/ Pythran], and PyPy.

References