为什么numba不能与此嵌套函数一起使用?

时间:2018-07-11 13:46:32

标签: python python-3.x precompile

如果有人对解决问题感兴趣,我在这里报告了描述的错误:https://github.com/numba/numba/issues/3095

我正在尝试使用numba预编译在3D时间序列数据上运行的最小化。第一步,我想定义一个成本函数,但这已经失败了。这是我的代码:

from numba import jit

@jit
def tester(axis,data):
    def lineCost(pars):
        A=pars[0]
        B=pars[1]
        return np.sum((A*axis+B - data)**2)
    return lineCost([axis,data])

tester(1,2)

这会产生“未实现”错误:

~/.local/lib/python3.5/site-packages/numba/lowering.py in lower(self)
    171         if self.generator_info is None:
    172             self.genlower = None
--> 173             self.lower_normal_function(self.fndesc)
    174         else:
    175             self.genlower = self.GeneratorLower(self)

~/.local/lib/python3.5/site-packages/numba/lowering.py in lower_normal_function(self, fndesc)
    212         # Init argument values
    213         self.extract_function_arguments()
--> 214         entry_block_tail = self.lower_function_body()
    215
    216         # Close tail of entry block

~/.local/lib/python3.5/site-packages/numba/lowering.py in lower_function_body(self)
    237             bb = self.blkmap[offset]
    238             self.builder.position_at_end(bb)
--> 239             self.lower_block(block)
    240
    241         self.post_lower()

~/.local/lib/python3.5/site-packages/numba/lowering.py in lower_block(self, block)
    252             with new_error_context('lowering "{inst}" at {loc}', inst=inst,
    253                                    loc=self.loc, errcls_=defaulterrcls):
--> 254                 self.lower_inst(inst)
    255
    256     def create_cpython_wrapper(self, release_gil=False):

/usr/lib/python3.5/contextlib.py in __exit__(self, type, value, traceback)
     75                 value = type()
     76             try:
---> 77                 self.gen.throw(type, value, traceback)
     78                 raise RuntimeError("generator didn't stop after throw()")
     79             except StopIteration as exc:

~/.local/lib/python3.5/site-packages/numba/errors.py in new_error_context(fmt_, *args, **kwargs)
    583         from numba import config
    584         tb = sys.exc_info()[2] if config.FULL_TRACEBACKS else None
--> 585         six.reraise(type(newerr), newerr, tb)
    586
    587

~/.local/lib/python3.5/site-packages/numba/six.py in reraise(tp, value, tb)
    657         if value.__traceback__ is not tb:
    658             raise value.with_traceback(tb)
--> 659         raise value
    660
    661 else:

LoweringError: Failed at object (object mode backend)
make_function(closure=$0.3, defaults=None, name=$const0.5, code=<code object lineCost at 0x7fd7ada3b810, file "<ipython-input-59-ef6835d3b147>", line 3>)

File "<ipython-input-59-ef6835d3b147>", line 3:
def tester(axis,data):
    def lineCost(pars):
    ^
[1] During: lowering "$0.6 = make_function(closure=$0.3, defaults=None, name=$const0.5, code=<code object lineCost at 0x7fd7ada3b810, file "<ipython-input-59-ef6835d3b147>", line 3>)" at <ipython-input-59-ef6835d3b147> (3)
-------------------------------------------------------------------------------
This should not have happened, a problem has occurred in Numba's internals.

Please report the error message and traceback, along with a minimal reproducer
at: https://github.com/numba/numba/issues/new

If more help is needed please feel free to speak to the Numba core developers
directly at: https://gitter.im/numba/numba

Thanks in advance for your help in improving Numba!

您能帮助我理解代码的哪一部分导致numba问题吗?那将有很大帮助。谢谢!

最好, 马尔特

2 个答案:

答案 0 :(得分:0)

避免使用全局变量(数据和轴在lineCost中是全局的),避免使用包含函数的函数,并避免使用列表([axis,data])。

工作示例

from numba import jit

@jit
def lineCost(axis,data):
    return np.sum((axis*axis+data - data)**2)

@jit
def tester(axis,data):
    return lineCost(axis,data)

tester(1,2)

大多数情况应该在最新版本中起作用,但是不建议您使用通常包含一些错误或某些细节的最新功能,这些错误或某些细节不受支持。

即使这种方法行得通,但如果性能低于预期,也不会给我带来太多惊喜。

答案 1 :(得分:0)

实际上,这似乎是一个错误,该错误已在最新版本中删除:) https://github.com/numba/numba/issues/3095