- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 numba.jit 加速一个简单的 Python 循环。但似乎 jit 无法处理基本的数组索引和切片?我能做些什么来使这成为可能吗?如果 jit 不能处理基本的 numpy 数组,我不明白它怎么会有用。
我强制使用 nopython 模式。它在对象模式下工作,但这根本不会加速代码,所以它是我需要的 nopython 模式。
下面的代码只是一个说明问题的例子。我的实际代码有同样的问题,但有更多的循环和迭代,因此 jit 会非常有用。
import numpy as np
from numba import jit
n = 100
myarray = np.zeros(n)
@jit(nopython=True)
def compute(n):
for i in xrange(n):
myarray[i] += 1 # This indexing causes the error.
compute(n)
"""Sample run:
> python jit_test.py
> ...
> Failed at nopython (nopython frontend)
> Internal error at <numba.typeinfer.SetItemConstrain object at 0x7f700c89a7d0>:
> Immutable array
> File "jit_test.py", line 10
"""
如果我尝试切片而不是出现不同的错误。
# The rest of the code unchanged.
myarray[:] += 1 # This slicing causes the error.
# The rest of the code unchanged.
"""Sample run:
> python jit_test.py
> ...
> Failed at nopython (nopython frontend)
> Internal error at <numba.typeinfer.IntrinsicCallConstrain object at 0x7f297e2a9dd0>:
> Don't know how to create implicit output array with 'A' layout.
> File "jit_test2.py", line 10
"""
我的构建:
numba version: 0.19.1
numpy version: 1.9.2
python version: 2.7.10
最佳答案
在 numba 中,全局数组是静态的。您将在您的方法中获得一个只读副本。见:
如果您想修改一个数组,要么显式传入它,要么在函数中创建它(在进行数组内存管理的较新版本的 Numba 中)并返回它。
例如:
import numpy as np
import numba as nb
def compute(arr):
for i in xrange(arr.shape[0]):
arr[i] += 1
n = 100
myarray = np.zeros(n)
jitcompute = nb.jit(nopython=True)(compute)
然后计时:
In [12]: %timeit compute(myarray)
10000 loops, best of 3: 25.7 µs per loop
In [13]: %timeit jitcompute(myarray)
The slowest run took 17.06 times longer than the fastest. This could mean that an intermediate result is being cached
1000000 loops, best of 3: 461 ns per loop
一些注意事项:
@jit
装饰器,这样我就可以编写一次方法定义,并在使用和不使用 numba jit-ing 的情况下为方法计时,但是 jitcompute
等同于在 compute
上使用装饰器会得到的结果。 关于python - numba.jit (nopython) 可以处理数组索引和切片吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32508628/
在动态语言中,动态类型代码 JIT 是如何编译成机器码的?更具体地说:编译器是否会在某个时候推断类型?还是在这些情况下严格解释? 例如,如果我有类似下面的伪代码 def func(arg) i
X86 和 AMD64 是许多计算环境(桌面、服务器和 super 计算机)最重要的架构。显然,JIT 编译器应该同时支持它们才能获得认可。 直到最近,SPARC 架构才是编译器合乎逻辑的下一步,特别
既然有如此多的 JIT 实现,每个 JIT 都会发出 native 代码。那么为什么没有人制作像 JIT2EXE 这样的工具来将 native 代码保存为 native 可执行文件呢? 最佳答案 这个
JIT 编译器将字节码编译成机器码的概念我还是不太清楚。我想知道为什么它比非 JIT 解释器产生更快的代码。有人可以给我一个很好的例子来说明这个过程是如何完成的吗? 最佳答案 假设您有一个需要执行一百
Torchscript 提供了 torch.jit.trace 和 torch.jit.script 将 pytorch 代码从 Eager 模式转换为脚本模型。从文档中,我可以理解 torch.ji
好的,我已经阅读了一些关于 JIT 和非 JIT 启用解释器之间差异的讨论,以及为什么 JIT 通常会提高性能。 但是,我的问题是: 最终,不支持 JIT 的解释器是否必须像 JIT 编译器那样将字节
有没有办法在消除 JIT 开销的同时实现 JIT 性能?最好通过将类文件编译为 native 镜像。 我研究过GCJ,但即使对于简单的程序,GCJ输出的性能也比Java JIT差很多。 最佳答案 您可
我试图更好地理解 JIT 编译器在 volatile 变量值缓存方面如何为 java 工作。考虑这个问题中提出的例子: Infinite loop problem with while loop an
我的代码是这样的: @jit(nopython=True) def sum_fn(arg1, arg2, ...argn): ..... for i in xrange(len(arg
以下代码无效: def get_unique(arr): return jnp.unique(arr) get_unique = jit(get_unique) get_unique(jnp.
我需要能够调用一个 GPU 函数,该函数本身间接调用另一个 GPU 函数: from numba import cuda, jit import numpy as np # GPU function
我有一个关于使用 Continuum 的 Accelerate 和 numba 包中的 Python CUDA 库的问题。正在使用装饰器@jit与 target = gpu同 @cuda.jit ?
有人可以指出我的方向,这可能会让我明白为什么 JIT 会取消优化我的循环? (OSR)。看起来它被 C1 编译一次,然后多次取消优化(我可以看到数十或数百个以 开头的日志) 这是包含该重要循环的类:
我引用了Oracle的以下文档: http://docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/geninfo/diagnos/underst_jit
我需要在 C++ 中运行预训练的 pytorch 神经网络模型(在 python 中训练)来进行预测。 为此,我按照此处给出的有关如何在 C++ 中加载 pytorch 模型的说明进行操作:https
我正在使用 numbas @jit 装饰器在 python 中添加两个 numpy 数组。如果我使用 @jit 与 python 相比,性能是如此之高。 然而,即使我传入 @numba.jit(nop
我是Python新手。 我编写了一些代码尝试将图片混合为新图片。 我完成了,但是浪费了太多时间。 所以我尝试使用 Numba 让代码在我的 GPU 上运行。但遇到一些警告和错误 os Ubuntu 1
我正在将我的网站从安装在共享网络托管帐户(在 DreamHost)上的 PHP v.5 转换为在 PHP 7.3.11 上运行。转换后,我开始注意到偶尔会收到以下警告: Warning: preg_m
在 Stack Overflow 上向所有编译器设计者致以问候。 我目前正在从事一个项目,该项目的重点是开发一种用于高性能计算的新脚本语言。源代码首先被编译成字节码表示。字节码然后由运行时加载,它对其
我相信 Apple 已禁止在 ARM64 架构上同时写入和执行内存,请参阅: 参见 mmap() RWX page on MacOS (ARM64 architecture)? 这使得像 jonesf
我是一名优秀的程序员,十分优秀!