numba Package

numba Package

class numba.__init__.partial

Bases: object

partial(func, *args, **keywords) - new function with partial application of the given arguments and keywords.

args

tuple of arguments to future partial calls

func

function object to use in future partial calls

keywords

dictionary of keyword arguments to future partial calls

class numba.__init__.NumbaType(*args, **kwds)

Bases: numba.typesystem.types._NumbaType

Base for numba types.

conser = <numba.typesystem.itypesystem.Conser object at 0x105012310>
classmethod default_args(args, kwargs)
defaults = {}
mutable = True
typename = None
class numba.__init__.builtin_(*args, **kwds)

Bases: numba.typesystem.types.known_value

argnames = ['name', 'value']
conser = <numba.typesystem.itypesystem.Conser object at 0x105019bd0>
defaults = {}
flags = ['object']
func
is_builtin = True
is_object = True
mutable = False
typename = 'builtin'
numba.__init__.datetime(units=None, numpy=True)
class numba.__init__.pointer(*args, **kwds)

Bases: numba.typesystem.types.NumbaType

argnames = ['base_type']
conser = <numba.typesystem.itypesystem.Conser object at 0x105012650>
defaults = {}
is_pointer = True
is_string
mutable = False
typename = 'pointer'
class numba.__init__.tuple_(*args, **kwds)

Bases: numba.typesystem.types.ContainerListType

tuple(base_type, size)

conser = <numba.typesystem.itypesystem.Conser object at 0x105015b50>
defaults = {}
is_container = True
is_object = True
is_tuple = True
mutable = False
typename = 'tuple'
class numba.__init__.imap

Bases: object

imap(func, *iterables) –> imap object

Make an iterator that computes the function using arguments from each of the iterables. Like map() except that it returns an iterator instead of a list and that it stops when the shortest iterable is exhausted instead of filling in None for shorter iterables.

next

x.next() -> the next value, or raise StopIteration

class numba.__init__.carray(*args, **kwds)

Bases: numba.typesystem.types.NumbaType

argnames = ['base_type', 'size']
conser = <numba.typesystem.itypesystem.Conser object at 0x105012910>
defaults = {}
is_carray = True
mutable = False
typename = 'carray'
numba.__init__.Accessor
class numba.__init__.Conser(constructor)

Bases: object

Conser: constructs new objects only when not already available. Objects are weakreffed to sanitize memory consumption.

This allows the objects to be compared by and hashed on identity.

constructor
get(*args)
class numba.__init__.autojit_function(*args, **kwds)

Bases: numba.typesystem.types.NumbaType

Type for autojit functions

argnames = ['autojit_func']
conser = <numba.typesystem.itypesystem.Conser object at 0x105012fd0>
defaults = {}
flags = ['object']
is_autojit_function = True
is_object = True
mutable = False
typename = 'autojit_function'
numba.__init__.notconsing(cls)
class numba.__init__.complex_(*args, **kwds)

Bases: numba.typesystem.types.NumbaType

argnames = ['base_type']
conser = <numba.typesystem.itypesystem.Conser object at 0x1050153d0>
defaults = {}
flags = ['numeric']
is_complex = True
is_numeric = True
itemsize
mutable = False
typename = 'complex'
numba.__init__.consing(cls)

Cons calls to the constructor.

class numba.__init__.reference(*args, **kwds)

Bases: numba.typesystem.types.NumbaType

A reference to an (primitive or Python) object. This is passed as a pointer and dereferences automatically.

Currently only supported for structs.

argnames = ['referenced_type']
conser = <numba.typesystem.itypesystem.Conser object at 0x105019350>
defaults = {}
is_reference = True
mutable = False
typename = 'reference'
class numba.__init__.numpy_dtype(*args, **kwds)

Bases: numba.typesystem.types.NumbaType

Type of numpy dtypes

argnames = ['dtype']
conser = <numba.typesystem.itypesystem.Conser object at 0x105015290>
defaults = {}
flags = ['object']
is_numpy_dtype = True
is_object = True
mutable = False
typename = 'numpy_dtype'
class numba.__init__.meta(*args, **kwds)

Bases: numba.typesystem.types.NumbaType

A type instance in user code. e.g. double(value). The Name node will have a cast-type with dst_type ‘double’.

argnames = ['dst_type']
conser = <numba.typesystem.itypesystem.Conser object at 0x105015890>
defaults = {}
flags = ['object', 'cast']
is_cast = True
is_meta = True
is_object = True
mutable = False
typename = 'meta'
class numba.__init__.numpy_attribute(*args, **kwds)

Bases: numba.typesystem.types.NumbaType

argnames = ['module', 'attr']
conser = <numba.typesystem.itypesystem.Conser object at 0x105019050>
defaults = {}
flags = ['object', 'known_value']
is_known_value = True
is_numpy_attribute = True
is_object = True
typename = 'numpy_attribute'
value
class numba.__init__.Type(kind, *params, **kwds)

Bases: object

Base of all types.

classmethod default_args(args, kwargs)

Add defaults to a given args tuple for type construction

metadata = None
classmethod unit(kind, name, flags=(), **kwds)

Nullary type constructor creating the most elementary of types. Does not compose any other type.

numba.__init__.obj_type(name)
numba.__init__.timedelta(units=None, numpy=True)
class numba.__init__.method(*args, **kwds)

Bases: numba.typesystem.types.NumbaType

Method of something.

base_type: the object type the attribute was accessed on
argnames = ['base_type', 'attr_name']
conser = <numba.typesystem.itypesystem.Conser object at 0x105019490>
defaults = {}
flags = ['object']
is_method = True
is_object = True
mutable = False
typename = 'method'
class numba.__init__.dict_(*args, **kwds)

Bases: numba.typesystem.types.MapContainerType

dict(key, value, size)

conser = <numba.typesystem.itypesystem.Conser object at 0x105015f10>
defaults = {}
is_dict = True
is_object = True
mutable = False
typename = 'dict'
class numba.__init__.known_value(*args, **kwds)

Bases: numba.typesystem.types.NumbaType

Type which is associated with a known value or well-defined symbolic expression:

np.add => np.add np.add.reduce => (np.add, “reduce”)
(Remember that unbound methods like np.add.reduce are transient, i.e.
np.add.reduce is not np.add.reduce).
argnames = ['value']
conser = <numba.typesystem.itypesystem.Conser object at 0x105019810>
defaults = {}
is_known_value = True
mutable = False
typename = 'known_value'
class numba.__init__.ContainerListType(*args, **kwds)

Bases: numba.typesystem.types.NumbaType

Parameters:
  • base_type – the element type of the tuple
  • size – set to a value >= 0 is the size is known
Returns:

a tuple type representation

argnames = ['base_type', 'size']
conser = <numba.typesystem.itypesystem.Conser object at 0x1050159d0>
defaults = {}
flags = ['object', 'container']
is_container = True
is_object = True
is_sized()
mutable = False
numba.__init__.rank(type)
class numba.__init__.module_attribute(*args, **kwds)

Bases: numba.typesystem.types.NumbaType

argnames = ['module', 'attr']
conser = <numba.typesystem.itypesystem.Conser object at 0x1050191d0>
defaults = {}
flags = ['object', 'known_value']
is_known_value = True
is_module_attribute = True
is_object = True
typename = 'module_attribute'
value
numba.__init__.array(dtype, ndim, is_c_contig=False, is_f_contig=False, inner_contig=False)
Parameters:
  • dtype – the Numba dtype type (e.g. double)
  • ndim – the array dimensionality (int)
Returns:

an array type representation

numba.__init__.unit(*args, **kwargs)
class numba.__init__.global_(*args, **kwds)

Bases: numba.typesystem.types.known_value

Global type

conser = <numba.typesystem.itypesystem.Conser object at 0x105019ad0>
defaults = {}
is_global = True
mutable = True
typename = 'global'
class numba.__init__.function(*args, **kwds)

Bases: numba.typesystem.types.NumbaType

actual_signature

Passing structs by value is not properly supported for different calling conventions in LLVM, so we take an extra argument pointing to a caller-allocated struct value.

add_arg(i, arg)
argnames = ['return_type', 'args', 'name', 'is_vararg']
conser = <numba.typesystem.itypesystem.Conser object at 0x105012550>
defaults = {'is_vararg': False, 'name': None}
is_function = True
mutable = False
struct_by_reference
struct_return_type
typename = 'function'
class numba.__init__.array_(*args, **kwds)

Bases: numba.typesystem.types.NumbaType

An array type. array_ may be sliced to obtain a subtype:

>>> double[:, :, ::1][1:]
double[:, ::1]
>>> double[:, :, ::1][:-1]
double[:, :]
>>> double[::1, :, :][:-1]
double[::1, :]
>>> double[::1, :, :][1:]
double[:, :]
argnames = ['dtype', 'ndim', 'is_c_contig', 'is_f_contig', 'inner_contig']
conser = <numba.typesystem.itypesystem.Conser object at 0x105012d90>
defaults = {'is_f_contig': False, 'is_c_contig': False, 'inner_contig': False}
flags = ['object']
is_array = True
is_object = True
mutable = False
pointer()
strided
typename = 'array'
numba.__init__.sort_key(t)
class numba.__init__.module(*args, **kwds)

Bases: numba.typesystem.types.known_value

Represents a type for modules.

