numba NotImplementedError: Unknown operator 'in'

numba NotImplementedError: Unknown operator 'in'

from numba import jit
from numpy import arange

# jit decorator tells Numba to compile this function.
# The argument types will be inferred by Numba when function is called.
@jit
def sum2d(arr,m):
    M, N = arr.shape
    result = 0.0
    if m in ['yes']:
        for i in range(M):
            for j in range(N):
                result += arr[i,j]
    return result

a = arange(9).reshape(3,3)

执行结果出错。问题:numba 是否支持 'in' 运算符?如果不支持,我可以解决这个问题吗?

print(sum2d(a,'yes'))

更新:

import cpkg_resources
env = pkg_resources.Environment()
env['numba'][0]
numba 0.19.1 (/Users/blah/anaconda/lib/python2.7/site-packages)

LoweringError                             Traceback (most recent call last)
<ipython-input-4-1100b95cf735> in <module>()
----> 1 print(sum2d(a,'yes'))

/Users/blah/anaconda/lib/python2.7/site-packages/numba/dispatcher.pyc in _compile_for_args(self, *args, **kws)
    163         assert not kws
    164         sig = tuple([self.typeof_pyval(a) for a in args])
--> 165         return self.compile(sig)
    166 
    167     def inspect_llvm(self, signature=None):

/Users/blah/anaconda/lib/python2.7/site-packages/numba/dispatcher.pyc in compile(self, sig)
    301                                           self.py_func,
    302                                           args=args, return_type=return_type,
--> 303                                           flags=flags, locals=self.locals)
    304 
    305             # Check typing error if object mode is used

/Users/blah/anaconda/lib/python2.7/site-packages/numba/compiler.pyc in compile_extra(typingctx, targetctx, func, args, return_type, flags, locals, library)
    593     pipeline = Pipeline(typingctx, targetctx, library,
    594                         args, return_type, flags, locals)
--> 595     return pipeline.compile_extra(func)
    596 
    597 

/Users/blah/anaconda/lib/python2.7/site-packages/numba/compiler.pyc in compile_extra(self, func)
    316                 raise e
    317 
--> 318         return self.compile_bytecode(bc, func_attr=self.func_attr)
    319 
    320     def compile_bytecode(self, bc, lifted=(), lifted_from=None,

/Users/blah/anaconda/lib/python2.7/site-packages/numba/compiler.pyc in compile_bytecode(self, bc, lifted, lifted_from, func_attr)
    325         self.lifted_from = lifted_from
    326         self.func_attr = func_attr
--> 327         return self._compile_bytecode()
    328 
    329     def compile_internal(self, bc, func_attr=DEFAULT_FUNCTION_ATTRIBUTES):

/Users/blah/anaconda/lib/python2.7/site-packages/numba/compiler.pyc in _compile_bytecode(self)
    580 
    581         pm.finalize()
--> 582         return pm.run(self.status)
    583 
    584 

/Users/blah/anaconda/lib/python2.7/site-packages/numba/compiler.pyc in run(self, status)
    207                     # No more fallback pipelines?
    208                     if is_final_pipeline:
--> 209                         raise patched_exception
    210                     # Go to next fallback pipeline
    211                     else:

LoweringError: Caused By:
Traceback (most recent call last):
  File "/Users/blah/anaconda/lib/python2.7/site-packages/numba/compiler.py", line 201, in run
    res = stage()
  File "/Users/blah/anaconda/lib/python2.7/site-packages/numba/compiler.py", line 393, in stage_objectmode_frontend
    cres = self.frontend_looplift()
  File "/Users/blah/anaconda/lib/python2.7/site-packages/numba/compiler.py", line 384, in frontend_looplift
    func_attr=self.func_attr)
  File "/Users/blah/anaconda/lib/python2.7/site-packages/numba/compiler.py", line 604, in compile_bytecode
    return pipeline.compile_bytecode(bc=bc, lifted=lifted, lifted_from=lifted_from, func_attr=func_attr)
  File "/Users/blah/anaconda/lib/python2.7/site-packages/numba/compiler.py", line 327, in compile_bytecode
    return self._compile_bytecode()
  File "/Users/blah/anaconda/lib/python2.7/site-packages/numba/compiler.py", line 582, in _compile_bytecode
    return pm.run(self.status)
  File "/Users/blah/anaconda/lib/python2.7/site-packages/numba/compiler.py", line 209, in run
    raise patched_exception
