- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试开始使用 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
它工作正常。它在调用 @autojit
时抛出 SyntaxError
当然是一个线索,但我对此还很陌生,所以我不能说什么......
此外,以防万一,我在 IPython Notebook 中运行它,以便在启动时自动加载 numpy、scipy 和 matplotlib。
最佳答案
我认为问题可能与此提交有关:
https://github.com/llvmpy/llvmpy/commit/b9752e1e981499879823f1f371e61b037706be4b
您会看到 alloca 的 API 发生了变化(第二个参数现在是大小,而不是名称)。 NUMBA 代码似乎将名称(即“return_value”)作为第二个参数传递。作为一个 glace,我猜测您可以更改 所有 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")
你会得到正确的行为。
关于python - 属性错误 : 'str' object has no attribute '_ptr' in Numba @autojit function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21323431/
我想将一个类对象传递给一个函数。我可以让它工作,但我想知道是否有我可以分配的类型?我有一个我正在尝试做的“最小”示例。 spec = [("a", float64),("b",float64)] @j
我有一个简单的函数来对扑克手牌进行排序(手牌是字符串)。 我用 rA,rB = rank(a),rank(b) 调用它,这是我的实现。没有 @jit(nopython=True) 也能很好地工作,但是
我在这里有一个简单的例子来帮助我理解使用 numba 和 cython。我是 numba 和 cython 的新手。我已经尽力结合所有技巧来使 numba 更快,并且在某种程度上,cython 也是如
我正在使用 numbas @jit 装饰器在 python 中添加两个 numpy 数组。如果我使用 @jit 与 python 相比,性能是如此之高。 然而,即使我传入 @numba.jit(nop
我需要为通用指标构建相异矩阵。由于我需要算法快速运行,所以我在 nopython 模式下使用了 numba 0.35。这是我的代码 import numpy as np from numba impo
Numba Cuda 有 syncthreads() 来同步一个 block 中的所有线程。如何在不退出当前内核的情况下同步网格中的所有 block ? 在 C-Cuda 中有一个 cooperati
有人尝试在Google合作伙伴中使用numba吗?我只是不知道如何在此环境中进行设置。 此刻,我陷入了错误library nvvm not found。 最佳答案 将此代码复制到单元格中。这个对我有用
我想编写一个函数,它既可以作为 jitted 函数运行,也可以作为普通 python 或对象模式 numba 运行,具体取决于 numba 是否能够进行类型推断。我实际上更喜欢普通的 python,但
我有一个非常简单的问题我无法解决。 我正在使用 Numba 和 Cuda。我有一个列表 T=[1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0] 我想要一个包含列表元素的元组,如
我正在测试一些采用 numpy 数组的函数的 numba 性能,并比较: import numpy as np from numba import jit, vectorize, float64 im
我正在使用 Scipy 的 interpolate.interp1d 在 Python3 中插入一维数组。我想将它与 numba 一起使用,但不支持 scipy 和此功能。是否有 numba 支持
我是 Numba 的新手,我正在尝试使用 Numba(版本 0.54.1)在 Python 中实现旧的 Fortran 代码,但是当我添加 parallel = True 时,程序实际上变慢了.我的程
我需要在 Python 中创建一个位数组。到目前为止,我发现可以使用 bitarray 生成非常节省内存的数组。模块。 然而,我的最终目的是使用来自Numba 的@vectorize 装饰器。 . N
我认为这是一个简单的问题,但我发现 numba 文档缺乏关于如何将字符串类型与 numpy 数组和字典一起使用的信息。我有一个我想使用 numba 的函数,它需要一个邮政编码列表,然后是一个映射邮政编
假设我有两个功能 def my_sub1(a): return a + 2 def my_main(a): a += 1 b = mysub1(a) return b
在以下用于逻辑比较的 numba 编译函数中,性能下降的原因可能是什么: from numba import njit t = (True, 'and_', False) #@njit(boolean
我的代码使用如下列表的笛卡尔积: import itertools cartesian_product = itertools.product(list('ABCDEF'), repeat=n) n可
我正在使用 Numba(版本 0.37.0)来优化 GPU 代码。我想使用组合矢量化函数(使用 Numba 的 @vectorize 装饰器)。 导入和数据: import numpy as np f
我想知道在 numba 函数中计算两个列表的交集的最快方法。只是为了澄清:两个列表的交集示例: Input : lst1 = [15, 9, 10, 56, 23, 78, 5, 4, 9] lst2
我正在使用 Numba 非 python 模式和一些 NumPy 函数。 @njit def invert(W, copy=True): ''' Inverts elementwise
我是一名优秀的程序员,十分优秀!