Attributes:
is_numpy_module: whether the module is the numpy module module: in case of numpy, the numpy module or a submodule
conser = <numba.typesystem.itypesystem.Conser object at 0x105019d50>
defaults = {}
flags = ['object']
is_math_module
is_module = True
is_numba_module
is_numpy_module
is_object = True
module
mutable = False
typename = 'module'
class numba.__init__.jit_function(*args, **kwds)

Bases: numba.typesystem.types.NumbaType

Type for jit functions

argnames = ['jit_func']
conser = <numba.typesystem.itypesystem.Conser object at 0x105015150>
defaults = {}
flags = ['object']
is_jit_function = True
is_object = True
mutable = False
typename = 'jit_function'
class numba.__init__.pointer_to_function(*args, **kwds)

Bases: numba.typesystem.types.NumbaType

Pointer to a function at a known address represented by some Python
object (e.g. a ctypes or CFFI function).
argnames = ['obj', 'ptr', 'signature']
conser = <numba.typesystem.itypesystem.Conser object at 0x105019610>
defaults = {}
flags = ['object']
is_object = True
is_pointer_to_function = True
typename = 'pointer_to_function'
class numba.__init__.list_(*args, **kwds)

Bases: numba.typesystem.types.ContainerListType

list(base_type, size)

conser = <numba.typesystem.itypesystem.Conser object at 0x105015c50>
defaults = {}
is_container = True
is_list = True
is_object = True
mutable = False
typename = 'list'
class numba.__init__.TypeMetaClass(name, bases, dict)

Bases: type

Metaclass for numba types, conses immutable types.

numba.__init__.struct_(fields=(), name=None, readonly=False, packed=False, **kwargs)

Create a mutable struct type

class numba.__init__.timedelta_(*args, **kwds)

Bases: numba.typesystem.types.NumbaType

argnames = ['diff', 'units', 'units_char']
conser = <numba.typesystem.itypesystem.Conser object at 0x1050156d0>
defaults = {}
flags = ['numeric']
is_numeric = True
is_numpy_timedelta = True
is_timedelta = True
itemsize
mutable = False
typename = 'timedelta'
class numba.__init__.known_pointer(*args, **kwds)

Bases: numba.typesystem.types.pointer

argnames = ['base_type', 'address']
conser = <numba.typesystem.itypesystem.Conser object at 0x105019950>
defaults = {}
is_known_pointer = True
mutable = False
typename = 'known_pointer'
class numba.__init__.istruct(*args, **kwds)

Bases: numba.typesystem.types.NumbaType

argnames = ['fields', 'name', 'readonly', 'packed']
conser = <numba.typesystem.itypesystem.Conser object at 0x105012a90>
defaults = {'readonly': False, 'name': None, 'packed': False}
fielddict
is_istruct = True
is_prefix(other_struct)
mutable = False
offsetof(field_name)

Compute the offset of a field. Must be used only after mutation has finished.

subtypes
typename = 'istruct'
class numba.__init__.datetime_(*args, **kwds)

Bases: numba.typesystem.types.NumbaType

argnames = ['timestamp', 'units', 'units_char']
conser = <numba.typesystem.itypesystem.Conser object at 0x105015510>
defaults = {}
flags = ['numeric']
is_datetime = True
is_numeric = True
is_numpy_datetime = True
itemsize
mutable = False
typename = 'datetime'
numba.__init__.add_flags(obj, flags)
numba.__init__.from_numpy_dtype(np_dtype)
Parameters:np_dtype – the NumPy dtype (e.g. np.dtype(np.double))
Returns:a dtype type representation
class numba.__init__.Function(signature, py_func)

Bases: object

Function types may be called with Python functions to create a Function object. This may be used to minivect users for their own purposes. e.g.

@double(double, double) def myfunc(...):

...
numba.__init__.pass_by_ref(type)
class numba.__init__.MapContainerType(*args, **kwds)

Bases: numba.typesystem.types.NumbaType

argnames = ['key_type', 'value_type', 'size']
conser = <numba.typesystem.itypesystem.Conser object at 0x105015d50>
defaults = {}
flags = ['object']
is_object = True
mutable = False
numba.__init__.tyname(name)
class numba.__init__.sized_pointer(*args, **kwds)

Bases: numba.typesystem.types.NumbaType

A pointer with knowledge of its range.

E.g. an array’s ‘shape’ or ‘strides’ attribute. This also allow tuple unpacking.

argnames = ['base_type', 'size']
conser = <numba.typesystem.itypesystem.Conser object at 0x105012790>
defaults = {}
flags = ['pointer']
is_pointer = True
is_sized_pointer = True
mutable = False
typename = 'sized_pointer'
numba.__init__.register(module, **kws)

@register(module) def my_type_function(arg1, ..., argN):

...
numba.__init__.autojit(template_signature=None, backend='ast', target='cpu', nopython=False, locals=None, **kwargs)

Creates a function that dispatches to type-specialized LLVM functions based on the input argument types. If no specialized function exists for a set of input argument types, the dispatcher creates and caches a new specialized function at call time.

numba.__init__.jit(restype=None, argtypes=None, backend='ast', target='cpu', nopython=False, **kws)

Compile a function given the input and return types.

There are multiple ways to specify the type signature:

  • Using the restype and argtypes arguments, passing Numba types.
  • By constructing a Numba function type and passing that as the first argument to the decorator. You can create a function type by calling an exisiting Numba type, which is the return type, and the arguments to that call define the argument types. For example, f8(f8) would create a Numba function type that takes a single double-precision floating point value argument, and returns a double-precision floating point value.
  • As above, but using a string instead of a constructed function type. Example: jit("f8(f8)").

If backend=’bytecode’ the bytecode translator is used, if backend=’ast’ the AST translator is used. By default, the AST translator is used. Note that the bytecode translator is deprecated as of the 0.3 release.

numba.__init__.export(signature, env_name=None, env=None, **kws)

Construct a decorator that takes a function and exports one

A signature is a string with

name ret_type(arg_type, argtype, ...)

numba.__init__.exportmany(signatures, env_name=None, env=None, **kws)

A Decorator that exports many signatures for a single function

numba.__init__.typeof(value)

Get the type of a variable or value.

Used outside of Numba code, infers the type for the object.

numba.__init__.addressof(obj, propagate=True)

Take the address of a compiled jit function.

Parameters:
  • obj – the jit function
  • write_unraisable – whether to write uncaught exceptions to stderr
  • propagate – whether to always propagate exceptions
Returns:

ctypes function pointer

numba.__init__.prange(start=0, stop=None, step=1)

_version Module

numba._version.get_versions(default={}, verbose=False)

ad Module

array_expressions Module

class numba.array_expressions.ArrayExpressionRewrite(context, func, ast, locals=None, func_signature=None, nopython=0, symtab=None, **kwargs)

Bases: numba.visitors.NumbaTransformer

Find element-wise expressions and run ElementalMapper to turn it into a minivect AST or a ufunc.

elementwise = False
is_slice_assign = False
nesting_level = 0
register_array_expression(node, lhs=None)

Start the mapping process for the outermost node in the array expression.

visit_Assign(node)
visit_BinOp(node)
visit_Call(node)
visit_Subscript(node)
visit_UnaryOp(node)
visit_elementwise(elementwise, node)
class numba.array_expressions.ArrayExpressionRewriteNative(context, func, ast, locals=None, func_signature=None, nopython=0, symtab=None, **kwargs)

Bases: numba.array_expressions.ArrayExpressionRewrite

Compile array expressions to a minivect kernel that calls a Numba scalar kernel with scalar inputs:

a[:, :] = b[:, :] * c[:, :]

becomes

tmp_a = slice(a) tmp_b = slice(b) tmp_c = slice(c) shape = broadcast(tmp_a, tmp_b, tmp_c) call minikernel(shape, tmp_a.data, tmp_a.strides,

tmp_b.data, tmp_b.strides, tmp_c.data, tmp_c.strides)

with

def numba_kernel(b, c):
return b * c
def minikernel(...):
for (...)
for(...)
a[i, j] = numba_kernel(b[i, j], c[i, j])

CAN be used in a nopython context

array_attr(node, attr)
expr_count = 0
register_array_expression(node, lhs=None)
class numba.array_expressions.ArrayExpressionRewriteUfunc(context, func, ast, vectorizer_cls=None)

Bases: numba.array_expressions.ArrayExpressionRewrite

Compile array expressions to ufuncs. Then call the ufunc with the array arguments.

vectorizer_cls: the ufunc vectorizer to use

CANNOT be used in a nopython context

register_array_expression(node, lhs=None)
class numba.array_expressions.NumbaStaticArgsContext

Bases: numba.utils.NumbaContext

Use a static argument list: shape, data1, strides1, data2, strides2, ...

astbuilder_cls

alias of ASTBuilder

init()
optimize_broadcasting = False
optimize_llvm = False
to_llvm(type)
numba.array_expressions.get_py_ufunc(env, lhs, node)
numba.array_expressions.get_py_ufunc_ast(env, lhs, node)
numba.array_expressions.is_elementwise_assignment(assmnt_node)

array_validation Module

class numba.array_validation.ArrayValidator(env)

Bases: ast.NodeVisitor

Validate array usage, depending on array representation (i.e. numpy vs. LLArray)

visit_CoerceToNative(node)
visit_CoerceToObject(node)
visit_CoercionNode(node)
visit_MultiArrayAPINode(node)
numba.array_validation.is_object(t)

ast_constant_folding Module

This module implements constant folding on the AST. It handles simple cases such as

  • 1 + 2 -> 3

  • 2 ** 10 -> 1024

  • N=1; N + 1 -> 2 (for N is assigned as global variable or a variable

    that’s only assigned once)

