2014-01-24 80 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

相关问题