2014-01-24 74 views
2

我想上手Numba,並且已經得到它安裝了我的第一個經驗是與下面的代碼位:AttributeError的:「海峽」對象在Numba沒有屬性「_ptr」 @autojit功能

from numba import autojit 

@autojit 
def trial(a,b): 
    return a+b 

trial(1,1) 

我得到以下錯誤,它告訴我autojit誤解了變量類型,但沒有告訴我更多。 (與其他方式相同,例如@jit(...))。該問題類似於this,但不是特定於操作:無論函數在做什麼或它有多簡單,都會發生此問題(如示例顯示)。對於這個問題可能有什麼建議?在Ubuntu 12.04上運行,並根據Github上的說明進行安裝。

--------------------------------------------------------------------------- 
AttributeError       Traceback (most recent call last) 
<ipython-input-1-653102b59b98> in <module>() 
     5  return a+b 
     6 
----> 7 trial(1,1) 

/usr/local/lib/python2.7/dist-packages/numba/numbawrapper.so in numba.numbawrapper._NumbaSpecializingWrapper.__call__ (numba/numbawrapper.c:3934)() 

/usr/local/lib/python2.7/dist-packages/numba/wrapping/compiler.pyc in compile_from_args(self, args, kwargs) 
    67  def compile_from_args(self, args, kwargs): 
    68   signature = self.resolve_argtypes(args, kwargs) 
---> 69   return self.compile(signature) 
    70 
    71  def compile(self, signature): 

/usr/local/lib/python2.7/dist-packages/numba/wrapping/compiler.pyc in compile(self, signature) 
    86      env=self.env, func_ast=self.ast, **self.flags) 
    87 
---> 88   compiled_function = dec(self.py_func) 
    89   return compiled_function 
    90 

/usr/local/lib/python2.7/dist-packages/numba/decorators.pyc in _jit_decorator(func) 
    222   sig, lfunc, wrapper = compile_function(env, func, argtys, 
    223            restype=return_type, 
--> 224            nopython=nopython, func_ast=func_ast, **kwargs) 
    225   return numbawrapper.create_numba_wrapper(func, wrapper, sig, lfunc) 
    226 

/usr/local/lib/python2.7/dist-packages/numba/decorators.pyc in compile_function(env, func, argtypes, restype, func_ast, **kwds) 
    131  assert kwds.get('llvm_module') is None, kwds.get('llvm_module') 
    132 
--> 133  func_env = pipeline.compile2(env, func, restype, argtypes, func_ast=func_ast, **kwds) 
    134 
    135  function_cache.register_specialization(func_env) 

/usr/local/lib/python2.7/dist-packages/numba/pipeline.pyc in compile2(env, func, restype, argtypes, ctypes, compile_only, func_ast, **kwds) 
    142   pipeline = env.get_pipeline(kwds.get('pipeline_name', None)) 
    143   func_ast.pipeline = pipeline 
--> 144   post_ast = pipeline(func_ast, env) 
    145   func_signature = func_env.func_signature 
    146   symtab = func_env.symtab 

/usr/local/lib/python2.7/dist-packages/numba/pipeline.pyc in __call__(self, ast, env) 
    189 
    190   if self.is_composed: 
--> 191    ast = self.transform(ast, env) 
    192   else: 
    193    try: 

/usr/local/lib/python2.7/dist-packages/numba/pipeline.pyc in transform(self, ast, env) 
    654     stage_tuple = (stage, utils.ast2tree(ast)) 
    655     logger.debug(pprint.pformat(stage_tuple)) 
--> 656    ast = stage(ast, env) 
    657   return ast 
    658 

/usr/local/lib/python2.7/dist-packages/numba/pipeline.pyc in _stage(ast, env) 
    639    def _stage(ast, env): 
    640     stage_obj = getattr(env.pipeline_stages, name) 
--> 641     return _check_stage_object(stage_obj)(ast, env) 
    642    _stage.__name__ = name 
    643    stage = _stage 

/usr/local/lib/python2.7/dist-packages/numba/pipeline.pyc in __call__(self, ast, env) 
    192   else: 
    193    try: 
--> 194     ast = self.transform(ast, env) 
    195    except error.NumbaError as e: 
    196     func_env = env.translation.crnt 

/usr/local/lib/python2.7/dist-packages/numba/pipeline.pyc in transform(self, ast, env) 
    551    **func_env.kwargs) 
    552 
