关于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更具有一般性。该文章在实现该操作过程中,主要使用cupy 对 involution进行加速,具体实现过程请参考项目中的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‘