消除 numba.lowering.LoweringError:内部错误

4 投票
1 回答
3464 浏览
提问于 2025-04-20 06:50

我正在使用numba来加速我的代码,原本没有numba的时候运行得很好。但是在加上@jit之后,程序崩溃了,出现了这个错误:

Traceback (most recent call last):
  File "C:\work_asaaki\code\gbc_classifier_train_7.py", line 54, in <module>
    gentlebooster.train(X_train, y_train, boosting_rounds)
  File "C:\work_asaaki\code\gentleboost_c_class_jit_v7_nolimit.py", line 298, in train
    self.g_per_round, self.g = train_function(X, y, H)  
  File "C:\Anaconda\lib\site-packages\numba\dispatcher.py", line 152, in _compile_for_args
    return self.jit(sig)
  File "C:\Anaconda\lib\site-packages\numba\dispatcher.py", line 143, in jit
    return self.compile(sig, **kws)
  File "C:\Anaconda\lib\site-packages\numba\dispatcher.py", line 250, in compile
    locals=self.locals)
  File "C:\Anaconda\lib\site-packages\numba\compiler.py", line 183, in compile_bytecode
    flags.no_compile)
  File "C:\Anaconda\lib\site-packages\numba\compiler.py", line 323, in native_lowering_stage
    lower.lower()
  File "C:\Anaconda\lib\site-packages\numba\lowering.py", line 219, in lower
    self.lower_block(block)
  File "C:\Anaconda\lib\site-packages\numba\lowering.py", line 254, in lower_block
    raise LoweringError(msg, inst.loc)
numba.lowering.LoweringError: Internal error:
NotImplementedError: ('cast', <llvm.core.Instruction object at 0x000000001801D320>, slice3_type, int64)
File "gentleboost_c_class_jit_v7_nolimit.py", line 103

下面是第103行的代码,它在一个循环里:

weights = np.empty([n,m])
for curr_n in range(n):
    weights[curr_n,:] = 1.0/(n) # this is line 103

这里的n是我在代码上面某个地方已经定义好的常量。

我该怎么解决这个错误呢?什么是“lowering”?我在一台64位的机器上使用的是Anaconda 2.0.1,Numba 0.13.x和Numpy 1.8.x。

1 个回答

5

根据这个链接:https://gist.github.com/cc7768/bc5b8b7b9052708f0c0a

我找到了避免这个问题的方法。与其用冒号 : 来表示任何行或列,我选择把循环拆成两个循环,明确地指定数组中每个维度的索引:

weights = np.empty([n,m])
for curr_n in range(n):
    for curr_m in range (m):
        weights[curr_n,curr_m] = 1.0/(n)

在这之后,我的代码中还有其他地方使用了冒号,但它们没有导致后面的错误,不知道为什么。

撰写回答