Cupy的使用

关于Cupy中的AttributeError: module 'cupy.cuda' has no attribute 'compile_with_cache'问题

Cupy简介

CuPy 是一个兼容 NumPy/SciPy 的数组库,用于使用 Python 进行 GPU 加速计算。 CuPy 可作为在 NVIDIA CUDA 或 AMD ROCm 平台上运行现有 NumPy/SciPy 代码的直接替代品。

代码实例

论文

Involution: Inverting the Inherence of Convolution for Visual Recognition
链接: link.
该文章中提出了involution操作,相较于传统卷积和自注意力机制操作,involution更具有一般性。该文章在实现该操作过程中,主要使用cupyinvolution进行加速,具体实现过程请参考项目中的involution_cuda.py文件。

代码

自己搭建的虚拟环境使用python包的版本:cupy=13.1.0,torch=2.3,cuda版本为12.2,python=3.10.14

# 原始代码
import cupy

@cupy._util.memoize(for_each_device=True)
def load_kernel(kernel_name, code, **kwargs):
     code = Template(code).substitute(**kwargs)
     kernel_code = cupy.cuda.compile_with_cache(code)
     return kernel_code.get_function(kernel_name)

报错为:

AttributeError: module 'cupy.cuda' has no attribute 'compile_with_cache'

cupy v1.10以上版本对cupy中的cuda进行了修改,cupy.cuda.compile_with_cache() 以及get_function()方法,集成在cupy.RawKernel中,所以修改后的代码为:

# cupy v1.10以上版本
@cupy._util.memoize(for_each_device=True)
def load_kernel(kernel_name, code, **kwargs):
    code = Template(code).substitute(**kwargs)
    cupy_launchr = cupy.RawKernel(code, kernel_name)
    return cupy_launchr

参考文献

改文章的参考链接如下:
Involution: Inverting the Inherence of Convolution for Visual Recognition
involution-pytorch
解决CuPy v10以上版本中cuda.compile_with_cache方法返回为None:‘NoneType‘ object has no attribute ‘get_function‘

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值