静かなる名辞

pythonとプログラミングのこと


numbaとnumpy.emptyでbool配列が作れないとき

 タイトル通りのことをやろうとして、なんかエラーになったんですよね。

import numpy as np
from numba import jit

@jit("b1[:]()", nopython=True)
def f():
    a = np.empty(100, np.bool)
    return a

f()

 動きそうに見えますが、

Traceback (most recent call last):
  File "filename.py", line 4, in <module>
    @jit("b1[:]()", nopython=True)
  File "/*/site-packages/numba/decorators.py", line 200, in wrapper
    disp.compile(sig)
  File "/*/site-packages/numba/compiler_lock.py", line 32, in _acquire_compile_lock
    return func(*args, **kwargs)
  File "/*/site-packages/numba/dispatcher.py", line 768, in compile
    cres = self._compiler.compile(args, return_type)
  File "/*/site-packages/numba/dispatcher.py", line 81, in compile
    raise retval
  File "/*/site-packages/numba/dispatcher.py", line 91, in _compile_cached
    retval = self._compile_core(args, return_type)
  File "/*/site-packages/numba/dispatcher.py", line 109, in _compile_core
    pipeline_class=self.pipeline_class)
  File "/*/site-packages/numba/compiler.py", line 551, in compile_extra
    return pipeline.compile_extra(func)
  File "/*/site-packages/numba/compiler.py", line 331, in compile_extra
    return self._compile_bytecode()
  File "/*/site-packages/numba/compiler.py", line 393, in _compile_bytecode
    return self._compile_core()
  File "/*/site-packages/numba/compiler.py", line 373, in _compile_core
    raise e
  File "/*/site-packages/numba/compiler.py", line 364, in _compile_core
    pm.run(self.state)
  File "/*/site-packages/numba/compiler_machinery.py", line 347, in run
    raise patched_exception
  File "/*/site-packages/numba/compiler_machinery.py", line 338, in run
    self._runPass(idx, pass_inst, state)
  File "/*/site-packages/numba/compiler_lock.py", line 32, in _acquire_compile_lock
    return func(*args, **kwargs)
  File "/*/site-packages/numba/compiler_machinery.py", line 302, in _runPass
    mutated |= check(pss.run_pass, internal_state)
  File "/*/site-packages/numba/compiler_machinery.py", line 275, in check
    mangled = func(compiler_state)
  File "/*/site-packages/numba/typed_passes.py", line 95, in run_pass
    raise_errors=self._raise_errors)
  File "/*/site-packages/numba/typed_passes.py", line 67, in type_inference_stage
    infer.propagate(raise_errors=raise_errors)
  File "/*/site-packages/numba/typeinfer.py", line 985, in propagate
    raise errors[0]
numba.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Invalid use of Function(<built-in function empty>) with argument(s) of type(s): (Literal[int](100), Function(<class 'bool'>))
 * parameterized
In definition 0:
    All templates rejected with literals.
In definition 1:
    All templates rejected without literals.
This error is usually caused by passing an argument of a type that is unsupported by the named function.
[1] During: resolving callee type: Function(<built-in function empty>)
[2] During: typing of call at filename.py (6)


File "filename.py", line 6:
def f():
    a = np.empty(100, np.bool)
    ^

 ふざけたエラーが出ました。

 ググって解決しました。

Related to #1311. np.bool_ works as expected.
inference failure with np.empty on bool type · Issue #2529 · numba/numba · GitHub

 代わりにnp.bool_を使えばいいらしいです。馬鹿馬鹿しいですね。numbaの開発的には直すつもりはなさそうです。

@jit("b1[:]()", nopython=True)
def f():
    a = np.empty(100, np.bool_)
    return a

 こうするとちゃんと動きました。

 bool型のdtypeを指定したいときとかは気をつけましょう。