关于python:如何在numba编译函数中使用np.empty;

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

当我试图在用numba编译的函数定义中使用np.empty并打开nopython=True以确保优化类型有效时,遇到了这个奇怪的错误。

这很奇怪,因为numba声称用前两个参数支持np.empty,而我只使用前两个参数(我认为正确吗?)所以我不知道为什么打字不正确。

1
2
3
@jit(nopython=True)
def empty():
    return np.empty(5, np.float)

在ipython笔记本中定义了上述功能之后,

1
empty()

给出以下错误消息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
---------------------------------------------------------------------------
TypingError                               Traceback (most recent call last)
<ipython-input-88-927345c8757f> in <module>()
----> 1 empty()

~/.../lib/python3.5/site-packages/numba/dispatcher.py in _compile_for_args(self, *args, **kws)
    342                 raise e
    343             else:
--> 344                 reraise(type(e), e, None)
    345         except errors.UnsupportedError as e:
    346             # Something unsupported is present in the user code, add help info

~/.../lib/python3.5/site-packages/numba/six.py in reraise(tp, value, tb)
    656             value = tp()
    657         if value.__traceback__ is not tb:
--> 658             raise value.with_traceback(tb)
    659         raise value
    660
TypingError: Failed at nopython (nopython frontend)
Invalid usage of Function(<built-in function empty>) with parameters (int64, Function(<class 'float'>))
 * parameterized
In definition 0:
    All templates rejected
[1] During: resolving callee type: Function(<built-in function empty>)
[2] During: typing of call at <ipython-input-87-8c7e8fa4c6eb> (3)


File"<ipython-input-87-8c7e8fa4c6eb>", line 3:
def empty():
    return np.empty(5, np.float)
    ^

This is not usually a problem with Numba itself but instead often caused by
the use of unsupported features or an issue in resolving types.

To see Python/NumPy features supported by the latest release of Numba visit:
http://numba.pydata.org/numba-doc/dev/reference/pysupported.html
and
http://numba.pydata.org/numba-doc/dev/reference/numpysupported.html

For more information about typing errors and how to debug them visit:
http://numba.pydata.org/numba-doc/latest/user/troubleshoot.html#my-code-doesn-t-compile

If you think your code should work with Numba, please report the error message
and traceback, along with a minimal reproducer at:
https://github.com/numba/numba/issues/new

问题是,对于numba中的numpy数组,np.float不是有效的数据类型。您必须向numba提供显式的数据类型。这不仅是np.empty的问题,而且对于其他数组创建例程(如np.onesnp.zeros)……在NuMBA中。

要使您的示例工作,只需要进行一点更改:

1
2
3
4
5
6
7
8
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()

或者简写为np.float_。或者,如果您想要32位浮点型,请使用np.float32

注意,np.float只是普通python float的别名,因此不是真正的numpy数据类型:

1
2
3
4
5
6
>>> np.float is float
True
>>> issubclass(np.float, np.generic)
False
>>> issubclass(np.float64, np.generic)
True

同样,还有一些附加的别名,它们被解释为numpy数据类型(source):

Built-in Python types

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

1
2
3
4
5
6
7
8
9
int          int_
bool         bool_
float        float_
complex      cfloat
bytes        bytes_
str          bytes_ (Python2) or unicode_ (Python3)
unicode      unicode_
buffer       void
(all others) object_

但是,numba不知道这些别名,即使不处理numba,您最好直接使用真正的数据类型:

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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)

请注意,其中一些不受numba支持!