【问题标题】:AttributeError: 'str' object has no attribute '_ptr' in Numba @autojit functionAttributeError:“str”对象在 Numba @autojit 函数中没有属性“_ptr”
【发布时间】:2014-01-24 02:40:48
【问题描述】:

我正在尝试开始使用 Numba,安装它后,我的第一次体验是使用以下代码:

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 的回应,我使用了 Github 页面上“自定义 Python 环境”的说明以及我的LLVM_BUILD_DIR=/opt/。从 repo 中的 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 Notebook 中运行它,以便在启动时自动加载 numpy、scipy 和 matplotlib。

【问题讨论】:

标签: python numpy numba


【解决方案1】:

我认为问题可能与此提交有关:

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

您会看到分配给 alloca 的 API 发生了变化(第二个参数现在是大小,而不是名称)。 NUMBA 代码似乎将名称(即“return_value”)作为第二个参数传递。作为一个亮点,我猜你可以改变 all 的 numba 调用来传递 None。例如,这是我遇到相同错误的一行:

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

切换到:

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

你会得到正确的行为。

【讨论】:

  • 在构建和安装之前切换到 llvmpy git co 0.12.1 的 0.12.1 标签。
  • 在构建 llvmpy 之前切换到 0.12.1 git checkout 0.12.1 然后 numba 解决了问题。我上面介绍的示例现在可以正常工作。谢谢。
【解决方案2】:

此代码适用于在 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

【讨论】:

  • 我在上面添加了一些说明来回答您的问题。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-10
  • 2021-10-04
  • 2019-12-02
  • 2021-09-25
  • 2014-03-04
相关资源
最近更新 更多