from __future__ import print_function, division, absolute_import

class numba.ast_constant_folding.ConstantExprRecognizer(const_name_set)

Bases: ast.NodeVisitor

generic_visit(node)
visit_BinOp(node)
visit_BoolOp(node)
visit_Compare(node)
class numba.ast_constant_folding.ConstantFolder(*args, **kws)

Bases: numba.visitors.NumbaTransformer

Perform constant folding on AST.

NOTE: Forbids assignment to True, False, None.

eval_binary_operation(op, left, right)

Evaluate the constant expression and return a ast.Num instance containing the result.

is_constant(node)
valueof(node)

Get constant value from AST node.

visit_Assign(node)

Store the rhs value so we can inline them in future reference.

TODO: Remove assignment of constant

visit_BinOp(node)
visit_BoolOp(node)
visit_Compare(node)
visit_Name(node)
class numba.ast_constant_folding.ConstantMarker(*args, **kws)

Bases: numba.visitors.NumbaVisitor

A conservative constant marker. Conservative because we handle the simplest cases only.

get_constants()

Return a set of constant variable names

visit_Assign(node)
visit_AugAssign(node)
visit_For(node)
exception numba.ast_constant_folding.NotConstExprError

Bases: exceptions.Exception

numba.ast_constant_folding.is_constant(node, constants=set([]))
numba.ast_constant_folding.is_simple_value(value)

astsix Module

class numba.astsix.AST3to2

Bases: ast.NodeTransformer

generic_visit(node)
visit_Index(node)
visit_Raise(node)
visit_With(node)

Rewrite the With statement.

Python < 3.3:

With(expr context_expr, expr? optional_vars, stmt* body)

Python 3.3:

With(withitem* items, stmt* body) withitem = (expr context_expr, expr? optional_vars)
visit_arguments(node)
class numba.astsix.Raise

Bases: _ast.stmt

Py2 compatible Raise node

class numba.astsix.With

Bases: _ast.stmt

Node for AST compatibility with python 3.3.

numba.astsix.iter_fields(node)

Yield a tuple of (fieldname, value) for each field in node._fields that is present on node.

closures Module

This module provides support for closures and inner functions.

@autojit def outer():

a = 10 # this is a cellvar

@jit(‘void()’) def inner():

print a # this is a freevar

inner() a = 12 return inner

The ‘inner’ function closes over the outer scope. Each function with cellvars packs them into a heap-allocated structure, the closure scope.

The closure scope is passed into ‘inner’ when called from within outer.

The execution of ‘def’ creates a NumbaFunction, which has itself as the
m_self attribute. So when ‘inner’ is invoked from Python, the numba wrapper function gets called with NumbaFunction object and the args tuple. The closure scope is then set in NumbaFunction.func_closure.

The closure scope is an extension type with the cellvars as attributes. Closure scopes are chained together, since multiple inner scopes may need to share a single outer scope. E.g.

def outer(a):
def inner(b):
def inner_inner():
print a, b

return inner_inner

return inner(1), inner(2)

We have three live closure scopes here:

scope_outer = { ‘a’: a } # call to ‘outer’ scope_inner_1 = { ‘scope_outer’: scope_outer, ‘b’: 1 } # call to ‘inner’ with b=1 scope_inner_2 = { ‘scope_outer’: scope_outer, ‘b’: 2 } # call to ‘inner’ with b=2

Function ‘inner_inner’ defines no new scope, since it contains no cellvars. But it does contain a freevar from scope_outer and scope_inner, so it gets scope_inner passed as first argument. scope_inner has a reference to scope outer, so all variables can be resolved.

These scopes are instances of a numba extension class.

class numba.closures.ClosureSpecializer(*args, **kwargs)

Bases: numba.closures.ClosureTransformer

Lowering of closure instantiation and calling.

  • Instantiates the closure scope and makes the necessary assignments
  • Rewrites local variable accesses to accesses on the instantiated scope
  • Instantiate function with closure scope

Also rewrite calls to closures.

create_numba_function(node, func_env)
get_qname(closure_node)
retrieve_closure_from_numbafunc(node)

Retrieve the closure scope from ((NumbaFunctionObject *) numba_func).func_closure

visit_ClosureCallNode(node)
visit_ClosureNode(node)

Compile the inner function.

visit_ClosureScopeLoadNode(node)
visit_InstantiateClosureScope(node)

Instantiate a closure scope.

After instantiation, assign the parent scope and all function arguments that belong in the scope to the scope.

visit_Name(node)

Resolve cellvars and freevars

class numba.closures.ClosureTransformer(context, func, ast, locals=None, func_signature=None, nopython=0, symtab=None, **kwargs)

Bases: numba.visitors.NumbaTransformer

outer_scope
class numba.closures.ClosureTypeInferer(*args, **kwargs)

Bases: numba.closures.ClosureTransformer

Runs just after type inference after the outer variables types are resolved.

  1. run type inferencer on inner functions
  2. build scope extension types pre-order
  3. generate nodes to instantiate scope extension type at call time
update_closures(func_def, scope_type, ext_type)

Patch closures to get the closure scope as the first argument.

visit_FunctionDef(node)
numba.closures.assert_constant(visit_func, decorator, result_node)
numba.closures.check_signature_decorator(visit_func, decorator)
numba.closures.check_valid_argtype(argtype_node, argtype)
numba.closures.err_decorator(decorator)
numba.closures.get_locals(symtab)
numba.closures.handle_jit_decorator(visit_func, func_def, decorator)
numba.closures.is_closure_signature(func_signature)
numba.closures.lookup_scope_attribute(cur_scope, var_name, ctx=None)

Look up a variable in the closure scope

numba.closures.outer_scope_field(scope_type)
numba.closures.parse_argtypes(visit_func, decorator, func_def, jit_args)
numba.closures.parse_restype(visit_func, decorator, jit_args)
numba.closures.process_closures(env, outer_func_def, outer_symtab, **kwds)

Process closures recursively and for each variable in each function determine whether it is a freevar, a cellvar, a local or otherwise.

numba.closures.process_decorators(env, visit_func, node)

config Module

class numba.config.Config

Bases: object

colour = True
terminal_background = 'dark'

decorators Module

numba.decorators.autojit(template_signature=None, backend='ast', target='cpu', nopython=False, locals=None, **kwargs)

Creates a function that dispatches to type-specialized LLVM functions based on the input argument types. If no specialized function exists for a set of input argument types, the dispatcher creates and caches a new specialized function at call time.

numba.decorators.jit(restype=None, argtypes=None, backend='ast', target='cpu', nopython=False, **kws)

Compile a function given the input and return types.

There are multiple ways to specify the type signature:

  • Using the restype and argtypes arguments, passing Numba types.
  • By constructing a Numba function type and passing that as the first argument to the decorator. You can create a function type by calling an exisiting Numba type, which is the return type, and the arguments to that call define the argument types. For example, f8(f8) would create a Numba function type that takes a single double-precision floating point value argument, and returns a double-precision floating point value.
  • As above, but using a string instead of a constructed function type. Example: jit("f8(f8)").

If backend=’bytecode’ the bytecode translator is used, if backend=’ast’ the AST translator is used. By default, the AST translator is used. Note that the bytecode translator is deprecated as of the 0.3 release.

numba.decorators.export(signature, env_name=None, env=None, **kws)

Construct a decorator that takes a function and exports one

A signature is a string with

name ret_type(arg_type, argtype, ...)

numba.decorators.exportmany(signatures, env_name=None, env=None, **kws)

A Decorator that exports many signatures for a single function

environment Module

class numba.environment.FunctionEnvironment(*args, **kws)

Bases: object

State for a function under translation.

annotate

Whether we need to annotate the source

annotations

Annotation dict { lineno : Annotation }

array

Array abstraction

ast

Abstract syntax tree for the function being translated.

ast_metadata

Metadata for AST nodes of the function being compiled.

cfdirectives

Directives for control flow.

cfg_transform

The Control Flow Analysis transform object (control_flow.ControlFlowAnalysis). Set during the cfg pass.

closure_scope

Collective symtol table containing all entries from outer functions.

closures

Map from ast nodes to closures.

error_env

Error environment for this function.

flow

Control flow graph. See numba.control_flow.

func

Function (or similar) being translated.

func_doc
func_name

Target function name.

func_signature

Type signature for the function being translated.

function_globals

Globals dict of the function

getstate()
inherit(**kwds)

Inherit from a parent FunctionEnvironment (e.g. to run pipeline stages on a subset of the AST).

init(parent, func, ast, func_signature, name=None, qualified_name=None, mangled_name=None, llvm_module=None, wrap=True, link=True, symtab=None, error_env=None, function_globals=None, locals=None, template_signature=None, is_closure=False, closures=None, closure_scope=None, refcount_args=True, ast_metadata=None, warn=True, warnstyle='fancy', typesystem=None, array=None, postpasses=None, annotate=False, **kws)
intermediates

list of Intermediate objects for annotation

is_closure

Flag indicating if the current function under translation is a closure or not.

is_partial

Whether this environment is a partially constructed environment

kill_attribute_assignments

Assignments to attributes that need to be removed from type inference pre-analysis. We need to do this for prange since we need to infer the types of variables to build a struct type for those variables. So we need type inference to be properly ordered, and not look at the attributes first.

kwargs

Additional keyword arguments. Deprecated, but kept for backward compatibility.

