2017-04-19 195 views
2

我使用numba.jitclass修飾器來標記我的類以進行優化。如何指定使用自定義類型在numba中的方法的函數簽名

我不知道如何指定我想要優化的run方法的簽名。該方法以ConvertedDocument對象數組作爲參數。看來,numba不能因爲出現以下錯誤找出自身的數組類型,當我嘗試調用在nopython模式下的運行方式:

Traceback (most recent call last): 
    File "numba_test.py", line 53, in <module> 
    print run(a) 
    File "/home/clasocki/anaconda2/envs/my_numba_env/lib/python2.7/site-packages/numba/dispatcher.py", line 310, in _compile_for_args 
    raise e 
numba.errors.TypingError: Caused By: 
Traceback (most recent call last): 
    File "/home/clasocki/anaconda2/envs/my_numba_env/lib/python2.7/site-packages/numba/compiler.py", line 230, in run 
    stage() 
    File "/home/clasocki/anaconda2/envs/my_numba_env/lib/python2.7/site-packages/numba/compiler.py", line 444, in stage_nopython_frontend 
    self.locals) 
    File "/home/clasocki/anaconda2/envs/my_numba_env/lib/python2.7/site-packages/numba/compiler.py", line 800, in type_inference_stage 
    infer.propagate() 
    File "/home/clasocki/anaconda2/envs/my_numba_env/lib/python2.7/site-packages/numba/typeinfer.py", line 767, in propagate 
    raise errors[0] 
TypingError: Internal error at <numba.typeinfer.ExhaustIterConstraint object at 0x788cc9572d50>: 
--%<----------------------------------------------------------------- 
Traceback (most recent call last): 
    File "/home/clasocki/anaconda2/envs/my_numba_env/lib/python2.7/site-packages/numba/typeinfer.py", line 128, in propagate 
    constraint(typeinfer) 
    File "/home/clasocki/anaconda2/envs/my_numba_env/lib/python2.7/site-packages/numba/typeinfer.py", line 264, in __call__ 
    raise TypingError("failed to unpack {}".format(tp), loc=self.loc) 
    File "/home/clasocki/anaconda2/envs/my_numba_env/lib/python2.7/contextlib.py", line 35, in __exit__ 
    self.gen.throw(type, value, traceback) 
    File "/home/clasocki/anaconda2/envs/my_numba_env/lib/python2.7/site-packages/numba/errors.py", line 249, in new_error_context 
    six.reraise(type(newerr), newerr, sys.exc_info()[2]) 
    File "/home/clasocki/anaconda2/envs/my_numba_env/lib/python2.7/site-packages/numba/errors.py", line 243, in new_error_context 
    yield 
    File "/home/clasocki/anaconda2/envs/my_numba_env/lib/python2.7/site-packages/numba/typeinfer.py", line 264, in __call__ 
    raise TypingError("failed to unpack {}".format(tp), loc=self.loc) 
InternalError: local variable 'tp' referenced before assignment 
[1] During: typing of exhaust iter at numba_test.py (40) 
--%<----------------------------------------------------------------- 

File "numba_test.py", line 40 

Failed at nopython (nopython frontend) 
Internal error at <numba.typeinfer.ExhaustIterConstraint object at 0x788cc9572d50>: 
--%<----------------------------------------------------------------- 
Traceback (most recent call last): 
    File "/home/clasocki/anaconda2/envs/my_numba_env/lib/python2.7/site-packages/numba/typeinfer.py", line 128, in propagate 
    constraint(typeinfer) 
    File "/home/clasocki/anaconda2/envs/my_numba_env/lib/python2.7/site-packages/numba/typeinfer.py", line 264, in __call__ 
    raise TypingError("failed to unpack {}".format(tp), loc=self.loc) 
    File "/home/clasocki/anaconda2/envs/my_numba_env/lib/python2.7/contextlib.py", line 35, in __exit__ 
    self.gen.throw(type, value, traceback) 
    File "/home/clasocki/anaconda2/envs/my_numba_env/lib/python2.7/site-packages/numba/errors.py", line 249, in new_error_context 
    six.reraise(type(newerr), newerr, sys.exc_info()[2]) 
    File "/home/clasocki/anaconda2/envs/my_numba_env/lib/python2.7/site-packages/numba/errors.py", line 243, in new_error_context 
    yield 
    File "/home/clasocki/anaconda2/envs/my_numba_env/lib/python2.7/site-packages/numba/typeinfer.py", line 264, in __call__ 
    raise TypingError("failed to unpack {}".format(tp), loc=self.loc) 
InternalError: local variable 'tp' referenced before assignment 
[1] During: typing of exhaust iter at numba_test.py (40) 
--%<----------------------------------------------------------------- 

File "numba_test.py", line 40 

This error may have been caused by the following argument(s): 
- argument 0: Unsupported array dtype: object 

下面是我指定的numba裝飾:

spec = [ 
    ('profile', numba.typeof(numpy.asarray([1.0, 2.0]))), 
    ('word_weights', numba.typeof(numpy.asarray([(1.0,2.0)]))) 
] 

@numba.jitclass(spec) 
class ConvertedDocument(object): 
    def __init__(self, profile, word_weights): 
     self.profile = profile 
     self.word_weights = word_weights 

@numba.jit(nopython=True,cache=True) 
def run(docs): 
    s = 0 
    for doc in docs: #array of documents 
     for w_id, weight in doc.word_weights: #accessing document's property 
      s += weight 

    return s 

這是run方法的調用方式:

x = numpy.asarray([1.0, 2.0]) 
y = numpy.asarray([(1.0,2.0), (3.0,4.0)]) 
a = numpy.asarray([ConvertedDocument(x,y)]) 
print run(a) 

如果a numpy的陣列替換Python列表中,例外情況如下:

Failed at nopython (nopython mode backend) 
reflected list(instance.jitclass.ConvertedDocument#3bffb70<profile:array(float64, 1d, C),word_weights:array(float64, 2d, C)>): unsupported nested memory-managed object 

有誰知道如何在自定義類型使用還是迭代過對象的數組被支持或不指定方法簽名?

回答

0

問題似乎是你不能在jitclass對象上調用np.nditer,這是有道理的,因爲jitclass是不可迭代的。它將數據存儲爲數組(和其他數據類型)的結構而不是結構數組。你正試圖把它用作後者。如果除了兩個數組屬性之外,還有一堆標量數據屬性或不同大小的數組,那麼對於如何迭代jitclass對象將是不明確的。

錯誤消息是公認不清楚。我的建議會迭代你直接需要的word_weights索引。

+0

感謝您的回覆。事情是'docs'是一個文檔數組 - 不是一個'jitclass'對象,所以我試圖遍歷一個可迭代的文檔數組。我通過刪除'np.nditer'來更新我的示例,這可能根本不需要,並且導致了以前的錯誤。現在的錯誤是不同的,並表明目前這種迭代可能不被支持,但我不確定。 – ce57

相關問題