Python numba @vectorize/@guvectorize

本文介绍了Numba中的Ufunc概念,探讨了@vectorize和@guvectorize装饰器的区别,以及它们如何简化对numpy数组的并行计算。特别关注了使用C语言实现Ufunc的挑战和这两种装饰器在性能优化中的作用。

When learning numba, the concept of 'ufunc' is mentioned frequently.

Ufunc means Universal Functions, it apply certain computation on every single elements inside a numpy array.

@vectorize, as a decorator, basically converts the decorated function to a ufunc. I understand it as a much simpler way of .apply() or map() with a for loop iterating every single elements inside an array.

This is useful also because creating a ufunc requires coding in C, which could be hard and not so straight forward.

@guvectorize, on the other hand, is slightly different. This decorator won't return the underlying function's return, but return the last argument in the function. For example, in the offical file:

@guvectorize([(int64[:], int64, int64[:])], '(n),()->(n)')
def g(x, y, res):
    for i in range(x.shape[0]):
        res[i] = x[i] + y

As shown above, 'res' is the supposed return value, and it is more argument in the function 'g()'.

Like @jit, @vectorize and @guvectorize both support parameter (nopython=True) to avoid falling back to object mode.

(bevfusion) lyq@ps:/mnt/sdb/lyq/bevfusion/mmdetection3d$ pip list | grep -E "mmdet3d|mmengine" mmdet3d 0.0.0 mmengine 0.10.7 (bevfusion) lyq@ps:/mnt/sdb/lyq/bevfusion/mmdetection3d$ python -c "from mmdet3d.apis import init_model; print('导入成功')" Traceback (most recent call last): File "<string>", line 1, in <module> File "/mnt/sdb/lyq/bevfusion/mmdetection3d/mmdet3d/apis/__init__.py", line 2, in <module> from .inference import (convert_SyncBN, inference_detector, File "/mnt/sdb/lyq/bevfusion/mmdetection3d/mmdet3d/apis/inference.py", line 18, in <module> from mmdet3d.structures import Box3DMode, Det3DDataSample, get_box_type File "/mnt/sdb/lyq/bevfusion/mmdetection3d/mmdet3d/structures/__init__.py", line 9, in <module> from .ops import (AxisAlignedBboxOverlaps3D, BboxOverlaps3D, File "/mnt/sdb/lyq/bevfusion/mmdetection3d/mmdet3d/structures/ops/__init__.py", line 3, in <module> from .box_np_ops import (box2d_to_corner_jit, box3d_to_bbox, File "/mnt/sdb/lyq/bevfusion/mmdetection3d/mmdet3d/structures/ops/box_np_ops.py", line 7, in <module> import numba File "/mnt/sdb/lyq/anaconda3/envs/bevfusion/lib/python3.8/site-packages/numba/__init__.py", line 43, in <module> from numba.np.ufunc import (vectorize, guvectorize, threading_layer, File "/mnt/sdb/lyq/anaconda3/envs/bevfusion/lib/python3.8/site-packages/numba/np/ufunc/__init__.py", line 3, in <module> from numba.np.ufunc.decorators import Vectorize, GUVectorize, vectorize, guvectorize File "/mnt/sdb/lyq/anaconda3/envs/bevfusion/lib/python3.8/site-packages/numba/np/ufunc/decorators.py", line 3, in <module> from numba.np.ufunc import _internal SystemError: initialization of _internal failed without raising an exception (bevfusion) lyq@ps:/mnt/sdb/lyq/bevfusion/mmdetection3d$
最新发布
11-13
Python Numba库中的@jit装饰器可用于优化Python代码的性能,使其接近C、C++和Fortran的执行速度,而无需更改Python解释器。通过使用@jit装饰器,我们可以将特定的函数或循环转换为机器码,从而提高执行效率。引用 例如,在使用@jit装饰器的情况下,可以将普通的Python函数转换为使用Numba库优化的函数。这样的优化可以通过将函数的参数类型声明为int32或float等来实现,从而提高运行速度。引用和 对于使用@jit装饰器的函数,可以使用%timeit命令来测试其运行时间。在测试中,我们可以看到使用Numba优化的函数在执行时比普通的Python函数更快。引用 总之,Python中的Numba库提供了一种优化代码性能的方法,可以通过将特定函数或循环转换为机器码来实现。这种优化可以显著提高代码的执行速度,使其接近于使用C、C++或Fortran编写的代码。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [提升Python运算效率1 - numba-jit](https://blog.youkuaiyun.com/linkedin_21843693/article/details/125857106)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值