lfunc

Compiled, native, Numba function

lfunc_pointer

Pointer to underlying compiled function. Can be used as a callback.

Flag indicating whether the LLVM function needs to be linked into the global fast module from LLVMContextManager

llvm_module

LLVM module for this function. This module is first optimized and then linked into a global module. The Python wrapper function goes directly into the main fat module.

llvm_wrapper_func

The LLVM wrapper function for the target function. This is a wrapper function that accept python object arguments and returns an object.

locals

A map from local variable names to types. Used to handle the locals keyword argument to the autojit decorator. ({ “local_var_name” : local_var_type } for @autojit(locals=...))

mangled_name

Mangled name of compiled function.

module_name

Name of the function module.

need_closure_wrapper

Whether this closure needs a Python wrapper function

numba

Grandparent environment (top-level Numba environment).

numba_wrapper_func

The Numba wrapper function (see numbafunction.c) for the target function. This is a wrapper function that accept python object arguments and returns an object.

postpasses

List of passes that should run on the final llvm ir before linking

qualified_name

Target qualified function name (‘mymodule.myfunc’)

refcount_args

Whether to use refcounting for the function arguments

symtab

A map from local variable names to symbol table variables for all local variables. ({ “local_var_name” : numba.symtab.Variable(local_var_type) })

template_signature

Template signature for @autojit. E.g. T(T[:, :]). See numba.typesystem.templatetypes.

translator

The code generator instance used to generate the target LLVM function. Set during the code generation pass, and used for after-the-fact wrapper generation.

typesystem

Typesystem for this compilation

warn

Flag that enables control flow warnings on a per-function level.

warnstyle

Warning style, currently available: simple, fancy

wrap

Flag indicating whether the function needs a wrapper function to be callable from Python.

class numba.environment.FunctionErrorEnvironment(func, ast, warnstyle)

Bases: object

Environment for errors or warnings that occurr during translation of a function.

ast

Original Abstract Syntax Tree for the function being translated.

collection

Collection of error and warning messages

enable_post_mortem

Enable post-mortem debugging for the Numba compiler

func

Function (or similar) being translated.

merge_in(parent_error_env)

Merge error messages into another error environment. Useful to propagate error messages for inner functions outwards.

source

Function source code

warning_styles = {'simple': <class 'numba.reporting.MessageCollection'>, 'fancy': <class 'numba.reporting.FancyMessageCollection'>}
class numba.environment.NumbaEnvironment(name, *args, **kws)

Bases: numba.environment._AbstractNumbaEnvironment

Defines global state for a Numba translator.

class FunctionEnvironment(*args, **kws)

Bases: object

State for a function under translation.

annotate

Whether we need to annotate the source

annotations

Annotation dict { lineno : Annotation }

array

Array abstraction

ast

Abstract syntax tree for the function being translated.

ast_metadata

Metadata for AST nodes of the function being compiled.

cfdirectives

Directives for control flow.

cfg_transform

The Control Flow Analysis transform object (control_flow.ControlFlowAnalysis). Set during the cfg pass.

closure_scope

Collective symtol table containing all entries from outer functions.

closures

Map from ast nodes to closures.

error_env

Error environment for this function.

flow

Control flow graph. See numba.control_flow.

func

Function (or similar) being translated.

func_doc
func_name

Target function name.

func_signature

Type signature for the function being translated.

function_globals

Globals dict of the function

getstate()
inherit(**kwds)

Inherit from a parent FunctionEnvironment (e.g. to run pipeline stages on a subset of the AST).

init(parent, func, ast, func_signature, name=None, qualified_name=None, mangled_name=None, llvm_module=None, wrap=True, link=True, symtab=None, error_env=None, function_globals=None, locals=None, template_signature=None, is_closure=False, closures=None, closure_scope=None, refcount_args=True, ast_metadata=None, warn=True, warnstyle='fancy', typesystem=None, array=None, postpasses=None, annotate=False, **kws)
intermediates

list of Intermediate objects for annotation

is_closure

Flag indicating if the current function under translation is a closure or not.

is_partial

Whether this environment is a partially constructed environment

kill_attribute_assignments

Assignments to attributes that need to be removed from type inference pre-analysis. We need to do this for prange since we need to infer the types of variables to build a struct type for those variables. So we need type inference to be properly ordered, and not look at the attributes first.

kwargs

Additional keyword arguments. Deprecated, but kept for backward compatibility.

lfunc

Compiled, native, Numba function

lfunc_pointer

Pointer to underlying compiled function. Can be used as a callback.

Flag indicating whether the LLVM function needs to be linked into the global fast module from LLVMContextManager

llvm_module

LLVM module for this function. This module is first optimized and then linked into a global module. The Python wrapper function goes directly into the main fat module.

llvm_wrapper_func

The LLVM wrapper function for the target function. This is a wrapper function that accept python object arguments and returns an object.

locals

A map from local variable names to types. Used to handle the locals keyword argument to the autojit decorator. ({ “local_var_name” : local_var_type } for @autojit(locals=...))

mangled_name

Mangled name of compiled function.

module_name

Name of the function module.

need_closure_wrapper

Whether this closure needs a Python wrapper function

numba

Grandparent environment (top-level Numba environment).

numba_wrapper_func

The Numba wrapper function (see numbafunction.c) for the target function. This is a wrapper function that accept python object arguments and returns an object.

postpasses

List of passes that should run on the final llvm ir before linking

qualified_name

Target qualified function name (‘mymodule.myfunc’)

refcount_args

Whether to use refcounting for the function arguments

symtab

A map from local variable names to symbol table variables for all local variables. ({ “local_var_name” : numba.symtab.Variable(local_var_type) })

template_signature

Template signature for @autojit. E.g. T(T[:, :]). See numba.typesystem.templatetypes.

translator

The code generator instance used to generate the target LLVM function. Set during the code generation pass, and used for after-the-fact wrapper generation.

typesystem

Typesystem for this compilation

warn

Flag that enables control flow warnings on a per-function level.

warnstyle

Warning style, currently available: simple, fancy

wrap

Flag indicating whether the function needs a wrapper function to be callable from Python.

class NumbaEnvironment.TranslationContext(env, *args, **kws)

Bases: object

Context manager for handling a translation. Pushes a FunctionEnvironment input onto the given translation environment’s stack, and pops it when leaving the translation context.

class NumbaEnvironment.TranslationEnvironment(parent, **kws)

Bases: object

State for a given translation.

allow_rebind_args

Flag indicating whether the type of arguments may be overridden for @jit functions. This is always true (except for in tests perhaps!)

crnt

The environment corresponding to the current function under translation.

func_envs

Map from root AST nodes to FunctionEnvironment objects.This allows tracking environments of partially processed ASTs.

functions

A map from target function names that are under compilation to their corresponding FunctionEnvironments

get_env(ast)
get_or_make_env(func, ast, func_signature, **kwds)
is_pycc

Flag that tells us whether this function is being exported with pycc.

make_partial_env(ast, **kwds)

Create a partial environment for a function that only initializes the given attributes.

Later attributes will override existing attributes.

nopython

Flag used to indicate if calls to the Python C API are permitted or not during code generation.

numba

Parent environment

pop()
push(func, ast, func_signature, **kws)
push_env(func_env, **kws)
set_flags(**kws)
stack

A stack consisting of FunctionEnvironment instances. Used to manage lexical closures.

warn

Flag that enables control flow warnings. FunctionEnvironment inherits this unless overridden.

NumbaEnvironment.annotation_blocks

List of annotation information for different functions.

NumbaEnvironment.cmdopts

Dict of command line options from bin/numba.py

NumbaEnvironment.constants_manager

Holds constant values in an LLVM module.

NumbaEnvironment.context

Defines a global typing context for handling promotions and type representations.

NumbaEnvironment.crnt
NumbaEnvironment.debug

Global flag indicating verbose debugging output should be enabled.

NumbaEnvironment.debug_coercions

Flag for checking type coercions done during late specialization.

NumbaEnvironment.default_pipeline

Default entry point name. Used to index into the “pipelines” map.

NumbaEnvironment.environment_map = {None: NumbaEnvironment(numba)}
NumbaEnvironment.exports

Translation environment for pycc usage

classmethod NumbaEnvironment.get_environment(environment_key=None, *args, **kws)

Given an optional key, return the global Numba environment for that key. If no key is given, return the default global environment.

Note that internally, the default environment is mapped to None.

NumbaEnvironment.get_or_add_pipeline(pipeline_name=None, pipeline_ctor=None)
NumbaEnvironment.get_pipeline(pipeline_name=None)

Convenience function for getting a pipeline object (which should be a callable object that accepts (ast, env) arguments, and returns an ast).

NumbaEnvironment.llvm_context

Manages the global LLVM module and linkages of new translations.

NumbaEnvironment.name

Name of the environment.

NumbaEnvironment.pipeline_stages

Namespace for pipeline stages. Initially set to the numba.pipeline module.

NumbaEnvironment.pipelines

Map from entry point names to PipelineStages.

NumbaEnvironment.specializations

Cache for previously specialized functions.

NumbaEnvironment.stage_checks

Global flag for enabling detailed checks in translation pipeline stages.

NumbaEnvironment.translation

Current translation environment, specific to the current pipeline being run.

class numba.environment.PyccEnvironment(wrap_exports=False, *args, **kws)

Bases: object

pycc environment

Includes flags, and modules for exported functions.

function_module_map

Map from function names to LLVM modules that define the translated function.

function_signature_map

Map from function names to type signatures for the translated function (used for header generation).

function_wrapper_map

Map from function names to tuples containing LLVM wrapper functions and LLVM modules that define the wrapper function.

reset(wrap_exports=False, *args, **kws)

Clear the current set of exported functions.

wrap_exports

Boolean flag used to indicate that Python wrappers should be generated for exported functions.

class numba.environment.TranslationContext(env, *args, **kws)

Bases: object

Context manager for handling a translation. Pushes a FunctionEnvironment input onto the given translation environment’s stack, and pops it when leaving the translation context.

class numba.environment.TranslationEnvironment(parent, **kws)

Bases: object

State for a given translation.

allow_rebind_args

Flag indicating whether the type of arguments may be overridden for @jit functions. This is always true (except for in tests perhaps!)

crnt

The environment corresponding to the current function under translation.

func_envs

Map from root AST nodes to FunctionEnvironment objects.This allows tracking environments of partially processed ASTs.

functions

A map from target function names that are under compilation to their corresponding FunctionEnvironments

get_env(ast)
get_or_make_env(func, ast, func_signature, **kwds)
is_pycc

Flag that tells us whether this function is being exported with pycc.

make_partial_env(ast, **kwds)

Create a partial environment for a function that only initializes the given attributes.

Later attributes will override existing attributes.

nopython

Flag used to indicate if calls to the Python C API are permitted or not during code generation.

numba

Parent environment

pop()
push(func, ast, func_signature, **kws)
push_env(func_env, **kws)
set_flags(**kws)
stack

A stack consisting of FunctionEnvironment instances. Used to manage lexical closures.

warn

Flag that enables control flow warnings. FunctionEnvironment inherits this unless overridden.

numba.environment.insert_stage(pipeline_order, stage, after=None, before=None)
numba.environment.main(*args)
numba.environment.upfr(order, x)
numba.environment.upto(order, x)

error Module

exception numba.error.NumbaError(node, msg=None, *args, **kwds)

Bases: exceptions.Exception

Some error happened during compilation

exception numba.error.InternalError

Bases: exceptions.Exception

Indicates a compiler bug

exception numba.error.InvalidTemplateError

Bases: exceptions.Exception

Raised for invalid template type specifications

function_util Module

numba.function_util.external_call(context, llvm_module, name, args=(), temp_name=None)
numba.function_util.external_call_func(context, llvm_module, extfn, args=(), temp_name=None)

Build a call node to the specified external function.

context — A numba context llvm_module — A LLVM llvm_module name — Name of the external function args — [optional] argument of for the call temp_name — [optional] Name of the temporary value in LLVM IR.

numba.function_util.utility_call(context, llvm_module, name, args=(), temp_name=None)

functions Module

class numba.functions.FunctionCache(context=None, env=None)

Bases: object

Cache for compiler functions, declared external functions and constants.

get_autojit_cache(py_func)

Get the numbawrapper.AutojitFunctionCache that does a quick lookup for the cached case.

get_function(py_func, argtypes, flags)

Get a compiled function in the the function cache. The function must not be an external function.

For an external function, is_registered() must return False.

is_registered(func)

Check if a function is registered to the FunctionCache instance.

register(func)

Register a function to the FunctionCache.

It is necessary before calling compile_function().

register_specialization(func_env)
numba.functions.fix_ast_lineno(tree)
numba.functions.keep_alive(py_func, obj)

Keep an object alive for the lifetime of the translated unit.

This is a HACK. Make live objects part of the function-cache

NOTE: py_func may be None, so we can’t make it a function attribute

lexing Module

numba.lexing.lex_source(code, lexer='python', output='html', inline_css=True)
>>> lex_source("print 'hello world'", "python", "html")
<div ...> ... </div>

llvm_types Module

llvm_types

Utility module containing common (to Numba) LLVM types.

numba.llvm_types.constant_int(value, type=<llvm.core.IntegerType object at 0x1050bd910>)
numba.llvm_types.to_llvm(type)

macros Module

numba.macros.c_string_slice_1(context, builder, c_string, lb)
numba.macros.c_string_slice_2(context, builder, c_string, lb, ub=None)

metadata Module

Hold metadata for instructions.

class numba.metadata.TBAAMetadata(module)

Bases: object

Type Based Alias Analysis metadata. This defines a type tree where types in different branches are known not to alias each other. Only the ancestors of a type node alias that node.

find_root(type)

Find the metadata root of a type. E.g. if we have tbaa(sometype).pointer(), we want to return tbaa(sometype).root.pointer()

get_metadata(type, typeroot=None)
get_string(string)
initialize()
make_metadata(typename, root, is_constant=False)
make_unique_metadata(root, is_constant=False)
set_metadata(load_instr, metadata)
set_tbaa(load_instr, type)
numba.metadata.is_tbaa_type(type)
numba.metadata.typename(type)

Get the TBAA type name

missing Module

class numba.missing.FixMissingLocations(lineno, col_offset, override=False)

Bases: ast.NodeVisitor

Fix missing source position information.

visit(node)

multiarray_api Module

multiarray_api

Defines a utility class for generating LLVM code that retrieves values out of the Numpy array C API PyCObject/capsule.

class numba.multiarray_api.MultiarrayAPI(include_source_path=None)

Bases: object

classmethod c_ty_str_to_llvm(c_ty_str)
calculate_api_addr()
classmethod non_fn_ty_to_llvm(c_ty_str)
set_PyArray_API(module)

Adds PyArray_API as a global variable to the input LLVM module.

naming Module

numba.naming.specialized_mangle(func_name, types)
numba.naming.type_mangle(*types)

ndarray_helpers Module

class numba.ndarray_helpers.Array

Bases: object

Interface for foreign arrays, like LLArray

data

Return the data pointer of this array (for A.data)

from_type(llvm_dtype)

Given an LLVM representation of the dtype, return the LLVM array type representation

getptr(*indices)

Compute an element pointer given LLVM indices into the array

ndim

Return the dimensionality of this array as an LLVM constant

shape

Return the extents as a list of loaded LLVM values

shape_ptr

Return the shape pointer of this array (for A.shape[0], etc)

strides

Return the strides as a list of loaded LLVM values

strides_ptr

Return the strides pointer of this array (for A.strides[0], etc)

class numba.ndarray_helpers.NumpyArray(pyarray_ptr, builder, tbaa=None, type=None)

Bases: numba.ndarray_helpers.Array

LLArray compatible inferface for NumPy’s ndarray

data
classmethod from_type(llvm_dtype)
getptr(*indices)
itemsize
ndim
preload(ptr, count=None)
shape
shape_ptr
strides
strides_ptr
class numba.ndarray_helpers.PyArrayAccessor(builder, pyarray_ptr, tbaa=None, dtype=None)

Bases: object

Convenient access to a the native fields of a NumPy array.

builder: llvmpy IRBuilder pyarray_ptr: pointer to the numpy array tbaa: metadata.TBAAMetadata instance

base
data
descr
dimensions
flags
get_data()
ndim
set_data(value)
shape
strides
typed_data(context)
numba.ndarray_helpers.load_at(builder, ptr, idx)
numba.ndarray_helpers.make_property(type=None, invariant=True)

type: The type to be used for TBAA annotation

numba.ndarray_helpers.ptr_at(builder, ptr, idx)
numba.ndarray_helpers.set_metadata(tbaa, instr, type)
numba.ndarray_helpers.store_at(builder, ptr, idx, val)

normalize Module

Initial AST validation and normalization.

class numba.normalize.FuncDefExprNode(**kwargs)

Bases: numba.nodes.basenodes.Node

Wraps an inner function node until the closure code kicks in.

class numba.normalize.NormalizeAST(context, func, ast, locals=None, func_signature=None, nopython=0, symtab=None, **kwargs)

Bases: numba.visitors.NumbaTransformer

Normalize AST

DISABLED_visit_Compare(node)

Reduce cascaded comparisons into single comparisons

function_level = 0
handle_inner_function(node)

Create assignment code for inner functions and mark the assignment

ir = <module 'numba.nodes' from '/Users/jayvius/anaconda/lib/python2.7/site-packages/numba/nodes/__init__.pyc'>
visit_AugAssign(node)

Inplace assignment.

Resolve a += b to a = a + b. Set ‘inplace_op’ attribute of the Assign node so later stages may recognize inplace assignment.

Do this now, so that we can correctly mark the RHS reference.

visit_FunctionDef(node)
visit_ListComp(node)

Rewrite list comprehensions to the equivalent for loops.

AST syntax:

ListComp(expr elt, comprehension* generators) comprehension = (expr target, expr iter, expr* ifs)

‘ifs’ represent a chain of ANDs

odict Module

class numba.odict.OrderedDict(*args, **kwds)

Bases: dict, UserDict.DictMixin

clear()
copy()
classmethod fromkeys(iterable, value=None)
items()
iteritems()
iterkeys()
itervalues()
keys()
pop(key, *args)
popitem(last=True)
setdefault(key, default=None)
update(other=None, **kwargs)
values()

oset Module

Ordered Set. See http://code.activestate.com/recipes/576694/

class numba.oset.OrderedSet(iterable=None)

Bases: _abcoll.MutableSet

add(key)
discard(key)
pop(last=True)
update(it)

parallel Module

Support for

for i in numba.prange(...):
...

The implementation isn’t particularly good, and should be greatly simplified at some point.

numba.parallel.InvokeAndJoinThreads

alias of UserNode

class numba.parallel.PrangeCleanup(context, func, ast, locals=None, func_signature=None, nopython=0, symtab=None, **kwargs)

Bases: numba.visitors.NumbaTransformer

Clean up outlined prange loops after type inference (removes them entirely).

visit_For(node)
class numba.parallel.PrangeExpander(context, func, ast, locals=None, func_signature=None, nopython=0, symtab=None, **kwargs)

Bases: numba.visitors.NumbaTransformer

Rewrite ‘for i in prange(...): ...’ before the control flow pass.

error_check_prange(node)
is_numba_prange(node)
match_global(node, expected_value)
prange = 0
visit_Call(node)
visit_For(node)
visit_FunctionDef(node)
class numba.parallel.PrangeNode(start, stop, step, **kwargs)

Bases: numba.nodes.basenodes.ExprNode

Prange node. This replaces the For loop iterator in the initial stage. After type inference and before closure type inference it replaces the entire loop.

func_def = None
num_threads_node = None
privates = None
privates_struct_type = None
reductions = None
target = None
template_vars = None
class numba.parallel.PrangePrivatesReplacer(context, func, ast, locals=None, func_signature=None, nopython=0, symtab=None, **kwargs)

Bases: numba.visitors.NumbaTransformer

Rewrite private variables to accesses on the privates before closure type inference (closure type inference of the outer function, and type inference (and control flow analysis) of the inner function).

in_prange_closure = 0
visit_Call(node)
visit_FunctionDef(node)

Analyse immedidate prange functions (not ones in closures).

Don’t re-analyze prange functions when the prange function closures themselves are compiled.

visit_Name(node)
class numba.parallel.PrangeType(*args, **kwds)

Bases: numba.typesystem.types.NumbaType

conser = <numba.typesystem.itypesystem.Conser object at 0x108ecdb90>
defaults = {}
is_prange = True
is_range = True
numba.parallel.TypeofNode

alias of UserNode

class numba.parallel.VariableFindingVisitor

Bases: numba.visitors.VariableFindingVisitor

Find referenced and assigned ast.Name nodes

register_assignment(node, target, op)
visit_Assign(node)
numba.parallel.VariableTypeInferingNode

alias of UserNode

numba.parallel.assign(name, expr)
numba.parallel.create_prange_closure(env, prange_node, body, target)
numba.parallel.get_reduction_default(op)
numba.parallel.get_reduction_op(op)
numba.parallel.get_threadpool_funcs(context, context_struct_type, target_name, lfunc, lfunc_pointer, signature, num_threads, llvm_module)

Get functions to run the closure in separate threads.

context:
the Numba/Minivect context
context_struct_type:
the struct type holding all private and reduction variables
numba.parallel.kill_attribute_assignments(env, prange_node, temporaries)

Remove attribute assignments from the list of statements that need to be resolved before type inference.

numba.parallel.make_privates_struct_type(privates_struct_type, names)

Update the struct of privates and reductions once we know the field types.

numba.parallel.perform_reductions(context, prange_node)
numba.parallel.prange(start=0, stop=None, step=1)
numba.parallel.rewrite_prange(env, prange_node, target, locals_dict, closures_dict)
numba.parallel.typeof(name, expr)

pipeline Module

This module contains the Pipeline class which provides a pluggable way to define the transformations and the order in which they run on the AST.

class numba.pipeline.AST3to2

Bases: numba.pipeline.PipelineStage

transform(ast, env)
numba.pipeline.CleanupPrange(ast, env)
class numba.pipeline.ClosureTypeInference

Bases: numba.pipeline.PipelineStage

transform(ast, env)
class numba.pipeline.CodeGen

Bases: numba.pipeline.PipelineStage

transform(ast, env)
class numba.pipeline.ComposedPipelineStage(stages=None)

Bases: numba.pipeline.PipelineStage

static check_stage(stage)
classmethod compose(stage0, stage1)
is_composed = True
transform(ast, env)
class numba.pipeline.ConstFolding

Bases: numba.pipeline.PipelineStage

check_postconditions(ast, env)
check_preconditions(ast, env)
transform(ast, env)
class numba.pipeline.ControlFlowAnalysis

Bases: numba.pipeline.PipelineStage

check_preconditions(ast, env)
pre_condition_schema
transform(ast, env)
class numba.pipeline.ErrorReporting

Bases: numba.pipeline.PipelineStage

Sort and issue warnings and errors

transform(ast, env)
numba.pipeline.ExpandPrange(ast, env)
class numba.pipeline.ExtensionTypeLowerer

Bases: numba.pipeline.PipelineStage

transform(ast, env)
class numba.pipeline.FixASTLocations

Bases: numba.pipeline.PipelineStage

transform(ast, env)
class numba.pipeline.LateSpecializer

Bases: numba.pipeline.PipelineStage

transform(ast, env)
class numba.pipeline.LinkingStage

Bases: numba.pipeline.PipelineStage

Link the resulting LLVM function into the global fat module.

transform(ast, env)
class numba.pipeline.LowerRaise

Bases: numba.pipeline.PipelineStage

transform(ast, env)
class numba.pipeline.NormalizeASTStage

Bases: numba.pipeline.PipelineStage

transform(ast, env)
class numba.pipeline.Optimize

Bases: numba.pipeline.PipelineStage

transform(ast, env)
class numba.pipeline.PipelineStage

Bases: object

check_postconditions(ast, env)
check_preconditions(ast, env)
is_composed = False
make_specializer(cls, ast, env, **kws)
transform(ast, env)
class numba.pipeline.PostPass

Bases: numba.pipeline.PipelineStage

transform(ast, env)
class numba.pipeline.RewriteArrayExpressions

Bases: numba.pipeline.PipelineStage

transform(ast, env)
numba.pipeline.RewritePrangePrivates(ast, env)
class numba.pipeline.SimplePipelineStage

Bases: numba.pipeline.PipelineStage

transform(ast, env)
transformer = None
class numba.pipeline.Specialize

Bases: numba.pipeline.PipelineStage

transform(ast, env)
class numba.pipeline.SpecializeClosures

Bases: numba.pipeline.SimplePipelineStage

transformer

alias of ClosureSpecializer

class numba.pipeline.SpecializeComparisons

Bases: numba.pipeline.PipelineStage

transform(ast, env)
class numba.pipeline.SpecializeExceptions

Bases: numba.pipeline.PipelineStage

transform(ast, env)
class numba.pipeline.SpecializeFunccalls

Bases: numba.pipeline.PipelineStage

transform(ast, env)
class numba.pipeline.SpecializeLoops

Bases: numba.pipeline.PipelineStage

transform(ast, env)
class numba.pipeline.SpecializeSSA

Bases: numba.pipeline.PipelineStage

transform(ast, env)
class numba.pipeline.TransformBuiltinLoops

Bases: numba.pipeline.PipelineStage

transform(ast, env)
class numba.pipeline.TransformFor

Bases: numba.pipeline.PipelineStage

transform(ast, env)
class numba.pipeline.TypeInfer

Bases: numba.pipeline.PipelineStage

check_preconditions(ast, env)
transform(ast, env)
class numba.pipeline.TypeSet

Bases: numba.pipeline.PipelineStage

transform(ast, env)
class numba.pipeline.UpdateAttributeStatements

Bases: numba.pipeline.PipelineStage

transform(ast, env)
class numba.pipeline.ValidateASTStage

Bases: numba.pipeline.PipelineStage

transform(ast, env)
class numba.pipeline.WrapperStage

Bases: numba.pipeline.PipelineStage

Build a wrapper LLVM function around the compiled numba function to call it from Python.

transform(ast, env)
numba.pipeline.ast3to2(ast, env)
numba.pipeline.cleanup_symtab(ast, env)

Pop original variables from the symtab

numba.pipeline.compile2(env, func, restype=None, argtypes=None, ctypes=False, compile_only=False, func_ast=None, **kwds)

Compile a numba annotated function.

  • decompile function into a Python ast
  • run type inference using the given input types
  • compile the function to LLVM
numba.pipeline.create_lfunc(tree, env)

Update the FunctionEnvironment with an LLVM function if the signature is known (try this before type inference to support recursion).

numba.pipeline.create_lfunc1(tree, env)
numba.pipeline.create_lfunc2(tree, env)
numba.pipeline.create_lfunc3(tree, env)
numba.pipeline.get_lfunc(env, func_env)
numba.pipeline.get_locals(ast, locals_dict)
numba.pipeline.infer_types2(env, func, restype=None, argtypes=None, **kwargs)

Like run_pipeline, but takes restype and argtypes instead of a function

numba.pipeline.module_name(func)
numba.pipeline.resolve_templates(ast, env)
numba.pipeline.run_env(env, func_env, **kwargs)
numba.pipeline.run_pipeline2(env, func, func_ast, func_signature, pipeline=None, **kwargs)
numba.pipeline.run_prange(name)
numba.pipeline.update_signature(tree, env)
numba.pipeline.validate_arrays(ast, env)
numba.pipeline.validate_signature(tree, env)

postpasses Module

Postpasses over the LLVM IR. The signature of each postpass is postpass(env, ee, lmod, lfunc) -> lfunc

numba.math.* -> llvmmath.*

numba.postpasses.register_default(name)

prettyprint Module

Pretty printing of numba IRs.

