简体   繁体   English

使用 Cupy 的 GPU FFT 卷积

[英]GPU FFT Convolution using Cupy

I am attempting to use Cupy to perform a FFT convolution operation on the GPU.我正在尝试使用 Cupy 在 GPU 上执行 FFT 卷积操作。

Using the source code for scipy.signal.fftconvolve, I came up with the following Numpy based function, which works nicely:使用 scipy.signal.fftconvolve 的源代码,我想出了以下基于 Numpy 的函数,它运行良好:

import numpy as np

def FFTConvolve(in1, in2):

    if in1.ndim == in2.ndim == 0:  # scalar inputs
        return in1 * in2
    elif not in1.ndim == in2.ndim:
        raise ValueError("Dimensions do not match.")
    elif in1.size == 0 or in2.size == 0:  # empty arrays
        return array([])

    s1 = np.asarray(in1.shape)
    s2 = np.asarray(in2.shape)

    shape = s1 + s2 - 1

    fsize = 2 ** np.ceil(np.log2(shape)).astype(int) 
    fslice = tuple([slice(0, int(sz)) for sz in shape])

    ret = np.fft.ifft(np.fft.fft(in1, fsize) * np.fft.fft(in2, fsize))[fslice].copy()

    return ret

I naively write the program for Cupy as follows:我天真地为Cupy编写程序如下:

import cupy as cp

def FFTConvolve(in1, in2):

     if in1.ndim == in2.ndim == 0:  # scalar inputs
          return in1 * in2
     elif not in1.ndim == in2.ndim:
          raise ValueError("Dimensions do not match.")
     elif in1.size == 0 or in2.size == 0:  # empty arrays
          return array([])

     in1 = cp.asarray(in1)
     in2 = cp.asarray(in2)

     s1 = cp.asarray(in1.shape)
     s2 = cp.asarray(in2.shape)

     shape = s1 + s2 - 1

     fsize = 2 ** cp.ceil(cp.log2(shape)).astype(int) 
     fslice = tuple([slice(0, int(sz)) for sz in shape])

     ret = cp.fft.ifftn(cp.fft.fftn(in1, fsize) * cp.fft.fftn(in2, fsize))[fslice].copy()

     return ret

The latter gives me the following error, on the line enter code here :后者给了我以下错误,在enter code here

TypeError: 'cupy.core.core.ndarray' object cannot be interpreted as an integer

The documentation for cupy.fft.ftt state that it accepts tuple as an range, but for some reason reads it as a cupy.ndarray. cupy.fft.ftt 的文档声明它接受元组作为范围,但出于某种原因将其读取为cupy.ndarray。

Can someone kindly point me in the right direction?有人可以指出我正确的方向吗?

The solution was to use the cp.asnumpy() command:解决方案是使用cp.asnumpy()命令:

def FFTConvolve(in1, in2):

    if in1.ndim == in2.ndim == 0:  # scalar inputs
        return in1 * in2
    elif not in1.ndim == in2.ndim:
        raise ValueError("Dimensions do not match.")
    elif in1.size == 0 or in2.size == 0:  # empty arrays
        return array([])

    s1 = np.asarray(in1.shape)
    s2 = np.asarray(in2.shape)

    shape = s1 + s2 - 1

    fsize = 2 ** np.ceil(np.log2(shape)).astype(int) 
    fslice = tuple([slice(0, int(sz)) for sz in shape])

    ret = cp.fft.ifftn(cp.fft.fftn(in1, np.asarray(fsize)) * cp.fft.fftn(in2, np.asarray(fsize)))[fslice].copy()
    return ret

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM