## Enhancements to Numba's guvectorize decorator

Published February 25, 2021

guilhermeleobas

Guilherme Leobas

Starting from Numba 0.53, Numba will ship with an enhanced version of the `@guvectorize`

decorator. Similar to the @vectorize decorator, @guvectorize now has two modes of operation:

- Eager, or decoration-time compilation and
- Lazy, or call-time compilation

Before, only the eager approach was supported. In this mode, users are required to provide a list of concrete supported types beforehand as its first argument. Now, this list can be omitted if desired and as one calls it, Numba dynamically generates new kernels for previously unsupported types.

## NumPy Universal Functions

NumPy has functions called Universal functions or ufuncs. Ufuncs are functions that operate on `ndarrays`

element-by-element. Examples of universal functions are `np.log`

and `np.log2`

, which compute the natural and base-2 logarithms, respectively. Alongside ufuncs, NumPy also has the notion of generalized ufuncs or gufuncs. While the former is limited to element-by-element operations, the latter supports subarray-by-subarray operations.

Creating new NumPy ufuncs is not an easy process and may require one to write some C code. Numba extends the NumPy mechanism for registering and using (generalized) universal functions with two decorators: `@vectorize`

and `@guvectorize`

. Those decorators allow one to easily create universal functions from Python, leaving the grunt work to Numba.

For instance, consider the function `guvec`

, which adds a scalar to every element in an array:

Notice that `guvectorize`

functions don't return their result value. Instead, they have to have the return array passed as an argument.

Previously, to use this decorator, one would have to declare the argument types in advance. One can inspect the supported types through the `.types`

property.

The commands above also show that `guvec`

is a NumPy ufunc and behaves like it. If one attempts to call it with non-supported argument types, it will fail with the following error message:

## Dynamic compilation

In Numba 0.53, one can omit the first argument to build a dynamic generalized universal function. For instance, consider the function `dyn_gufunc`

below:

As one makes calls to `dyn_guvec`

, new kernels will be generated for previously unsupported input types. The following set of interactions will illustrate how dynamic compilation works for a dynamic generalized ufunc:

If this was a normal guvectorize function, one would have seen an exception complaining that the gufunc could not handle the given input. One can add additional loops by calling `dyn_guvec`

with new types:

## Current limitations

In NumPy, it is fine to omit the output argument when calling a generalized ufunc.

The same is not possible in a dynamic ufunc. Numba would have to guess the output type and shape to correctly generate code based on the input and signature.

## Next steps

In the future we would like to bring the `@guvectorize`

capabilities closer to the `@vectorize`

ones. For instance, currently it is not possible to call a guvectorize function from a jitted (`@jit`

) function. Some work needs to be done in this direction.

We would like to thank the D. E. Shaw group for sponsoring this work. The D. E. Shaw group collaborates with Quansight on numerous open source projects, including Numba, Dask and Project Jupyter.