numba.prettyprint.dump_annotations(ast, env)
numba.prettyprint.dump_ast(ast, env)
numba.prettyprint.dump_cfg(ast, env)
numba.prettyprint.dump_llvm(ast, env)
numba.prettyprint.dump_optimized(ast, env)
numba.prettyprint.dumppass(option)

reporting Module

Error reporting. Used by the CFA and by each FunctionEnvironment, which can collect errors and warnings and issue them after failed or successful compilation.

class numba.reporting.FancyMessageCollection(ast=None, source_lines=None, file=None)

Bases: numba.reporting.MessageCollection

footer()
header()
report_message(message, node, type)
class numba.reporting.MessageCollection(ast=None, source_lines=None, file=None)

Bases: object

Collect error/warnings messages first then sort

error(node, message)
footer()
header()
report(post_mortem=False)
report_message(message, node, type)
warning(node, message)
class numba.reporting.SourceDescr(func, ast)

Bases: object

Source code descriptor.

get_lines()
numba.reporting.format_msg(type, source_lines, node, msg)
numba.reporting.format_msg_simple(type, node, message)
numba.reporting.getpos(node)
numba.reporting.offset(source_lines)
numba.reporting.report(env, exc=None)
Parameters:
  • function_error_env – the FunctionErrorEnvironment
  • post_mortem – whether to enable post-mortem debugging of Numba
  • exc – currently propagating exception
numba.reporting.sort_message(collected_message)

scrape_multiarray_api Module

scrape_multiarray_api

Utilities for reading the __multiarray_api.h file, and scraping three things: symbolic names of API members, array indices for those members, and LLVM types for those members.

numba.scrape_multiarray_api.c_ty_str_to_llvm(c_ty_str)
numba.scrape_multiarray_api.gen_python(processed_source, template_str=None)
numba.scrape_multiarray_api.get_include()
numba.scrape_multiarray_api.joinlines(source_lines)

Remove backslashes appearing next to line breaks in a string and strip it after doing so.

numba.scrape_multiarray_api.main(*args, **kws)

Initial prototype for automatically generating multiarray C API call information for llvm-py. Not actually used to generate any Numba modules (dynamically handled by multiarray_api).

numba.scrape_multiarray_api.map_type(ty_str)
numba.scrape_multiarray_api.process_definition(source_defn)
numba.scrape_multiarray_api.process_source(source_file_path)
numba.scrape_multiarray_api.process_type(ty_str)

special Module

Special compiler-recognized numba functions and attributes.

numba.special.typeof(value)

Get the type of a variable or value.

Used outside of Numba code, infers the type for the object.

numba.special.addressof(obj, propagate=True)

Take the address of a compiled jit function.

Parameters:
  • obj – the jit function
  • write_unraisable – whether to write uncaught exceptions to stderr
  • propagate – whether to always propagate exceptions
Returns:

ctypes function pointer

numba.special.prange(start=0, stop=None, step=1)

stdio_util Module

numba.stdio_util.c_void_pp

alias of LP_c_void_p

numba.stdio_util.get_libc()
numba.stdio_util.get_stdio_streams()

Returns file pointers (FILE *) as Python integers for the C stdio stdin, stdout, and stderr streams.

numba.stdio_util.get_stream_as_node(fp)
numba.stdio_util.main()

symtab Module

class numba.symtab.Symtab(symtab_dict=None, parent=None)

Bases: object

lookup(name)
lookup_most_recent(name)

Look up the most recent definition of a variable in this block.

lookup_promotion(var_name, dst_type)
lookup_renamed(name, version)
rename(var, block, kills_previous_def=True)

Create a new renamed variable linked to the given variable, which becomes its parent.

renamed_name(name, count)
class numba.symtab.Variable(type, is_constant=False, is_local=False, is_global=False, is_builtin=False, name=None, lvalue=None, constant_value=None, promotable_type=True, is_arg=False)

Bases: object

Variables placed on the stack. They allow an indirection so, that when used in an operation, the correct LLVM type can be inserted.

Attributes:
type: the Numba type (see numba.typesystem) is_local/is_global/is_constant name: name of local or global lvalue: LLVM Value state: state passed from one stage to the next
ctypes_type

The ctypes type for the type of this variable.

deferred_type
classmethod from_variable(variable, **kwds)
init_array_flags()
is_builtin = False
is_cellvar
is_freevar
is_global = False
ltype

The LLVM type for the type of this variable or LLVM Value.

classmethod make_shared_property(name)
need_arg_copy
perform_assignment(rhs_type)

Called when an assignment is made to this variable.

unmangled_name
warn_unused = True

templating Module

String templating support. The following syntax is supported:

  1. Arbitrary Python code

  2. Placeholders for AST sub-tree substitutions:

    {{some_node}}

  3. Typed, untyped and code template variables:

    $some_var

    Typed and untyped variables are just artificial program (Python) variables.

    • Typed variables end up in the jit/autojit locals dict
    • Untyped variables are renameable/ssa variables
    • Code variables simply expand to arbitrary code
class numba.templating.Interpolate(substitutions, template_variables)

Bases: ast.NodeTransformer

Interpolate template substitutions.

substitutions: { var_name : substitution_node }

This is for {{var_name}} template syntax. This allows substituting arbitrary asts is specific places.

template_variables: [TemplateVariable(...)]

This is for $var syntax. This allows the introductions of typed or untyped variables, as well as code variables.
make_substitutions_clonenodes()
visit_Name(node)
class numba.templating.TempName

Bases: object

count()
temp_name(name)
class numba.templating.TemplateContext(context, template, env=None)

Bases: object

The context in which a template is evaluated. Allows defining template variables and a mechanism to merge those variables back into the function being compiled.

add_variable(var)

Add an external template $variable to this context

code_var(name)

Create a template $variable that expands to generated code statements

code_vars(*names)

Create a number of code $variables

get_vars_symtab()
string_substitute(s)
temp_var(name, type=None, code=False)

Create and add a new template $variable

temp_vars(*names)

Create a number of template $variables

template(substitutions)

Run a template and perform the given {{substitutions}}

template_type_infer(substitutions, **kwargs)
update_locals(locals_dict)

Update an external jit/autojit locals dict with our template $variables

class numba.templating.TemplateVariable(type, name, temp_name=None, code=False, **kwargs)

Bases: numba.symtab.Variable

A fake variable used in a template. The ‘code’ is substituted using string interpolation before the source is parsed. If the type is set, the symbol table is updated. The default ‘code’ is a temporary variable name.

node
numba.templating.dummy_type_infer(context, tree, order=['type_infer', 'type_set'], env=None, **kwargs)
numba.templating.temp_name(name='')
numba.templating.template(s, substitutions, template_variables=None)
numba.templating.template_simple(s, **substitutions)

threads Module

Implements threads using llvm cbuilder. Taken from numbapro/vectorizers/parallel.py

class numba.threads.PThreadAPI(cbuilder)

Bases: llvm_cbuilder.builder.CExternal

external declaration of pthread API

pthread_create = <llvm.core.FunctionType object at 0x1072809d0>
pthread_join = <llvm.core.FunctionType object at 0x108620c90>
pthread_t = <llvm.core.PointerType object at 0x105490ed0>
numba.threads.ParallelMixin

alias of ParallelUFuncPosixMixin

class numba.threads.ParallelUFuncPosixMixin

Bases: object

ParallelUFunc mixin that implements _dispatch_worker to use pthread.

class numba.threads.ParallelUFuncWindowsMixin

Bases: object

ParallelUFunc mixin that implements _dispatch_worker to use Windows threading.

class numba.threads.WinThreadAPI(cbuilder)

Bases: llvm_cbuilder.builder.CExternal

external declaration of pthread API

CloseHandle = <llvm.core.FunctionType object at 0x108ecde10>
CreateThread = <llvm.core.FunctionType object at 0x108ecdd50>
WaitForSingleObject = <llvm.core.FunctionType object at 0x108ecdcd0>
handle_t = <llvm.core.PointerType object at 0x105490ed0>

traits Module

Minimal traits implementation:

@traits class MyClass(object):

attr = Instance(SomeClass) my_delegation = Delegate(‘attr’)
numba.traits.Delegate

Delegate to some other object.

class numba.traits.TraitBase(value, doc=None)

Bases: object

Base class for traits

set_attr_name(name)
numba.traits.traits(cls)

@traits class decorator

transforms Module

This module provides a variety of transforms that transform the AST into a final form ready for code generation.

Below follows an explanation and justification of the design of the main compilation stages in numba.