--> 553   func_env.translator.translate() 
    554   func_env.lfunc = func_env.translator.lfunc 
    555   return ast 

/usr/local/lib/python2.7/dist-packages/numba/codegen/translate.pyc in translate(self) 
    327   self.lfunc = None 
    328   try: 
--> 329    self.setup_func() 
    330    if isinstance(self.ast, ast.FunctionDef): 
    331     # Handle the doc string for the function 

/usr/local/lib/python2.7/dist-packages/numba/codegen/translate.pyc in setup_func(self) 
    304 
    305   # TODO: Put current function into symbol table for recursive call 
--> 306   self.setup_return() 
    307 
    308   if self.have_cfg: 

/usr/local/lib/python2.7/dist-packages/numba/codegen/translate.pyc in setup_return(self) 
    471    llvm_ret_type = self.func_signature.return_type.to_llvm(self.context) 
    472    self.return_value = self.builder.alloca(llvm_ret_type, 
--> 473              "return_value") 
    474 
    475   # All non-NULL object emporaries are DECREFed here 

/usr/local/lib/python2.7/dist-packages/llvm/core.pyc in alloca(self, ty, size, name) 
    2303 
    2304  def alloca(self, ty, size=None, name=""): 
-> 2305   sizeptr = size._ptr if size else None 
    2306   return _make_value(self._ptr.CreateAlloca(ty._ptr, sizeptr, name)) 
    2307 

AttributeError: 'str' object has no attribute '_ptr' 

編輯:針對@JoshAdel,我使用「自定義的Python環境」 GitHub的頁面上,說明我的LLVM_BUILD_DIR=/opt/。從回購中的CHANGE_LOG中,我將安裝的版本設爲0.11。如果我跑你提供的例子中,我得到

from numba import autojit, typeof 

@autojit 
def trial(a,b): 
    print typeof(a), typeof(b) 
    return a+b 

trial(1,1) 

到如果刪除其

File "<unknown file>", line 2 
    print typeof(a), typeof(b) 
      ^
SyntaxError: invalid syntax 

它工作正常的@autojit。它拋出一個SyntaxError@autojit調用肯定​​是一個線索,但我已經足夠新,我不能說什麼...

此外,萬一它很重要,我運行在IPython筆記本,以便numpy,scipy和matplotlib在啓動時自動加載。

+0

我猜這是一個內部問題,如果你覺得沒有錯的話。 – aIKid

+0

正如旁觀,numpy,scipy等只在IPython中自動加載,你是用'--pylab'標誌啓動的,不推薦使用:http://carreau.github.io/posts/10-No- PyLab-Thanks.ipynb。html – JoshAdel

回答

1

我想該問題可能與此有關承諾:

https://github.com/llvmpy/llvmpy/commit/b9752e1e981499879823f1f371e61b037706be4b

你會看到API來ALLOCA改變(第二個參數是現在的大小,而不是名稱)。 NUMBA代碼似乎正在傳遞一個名稱(即'return_value')作爲第二個參數。作爲一個驚人的我猜測你可以改變全部的numba調用傳遞無。例如,下面是一條線在那裏我得到了同樣的錯誤:

 self.return_value = self.builder.alloca(llvm_ret_type, 
               "return_value") 

它切換到:

 self.return_value = self.builder.alloca(llvm_ret_type, None, 
               "return_value") 

而且你會得到正確的行爲。

+0

在構建和安裝之前,切換到llvmpy'git co 0.12.1'的0.12.1標籤。 – user2213228

+0

在構建llvmpy之前切換到0.12.1'git checkout 0.12.1',然後修復numba問題。我上面介紹的例子現在可以正常工作。謝謝。 – user3230304

1

此代碼適用於OSX上的Anaconda發行版使用Numba 0.11.1。你正在使用哪個版本?你說你已經通過github上的說明安裝了,但是有幾個選項可以列出來。此外,這是什麼細微的變化的輸出(您可能需要進一步調整的事情,比如降return語句來得到它的運行):

from numba import autojit, typeof 

@autojit 
def trial(a,b): 
    print typeof(a), typeof(b) 
    return a+b 

print trial(1,1) 

我得到:

int int 
2 
+0

我在上面添加了一些說明來回答你的問題。謝謝。 – user3230304

相關問題