How to use np.empty inside numba compiled function; Error message “All templates rejected”

对着背影说爱祢 提交于 2019-12-07 13:36:28

The problem is that np.float is not a valid datatype for a NumPy array in numba. You have to provide the explicit dtype to numba. This isn't just a problem with np.empty but also for other array-creation routines like np.ones, np.zeros, ... in numba.

To make your example work only a little change is needed:

from numba import jit
import numpy as np

@jit(nopython=True)
def empty():
    return np.empty(5, np.float64)  # np.float64 instead of np.float

empty()

Or the shorthand np.float_. Or if you want 32 bit floats use np.float32 instead.


Note that np.float is just an alias for the normal Python float and as such not a real NumPy dtype:

>>> np.float is float
True
>>> issubclass(np.float, np.generic)
False
>>> issubclass(np.float64, np.generic)
True

Likewise there are some additional aliases that just are interpreted as if they were NumPy dtypes (source):

Built-in Python types

Several python types are equivalent to a corresponding array scalar when used to generate a dtype object:

int          int_
bool         bool_
float        float_
complex      cfloat
bytes        bytes_
str          bytes_ (Python2) or unicode_ (Python3)
unicode      unicode_
buffer       void
(all others) object_

However numba doesn't know about these aliases and even when not dealing with numba you are probably better off using the real dtypes directly:

Array types and conversions between types

NumPy supports a much greater variety of numerical types than Python does. This section shows which are available, and how to modify an array’s data-type.

Data type     Description
bool_         Boolean (True or False) stored as a byte
int_          Default integer type (same as C long; normally either int64 or int32)
intc          Identical to C int (normally int32 or int64)
intp          Integer used for indexing (same as C ssize_t; normally either int32 or int64)
int8          Byte (-128 to 127)
int16         Integer (-32768 to 32767)
int32         Integer (-2147483648 to 2147483647)
int64         Integer (-9223372036854775808 to 9223372036854775807)
uint8         Unsigned integer (0 to 255)
uint16        Unsigned integer (0 to 65535)
uint32        Unsigned integer (0 to 4294967295)
uint64        Unsigned integer (0 to 18446744073709551615)
float_        Shorthand for float64.
float16       Half precision float: sign bit, 5 bits exponent, 10 bits mantissa
float32       Single precision float: sign bit, 8 bits exponent, 23 bits mantissa
float64       Double precision float: sign bit, 11 bits exponent, 52 bits mantissa
complex_      Shorthand for complex128.
complex64     Complex number, represented by two 32-bit floats (real and imaginary components)
complex128    Complex number, represented by two 64-bit floats (real and imaginary components)

Note that some of these are not supported by numba!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!