LoweringError: Caused By:
Traceback (most recent call last):
  File "/Users/blah/anaconda/lib/python2.7/site-packages/numba/compiler.py", line 201, in run
    res = stage()
  File "/Users/blah/anaconda/lib/python2.7/site-packages/numba/compiler.py", line 515, in stage_objectmode_backend
    res = self._backend(lowerfn, objectmode=True)
  File "/Users/blah/anaconda/lib/python2.7/site-packages/numba/compiler.py", line 493, in _backend
    lowered = lowerfn()
  File "/Users/blah/anaconda/lib/python2.7/site-packages/numba/compiler.py", line 471, in backend_object_mode
    self.flags)
  File "/Users/blah/anaconda/lib/python2.7/site-packages/numba/compiler.py", line 750, in py_lowering_stage
    lower.lower()
  File "/Users/blah/anaconda/lib/python2.7/site-packages/numba/lowering.py", line 82, in lower
    self.lower_normal_function(self.fndesc)
  File "/Users/blah/anaconda/lib/python2.7/site-packages/numba/lowering.py", line 114, in lower_normal_function
    entry_block_tail = self.lower_function_body()
  File "/Users/blah/anaconda/lib/python2.7/site-packages/numba/lowering.py", line 140, in lower_function_body
    self.lower_block(block)
  File "/Users/blah/anaconda/lib/python2.7/site-packages/numba/lowering.py", line 158, in lower_block
    raise LoweringError(msg, inst.loc)
LoweringError: Internal error:
NotImplementedError: Unknown operator 'in'
File "<ipython-input-3-f51bcec7986c>", line 10

Failed at object (object mode backend)
Internal error:
NotImplementedError: Unknown operator 'in'
File "<ipython-input-3-f51bcec7986c>", line 10

Failed at object (object mode frontend)
Caused By:
Traceback (most recent call last):
  File "/Users/blah/anaconda/lib/python2.7/site-packages/numba/compiler.py", line 201, in run
    res = stage()
  File "/Users/blah/anaconda/lib/python2.7/site-packages/numba/compiler.py", line 515, in stage_objectmode_backend
    res = self._backend(lowerfn, objectmode=True)
  File "/Users/blah/anaconda/lib/python2.7/site-packages/numba/compiler.py", line 493, in _backend
    lowered = lowerfn()
  File "/Users/blah/anaconda/lib/python2.7/site-packages/numba/compiler.py", line 471, in backend_object_mode
    self.flags)
  File "/Users/blah/anaconda/lib/python2.7/site-packages/numba/compiler.py", line 750, in py_lowering_stage
    lower.lower()
  File "/Users/blah/anaconda/lib/python2.7/site-packages/numba/lowering.py", line 82, in lower
    self.lower_normal_function(self.fndesc)
  File "/Users/blah/anaconda/lib/python2.7/site-packages/numba/lowering.py", line 114, in lower_normal_function
    entry_block_tail = self.lower_function_body()
  File "/Users/blah/anaconda/lib/python2.7/site-packages/numba/lowering.py", line 140, in lower_function_body
    self.lower_block(block)
  File "/Users/blah/anaconda/lib/python2.7/site-packages/numba/lowering.py", line 158, in lower_block
    raise LoweringError(msg, inst.loc)
LoweringError: Internal error:
NotImplementedError: Unknown operator 'in'
File "<ipython-input-3-f51bcec7986c>", line 10

Failed at object (object mode backend)
Internal error:
NotImplementedError: Unknown operator 'in'
File "<ipython-input-3-f51bcec7986c>", line 10

对于遇到此错误的任何其他人:

in 运算符似乎在 numba 0.21 see changelog 版本之前没有完全实现。一旦 numba 更新,问题就不会发生。