We start with a Python AST, compiled from source code or decompiled from bytecode using meta. We run the following transformations:

  1. Type inference

    Infer types of all expressions, and fix the types of all local variables. Local variable types are promoted (for instance float to double), but cannot change (e.g. string cannot be assigned to float).

    When the type inferencer cannot determine a type, such as when it calls a Python function or method that is not a Numba function, it assumes type object. Object variables may be coerced to and from most native types.

    The type inferencer inserts CoercionNode nodes that perform such coercions, as well as coercions between promotable native types. It also resolves the return type of many math functions called in the numpy, math and cmath modules.

    Each AST expression node has a Variable that holds the type of the expression, as well as any meta-data such as constant values that have been determined.

  2. Transform for loops

    Provides some transformations of for loops over arrays to loops over a range. Iteration over range/xrange is resolved at compilation time.

    What I would like to see is the generation of a RangeNode holding a ast.Compare and an iteration variable incrementing ast.BinOp.

  3. Low level specializations (LateSpecializer)

    This stage performs low-level specializations. For instance it resolves coercions to and from object into calls such as PyFloat_FromDouble, with a fallback to Py_BuildValue/PyArg_ParseTuple.

    This specializer also has the responsibility to ensure that new references are accounted for by refcounting ObjectTempNode nodes. This node absorbs the references and lets parent nodes borrow the reference. At function cleanup, it decrefs its value. In loops, it also decrefs any previous value, if set. Hence, these temporaries must be initialized to NULL.

    An object temporary is specific to one specific sub-expression, and they are not reused (like in Cython).

    It also rewrites object attribute access and method calls into PyObject_GetAttrString etc.

  4. Code generation

    Generate LLVM code from the transformed AST.

    This should be as minimal as possible, and should not contain blobs of code performing complex operations. Instead, complex operations should be broken down by AST transformers into fundamental operations that are already supported by the AST.

    This way we maximize code reuse, and make potential future additions of different code generation backends easier. This can be taken only so far, since low-level transformations must also tailor to limitations of the code generation backend, such as intrinsic LLVM calls or calls into libc. However, code reuse is especially convenient in the face of type coercions, which LLVM does not provide any leniency for.

class numba.transforms.BuiltinResolver(env)

Bases: object

Perform final low-level transformations such as abs(value) -> fabs(value)

resolve_builtin_call(node, func)

Resolve an ast.Call() of a built-in function.

Returns None if no specific transformation is applied.

resolve_builtin_call_or_object(node, func)

Resolve an ast.Call() of a built-in function, or call the built-in through the object layer otherwise.

class numba.transforms.LateSpecializer(context, func, ast, locals=None, func_signature=None, nopython=0, symtab=None, **kwargs)

Bases: numba.transforms.ResolveCoercions, numba.visitors.NoPythonContextMixin

allocate_struct_on_stack(assmnt_node, target)
check_context(node)
setup_error_return(node, ret_type)

Set FunctionDef.error_return to the AST statement that returns a “bad value” that can be used as error indicator.

verify_context(ret_type)
visit_ArrayNewEmptyNode(node)
visit_ArrayNewNode(node)
visit_Assign(node)
visit_Attribute(node)
visit_BinOp(node)
visit_Call(node)
visit_ConstNode(node)
visit_ControlBlock(node)
visit_Dict(node)
visit_ExtSlice(node)
visit_For(node)
visit_FunctionDef(node)
visit_Index(node)
visit_List(node)
visit_Name(node)
visit_ObjectCallNode(node)
visit_Print(node)
visit_Return(node)
visit_Slice(node)

Rewrite slice objects. Do this late in the pipeline so that other code can still recognize the code structure.

visit_Subscript(node)
visit_Tuple(node)
visit_UnaryOp(node)
visit_UserNode(node)
visit_While(node)
class numba.transforms.ResolveCoercions(context, func, ast, locals=None, func_signature=None, nopython=0, symtab=None, **kwargs)

Bases: numba.visitors.NumbaTransformer

coerce_to_function_pointer(node, jit_func_type, func_pointer_type)
convert_int_to_object(arg)
object_to_int(node, dst_type)

Return node that converts the given node to the dst_type. This also performs overflow/underflow checking, and conversion to a Python int or long if necessary.

PyLong_AsLong and friends do not do this (overflow/underflow checking is only for longs, and conversion to int|long depends on the Python version).

str_to_int(dst_type, node)
visit_CoerceToNative(node)

Try to perform fast coercion using e.g. PyLong_AsLong(), with a fallback to PyArg_ParseTuple().

visit_CoerceToObject(node)
visit_CoercionNode(node)
numba.transforms.get_funcname(py_func)
numba.transforms.math_call(env, name, args, dst_type)
numba.transforms.math_call2(env, name, call_node)
numba.transforms.resolve_pow(env, restype, args)

typedefs Module

numba.typedefs.define(u)

ufunc_builder Module

class numba.ufunc_builder.UFuncBuilder

Bases: object

Create a Python ufunc AST function. Demote the types of arrays to scalars in the ufunc and generate a return.

build_ufunc_ast(tree)
compile_to_pyfunc(ufunc_ast, globals=())

Compile the ufunc ast to a function

register_operand(node)

Register a sub-expression as something that will be evaluated outside the kernel, and the result of which will be passed into the kernel. This can be a variable name:

a + b

->

f(arg1, arg2):
return arg1 + arg2

For which ‘a’ and ‘b’ are the operands.

restore(state)

Restore saved state

save()

Save the state of the builder to allow processing other parts of the tree.

ufunc_counter = 0
class numba.ufunc_builder.UFuncConverter(env)

Bases: ast.NodeTransformer

Convert a Python array expression AST to a scalar ufunc kernel by demoting array types to scalar types.

demote(type)
demote_type(node)
generic_visit(node)

Register Name etc as operands to the ufunc

visit_BinOp(node)
visit_Call(node)
visit_CoercionNode(node)
visit_UnaryOp(node)
visit_scalar_or_array(node)

utils Module

class numba.utils.NumbaContext

Bases: numba.minivect.miniast.LLVMContext

astbuilder_cls

alias of DynamicArgumentASTBuilder

init()
is_object(type)
optimize_broadcasting = False
shape_type = npy_intp *
strides_type = npy_intp *
class numba.utils.TypedProperty(ty, doc, default=None)

Bases: object

Defines a class property that does a type check in the setter.

deleter(obj)
getter(obj)
setter(obj, new_val)
class numba.utils.WriteOnceTypedProperty(ty, doc, default=None)

Bases: numba.utils.TypedProperty

setter(obj, *args, **kws)
numba.utils.ast2tree(node, include_attrs=True)

Transform a Python AST object into nested tuples and lists.

numba.utils.debugout(*args)

This is a magic function. If you use it in compiled functions, Numba should generate code for displaying the received value.

numba.utils.decode_filename(filename)
numba.utils.dump(node, *args, **kws)

Transform a Python AST object into nested tuples and lists, and pretty-print the result.

numba.utils.get_minivect_context()
numba.utils.hashable(x)
numba.utils.is_builtin(name)
numba.utils.itercode(code)

Return a generator of byte-offset, opcode, and argument from a byte-code-string

numba.utils.pformat_ast(node, include_attrs=True, **kws)

Transform a Python AST object into nested tuples and lists, and return as a string formatted using pprint.pformat().

numba.utils.process_sig(sigstr, name=None)
numba.utils.process_signature(sigstr, name=None)

Given a signature string consisting of a return type, argument types, and possibly a function name, return a signature object.

numba.utils.tree2ast(node, namespace)

Given an AST represented as tuples and lists, attempt to reconstruct the AST object, given a namespace that defines the node constructors.

validate Module

Initial AST validation and normalization.

class numba.validate.ValidateAST

Bases: ast.NodeVisitor

Validate AST

visit_DictComp(node)
visit_For(node)
visit_GeneratorExp(node)
visit_SetComp(node)
visit_With(node)

visitors Module

class numba.visitors.CooperativeBase(*args, **kwargs)

Bases: object

class numba.visitors.NoPythonContextMixin

Bases: object

visit_WithNoPythonNode(node, errorcheck=True)
visit_WithPythonNode(node, errorcheck=True)
class numba.visitors.NumbaTransformer(context, func, ast, locals=None, func_signature=None, nopython=0, symtab=None, **kwargs)

Bases: numba.visitors.NumbaVisitorMixin, ast.NodeTransformer

Mutating visitor

class numba.visitors.NumbaVisitor(context, func, ast, locals=None, func_signature=None, nopython=0, symtab=None, **kwargs)

Bases: ast.NodeVisitor, numba.visitors.NumbaVisitorMixin

Non-mutating visitor

visitlist(list)
class numba.visitors.NumbaVisitorMixin(context, func, ast, locals=None, func_signature=None, nopython=0, symtab=None, **kwargs)

Bases: numba.visitors.CooperativeBase

add_overload(visit_name, func)
annotate(node, key, value)
changed_block(old_block, new_block)

Callback for when a new cfg block is encountered.

current_env
deferred_error(node, msg)

Issue a deferred-terminating error

error(node, msg)

Issue a terminating error

func_level = 0
func_name
handle_phis(reversed=False)
have(t1, t2, p1, p2)

Return whether the two variables have the indicated properties:

>>> have(int_, float_, "is_float", "is_int")
float_

If true, returns the type indicated by the first property.

have_types(v1, v2, p1, p2)
invalidate_locals(ast=None)
is_closure_signature(func_signature)
is_complex(n)
is_int(n)
is_real(n)
keep_alive(obj)

Keep an object alive for the lifetime of the translated unit.

This is a HACK. Make live objects part of the function-cache

qualified_name
query(node, key)
run_template(s, vars=None, **substitutions)
setblock(cfg_basic_block)
valid_locals(func)
visit_CloneNode(node)
visit_ControlBlock(node)
visit_func_children(node)
visitlist(list)
warn(node, msg)

Issue a warning

class numba.visitors.VariableFindingVisitor(*args, **kwargs)

Bases: numba.visitors.NumbaVisitor

Find referenced and assigned ast.Name nodes

function_level = 0
visit_ClassDef(node)
visit_ClosureNode(node)
visit_FunctionDef(node)
visit_Global(node)
visit_Import(node)
visit_ImportFrom(node)
visit_Lambda(node)
visit_Name(node)
numba.visitors.determine_variable_status(env, ast, locals_dict)

Determine what category referenced and assignment variables fall in:

  • local variables
  • free variables
  • cell variables

Subpackages